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

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 a86e69b was 62f851f, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on plot for batch

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