Changeset 634f1cf in sasview for prview/perspectives


Ignore:
Timestamp:
May 9, 2008 6:09:46 PM (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:
4a0536a
Parents:
f71287f4
Message:

Allow user to set q_min/q_max

Location:
prview/perspectives/pr
Files:
4 edited

Legend:

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

    r32dffae4 r634f1cf  
    124124        self.file_radio = None 
    125125        self.plot_radio = None 
     126        self.label_sugg = None 
     127        self.qmin_ctl = None 
     128        self.qmax_ctl = None 
    126129         
    127130        ## Estimates 
     
    145148        elif name=='chi2': 
    146149            self.chi2_ctl.SetValue("%-5.3g" % value) 
     150        elif name=='q_min': 
     151            self.qmin_ctl.SetValue("%-5.3g" % value) 
     152        elif name=='q_max': 
     153            self.qmax_ctl.SetValue("%-5.3g" % value) 
    147154        elif name=='elapsed': 
    148155            self.time_ctl.SetValue("%-5.2g" % value) 
     
    150157            self.osc_ctl.SetValue("%-5.2g" % value) 
    151158        elif name=='alpha_estimate': 
    152             self.alpha_estimate_ctl.SetValue("%-3.1g" % value) 
     159            self.alpha_estimate_ctl.SetToolTipString("Click to accept value.") 
     160            self.alpha_estimate_ctl.Enable(True) 
     161            self.alpha_estimate_ctl.SetLabel("%-3.1g" % value) 
     162            #self.alpha_estimate_ctl.Show() 
     163            #self.label_sugg.Show() 
    153164        elif name=='plotname': 
    154165            self.plot_data.SetValue(str(value)) 
     
    170181        elif name=='chi2': 
    171182            self.chi2_ctl.GetValue() 
     183        elif name=='q_min': 
     184            self.qmin_ctl.GetValue() 
     185        elif name=='q_max': 
     186            self.qmax_ctl.GetValue() 
    172187        elif name=='elapsed': 
    173188            self.time_ctl.GetValue() 
     
    240255        label_alpha = wx.StaticText(self, -1, "Regularization constant") 
    241256        label_dmax  = wx.StaticText(self, -1, "Max distance [A]") 
    242         label_sugg  = wx.StaticText(self, -1, "Suggested value") 
     257        self.label_sugg  = wx.StaticText(self, -1, "Suggested value") 
     258        #self.label_sugg.Hide() 
    243259         
    244260        self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20)) 
     
    248264        self.dmax_ctl  = wx.TextCtrl(self, -1, size=(60,20)) 
    249265        self.dmax_ctl.SetToolTipString("Maximum distance between any two points in the system.") 
    250         self.alpha_estimate_ctl  = wx.TextCtrl(self, -1, size=(60,20)) 
     266        id = wx.NewId() 
     267        self.alpha_estimate_ctl  = wx.Button(self, id, "") 
     268        #self.alpha_estimate_ctl.Hide() 
     269        self.Bind(wx.EVT_BUTTON, self._on_accept_alpha, id = id)    
    251270        self.alpha_estimate_ctl.Enable(False) 
    252         self.alpha_estimate_ctl.SetToolTipString("Value of alpha below which P(r) may have multiple peaks.") 
     271        #self.alpha_estimate_ctl.SetBackgroundColour('#ffdf85') 
     272        #self.alpha_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour()) 
     273        self.alpha_estimate_ctl.SetToolTipString("Waiting for estimate...") 
    253274         
    254275        # EVT_TEXT would trigger an event for each character entered 
    255276        self.nfunc_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed) 
    256         #self.alpha_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed) 
     277        self.alpha_ctl.Bind(wx.EVT_KILL_FOCUS, self._read_pars) 
    257278        self.dmax_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed) 
    258279        self.Bind(wx.EVT_TEXT_ENTER, self._on_pars_changed) 
     
    261282 
    262283        iy = 0 
    263         sizer_params.Add(label_sugg,       (iy,2), (1,1), wx.LEFT, 15) 
     284        sizer_params.Add(self.label_sugg,       (iy,2), (1,1), wx.LEFT, 15) 
    264285        iy += 1 
    265286        sizer_params.Add(label_nfunc,      (iy,0), (1,1), wx.LEFT, 15) 
     
    274295 
    275296        boxsizer2.Add(sizer_params, 0) 
     297         
    276298        vbox.Add(boxsizer2) 
     299 
     300        # ----- Q range ----- 
     301        qbox = wx.StaticBox(self, -1, "Q range") 
     302        qboxsizer = wx.StaticBoxSizer(qbox, wx.VERTICAL) 
     303        qboxsizer.SetMinSize((320,20)) 
     304         
     305        sizer_q = wx.GridBagSizer(5,5) 
     306 
     307        label_qmin = wx.StaticText(self, -1, "Q min") 
     308        label_qmax = wx.StaticText(self, -1, "Q max") 
     309        self.qmin_ctl = wx.TextCtrl(self, -1, size=(60,20)) 
     310        self.qmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) 
     311         
     312        iy = 0 
     313        sizer_q.Add(label_qmin, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) 
     314        sizer_q.Add(self.qmin_ctl, (iy,1), (1,1), wx.LEFT|wx.EXPAND, 10) 
     315        sizer_q.Add(label_qmax, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 15) 
     316        sizer_q.Add(self.qmax_ctl, (iy,3), (1,1), wx.LEFT|wx.EXPAND, 10) 
     317        qboxsizer.Add(sizer_q, wx.TOP, 15) 
     318        vbox.Add(qboxsizer) 
     319         
     320         
    277321 
    278322        # ----- Results ----- 
     
    334378        self.SetSizer(vbox) 
    335379         
     380 
     381         
     382    def _on_accept_alpha(self, evt): 
     383        """ 
     384            User has accepted the estimated alpha,  
     385            set it as part of the input parameters 
     386        """ 
     387        try: 
     388            alpha = self.alpha_estimate_ctl.GetLabel() 
     389            tmp = float(alpha) 
     390            self.alpha_ctl.SetValue(alpha) 
     391        except: 
     392            # No estimate or bad estimate, either do nothing 
     393            import sys 
     394            print "InversionControl._on_accept_alpha: %s" % sys.exc_value 
     395            pass 
     396         
     397         
    336398    def _on_pars_changed(self, evt): 
    337399        """ 
     
    340402            scenes.  
    341403        """ 
    342         flag, alpha, dmax, nfunc = self._read_pars() 
     404        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars() 
    343405         
    344406        # If the pars are valid, estimate alpha 
     
    347409                dataset = self.plot_data.GetValue() 
    348410                self.manager.estimate_plot_inversion(alpha=alpha, nfunc=nfunc,  
    349                                                      d_max=dmax) 
     411                                                     d_max=dmax, 
     412                                                     q_min=qmin, q_max=qmax) 
    350413            else: 
    351414                path = self.data_file.GetValue() 
    352415                self.manager.estimate_file_inversion(alpha=alpha, nfunc=nfunc,  
    353                                                      d_max=dmax, path=path) 
    354          
    355          
    356     def _read_pars(self):     
     416                                                     d_max=dmax, path=path, 
     417                                                     q_min=qmin, q_max=qmax) 
     418         
     419         
     420    def _read_pars(self, evt=None):     
    357421        alpha = 0 
    358422        nfunc = 5 
     
    391455            self.nfunc_ctl.Refresh() 
    392456         
    393         return flag, alpha, dmax, nfunc 
     457        # Read qmin 
     458        try: 
     459            qmin_str = self.qmin_ctl.GetValue() 
     460            if len(qmin_str.lstrip().rstrip())==0: 
     461                qmin = None 
     462            else: 
     463                qmin = float(qmin_str) 
     464                self.qmin_ctl.SetBackgroundColour(wx.WHITE) 
     465                self.qmin_ctl.Refresh() 
     466        except: 
     467            flag = False 
     468            self.qmin_ctl.SetBackgroundColour("pink") 
     469            self.qmin_ctl.Refresh() 
     470         
     471        # Read qmax 
     472        try: 
     473            qmax_str = self.qmax_ctl.GetValue() 
     474            if len(qmax_str.lstrip().rstrip())==0: 
     475                qmax = None 
     476            else: 
     477                qmax = float(qmax_str) 
     478                self.qmax_ctl.SetBackgroundColour(wx.WHITE) 
     479                self.qmax_ctl.Refresh() 
     480        except: 
     481            flag = False 
     482            self.qmax_ctl.SetBackgroundColour("pink") 
     483            self.qmax_ctl.Refresh() 
     484         
     485        return flag, alpha, dmax, nfunc, qmin, qmax 
    394486     
    395487    def _on_invert(self, evt): 
     
    401493        # Push it to the manager 
    402494         
    403         flag, alpha, dmax, nfunc = self._read_pars() 
     495        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars() 
    404496         
    405497        if flag: 
     
    407499                dataset = self.plot_data.GetValue() 
    408500                self.manager.setup_plot_inversion(alpha=alpha, nfunc=nfunc,  
    409                                                   d_max=dmax) 
     501                                                  d_max=dmax, 
     502                                                  q_min=qmin, q_max=qmax) 
    410503            else: 
    411504                path = self.data_file.GetValue() 
    412505                self.manager.setup_file_inversion(alpha=alpha, nfunc=nfunc,  
    413                                                   d_max=dmax, path=path) 
     506                                                  d_max=dmax, path=path, 
     507                                                  q_min=qmin, q_max=qmax 
     508                                                  ) 
    414509                 
    415510        else: 
  • prview/perspectives/pr/pr.py

    r32dffae4 r634f1cf  
    2929        self.nfunc      = 10 
    3030        self.max_length = 140.0 
     31        self.q_min      = None 
     32        self.q_max      = None 
    3133        ## Remember last plottable processed 
    3234        self.last_data  = "sphere_60_q0_2.txt" 
     
    158160                maxq=q_i 
    159161                 
    160         x = pylab.arange(0.001, maxq, maxq/301.0) 
     162        minq = 0.001 
     163         
     164        # Check for user min/max 
     165        if not pr.q_min==None: 
     166            minq = pr.q_min 
     167        if not pr.q_max==None: 
     168            maxq = pr.q_max 
     169                 
     170        x = pylab.arange(minq, maxq, maxq/301.0) 
    161171        y = numpy.zeros(len(x)) 
    162172        err = numpy.zeros(len(x)) 
     
    349359        #result_panel = InversionResults(self.parent, -1, style=wx.RAISED_BORDER) 
    350360         
    351     def setup_plot_inversion(self, alpha, nfunc, d_max): 
     361    def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None): 
    352362        self.alpha = alpha 
    353363        self.nfunc = nfunc 
    354364        self.max_length = d_max 
     365        self.q_min = q_min 
     366        self.q_max = q_max 
    355367         
    356368        self._create_plot_pr() 
    357369        self.perform_inversion() 
    358370 
    359     def estimate_plot_inversion(self, alpha, nfunc, d_max): 
     371    def estimate_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None): 
    360372        self.alpha = alpha 
    361373        self.nfunc = nfunc 
    362374        self.max_length = d_max 
     375        self.q_min = q_min 
     376        self.q_max = q_max 
    363377         
    364378        self._create_plot_pr() 
     
    375389        pr.d_max = self.max_length 
    376390        pr.alpha = self.alpha 
     391        pr.q_min = self.q_min 
     392        pr.q_max = self.q_max 
    377393        pr.x = self.current_plottable.x 
    378394        pr.y = self.current_plottable.y 
     
    391407 
    392408           
    393     def setup_file_inversion(self, alpha, nfunc, d_max, path): 
     409    def setup_file_inversion(self, alpha, nfunc, d_max, path, q_min=None, q_max=None): 
    394410        self.alpha = alpha 
    395411        self.nfunc = nfunc 
    396412        self.max_length = d_max 
     413        self.q_min = q_min 
     414        self.q_max = q_max 
    397415         
    398416        self._create_file_pr(path) 
     
    400418        self.perform_inversion() 
    401419           
    402     def estimate_file_inversion(self, alpha, nfunc, d_max, path): 
     420    def estimate_file_inversion(self, alpha, nfunc, d_max, path, q_min=None, q_max=None): 
    403421        self.alpha = alpha 
    404422        self.nfunc = nfunc 
    405423        self.max_length = d_max 
     424        self.q_min = q_min 
     425        self.q_max = q_max 
    406426         
    407427        if self._create_file_pr(path): 
     
    422442            pr.d_max = self.max_length 
    423443            pr.alpha = self.alpha 
     444            pr.q_min = self.q_min 
     445            pr.q_max = self.q_max 
    424446            pr.x = x 
    425447            pr.y = y 
  • prview/perspectives/pr/pr_thread.py

    r32dffae4 r634f1cf  
    7878         
    7979    def compute(self): 
    80         import time 
     80        """ 
     81            Calculates the estimate 
     82        """ 
    8183        try:             
    82             self.starttime = time.time() 
    83             # If the current alpha is zero, try 
    84             # another value 
    85             if self.pr.alpha<=0: 
    86                 self.pr.alpha = 0.0001 
    87                   
    88             # Perform inversion to find the largest alpha 
    89             out, cov = self.pr.lstsq(self.nfunc) 
    90             elapsed = time.time()-self.starttime 
    91             initial_alpha = self.pr.alpha 
    92             initial_peaks = self.pr.get_peaks(out) 
    93  
    94             # Try the inversion with the estimated alpha 
    95             self.pr.alpha = self.pr.suggested_alpha 
    96             out, cov = self.pr.lstsq(self.nfunc) 
    97  
    98             npeaks = self.pr.get_peaks(out) 
    99             # if more than one peak to start with 
    100             # just return the estimate 
    101             if npeaks>1: 
    102                 message = "Your P(r) is not smooth, please check your inversion parameters" 
    103                 self.complete(alpha=self.pr.suggested_alpha, message=message, elapsed=elapsed) 
    104             else: 
    105                  
    106                 # Look at smaller values 
    107                 # We assume that for the suggested alpha, we have 1 peak 
    108                 # if not, send a message to change parameters 
    109                 alpha = self.pr.suggested_alpha 
    110                 best_alpha = self.pr.suggested_alpha 
    111                 found = False 
    112                 for i in range(10): 
    113                     self.pr.alpha = (0.33)**(i+1)*alpha 
    114                     out, cov = self.pr.lstsq(self.nfunc) 
    115                     #osc = self.pr.oscillations(out)  
    116                     #print self.pr.alpha, osc 
    117                      
    118                     peaks = self.pr.get_peaks(out) 
    119                     print self.pr.alpha, peaks 
    120                     if peaks>1: 
    121                         found = True 
    122                         break 
    123                     best_alpha = self.pr.alpha 
    124                      
    125                 # If we didn't find a turning point for alpha and 
    126                 # the initial alpha already had only one peak, 
    127                 # just return that 
    128                 if not found and initial_peaks==1 and initial_alpha<best_alpha: 
    129                     best_alpha = initial_alpha 
    130                      
    131                 # Check whether the size makes sense 
    132                 message=None 
    133                  
    134                 if not found: 
    135                     message = "None" 
    136                 elif best_alpha>=0.5*self.pr.suggested_alpha: 
    137                     # best alpha is too big, return a  
    138                     # reasonable value 
    139                     message  = "The estimated alpha for your system is too large. " 
    140                     message += "Try increasing your maximum distance." 
    141                  
    142                 self.complete(alpha=best_alpha, message=None, elapsed=elapsed) 
    143  
     84            alpha, message, elapsed = self.pr.estimate_alpha(self.nfunc) 
     85            self.complete(alpha=alpha, message=message, elapsed=elapsed) 
    14486        except: 
    14587            if not self.error_func==None: 
  • prview/perspectives/pr/requirements.txt

    r32dffae4 r634f1cf  
    111. Thread 
    222. Find good defaults. Automate the inversion. 
    3    -How do we estimate d_max? 
    4    -How do we estimate alpha? 
     3   -How do we estimate d_max? Guinier fit? 
     4   -How do we estimate alpha? [partially done] 
    55   Compute the residuals once and choose a value that brings the reg term up to  
    66   a fraction of the residuals. 
     
    995. Create handshake so that a plug-in can set context menu items to its own panels... 
    10106. Return figure of merit values and suggest the values for d_max and alpha. 
    11 7. Show time estimate 
     117. Show time estimate [done] 
    12128. Plot the reg term alone to see what it looks like 
    13139. Use data loader perspective/plug-in to load data/choose file 
     
    151511. Define limits of applicability 
    161612. Return figures of merit 
    17 13. Errors on output constants 
     1713. Errors on output constants [done] 
    181814. Clean up the InversionDialog 
    191915. Add online help for inputs and outputs (especially figures of merit) 
Note: See TracChangeset for help on using the changeset viewer.