Changeset b659551 in sasview


Ignore:
Timestamp:
Jun 13, 2008 11:10:42 AM (16 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:
ad8bcd6
Parents:
b00b487
Message:

Added reset button, added option to change number of points on the P(r) plot, fixed minor problems with displaying error bars.

Location:
prview/perspectives/pr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • prview/perspectives/pr/inversion_panel.py

    r660b1e6 rb659551  
    414414        button_OK.SetToolTipString("Perform P(r) inversion.") 
    415415        self.Bind(wx.EVT_BUTTON, self._on_invert, id = id)    
     416         
     417        id = wx.NewId() 
     418        button_Reset = wx.Button(self, id, "Reset") 
     419        button_Reset.SetToolTipString("Reset inversion parameters to default.") 
     420        self.Bind(wx.EVT_BUTTON, self._on_reset, id = id)    
    416421        #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") 
    417422         
    418423        sizer_button = wx.BoxSizer(wx.HORIZONTAL) 
    419424        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
     425        sizer_button.Add(button_Reset, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) 
    420426        sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) 
    421427        #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)         
     
    442448            pass 
    443449         
    444          
    445     def _on_pars_changed(self, evt): 
     450    def _on_reset(self, evt): 
     451        """ 
     452            Resets inversion parameters 
     453        """ 
     454        self.nfunc = self.manager.DEFAULT_NFUNC 
     455        self.d_max = self.manager.DEFAULT_DMAX 
     456        self.alpha = self.manager.DEFAULT_ALPHA 
     457        self.qmin_ctl.SetValue("") 
     458        self.qmax_ctl.SetValue("") 
     459        self.time_ctl.SetValue("") 
     460        self.chi2_ctl.SetValue("") 
     461        self.osc_ctl.SetValue("") 
     462        self.pos_ctl.SetValue("") 
     463        self.pos_err_ctl.SetValue("") 
     464        self.alpha_estimate_ctl.Enable(False) 
     465        self.alpha_estimate_ctl.SetLabel("") 
     466        self._on_pars_changed() 
     467         
     468    def _on_pars_changed(self, evt=None): 
    446469        """ 
    447470            Called when an input parameter has changed 
     
    495518        try: 
    496519            nfunc = int(self.nfunc_ctl.GetValue()) 
     520            npts = self.manager.get_npts() 
     521            if npts>0 and nfunc>npts: 
     522                message = "Number of function terms should be smaller than the number of points" 
     523                wx.PostEvent(self.manager.parent, StatusEvent(status=message)) 
     524                raise ValueError, message 
    497525            self.nfunc_ctl.SetBackgroundColour(wx.WHITE) 
    498526            self.nfunc_ctl.Refresh() 
     
    610638        self.Centre() 
    611639 
     640class PrDistDialog(wx.Dialog): 
     641    """ 
     642        Property dialog to let the user change the number 
     643        of points on the P(r) plot. 
     644    """ 
     645    def __init__(self, parent, id): 
     646        from sans.pr.invertor import help 
     647        wx.Dialog.__init__(self, parent, id, size=(250, 120)) 
     648        self.SetTitle("P(r) distribution")  
     649         
     650 
     651        vbox = wx.BoxSizer(wx.VERTICAL) 
     652         
     653        label_npts = wx.StaticText(self, -1, "Number of points") 
     654        self.npts_ctl = wx.TextCtrl(self, -1, size=(100,20)) 
     655                  
     656        pars_sizer = wx.GridBagSizer(5,5) 
     657        iy = 0 
     658        pars_sizer.Add(label_npts,      (iy,0), (1,1), wx.LEFT, 15) 
     659        pars_sizer.Add(self.npts_ctl,   (iy,1), (1,1), wx.RIGHT, 0) 
     660         
     661        vbox.Add(pars_sizer, 0, wx.ALL|wx.EXPAND, 15) 
     662 
     663 
     664        static_line = wx.StaticLine(self, -1) 
     665        vbox.Add(static_line, 0, wx.EXPAND, 0) 
     666         
     667        button_OK = wx.Button(self, wx.ID_OK, "OK") 
     668        self.Bind(wx.EVT_BUTTON, self._checkValues, button_OK) 
     669        button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") 
     670         
     671        sizer_button = wx.BoxSizer(wx.HORIZONTAL) 
     672        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
     673        sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 
     674        sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)         
     675        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) 
     676 
     677        self.SetSizer(vbox) 
     678        self.SetAutoLayout(True) 
     679         
     680        self.Layout() 
     681        self.Centre() 
     682 
     683    def _checkValues(self, event): 
     684        """ 
     685            Check the dialog content. 
     686        """ 
     687        flag = True 
     688        try: 
     689            int(self.npts_ctl.GetValue()) 
     690            self.npts_ctl.SetBackgroundColour(wx.WHITE) 
     691            self.npts_ctl.Refresh() 
     692        except: 
     693            flag = False 
     694            self.npts_ctl.SetBackgroundColour("pink") 
     695            self.npts_ctl.Refresh() 
     696        if flag: 
     697            event.Skip(True) 
     698 
     699    def get_content(self): 
     700        """ 
     701            Return the content of the dialog. 
     702            At this point the values have already been 
     703            checked. 
     704        """ 
     705        value = int(self.npts_ctl.GetValue()) 
     706        return value 
     707     
     708    def set_content(self, npts): 
     709        """ 
     710            Initialize the content of the dialog. 
     711        """ 
     712        self.npts_ctl.SetValue("%i" % npts) 
     713 
    612714 
    613715class ParsDialog(wx.Panel): 
     
    751853    def OnInit(self): 
    752854        wx.InitAllImageHandlers() 
    753         dialog = HelpDialog(None, -1) 
     855        dialog = PrDistDialog(None, -1) 
    754856        if dialog.ShowModal() == wx.ID_OK: 
    755857            pass 
  • prview/perspectives/pr/pr.py

    r660b1e6 rb659551  
    1313class Plugin: 
    1414     
     15    DEFAULT_ALPHA = 0.0001 
     16    DEFAULT_NFUNC = 10 
     17    DEFAULT_DMAX  = 140.0 
     18     
    1519    def __init__(self): 
    1620        ## Plug-in name 
     
    2731         
    2832        ## State data 
    29         self.alpha      = 0.0001 
    30         self.nfunc      = 10 
    31         self.max_length = 140.0 
     33        self.alpha      = self.DEFAULT_ALPHA 
     34        self.nfunc      = self.DEFAULT_NFUNC 
     35        self.max_length = self.DEFAULT_DMAX 
    3236        self.q_min      = None 
    3337        self.q_max      = None 
     
    4852        ## Currently views plottable 
    4953        self.current_plottable = None 
     54        ## Number of P(r) points to display on the output plot 
     55        self._pr_npts = 51 
    5056 
    5157    def populate_menu(self, id, owner): 
     
    118124 
    119125        # Show input P(r) 
    120         new_plot = Data1D(pr.x, pr.y, pr.err) 
     126        new_plot = Data1D(pr.x, pr.y, dy=pr.err) 
    121127        new_plot.name = "P_{obs}(r)" 
    122128        new_plot.xaxis("\\rm{r}", 'A') 
     
    157163        #Put this call in plottables/guitools     
    158164        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Sphere P(r)")) 
     165         
     166    def get_npts(self): 
     167        """ 
     168            Returns the number of points in the I(q) data 
     169        """ 
     170        try: 
     171            return len(self.pr.x) 
     172        except: 
     173            return 0 
    159174         
    160175    def show_iq(self, out, pr, q=None): 
     
    203218         
    204219         
     220    def _on_pr_npts(self, evt): 
     221        """ 
     222            Redisplay P(r) with a different number of points 
     223        """    
     224        from inversion_panel import PrDistDialog 
     225        dialog = PrDistDialog(None, -1) 
     226        dialog.set_content(self._pr_npts) 
     227        if dialog.ShowModal() == wx.ID_OK: 
     228            self._pr_npts= dialog.get_content() 
     229            dialog.Destroy() 
     230            self.show_pr(self.pr.out, self.pr, self.pr.cov) 
     231        else: 
     232            dialog.Destroy() 
    205233         
    206234         
     
    213241         
    214242        # Show P(r) 
    215         x = pylab.arange(0.0, pr.d_max, pr.d_max/51.0) 
     243        x = pylab.arange(0.0, pr.d_max, pr.d_max/self._pr_npts) 
    216244     
    217245        y = numpy.zeros(len(x)) 
     
    253281        return self.parent.choose_file() 
    254282                 
    255     def load(self, path = "sphere_test_data.txt"): 
     283    def load(self, path = "sphere_60_q0_2.txt"): 
    256284        import numpy, math, sys 
    257285        # Read the data from the data file 
     
    259287        data_y   = numpy.zeros(0) 
    260288        data_err = numpy.zeros(0) 
     289        scale    = None 
     290        min_err  = 0.0 
    261291        if not path == None: 
    262292            input_f = open(path,'r') 
     
    268298                    x = float(toks[0]) 
    269299                    y = float(toks[1]) 
    270                     try: 
    271                         scale = 0.05/math.sqrt(data_x[0]) 
    272                     except: 
    273                         scale = 1.0 
    274                     #data_err = numpy.append(data_err, 10.0*math.sqrt(y)+1000.0) 
     300                    if len(toks)>2: 
     301                        err = float(toks[2]) 
     302                    else: 
     303                        if scale==None: 
     304                            scale = 0.05*math.sqrt(y) 
     305                            #scale = 0.05/math.sqrt(y) 
     306                            min_err = 0.01*y 
     307                        err = scale*math.sqrt(y)+min_err 
     308                        #err = 0 
     309                         
    275310                    data_x = numpy.append(data_x, x) 
    276311                    data_y = numpy.append(data_y, y) 
    277                     data_err = numpy.append(data_err, scale*math.sqrt(math.fabs(y))) 
     312                    data_err = numpy.append(data_err, err) 
    278313                except: 
    279                     print "Error reading line: ", line 
    280                     print sys.exc_value 
     314                    pass 
    281315                    
    282         print "Lines read:", len(data_x) 
    283         return data_x, data_y, data_err 
     316        return data_x, data_y, data_err      
    284317         
    285318    def pr_theory(self, r, R): 
     
    303336                 
    304337                return [["Compute P(r)", "Compute P(r) from distribution", self._on_context_inversion], 
    305                        ["Add P(r) data", "Load a data file and display it on this plot", self._on_add_data]] 
     338                       ["Add P(r) data", "Load a data file and display it on this plot", self._on_add_data], 
     339                       ["Change number of P(r) points", "Change the number of points on the P(r) output", self._on_pr_npts]] 
    306340                 
    307341        return [["Compute P(r)", "Compute P(r) from distribution", self._on_context_inversion]] 
     
    318352        x, y, err = self.parent.load_ascii_1D(path) 
    319353         
    320         new_plot = Data1D(x, y, dy=err) 
     354        #new_plot = Data1D(x, y, dy=err) 
     355        new_plot = Theory1D(x, y) 
    321356        new_plot.name = "P_{loaded}(r)" 
    322357        new_plot.xaxis("\\rm{r}", 'A') 
     
    369404        # Save useful info 
    370405        self.elapsed = elapsed 
     406        # Save Pr invertor 
     407        self.pr = pr 
     408         
    371409        message = "Computation completed in %g seconds [chi2=%g]" % (elapsed, pr.chi2) 
    372410        wx.PostEvent(self.parent, StatusEvent(status=message)) 
     
    387425                print "%d: %g +- %g" % (i, out[i], math.sqrt(math.fabs(cov[i][i]))) 
    388426            except:  
     427                print sys.exc_value 
    389428                print "%d: %g +- ?" % (i, out[i])         
    390429         
    391430        # Make a plot of I(q) data 
    392         new_plot = Data1D(self.pr.x, self.pr.y, self.pr.err) 
     431        new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) 
    393432        new_plot.name = "I_{obs}(q)" 
    394433        new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
     
    411450               
    412451        # Make a plot of I(q) data 
    413         new_plot = Data1D(self.pr.x, self.pr.y, self.pr.err) 
     452        new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) 
    414453        new_plot.name = "I_{obs}(q)" 
    415454        new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
Note: See TracChangeset for help on using the changeset viewer.