source: sasview/src/sas/sascalc/data_util/qsmearing.py @ 8cb0692

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 8cb0692 was 8cb0692, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

use resolution functions from sasmodels

  • Property mode set to 100755
File size: 5.3 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
18
19def smear_selection(data, model = None):
20    """
21    Creates the right type of smearer according
22    to the data.
23
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 resolution smearing data
51    _found_resolution = False
52    if data.dx is not None and len(data.dx) == len(data.x):
53       
54        # Check that we have non-zero data
55        if data.dx[0] > 0.0:
56            _found_resolution = True
57            #print "_found_resolution",_found_resolution
58            #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0]
59    # If we found resolution smearing data, return a QSmearer
60    if _found_resolution == True:
61         return pinhole_smear(data, model)
62
63    # Look for slit smearing data
64    _found_slit = False
65    if data.dxl is not None and len(data.dxl) == len(data.x) \
66        and data.dxw is not None and len(data.dxw) == len(data.x):
67       
68        # Check that we have non-zero data
69        if data.dxl[0] > 0.0 or data.dxw[0] > 0.0:
70            _found_slit = True
71       
72        # Sanity check: all data should be the same as a function of Q
73        for item in data.dxl:
74            if data.dxl[0] != item:
75                _found_resolution = False
76                break
77           
78        for item in data.dxw:
79            if data.dxw[0] != item:
80                _found_resolution = False
81                break
82    # If we found slit smearing data, return a slit smearer
83    if _found_slit == True:
84        return slit_smear(data, model)
85    return None
86
87
88class PySmear(object):
89    """
90    Wrapper for pure python sasmodels resolution functions.
91    """
92    def __init__(self, resolution, model):
93        self.model = model
94        self.resolution = resolution
95        self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0])
96
97    def apply(self, iq_in, first_bin=0, last_bin=None):
98        """
99        Apply the resolution function to the data.
100
101        Note that this is called with iq_in matching data.x, but with
102        iq_in[first_bin:last_bin] set to theory values for these bins,
103        and the remainder left undefined.  The first_bin, last_bin values
104        should be those returned from get_bin_range.
105
106        The returned value is of the same length as iq_in, with the range
107        first_bin:last_bin set to the resolution smeared values.
108        """
109        if last_bin is None: last_bin = len(iq_in)
110        start, end = first_bin + self.offset, last_bin + self.offset
111        q_calc = self.resolution.q_calc
112        iq_calc = numpy.empty_like(q_calc)
113        if start > 0:
114            iq_calc[:start] = self.model.evalDistribution(q_calc[:start])
115        if end+1 < len(q_calc):
116            iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:])
117        iq_calc[start:end+1] = iq_in[first_bin:last_bin+1]
118        smeared = self.resolution.apply(iq_calc)
119        return smeared
120    __call__ = apply
121
122    def get_bin_range(self, q_min=None, q_max=None):
123        """
124        For a given q_min, q_max, find the corresponding indices in the data.
125
126        Returns first, last.
127
128        Note that these are indexes into q from the data, not the q_calc
129        needed by the resolution function.  Note also that these are the
130        indices, not the range limits.  That is, the complete range will be
131        q[first:last+1].
132        """
133        q = self.resolution.q
134        first = numpy.searchsorted(q, q_min)
135        last = numpy.searchsorted(q, q_max)
136        return first, min(last,len(q)-1)
137
138def slit_smear(data, model=None):
139    q = data.x
140    width = data.dxw if data.dxw is not None else 0
141    height = data.dxl if data.dxl is not None else 0
142    # TODO: width and height seem to be reversed
143    return PySmear(Slit1D(q, height, width), model)
144
145def pinhole_smear(data, model=None):
146    q = data.x
147    width = data.dx if data.dx is not None else 0
148    return PySmear(Pinhole1D(q, width), model)
Note: See TracBrowser for help on using the repository browser.