source: sasview/src/sas/perspectives/fitting/model_thread.py @ 2f4b430

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 2f4b430 was 2f4b430, checked in by Doucet, Mathieu <doucetm@…>, 9 years ago

Take care of white spaces (pylint)

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