source: sasview/src/sas/perspectives/fitting/model_thread.py @ a3f125f0

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since a3f125f0 was a3f125f0, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

refactor resolution calculation to enable sasmodels resolution calcuator for pinhole smearing, but don't enable it yet

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