source: sasview/sansview/perspectives/fitting/fitpanel.py @ 90d04eb

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 90d04eb was 90d04eb, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on fitpanel

  • Property mode set to 100644
File size: 15.2 KB
Line 
1
2import numpy
3import string 
4import wx
5import wx.aui
6
7import basepage
8
9_BOX_WIDTH = 80
10
11
12class StateIterator(object):
13    """
14        Contains all saved state of a given page.
15        Provide position of the current state of a page, the first save state
16        and the last state for a given page.
17        Allow easy undo or redo for a given page 
18    """
19    def __init__(self):
20        self._current=0
21       
22   
23    def __iter__(self):
24        return self
25   
26   
27    def first(self):
28        self._current =0
29        return self._current
30   
31    def next(self, max ):
32        if self._current < max:
33            self._current += 1
34        return self._current
35   
36    def previous(self):
37        if self._current > 0:
38            self._current = self._current -1
39        return self._current
40   
41    def currentPosition(self):
42        return self._current
43   
44    def setPosition(self, value):
45        if value >=0:
46            self._current = int(value)
47       
48   
49class ListOfState(list):     
50    def __init__(self, *args, **kw):
51        list.__init__(self, *args, **kw)
52        self.iterator = StateIterator()
53       
54    def appendItem(self, x):
55        self.append(x)
56        self.iterator.setPosition(value= len(self)-1)
57       
58    def removeItem(self, x):
59        self.iterator.previous()
60        self.remove(x)
61       
62    def getPreviousItem(self):
63        position = self.iterator.previous()
64       
65        if position < 0:
66            return None
67        else:
68            return self[position]
69       
70    def getNextItem(self):
71        position = self.iterator.next(max= len(self)-1)
72        if position >= len(self):
73            return None
74        else:
75            return self[position]
76       
77    def getCurrentItem(self):
78        postion = self.iterator.currentPosition()
79        if postion >= 0 and position < len(self):
80            return self[postion]
81        else:
82            return None
83       
84    def getCurrentPosition(self):
85        return self.iterator.currentPosition()
86         
87
88class PageInfo(object):
89    """
90        this class contains the minimum numbers of data members
91        a fitpage or model page need to be initialized.
92    """
93    data = None
94    model =  None
95    manager = None
96    event_owner= None
97    model_list_box = None
98    name = None
99    ## Internal name for the AUI manager
100    window_name = "Page"
101    ## Title to appear on top of the window
102    window_caption = "Page"
103    #type of page can be real data , theory 1D or therory2D
104    type = "Data"
105    def __init__(self, model=None, data=None, manager=None,
106                  event_owner=None, model_list_box=None, name=None):
107        """
108            Initialize data members
109        """
110        self.data = data
111        self.model= model
112        self.manager= manager
113        self.event_owner= event_owner
114        self.model_list_box = model_list_box
115        self.name=None
116        self.window_name = "Page"
117        self.window_caption = "Page"
118        self.type = "Data"
119       
120class FitPanel(wx.aui.AuiNotebook):   
121
122    """
123        FitPanel class contains fields allowing to fit  models and  data
124        @note: For Fit to be performed the user should check at least one parameter
125        on fit Panel window.
126       
127    """
128    ## Internal name for the AUI manager
129    window_name = "Fit panel"
130    ## Title to appear on top of the window
131    window_caption = "Fit Panel "
132    CENTER_PANE = True
133   
134    def __init__(self, parent, *args, **kwargs):
135        wx.aui.AuiNotebook.__init__(self, parent, -1,
136                    style= wx.aui.AUI_NB_WINDOWLIST_BUTTON|
137                    wx.aui.AUI_NB_DEFAULT_STYLE|
138                    wx.CLIP_CHILDREN)
139   
140        self.manager=None
141        self.parent=parent
142        self.event_owner=None
143       
144        pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE
145        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page)
146       
147        #dictionary of miodel {model class name, model class}
148        self.model_list_box = {}
149        ## save the title of the last page tab added
150        self.fit_page_name = {}
151        ## list of existing fit page
152        self.opened_pages = {}
153        #page of simultaneous fit
154        self.sim_page = None
155        ## get the state of a page
156        self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate)
157        self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo)
158        self.Bind(basepage.EVT_NEXT_STATE, self._onRedo)
159       
160        #add default page
161        from hint_fitpage import HintFitPage
162        self.hint_page = HintFitPage(self) 
163        self.AddPage(page=self.hint_page, caption="Hint")
164        #Add the first fit page
165        self.add_empty_page()
166       
167        # increment number for model name
168        self.count=0
169        #updating the panel
170        self.Update()
171        self.Center()
172 
173    def on_close_page(self, event):
174        """
175             close page and remove all references to the closed page
176        """
177        nbr_page = self.GetPageCount()
178        if nbr_page == 1:
179            event.Veto()
180            return 
181        selected_page = self.GetPage(self.GetSelection())
182        self._close_helper(selected_page=selected_page)
183       
184    def close_page_with_data(self, deleted_data):
185        """
186            close a fit page when its data is completely remove from the graph
187        """
188        for index in range(self.GetPageCount()):
189            selected_page = self.GetPage(index) 
190            if hasattr(selected_page,"get_data"):
191                data = selected_page.get_data()
192                if data.name == deleted_data.name:
193                    self._close_helper(selected_page)
194                    self.DeletePage(index)
195                    break
196       
197    def set_manager(self, manager):
198        """
199             set panel manager
200             @param manager: instance of plugin fitting
201        """
202        self.manager = manager
203
204       
205    def set_owner(self,owner):
206        """
207            set and owner for fitpanel
208            @param owner: the class responsible of plotting
209        """
210        self.event_owner = owner
211   
212    def set_model_list(self, dict):
213         """
214             copy a dictionary of model into its own dictionary
215             @param dict: dictionnary made of model name as key and model class
216             as value
217         """
218         self.model_list_box = dict
219       
220 
221    def get_current_page(self):
222        """
223            @return the current page selected
224        """
225        return self.GetPage(self.GetSelection() )
226   
227    def add_sim_page(self):
228        """
229            Add the simultaneous fit page
230        """
231        from simfitpage import SimultaneousFitPage
232        page_finder= self.manager.get_page_finder()
233        self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1)
234       
235        self.AddPage(self.sim_page,caption="Simultaneous Fit",select=True)
236        self.sim_page.set_manager(self.manager)
237        return self.sim_page
238       
239    def get_page_info(self, data=None):
240        """
241            fill information required to add a page in the fit panel
242        """
243        name = "Fit Page"
244        type = 'empty'
245        if data is not None:
246            if data.is_data:
247                name = data.name
248                type = 'Data'
249            else:
250                if data.__class__.__name__ == "Data2D":
251                    name = 'Model 2D Fit'
252                    type = 'Theory2D'
253                else:
254                    name = 'Model 1D Fit'
255                    type = 'Theory1D'
256        page_info = PageInfo(data=data, name=name)
257        page_info.event_owner = self.event_owner
258        page_info.manager = self.manager
259        page_info.window_name = name
260        page_info.window_caption = name
261        page_info.type = type
262        return page_info
263   
264    def add_empty_page(self):
265        """
266            add an empty page
267        """
268        page_info = self.get_page_info()
269        from fitpage import FitPage
270        panel = FitPage(parent=self, page_info=page_info)
271        self.AddPage(page=panel, caption=page_info.window_name, select=True)
272        self.opened_pages[page_info.type] = [page_info.window_name, panel]
273        return panel
274   
275    def add_page(self, page_info):
276        """
277            add a new page
278        """
279        from fitpage import FitPage
280        panel = FitPage(parent=self, page_info=page_info)
281        self.AddPage(page=panel, caption=page_info.window_name, select=True)
282        index = self.GetPageIndex(panel)
283        self.change_page_content(data=page_info.data, index=index)
284        return panel
285   
286    def change_page_content(self, data, index):
287        """
288            replace the contains of an existing page
289        """
290        page_info = self.get_page_info(data=data)
291        self.SetPageText(index, page_info.window_name)
292        panel = self.GetPage(index)
293        panel.set_data(data)
294        if panel.model_list_box is None or len(panel.model_list_box) == 0: 
295            page_info.model_list_box = self.model_list_box.get_list()
296            panel.populate_box(dict=page_info.model_list_box)
297            panel.initialize_combox()
298        panel.set_page_info(page_info=page_info)
299        self.opened_pages[page_info.type] = [page_info.window_name, panel]
300        return panel
301   
302    def replace_page(self, index, page_info, type):
303        """
304            replace an existing page
305        """
306        self.DeletePage(index)
307        del self.opened_pages[type]
308        return self.add_page(page_info=page_info)
309       
310    def add_fit_page(self, data, reset=False):
311        """
312            Add a fitting page on the notebook contained by fitpanel
313            @param data: data to fit
314            @return panel : page just added for further used. is used by fitting module
315        """
316        if data is None:
317            return None
318        page_info = self.get_page_info(data=data)
319        type = page_info.type
320        npages = len(self.opened_pages.keys())
321        #check if only and empty page is opened
322        if len(self.opened_pages.keys()) > 0:
323            first_page_type = self.opened_pages.keys()[0]
324            if npages == 1 and first_page_type in ['empty']:
325                #replace the first empty page
326                name, panel = self.opened_pages[first_page_type]
327                index = self.GetPageIndex(panel)
328                panel = self.change_page_content(data=data, index=index)
329                del self.opened_pages[first_page_type]
330                return panel
331        if type in self.opened_pages.keys():
332            #this type of page is already created but it is a theory
333            # meaning the same page is just to fit different data
334            if not type.lower() in ['data']:
335                #delete the previous theory page and add a new one
336                name, panel = self.opened_pages[type]
337                self.manager.reset_plot_panel(panel.get_data())
338                #delete the existing page and replace it
339                index = self.GetPageIndex(panel)
340                panel = self.replace_page(index=index, page_info=page_info, type=type)
341                return panel
342            else:
343                for name, panel in self.opened_pages.values():
344                    #Don't return any panel is the exact same page is created
345                    if name == page_info.window_name:
346                        return None
347                    else:
348                        panel = self.add_page(page_info=page_info)
349                        return panel       
350        else:
351            #a new type of page is created
352            panel = self.add_page(page_info=page_info)
353            return panel
354   
355    def  _onGetstate(self, event):
356        """
357            copy the state of a page
358        """
359        page= event.page
360        if page.window_name in self.fit_page_name:
361            self.fit_page_name[page.window_name].appendItem(page.createMemento()) 
362           
363    def _onUndo(self, event ):
364        """
365            return the previous state of a given page is available
366        """
367        page = event.page
368        if page.window_name in self.fit_page_name:
369            if self.fit_page_name[page.window_name].getCurrentPosition()==0:
370                state = None
371            else:
372                state = self.fit_page_name[page.window_name].getPreviousItem()
373                page._redo.Enable(True)
374            page.reset_page(state)
375       
376    def _onRedo(self, event): 
377        """
378            return the next state available
379        """       
380        page = event.page
381        if page.window_name in self.fit_page_name:
382            length= len(self.fit_page_name[page.window_name])
383            if self.fit_page_name[page.window_name].getCurrentPosition()== length -1:
384                state = None
385                page._redo.Enable(False)
386                page._redo.Enable(True)
387            else:
388                state = self.fit_page_name[page.window_name].getNextItem()
389            page.reset_page(state) 
390                 
391    def _close_helper(self, selected_page):
392        """
393            Delete the given page from the notebook
394        """
395        #remove hint page
396        if selected_page == self.hint_page:
397            return
398        ## removing sim_page
399        if selected_page == self.sim_page :
400            self.manager.sim_page=None 
401            return
402       
403        ## closing other pages
404        state = selected_page.createMemento()
405        page_name = selected_page.window_name
406        page_finder = self.manager.get_page_finder() 
407        fitproblem = None
408        ## removing fit page
409        data = selected_page.get_data()
410        #Don' t remove plot for 2D
411        flag = True
412        if data.__class__.__name__ == 'Data2D':
413            flag = False
414        if selected_page in page_finder:
415            #Delete the name of the page into the list of open page
416            for type, list in self.opened_pages.iteritems():
417                #Don't return any panel is the exact same page is created
418                name = str(list[0])
419                if flag and selected_page.window_name == name:
420                    if type.lower() in ['theory1d', 'theory2d']:
421                        self.manager.remove_plot(selected_page, theory=True)
422                    else:
423                        self.manager.remove_plot(selected_page, theory=False)
424                    break 
425            del page_finder[selected_page]
426        ##remove the check box link to the model name of this page (selected_page)
427        try:
428            self.sim_page.draw_page()
429        except:
430            ## that page is already deleted no need to remove check box on
431            ##non existing page
432            pass
433               
434        #Delete the name of the page into the list of open page
435        for type, list in self.opened_pages.iteritems():
436            #Don't return any panel is the exact same page is created
437            name = str(list[0])
438            if selected_page.window_name == name:
439                del self.opened_pages[type]
440                break 
441     
442 
Note: See TracBrowser for help on using the repository browser.