source: sasview/src/sas/qtgui/Perspectives/Fitting/ModelThread.py @ 2df558e

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 2df558e was 2df558e, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Updated non-twisted on-complete call for 2D data. SASVIEW-1025

  • Property mode set to 100644
File size: 10.0 KB
Line 
1"""
2    Calculation thread for modeling
3"""
4
5import time
6import numpy
7import math
8from sas.sascalc.data_util.calcthread import CalcThread
9from sas.sascalc.fit.MultiplicationModel import MultiplicationModel
10import sas.qtgui.Utilities.LocalConfig as LocalConfig
11
12class Calc2D(CalcThread):
13    """
14    Compute 2D model
15    This calculation assumes a 2-fold symmetry of the model
16    where points are computed for one half of the detector
17    and I(qx, qy) = I(-qx, -qy) is assumed.
18    """
19    def __init__(self, data, model, smearer, qmin, qmax, page_id,
20                 state=None,
21                 weight=None,
22                 fid=None,
23                 toggle_mode_on=False,
24                 completefn=None,
25                 updatefn=None,
26                 update_chisqr=True,
27                 source='model',
28                 yieldtime=0.04,
29                 worktime=0.04,
30                 exception_handler=None,
31                 ):
32        CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime,
33                            exception_handler=exception_handler)
34        self.qmin = qmin
35        self.qmax = qmax
36        self.weight = weight
37        self.fid = fid
38        #self.qstep = qstep
39        self.toggle_mode_on = toggle_mode_on
40        self.data = data
41        self.page_id = page_id
42        self.state = None
43        # the model on to calculate
44        self.model = model
45        self.smearer = smearer
46        self.starttime = 0
47        self.update_chisqr = update_chisqr
48        self.source = source
49
50    def compute(self):
51        """
52        Compute the data given a model function
53        """
54        self.starttime = time.time()
55        # Determine appropriate q range
56        if self.qmin is None:
57            self.qmin = 0
58        if self.qmax is None:
59            if self.data is not None:
60                newx = math.pow(max(math.fabs(self.data.xmax),
61                                   math.fabs(self.data.xmin)), 2)
62                newy = math.pow(max(math.fabs(self.data.ymax),
63                                   math.fabs(self.data.ymin)), 2)
64                self.qmax = math.sqrt(newx + newy)
65
66        if self.data is None:
67            msg = "Compute Calc2D receive data = %s.\n" % str(self.data)
68            raise ValueError(msg)
69
70        # Define matrix where data will be plotted
71        radius = numpy.sqrt((self.data.qx_data * self.data.qx_data) + \
72                    (self.data.qy_data * self.data.qy_data))
73
74        # For theory, qmax is based on 1d qmax
75        # so that must be mulitified by sqrt(2) to get actual max for 2d
76        index_model = (self.qmin <= radius) & (radius <= self.qmax)
77        index_model = index_model & self.data.mask
78        index_model = index_model & numpy.isfinite(self.data.data)
79
80        if self.smearer is not None:
81            # Set smearer w/ data, model and index.
82            fn = self.smearer
83            fn.set_model(self.model)
84            fn.set_index(index_model)
85            # Calculate smeared Intensity
86            #(by Gaussian averaging): DataLoader/smearing2d/Smearer2D()
87            value = fn.get_value()
88        else:
89            # calculation w/o smearing
90            value = self.model.evalDistribution([
91                self.data.qx_data[index_model],
92                self.data.qy_data[index_model]
93            ])
94        output = numpy.zeros(len(self.data.qx_data))
95        # output default is None
96        # This method is to distinguish between masked
97        #point(nan) and data point = 0.
98        output = output / output
99        # set value for self.mask==True, else still None to Plottools
100        output[index_model] = value
101        elapsed = time.time() - self.starttime
102
103        if LocalConfig.USING_TWISTED:
104            return (output,
105                    self.data,
106                    self.page_id,
107                    self.model,
108                    self.state,
109                    self.toggle_mode_on,
110                    elapsed,
111                    index_model,
112                    self.fid,
113                    self.qmin,
114                    self.qmax,
115                    self.weight,
116                    self.update_chisqr,
117                    self.source)
118        else:
119            self.completefn((output,
120                           self.data,
121                           self.page_id,
122                           self.model,
123                           self.state,
124                           self.toggle_mode_on,
125                           elapsed,
126                           index_model,
127                           self.fid,
128                           self.qmin,
129                           self.qmax,
130                           self.weight,
131                           #qstep=self.qstep,
132                           self.update_chisqr,
133                           self.source))
134
135
136class Calc1D(CalcThread):
137    """
138    Compute 1D data
139    """
140    def __init__(self, model,
141                 page_id,
142                 data,
143                 fid=None,
144                 qmin=None,
145                 qmax=None,
146                 weight=None,
147                 smearer=None,
148                 toggle_mode_on=False,
149                 state=None,
150                 completefn=None,
151                 update_chisqr=True,
152                 source='model',
153                 updatefn=None,
154                 yieldtime=0.01,
155                 worktime=0.01,
156                 exception_handler=None,
157                 ):
158        """
159        """
160        CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime,
161                            exception_handler=exception_handler)
162        self.fid = fid
163        self.data = data
164        self.qmin = qmin
165        self.qmax = qmax
166        self.model = model
167        self.weight = weight
168        self.toggle_mode_on = toggle_mode_on
169        self.state = state
170        self.page_id = page_id
171        self.smearer = smearer
172        self.starttime = 0
173        self.update_chisqr = update_chisqr
174        self.source = source
175        self.out = None
176        self.index = None
177
178    def compute(self):
179        """
180        Compute model 1d value given qmin , qmax , x value
181        """
182        self.starttime = time.time()
183        output = numpy.zeros((len(self.data.x)))
184        index = (self.qmin <= self.data.x) & (self.data.x <= self.qmax)
185
186        # If we use a smearer, also return the unsmeared model
187        unsmeared_output = None
188        unsmeared_data = None
189        unsmeared_error = None
190        ##smearer the ouput of the plot
191        if self.smearer is not None:
192            first_bin, last_bin = self.smearer.get_bin_range(self.qmin,
193                                                             self.qmax)
194            mask = self.data.x[first_bin:last_bin+1]
195            unsmeared_output = numpy.zeros((len(self.data.x)))
196            unsmeared_output[first_bin:last_bin+1] = self.model.evalDistribution(mask)
197            output = self.smearer(unsmeared_output, first_bin, last_bin)
198
199            # Rescale data to unsmeared model
200            # Check that the arrays are compatible. If we only have a model but no data,
201            # the length of data.y will be zero.
202            if isinstance(self.data.y, numpy.ndarray) and output.shape == self.data.y.shape:
203                unsmeared_data = numpy.zeros((len(self.data.x)))
204                unsmeared_error = numpy.zeros((len(self.data.x)))
205                unsmeared_data[first_bin:last_bin+1] = self.data.y[first_bin:last_bin+1]\
206                                                        * unsmeared_output[first_bin:last_bin+1]\
207                                                        / output[first_bin:last_bin+1]
208                unsmeared_error[first_bin:last_bin+1] = self.data.dy[first_bin:last_bin+1]\
209                                                        * unsmeared_output[first_bin:last_bin+1]\
210                                                        / output[first_bin:last_bin+1]
211                unsmeared_output=unsmeared_output[index]
212                unsmeared_data=unsmeared_data[index]
213                unsmeared_error=unsmeared_error
214        else:
215            output[index] = self.model.evalDistribution(self.data.x[index])
216
217        sq_values = None
218        pq_values = None
219        s_model = None
220        p_model = None
221        if isinstance(self.model, MultiplicationModel):
222            s_model = self.model.s_model
223            p_model = self.model.p_model
224        elif hasattr(self.model, "calc_composition_models"):
225            results = self.model.calc_composition_models(self.data.x[index])
226            if results is not None:
227                pq_values, sq_values = results
228
229        if pq_values is None or sq_values is None:
230            if p_model is not None and s_model is not None:
231                sq_values = numpy.zeros((len(self.data.x)))
232                pq_values = numpy.zeros((len(self.data.x)))
233                sq_values[index] = s_model.evalDistribution(self.data.x[index])
234                pq_values[index] = p_model.evalDistribution(self.data.x[index])
235
236        elapsed = time.time() - self.starttime
237
238        if LocalConfig.USING_TWISTED:
239            return (self.data.x[index], output[index],
240                    self.page_id,
241                    self.state,
242                    self.weight,
243                    self.fid,
244                    self.toggle_mode_on,
245                    elapsed, index, self.model,
246                    self.data,
247                    self.update_chisqr,
248                    self.source,
249                    unsmeared_output, unsmeared_data, unsmeared_error,
250                    pq_values, sq_values)
251        else:
252            self.completefn((self.data.x[index], output[index],
253                        self.page_id,
254                        self.state,
255                        self.weight,
256                        self.fid,
257                        self.toggle_mode_on,
258                        elapsed, index, self.model,
259                        self.data,
260                        self.update_chisqr,
261                        self.source,
262                        unsmeared_output, unsmeared_data, unsmeared_error,
263                        pq_values, sq_values))
264
265    def results(self):
266        """
267        Send resuts of the computation
268        """
269        return [self.out, self.index]
Note: See TracBrowser for help on using the repository browser.