source: sasview/src/sas/sascalc/corfunc/transform_thread.py @ c728295

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c728295 was c728295, checked in by lewis, 7 years ago

Remove unused argument and import

  • Property mode set to 100644
File size: 4.5 KB
Line 
1from sas.sascalc.data_util.calcthread import CalcThread
2from sas.sascalc.dataloader.data_info import Data1D
3from scipy.fftpack import dct
4from scipy.integrate import trapz
5import numpy as np
6from time import sleep
7
8class FourierThread(CalcThread):
9    def __init__(self, raw_data, extrapolated_data, bg, updatefn=None,
10        completefn=None):
11        CalcThread.__init__(self, updatefn=updatefn, completefn=completefn)
12        self.data = raw_data
13        self.background = bg
14        self.extrapolation = extrapolated_data
15
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
23    def compute(self):
24        qs = self.extrapolation.x
25        iqs = self.extrapolation.y
26        q = self.data.x
27        background = self.background
28
29        xs = np.pi*np.arange(len(qs),dtype=np.float32)/(q[1]-q[0])/len(qs)
30
31        self.ready(delay=0.0)
32        self.update(msg="Fourier transform in progress.")
33        self.ready(delay=0.0)
34
35        if self.check_if_cancelled(): return
36        try:
37            # ----- 1D Correlation Function -----
38            gamma1 = dct((iqs-background)*qs**2)
39            gamma1 = gamma1 / gamma1.max()
40
41            if self.check_if_cancelled(): return
42
43            # ----- 3D Correlation Function -----
44            # gamma3(R) = 1/R int_{0}^{R} gamma1(x) dx
45            # trapz uses the trapezium rule to calculate the integral
46            mask = xs <= 200.0 # Only calculate gamma3 up to x=200 (as this is all that's plotted)
47            gamma3 = [trapz(gamma1[:n], xs[:n])/xs[n-1] for n in range(2, len(xs[mask]) + 1)]
48            gamma3.insert(0, 1.0) # Gamma_3(0) is defined as 1
49            gamma3 = np.array(gamma3)
50
51            if self.check_if_cancelled(): return
52
53            # ----- Interface Distribution function -----
54            dmax = 200.0 # Max real space value to calculate IDF up to
55            dstep = 0.5
56            qmax = 1.0 # Max q value to integrate up to when calculating IDF
57
58            # Units of x axis depend on qmax (for some reason?). This scales
59            # the xgamma array appropriately, since qmax was set to 0.6 in
60            # the original fortran code.
61            x_scale = qmax / 0.6
62
63            xgamma = np.arange(0, dmax/x_scale, step=dstep/x_scale)
64            idf = np.zeros(len(xgamma))
65
66            # nth moment = integral(q^n * I(q), q=0, q=inf)
67            moment = np.zeros(5)
68            for n in range(5):
69                integrand = qs**n * (iqs-background)
70                moment[n] = trapz(integrand[qs < qmax], qs[qs < qmax])
71                if self.check_if_cancelled(): return
72
73            # idf(x) = -integral(q^4 * I(q)*cos(qx), q=0, q=inf) / 2nd moment
74            # => idf(0) = -integral(q^4 * I(q), 0, inf) / (2nd moment)
75            #  = -(4th moment)/(2nd moment)
76            idf[0] = -moment[4] / moment[2]
77            for i in range(1, len(xgamma)):
78                d = xgamma[i]
79
80                integrand = -qs**4 * (iqs-background) * np.cos(d*qs)
81                idf[i] = trapz(integrand[qs < qmax], qs[qs < qmax])
82                idf[i] /= moment[2]
83                if self.check_if_cancelled(): return
84
85            xgamma *= x_scale
86
87        except Exception as e:
88            import logging
89            logger = logging.getLogger(__name__)
90            logger.error(e)
91
92            self.update(msg="Fourier transform failed.")
93            self.complete(transforms=None)
94            return
95        if self.isquit():
96            return
97        self.update(msg="Fourier transform completed.")
98
99        transform1 = Data1D(xs, gamma1)
100        transform3 = Data1D(xs[xs <= 200], gamma3)
101        idf = Data1D(xgamma, idf)
102
103        transforms = (transform1, transform3, idf)
104
105        self.complete(transforms=transforms)
106
107class HilbertThread(CalcThread):
108    def __init__(self, raw_data, extrapolated_data, bg, updatefn=None,
109        completefn=None):
110        CalcThread.__init__(self, updatefn=updatefn, completefn=completefn)
111        self.data = raw_data
112        self.background = bg
113        self.extrapolation = extrapolated_data
114
115    def compute(self):
116        qs = self.extrapolation.x
117        iqs = self.extrapolation.y
118        q = self.data.x
119        background = self.background
120
121        self.ready(delay=0.0)
122        self.update(msg="Starting Hilbert transform.")
123        self.ready(delay=0.0)
124        if self.isquit():
125            return
126
127        # TODO: Implement hilbert transform
128
129        self.update(msg="Hilbert transform completed.")
130
131        self.complete(transforms=None)
Note: See TracBrowser for help on using the repository browser.