source: sasview/src/sas/sascalc/data_util/qsmearing.py @ 87b9447

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 87b9447 was 87b9447, checked in by ajj, 8 years ago

first attempt at using hankel transform as resolution function (not working)

  • Property mode set to 100644
File size: 5.8 KB
Line 
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######################################################################
11import numpy
12import math
13import logging
14import sys
15
16from sasmodels.resolution import Slit1D, Pinhole1D
17from sasmodels.resolution2d import Pinhole2D
18from sasmodels import sesans
19
20def smear_selection(data, model = None):
21    """
22    Creates the right type of smearer according
23    to the data.
24    The canSAS format has a rule that either
25    slit smearing data OR resolution smearing data
26    is available.
27
28    For the present purpose, we choose the one that
29    has none-zero data. If both slit and resolution
30    smearing arrays are filled with good data
31    (which should not happen), then we choose the
32    resolution smearing data.
33
34    :param data: Data1D object
35    :param model: sas.model instance
36    """
37    # Sanity check. If we are not dealing with a SAS Data1D
38    # object, just return None
39    if  data.__class__.__name__ not in ['Data1D', 'Theory1D']:
40        if data == None:
41            return None
42        elif data.dqx_data == None or data.dqy_data == None:
43            return None
44        return Pinhole2D(data)
45
46    if  not hasattr(data, "dx") and not hasattr(data, "dxl")\
47         and not hasattr(data, "dxw"):
48        return None
49
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
60    # Look for resolution smearing data
61    _found_resolution = False
62    if data.dx is not None and len(data.dx) == len(data.x):
63
64        # Check that we have non-zero data
65        if data.dx[0] > 0.0:
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:
71         return pinhole_smear(data, model)
72
73    # Look for slit smearing data
74    _found_slit = False
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
78        # Check that we have non-zero data
79        if data.dxl[0] > 0.0 or data.dxw[0] > 0.0:
80            _found_slit = True
81
82        # Sanity check: all data should be the same as a function of Q
83        for item in data.dxl:
84            if data.dxl[0] != item:
85                _found_resolution = False
86                break
87
88        for item in data.dxw:
89            if data.dxw[0] != item:
90                _found_resolution = False
91                break
92    # If we found slit smearing data, return a slit smearer
93    if _found_slit == True:
94        return slit_smear(data, model)
95
96    return None
97
98def 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
113
114class 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
160def 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
167def pinhole_smear(data, model=None):
168    q = data.x
169    width = data.dx if data.dx is not None else 0
170    return PySmear(Pinhole1D(q, width), model)
Note: See TracBrowser for help on using the repository browser.