Changeset 7e7e806 in sasview


Ignore:
Timestamp:
Aug 26, 2011 8:22:08 AM (13 years ago)
Author:
Gervaise Alina <gervyh@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
c647377
Parents:
2dbffcc
Message:

modify model thread to make data as a required input parameter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fittingview/src/sans/perspectives/fitting/model_thread.py

    r49c69de r7e7e806  
    22 
    33import time 
     4import sys 
     5import numpy 
     6import math 
     7from sans.models.smearing_2d import Smearer2D 
    48from data_util.calcthread import CalcThread 
    5 import sys 
    6 import numpy,math 
    7 from sans.models.smearing_2d import Smearer2D 
    89 
    910class Calc2D(CalcThread): 
     
    1415    and I(qx, qy) = I(-qx, -qy) is assumed. 
    1516    """ 
    16     def __init__(self, x, y, data,model,smearer,qmin, qmax,qstep, 
    17                  page_id , 
     17    def __init__(self, data, model, smearer, qmin, qmax,  page_id, 
    1818                 state=None, 
    1919                 toggle_mode_on=False, 
    20                  completefn = None, 
    21                  updatefn   = None, 
     20                 completefn=None, 
     21                 updatefn=None, 
    2222                 update_chisqr=True, 
    23                  yieldtime  = 0.04, 
    24                  worktime   = 0.04 
     23                 yieldtime=0.04, 
     24                 worktime=0.04 
    2525                 ): 
    2626        CalcThread.__init__(self,completefn, 
     
    2828                 yieldtime, 
    2929                 worktime) 
    30         self.qmin= qmin 
    31         self.qmax= qmax 
    32         self.qstep= qstep 
     30        self.qmin = qmin 
     31        self.qmax = qmax 
     32        #self.qstep = qstep 
    3333        self.toggle_mode_on = toggle_mode_on 
    34         self.x = x 
    35         self.y = y 
    36         self.data= data 
     34        self.data = data 
    3735        self.page_id = page_id 
    3836        self.state = None 
    3937        # the model on to calculate 
    4038        self.model = model 
    41         self.smearer = smearer#(data=self.data,model=self.model) 
     39        self.smearer = smearer 
    4240        self.starttime = 0   
    4341        self.update_chisqr = update_chisqr 
     
    4947        self.starttime = time.time() 
    5048        # Determine appropriate q range 
    51         if self.qmin==None: 
     49        if self.qmin == None: 
    5250            self.qmin = 0 
    53         if self.qmax== None: 
    54             if self.data !=None: 
    55                 newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) 
    56                 newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) 
    57                 self.qmax=math.sqrt( newx + newy ) 
    58          
    59         if self.data != None: 
    60             self.I_data = self.data.data 
    61             self.qx_data = self.data.qx_data 
    62             self.qy_data = self.data.qy_data 
    63             self.dqx_data = self.data.dqx_data 
    64             self.dqy_data = self.data.dqy_data 
    65             self.mask    = self.data.mask 
    66         else:           
    67             xbin =  numpy.linspace(start= -1*self.qmax, 
    68                                    stop= self.qmax, 
    69                                    num= self.qstep, 
    70                                    endpoint=True )   
    71             ybin = numpy.linspace(start= -1*self.qmax, 
    72                                    stop= self.qmax, 
    73                                    num= self.qstep, 
    74                                    endpoint=True )             
    75              
    76             new_xbin = numpy.tile(xbin, (len(ybin),1)) 
    77             new_ybin = numpy.tile(ybin, (len(xbin),1)) 
    78             new_ybin = new_ybin.swapaxes(0,1) 
    79             new_xbin = new_xbin.flatten() 
    80             new_ybin = new_ybin.flatten() 
    81             self.qy_data = new_ybin 
    82             self.qx_data = new_xbin 
    83             # fake data 
    84             self.I_data = numpy.ones(len(self.qx_data)) 
    85             
    86             self.mask = numpy.ones(len(self.qx_data),dtype=bool) 
    87              
    88         # Define matrix where data will be plotted     
    89         radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data ) 
    90         index_data= (self.qmin<= radius)&(self.mask) 
     51        if self.qmax == None: 
     52            if self.data != None: 
     53                newx = math.pow(max(math.fabs(self.data.xmax), 
     54                                   math.fabs(self.data.xmin)), 2) 
     55                newy = math.pow(max(math.fabs(self.data.ymax), 
     56                                   math.fabs(self.data.ymin)), 2) 
     57                self.qmax = math.sqrt(newx + newy) 
     58         
     59        if self.data is None: 
     60            msg = "Compute Calc2D receive data = %s.\n" % str(self.data)     
     61            raise ValueError, msg 
     62             
     63        # Define matrix where data will be plotted  
     64        radius= numpy.sqrt((self.data.qx_data * self.data.qx_data) + \ 
     65                    (self.data.qy_data * self.data.qy_data)) 
     66        index_data = (self.qmin <= radius) & self.data.mask 
    9167 
    9268        # For theory, qmax is based on 1d qmax  
    9369        # so that must be mulitified by sqrt(2) to get actual max for 2d 
    94         index_model = ((self.qmin <= radius)&(radius<= self.qmax)) 
    95         index_model = (index_model)&(self.mask) 
    96         index_model = (index_model)&(numpy.isfinite(self.I_data)) 
    97         if self.data ==None: 
    98             # Only qmin value will be consider for the detector 
    99             index_model = index_data   
    100  
    101         if self.smearer != None: 
     70        index_model = (self.qmin <= radius) & (radius <= self.qmax) 
     71        index_model = index_model & self.data.mask 
     72        index_model = index_model & numpy.isfinite(self.data.data) 
     73       
     74        if self.smearer is not None: 
    10275            # Set smearer w/ data, model and index. 
    10376            fn = self.smearer 
     
    10679            # Get necessary data from self.data and set the data for smearing 
    10780            fn.get_data() 
    108             # Calculate smeared Intensity (by Gaussian averaging): DataLoader/smearing2d/Smearer2D() 
     81            # Calculate smeared Intensity  
     82            #(by Gaussian averaging): DataLoader/smearing2d/Smearer2D() 
    10983            value = fn.get_value() 
    110  
    11184        else:     
    11285            # calculation w/o smearing 
    113             value =  self.model.evalDistribution([self.qx_data[index_model],self.qy_data[index_model]]) 
    114  
    115         output = numpy.zeros(len(self.qx_data)) 
    116          
     86            value =  self.model.evalDistribution([self.data.qx_data[index_model], 
     87                                                self.data.qy_data[index_model]]) 
     88        output = numpy.zeros(len(self.data.qx_data)) 
    11789        # output default is None 
    118         # This method is to distinguish between masked point(nan) and data point = 0. 
     90        # This method is to distinguish between masked  
     91        #point(nan) and data point = 0. 
    11992        output = output/output 
    12093        # set value for self.mask==True, else still None to Plottools 
    12194        output[index_model] = value  
    122  
    12395        elapsed = time.time()-self.starttime 
    12496        self.complete(image=output, 
     
    132104                       qmin=self.qmin, 
    133105                       qmax=self.qmax, 
    134                        qstep=self.qstep, 
    135                        update_chisqr = self.update_chisqr ) 
     106                       #qstep=self.qstep, 
     107                       update_chisqr = self.update_chisqr) 
    136108         
    137109 
     
    140112    Compute 1D data 
    141113    """ 
    142     def __init__(self, x, model, 
     114    def __init__(self, model, 
    143115                 page_id, 
    144                  data=None, 
     116                 data, 
    145117                 qmin=None, 
    146118                 qmax=None, 
     
    150122                 completefn = None, 
    151123                 update_chisqr=True, 
    152                  updatefn   = None, 
    153                  yieldtime  = 0.01, 
    154                  worktime   = 0.01 
     124                 updatefn=None, 
     125                 yieldtime=0.01, 
     126                 worktime=0.01 
    155127                 ): 
    156128        """ 
     
    160132                 yieldtime, 
    161133                 worktime) 
    162         self.x = numpy.array(x) 
    163134        self.data = data 
    164135        self.qmin = qmin 
     
    177148        """ 
    178149        self.starttime = time.time() 
    179         output = numpy.zeros((len(self.x))) 
    180         index= (self.qmin <= self.x)& (self.x <= self.qmax) 
     150        output = numpy.zeros((len(self.data.x))) 
     151        index= (self.qmin <= self.data.x)& (self.data.x <= self.qmax) 
    181152      
    182153        ##smearer the ouput of the plot     
    183         if self.smearer!=None: 
    184             first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) 
    185             output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) 
     154        if self.smearer is not None: 
     155            first_bin, last_bin = self.smearer.get_bin_range(self.qmin,  
     156                                                             self.qmax) 
     157            mask = self.data.x[first_bin:last_bin] 
     158            output[first_bin:last_bin] = self.model.evalDistribution(mask) 
    186159            output = self.smearer(output, first_bin, last_bin)  
    187160        else: 
    188             output[index] = self.model.evalDistribution(self.x[index]) 
     161            output[index] = self.model.evalDistribution(self.data.x[index]) 
    189162          
    190163        elapsed = time.time() - self.starttime 
    191164        
    192         self.complete(x=self.x[index], y=output[index],  
     165        self.complete(x=self.data.x[index], y=output[index],  
    193166                      page_id=self.page_id, 
    194167                      state=self.state, 
     
    196169                      elapsed=elapsed,index=index, model=self.model, 
    197170                      data=self.data,  
    198                       update_chisqr = self.update_chisqr ) 
     171                      update_chisqr = self.update_chisqr) 
    199172         
    200173    def results(self): 
Note: See TracChangeset for help on using the changeset viewer.