source: sasview/src/sas/sascalc/data_util/qsmearing.py @ 392056d

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 392056d was 392056d, checked in by jhbakker, 8 years ago

This version of SESANS integration will be merged to master

  • Property mode set to 100644
File size: 6.2 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
15from sasmodels import sesans
16
17from sasmodels.resolution import Slit1D, Pinhole1D, SESANS1D
18from sasmodels.resolution2d import Pinhole2D
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 resolution smearing data
51    _found_sesans = False
52    if data.dx is not None and data.meta_data['loader']=='SESANS':
53        if data.dx[0] > 0.0:
54            _found_sesans = True
55
56    if _found_sesans == True:
57        return sesans_smear(data, model)
58
59    _found_resolution = False
60    if data.dx is not None and len(data.dx) == len(data.x):
61
62        # Check that we have non-zero data
63        if data.dx[0] > 0.0:
64            _found_resolution = True
65            #print "_found_resolution",_found_resolution
66            #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0]
67    # If we found resolution smearing data, return a QSmearer
68    if _found_resolution == True:
69         return pinhole_smear(data, model)
70
71    # Look for slit smearing data
72    _found_slit = False
73    if data.dxl is not None and len(data.dxl) == len(data.x) \
74        and data.dxw is not None and len(data.dxw) == len(data.x):
75
76        # Check that we have non-zero data
77        if data.dxl[0] > 0.0 or data.dxw[0] > 0.0:
78            _found_slit = True
79
80        # Sanity check: all data should be the same as a function of Q
81        for item in data.dxl:
82            if data.dxl[0] != item:
83                _found_resolution = False
84                break
85
86        for item in data.dxw:
87            if data.dxw[0] != item:
88                _found_resolution = False
89                break
90    # If we found slit smearing data, return a slit smearer
91    if _found_slit == True:
92        return slit_smear(data, model)
93    return None
94
95
96class PySmear(object):
97    """
98    Wrapper for pure python sasmodels resolution functions.
99    """
100    def __init__(self, resolution, model):
101        self.model = model
102        self.resolution = resolution
103        if hasattr(self.resolution, 'data'):
104            if self.resolution.data.meta_data['loader'] == 'SESANS':
105                self.offset = 0
106            # This is default behaviour, for future resolution/transform functions this needs to be revisited.
107            else:
108                self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0])
109        else:
110            self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0])
111
112        #self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0])
113
114    def apply(self, iq_in, first_bin=0, last_bin=None):
115        """
116        Apply the resolution function to the data.
117        Note that this is called with iq_in matching data.x, but with
118        iq_in[first_bin:last_bin] set to theory values for these bins,
119        and the remainder left undefined.  The first_bin, last_bin values
120        should be those returned from get_bin_range.
121        The returned value is of the same length as iq_in, with the range
122        first_bin:last_bin set to the resolution smeared values.
123        """
124        if last_bin is None: last_bin = len(iq_in)
125        start, end = first_bin + self.offset, last_bin + self.offset
126        q_calc = self.resolution.q_calc
127        iq_calc = numpy.empty_like(q_calc)
128        if start > 0:
129            iq_calc[:start] = self.model.evalDistribution(q_calc[:start])
130        if end+1 < len(q_calc):
131            iq_calc[end+1:] = self.model.evalDistribution(q_calc[end+1:])
132        iq_calc[start:end+1] = iq_in[first_bin:last_bin+1]
133        smeared = self.resolution.apply(iq_calc)
134        return smeared
135    __call__ = apply
136
137    def get_bin_range(self, q_min=None, q_max=None):
138        """
139        For a given q_min, q_max, find the corresponding indices in the data.
140        Returns first, last.
141        Note that these are indexes into q from the data, not the q_calc
142        needed by the resolution function.  Note also that these are the
143        indices, not the range limits.  That is, the complete range will be
144        q[first:last+1].
145        """
146
147        q = self.resolution.q
148        first = numpy.searchsorted(q, q_min)
149        last = numpy.searchsorted(q, q_max)
150        return first, min(last,len(q)-1)
151
152def slit_smear(data, model=None):
153    q = data.x
154    width = data.dxw if data.dxw is not None else 0
155    height = data.dxl if data.dxl is not None else 0
156    # TODO: width and height seem to be reversed
157    return PySmear(Slit1D(q, height, width), model)
158
159def pinhole_smear(data, model=None):
160    q = data.x
161    width = data.dx if data.dx is not None else 0
162    return PySmear(Pinhole1D(q, width), model)
163
164def sesans_smear(data, model=None):
165    #This should be calculated characteristic length scale
166    #Probably not a data prameter either
167    #Need function to calculate this based on model
168    #Here assume a number
169    Rmax = 1000000
170    q_calc = sesans.make_q(data.sample.zacceptance, Rmax)
171    return PySmear(SESANS1D(data,q_calc),model)
Note: See TracBrowser for help on using the repository browser.