Changeset 412c509 in sasview


Ignore:
Timestamp:
Jul 11, 2017 9:59:44 AM (7 years ago)
Author:
lewis
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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
2a54ba5
Parents:
457f735
git-author:
Lewis O'Driscoll <lewis.o'driscoll@…> (07/11/17 09:59:23)
git-committer:
Lewis O'Driscoll <lewis.o'driscoll@…> (07/11/17 09:59:44)
Message:

Compute 3D correlation function as well as 1D

Location:
src/sas
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/corfunc/corfunc_calculator.py

    rff11b21 r412c509  
    3434 
    3535        def __call__(self, x): 
    36             if self._lastx == [] or x.tolist() != self._lastx.tolist(): 
     36            # If input is a single number, evaluate the function at that number 
     37            # and return a single number 
     38            if type(x) == float or type(x) == int: 
     39                return self._smoothed_function(np.array([x]))[0] 
     40            # If input is a list, and is different to the last input, evaluate 
     41            # the function at each point. If the input is the same as last time 
     42            # the function was called, return the result that was calculated 
     43            # last time instead of explicity evaluating the function again. 
     44            elif self._lastx == [] or x.tolist() != self._lastx.tolist(): 
    3745                self._lasty = self._smoothed_function(x) 
    3846                self._lastx = x 
     
    121129        extrapolation = Data1D(qs, iqs) 
    122130 
    123         return params, extrapolation 
    124  
    125     def compute_transform(self, extrapolation, trans_type, background=None, 
    126         completefn=None, updatefn=None): 
     131        return params, extrapolation, s2 
     132 
     133    def compute_transform(self, extrapolation, trans_type, extrap_fn=None, 
     134        background=None, completefn=None, updatefn=None): 
    127135        """ 
    128136        Transform an extrapolated scattering curve into a correlation function. 
     
    131139        :param background: The background value (if not provided, previously 
    132140            calculated value will be used) 
     141        :param extrap_fn: A callable function representing the extraoplated data 
    133142        :param completefn: The function to call when the transform calculation 
    134143            is complete` 
     
    144153        if trans_type == 'fourier': 
    145154            self._transform_thread = FourierThread(self._data, extrapolation, 
    146             background, completefn=completefn, updatefn=updatefn) 
     155            background, extrap_fn=extrap_fn, completefn=completefn, 
     156            updatefn=updatefn) 
    147157        elif trans_type == 'hilbert': 
    148158            self._transform_thread = HilbertThread(self._data, extrapolation, 
  • src/sas/sascalc/corfunc/transform_thread.py

    r457f735 r412c509  
    22from sas.sascalc.dataloader.data_info import Data1D 
    33from scipy.fftpack import dct 
     4from scipy.integrate import simps, trapz 
    45import numpy as np 
    56from time import sleep 
    67 
    78class FourierThread(CalcThread): 
    8     def __init__(self, raw_data, extrapolated_data, bg, updatefn=None, 
    9         completefn=None): 
     9    def __init__(self, raw_data, extrapolated_data, bg, extrap_fn=None, 
     10        updatefn=None, completefn=None): 
    1011        CalcThread.__init__(self, updatefn=updatefn, completefn=completefn) 
    1112        self.data = raw_data 
    1213        self.background = bg 
    1314        self.extrapolation = extrapolated_data 
     15        self.extrap_fn = extrap_fn 
    1416 
    1517    def compute(self): 
     
    1921        background = self.background 
    2022 
     23        xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs) 
     24 
    2125        self.ready(delay=0.0) 
    22         self.update(msg="Starting Fourier transform.") 
     26        self.update(msg="Fourier transform in progress.") 
    2327        self.ready(delay=0.0) 
     28 
    2429        if self.isquit(): 
    2530            return 
     
    2732            gamma1 = dct((iqs-background)*qs**2) 
    2833            gamma1 = gamma1 / gamma1.max() 
    29         except: 
     34 
     35            # gamma3(R) = 1/R int_{0}^{R} gamma1(x) dx 
     36            # simps uses simpson's rule to calculate the integral 
     37            gamma3 = [trapz(gamma1[:n], xs[:n])/xs[n-1] for n in range(1, len(xs+1))] 
     38            gamma3 = np.array(gamma3) 
     39        except Exception as e: 
     40            import logging 
     41            logger = logging.getLogger(__name__) 
     42            logger.error(e) 
     43 
    3044            self.update(msg="Fourier transform failed.") 
    31             self.complete(transform=None) 
     45            self.complete(transforms=None) 
    3246            return 
    3347        if self.isquit(): 
     
    3549        self.update(msg="Fourier transform completed.") 
    3650 
    37         xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs) 
    3851        transform1 = Data1D(xs, gamma1) 
    39         transform3 = Data1D() 
     52        transform3 = Data1D(xs, gamma3) 
    4053 
    4154        transforms = (transform1, transform3) 
  • src/sas/sasgui/perspectives/corfunc/corfunc_panel.py

    r457f735 r412c509  
    5555        self._data = data # The data to be analysed (corrected fr background) 
    5656        self._extrapolated_data = None # The extrapolated data set 
     57        # Callable object of class CorfuncCalculator._Interpolator representing 
     58        # the extrapolated and interpolated data 
     59        self._extrapolated_fn = None 
    5760        self._transformed_data = None # Fourier trans. of the extrapolated data 
    5861        self._calculator = CorfuncCalculator() 
     
    218221 
    219222        try: 
    220             params, self._extrapolated_data = self._calculator.compute_extrapolation() 
     223            params, self._extrapolated_data, self._extrapolated_fn = \ 
     224                self._calculator.compute_extrapolation() 
    221225        except Exception as e: 
    222226            msg = "Error extrapolating data:\n" 
     
    241245            self._calculator.compute_transform(self._extrapolated_data, 
    242246                self.transform_type, background=self.background, 
     247                extrap_fn=self._extrapolated_fn, 
    243248                completefn=self.transform_complete, 
    244249                updatefn=self.transform_update) 
     
    276281        plot_y = transform1.y[np.where(transform1.x <= 200)] 
    277282        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL1) 
     283        plot_x = transform3.x[np.where(transform3.x <= 200)] 
     284        plot_y = transform3.y[np.where(transform3.x <= 200)] 
     285        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL3) 
    278286        # Only enable extract params button if a fourier trans. has been done 
    279287        if self.transform_type == 'fourier': 
Note: See TracChangeset for help on using the changeset viewer.