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