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
RevLine 
[bb18ef1]1import time
[e733619]2from data_util.calcthread import CalcThread
[bb18ef1]3import sys
[d16e396]4import numpy,math
[f72333f]5from DataLoader.smearing_2d import Smearer2D
6import fitpage
[bb18ef1]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   
[f72333f]15    def __init__(self, x, y, data,model,smearer,qmin, qmax,qstep,
[bb18ef1]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
[c77d859]26        self.qmax= qmax
[bb18ef1]27        self.qstep= qstep
[e575db9]28
29        self.x = x
30        self.y = y
[c77d859]31        self.data= data
[1b001a7]32        # the model on to calculate
[bb18ef1]33        self.model = model
[f72333f]34        self.smearer = smearer#(data=self.data,model=self.model)
[904713c]35        self.starttime = 0 
[bb18ef1]36       
37    def compute(self):
38        """
39            Compute the data given a model function
40        """
[1b001a7]41        self.starttime = time.time()
42        # Determine appropriate q range
[c77d859]43        if self.qmin==None:
44            self.qmin = 0
45        if self.qmax== None:
[cd3d15b]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 )
[e575db9]50       
51        if self.data != None:
[43e685d]52            self.I_data = self.data.data
[e575db9]53            self.qx_data = self.data.qx_data
54            self.qy_data = self.data.qy_data
[f72333f]55            self.dqx_data = self.data.dqx_data
56            self.dqy_data = self.data.dqy_data
[e575db9]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
[43e685d]75            # fake data
76            self.I_data = numpy.ones(len(self.qx_data))
[e575db9]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)
[43e685d]83
[e575db9]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))
[51a71a3]87        index_model = (index_model)&(self.mask)
88        index_model = (index_model)&(numpy.isfinite(self.I_data))
[e575db9]89        if self.data ==None:
[d2caa18]90            # Only qmin value will be consider for the detector
[51a71a3]91            index_model = index_data 
[f72333f]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]])
[e575db9]106
[51a71a3]107        output = numpy.zeros(len(self.qx_data))
[43e685d]108       
109        # output default is None
[f72333f]110        # This method is to distinguish between masked point(nan) and data point = 0.
[43e685d]111        output = output/output
112        # set value for self.mask==True, else still None to Plottools
[51a71a3]113        output[index_model] = value
[f72333f]114
[1b001a7]115        elapsed = time.time()-self.starttime
116        self.complete( image = output,
117                       data = self.data , 
118                       model = self.model,
119                       elapsed = elapsed,
[f72333f]120                       index = index_model,
[1b001a7]121                       qmin = self.qmin,
[e575db9]122                       qmax = self.qmax,
[1b001a7]123                       qstep = self.qstep )
124       
[bb18ef1]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)
[1b001a7]143        self.x = numpy.array(x)
[bb18ef1]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):
[c77d859]152        """
153            Compute model 1d value given qmin , qmax , x value
154        """
[1b001a7]155       
[bb18ef1]156        self.starttime = time.time()
[cad821b]157        output = numpy.zeros((len(self.x)))
158        index= (self.qmin <= self.x)& (self.x <= self.qmax)
[bfe4644]159     
[fb8daaaf]160        ##smearer the ouput of the plot   
[bb18ef1]161        if self.smearer!=None:
[a0bc608]162            first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax)
[e627f19]163            output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin])
[a0bc608]164            output = self.smearer(output, first_bin, last_bin) 
[e627f19]165        else:
166            output[index] = self.model.evalDistribution(self.x[index])
[bfe4644]167         
[bb18ef1]168        elapsed = time.time()-self.starttime
[785c8233]169       
[a0bc608]170        self.complete(x= self.x[index], y= output[index], 
[f72333f]171                      elapsed=elapsed,index=index, model= self.model, data=self.data)
172
[bb18ef1]173       
[f72333f]174    def results(self):
175        """
176            Send resuts of the computation
177        """
178        return [self.out, self.index]
[1b001a7]179               
[bb18ef1]180class CalcCommandline:
181    def __init__(self, n=20000):
182        #print thread.get_ident()
183        from sans.models.CylinderModel import CylinderModel
184       
[904713c]185        model = CylinderModel()
[bb18ef1]186       
187         
188        print model.runXY([0.01, 0.02])
189       
190        qmax = 0.01
191        qstep = 0.0001
192        self.done = False
193       
[904713c]194        x = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
195        y = numpy.arange(-qmax, qmax+qstep*0.01, qstep)
196   
[bb18ef1]197   
[904713c]198        calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep,
[bb18ef1]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
[904713c]212    def complete(self, image, data, model, elapsed, qmin, qmax, qstep ):
[bb18ef1]213        print "complete"
214        self.done = True
215
216if __name__ == "__main__":
217    CalcCommandline()
218   
Note: See TracBrowser for help on using the repository browser.