source: sasview/sansview/perspectives/fitting/model_thread.py @ e2f0554

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 e2f0554 was 5ef55d2, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on addd theory to data_panel

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