[240a2d2] | 1 | """ |
---|
| 2 | Handle Q smearing |
---|
| 3 | """ |
---|
| 4 | ##################################################################### |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[44c0746] | 7 | #project funded by the US National Science Foundation. |
---|
[240a2d2] | 8 | #See the license text in license.txt |
---|
| 9 | #copyright 2008, University of Tennessee |
---|
| 10 | ###################################################################### |
---|
| 11 | import numpy |
---|
| 12 | import math |
---|
| 13 | import logging |
---|
| 14 | import sys |
---|
[1fac6c0] | 15 | from sasmodels import sesans |
---|
[240a2d2] | 16 | |
---|
[4581ac9] | 17 | import numpy as np # type: ignore |
---|
| 18 | from numpy import pi, exp # type: ignore |
---|
| 19 | from scipy.special import jv as besselj |
---|
| 20 | |
---|
[f7bc948] | 21 | from sasmodels.resolution import Slit1D, Pinhole1D, SESANS1D |
---|
[f8aa738] | 22 | from sasmodels.resolution2d import Pinhole2D |
---|
[4581ac9] | 23 | from src.sas.sascalc.data_util.nxsunit import Converter |
---|
| 24 | |
---|
[f8aa738] | 25 | |
---|
| 26 | def smear_selection(data, model = None): |
---|
[240a2d2] | 27 | """ |
---|
[f8aa738] | 28 | Creates the right type of smearer according |
---|
[240a2d2] | 29 | to the data. |
---|
| 30 | The canSAS format has a rule that either |
---|
| 31 | slit smearing data OR resolution smearing data |
---|
[f8aa738] | 32 | is available. |
---|
| 33 | |
---|
[240a2d2] | 34 | For the present purpose, we choose the one that |
---|
| 35 | has none-zero data. If both slit and resolution |
---|
[f8aa738] | 36 | smearing arrays are filled with good data |
---|
[240a2d2] | 37 | (which should not happen), then we choose the |
---|
[f8aa738] | 38 | resolution smearing data. |
---|
| 39 | |
---|
| 40 | :param data: Data1D object |
---|
[240a2d2] | 41 | :param model: sas.model instance |
---|
| 42 | """ |
---|
| 43 | # Sanity check. If we are not dealing with a SAS Data1D |
---|
| 44 | # object, just return None |
---|
[4581ac9] | 45 | |
---|
| 46 | # This checks for 2D data (does not throw exception because fail is common) |
---|
[f8aa738] | 47 | if data.__class__.__name__ not in ['Data1D', 'Theory1D']: |
---|
| 48 | if data == None: |
---|
[240a2d2] | 49 | return None |
---|
[f8aa738] | 50 | elif data.dqx_data == None or data.dqy_data == None: |
---|
[240a2d2] | 51 | return None |
---|
[f8aa738] | 52 | return Pinhole2D(data) |
---|
[4581ac9] | 53 | # This checks for 1D data with smearing info in the data itself (again, fail is likely; no exceptions) |
---|
[f8aa738] | 54 | if not hasattr(data, "dx") and not hasattr(data, "dxl")\ |
---|
| 55 | and not hasattr(data, "dxw"): |
---|
[240a2d2] | 56 | return None |
---|
[f8aa738] | 57 | |
---|
[f7bc948] | 58 | # Look for resolution smearing data |
---|
[4581ac9] | 59 | # This is the code that checks for SESANS data; it looks for the file loader |
---|
| 60 | # TODO: change other sanity checks to check for file loader instead of data structure? |
---|
[87b9447] | 61 | _found_sesans = False |
---|
[1fac6c0] | 62 | if data.dx is not None and data.meta_data['loader']=='SESANS': |
---|
[f7bc948] | 63 | if data.dx[0] > 0.0: |
---|
| 64 | _found_sesans = True |
---|
[87b9447] | 65 | |
---|
| 66 | if _found_sesans == True: |
---|
[4581ac9] | 67 | #Pre-computing the Hankel matrix |
---|
| 68 | Rmax = 1000000 |
---|
| 69 | q_calc = sesans.make_q(data.sample.zacceptance, Rmax) |
---|
| 70 | SElength = Converter(data._xunit)(data.x, "A") |
---|
| 71 | dq = q_calc[1] - q_calc[0] |
---|
| 72 | H0 = dq / (2 * pi) * q_calc |
---|
| 73 | H = dq / (2 * pi) * besselj(0, np.outer(q_calc, SElength)) |
---|
| 74 | |
---|
| 75 | return PySmear(SESANS1D(data, H0, H, q_calc), model) |
---|
[87b9447] | 76 | |
---|
[240a2d2] | 77 | _found_resolution = False |
---|
[f8aa738] | 78 | if data.dx is not None and len(data.dx) == len(data.x): |
---|
| 79 | |
---|
[240a2d2] | 80 | # Check that we have non-zero data |
---|
[f8aa738] | 81 | if data.dx[0] > 0.0: |
---|
[240a2d2] | 82 | _found_resolution = True |
---|
| 83 | #print "_found_resolution",_found_resolution |
---|
| 84 | #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0] |
---|
| 85 | # If we found resolution smearing data, return a QSmearer |
---|
| 86 | if _found_resolution == True: |
---|
[f8aa738] | 87 | return pinhole_smear(data, model) |
---|
[240a2d2] | 88 | |
---|
| 89 | # Look for slit smearing data |
---|
| 90 | _found_slit = False |
---|
[f8aa738] | 91 | if data.dxl is not None and len(data.dxl) == len(data.x) \ |
---|
| 92 | and data.dxw is not None and len(data.dxw) == len(data.x): |
---|
| 93 | |
---|
[240a2d2] | 94 | # Check that we have non-zero data |
---|
[f8aa738] | 95 | if data.dxl[0] > 0.0 or data.dxw[0] > 0.0: |
---|
[240a2d2] | 96 | _found_slit = True |
---|
[f8aa738] | 97 | |
---|
[240a2d2] | 98 | # Sanity check: all data should be the same as a function of Q |
---|
[f8aa738] | 99 | for item in data.dxl: |
---|
| 100 | if data.dxl[0] != item: |
---|
[240a2d2] | 101 | _found_resolution = False |
---|
| 102 | break |
---|
[f8aa738] | 103 | |
---|
| 104 | for item in data.dxw: |
---|
| 105 | if data.dxw[0] != item: |
---|
[240a2d2] | 106 | _found_resolution = False |
---|
| 107 | break |
---|
| 108 | # If we found slit smearing data, return a slit smearer |
---|
| 109 | if _found_slit == True: |
---|
[f8aa738] | 110 | return slit_smear(data, model) |
---|
[240a2d2] | 111 | return None |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | class PySmear(object): |
---|
| 115 | """ |
---|
| 116 | Wrapper for pure python sasmodels resolution functions. |
---|
| 117 | """ |
---|
| 118 | def __init__(self, resolution, model): |
---|
| 119 | self.model = model |
---|
| 120 | self.resolution = resolution |
---|
[1fac6c0] | 121 | if hasattr(self.resolution, 'data'): |
---|
| 122 | if self.resolution.data.meta_data['loader'] == 'SESANS': |
---|
| 123 | self.offset = 0 |
---|
| 124 | # This is default behaviour, for future resolution/transform functions this needs to be revisited. |
---|
| 125 | else: |
---|
| 126 | self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
| 127 | else: |
---|
| 128 | self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
| 129 | |
---|
| 130 | #self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
[240a2d2] | 131 | |
---|
| 132 | def apply(self, iq_in, first_bin=0, last_bin=None): |
---|
| 133 | """ |
---|
| 134 | Apply the resolution function to the data. |
---|
| 135 | Note that this is called with iq_in matching data.x, but with |
---|
| 136 | iq_in[first_bin:last_bin] set to theory values for these bins, |
---|
| 137 | and the remainder left undefined. The first_bin, last_bin values |
---|
| 138 | should be those returned from get_bin_range. |
---|
| 139 | The returned value is of the same length as iq_in, with the range |
---|
| 140 | first_bin:last_bin set to the resolution smeared values. |
---|
| 141 | """ |
---|
| 142 | if last_bin is None: last_bin = len(iq_in) |
---|
| 143 | start, end = first_bin + self.offset, last_bin + self.offset |
---|
| 144 | q_calc = self.resolution.q_calc |
---|
| 145 | iq_calc = numpy.empty_like(q_calc) |
---|
| 146 | if start > 0: |
---|
| 147 | iq_calc[:start] = self.model.evalDistribution(q_calc[:start]) |
---|
| 148 | if end+1 < len(q_calc): |
---|
| 149 | iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:]) |
---|
| 150 | iq_calc[start:end+1] = iq_in[first_bin:last_bin+1] |
---|
| 151 | smeared = self.resolution.apply(iq_calc) |
---|
| 152 | return smeared |
---|
| 153 | __call__ = apply |
---|
| 154 | |
---|
| 155 | def get_bin_range(self, q_min=None, q_max=None): |
---|
| 156 | """ |
---|
| 157 | For a given q_min, q_max, find the corresponding indices in the data. |
---|
| 158 | Returns first, last. |
---|
| 159 | Note that these are indexes into q from the data, not the q_calc |
---|
| 160 | needed by the resolution function. Note also that these are the |
---|
| 161 | indices, not the range limits. That is, the complete range will be |
---|
| 162 | q[first:last+1]. |
---|
| 163 | """ |
---|
[1fac6c0] | 164 | |
---|
[240a2d2] | 165 | q = self.resolution.q |
---|
| 166 | first = numpy.searchsorted(q, q_min) |
---|
| 167 | last = numpy.searchsorted(q, q_max) |
---|
| 168 | return first, min(last,len(q)-1) |
---|
| 169 | |
---|
| 170 | def slit_smear(data, model=None): |
---|
| 171 | q = data.x |
---|
| 172 | width = data.dxw if data.dxw is not None else 0 |
---|
| 173 | height = data.dxl if data.dxl is not None else 0 |
---|
| 174 | # TODO: width and height seem to be reversed |
---|
| 175 | return PySmear(Slit1D(q, height, width), model) |
---|
| 176 | |
---|
| 177 | def pinhole_smear(data, model=None): |
---|
| 178 | q = data.x |
---|
| 179 | width = data.dx if data.dx is not None else 0 |
---|
[44c0746] | 180 | return PySmear(Pinhole1D(q, width), model) |
---|
[f7bc948] | 181 | |
---|
| 182 | def sesans_smear(data, model=None): |
---|
| 183 | #This should be calculated characteristic length scale |
---|
| 184 | #Probably not a data prameter either |
---|
| 185 | #Need function to calculate this based on model |
---|
| 186 | #Here assume a number |
---|
[392056d] | 187 | Rmax = 1000000 |
---|
[1fac6c0] | 188 | q_calc = sesans.make_q(data.sample.zacceptance, Rmax) |
---|
[4581ac9] | 189 | SElength=Converter(data._xunit)(data.x, "A") |
---|
| 190 | #return sesans.HankelTransform(q_calc, SElength) |
---|
| 191 | #Old return statement, running through the smearer |
---|
| 192 | #return PySmear(SESANS1D(data,q_calc),model) |
---|