source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py @ 17e2d502

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 17e2d502 was 17e2d502, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 5 years ago

Batch page serialization/deserialization

  • Property mode set to 100644
File size: 13.4 KB
Line 
1import numpy
2
3from PyQt5 import QtCore
4from PyQt5 import QtGui
5from PyQt5 import QtWidgets
6
7from bumps import options
8from bumps import fitters
9
10import sas.qtgui.Utilities.LocalConfig as LocalConfig
11import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary
12
13from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget
14from sas.qtgui.Perspectives.Fitting.ConstraintWidget import ConstraintWidget
15from sas.qtgui.Perspectives.Fitting.FittingOptions import FittingOptions
16from sas.qtgui.Perspectives.Fitting.GPUOptions import GPUOptions
17
18class FittingWindow(QtWidgets.QTabWidget):
19    """
20    """
21    tabsModifiedSignal = QtCore.pyqtSignal()
22    fittingStartedSignal = QtCore.pyqtSignal(list)
23    fittingStoppedSignal = QtCore.pyqtSignal(list)
24
25    name = "Fitting" # For displaying in the combo box in DataExplorer
26    def __init__(self, parent=None, data=None):
27
28        super(FittingWindow, self).__init__()
29
30        self.parent = parent
31        self._data = data
32
33        # List of active fits
34        self.tabs = []
35
36        # Max index for adding new, non-clashing tab names
37        self.maxIndex = 1
38
39        ## Index of the current tab
40        #self.currentTab = 0
41
42        # The default optimizer
43        self.optimizer = 'Levenberg-Marquardt'
44
45        # Dataset index -> Fitting tab mapping
46        self.dataToFitTab = {}
47
48        # The tabs need to be closeable
49        self.setTabsClosable(True)
50
51        # The tabs need to be movabe
52        self.setMovable(True)
53
54        self.communicate = self.parent.communicator()
55
56        # Initialize the first tab
57        self.addFit(None)
58
59        # Deal with signals
60        self.tabCloseRequested.connect(self.tabCloses)
61        self.communicate.dataDeletedSignal.connect(self.dataDeleted)
62        self.fittingStartedSignal.connect(self.onFittingStarted)
63        self.fittingStoppedSignal.connect(self.onFittingStopped)
64
65        self.communicate.copyFitParamsSignal.connect(self.onParamCopy)
66        self.communicate.pasteFitParamsSignal.connect(self.onParamPaste)
67        self.communicate.copyExcelFitParamsSignal.connect(self.onExcelCopy)
68        self.communicate.copyLatexFitParamsSignal.connect(self.onLatexCopy)
69
70
71        # Perspective window not allowed to close by default
72        self._allow_close = False
73
74        # Fit options - uniform for all tabs
75        self.fit_options = options.FIT_CONFIG
76        self.fit_options_widget = FittingOptions(self, config=self.fit_options)
77        self.fit_options.selected_id = fitters.LevenbergMarquardtFit.id
78
79        # Listen to GUI Manager signal updating fit options
80        self.fit_options_widget.fit_option_changed.connect(self.onFittingOptionsChange)
81
82        # GPU Options
83        self.gpu_options_widget = GPUOptions(self)
84
85        self.updateWindowTitle()
86
87    def updateWindowTitle(self):
88        """
89        Update the window title with the current optimizer name
90        """
91        self.optimizer = self.fit_options.selected_name
92        self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer)
93
94
95    def setClosable(self, value=True):
96        """
97        Allow outsiders to close this widget
98        """
99        assert isinstance(value, bool)
100
101        self._allow_close = value
102
103    def onParamCopy(self):
104        self.currentTab.onCopyToClipboard("")
105
106    def onParamPaste(self):
107        self.currentTab.onParameterPaste()
108
109    def onExcelCopy(self):
110        self.currentTab.onCopyToClipboard("Excel")
111
112    def onLatexCopy(self):
113        self.currentTab.onCopyToClipboard("Latex")
114
115    def serializeAllFitpage(self):
116        # serialize all active fitpages and return
117        # a dictionary: {data_id: fitpage_state}
118        params = {}
119        for i, tab in enumerate(self.tabs):
120            tab_data = self.getSerializedFitpage(tab)
121            if tab.tab_id is None: continue
122            if 'data_id' not in tab_data: continue
123            id = tab_data['data_id'][0]
124            if isinstance(id, list):
125                for i in id:
126                    if i in params:
127                        params[i].append(tab_data)
128                    else:
129                        params[i] = [tab_data]
130            else:
131                if id in params:
132                    params[id].append(tab_data)
133                else:
134                    params[id] = [tab_data]
135        return params
136
137    def serializeCurrentFitpage(self):
138        # serialize current(active) fitpage
139        return self.getSerializedFitpage(self.currentTab)
140
141    def getSerializedFitpage(self, tab):
142        """
143        get serialize requested fit tab
144        """
145        fitpage_state = tab.getFitPage()
146        fitpage_state += tab.getFitModel()
147        # put the text into dictionary
148        line_dict = {}
149        for line in fitpage_state:
150            #content = line.split(',')
151            if len(line) > 1:
152                line_dict[line[0]] = line[1:]
153        return line_dict
154
155    def currentTabDataId(self):
156        """
157        Returns the data ID of the current tab
158        """
159        tab_id = None
160        if self.currentTab.data:
161            tab_id = self.currentTab.data.id
162        return tab_id
163
164    def updateFromParameters(self, parameters):
165        """
166        Pass the update parameters to the current fit page
167        """
168        self.currentTab.createPageForParameters(parameters)
169
170    def closeEvent(self, event):
171        """
172        Overwrite QDialog close method to allow for custom widget close
173        """
174        # Invoke fit page events
175        if self._allow_close:
176            # reset the closability flag
177            self.setClosable(value=False)
178            # Tell the MdiArea to close the container
179            self.parentWidget().close()
180            event.accept()
181        else:
182            # Maybe we should just minimize
183            self.setWindowState(QtCore.Qt.WindowMinimized)
184            event.ignore()
185
186    def addFit(self, data, is_batch=False):
187        """
188        Add a new tab for passed data
189        """
190        tab     = FittingWidget(parent=self.parent, data=data, tab_id=self.maxIndex)
191        tab.is_batch_fitting = is_batch
192
193        # Add this tab to the object library so it can be retrieved by scripting/jupyter
194        tab_name = self.getTabName(is_batch=is_batch)
195        ObjectLibrary.addObject(tab_name, tab)
196        self.tabs.append(tab)
197        if data:
198            self.updateFitDict(data, tab_name)
199        self.maxIndex += 1
200        icon = QtGui.QIcon()
201        if is_batch:
202            icon.addPixmap(QtGui.QPixmap("src/sas/qtgui/images/icons/layers.svg"))
203        self.addTab(tab, icon, tab_name)
204        # Show the new tab
205        self.setCurrentWidget(tab);
206        # Notify listeners
207        self.tabsModifiedSignal.emit()
208
209    def addConstraintTab(self):
210        """
211        Add a new C&S fitting tab
212        """
213        tabs = [isinstance(tab, ConstraintWidget) for tab in self.tabs]
214        if any(tabs):
215            # We already have a C&S tab: show it
216            self.setCurrentIndex(tabs.index(True))
217            return
218        tab     = ConstraintWidget(parent=self)
219        # Add this tab to the object library so it can be retrieved by scripting/jupyter
220        tab_name = self.getCSTabName() # TODO update the tab name scheme
221        ObjectLibrary.addObject(tab_name, tab)
222        self.tabs.append(tab)
223        icon = QtGui.QIcon()
224        icon.addPixmap(QtGui.QPixmap("src/sas/qtgui/images/icons/link.svg"))
225        self.addTab(tab, icon, tab_name)
226
227        # This will be the last tab, so set the index accordingly
228        self.setCurrentIndex(self.count()-1)
229
230    def updateFitDict(self, item_key, tab_name):
231        """
232        Create a list if none exists and append if there's already a list
233        """
234        item_key_str = str(item_key)
235        if item_key_str in list(self.dataToFitTab.keys()):
236            self.dataToFitTab[item_key_str].append(tab_name)
237        else:
238            self.dataToFitTab[item_key_str] = [tab_name]
239
240    def getTabName(self, is_batch=False):
241        """
242        Get the new tab name, based on the number of fitting tabs so far
243        """
244        page_name = "BatchPage" if is_batch else "FitPage"
245        page_name = page_name + str(self.maxIndex)
246        return page_name
247
248    def getCSTabName(self):
249        """
250        Get the new tab name, based on the number of fitting tabs so far
251        """
252        page_name = "Const. & Simul. Fit"
253        return page_name
254
255    def resetTab(self, index):
256        """
257        Adds a new tab and removes the last tab
258        as a way of resetting the fit tabs
259        """
260        # If data on tab empty - do nothing
261        if index in self.tabs and not self.tabs[index].data:
262            return
263        # Add a new, empy tab
264        self.addFit(None)
265        # Remove the previous last tab
266        self.tabCloses(index)
267
268    def tabCloses(self, index):
269        """
270        Update local bookkeeping on tab close
271        """
272        #assert len(self.tabs) >= index
273        # don't remove the last tab
274        if len(self.tabs) <= 1:
275            self.resetTab(index)
276            return
277        try:
278            ObjectLibrary.deleteObjectByRef(self.tabs[index])
279            self.removeTab(index)
280            del self.tabs[index]
281            self.tabsModifiedSignal.emit()
282        except IndexError:
283            # The tab might have already been deleted previously
284            pass
285
286    def closeTabByName(self, tab_name):
287        """
288        Given name of the fitting tab - close it
289        """
290        for tab_index in range(len(self.tabs)):
291            if self.tabText(tab_index) == tab_name:
292                self.tabCloses(tab_index)
293        pass # debug hook
294
295    def dataDeleted(self, index_list):
296        """
297        Delete fit tabs referencing given data
298        """
299        if not index_list or not self.dataToFitTab:
300            return
301        for index_to_delete in index_list:
302            index_to_delete_str = str(index_to_delete)
303            if index_to_delete_str in list(self.dataToFitTab.keys()):
304                for tab_name in self.dataToFitTab[index_to_delete_str]:
305                    # delete tab #index after corresponding data got removed
306                    self.closeTabByName(tab_name)
307                self.dataToFitTab.pop(index_to_delete_str)
308
309    def allowBatch(self):
310        """
311        Tell the caller that we accept multiple data instances
312        """
313        return True
314
315    def isSerializable(self):
316        """
317        Tell the caller that this perspective writes its state
318        """
319        return True
320
321    def setData(self, data_item=None, is_batch=False):
322        """
323        Assign new dataset to the fitting instance
324        Obtain a QStandardItem object and dissect it to get Data1D/2D
325        Pass it over to the calculator
326        """
327        assert data_item is not None
328
329        if not isinstance(data_item, list):
330            msg = "Incorrect type passed to the Fitting Perspective"
331            raise AttributeError(msg)
332
333        if not isinstance(data_item[0], QtGui.QStandardItem):
334            msg = "Incorrect type passed to the Fitting Perspective"
335            raise AttributeError(msg)
336
337        if is_batch:
338            # Just create a new fit tab. No empty batchFit tabs
339            self.addFit(data_item, is_batch=is_batch)
340            return
341
342        items = [data_item] if is_batch else data_item
343        for data in items:
344            # Find the first unassigned tab.
345            # If none, open a new tab.
346            available_tabs = [tab.acceptsData() for tab in self.tabs]
347
348            if numpy.any(available_tabs):
349                first_good_tab = available_tabs.index(True)
350                self.tabs[first_good_tab].data = data
351                tab_name = str(self.tabText(first_good_tab))
352                self.updateFitDict(data, tab_name)
353            else:
354                self.addFit(data, is_batch=is_batch)
355
356    def onFittingOptionsChange(self, fit_engine):
357        """
358        React to the fitting algorithm change by modifying window title
359        """
360        fitter = [f.id for f in options.FITTERS if f.name == str(fit_engine)][0]
361        # set the optimizer
362        self.fit_options.selected_id = str(fitter)
363        # Update the title
364        self.updateWindowTitle()
365
366    def onFittingStarted(self, tabs_for_fitting=None):
367        """
368        Notify tabs listed in tabs_for_fitting
369        that the fitting thread started
370        """
371        assert(isinstance(tabs_for_fitting, list))
372        assert(len(tabs_for_fitting)>0)
373
374        for tab_object in self.tabs:
375            if not isinstance(tab_object, FittingWidget):
376                continue
377            page_name = "Page%s"%tab_object.tab_id
378            if any([page_name in tab for tab in tabs_for_fitting]):
379                tab_object.disableInteractiveElements()
380
381        pass
382
383    def onFittingStopped(self, tabs_for_fitting=None):
384        """
385        Notify tabs listed in tabs_for_fitting
386        that the fitting thread stopped
387        """
388        assert(isinstance(tabs_for_fitting, list))
389        assert(len(tabs_for_fitting)>0)
390
391        for tab_object in self.tabs:
392            if not isinstance(tab_object, FittingWidget):
393                continue
394            page_name = "Page%s"%tab_object.tab_id
395            if any([page_name in tab for tab in tabs_for_fitting]):
396                tab_object.enableInteractiveElements()
397
398        pass
399
400    def getCurrentStateAsXml(self):
401        """
402        Returns an XML version of the current state
403        """
404        state = {}
405        for tab in self.tabs:
406            pass
407        return state
408
409    @property
410    def currentTab(self):
411        """
412        Returns the tab widget currently shown
413        """
414        return self.currentWidget()
415
Note: See TracBrowser for help on using the repository browser.