[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) |
---|
| 7 | #project funded by the US National Science Foundation. |
---|
| 8 | #See the license text in license.txt |
---|
| 9 | #copyright 2008, University of Tennessee |
---|
| 10 | ###################################################################### |
---|
| 11 | import math |
---|
| 12 | import logging |
---|
| 13 | import sys |
---|
[775e0b7] | 14 | |
---|
[a9f579c] | 15 | import numpy as np # type: ignore |
---|
| 16 | from numpy import pi, exp # type:ignore |
---|
[775e0b7] | 17 | |
---|
[f8aa738] | 18 | from sasmodels.resolution import Slit1D, Pinhole1D |
---|
[157b716] | 19 | from sasmodels.sesans import SesansTransform |
---|
[f8aa738] | 20 | from sasmodels.resolution2d import Pinhole2D |
---|
[775e0b7] | 21 | from .nxsunit import Converter |
---|
[f8aa738] | 22 | |
---|
| 23 | def smear_selection(data, model = None): |
---|
[240a2d2] | 24 | """ |
---|
[f8aa738] | 25 | Creates the right type of smearer according |
---|
[240a2d2] | 26 | to the data. |
---|
| 27 | The canSAS format has a rule that either |
---|
| 28 | slit smearing data OR resolution smearing data |
---|
[f8aa738] | 29 | is available. |
---|
| 30 | |
---|
[240a2d2] | 31 | For the present purpose, we choose the one that |
---|
| 32 | has none-zero data. If both slit and resolution |
---|
[f8aa738] | 33 | smearing arrays are filled with good data |
---|
[240a2d2] | 34 | (which should not happen), then we choose the |
---|
[f8aa738] | 35 | resolution smearing data. |
---|
| 36 | |
---|
| 37 | :param data: Data1D object |
---|
[240a2d2] | 38 | :param model: sas.model instance |
---|
| 39 | """ |
---|
| 40 | # Sanity check. If we are not dealing with a SAS Data1D |
---|
| 41 | # object, just return None |
---|
[a9f579c] | 42 | # This checks for 2D data (does not throw exception because fail is common) |
---|
[f8aa738] | 43 | if data.__class__.__name__ not in ['Data1D', 'Theory1D']: |
---|
| 44 | if data == None: |
---|
[240a2d2] | 45 | return None |
---|
[f8aa738] | 46 | elif data.dqx_data == None or data.dqy_data == None: |
---|
[240a2d2] | 47 | return None |
---|
[2ffe241] | 48 | return PySmear2D(data) |
---|
[a9f579c] | 49 | # This checks for 1D data with smearing info in the data itself (again, fail is likely; no exceptions) |
---|
[f8aa738] | 50 | if not hasattr(data, "dx") and not hasattr(data, "dxl")\ |
---|
| 51 | and not hasattr(data, "dxw"): |
---|
[240a2d2] | 52 | return None |
---|
[f8aa738] | 53 | |
---|
[240a2d2] | 54 | # Look for resolution smearing data |
---|
[a9f579c] | 55 | # This is the code that checks for SESANS data; it looks for the file loader |
---|
| 56 | # TODO: change other sanity checks to check for file loader instead of data structure? |
---|
| 57 | _found_sesans = False |
---|
| 58 | #if data.dx is not None and data.meta_data['loader']=='SESANS': |
---|
| 59 | if data.dx is not None and data.isSesans: |
---|
| 60 | #if data.dx[0] > 0.0: |
---|
[9a5097c] | 61 | if np.size(data.dx[data.dx <= 0]) == 0: |
---|
[a9f579c] | 62 | _found_sesans = True |
---|
| 63 | # if data.dx[0] <= 0.0: |
---|
[9a5097c] | 64 | if np.size(data.dx[data.dx <= 0]) > 0: |
---|
[a9f579c] | 65 | raise ValueError('one or more of your dx values are negative, please check the data file!') |
---|
| 66 | |
---|
| 67 | if _found_sesans == True: |
---|
| 68 | #Pre-compute the Hankel matrix (H) |
---|
| 69 | qmax, qunits = data.sample.zacceptance |
---|
[157b716] | 70 | SElength = Converter(data._xunit)(data.x, "A") |
---|
| 71 | zaccept = Converter(qunits)(qmax, "1/A"), |
---|
| 72 | Rmax = 10000000 |
---|
| 73 | hankel = SesansTransform(data.x, SElength, zaccept, Rmax) |
---|
[a9f579c] | 74 | # Then return the actual transform, as if it were a smearing function |
---|
[157b716] | 75 | return PySmear(hankel, model, offset=0) |
---|
[a9f579c] | 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 | """ |
---|
[157b716] | 118 | def __init__(self, resolution, model, offset=None): |
---|
[240a2d2] | 119 | self.model = model |
---|
| 120 | self.resolution = resolution |
---|
[157b716] | 121 | if offset is None: |
---|
[9a5097c] | 122 | offset = np.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
[157b716] | 123 | self.offset = offset |
---|
[240a2d2] | 124 | |
---|
| 125 | def apply(self, iq_in, first_bin=0, last_bin=None): |
---|
| 126 | """ |
---|
| 127 | Apply the resolution function to the data. |
---|
| 128 | Note that this is called with iq_in matching data.x, but with |
---|
| 129 | iq_in[first_bin:last_bin] set to theory values for these bins, |
---|
| 130 | and the remainder left undefined. The first_bin, last_bin values |
---|
| 131 | should be those returned from get_bin_range. |
---|
| 132 | The returned value is of the same length as iq_in, with the range |
---|
| 133 | first_bin:last_bin set to the resolution smeared values. |
---|
| 134 | """ |
---|
| 135 | if last_bin is None: last_bin = len(iq_in) |
---|
| 136 | start, end = first_bin + self.offset, last_bin + self.offset |
---|
| 137 | q_calc = self.resolution.q_calc |
---|
[9a5097c] | 138 | iq_calc = np.empty_like(q_calc) |
---|
[240a2d2] | 139 | if start > 0: |
---|
| 140 | iq_calc[:start] = self.model.evalDistribution(q_calc[:start]) |
---|
| 141 | if end+1 < len(q_calc): |
---|
| 142 | iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:]) |
---|
| 143 | iq_calc[start:end+1] = iq_in[first_bin:last_bin+1] |
---|
| 144 | smeared = self.resolution.apply(iq_calc) |
---|
| 145 | return smeared |
---|
| 146 | __call__ = apply |
---|
| 147 | |
---|
| 148 | def get_bin_range(self, q_min=None, q_max=None): |
---|
| 149 | """ |
---|
| 150 | For a given q_min, q_max, find the corresponding indices in the data. |
---|
| 151 | Returns first, last. |
---|
| 152 | Note that these are indexes into q from the data, not the q_calc |
---|
| 153 | needed by the resolution function. Note also that these are the |
---|
| 154 | indices, not the range limits. That is, the complete range will be |
---|
| 155 | q[first:last+1]. |
---|
| 156 | """ |
---|
| 157 | q = self.resolution.q |
---|
[9a5097c] | 158 | first = np.searchsorted(q, q_min) |
---|
| 159 | last = np.searchsorted(q, q_max) |
---|
[240a2d2] | 160 | return first, min(last,len(q)-1) |
---|
| 161 | |
---|
| 162 | def slit_smear(data, model=None): |
---|
| 163 | q = data.x |
---|
| 164 | width = data.dxw if data.dxw is not None else 0 |
---|
| 165 | height = data.dxl if data.dxl is not None else 0 |
---|
| 166 | # TODO: width and height seem to be reversed |
---|
| 167 | return PySmear(Slit1D(q, height, width), model) |
---|
| 168 | |
---|
| 169 | def pinhole_smear(data, model=None): |
---|
| 170 | q = data.x |
---|
| 171 | width = data.dx if data.dx is not None else 0 |
---|
[d3911e3] | 172 | return PySmear(Pinhole1D(q, width), model) |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | class PySmear2D(object): |
---|
| 176 | """ |
---|
| 177 | Q smearing class for SAS 2d pinhole data |
---|
| 178 | """ |
---|
| 179 | |
---|
| 180 | def __init__(self, data=None, model=None): |
---|
| 181 | self.data = data |
---|
| 182 | self.model = model |
---|
| 183 | self.accuracy = 'Low' |
---|
| 184 | self.limit = 3.0 |
---|
| 185 | self.index = None |
---|
| 186 | self.coords = 'polar' |
---|
| 187 | self.smearer = True |
---|
| 188 | |
---|
| 189 | def set_accuracy(self, accuracy='Low'): |
---|
| 190 | """ |
---|
| 191 | Set accuracy. |
---|
| 192 | |
---|
| 193 | :param accuracy: string |
---|
| 194 | """ |
---|
| 195 | self.accuracy = accuracy |
---|
| 196 | |
---|
| 197 | def set_smearer(self, smearer=True): |
---|
| 198 | """ |
---|
| 199 | Set whether or not smearer will be used |
---|
| 200 | |
---|
| 201 | :param smearer: smear object |
---|
| 202 | |
---|
| 203 | """ |
---|
| 204 | self.smearer = smearer |
---|
| 205 | |
---|
| 206 | def set_data(self, data=None): |
---|
| 207 | """ |
---|
| 208 | Set data. |
---|
| 209 | |
---|
| 210 | :param data: DataLoader.Data_info type |
---|
| 211 | """ |
---|
| 212 | self.data = data |
---|
| 213 | |
---|
| 214 | def set_model(self, model=None): |
---|
| 215 | """ |
---|
| 216 | Set model. |
---|
| 217 | |
---|
| 218 | :param model: sas.models instance |
---|
| 219 | """ |
---|
| 220 | self.model = model |
---|
| 221 | |
---|
| 222 | def set_index(self, index=None): |
---|
| 223 | """ |
---|
| 224 | Set index. |
---|
| 225 | |
---|
| 226 | :param index: 1d arrays |
---|
| 227 | """ |
---|
| 228 | self.index = index |
---|
| 229 | |
---|
| 230 | def get_value(self): |
---|
| 231 | """ |
---|
| 232 | Over sampling of r_nbins times phi_nbins, calculate Gaussian weights, |
---|
| 233 | then find smeared intensity |
---|
| 234 | """ |
---|
| 235 | if self.smearer: |
---|
| 236 | res = Pinhole2D(data=self.data, index=self.index, |
---|
| 237 | nsigma=3.0, accuracy=self.accuracy, |
---|
| 238 | coords=self.coords) |
---|
| 239 | val = self.model.evalDistribution(res.q_calc) |
---|
| 240 | return res.apply(val) |
---|
| 241 | else: |
---|
| 242 | index = self.index if self.index is not None else slice(None) |
---|
| 243 | qx_data = self.data.qx_data[index] |
---|
| 244 | qy_data = self.data.qy_data[index] |
---|
| 245 | q_calc = [qx_data, qy_data] |
---|
| 246 | val = self.model.evalDistribution(q_calc) |
---|
| 247 | return val |
---|
| 248 | |
---|