source: sasview/fittingview/src/sans/perspectives/fitting/model_thread.py @ 7e7e806

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 7e7e806 was 7e7e806, checked in by Gervaise Alina <gervyh@…>, 13 years ago

modify model thread to make data as a required input parameter

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