Ignore:
Timestamp:
Mar 6, 2009 11:19:04 AM (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:
925a30e
Parents:
d72cee0
Message:

comment added on fitting

File:
1 edited

Legend:

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

    r54905de rf93dfcb  
    8686        #menu for model 
    8787        menu2 = wx.Menu() 
    88      
    8988        self.menu_mng.populate_menu(menu2, owner) 
    9089        id2 = wx.NewId() 
    9190        owner.Bind(models.EVT_MODEL,self._on_model_menu) 
    92        
     91        ## Allow fitpanel to access list of models 
    9392        self.fit_panel.set_owner(owner) 
    9493        self.fit_panel.set_model_list(self.menu_mng.get_model_list()) 
     
    9897        return [(id, self.menu1, "Fitting"),(id2, menu2, "Model")] 
    9998     
     99     
    100100    def on_add_sim_page(self, event): 
    101101        """ 
     102            Create a page to access simultaneous fit option 
    102103        """ 
    103104        self.sim_page= self.fit_panel.add_sim_page() 
     
    110111            TODO: replace the text with a nice image 
    111112        """ 
    112         #from helpDialog import  HelpWindow 
    113         #dialog = HelpWindow(None, -1, 'HelpWindow') 
    114         #if dialog.ShowModal() == wx.ID_OK: 
    115         #    pass 
    116         #dialog.Destroy() 
    117113        from helpPanel import  HelpWindow 
    118114        frame = HelpWindow(None, -1, 'HelpWindow')     
     
    132128                          "Dialog with fitting parameters ", self._onSelect]]  
    133129            else: 
    134                 #if item.name==graph.selected_plottable and\ 
    135                 # item.__class__.__name__ is  "Data1D": 
     130                
    136131                if item.name==graph.selected_plottable : 
    137132                    return [["Select data  for Fitting", \ 
     
    147142        # Creation of the fit panel 
    148143        self.fit_panel = FitPanel(self.parent, -1) 
    149         #Set the manager forthe main panel 
     144        #Set the manager for the main panel 
    150145        self.fit_panel.set_manager(self) 
    151146        # List of windows used for the perspective 
     
    160155        self.parent.Bind( ERR_DATA, self._on_data_error) 
    161156         
    162         #create the fitting panel 
    163         #return [self.fit_panel] 
    164          
     157        #Send the fitting panel to guiframe 
    165158        self.mypanels.append(self.fit_panel) 
    166159        return self.mypanels 
     
    169162    def _on_slicer_event(self, event): 
    170163        """ 
    171         """ 
    172         #print "fitting:slicer event ", event.panel 
     164            Receive a panel as event and send it to guiframe  
     165            @param event: event containing a panel 
     166        """ 
    173167        if event.panel!=None: 
    174168            new_panel = event.panel 
     
    214208    def copy_data(self, item, dy): 
    215209        """ 
     210            receive a data 1D and the list of errors on dy 
     211            and create a new data1D data 
     212            @param return  
    216213        """ 
    217214        detector=None 
     
    243240        for item in self.panel.graph.plottables: 
    244241            if item.name == self.panel.graph.selected_plottable: 
     242                ## reset the error back to data 
    245243                if len(self.err_dy)>0: 
    246244                    if item.name in  self.err_dy.iterkeys(): 
     
    258256            else: 
    259257                data= item 
    260                          
    261                          
     258            ## create anew page                    
    262259            if item.name == self.panel.graph.selected_plottable or\ 
    263260                 item.__class__.__name__ is "Data2D": 
    264                 #find a name for the page created for notebook 
    265                 #print "fitting", self.panel 
    266261                try: 
    267                     
    268                     #page = self.fit_panel.add_fit_page(item) 
    269262                    page = self.fit_panel.add_fit_page(data) 
    270                     #page, model_name = self.fit_panel.add_fit_page(item) 
    271263                    # add data associated to the page created 
    272                     
    273264                    if page !=None:    
    274265                        #create a fitproblem storing all link to data,model,page creation 
    275266                        self.page_finder[page]= FitProblem() 
    276                         #self.page_finder[page].save_model_name(model_name)   
     267                        ## item is almost the same as data but contains 
     268                        ## axis info for plotting  
    277269                        self.page_finder[page].add_plotted_data(item) 
    278270                        self.page_finder[page].add_fit_data(data) 
    279                         
    280                              
     271                         
    281272                        wx.PostEvent(self.parent, StatusEvent(status="Page Created")) 
    282273                    else: 
    283274                        wx.PostEvent(self.parent, StatusEvent(status="Page was already Created")) 
    284275                except: 
    285                     #raise 
    286276                    wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ 
    287277                    %sys.exc_value)) 
     
    291281    def schedule_for_fit(self,value=0,fitproblem =None):   
    292282        """ 
    293          
     283            Set the fit problem field to 0 or 1 to schedule that problem to fit. 
     284            Schedule the specified fitproblem or get the fit problem related to  
     285            the current page and set value. 
     286            @param value : integer 0 or 1  
     287            @param fitproblem: fitproblem to schedule or not to fit 
    294288        """    
    295289        if fitproblem !=None: 
     
    316310             @note: expecting park used for fit. 
    317311        """   
    318         #sim_page=self.fit_panel.GetPage(1)  
    319312        sim_page= self.sim_page 
    320313        for page, value in self.page_finder.iteritems(): 
    321314            if page != sim_page: 
    322315                list=value.get_model() 
    323                 model=list[0] 
    324                 #print "fitting",model.name,modelname, values 
     316                model = list[0] 
    325317                if model.name== modelname: 
    326318                    value.set_model_param(names,values) 
     
    354346           
    355347        """ 
    356          
    357         #print "single fit ", pars,result.pvec,result.stderr,result.fitness 
    358348        wx.PostEvent(self.parent, StatusEvent(status="Single fit \ 
    359         complete " , type="stop")) 
     349        complete! " , type="stop")) 
    360350        try: 
    361351            for page, value in self.page_finder.iteritems(): 
    362352                if page==cpage : 
    363                     #fitdata = value.get_data() 
    364353                    list = value.get_model() 
    365354                    model= list[0] 
    366355                    break 
     356            ## reset the current model with new fitted parameters     
    367357            i = 0 
    368            
    369358            for name in pars: 
    370359                if result.pvec.__class__==numpy.float64: 
    371360                    model.setParam(name,result.pvec) 
    372361                else: 
    373                     #print "fitting result : chisqr",result.fitness 
    374                     #print "fitting result : pvec",result.pvec 
    375                     #print "fitting result : stderr",result.stderr 
    376362                    model.setParam(name,result.pvec[i]) 
    377                     #print "fitting: single fit", name, result.pvec[i] 
    378363                    i += 1 
    379             
    380             
     364            ## Reset values of the current page to fit result 
    381365            cpage.onsetValues(result.fitness, result.pvec,result.stderr) 
    382             #title="Fitted model 2D " 
     366            ## plot the current model with new param 
    383367            self.plot_helper(currpage=cpage,qmin=qmin,qmax=qmax, 
    384368                             ymin=ymin, ymax=ymax, 
    385369                             xmin=xmin, xmax=xmax,title=None) 
    386370        except: 
    387             #raise 
    388             wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 
     371            msg= "Single Fit completed but Following error occurred:" 
     372            wx.PostEvent(self.parent, StatusEvent(status="%s %s" % (msg, sys.exc_value))) 
    389373            return 
    390374        
     
    399383        """ 
    400384        if cpage!=None: 
     385            ## the fit perform was a single fit but selected  from the  
     386            ## simultaneous fit page 
    401387            self._single_fit_completed(result=result,pars=pars,cpage=cpage, 
    402388                                       qmin=qmin,qmax=qmax, 
     
    406392            wx.PostEvent(self.parent, StatusEvent(status="Simultaneous fit \ 
    407393            complete ", type="stop")) 
    408             #print "result",cpage,str(result),result.parameters,result.fitness,result.stderr 
    409              
     394            ## fit more than 1 model at the same time  
    410395            try: 
    411396                for page, value in self.page_finder.iteritems(): 
    412397                    if value.get_scheduled()==1: 
    413                         #fitdata = value.get_data() 
    414398                        list = value.get_model() 
    415399                        model= list[0] 
     
    427411                                small_cov.append(p.stderr) 
    428412                                model.setParam(param_name,p.value)   
     413                                 
    429414                        # Display result on each page  
    430                         #print "mmmm",p.value,p.stderr 
    431415                        page.onsetValues(result.fitness, small_out,small_cov) 
    432                         #print "out,err",small_out,small_cov 
    433                         #Replot model 
     416                        #Replot models 
     417                        msg= "Single Fit completed plotting %s:"%model.name 
     418                        wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) 
    434419                        self.plot_helper(currpage= page,qmin= qmin,qmax= qmax, 
    435420                                         xmin=xmin, xmax=xmax, 
    436421                                         ymin=ymin, ymax=ymax)  
    437422            except: 
    438                 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) 
    439                 return 
    440              
    441              
     423                 msg= "Simultaneous Fit completed but Following error occurred: " 
     424                 wx.PostEvent(self.parent, StatusEvent(status="%s%s" %(msg,sys.exc_value))) 
     425                 return  
     426              
     427              
    442428    def stop_fit(self): 
    443429        """ 
     430            Stop the fit engine 
    444431        """ 
    445432        if self.calc_thread != None and self.calc_thread.isrunning(): 
    446             #self.calc_thread.interrupt() 
    447433            self.calc_thread.stop() 
    448434            wx.PostEvent(self.parent, StatusEvent(status="Fitting  \ 
     
    458444             
    459445        """ 
    460         #print "in single fitting" 
    461446        #set an engine to perform fit 
    462447        from sans.fit.Fitting import Fit 
    463         self.fitter= Fit(self._fit_engine) 
     448        self.fitter = Fit(self._fit_engine) 
    464449        #Setting an id to store model and data in fit engine 
    465         self.fit_id= 0 
     450        self.fit_id = 0 
    466451        if id!=None: 
    467             self.fit_id= id 
    468          
    469         page_fitted=None 
    470         fit_problem=None 
     452            self.fit_id = id 
     453         
     454        page_fitted = None 
     455        fit_problem = None 
    471456        #Get information (model , data) related to the page on  
    472457        #with the fit will be perform 
    473         current_pg=self.fit_panel.get_current_page()  
    474         #simul_pg=self.fit_panel.GetPage(1 ) 
    475         simul_pg=self.sim_page 
     458        current_pg= self.fit_panel.get_current_page()  
     459        simul_pg= self.sim_page 
    476460        pars=[]    
    477         #for page, value in self.page_finder.iteritems(): 
    478         if current_pg!= simul_pg: 
    479         #    if  value.get_scheduled() ==1 : 
     461        ## Check that the current page is different from self.sim_page 
     462        if current_pg != simul_pg: 
    480463            value = self.page_finder[current_pg] 
    481464            metadata =  value.get_fit_data() 
    482465            list = value.get_model() 
    483466            model = list[0] 
    484             smearer= value.get_smearer() 
     467            smearer = value.get_smearer() 
    485468            
    486      
    487469            #Create list of parameters for fitting used 
    488              
    489470            templist=[] 
    490471            try: 
    491                 #templist=current_pg.get_param_list() 
    492                 templist=current_pg.get_param_list() 
     472                ## get the list of parameter names to fit 
     473                templist = current_pg.get_param_list() 
    493474                for element in templist: 
    494475                    pars.append(str(element[0].GetLabelText())) 
    495476                pars.sort() 
     477                  
    496478                #Do the single fit 
    497                 self.fitter.set_model(Model(model), self.fit_id, pars)  
    498                 if self._fit_engine=="scipy": 
    499                     wx.PostEvent(self.parent, StatusEvent(status="Parameters range will\ 
    500                      be ignored for %s fitting engine"%str(self._fit_engine))) 
    501                 #print "args...:",metadata,self.fit_id,smearer,qmin,qmax,ymin,ymax 
     479                self.fitter.set_model(Model(model), self.fit_id, pars) 
    502480                dy=[] 
    503481                x=[] 
    504482                y=[] 
     483                ## checking the validity of error 
    505484                if metadata.__class__ in  ["Data1D","Theory1D"]: 
    506485                    for i in range(len(metadata.dy)): 
     
    516495                        metadata.x=numpy.zeros(len(x)) 
    517496                        metadata.x=x 
     497                         
    518498                self.fitter.set_data(data=metadata,Uid=self.fit_id, 
    519499                                     smearer=smearer,qmin= qmin,qmax=qmax, 
    520500                                     ymin=ymin,ymax=ymax) 
    521                 self.fitter.select_problem_for_fit(Uid=self.fit_id,value=value.get_scheduled()) 
     501                 
     502                self.fitter.select_problem_for_fit(Uid= self.fit_id, 
     503                                                   value= value.get_scheduled()) 
    522504                page_fitted=current_pg 
    523                 #self.fit_id+=1 
    524                 #self.schedule_for_fit( 0,value)  
     505                
    525506            except: 
    526                 wx.PostEvent(self.parent, StatusEvent(status="Single Fit error: %s" % sys.exc_value)) 
     507                msg= "Single Fit error: %s" % sys.exc_value 
     508                wx.PostEvent(self.parent, StatusEvent(status= msg )) 
    527509                return 
    528510            # make sure to keep an alphabetic order  
    529511            #of parameter names in the list       
    530512            try: 
    531                 # If a thread is already started, stop it 
     513                ## If a thread is already started, stop it 
    532514                if self.calc_thread != None and self.calc_thread.isrunning(): 
    533515                    self.calc_thread.stop() 
     
    566548             self.fit_id= id 
    567549        
    568          
    569550        for page, value in self.page_finder.iteritems(): 
    570551            try: 
     
    573554                    list = value.get_model() 
    574555                    model= list[0] 
    575                     #Create dictionary of parameters for fitting used 
     556                    ## store page 
    576557                    cpage= page 
    577                     print "" 
     558                    #Get list of parameters name to fit 
    578559                    pars = [] 
    579560                    templist = [] 
     
    586567                            wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 
    587568                            return 
    588                     # need to check this print "new model " 
    589                      
     569                    ## create a park model and reset parameter value if constraint 
     570                    ## is given 
    590571                    new_model=Model(model) 
    591572                    param=value.get_model_param() 
     
    595576                            param_value = item[1] 
    596577                            param_name = item[0] 
    597      
    598578                            new_model.parameterset[ param_name].set( param_value ) 
    599579                         
    600580                    self.fitter.set_model(new_model, self.fit_id, pars)  
    601                     #print "sim-->model",metadata,model,self.fit_id, pars 
     581                    ## check that non -zero value are send as dy in the fit engine 
    602582                    dy=[] 
    603583                    x=[] 
     
    616596                                metadata.x=numpy.zeros(len(x)) 
    617597                                metadata.x=x 
     598                                 
    618599                    self.fitter.set_data(metadata,self.fit_id,qmin,qmax,ymin,ymax) 
    619                     #print "sim---->value of problem",value.get_scheduled() 
    620                     self.fitter.select_problem_for_fit(Uid=self.fit_id,value=value.get_scheduled()) 
     600                    self.fitter.select_problem_for_fit(Uid= self.fit_id, 
     601                                                       value= value.get_scheduled()) 
    621602                    self.fit_id += 1  
    622603                    value.clear_model_param() 
    623                     #self.schedule_for_fit( 0,value)  
     604                    
    624605            except: 
    625                 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 
     606                msg= "Simultaneous Fit error: %s" % sys.exc_value 
     607                wx.PostEvent(self.parent, StatusEvent(status= msg )) 
    626608                return  
     609             
    627610        #Do the simultaneous fit 
    628611        try: 
    629             # If a thread is already started, stop it 
     612            ## If a thread is already started, stop it 
    630613            if self.calc_thread != None and self.calc_thread.isrunning(): 
    631614                self.calc_thread.stop() 
     615            ## Perform a single fit , then need to page  
    632616            if  self.fit_id==1: 
    633617                self.calc_thread =FitThread(parent =self.parent, 
     
    643627                       
    644628            else: 
     629                ## Perform more than 1 fit at the time 
    645630                self.calc_thread =FitThread(parent =self.parent, 
    646631                                        fn= self.fitter, 
     
    655640             
    656641        except: 
    657             wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 
     642            msg= "Simultaneous Fit error: %s" % sys.exc_value 
     643            wx.PostEvent(self.parent, StatusEvent(status= msg )) 
    658644            return 
    659645         
     
    661647    def _onset_engine(self,event): 
    662648        """  
    663             set engine to scipy 
     649            Toggle engine name from "park" to "scipy" or reverse if event received 
     650            @param event: wx.menu item checked 
    664651        """ 
    665652        if self._fit_engine== 'park': 
     
    667654        else: 
    668655            self._on_change_engine('park') 
    669         wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) 
     656             
     657        msg= "Engine set to: %s" % self._fit_engine 
     658        wx.PostEvent(self.parent, StatusEvent(status= msg )) 
    670659   
    671660     
     
    681670        """ 
    682671            react to model selection on any combo box or model menu.plot the model   
     672            @param evt: wx.combobox event 
    683673        """ 
    684674        
    685675        model = evt.model 
    686676        name = evt.name 
    687          
    688         #print "name fitting", name 
    689         #sim_page=self.fit_panel.GetPage(1) 
     677        
    690678        sim_page=self.sim_page 
    691679        current_pg = self.fit_panel.get_current_page()  
     680        ## make sure nothing is done on self.sim_page 
     681        ## example trying to call set_panel on self.sim_page 
    692682        if current_pg != sim_page: 
    693683            current_pg.set_panel(model) 
    694             #model.name = self.page_finder[current_pg].get_name() 
    695             #print "model name", model.name 
     684            
    696685            model.name="M"+str(self.index_model) 
    697686            try: 
     
    702691            self.index_model += 1   
    703692             
    704              
    705693            # save model name 
    706              
    707694            # save the name containing the data name with the appropriate model 
    708695            self.page_finder[current_pg].set_model(model,M_name) 
    709696            self.plot_helper(currpage= current_pg,qmin= None,qmax= None) 
     697            ## draw  self.sim_page and store information on the selected model 
    710698            if self.sim_page!=None: 
    711699                self.sim_page.add_model(self.page_finder) 
    712700         
     701         
     702         
    713703    def  set_smearer(self,smearer): 
    714704        """ 
    715          
     705            Get a smear object and store it to a fit problem 
     706            @param smearer: smear object to allow smearing data 
    716707        """    
    717708        current_pg=self.fit_panel.get_current_page() 
    718709        self.page_finder[current_pg].set_smearer(smearer) 
     710          
    719711          
    720712    def redraw_model(self,qmin= None,qmax= None): 
     
    730722        self.plot_helper(currpage=page,qmin= qmin,qmax= qmax) 
    731723         
    732     def plot_helper(self,currpage, fitModel=None, qmin=None,qmax=None, 
     724         
     725         
     726    def plot_helper(self,currpage, qmin=None,qmax=None, 
    733727                    ymin=None,ymax=None, xmin=None, xmax=None,title=None ): 
    734728        """ 
     
    740734            for page in self.page_finder.iterkeys(): 
    741735                if  page==currpage :   
    742                     data=self.page_finder[page].get_plotted_data() 
    743                     list=self.page_finder[page].get_model() 
     736                    data= self.page_finder[page].get_plotted_data() 
     737                    list= self.page_finder[page].get_model() 
     738                    smearer= self.page_finder[page].get_smearer() 
    744739                    model=list[0] 
    745740                    break  
    746             #print "model in fitting",model 
     741            ## create Model 1D 
    747742            if data!=None and data.__class__.__name__ != 'Data2D': 
    748                 theory = Theory1D(x=[], y=[]) 
     743                x= numpy.zeros(len(data.x))   
     744                y= numpy.zeros(len(data.x))  
     745                 
     746                theory = Theory1D(x=x, y=y) 
    749747                theory.name = model.name 
    750748                theory.group_id = data.group_id 
    751749                theory.id = "Model" 
    752                     
     750                 
     751                  
     752                 
    753753                x_name, x_units = data.get_xaxis()  
    754754                y_name, y_units = data.get_yaxis()  
     
    759759                if qmax == None : 
    760760                   qmax = max(data.x) 
     761                j=0 
    761762                try: 
    762763                    tempx = qmin 
    763764                    tempy = model.run(qmin) 
    764                     theory.x.append(tempx) 
    765                     theory.y.append(tempy) 
     765                    theory.x[j]=tempx 
     766                    theory.y[j]=tempy 
     767                    j+=1 
    766768                except : 
    767769                        wx.PostEvent(self.parent, StatusEvent(status="fitting \ 
     
    773775                            tempx = data.x[i] 
    774776                            tempy = model.run(tempx) 
    775                             theory.x.append(tempx)  
    776                             theory.y.append(tempy) 
     777                             
     778                            if j< i: 
     779                                theory.x[j]=tempx  
     780                                theory.y[j]=tempy 
     781                                j+=1 
    777782                            
    778783                    except: 
     
    782787                    tempx = qmax 
    783788                    tempy = model.run(qmax) 
    784                     theory.x.append(tempx) 
    785                     theory.y.append(tempy) 
     789                 
     790                    theory.x[j]=tempx  
     791                    theory.y[j]=tempy 
     792                    j+=1 
    786793                except: 
    787794                    wx.PostEvent(self.parent, StatusEvent(status="fitting \ 
    788795                        skipping point x %g %s" %(qmin, sys.exc_value)) ) 
    789                      
     796                 
     797                if smearer!=None: 
     798                    theory.y= smearer(theory.y)    
    790799                wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 
    791800                                                title=str(data.name))) 
    792801            else: 
    793                 
     802                ## create Model 2D 
    794803                theory=Data2D(data.data, data.err_data) 
    795804                theory.name= model.name 
     
    805814                theory.y_bins= data.y_bins 
    806815                tempy=[] 
    807                 #print "max x,y",max(data.xmax,data.xmin),max(data.ymax,data.ymin) 
     816                 
    808817                if qmin==None: 
    809                     qmin=0#min(math.fabs(data.xmax),math.fabs(data.ymin)) 
     818                    qmin=0 
    810819                if qmax==None: 
    811                     qmax=math.sqrt(math.pow(max(math.fabs(data.xmax),math.fabs(data.xmin)),2)/ 
    812                                    +math.pow(max(math.fabs(data.ymax),math.fabs(data.ymin)),2)) 
     820                    x= math.pow(max(math.fabs(data.xmax),math.fabs(data.xmin)),2) 
     821                    y= math.pow(max(math.fabs(data.ymax),math.fabs(data.ymin)),2) 
     822                    qmax=math.sqrt( x+y ) 
     823                     
    813824                if ymin==None: 
    814825                    ymin=data.ymin 
     
    823834                for j in range(len(data.y_bins)): 
    824835                    for i in range(len(data.x_bins)): 
    825                         tempqij=math.sqrt((math.pow(data.y_bins[j],2)+math.pow(data.x_bins[i],2))) 
     836                        tempqij=math.sqrt(( math.pow(data.y_bins[j],2)\ 
     837                                           +math.pow(data.x_bins[i],2) )) 
     838                         
    826839                        if tempqij>= qmin and tempqij<= qmax:  
    827                             theory.data[j][i]=model.runXY([data.y_bins[j],data.x_bins[i]]) 
     840                            theory.data[j][i] = model.runXY([data.y_bins[j], 
     841                                                             data.x_bins[i]] ) 
    828842                        else: 
    829                             theory.data[j][i]=0 #None # Later, We need to decide which of  0 and None is better. 
    830                 #print "len(data.x_bins),len(data.y_bins);",len(data.x_bins),len(data.y_bins),qmax 
    831                 #print "fitting : plot_helper:", theory.image 
    832                 #print "fitting : plot_helper:",theory.image 
     843                            ## Later, We need to decide which of  0 and None is better. 
     844                            theory.data[j][i]=0  
     845              
    833846                theory.detector= data.detector 
    834847                theory.source= data.source 
    835                  
     848                ## qmin, qmax will used later to define q range. 
    836849                theory.qmin= qmin 
    837850                theory.qmax= qmax 
     851                ## plot boundaries 
    838852                theory.ymin= ymin 
    839853                theory.ymax= ymax 
     
    848862        """ 
    849863            Plot a theory from a model selected from the menu 
     864            @param evt: wx.menu event 
    850865        """ 
    851866        name = evt.model.__name__ 
    852867        if hasattr(evt.model, "name"): 
    853868            name = evt.model.name 
    854         #print "on model menu", name 
    855869        model=evt.model() 
    856870        description=model.description 
     
    861875        self.fit_panel.add_model_page(model,description,name,topmenu=True) 
    862876         
    863     def draw_model(self,model,name ,data=None,description=None,enable1D=True, enable2D=False, 
    864                    qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 
    865         """ 
    866              draw model with default data value 
    867         """ 
     877         
     878    def draw_model(self, model, name, data= None, description= None, 
     879                   enable1D= True, enable2D= False, 
     880                   qmin= DEFAULT_QMIN, qmax= DEFAULT_QMAX, qstep= DEFAULT_NPTS): 
     881        """ 
     882             Draw model. 
     883             @param model: the model to draw 
     884             @param name: the name of the model to draw 
     885             @param data: the data on which the model is based to be drawn 
     886             @param description: model's description 
     887             @param enable1D: if true enable drawing model 1D 
     888             @param enable2D: if true enable drawing model 2D 
     889             @param qmin:  Range's minimum value to draw model 
     890             @param qmax:  Range's maximum value to draw model 
     891             @param qstep: number of step to divide the x and y-axis 
     892              
     893        """ 
     894        ## draw model based on loaded data 
    868895        if data !=None: 
    869896            self.redraw_model(qmin,qmax) 
    870897            return  
     898        ## draw model 1D with no loaded data 
    871899        self._draw_model1D(model,name,model.description, enable1D,qmin,qmax, qstep) 
     900        ## draw model 2D with no initial data 
    872901        self._draw_model2D(model=model, 
    873902                           description=model.description, 
     
    878907         
    879908               
    880     def _draw_model1D(self,model,name,description=None, enable1D=True, 
    881                       qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 
    882         """ 
    883          
    884         """ 
    885          
    886         if enable1D: 
    887             x=  numpy.linspace(start= qmin, 
    888                                stop= qmax, 
    889                                num= qstep, 
    890                                endpoint=True 
    891                                )       
    892             xlen= len(x) 
    893             y = numpy.zeros(xlen) 
    894             if not enable1D: 
    895                 for i in range(xlen): 
    896                     y[i] = model.run(x[i]) 
     909    def _draw_model1D(self, model, name, description= None, enable1D= True, 
     910                      qmin= DEFAULT_QMIN, qmax= DEFAULT_QMAX, qstep= DEFAULT_NPTS): 
     911        """ 
     912            Draw model 1D between qmin and qmax value 
     913            @param model: the model to draw 
     914            @param name: the name of the model 
     915            @param description: descripion of the model as string 
     916            @param enable1D:  allow to draw model 1D if True  else False 
     917            @param qmin: the minimum value of the range to draw the model 
     918            @param qmax: the maximum value of the range to draw the model 
     919            @param qstep: the number of points to draw   
     920        """ 
     921        x=  numpy.linspace(start= qmin, 
     922                           stop= qmax, 
     923                           num= qstep, 
     924                           endpoint=True 
     925                           )       
     926        xlen= len(x) 
     927        y = numpy.zeros(xlen) 
     928         
     929        if  enable1D: 
     930            for i in range(xlen): 
     931                y[i] = model.run(x[i])  
     932            try: 
     933                new_plot = Theory1D(x, y) 
     934                new_plot.name = name 
     935                new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
     936                new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
     937                new_plot.id ="Model" 
     938                new_plot.group_id ="Model" 
     939                # Pass the reset flag to let the plotting event handler 
     940                # know that we are replacing the whole plot 
     941                wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 
     942                                 title="Analytical model 1D ", reset=True )) 
    897943                 
    898                 try: 
    899                     new_plot = Theory1D(x, y) 
    900                      
    901                     new_plot.name = name 
    902                     new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    903                     new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
    904                     new_plot.id = "Model" 
    905                     new_plot.group_id ="Model" 
    906                     wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Analytical model 1D")) 
    907                 except: 
    908                     wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) 
    909                     #raise 
    910                     return 
    911             else: 
    912                 for i in range(xlen): 
    913                     y[i] = model.run(x[i])  
    914                 try: 
    915                     new_plot = Theory1D(x, y) 
    916                     #print "draw model 1D", name 
    917                     new_plot.name = name 
    918                     new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    919                     new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
    920                     new_plot.id ="Model" 
    921                     new_plot.group_id ="Model" 
    922                      
    923                     # Pass the reset flag to let the plotting event handler 
    924                     # know that we are replacing the whole plot 
    925                     wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 
    926                                      title="Analytical model 1D ", reset=True )) 
    927                      
    928                 except: 
    929                     #raise 
    930                     wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) 
    931                     return 
    932      
    933      
     944            except: 
     945                msg= " Error occurred when drawing %s Model 1D: "%new_plot.name 
     946                msg+= " %s"%sys.exc_value 
     947                wx.PostEvent( self.parent, StatusEvent(status= msg )) 
     948                return 
     949 
     950 
    934951     
    935952    def update(self, output,time): 
    936953        """ 
     954            Update the output of plotting model 
    937955        """ 
    938956        pass 
     
    940958    def complete(self, output, elapsed, model, qmin, qmax,qstep=DEFAULT_NPTS): 
    941959        """ 
    942         """ 
    943         wx.PostEvent(self.parent, StatusEvent(status="Calc \ 
    944         complete !" , type="stop")) 
    945         #print "complete",output, model,qmin, qmax 
     960            Complete get the result of modelthread and create model 2D 
     961            that can be plot. 
     962        """ 
     963        msg = "Calc complete !" 
     964        wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) 
     965     
    946966        data = output 
    947967        temp= numpy.zeros(numpy.shape(data)) 
     
    950970     
    951971        from DataLoader.data_info import Detector, Source 
    952          
    953972        detector = Detector() 
    954973        theory.detector=[] 
    955974        theory.detector.append(detector)  
    956975             
    957         theory.detector[0].distance=1e+32#13705.0 
     976        theory.detector[0].distance=1e+32 
    958977        theory.source= Source() 
    959         theory.source.wavelength=2*math.pi/1e+32#8.4 
     978        theory.source.wavelength=2*math.pi/1e+32 
    960979        theory.x_bins =[] 
    961980        theory.y_bins =[] 
    962         #Now qmax is xmax. 
    963         xmax=2*theory.detector[0].distance*math.atan(qmax/(4*math.pi/theory.source.wavelength)) 
    964         theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5)#5.0 
    965         theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5)#5.0 
     981        ## Create detector for Model 2D 
     982        xmax=2*theory.detector[0].distance*math.atan(\ 
     983                            qmax/(4*math.pi/theory.source.wavelength)) 
     984         
     985        theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5) 
     986        theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5) 
    966987        theory.detector[0].beam_center.x= qmax 
    967988        theory.detector[0].beam_center.y= qmax 
    968         #print "xmax,qmax",xmax,qmax 
    969         # compute x_bins and y_bins 
    970         # Qx and Qy vectors 
    971      
     989        ## create x_bins and y_bins of the model 2D 
    972990        distance   = theory.detector[0].distance 
    973991        pixel      = qstep/2-1 
     
    981999         
    9821000        size_x, size_y= numpy.shape(theory.data) 
    983         #print "size_x,size_y",size_x, size_y 
    984         #This case, q and x are equivalent to each other. 
    9851001        for i_x in range(size_x): 
    986             theta = (i_x-center_x)*pixel_width_x / distance #/ size_x#100.0 
     1002            theta = (i_x-center_x)*pixel_width_x / distance  
    9871003            qx = 4.0*math.pi/wavelength * math.tan(theta/2.0) 
    9881004            theory.x_bins.append(qx)     
    9891005        for i_y in range(size_y): 
    990             theta = (i_y-center_y)*pixel_width_y / distance #/ size_y#100.0 
     1006            theta = (i_y-center_y)*pixel_width_y / distance  
    9911007            qy =4.0*math.pi/wavelength * math.tan(theta/2.0) 
    9921008            theory.y_bins.append(qy) 
    9931009            
    994         #theory.err_data= numpy.     
    9951010        theory.name= model.name 
    9961011        theory.group_id ="Model" 
    9971012        theory.id ="Model" 
    998          
    999         
    1000          
    1001         theory.xmin= -qmax#/math.sqrt(2)#You can not do sqrt(2), that changes axis scale not data itself 
    1002         theory.xmax= qmax#/math.sqrt(2) 
    1003         theory.ymin= -qmax#/math.sqrt(2) 
    1004         theory.ymax= qmax#/math.sqrt(2) 
    1005          
    1006         #print "model draw comptele xmax",theory.xmax,model.name 
     1013        ## determine plot boundaries 
     1014        theory.xmin= -qmax 
     1015        theory.xmax= qmax 
     1016        theory.ymin= -qmax 
     1017        theory.ymax= qmax 
     1018        ## plot 
    10071019        wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 
    10081020                         title="Analytical model 2D ", reset=True )) 
     
    10121024    def _draw_model2D(self,model,description=None, enable2D=False, 
    10131025                      qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 
    1014          
     1026        """ 
     1027            draw model in 2D 
     1028            @param model: instance of the model to draw 
     1029            @param description: the description of the model 
     1030            @param enable2D: when True allows to draw model 2D 
     1031            @param qmin: the minimum value to  draw model 2D 
     1032            @param qmax: the maximum value to draw model 2D 
     1033            @param qstep: the number of division of Qx and Qy of the model to draw 
     1034             
     1035        """ 
    10151036        x=  numpy.linspace(start= -1*qmax, 
    10161037                               stop= qmax, 
     
    10221043                               endpoint=True ) 
    10231044        
    1024         lx = len(x) 
    1025         #print x 
    1026         #data=numpy.zeros([len(x),len(y)]) 
    10271045        self.model= model 
    10281046        if enable2D: 
    10291047            try: 
    10301048                from sans.guiframe.model_thread import Calc2D 
    1031                # print "in draw model 2d ",self.model 
    10321049                self.calc_thread = Calc2D(parent =self.parent,x=x, 
    10331050                                           y=y,model= self.model,  
     
    10401057                self.calc_thread.ready(2.5) 
    10411058            except: 
    1042                 raise 
     1059                msg= "Draw model 2D error: " 
     1060                wx.PostEvent(self.parent, StatusEvent(status="%s %s" % sys.exc_value)) 
     1061                return 
    10431062             
    1044     def show_panel2D(self, id=None ): 
    1045         self.parent.show_panel(self.model2D_id) 
     1063   
    10461064            
    10471065    
Note: See TracChangeset for help on using the changeset viewer.