Changeset 81e4cf7 in sasview for sansview


Ignore:
Timestamp:
Feb 28, 2009 1:41:17 PM (15 years ago)
Author:
Mathieu Doucet <doucetm@…>
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:
6d07882a
Parents:
acd91458
Message:

Fixed dispersion problems

File:
1 edited

Legend:

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

    rb491d6e r81e4cf7  
    44import numpy 
    55import copy 
    6  
     6import math 
     7from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion 
    78 
    89from sans.guicomm.events import StatusEvent    
     
    3435        # model on which the fit would be performed 
    3536        self.model=model 
     37        # Data member to store the dispersion object created 
     38        self._disp_obj_dict = {} 
    3639        self.back_up_model= model.clone() 
    3740        #list of dispersion paramaters 
     
    339342            self.disp_box.SetValue("GaussianModel") 
    340343            for k,v in self.polydisp.iteritems(): 
    341                 """ 
    342344                if str(v)=="MyModel": 
    343345                    self.disp_box.Insert("Select customized Model",id)   
    344346                else: 
    345                     self.disp_box.Insert(str(v),id) 
    346                 """ 
    347                 if str(v)!="MyModel": 
    348347                    self.disp_box.Insert(str(v),id)   
    349348                     
     
    585584                self.sizer5.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    586585            
    587             self.parameters.append([cb,ctl1,text2,ctl2]) 
     586                self.parameters.append([cb,ctl1,text2,ctl2]) 
    588587                 
    589588        iy+=1 
     
    640639            raise  
    641640         
    642     def _draw_disp_theta(self):  
    643         path = self._selectDlg() 
    644         dispersion=None 
    645         
    646         for key, value in self.polydisp.iteritems(): 
    647             if value =="MyModel": 
    648                 disp_ph = key() 
    649                 disp_th = key() 
    650                 break 
    651         values,weights = self.read_file(path) 
    652         if values ==None and weights==None: 
    653             return  
    654         import math 
    655         disp_ph.set_weights( values, weights) 
    656         disp_th.set_weights( values, weights) 
    657          
    658         if self.theta_cb !=None: 
    659             angle= self.theta_cb.GetLabelText() 
    660         n=0 
    661         self.disp_list=self.model.getDispParamList() 
    662         if angle.lower().count("theta")>0:    
    663             for param in self.model.getParamList(): 
    664                 if  not param in self.disp_list and  param.lower().count("theta")>0:  
    665                     self.model.set_dispersion(param, disp_th) 
    666                     self.model.dispersion[param]['npts'] = len(values) 
    667                     n+=1 
    668             
    669             if n ==0: 
    670               wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    671                             " Model contains no theta distribution"))   
    672             else: 
    673                 self._draw_model()  
    674     def _draw_disp_Phi(self):  
    675         path = self._selectDlg() 
    676         dispersion=None 
    677         
    678         for key, value in self.polydisp.iteritems(): 
    679             if value =="MyModel": 
    680                 disp_ph = key() 
    681                 disp_th = key() 
    682                 break 
    683         values,weights = self.read_file(path) 
    684         if values ==None and weights==None: 
    685             return  
    686         import math 
    687         disp_ph.set_weights( values, weights) 
    688         disp_th.set_weights( values, weights) 
    689          
    690         if self.theta_cb !=None: 
    691             angle= self.theta_cb.GetLabelText() 
    692         n=0 
    693         self.disp_list=self.model.getDispParamList() 
    694         if angle.lower().count("theta")>0:    
    695             for param in self.model.getParamList(): 
    696                 if  not param in self.disp_list and  param.lower().count("theta")>0:  
    697                     self.model.set_dispersion(param, disp_th) 
    698                     self.model.dispersion[param]['npts'] = len(values) 
    699                     n+=1 
    700             
    701             if n ==0: 
    702               wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    703                             " Model contains no theta distribution"))   
    704             else: 
    705                 self._draw_model()  
    706            
    707641           
    708642    def select_disp_angle(self, event):  
    709         if not self.phi_cb.GetValue(): 
    710             return 
    711         path = self._selectDlg() 
    712         dispersion=None 
    713         
    714         for key, value in self.polydisp.iteritems(): 
    715             if value =="MyModel": 
    716                 disp_ph = key() 
    717                 disp_th = key() 
    718                 
    719                 break 
    720         try: 
    721             values,weights = self.read_file(path) 
    722         except: 
    723             raise 
    724          
    725         if values ==None and weights==None: 
    726             return  
    727         import math 
    728         wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    729                             " Costumized Distribution: %s"% path)) 
    730         
    731         disp_ph.set_weights( values, weights) 
    732         disp_th.set_weights( values, weights) 
    733          
    734         
    735          
    736         if self.theta_cb !=None: 
    737             angle= self.phi_cb.GetLabelText() 
    738         n=0 
    739         #print "hello1",self.model.runXY([0.01,0.01])  
    740         self.disp_list=self.model.getDispParamList() 
    741         name= "phi" 
    742         if angle.lower().count(name)>0:    
    743             for param in self.model.getParamList(): 
    744                 if  not param in self.disp_list and  param.lower().count(name)>0:  
    745                     self.model.set_dispersion(param, disp_th) 
    746                     #print "para, th",param, disp_th 
    747                     #self.model.dispersion[param]['npts'] = len(values) 
    748                     n+=1 
    749             #print "model dispers list",self.model.getDispParamList() 
    750             if n ==0: 
    751               wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    752                             " Model contains no %s distribution"%name))   
    753             else: 
    754                 #print "hello2",self.model.runXY([0.01,0.01])  
    755                 #self._draw_model() 
    756                 qmin=self.qmin_x 
    757                 qmax=self.qmax_x 
    758                 qstep= self.num_points 
    759                 x=  numpy.linspace(start= -1*qmax, 
    760                                stop= qmax, 
    761                                num= qstep, 
    762                                endpoint=True )   
    763                 y = numpy.linspace(start= -1*qmax, 
    764                                stop= qmax, 
    765                                num= qstep, 
    766                                endpoint=True ) 
    767                 output= numpy.zeros([len(x),len(y)]) 
    768                 for i_x in range(int(len(x))): 
    769                     for i_y in range(int(len(y))): 
    770                         if (x[i_x]*x[i_x]+y[i_y]*y[i_y]) \ 
    771                         < qmin * qmin: 
    772                             output[i_x] [i_y]=0    
    773                         else: 
    774                             value = self.model.runXY([x[i_x], y[i_y]]) 
    775                             output[i_x] [i_y]=value  
    776                 #print"modelpage", output 
    777                 self.manager.complete( output=output, elapsed=None, model=self.model, qmin=qmin, qmax=qmax,qstep=qstep) 
    778                 #self._draw_model() 
     643        """ 
     644            Event for when a user select a parameter to average over. 
     645            @param event: check box event 
     646        """ 
     647         
     648         
     649        # Go through the list of dispersion check boxes to identify which one has changed  
     650        for p in self.disp_cb_dict: 
     651            # Catch which one of the box was just checked or unchecked. 
     652            if event.GetEventObject() == self.disp_cb_dict[p]:               
     653 
     654                 
     655                if self.disp_cb_dict[p].GetValue() == True: 
     656                    # The user wants this parameter to be averaged.  
     657                    # Pop up the file selection dialog. 
     658                    path = self._selectDlg() 
     659                     
     660                    # If nothing was selected, just return 
     661                    if path is None: 
     662                        self.disp_cb_dict[p].SetValue(False) 
     663                        return 
     664                     
     665                    try: 
     666                        values,weights = self.read_file(path) 
     667                    except: 
     668                        wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
     669                            "Could not read input file")) 
     670                        return 
     671                     
     672                    # If any of the two arrays is empty, notify the user that we won't 
     673                    # proceed  
     674                    if values is None or weights is None: 
     675                        wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
     676                            "The loaded %s distrubtion is corrupted or empty" % p)) 
     677                        return 
     678                         
     679                    # Tell the user that we are about to apply the distribution 
     680                    wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
     681                            "Applying loaded %s distribution: %s" % (p, path)))   
     682                     
     683                    # Create the dispersion objects 
     684                    disp_model = ArrayDispersion() 
     685                    disp_model.set_weights(values, weights) 
     686                    # Store the object to make it persist outside the scope of this method 
     687                    #TODO: refactor model to clean this up? 
     688                    self._disp_obj_dict[p] = disp_model 
     689                     
     690                    # Set the new model as the dispersion object for the selected parameter 
     691                    self.model.set_dispersion(p, disp_model) 
     692                     
     693                     
     694                    # Redraw the model 
     695                    self._draw_model() 
     696                          
     697                else: 
     698                    # The parameter was un-selected. Go back to Gaussian model (with 0 pts) 
     699                    disp_model = GaussianDispersion() 
     700                    # Store the object to make it persist outside the scope of this method 
     701                    #TODO: refactor model to clean this up? 
     702                    self._disp_obj_dict[p] = disp_model 
     703                     
     704                    # Set the new model as the dispersion object for the selected parameter 
     705                    self.model.set_dispersion(p, disp_model) 
     706                     
     707                    # Redraw the model 
     708                    self._draw_model() 
     709        return 
     710 
    779711                       
    780712                       
     
    797729            self.sizer8.Add(disp1,( iy, ix),(1,1),  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 
    798730             
    799             ix+=1  
    800             self.theta_cb = wx.CheckBox(self, -1,"Theta ", (10, 10)) 
    801             wx.EVT_CHECKBOX(self, self.theta_cb.GetId(), self.select_disp_angle) 
    802             self.sizer8.Add(self.theta_cb,(iy, ix),(1,1),\ 
    803                             wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    804             ix+=1 
    805             self.phi_cb = wx.CheckBox(self, -1,"Phi", (10, 10)) 
    806             wx.EVT_CHECKBOX(self, self.phi_cb.GetId(), self.select_disp_angle) 
    807             self.sizer8.Add(self.phi_cb,(iy, ix),(1,1),\ 
    808                             wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
     731            # Look for model parameters to which we can apply an ArrayDispersion model 
     732            # Add a check box for each parameter. 
     733            self.disp_cb_dict = {} 
     734            for p in self.model.dispersion.keys(): 
     735                ix+=1  
     736                self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) 
     737                 
     738                wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) 
     739                self.sizer8.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    809740             
    810741            ix =0 
     
    968899                     name=str(item[0].GetLabelText()) 
    969900                     value= float(item[1].GetValue()) 
    970                     
    971                      #print "model para", name,value 
    972901                     # If the value of the parameter has changed, 
    973902                     # update the model and set the is_modified flag 
    974903                     if value != self.model.getParam(name): 
    975                          #print "went hereee" 
    976904                         self.model.setParam(name,value) 
    977905                         is_modified = True 
     
    988916                     name=str(item[0]) 
    989917                     value= float(item[1].GetValue()) 
    990                      #print "model para", name,value,self.model.getParam(name) 
    991918                     # If the value of the parameter has changed, 
    992919                     # update the model and set the is_modified flag 
    993920                     if value != self.model.getParam(name): 
    994                          #print "went hereee" 
    995921                         self.model.setParam(name,value) 
    996922                         is_modified = True 
     
    1005931                     name=str(item[0].GetLabelText()) 
    1006932                     value= float(item[1].GetValue()) 
    1007                      #param_min= item[4].GetValue() 
    1008                      #param_max= item[5].GetValue() 
    1009                      #print " fittable model para", name,value 
    1010933                     # If the value of the parameter has changed, 
    1011934                     # update the model and set the is_modified flag 
    1012935                     if value != self.model.getParam(name): 
    1013                          #print "went here", name,value 
    1014936                         self.model.setParam(name,value) 
    1015937                         is_modified = True 
     
    1047969            [Note to coder: This way future changes will be done in only one place.]  
    1048970        """ 
    1049         #print "_draw_model",self.model 
    1050971        if name==None: 
    1051972            name= self.model.name 
Note: See TracChangeset for help on using the changeset viewer.