source: sasview/sansview/perspectives/fitting/fitpanel.py @ 384647dc

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 384647dc was 66ff250, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on fit stop

  • Property mode set to 100644
File size: 14.2 KB
RevLine 
[e2f1023]1
2import numpy
3import string 
[d89f09b]4import wx
[6bbeacd4]5import wx.lib.flatnotebook as fnb
[a93f525]6
[340c2b3]7from sans.guiframe.panel_base import PanelBase
[3cd5806]8from sans.guiframe.events import PanelOnFocusEvent
[6bbeacd4]9from sans.guiframe.events import StatusEvent
[66ff250]10
[cfc0913]11import basepage
[6bbeacd4]12import models
[330573d]13_BOX_WIDTH = 80
14
15
[6bbeacd4]16class FitPanel(fnb.FlatNotebook, PanelBase):   
[925a30e]17
[d89f09b]18    """
[5062bbf]19    FitPanel class contains fields allowing to fit  models and  data
20   
21    :note: For Fit to be performed the user should check at least one parameter
[d89f09b]22        on fit Panel window.
23       
24    """
25    ## Internal name for the AUI manager
26    window_name = "Fit panel"
27    ## Title to appear on top of the window
28    window_caption = "Fit Panel "
[2139c3f]29    CENTER_PANE = True
[7437880]30   
[6bbeacd4]31    def __init__(self, parent, manager=None, *args, **kwargs):
[5062bbf]32        """
33        """
[6bbeacd4]34        fnb.FlatNotebook.__init__(self, parent, -1,
[3244cbe1]35                    style= wx.aui.AUI_NB_WINDOWLIST_BUTTON|
36                    wx.aui.AUI_NB_DEFAULT_STYLE|
37                    wx.CLIP_CHILDREN)
[3cd5806]38        PanelBase.__init__(self, parent)
[6bbeacd4]39        self.SetWindowStyleFlag(style=fnb.FNB_FANCY_TABS)
40        self._manager = manager
[340c2b3]41        self.parent = parent
42        self.event_owner = None
[d89f09b]43        #dictionary of miodel {model class name, model class}
[6bbeacd4]44        self.menu_mng = models.ModelManager()
[b2d9826]45        self.model_list_box = self.menu_mng.get_model_list()
[6bbeacd4]46        #pageClosedEvent = fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING
47        self.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING , self.on_close_page)
48         ## save the title of the last page tab added
[2f189dc]49        self.fit_page_name = {}
[cfc0913]50        ## list of existing fit page
[2f189dc]51        self.opened_pages = {}
52        #page of simultaneous fit
[ffa69b6]53        self.sim_page = None
[66ff250]54        self.fit_engine_type = "scipy"
[cfc0913]55        ## get the state of a page
56        self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate)
[330573d]57        self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo)
[fe496eeb]58        self.Bind(basepage.EVT_NEXT_STATE, self._onRedo)
[90a7bbd]59        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.on_page_changing)
[66ff250]60     
[9b18735]61        #add default pages
62        self.add_default_pages()
[6bbeacd4]63   
[66ff250]64    def _on_engine_change(self, name='scipy'):
65        """
66        """
67        for panel in self.opened_pages.values():
68            self.set_engine_helper(panel=panel, name=name)
69           
70    def set_engine_helper(self, panel, name='scipy'):
71        """
72        """
73        self.fit_engine_type = name
74        if panel != self.sim_page:
75            panel._on_engine_change(name=self.fit_engine_type)
76               
[b2d9826]77    def update_model_list(self):
78        """
79        """
80        self.model_list_box = self.menu_mng.update()
81        return self.model_list_box
82       
83       
[66ff250]84    def get_page_by_id(self, uid): 
[6bbeacd4]85        """
86        """
[66ff250]87        if uid not in self.opened_pages:
88            msg = "Fitpanel cannot find ID: %s in self.opened_pages" % str(uid)
[6bbeacd4]89            raise ValueError, msg
90        else:
[66ff250]91            return self.opened_pages[uid]
[31b0c47]92       
[90a7bbd]93    def on_page_changing(self, event):
[6bbeacd4]94        """
95        """
[90a7bbd]96        pos = self.GetSelection()
97        if pos != -1:
98            selected_page = self.GetPage(pos)
99            wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page))
[6bbeacd4]100           
[3cd5806]101    def on_set_focus(self, event):
102        """
103        """
104        pos = self.GetSelection()
[90a7bbd]105        if pos != -1:
106            selected_page = self.GetPage(pos)
107            wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page))
[3cd5806]108       
[c8deee5]109    def get_data(self):
110        """
111        get the data in the current page
112        """
113        pos = self.GetSelection()
[90a7bbd]114        if pos != -1:
115            selected_page = self.GetPage(pos)
116            return selected_page.get_data()
[c8deee5]117   
[e88ebfd]118    def set_model_state(state):
119        """
120        receive a state to reset the model in the current page
121        """
122        pos = self.GetSelection()
123        if pos != -1:
124            selected_page = self.GetPage(pos)
125            selected_page.set_model_state(state)
126           
[c8deee5]127    def get_state(self):
128        """
129         return the state of the current selected page
130        """
131        pos = self.GetSelection()
[90a7bbd]132        if pos != -1:
133            selected_page = self.GetPage(pos)
134            return selected_page.get_state()
[c8deee5]135   
[9b18735]136    def add_default_pages(self):
137        """
138        Add default pages such as a hint page and an empty fit page
139        """
140        #add default page
141        from hint_fitpage import HintFitPage
142        self.hint_page = HintFitPage(self) 
[6bbeacd4]143        self.AddPage(self.hint_page,"Hint")
[3cd5806]144        self.hint_page.set_manager(self._manager)
[6bbeacd4]145 
[ac2dc0e]146    def close_all(self):
147        """
[8897d66]148        remove all pages, used when a svs file is opened
149        """
[90a7bbd]150       
[8897d66]151        #get number of pages
152        nop = self.GetPageCount()
153        #use while-loop, for-loop will not do the job well.
154        while (nop>0):
155            #delete the first page until no page exists
156            page = self.GetPage(0)
[90a7bbd]157            if self._manager.parent.panel_on_focus == page:
158                self._manager.parent.panel_on_focus = None
[8897d66]159            self._close_helper(selected_page=page)
160            self.DeletePage(0)
161            nop = nop - 1
[90a7bbd]162           
[ac2dc0e]163        ## save the title of the last page tab added
164        self.fit_page_name = {}
165        ## list of existing fit page
166        self.opened_pages = {} 
167         
[31b0c47]168    def set_state(self, state):
169        """
[5062bbf]170        Restore state of the panel
[31b0c47]171        """
172        page_is_opened = False
173        if state is not None:
[66ff250]174            for uid, panel in self.opened_pages.iteritems():
[31b0c47]175                #Don't return any panel is the exact same page is created
[66ff250]176                if uid == panel.uid:
[31b0c47]177                    # the page is still opened
178                    panel.reset_page(state=state)
[645f9b6]179                    panel.save_current_state() 
[31b0c47]180                    page_is_opened = True
181            if not page_is_opened:
[9f391af]182                panel = self._manager.add_fit_page(data=state.data)
[31b0c47]183                # add data associated to the page created
184                if panel is not None: 
[66ff250]185                    self._manager.store_page(page=panel.uid, data=state.data)
[31b0c47]186                    panel.reset_page(state=state)
[645f9b6]187                    panel.save_current_state()
[31b0c47]188                   
[90a7bbd]189    def clear_panel(self):
[9b18735]190        """
191        Clear and close all panels, used by guimanager
192        """
[90a7bbd]193       
194        #close all panels only when svs file opened
195        self.close_all()
196        self._manager.mypanels = []
197       
[9b18735]198                       
[ac2dc0e]199    def on_close_page(self, event=None):
[51d47b5]200        """
[5062bbf]201        close page and remove all references to the closed page
[51d47b5]202        """
[d361b462]203        nbr_page = self.GetPageCount()
[a93f525]204        if nbr_page == 1:
[ac2dc0e]205           
[d361b462]206            event.Veto()
207            return 
[51d47b5]208        selected_page = self.GetPage(self.GetSelection())
[2f189dc]209        self._close_helper(selected_page=selected_page)
[9853ad0]210       
[2f189dc]211    def close_page_with_data(self, deleted_data):
212        """
[5062bbf]213        close a fit page when its data is completely remove from the graph
[2f189dc]214        """
[784e2fa]215        if deleted_data is None:
216            return
[2f189dc]217        for index in range(self.GetPageCount()):
218            selected_page = self.GetPage(index) 
219            if hasattr(selected_page,"get_data"):
220                data = selected_page.get_data()
[ac2dc0e]221               
[784e2fa]222                if data is None:
223                    #the fitpanel exists and only the initial fit page is open
224                    #with no selected data
225                    return
[2f189dc]226                if data.name == deleted_data.name:
227                    self._close_helper(selected_page)
228                    self.DeletePage(index)
229                    break
[d89f09b]230       
231    def set_manager(self, manager):
232        """
[5062bbf]233        set panel manager
234       
235        :param manager: instance of plugin fitting
236       
[d89f09b]237        """
[c8deee5]238        self._manager = manager
[3cd5806]239        for pos in range(self.GetPageCount()):
240            page = self.GetPage(pos)
241            if page is not None:
242                page.set_manager(self._manager)
[a93f525]243
[ffa69b6]244    def set_model_list(self, dict):
[c77d859]245         """
[5062bbf]246         copy a dictionary of model into its own dictionary
247         
248         :param dict: dictionnary made of model name as key and model class
[c77d859]249             as value
250         """
251         self.model_list_box = dict
[51d47b5]252       
[c77d859]253    def get_current_page(self):
254        """
[5062bbf]255        :return: the current page selected
256       
[c77d859]257        """
[6bbeacd4]258        return self.GetPage(self.GetSelection())
[c77d859]259   
[51d47b5]260    def add_sim_page(self):
[925a30e]261        """
[5062bbf]262        Add the simultaneous fit page
[925a30e]263        """
[51d47b5]264        from simfitpage import SimultaneousFitPage
[c8deee5]265        page_finder= self._manager.get_page_finder()
[b28717b]266        self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1)
[66ff250]267        self.sim_page.uid = wx.NewId()
[6bbeacd4]268        self.AddPage(self.sim_page,"Simultaneous Fit", True)
[c8deee5]269        self.sim_page.set_manager(self._manager)
[51d47b5]270        return self.sim_page
[d89f09b]271       
[6bbeacd4]272 
[2f189dc]273    def add_empty_page(self):
[9853ad0]274        """
[5062bbf]275        add an empty page
[9853ad0]276        """
[2f189dc]277        from fitpage import FitPage
[6bbeacd4]278        panel = FitPage(parent=self)
[66ff250]279        panel.uid = wx.NewId()
[6bbeacd4]280        panel.populate_box(dict=self.model_list_box)
[c8deee5]281        panel.set_manager(self._manager)
[ae4ade7]282        caption = str(panel.window_name) + " " + str(self._manager.index_model)
283        self.AddPage(panel, caption, select=True)
[66ff250]284        self.opened_pages[panel.uid] = panel
285        self.set_engine_helper(panel=panel)
[2f189dc]286        return panel
287   
[8ee56a9]288    def delete_data(self, data):
289        """
290        Delete the given data
291        """
292        if data is None:
293            return None
[6bbeacd4]294    def set_data(self, data):
[2f189dc]295        """
[5062bbf]296        Add a fitting page on the notebook contained by fitpanel
297       
298        :param data: data to fit
299       
300        :return panel : page just added for further used. is used by fitting module
301       
[2f189dc]302        """
303        if data is None:
304            return None
[6bbeacd4]305        for page in self.opened_pages.values():
306            #check if the selected data existing in the fitpanel
307            pos = self.GetPageIndex(page)
308            if page.get_data() is None:
[a5701e6]309                enable2D = page.get_view_mode()
310                if (data.__class__.__name__ == "Data2D" and enable2D)\
311                or (data.__class__.__name__ == "Data1D" and not enable2D):
312                    page.set_data(data)
313                    self.SetPageText(pos, str(data.name))
314                    self.SetSelection(pos)
315                    return page
[ae4ade7]316               
[6bbeacd4]317            elif page.get_data().id == data.id:
318                msg = "Data already existing in the fitting panel"
319                wx.PostEvent(self._manager.parent, 
320                             StatusEvent(status=msg, info='warning')) 
321                self.SetSelection(pos)
322                return page
[9b18735]323       
[6bbeacd4]324        page = self.add_empty_page()
[9f391af]325        pos = self.GetPageIndex(page)
[6bbeacd4]326        page.set_data(data)
327        self.SetPageText(pos, str(data.name))
[66ff250]328        self.opened_pages[page.uid] = page
[ae4ade7]329       
[6bbeacd4]330        return page
331       
332    def _onGetstate(self, event):
[cfc0913]333        """
[5062bbf]334        copy the state of a page
[cfc0913]335        """
[6bbeacd4]336        page = event.page
[66ff250]337        if page.uid in self.fit_page_name:
338           self.fit_page_name[page.uid].appendItem(page.createMemento()) 
[cfc0913]339           
[330573d]340    def _onUndo(self, event ):
341        """
[5062bbf]342        return the previous state of a given page is available
[330573d]343        """
344        page = event.page
[66ff250]345        if page.uid in self.fit_page_name:
346            if self.fit_page_name[page.uid].getCurrentPosition()==0:
[330573d]347                state = None
348            else:
[66ff250]349                state = self.fit_page_name[page.uid].getPreviousItem()
[fe496eeb]350                page._redo.Enable(True)
[330573d]351            page.reset_page(state)
[fe496eeb]352       
[2f189dc]353    def _onRedo(self, event): 
[fe496eeb]354        """
[5062bbf]355        return the next state available
[fe496eeb]356        """       
357        page = event.page
[66ff250]358        if page.uid in self.fit_page_name:
359            length= len(self.fit_page_name[page.uid])
360            if self.fit_page_name[page.uid].getCurrentPosition()== length -1:
[fe496eeb]361                state = None
362                page._redo.Enable(False)
[3b9e023]363                page._redo.Enable(True)
[fe496eeb]364            else:
[66ff250]365                state =self.fit_page_name[page.uid].getNextItem()
[fe496eeb]366            page.reset_page(state) 
[2f189dc]367                 
368    def _close_helper(self, selected_page):
[3f1af74]369        """
[5062bbf]370        Delete the given page from the notebook
[3f1af74]371        """
[2f189dc]372        #remove hint page
[9f391af]373        if selected_page == self.hint_page:
[2f189dc]374            return
375        ## removing sim_page
376        if selected_page == self.sim_page :
[c8deee5]377            self._manager.sim_page=None 
[2f189dc]378            return
[b787e68c]379       
[2f189dc]380        ## closing other pages
381        state = selected_page.createMemento()
382        page_name = selected_page.window_name
[c8deee5]383        page_finder = self._manager.get_page_finder() 
[2f189dc]384        fitproblem = None
385        ## removing fit page
[c6036f5]386        data = selected_page.get_data()
387        #Don' t remove plot for 2D
388        flag = True
389        if data.__class__.__name__ == 'Data2D':
390            flag = False
[2f189dc]391        if selected_page in page_finder:
392            #Delete the name of the page into the list of open page
[66ff250]393            for uid, list in self.opened_pages.iteritems():
[2f189dc]394                #Don't return any panel is the exact same page is created
[6bbeacd4]395               
[66ff250]396                if flag and selected_page.uid == uid:
397                    self._manager.remove_plot(uid, theory=False)
[2f189dc]398                    break 
399            del page_finder[selected_page]
400        ##remove the check box link to the model name of this page (selected_page)
401        try:
402            self.sim_page.draw_page()
403        except:
404            ## that page is already deleted no need to remove check box on
405            ##non existing page
406            pass
407               
408        #Delete the name of the page into the list of open page
[66ff250]409        for uid, list in self.opened_pages.iteritems():
[2f189dc]410            #Don't return any panel is the exact same page is created
[6bbeacd4]411           
[66ff250]412            if selected_page.uid == uid:
413                del self.opened_pages[selected_page.uid]
[2f189dc]414                break 
[0aeabc6]415     
[b787e68c]416 
Note: See TracBrowser for help on using the repository browser.