Changeset d07f863 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Sep 16, 2017 10:50:38 PM (7 years ago)
Author:
GitHub <noreply@…>
Parents:
cfd27dd (diff), 0794ce3 (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.
git-author:
Lewis O'Driscoll <lewis.o'driscoll@…> (09/16/17 22:50:38)
git-committer:
GitHub <noreply@…> (09/16/17 22:50:38)
Message:

Merge 0794ce3ca2d0601bc623ffd5c74885b7383e69f4 into cfd27dd1bcdea96a621d51fbd290980fb6e6156a

Location:
src/sas/sascalc/corfunc
Files:
2 edited

Legend:

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

    rff11b21 rc728295  
    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 
     131        return params, extrapolation, s2 
    124132 
    125133    def compute_transform(self, extrapolation, trans_type, background=None, 
     
    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, 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

    rd03228e ra309667  
    22from sas.sascalc.dataloader.data_info import Data1D 
    33from scipy.fftpack import dct 
     4from scipy.integrate import trapz 
    45import numpy as np 
    56from time import sleep 
     
    1314        self.extrapolation = extrapolated_data 
    1415 
     16    def check_if_cancelled(self): 
     17        if self.isquit(): 
     18            self.update("Fourier transform cancelled.") 
     19            self.complete(transforms=None) 
     20            return True 
     21        return False 
     22 
    1523    def compute(self): 
    1624        qs = self.extrapolation.x 
     
    1927        background = self.background 
    2028 
     29        xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs) 
     30 
    2131        self.ready(delay=0.0) 
    22         self.update(msg="Starting Fourier transform.") 
     32        self.update(msg="Fourier transform in progress.") 
    2333        self.ready(delay=0.0) 
    24         if self.isquit(): 
    25             return 
     34 
     35        if self.check_if_cancelled(): return 
    2636        try: 
    27             gamma = dct((iqs-background)*qs**2) 
    28             gamma = gamma / gamma.max() 
    29         except: 
     37            # ----- 1D Correlation Function ----- 
     38            gamma1 = dct((iqs-background)*qs**2) 
     39            Q = gamma1.max() 
     40            gamma1 /= Q 
     41 
     42            if self.check_if_cancelled(): return 
     43 
     44            # ----- 3D Correlation Function ----- 
     45            # gamma3(R) = 1/R int_{0}^{R} gamma1(x) dx 
     46            # trapz uses the trapezium rule to calculate the integral 
     47            mask = xs <= 200.0 # Only calculate gamma3 up to x=200 (as this is all that's plotted) 
     48            gamma3 = [trapz(gamma1[:n], xs[:n])/xs[n-1] for n in range(2, len(xs[mask]) + 1)] 
     49            gamma3.insert(0, 1.0) # Gamma_3(0) is defined as 1 
     50            gamma3 = np.array(gamma3) 
     51 
     52            if self.check_if_cancelled(): return 
     53 
     54            # ----- Interface Distribution function ----- 
     55            idf = dct(-qs**4 * (iqs-background)) 
     56 
     57            if self.check_if_cancelled(): return 
     58 
     59            # Manually calculate IDF(0.0), since scipy DCT tends to give us a 
     60            # very large negative value. 
     61            # IDF(x) = int_0^inf q^4 * I(q) * cos(q*x) * dq 
     62            # => IDF(0) = int_0^inf q^4 * I(q) * dq 
     63            idf[0] = trapz(-qs**4 * (iqs-background), qs) 
     64            idf /= Q # Normalise using scattering invariant 
     65 
     66        except Exception as e: 
     67            import logging 
     68            logger = logging.getLogger(__name__) 
     69            logger.error(e) 
     70 
    3071            self.update(msg="Fourier transform failed.") 
    31             self.complete(transform=None) 
     72            self.complete(transforms=None) 
    3273            return 
    3374        if self.isquit(): 
     
    3576        self.update(msg="Fourier transform completed.") 
    3677 
    37         xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs) 
    38         transform = Data1D(xs, gamma) 
     78        transform1 = Data1D(xs, gamma1) 
     79        transform3 = Data1D(xs[xs <= 200], gamma3) 
     80        idf = Data1D(xs, idf) 
    3981 
    40         self.complete(transform=transform) 
     82        transforms = (transform1, transform3, idf) 
     83 
     84        self.complete(transforms=transforms) 
    4185 
    4286class HilbertThread(CalcThread): 
     
    64108        self.update(msg="Hilbert transform completed.") 
    65109 
    66         self.complete(transform=None) 
     110        self.complete(transforms=None) 
Note: See TracChangeset for help on using the changeset viewer.