source: sasview/sansview/perspectives/fitting/model_thread.py @ 11a7e11

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 11a7e11 was f72333f, checked in by Jae Cho <jhjcho@…>, 14 years ago

Plugged in 2D smear: traditional over-sampling method

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