1 | import numpy |
---|
2 | |
---|
3 | from PyQt5 import QtCore |
---|
4 | from PyQt5 import QtGui |
---|
5 | from PyQt5 import QtWidgets |
---|
6 | |
---|
7 | from bumps import options |
---|
8 | from bumps import fitters |
---|
9 | |
---|
10 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
11 | |
---|
12 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
13 | from sas.qtgui.Perspectives.Fitting.FittingOptions import FittingOptions |
---|
14 | #from sas.qtgui.Perspectives.Fitting import ModelUtilities |
---|
15 | |
---|
16 | class FittingWindow(QtWidgets.QTabWidget): |
---|
17 | """ |
---|
18 | """ |
---|
19 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
20 | def __init__(self, parent=None, data=None): |
---|
21 | |
---|
22 | super(FittingWindow, self).__init__() |
---|
23 | |
---|
24 | self.parent = parent |
---|
25 | self._data = data |
---|
26 | |
---|
27 | # List of active fits |
---|
28 | self.tabs = [] |
---|
29 | |
---|
30 | # Max index for adding new, non-clashing tab names |
---|
31 | self.maxIndex = 0 |
---|
32 | |
---|
33 | # Index of the current tab |
---|
34 | self.currentTab = 0 |
---|
35 | |
---|
36 | # The default optimizer |
---|
37 | self.optimizer = 'Levenberg-Marquardt' |
---|
38 | |
---|
39 | # Dataset inde -> Fitting tab mapping |
---|
40 | self.dataToFitTab = {} |
---|
41 | |
---|
42 | # The tabs need to be closeable |
---|
43 | self.setTabsClosable(True) |
---|
44 | |
---|
45 | self.communicate = self.parent.communicator() |
---|
46 | |
---|
47 | # Initialize the first tab |
---|
48 | self.addFit(None) |
---|
49 | |
---|
50 | # Deal with signals |
---|
51 | self.tabCloseRequested.connect(self.tabCloses) |
---|
52 | self.communicate.dataDeletedSignal.connect(self.dataDeleted) |
---|
53 | |
---|
54 | # Perspective window not allowed to close by default |
---|
55 | self._allow_close = False |
---|
56 | |
---|
57 | # Fit options - uniform for all tabs |
---|
58 | self.fit_options = options.FIT_CONFIG |
---|
59 | self.fit_options_widget = FittingOptions(self, config=self.fit_options) |
---|
60 | self.fit_options.selected_id = fitters.LevenbergMarquardtFit.id |
---|
61 | |
---|
62 | # Listen to GUI Manager signal updating fit options |
---|
63 | self.fit_options_widget.fit_option_changed.connect(self.onFittingOptionsChange) |
---|
64 | |
---|
65 | #self.menu_manager = ModelUtilities.ModelManager() |
---|
66 | ## TODO: reuse these in FittingWidget properly |
---|
67 | #self.model_list_box = self.menu_manager.get_model_list() |
---|
68 | #self.model_dictionary = self.menu_manager.get_model_dictionary() |
---|
69 | |
---|
70 | #self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
71 | self.updateWindowTitle() |
---|
72 | |
---|
73 | def updateWindowTitle(self): |
---|
74 | """ |
---|
75 | Update the window title with the current optimizer name |
---|
76 | """ |
---|
77 | self.optimizer = self.fit_options.selected_name |
---|
78 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
79 | |
---|
80 | |
---|
81 | def setClosable(self, value=True): |
---|
82 | """ |
---|
83 | Allow outsiders close this widget |
---|
84 | """ |
---|
85 | assert isinstance(value, bool) |
---|
86 | |
---|
87 | self._allow_close = value |
---|
88 | |
---|
89 | def closeEvent(self, event): |
---|
90 | """ |
---|
91 | Overwrite QDialog close method to allow for custom widget close |
---|
92 | """ |
---|
93 | # Invoke fit page events |
---|
94 | for tab in self.tabs: |
---|
95 | tab.close() |
---|
96 | if self._allow_close: |
---|
97 | # reset the closability flag |
---|
98 | self.setClosable(value=False) |
---|
99 | event.accept() |
---|
100 | else: |
---|
101 | # Maybe we should just minimize |
---|
102 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
103 | event.ignore() |
---|
104 | |
---|
105 | def addFit(self, data, is_batch=False): |
---|
106 | """ |
---|
107 | Add a new tab for passed data |
---|
108 | """ |
---|
109 | tab = FittingWidget(parent=self.parent, data=data, tab_id=self.maxIndex+1) |
---|
110 | tab.is_batch_fitting = is_batch |
---|
111 | # Add this tab to the object library so it can be retrieved by scripting/jupyter |
---|
112 | tab_name = self.tabName(is_batch=is_batch) |
---|
113 | ObjectLibrary.addObject(tab_name, tab) |
---|
114 | self.tabs.append(tab) |
---|
115 | if data: |
---|
116 | self.updateFitDict(data, tab_name) |
---|
117 | self.maxIndex += 1 |
---|
118 | self.addTab(tab, tab_name) |
---|
119 | |
---|
120 | def updateFitDict(self, item_key, tab_name): |
---|
121 | """ |
---|
122 | Create a list if none exists and append if there's already a list |
---|
123 | """ |
---|
124 | item_key_str = str(item_key) |
---|
125 | if item_key_str in list(self.dataToFitTab.keys()): |
---|
126 | self.dataToFitTab[item_key_str].append(tab_name) |
---|
127 | else: |
---|
128 | self.dataToFitTab[item_key_str] = [tab_name] |
---|
129 | |
---|
130 | #print "CURRENT dict: ", self.dataToFitTab |
---|
131 | |
---|
132 | def tabName(self, is_batch=False): |
---|
133 | """ |
---|
134 | Get the new tab name, based on the number of fitting tabs so far |
---|
135 | """ |
---|
136 | page_name = "BatchPage" if is_batch else "FitPage" |
---|
137 | page_name = page_name + str(self.maxIndex) |
---|
138 | return page_name |
---|
139 | |
---|
140 | def resetTab(self, index): |
---|
141 | """ |
---|
142 | Adds a new tab and removes the last tab |
---|
143 | as a way of resetting the fit tabs |
---|
144 | """ |
---|
145 | # If data on tab empty - do nothing |
---|
146 | if index in self.tabs and not self.tabs[index].data: |
---|
147 | return |
---|
148 | # Add a new, empy tab |
---|
149 | self.addFit(None) |
---|
150 | # Remove the previous last tab |
---|
151 | self.tabCloses(index) |
---|
152 | |
---|
153 | def tabCloses(self, index): |
---|
154 | """ |
---|
155 | Update local bookkeeping on tab close |
---|
156 | """ |
---|
157 | #assert len(self.tabs) >= index |
---|
158 | # don't remove the last tab |
---|
159 | if len(self.tabs) <= 1: |
---|
160 | self.resetTab(index) |
---|
161 | return |
---|
162 | try: |
---|
163 | ObjectLibrary.deleteObjectByRef(self.tabs[index]) |
---|
164 | self.removeTab(index) |
---|
165 | del self.tabs[index] |
---|
166 | except IndexError: |
---|
167 | # The tab might have already been deleted previously |
---|
168 | pass |
---|
169 | |
---|
170 | def closeTabByName(self, tab_name): |
---|
171 | """ |
---|
172 | Given name of the fitting tab - close it |
---|
173 | """ |
---|
174 | for tab_index in range(len(self.tabs)): |
---|
175 | if self.tabText(tab_index) == tab_name: |
---|
176 | self.tabCloses(tab_index) |
---|
177 | pass # debug hook |
---|
178 | |
---|
179 | def dataDeleted(self, index_list): |
---|
180 | """ |
---|
181 | Delete fit tabs referencing given data |
---|
182 | """ |
---|
183 | if not index_list or not self.dataToFitTab: |
---|
184 | return |
---|
185 | for index_to_delete in index_list: |
---|
186 | index_to_delete_str = str(index_to_delete) |
---|
187 | if index_to_delete_str in list(self.dataToFitTab.keys()): |
---|
188 | for tab_name in self.dataToFitTab[index_to_delete_str]: |
---|
189 | # delete tab #index after corresponding data got removed |
---|
190 | self.closeTabByName(tab_name) |
---|
191 | self.dataToFitTab.pop(index_to_delete_str) |
---|
192 | |
---|
193 | #print "CURRENT dict: ", self.dataToFitTab |
---|
194 | |
---|
195 | def allowBatch(self): |
---|
196 | """ |
---|
197 | Tell the caller that we accept multiple data instances |
---|
198 | """ |
---|
199 | return True |
---|
200 | |
---|
201 | def setData(self, data_item=None, is_batch=False): |
---|
202 | """ |
---|
203 | Assign new dataset to the fitting instance |
---|
204 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
205 | Pass it over to the calculator |
---|
206 | """ |
---|
207 | assert data_item is not None |
---|
208 | |
---|
209 | if not isinstance(data_item, list): |
---|
210 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
211 | raise AttributeError(msg) |
---|
212 | |
---|
213 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
214 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
215 | raise AttributeError(msg) |
---|
216 | |
---|
217 | items = [data_item] if is_batch else data_item |
---|
218 | |
---|
219 | for data in items: |
---|
220 | # Find the first unassigned tab. |
---|
221 | # If none, open a new tab. |
---|
222 | available_tabs = list([tab.acceptsData() for tab in self.tabs]) |
---|
223 | |
---|
224 | if numpy.any(available_tabs): |
---|
225 | first_good_tab = available_tabs.index(True) |
---|
226 | self.tabs[first_good_tab].data = data |
---|
227 | tab_name = str(self.tabText(first_good_tab)) |
---|
228 | self.updateFitDict(data, tab_name) |
---|
229 | else: |
---|
230 | self.addFit(data, is_batch=is_batch) |
---|
231 | |
---|
232 | def onFittingOptionsChange(self, fit_engine): |
---|
233 | """ |
---|
234 | React to the fitting algorithm change by modifying window title |
---|
235 | """ |
---|
236 | fitter = [f.id for f in options.FITTERS if f.name == str(fit_engine)][0] |
---|
237 | # set the optimizer |
---|
238 | self.fit_options.selected_id = str(fitter) |
---|
239 | # Update the title |
---|
240 | self.updateWindowTitle() |
---|
241 | |
---|
242 | pass |
---|