Changeset 8fa3fb8 in sasview for src/sas/sascalc/data_util


Ignore:
Timestamp:
Mar 1, 2017 9:46:54 AM (8 years ago)
Author:
butler
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
a97aebd
Parents:
cb1e9a5 (diff), 775e0b7 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'origin/master' into expandable-de-688

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/data_util/qsmearing.py

    rf8aa738 r775e0b7  
    1414import sys 
    1515 
     16import numpy as np  # type: ignore 
     17from numpy import pi, exp # type:ignore 
     18 
    1619from sasmodels.resolution import Slit1D, Pinhole1D 
     20from sasmodels.sesans import SesansTransform 
    1721from sasmodels.resolution2d import Pinhole2D 
     22from .nxsunit import Converter 
    1823 
    1924def smear_selection(data, model = None): 
     
    3641    # Sanity check. If we are not dealing with a SAS Data1D 
    3742    # object, just return None 
     43    # This checks for 2D data (does not throw exception because fail is common) 
    3844    if  data.__class__.__name__ not in ['Data1D', 'Theory1D']: 
    3945        if data == None: 
     
    4147        elif data.dqx_data == None or data.dqy_data == None: 
    4248            return None 
    43         return Pinhole2D(data) 
    44  
     49        return PySmear2D(data) 
     50    # This checks for 1D data with smearing info in the data itself (again, fail is likely; no exceptions) 
    4551    if  not hasattr(data, "dx") and not hasattr(data, "dxl")\ 
    4652         and not hasattr(data, "dxw"): 
     
    4854 
    4955    # Look for resolution smearing data 
     56    # This is the code that checks for SESANS data; it looks for the file loader 
     57    # TODO: change other sanity checks to check for file loader instead of data structure? 
     58    _found_sesans = False 
     59    #if data.dx is not None and data.meta_data['loader']=='SESANS': 
     60    if data.dx is not None and data.isSesans: 
     61        #if data.dx[0] > 0.0: 
     62        if numpy.size(data.dx[data.dx <= 0]) == 0: 
     63            _found_sesans = True 
     64        # if data.dx[0] <= 0.0: 
     65        if numpy.size(data.dx[data.dx <= 0]) > 0: 
     66            raise ValueError('one or more of your dx values are negative, please check the data file!') 
     67 
     68    if _found_sesans == True: 
     69        #Pre-compute the Hankel matrix (H) 
     70        qmax, qunits = data.sample.zacceptance 
     71        SElength = Converter(data._xunit)(data.x, "A") 
     72        zaccept = Converter(qunits)(qmax, "1/A"), 
     73        Rmax = 10000000 
     74        hankel = SesansTransform(data.x, SElength, zaccept, Rmax) 
     75        # Then return the actual transform, as if it were a smearing function 
     76        return PySmear(hankel, model, offset=0) 
     77 
    5078    _found_resolution = False 
    5179    if data.dx is not None and len(data.dx) == len(data.x): 
     
    89117    Wrapper for pure python sasmodels resolution functions. 
    90118    """ 
    91     def __init__(self, resolution, model): 
     119    def __init__(self, resolution, model, offset=None): 
    92120        self.model = model 
    93121        self.resolution = resolution 
    94         self.offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) 
     122        if offset is None: 
     123            offset = numpy.searchsorted(self.resolution.q_calc, self.resolution.q[0]) 
     124        self.offset = offset 
    95125 
    96126    def apply(self, iq_in, first_bin=0, last_bin=None): 
     
    142172    width = data.dx if data.dx is not None else 0 
    143173    return PySmear(Pinhole1D(q, width), model) 
     174 
     175 
     176class PySmear2D(object): 
     177    """ 
     178    Q smearing class for SAS 2d pinhole data 
     179    """ 
     180 
     181    def __init__(self, data=None, model=None): 
     182        self.data = data 
     183        self.model = model 
     184        self.accuracy = 'Low' 
     185        self.limit = 3.0 
     186        self.index = None 
     187        self.coords = 'polar' 
     188        self.smearer = True 
     189 
     190    def set_accuracy(self, accuracy='Low'): 
     191        """ 
     192        Set accuracy. 
     193 
     194        :param accuracy:  string 
     195        """ 
     196        self.accuracy = accuracy 
     197 
     198    def set_smearer(self, smearer=True): 
     199        """ 
     200        Set whether or not smearer will be used 
     201 
     202        :param smearer: smear object 
     203 
     204        """ 
     205        self.smearer = smearer 
     206 
     207    def set_data(self, data=None): 
     208        """ 
     209        Set data. 
     210 
     211        :param data: DataLoader.Data_info type 
     212        """ 
     213        self.data = data 
     214 
     215    def set_model(self, model=None): 
     216        """ 
     217        Set model. 
     218 
     219        :param model: sas.models instance 
     220        """ 
     221        self.model = model 
     222 
     223    def set_index(self, index=None): 
     224        """ 
     225        Set index. 
     226 
     227        :param index: 1d arrays 
     228        """ 
     229        self.index = index 
     230 
     231    def get_value(self): 
     232        """ 
     233        Over sampling of r_nbins times phi_nbins, calculate Gaussian weights, 
     234        then find smeared intensity 
     235        """ 
     236        if self.smearer: 
     237            res = Pinhole2D(data=self.data, index=self.index, 
     238                            nsigma=3.0, accuracy=self.accuracy, 
     239                            coords=self.coords) 
     240            val = self.model.evalDistribution(res.q_calc) 
     241            return res.apply(val) 
     242        else: 
     243            index = self.index if self.index is not None else slice(None) 
     244            qx_data = self.data.qx_data[index] 
     245            qy_data = self.data.qy_data[index] 
     246            q_calc = [qx_data, qy_data] 
     247            val = self.model.evalDistribution(q_calc) 
     248            return val 
     249 
Note: See TracChangeset for help on using the changeset viewer.