[6f7f652] | 1 | import numpy |
---|
[0efe791] | 2 | |
---|
[4c7dd9f] | 3 | from PyQt4 import QtCore |
---|
[60af928] | 4 | from PyQt4 import QtGui |
---|
[4c7dd9f] | 5 | |
---|
[2d0e0c1] | 6 | from bumps import options |
---|
| 7 | from bumps import fitters |
---|
| 8 | |
---|
[61a92d4] | 9 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
[5236449] | 10 | |
---|
[1bc27f1] | 11 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
[2d0e0c1] | 12 | from sas.qtgui.Perspectives.Fitting.FittingOptions import FittingOptions |
---|
[125c4be] | 13 | from sas.qtgui.Perspectives.Fitting import ModelUtilities |
---|
[8bdb6f5] | 14 | |
---|
[60af928] | 15 | class FittingWindow(QtGui.QTabWidget): |
---|
[0efe791] | 16 | """ |
---|
| 17 | """ |
---|
[60af928] | 18 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
[811bec1] | 19 | def __init__(self, parent=None, data=None): |
---|
[f46f6dc] | 20 | super(FittingWindow, self).__init__() |
---|
| 21 | |
---|
| 22 | self.parent = parent |
---|
| 23 | self._data = data |
---|
| 24 | |
---|
| 25 | # List of active fits |
---|
| 26 | self.tabs = [] |
---|
| 27 | |
---|
| 28 | # Max index for adding new, non-clashing tab names |
---|
| 29 | self.maxIndex = 0 |
---|
| 30 | |
---|
| 31 | # Index of the current tab |
---|
| 32 | self.currentTab = 0 |
---|
| 33 | |
---|
| 34 | # The current optimizer |
---|
[1bc27f1] | 35 | self.optimizer = 'Levenberg-Marquardt' |
---|
[f46f6dc] | 36 | |
---|
| 37 | # The tabs need to be closeable |
---|
| 38 | self.setTabsClosable(True) |
---|
[60af928] | 39 | |
---|
[811bec1] | 40 | self.communicate = self.parent.communicator() |
---|
| 41 | |
---|
[60af928] | 42 | # Initialize the first tab |
---|
[f46f6dc] | 43 | self.addFit(None) |
---|
| 44 | |
---|
| 45 | # Deal with signals |
---|
| 46 | self.tabCloseRequested.connect(self.tabCloses) |
---|
| 47 | |
---|
[b1e36a3] | 48 | # Perspective window not allowed to close by default |
---|
| 49 | self._allow_close = False |
---|
| 50 | |
---|
[2d0e0c1] | 51 | # Fit options - uniform for all tabs |
---|
| 52 | self.fit_options = options.FIT_CONFIG |
---|
| 53 | self.fit_options_widget = FittingOptions(self, config=self.fit_options) |
---|
| 54 | self.fit_options.selected_id = fitters.LevenbergMarquardtFit.id |
---|
| 55 | |
---|
| 56 | # Listen to GUI Manager signal updating fit options |
---|
| 57 | self.fit_options_widget.fit_option_changed.connect(self.onFittingOptionsChange) |
---|
| 58 | |
---|
[125c4be] | 59 | self.menu_manager = ModelUtilities.ModelManager() |
---|
| 60 | # TODO: reuse these in FittingWidget properly |
---|
| 61 | self.model_list_box = self.menu_manager.get_model_list() |
---|
| 62 | self.model_dictionary = self.menu_manager.get_model_dictionary() |
---|
| 63 | |
---|
[2d0e0c1] | 64 | #self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
| 65 | self.updateWindowTitle() |
---|
| 66 | |
---|
| 67 | def updateWindowTitle(self): |
---|
| 68 | """ |
---|
| 69 | Update the window title with the current optimizer name |
---|
| 70 | """ |
---|
| 71 | self.optimizer = self.fit_options.selected_name |
---|
[f46f6dc] | 72 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
[60af928] | 73 | |
---|
[2d0e0c1] | 74 | |
---|
[b1e36a3] | 75 | def setClosable(self, value=True): |
---|
| 76 | """ |
---|
| 77 | Allow outsiders close this widget |
---|
| 78 | """ |
---|
| 79 | assert isinstance(value, bool) |
---|
| 80 | |
---|
| 81 | self._allow_close = value |
---|
| 82 | |
---|
| 83 | def closeEvent(self, event): |
---|
| 84 | """ |
---|
| 85 | Overwrite QDialog close method to allow for custom widget close |
---|
| 86 | """ |
---|
[2add354] | 87 | # Invoke fit page events |
---|
| 88 | for tab in self.tabs: |
---|
| 89 | tab.close() |
---|
[b1e36a3] | 90 | if self._allow_close: |
---|
| 91 | # reset the closability flag |
---|
| 92 | self.setClosable(value=False) |
---|
| 93 | event.accept() |
---|
| 94 | else: |
---|
| 95 | # Maybe we should just minimize |
---|
| 96 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
[2add354] | 97 | event.ignore() |
---|
[b1e36a3] | 98 | |
---|
[60af928] | 99 | def addFit(self, data): |
---|
| 100 | """ |
---|
| 101 | Add a new tab for passed data |
---|
| 102 | """ |
---|
[1bc27f1] | 103 | tab = FittingWidget(parent=self.parent, data=data, tab_id=self.maxIndex+1) |
---|
[61a92d4] | 104 | # Add this tab to the object library so it can be retrieved by scripting/jupyter |
---|
| 105 | ObjectLibrary.addObject(self.tabName(), tab) |
---|
[f46f6dc] | 106 | self.tabs.append(tab) |
---|
| 107 | self.maxIndex += 1 |
---|
| 108 | self.addTab(tab, self.tabName()) |
---|
| 109 | |
---|
| 110 | def tabName(self): |
---|
| 111 | """ |
---|
| 112 | Get the new tab name, based on the number of fitting tabs so far |
---|
| 113 | """ |
---|
| 114 | page_name = "FitPage" + str(self.maxIndex) |
---|
| 115 | return page_name |
---|
| 116 | |
---|
| 117 | def tabCloses(self, index): |
---|
| 118 | """ |
---|
| 119 | Update local bookkeeping on tab close |
---|
| 120 | """ |
---|
[cbcdd2c] | 121 | assert len(self.tabs) >= index |
---|
[f46f6dc] | 122 | # don't remove the last tab |
---|
| 123 | if len(self.tabs) <= 1: |
---|
| 124 | return |
---|
[61a92d4] | 125 | ObjectLibrary.deleteObjectByRef(self.tabs[index]) |
---|
[f46f6dc] | 126 | del self.tabs[index] |
---|
| 127 | self.removeTab(index) |
---|
| 128 | |
---|
| 129 | def allowBatch(self): |
---|
| 130 | """ |
---|
| 131 | Tell the caller that we accept multiple data instances |
---|
| 132 | """ |
---|
| 133 | return True |
---|
| 134 | |
---|
[68c96d3] | 135 | def setData(self, data_item=None): |
---|
[f46f6dc] | 136 | """ |
---|
| 137 | Assign new dataset to the fitting instance |
---|
[5236449] | 138 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
| 139 | Pass it over to the calculator |
---|
[f46f6dc] | 140 | """ |
---|
[cbcdd2c] | 141 | assert data_item is not None |
---|
[68c96d3] | 142 | |
---|
[5236449] | 143 | if not isinstance(data_item, list): |
---|
| 144 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 145 | raise AttributeError, msg |
---|
| 146 | |
---|
| 147 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
| 148 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 149 | raise AttributeError, msg |
---|
| 150 | |
---|
[454670d] | 151 | for data in data_item: |
---|
| 152 | # Find the first unassigned tab. |
---|
| 153 | # If none, open a new tab. |
---|
| 154 | available_tabs = list(map(lambda tab: tab.acceptsData(), self.tabs)) |
---|
| 155 | |
---|
| 156 | if numpy.any(available_tabs): |
---|
| 157 | self.tabs[available_tabs.index(True)].data = data |
---|
| 158 | else: |
---|
| 159 | self.addFit(data) |
---|
[2d0e0c1] | 160 | |
---|
| 161 | def onFittingOptionsChange(self, fit_engine, fit_options): |
---|
| 162 | """ |
---|
| 163 | """ |
---|
| 164 | fitter = [f.id for f in options.FITTERS if f.name == str(fit_engine)][0] |
---|
| 165 | |
---|
| 166 | # set the optimizer |
---|
| 167 | self.fit_options.selected_id = str(fitter) |
---|
| 168 | # set the options |
---|
| 169 | # |
---|
| 170 | # Update the title |
---|
| 171 | self.updateWindowTitle() |
---|
| 172 | |
---|
| 173 | pass |
---|