[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 |
---|
| 15 | |
---|
[f8aa738] | 16 | from sasmodels.resolution import Slit1D, Pinhole1D |
---|
| 17 | from sasmodels.resolution2d import Pinhole2D |
---|
[44c0746] | 18 | from sasmodels import sesans |
---|
[f8aa738] | 19 | |
---|
| 20 | def smear_selection(data, model = None): |
---|
[240a2d2] | 21 | """ |
---|
[f8aa738] | 22 | Creates the right type of smearer according |
---|
[240a2d2] | 23 | to the data. |
---|
| 24 | The canSAS format has a rule that either |
---|
| 25 | slit smearing data OR resolution smearing data |
---|
[f8aa738] | 26 | is available. |
---|
| 27 | |
---|
[240a2d2] | 28 | For the present purpose, we choose the one that |
---|
| 29 | has none-zero data. If both slit and resolution |
---|
[f8aa738] | 30 | smearing arrays are filled with good data |
---|
[240a2d2] | 31 | (which should not happen), then we choose the |
---|
[f8aa738] | 32 | resolution smearing data. |
---|
| 33 | |
---|
| 34 | :param data: Data1D object |
---|
[240a2d2] | 35 | :param model: sas.model instance |
---|
| 36 | """ |
---|
| 37 | # Sanity check. If we are not dealing with a SAS Data1D |
---|
| 38 | # object, just return None |
---|
[f8aa738] | 39 | if data.__class__.__name__ not in ['Data1D', 'Theory1D']: |
---|
| 40 | if data == None: |
---|
[240a2d2] | 41 | return None |
---|
[f8aa738] | 42 | elif data.dqx_data == None or data.dqy_data == None: |
---|
[240a2d2] | 43 | return None |
---|
[f8aa738] | 44 | return Pinhole2D(data) |
---|
| 45 | |
---|
| 46 | if not hasattr(data, "dx") and not hasattr(data, "dxl")\ |
---|
| 47 | and not hasattr(data, "dxw"): |
---|
[240a2d2] | 48 | return None |
---|
[f8aa738] | 49 | |
---|
[87b9447] | 50 | # Look for sesans |
---|
| 51 | _found_sesans = False |
---|
| 52 | if hasattr(data,'lam') : |
---|
| 53 | _found_sesans = True |
---|
| 54 | logging.info("Found SESANS data!!") |
---|
| 55 | |
---|
| 56 | # If we found sesans data, do the necessary jiggery pokery |
---|
| 57 | if _found_sesans == True: |
---|
| 58 | return sesans_smear(data, model) |
---|
| 59 | |
---|
[240a2d2] | 60 | # Look for resolution smearing data |
---|
| 61 | _found_resolution = False |
---|
[f8aa738] | 62 | if data.dx is not None and len(data.dx) == len(data.x): |
---|
| 63 | |
---|
[240a2d2] | 64 | # Check that we have non-zero data |
---|
[f8aa738] | 65 | if data.dx[0] > 0.0: |
---|
[240a2d2] | 66 | _found_resolution = True |
---|
| 67 | #print "_found_resolution",_found_resolution |
---|
| 68 | #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0] |
---|
| 69 | # If we found resolution smearing data, return a QSmearer |
---|
| 70 | if _found_resolution == True: |
---|
[f8aa738] | 71 | return pinhole_smear(data, model) |
---|
[240a2d2] | 72 | |
---|
| 73 | # Look for slit smearing data |
---|
| 74 | _found_slit = False |
---|
[f8aa738] | 75 | if data.dxl is not None and len(data.dxl) == len(data.x) \ |
---|
| 76 | and data.dxw is not None and len(data.dxw) == len(data.x): |
---|
| 77 | |
---|
[240a2d2] | 78 | # Check that we have non-zero data |
---|
[f8aa738] | 79 | if data.dxl[0] > 0.0 or data.dxw[0] > 0.0: |
---|
[240a2d2] | 80 | _found_slit = True |
---|
[f8aa738] | 81 | |
---|
[240a2d2] | 82 | # Sanity check: all data should be the same as a function of Q |
---|
[f8aa738] | 83 | for item in data.dxl: |
---|
| 84 | if data.dxl[0] != item: |
---|
[240a2d2] | 85 | _found_resolution = False |
---|
| 86 | break |
---|
[f8aa738] | 87 | |
---|
| 88 | for item in data.dxw: |
---|
| 89 | if data.dxw[0] != item: |
---|
[240a2d2] | 90 | _found_resolution = False |
---|
| 91 | break |
---|
| 92 | # If we found slit smearing data, return a slit smearer |
---|
| 93 | if _found_slit == True: |
---|
[f8aa738] | 94 | return slit_smear(data, model) |
---|
[44c0746] | 95 | |
---|
[240a2d2] | 96 | return None |
---|
| 97 | |
---|
[44c0746] | 98 | def sesans_smear(data, model=None): |
---|
| 99 | q = sesans.make_q(data.sample.zacceptance, data.Rmax) |
---|
| 100 | index = slice(None, None) |
---|
| 101 | res = None |
---|
| 102 | if data.y is not None: |
---|
| 103 | Iq, dIq = data.y, data.dy |
---|
| 104 | else: |
---|
| 105 | Iq, dIq = None, None |
---|
| 106 | #self._theory = np.zeros_like(q) |
---|
| 107 | q_vectors = [q] |
---|
| 108 | q_mono = sesans.make_all_q(data) |
---|
| 109 | Iq = model.evalDistribution(q_mono) |
---|
| 110 | |
---|
| 111 | return sesans.transform(data, q, Iq, 0, 0) |
---|
| 112 | |
---|
[240a2d2] | 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 |
---|
| 121 | self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
| 122 | |
---|
| 123 | def apply(self, iq_in, first_bin=0, last_bin=None): |
---|
| 124 | """ |
---|
| 125 | Apply the resolution function to the data. |
---|
| 126 | Note that this is called with iq_in matching data.x, but with |
---|
| 127 | iq_in[first_bin:last_bin] set to theory values for these bins, |
---|
| 128 | and the remainder left undefined. The first_bin, last_bin values |
---|
| 129 | should be those returned from get_bin_range. |
---|
| 130 | The returned value is of the same length as iq_in, with the range |
---|
| 131 | first_bin:last_bin set to the resolution smeared values. |
---|
| 132 | """ |
---|
| 133 | if last_bin is None: last_bin = len(iq_in) |
---|
| 134 | start, end = first_bin + self.offset, last_bin + self.offset |
---|
| 135 | q_calc = self.resolution.q_calc |
---|
| 136 | iq_calc = numpy.empty_like(q_calc) |
---|
| 137 | if start > 0: |
---|
| 138 | iq_calc[:start] = self.model.evalDistribution(q_calc[:start]) |
---|
| 139 | if end+1 < len(q_calc): |
---|
| 140 | iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:]) |
---|
| 141 | iq_calc[start:end+1] = iq_in[first_bin:last_bin+1] |
---|
| 142 | smeared = self.resolution.apply(iq_calc) |
---|
| 143 | return smeared |
---|
| 144 | __call__ = apply |
---|
| 145 | |
---|
| 146 | def get_bin_range(self, q_min=None, q_max=None): |
---|
| 147 | """ |
---|
| 148 | For a given q_min, q_max, find the corresponding indices in the data. |
---|
| 149 | Returns first, last. |
---|
| 150 | Note that these are indexes into q from the data, not the q_calc |
---|
| 151 | needed by the resolution function. Note also that these are the |
---|
| 152 | indices, not the range limits. That is, the complete range will be |
---|
| 153 | q[first:last+1]. |
---|
| 154 | """ |
---|
| 155 | q = self.resolution.q |
---|
| 156 | first = numpy.searchsorted(q, q_min) |
---|
| 157 | last = numpy.searchsorted(q, q_max) |
---|
| 158 | return first, min(last,len(q)-1) |
---|
| 159 | |
---|
| 160 | def slit_smear(data, model=None): |
---|
| 161 | q = data.x |
---|
| 162 | width = data.dxw if data.dxw is not None else 0 |
---|
| 163 | height = data.dxl if data.dxl is not None else 0 |
---|
| 164 | # TODO: width and height seem to be reversed |
---|
| 165 | return PySmear(Slit1D(q, height, width), model) |
---|
| 166 | |
---|
| 167 | def pinhole_smear(data, model=None): |
---|
| 168 | q = data.x |
---|
| 169 | width = data.dx if data.dx is not None else 0 |
---|
[44c0746] | 170 | return PySmear(Pinhole1D(q, width), model) |
---|