Changeset e9b4cc4 in sasview for sansview


Ignore:
Timestamp:
Jan 15, 2009 5:39:19 PM (15 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:
633ba04
Parents:
681f0dc
Message:

add thread for fitting

Location:
sansview/perspectives/fitting
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • sansview/perspectives/fitting/fitpage1D.py

    rd23544dc re9b4cc4  
    483483            when enter value on panel redraw model according to changed 
    484484        """ 
    485         self.set_model_parameter() 
     485        self.set_model() 
    486486        self.compute_chisqr() 
    487487         
    488       
    489     def set_model_parameter(self): 
    490         """ 
    491             this method redraws the model according to parameters values changes 
    492             and the reset model according to paramaters changes 
    493         """ 
    494         print "went here",len(self.parameters) ,self.model 
     488    def set_model(self):  
    495489        if len(self.parameters) !=0 and self.model !=None: 
    496490            # Flag to register when a parameter has changed. 
    497             is_modified = False 
    498491            for item in self.parameters: 
    499492                try: 
     
    502495                    item[3].Clear() 
    503496                    item[3].Hide() 
    504                     name=str(item[0].GetLabelText()) 
    505                     value= float(item[1].GetValue()) 
    506                     # If the value of the parameter has changed, 
    507                     # update the model and set the is_modified flag 
    508                     if value != self.model.getParam(name): 
    509                         self.model.setParam(name,value) 
    510                         is_modified = True  
    511497                except: 
    512498                     wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 
    513499                            "Drawing  Error:wrong value entered : %s"% sys.exc_value)) 
     500        self.set_model_parameter() 
     501         
     502    def set_model_parameter(self): 
     503        if len(self.parameters) !=0 and self.model !=None: 
     504            # Flag to register when a parameter has changed. 
     505            is_modified = False 
     506            for item in self.parameters: 
     507                try: 
     508                     name=str(item[0].GetLabelText()) 
     509                     value= float(item[1].GetValue()) 
     510                     # If the value of the parameter has changed, 
     511                     # update the model and set the is_modified flag 
     512                     if value != self.model.getParam(name): 
     513                         self.model.setParam(name,value) 
     514                         is_modified = True 
     515                except: 
     516                     wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 
     517                            "Model Drawing  Error:wrong value entered : %s"% sys.exc_value)) 
     518             
    514519            # Here we should check whether the boundaries have been modified. 
    515520            # If qmin and qmax have been modified, update qmin and qmax and  
  • sansview/perspectives/fitting/fitpage2D.py

    rd23544dc re9b4cc4  
    200200                        for j in range(len(self.data.y_bins)): 
    201201                            if self.data.y_bins[j]>= ymin and self.data.y_bins[j]<= ymax: 
    202                                 print "data error ",self.data.err_data[j][i] 
    203202                                res.append( (self.data.data[j][i]- self.model.runXY(\ 
    204203                                 [self.data.x_bins[i],self.data.y_bins[j]]))\ 
     
    209208                    if numpy.isfinite(item): 
    210209                        sum +=item 
    211                 print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum 
     210                #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum 
    212211                #print res 
    213212                self.tcChi.SetValue(format_number(math.fabs(sum))) 
  • sansview/perspectives/fitting/fitting.py

    rd23544dc re9b4cc4  
    1010from fitproblem import FitProblem 
    1111from fitpanel import FitPanel 
    12  
     12from fit_thread import FitThread 
    1313import models,modelpage 
    1414import fitpage1D,fitpage2D 
    1515import park 
    1616DEFAULT_BEAM = 0.005 
     17import time 
     18import thread 
     19print "main",thread.get_ident() 
     20 
    1721class Plugin: 
    1822    """ 
     
    2832        ## List of panels for the simulation perspective (names) 
    2933        self.perspective = [] 
     34         
     35        self.calc_thread = None 
     36        self.done = False 
    3037        # Start with a good default 
    3138        self.elapsed = 0.022 
     
    222229            return model_name,param_name 
    223230         
    224          
    225     def _single_fit_completed(self,result,pars,cpage,qmin,qmax,ymin=None, ymax=None): 
     231    
     232    def _single_fit_completed(self,result,pars,cpage,qmin,qmax,elapsed,ymin=None, ymax=None): 
    226233        """ 
    227234            Display fit result on one page of the notebook. 
     
    233240           
    234241        """ 
     242        #self.done = True 
     243        #wx.PostEvent(self.parent, StatusEvent(status="Fitting Completed: %g" % elapsed)) 
    235244        try: 
    236245            for page, value in self.page_finder.iteritems(): 
     
    260269             
    261270        
    262     def _simul_fit_completed(self,result,qmin,qmax,ymin=None, ymax=None): 
     271    def _simul_fit_completed(self,result,qmin,qmax, elapsed,pars=None,cpage=None, ymin=None, ymax=None): 
    263272        """ 
    264273            Parameter estimation completed,  
     
    267276            @param elapsed: computation time 
    268277        """ 
     278        wx.PostEvent(self.parent, StatusEvent(status="Fitting Completed: %g" % elapsed)) 
    269279        try: 
    270280            for page, value in self.page_finder.iteritems(): 
     
    291301             wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 
    292302             
    293      
     303   
    294304    def _on_single_fit(self,id=None,qmin=None,qmax=None,ymin=None,ymax=None): 
    295305        """  
     
    344354                #of parameter names in the list       
    345355        try: 
    346             result=self.fitter.fit() 
    347             #self._single_fit_completed(result,pars,current_pg,qmin,qmax) 
    348             #print "single_fit: result",result.fitness,result.pvec,result.stderr 
    349             #self._single_fit_completed(result,pars,page,qmin,qmax) 
    350             self._single_fit_completed(result,pars,page_fitted,qmin,qmax,ymin,ymax) 
     356            # If a thread is already started, stop it 
     357            if self.calc_thread != None and self.calc_thread.isrunning(): 
     358                self.calc_thread.stop() 
     359                     
     360            self.calc_thread =FitThread(parent =self.parent, 
     361                                        fn= self.fitter, 
     362                                        pars= pars, 
     363                                        cpage= page_fitted, 
     364                                       qmin=qmin, 
     365                                       qmax=qmax, 
     366                                       ymin= ymin, 
     367                                       ymax= ymax, 
     368                                       completefn=self._single_fit_completed, 
     369                                       updatefn=None) 
     370            self.calc_thread.queue() 
     371            self.calc_thread.ready(2.5) 
     372            #while not self.done: 
     373                #print "when here" 
     374             #   time.sleep(1) 
     375             
     376            
    351377        except: 
    352378            raise 
     
    412438        #Do the simultaneous fit 
    413439        try: 
    414             result=self.fitter.fit() 
    415             self._simul_fit_completed(result,qmin,qmax,ymin,ymax) 
     440            # If a thread is already started, stop it 
     441            if self.calc_thread != None and self.calc_thread.isrunning(): 
     442                self.calc_thread.stop() 
     443                     
     444            self.calc_thread =FitThread(parent =self.parent, 
     445                                        fn= self.fitter, 
     446                                       qmin=qmin, 
     447                                       qmax=qmax, 
     448                                       ymin= ymin, 
     449                                       ymax= ymax, 
     450                                       completefn= self._simul_fit_completed, 
     451                                       updatefn=None) 
     452            self.calc_thread.queue() 
     453            self.calc_thread.ready(2.5) 
     454             
    416455        except: 
    417456            wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) 
     
    479518        self.plot_helper(currpage=page,qmin= qmin,qmax= qmax) 
    480519         
    481     def plot_helper(self,currpage,qmin=None,qmax=None,ymin=None,ymax=None): 
     520    def plot_helper(self,currpage, fitModel=None, qmin=None,qmax=None,ymin=None,ymax=None): 
    482521        """ 
    483522            Plot a theory given a model and data 
     
    654693                except: 
    655694                    raise 
    656     def update(self, output,elapsed): 
    657         print "Got an update", elapsed 
    658         wx.PostEvent(self.parent, StatusEvent(status="Plot \ 
    659         updating ... %g sec" % elapsed)) 
     695    def update(self, output,time): 
     696        pass 
    660697     
    661698    def complete(self, output, elapsed, model, qmin, qmax): 
    662         #printEVT("Calc complete in %g sec" % elapsed)  
     699        
    663700        wx.PostEvent(self.parent, StatusEvent(status="Calc \ 
    664701        complete in %g sec" % elapsed)) 
     
    723760                                       qmax=qmax, 
    724761                            completefn=self.complete, 
    725                             updatefn=self.update) 
     762                            updatefn=None) 
    726763            self.calc_thread.queue() 
    727764            self.calc_thread.ready(2.5) 
  • sansview/perspectives/fitting/model_thread.py

    rd15c0202 re9b4cc4  
    109109        self.model = model 
    110110        self.starttime = 0 
     111        
    111112         
    112113    def isquit(self): 
     
    120121            raise KeyboardInterrupt 
    121122         
     123    def update(self, output=None, time=None): 
     124         
     125        wx.PostEvent(self.parent, StatusEvent(status="Plot \ 
     126        updating ... %g sec" % time)) 
     127         
    122128    def compute(self): 
    123129        import numpy 
     
    125131        y = self.y 
    126132        output = numpy.zeros((len(x),len(y))) 
    127         """ 
    128         if self.qmin *self.qmax >=0: 
    129             print "same signe plotting" 
    130             if self.qmax==0: 
    131                 center_x= self.qmin/2 
    132                 center_y= self.qmin /2 
    133             else: 
    134                 center_x= (self.qmax -self.qmin)/2 
    135                 center_y= (self.qmax -self.qmin)/2 
    136         else: 
    137         """ 
     133       
    138134        center_x=0 
    139135        center_y=0 
    140         #print "center_x , center_y",center_x , center_y 
    141         #print "x ",len(x) 
    142         #print "y", y 
    143         #print "int(len(self.x)/2)",int(len(self.x)/2) 
     136        
    144137        self.starttime = time.time() 
     138        wx.PostEvent(self.parent, StatusEvent(status=\ 
     139                       "Start Drawing model %g " % self.starttime)) 
    145140        lx = len(self.x) 
    146141        
    147142        for i_x in range(len(self.x)): 
    148             
    149             # Check whether we need to bail out 
    150             self.update(output=output) 
     143            # Check whether we need to bail out 
     144            self.update(output=output, time=time.time() ) 
    151145            self.isquit() 
    152146             
     
    154148                try: 
    155149                    if (self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) \ 
    156                     < self.qmin * self.qmin: 
    157                         output[i_x] [i_y]=0 
    158                          
     150                        < self.qmin * self.qmin: 
     151                         
     152                        output[i_x] [i_y]=0   
    159153                    else: 
    160                         value1 = self.model.runXY([self.x[i_x]-center_x, self.y[i_y]-center_y]) 
    161                         
    162                          
    163                         output[i_x] [i_y]=value1  
    164                         
    165                      
     154                        value = self.model.runXY([self.x[i_x]-center_x, self.y[i_y]-center_y]) 
     155                        output[i_x] [i_y]=value    
    166156                except: 
    167157                     wx.PostEvent(self.parent, StatusEvent(status=\ 
  • sansview/perspectives/fitting/modelpage.py

    rd23544dc re9b4cc4  
    402402                self.qmax = float(self.xmax.GetValue()) 
    403403                is_modified = True 
    404             if float(self.npts.GetValue()) !=  self.num_points: 
    405                 self.qmax = float(self.npts.GetValue()) 
    406                 is_modified = True 
     404             
     405                if float(self.npts.GetValue()) !=  self.num_points: 
     406                    self.qmax = float(self.npts.GetValue()) 
     407                    is_modified = True 
     408           
    407409             
    408410            if is_modified: 
Note: See TracChangeset for help on using the changeset viewer.