[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 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 |
---|
| 18 | |
---|
| 19 | def smear_selection(data, model = None): |
---|
[240a2d2] | 20 | """ |
---|
[f8aa738] | 21 | Creates the right type of smearer according |
---|
[240a2d2] | 22 | to the data. |
---|
| 23 | The canSAS format has a rule that either |
---|
| 24 | slit smearing data OR resolution smearing data |
---|
[f8aa738] | 25 | is available. |
---|
| 26 | |
---|
[240a2d2] | 27 | For the present purpose, we choose the one that |
---|
| 28 | has none-zero data. If both slit and resolution |
---|
[f8aa738] | 29 | smearing arrays are filled with good data |
---|
[240a2d2] | 30 | (which should not happen), then we choose the |
---|
[f8aa738] | 31 | resolution smearing data. |
---|
| 32 | |
---|
| 33 | :param data: Data1D object |
---|
[240a2d2] | 34 | :param model: sas.model instance |
---|
| 35 | """ |
---|
| 36 | # Sanity check. If we are not dealing with a SAS Data1D |
---|
| 37 | # object, just return None |
---|
[f8aa738] | 38 | if data.__class__.__name__ not in ['Data1D', 'Theory1D']: |
---|
| 39 | if data == None: |
---|
[240a2d2] | 40 | return None |
---|
[f8aa738] | 41 | elif data.dqx_data == None or data.dqy_data == None: |
---|
[240a2d2] | 42 | return None |
---|
[f8aa738] | 43 | return Pinhole2D(data) |
---|
| 44 | |
---|
| 45 | if not hasattr(data, "dx") and not hasattr(data, "dxl")\ |
---|
| 46 | and not hasattr(data, "dxw"): |
---|
[240a2d2] | 47 | return None |
---|
[f8aa738] | 48 | |
---|
[240a2d2] | 49 | # Look for resolution smearing data |
---|
| 50 | _found_resolution = False |
---|
[f8aa738] | 51 | if data.dx is not None and len(data.dx) == len(data.x): |
---|
| 52 | |
---|
[240a2d2] | 53 | # Check that we have non-zero data |
---|
[f8aa738] | 54 | if data.dx[0] > 0.0: |
---|
[240a2d2] | 55 | _found_resolution = True |
---|
| 56 | #print "_found_resolution",_found_resolution |
---|
| 57 | #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0] |
---|
| 58 | # If we found resolution smearing data, return a QSmearer |
---|
| 59 | if _found_resolution == True: |
---|
[f8aa738] | 60 | return pinhole_smear(data, model) |
---|
[240a2d2] | 61 | |
---|
| 62 | # Look for slit smearing data |
---|
| 63 | _found_slit = False |
---|
[f8aa738] | 64 | if data.dxl is not None and len(data.dxl) == len(data.x) \ |
---|
| 65 | and data.dxw is not None and len(data.dxw) == len(data.x): |
---|
| 66 | |
---|
[240a2d2] | 67 | # Check that we have non-zero data |
---|
[f8aa738] | 68 | if data.dxl[0] > 0.0 or data.dxw[0] > 0.0: |
---|
[240a2d2] | 69 | _found_slit = True |
---|
[f8aa738] | 70 | |
---|
[240a2d2] | 71 | # Sanity check: all data should be the same as a function of Q |
---|
[f8aa738] | 72 | for item in data.dxl: |
---|
| 73 | if data.dxl[0] != item: |
---|
[240a2d2] | 74 | _found_resolution = False |
---|
| 75 | break |
---|
[f8aa738] | 76 | |
---|
| 77 | for item in data.dxw: |
---|
| 78 | if data.dxw[0] != item: |
---|
[240a2d2] | 79 | _found_resolution = False |
---|
| 80 | break |
---|
| 81 | # If we found slit smearing data, return a slit smearer |
---|
| 82 | if _found_slit == True: |
---|
[f8aa738] | 83 | return slit_smear(data, model) |
---|
[240a2d2] | 84 | return None |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | class PySmear(object): |
---|
| 88 | """ |
---|
| 89 | Wrapper for pure python sasmodels resolution functions. |
---|
| 90 | """ |
---|
| 91 | def __init__(self, resolution, model): |
---|
| 92 | self.model = model |
---|
| 93 | self.resolution = resolution |
---|
| 94 | self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) |
---|
| 95 | |
---|
| 96 | def apply(self, iq_in, first_bin=0, last_bin=None): |
---|
| 97 | """ |
---|
| 98 | Apply the resolution function to the data. |
---|
| 99 | Note that this is called with iq_in matching data.x, but with |
---|
| 100 | iq_in[first_bin:last_bin] set to theory values for these bins, |
---|
| 101 | and the remainder left undefined. The first_bin, last_bin values |
---|
| 102 | should be those returned from get_bin_range. |
---|
| 103 | The returned value is of the same length as iq_in, with the range |
---|
| 104 | first_bin:last_bin set to the resolution smeared values. |
---|
| 105 | """ |
---|
| 106 | if last_bin is None: last_bin = len(iq_in) |
---|
| 107 | start, end = first_bin + self.offset, last_bin + self.offset |
---|
| 108 | q_calc = self.resolution.q_calc |
---|
| 109 | iq_calc = numpy.empty_like(q_calc) |
---|
| 110 | if start > 0: |
---|
| 111 | iq_calc[:start] = self.model.evalDistribution(q_calc[:start]) |
---|
| 112 | if end+1 < len(q_calc): |
---|
| 113 | iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:]) |
---|
| 114 | iq_calc[start:end+1] = iq_in[first_bin:last_bin+1] |
---|
| 115 | smeared = self.resolution.apply(iq_calc) |
---|
| 116 | return smeared |
---|
| 117 | __call__ = apply |
---|
| 118 | |
---|
| 119 | def get_bin_range(self, q_min=None, q_max=None): |
---|
| 120 | """ |
---|
| 121 | For a given q_min, q_max, find the corresponding indices in the data. |
---|
| 122 | Returns first, last. |
---|
| 123 | Note that these are indexes into q from the data, not the q_calc |
---|
| 124 | needed by the resolution function. Note also that these are the |
---|
| 125 | indices, not the range limits. That is, the complete range will be |
---|
| 126 | q[first:last+1]. |
---|
| 127 | """ |
---|
| 128 | q = self.resolution.q |
---|
| 129 | first = numpy.searchsorted(q, q_min) |
---|
| 130 | last = numpy.searchsorted(q, q_max) |
---|
| 131 | return first, min(last,len(q)-1) |
---|
| 132 | |
---|
| 133 | def slit_smear(data, model=None): |
---|
| 134 | q = data.x |
---|
| 135 | width = data.dxw if data.dxw is not None else 0 |
---|
| 136 | height = data.dxl if data.dxl is not None else 0 |
---|
| 137 | # TODO: width and height seem to be reversed |
---|
| 138 | return PySmear(Slit1D(q, height, width), model) |
---|
| 139 | |
---|
| 140 | def pinhole_smear(data, model=None): |
---|
| 141 | q = data.x |
---|
| 142 | width = data.dx if data.dx is not None else 0 |
---|
[f8aa738] | 143 | return PySmear(Pinhole1D(q, width), model) |
---|