Changeset 6ef7ac5a in sasview


Ignore:
Timestamp:
Dec 31, 2009 4:26:14 PM (14 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:
6bbcbd9
Parents:
7e8601f
Message:

simview: update simulation when Q range and point density are changed from the control panel; show P(r) plot.

Location:
simview/perspectives/simulation
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • simview/perspectives/simulation/ShapeParameters.py

    rcb78690 r6ef7ac5a  
    1414import SimCanvas 
    1515 
    16  
    1716(CreateShapeEvent, EVT_ADD_SHAPE) = wx.lib.newevent.NewEvent() 
    1817(EditShapeEvent, EVT_EDIT_SHAPE)  = wx.lib.newevent.NewEvent() 
    19 (DelShapeEvent, EVT_DEL_SHAPE)  = wx.lib.newevent.NewEvent() 
     18(DelShapeEvent, EVT_DEL_SHAPE)    = wx.lib.newevent.NewEvent() 
     19(QRangeEvent, EVT_Q_RANGE)        = wx.lib.newevent.NewEvent()  
     20(PtDensityEvent, EVT_PT_DENSITY)  = wx.lib.newevent.NewEvent()  
    2021 
    2122class ShapeParameterPanel(wx.Panel): 
     
    2425    CENTER_PANE = True 
    2526     
    26     def __init__(self, parent, *args, **kwargs): 
     27    def __init__(self, parent, q_min=0.001, q_max=0.5, q_npts=10, pt_density=0.1, *args, **kwargs): 
    2728        wx.Panel.__init__(self, parent, *args, **kwargs) 
    2829         
     
    6263             
    6364        self.model_combo = wx.ComboBox(self, -1, value=value_list[0], choices=value_list, style=wx.CB_READONLY) 
    64         self.model_combo.SetToolTip(wx.ToolTip("select a geometric shape from the drop-down list")) 
     65        self.model_combo.SetToolTip(wx.ToolTip("Select a geometric shape from the drop-down list")) 
    6566         
    6667        ny+=1 
     
    7879        self.bck.Add(point_density_text, (ny,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    7980        self.point_density_ctrl = wx.TextCtrl(self, -1, size=(40,20), style=wx.TE_PROCESS_ENTER) 
    80         self.point_density_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_parameter_changed) 
    81         self.point_density_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_parameter_changed) 
     81        self.point_density_ctrl.SetValue(str(pt_density)) 
     82        self.point_density_ctrl.SetToolTip(wx.ToolTip("Enter the number of real-space points per Angstrom cube")) 
     83        self.point_density_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_density_changed) 
     84        self.point_density_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_density_changed) 
    8285        self.bck.Add(self.point_density_ctrl, (ny,1), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    8386         
     
    8790        self.bck.Add(q_min_text, (ny,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    8891        self.q_min_ctrl = wx.TextCtrl(self, -1, size=(40,20), style=wx.TE_PROCESS_ENTER) 
    89         self.q_min_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_parameter_changed) 
    90         self.q_min_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_parameter_changed) 
     92        self.q_min_ctrl.SetValue(str(q_min)) 
     93        self.q_min_ctrl.SetToolTip(wx.ToolTip("Enter the minimum Q value to be simulated")) 
     94        self.q_min_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_q_range_changed) 
     95        self.q_min_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_q_range_changed) 
    9196        self.bck.Add(self.q_min_ctrl, (ny,1), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    9297         
     
    9499        self.bck.Add(q_max_text, (ny,2), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    95100        self.q_max_ctrl = wx.TextCtrl(self, -1, size=(40,20), style=wx.TE_PROCESS_ENTER) 
    96         self.q_max_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_parameter_changed) 
    97         self.q_max_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_parameter_changed) 
     101        self.q_max_ctrl.SetValue(str(q_max)) 
     102        self.q_min_ctrl.SetToolTip(wx.ToolTip("Enter the maximum Q value to be simulated")) 
     103        self.q_max_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_q_range_changed) 
     104        self.q_max_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_q_range_changed) 
    98105        self.bck.Add(self.q_max_ctrl, (ny,3), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
     106         
     107        q_npts_text = wx.StaticText(self, -1, "No. of Q pts", style=wx.ALIGN_LEFT) 
     108        self.bck.Add(q_npts_text, (ny,4), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
     109        self.q_npts_ctrl = wx.TextCtrl(self, -1, size=(40,20), style=wx.TE_PROCESS_ENTER) 
     110        self.q_npts_ctrl.SetValue(str(q_npts)) 
     111        self.q_min_ctrl.SetToolTip(wx.ToolTip("Enter the number of Q points to be simulated")) 
     112        self.q_npts_ctrl.Bind(wx.EVT_TEXT_ENTER, self._on_q_range_changed) 
     113        self.q_npts_ctrl.Bind(wx.EVT_KILL_FOCUS, self._on_q_range_changed) 
     114        self.bck.Add(self.q_npts_ctrl, (ny,5), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) 
    99115         
    100116        # Shape List 
     
    132148        self.editShape(shape)     
    133149 
    134     def _on_parameter_changed(self, event): 
     150    def _on_density_changed(self, event): 
    135151        """ 
    136152            Process event that might mean a change of the simulation point density 
    137153        """ 
    138         event_obj = event.GetEventObject() 
    139         if event_obj.IsModified(): 
    140             event_obj.SetModified(False) 
    141             self._simulation_update() 
    142              
    143     def _simulation_update(self): 
    144         """ 
    145             Process an update of the simulation parameters 
    146         """ 
    147         #TODO: create an event to pass the parameters to the simulation plug-in for processing 
    148         print "TODO: Simulation update" 
    149         pass 
    150          
     154        npts  = self._readCtrlFloat(self.point_density_ctrl) 
     155        if npts is not None: 
     156            event = PtDensityEvent(npts=npts) 
     157            wx.PostEvent(self.parent, event) 
     158             
     159    def _on_q_range_changed(self, event): 
     160        """ 
     161            Process event that might mean a change in Q range 
     162            @param event: EVT_Q_RANGE event 
     163        """ 
     164        q_min = self._readCtrlFloat(self.q_min_ctrl) 
     165        q_max = self._readCtrlFloat(self.q_max_ctrl) 
     166        npts  = self._readCtrlInt(self.q_npts_ctrl) 
     167        if q_min is not None or q_max is not None or npts is not None: 
     168            event = QRangeEvent(q_min=q_min, q_max=q_max, npts=npts) 
     169            wx.PostEvent(self.parent, event) 
     170             
    151171    def _onEditShape(self, evt): 
    152172        """ 
     
    295315 
    296316    def _readCtrlFloat(self, ctrl): 
     317        """ 
     318            Parses a TextCtrl for a float value. 
     319            Returns None is the value hasn't changed or the value is not a float. 
     320            The control background turns pink if the value is not a float. 
     321             
     322            @param ctrl: TextCtrl object 
     323        """ 
    297324        if ctrl.IsModified(): 
     325            ctrl.SetModified(False) 
    298326            str_value = ctrl.GetValue().lstrip().rstrip() 
    299327            try: 
     
    303331                ctrl.Refresh() 
    304332                return flt_value 
     333            except: 
     334                ctrl.SetBackgroundColour("pink") 
     335                ctrl.Refresh() 
     336        return None 
     337 
     338    def _readCtrlInt(self, ctrl): 
     339        """ 
     340            Parses a TextCtrl for a float value. 
     341            Returns None is the value hasn't changed or the value is not a float. 
     342            The control background turns pink if the value is not a float. 
     343             
     344            @param ctrl: TextCtrl object 
     345        """ 
     346        if ctrl.IsModified(): 
     347            ctrl.SetModified(False) 
     348            str_value = ctrl.GetValue().lstrip().rstrip() 
     349            try: 
     350                int_value = int(str_value) 
     351                ctrl.SetBackgroundColour( 
     352                        wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) 
     353                ctrl.Refresh() 
     354                return int_value 
    305355            except: 
    306356                ctrl.SetBackgroundColour("pink") 
  • simview/perspectives/simulation/requirements.txt

    reaeb13e r6ef7ac5a  
    11UI requirements 
    2 - Allow user to modify q range from main panel 
     2- Allow user to modify q range from main panel [DONE] 
    33- Move list of shapes from top menu to central simulation panel 
    4 - Popup P(r) plot and update for each simulation 
     4- Popup P(r) plot and update for each simulation [DONE] 
    55- Allow user to choose whether he wants the simulation to be run automatically with each parameter change 
    66  or whether he wants to hit 'simulate' himself when he's done. 
  • simview/perspectives/simulation/simulation.py

    rcb78690 r6ef7ac5a  
    7171## Default number of q point for simulation output 
    7272DEFAULT_Q_NPTS = 10 
     73## Default number of real-space points per Angstrom cube 
     74DEFAULT_PT_DENSITY = 0.1 
    7375    
    7476class Plugin: 
     
    105107 
    106108        # Central simulation panel 
    107         self.paramPanel = ShapeParameters.ShapeParameterPanel(self.parent, style=wx.RAISED_BORDER) 
     109        self.paramPanel = ShapeParameters.ShapeParameterPanel(self.parent,  
     110                                                              q_min = self.q_min, 
     111                                                              q_max = self.q_max, 
     112                                                              q_npts = self.q_npts, 
     113                                                              pt_density = DEFAULT_PT_DENSITY, 
     114                                                              style=wx.RAISED_BORDER) 
    108115         
    109116        # Simulation 
    110117        self.volCanvas = VolumeCanvas.VolumeCanvas() 
    111         self.volCanvas.setParam('lores_density', 0.1) 
     118        self.volCanvas.setParam('lores_density', DEFAULT_PT_DENSITY) 
    112119        self.adapter = ShapeAdapter.ShapeVisitor() 
    113120        self._data_1D = None 
     
    128135        self.parent.Bind(ShapeParameters.EVT_ADD_SHAPE, self._onAddShape) 
    129136        self.parent.Bind(ShapeParameters.EVT_DEL_SHAPE, self._onDelShape) 
     137        self.parent.Bind(ShapeParameters.EVT_Q_RANGE, self._on_q_range_changed) 
     138        self.parent.Bind(ShapeParameters.EVT_PT_DENSITY, self._on_pt_density_changed) 
    130139 
    131140        return [self.plotPanel, self.paramPanel] 
     
    166175        # Refresh the 3D viewer 
    167176        self._refresh_3D_viewer()    
     177         
     178    def _on_q_range_changed(self, evt): 
     179        """ 
     180            Modify the Q range of the simulation output 
     181        """ 
     182        if evt.q_min is not None: 
     183            self.q_min = evt.q_min 
     184        if evt.q_max is not None: 
     185            self.q_max = evt.q_max 
     186        if evt.npts is not None: 
     187            self.q_npts = evt.npts 
     188         
     189        # Q-values for plotting simulated I(Q) 
     190        step = (self.q_max-self.q_min)/(self.q_npts-1) 
     191        self.x = numpy.arange(self.q_min, self.q_max+step*0.01, step)     
     192          
     193        # Compute the simulated I(Q) 
     194        self._simulate_Iq() 
     195         
     196    def _on_pt_density_changed(self, evt): 
     197        """ 
     198            Modify the Q range of the simulation output 
     199        """ 
     200        if evt.npts is not None: 
     201            self.volCanvas.setParam('lores_density', evt.npts) 
     202          
     203        # Compute the simulated I(Q) 
     204        self._simulate_Iq() 
    168205         
    169206    def _simulate_Iq(self): 
     
    212249        # Create the plotting event to pop up the I(Q) plot. 
    213250        new_plot = Data1D(x=self.x, y=output, dy=error) 
    214         new_plot.name = "Simulation" 
     251        new_plot.name = "I(Q) Simulation" 
    215252        new_plot.group_id = "simulation_output" 
    216253        new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    217254        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
    218         wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Simulation output")) 
     255        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Simulation I(Q)")) 
     256         
     257        # Create the plotting event to pop up the P(r) plot. 
     258        r, pr = self.volCanvas.getPrData() 
     259        new_plot = Data1D(x=r, y=pr, dy=[0]*len(r)) 
     260        new_plot.name = "P(r) Simulation" 
     261        new_plot.group_id = "simulated_pr" 
     262        new_plot.xaxis("\\rm{r}", 'A') 
     263        new_plot.yaxis("\\rm{P(r)} ","cm^{-3}")  
     264         
     265        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Simulated P(r)"))         
     266         
    219267         
    220268        # Notify the user of the simlation time and update the basic 
     
    235283    def populate_menu(self, id, owner): 
    236284        """ 
    237             -- TEMPORARY -- 
    238285            Create a menu for the plug-in 
    239              
    240             #TODO: remove this method once: 
    241                 1- P(r) is plotted with the I(q) simulation result 
    242         """ 
    243         # Shapes 
    244         shapes = wx.Menu() 
    245   
    246         #TODO: Save P(r) should be replaced by plotting P(r) for each simulation 
    247         id = wx.NewId() 
    248         shapes.Append(id, '&Save P(r) output') 
    249         wx.EVT_MENU(self.parent, id, self._onSavePr) 
    250  
    251         return [(id+1, shapes, "Save P(r)")]   
     286        """ 
     287        return []   
    252288     
    253289    def _change_point_density(self, point_density): 
     
    257293        """ 
    258294        self.volCanvas.setParam('lores_density', point_density) 
    259      
    260     def _onSavePr(self, evt): 
    261         """ 
    262             Save the current P(r) output to a file 
    263             TODO: refactor this away once P(r) is plotted 
    264         """       
    265         path = None 
    266         dlg = wx.FileDialog(None, "Choose a file", self._default_save_location, "", "*.txt", wx.SAVE) 
    267         if dlg.ShowModal() == wx.ID_OK: 
    268             path = dlg.GetPath() 
    269             self._default_save_location = os.path.dirname(path) 
    270         dlg.Destroy() 
    271          
    272         if not path == None: 
    273             self.volCanvas.write_pr(path)     
    274295     
    275296    def help(self, evt): 
Note: See TracChangeset for help on using the changeset viewer.