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

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 9b90bf8 was 9b90bf8, checked in by lewis, 7 years ago

Use DCT to calculate IDF

  • Property mode set to 100644
File size: 4.8 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            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            # dmax = 200.0 # Max real space value to calculate IDF up to
56            # dstep = 0.5 # Evaluate the IDF in steps of dstep along the real axis
57            # qmax = 1.0 # Max q value to integrate up to when calculating IDF
58
59            # Units of x axis depend on qmax (for some reason?). This scales
60            # the xgamma array appropriately, since qmax was set to 0.6 in
61            # the original fortran code.
62            # x_scale = qmax / 0.6
63
64            # xgamma = np.arange(0, dmax/x_scale, step=dstep/x_scale)
65            # idf = np.zeros(len(xgamma))
66            idf = dct(-qs**4 * (iqs-background))
67            idf[0] = trapz(-qs**4 * (iqs-background), qs)
68            idf /= Q
69
70            # nth moment = integral(q^n * I(q), q=0, q=inf)
71            # moment = np.zeros(5)
72            # for n in range(5):
73            #     integrand = qs**n * (iqs-background)
74            #     moment[n] = trapz(integrand[qs < qmax], qs[qs < qmax])
75            #     if self.check_if_cancelled(): return
76            #
77            # # idf(x) = -integral(q^4 * I(q)*cos(qx), q=0, q=inf) / 2nd moment
78            # # => idf(0) = -integral(q^4 * I(q), 0, inf) / (2nd moment)
79            # #  = -(4th moment)/(2nd moment)
80            # idf[0] = -moment[4] / moment[2]
81            # for i in range(1, len(xgamma)):
82            #     d = xgamma[i]
83            #
84            #     integrand = -qs**4 * (iqs-background) * np.cos(d*qs)
85            #     idf[i] = trapz(integrand[qs < qmax], qs[qs < qmax])
86            #     idf[i] /= moment[2]
87            #     if self.check_if_cancelled(): return
88            #
89            # xgamma *= x_scale
90
91        except Exception as e:
92            import logging
93            logger = logging.getLogger(__name__)
94            logger.error(e)
95
96            self.update(msg="Fourier transform failed.")
97            self.complete(transforms=None)
98            return
99        if self.isquit():
100            return
101        self.update(msg="Fourier transform completed.")
102
103        transform1 = Data1D(xs, gamma1)
104        transform3 = Data1D(xs[xs <= 200], gamma3)
105        idf = Data1D(xs, idf)
106
107        transforms = (transform1, transform3, idf)
108
109        self.complete(transforms=transforms)
110
111class HilbertThread(CalcThread):
112    def __init__(self, raw_data, extrapolated_data, bg, updatefn=None,
113        completefn=None):
114        CalcThread.__init__(self, updatefn=updatefn, completefn=completefn)
115        self.data = raw_data
116        self.background = bg
117        self.extrapolation = extrapolated_data
118
119    def compute(self):
120        qs = self.extrapolation.x
121        iqs = self.extrapolation.y
122        q = self.data.x
123        background = self.background
124
125        self.ready(delay=0.0)
126        self.update(msg="Starting Hilbert transform.")
127        self.ready(delay=0.0)
128        if self.isquit():
129            return
130
131        # TODO: Implement hilbert transform
132
133        self.update(msg="Hilbert transform completed.")
134
135        self.complete(transforms=None)
Note: See TracBrowser for help on using the repository browser.