1 | import sys |
---|
2 | import json |
---|
3 | import os |
---|
4 | import numpy |
---|
5 | from collections import defaultdict |
---|
6 | |
---|
7 | import logging |
---|
8 | import traceback |
---|
9 | from twisted.internet import threads |
---|
10 | |
---|
11 | from PyQt4 import QtGui |
---|
12 | from PyQt4 import QtCore |
---|
13 | |
---|
14 | from sasmodels import generate |
---|
15 | from sasmodels import modelinfo |
---|
16 | from sasmodels.sasview_model import load_standard_models |
---|
17 | |
---|
18 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
19 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
20 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
21 | import sas.qtgui.GuiUtils as GuiUtils |
---|
22 | from sas.sasgui.perspectives.fitting.model_thread import Calc1D |
---|
23 | from sas.sasgui.perspectives.fitting.model_thread import Calc2D |
---|
24 | |
---|
25 | from UI.FittingWidgetUI import Ui_FittingWidgetUI |
---|
26 | from sas.qtgui.Perspectives.Fitting.FittingLogic import FittingLogic |
---|
27 | from sas.qtgui.Perspectives.Fitting import FittingUtilities |
---|
28 | |
---|
29 | TAB_MAGNETISM = 4 |
---|
30 | TAB_POLY = 3 |
---|
31 | CATEGORY_DEFAULT = "Choose category..." |
---|
32 | CATEGORY_STRUCTURE = "Structure Factor" |
---|
33 | QMIN_DEFAULT = 0.0005 |
---|
34 | QMAX_DEFAULT = 0.5 |
---|
35 | NPTS_DEFAULT = 50 |
---|
36 | |
---|
37 | class FittingWidget(QtGui.QWidget, Ui_FittingWidgetUI): |
---|
38 | """ |
---|
39 | Main widget for selecting form and structure factor models |
---|
40 | """ |
---|
41 | def __init__(self, parent=None, data=None, id=1): |
---|
42 | |
---|
43 | super(FittingWidget, self).__init__() |
---|
44 | |
---|
45 | # Necessary globals |
---|
46 | self.parent = parent |
---|
47 | # SasModel is loaded |
---|
48 | self.model_is_loaded = False |
---|
49 | # Data[12]D passed and set |
---|
50 | self.data_is_loaded = False |
---|
51 | # Current SasModel in view |
---|
52 | self.kernel_module = None |
---|
53 | # Current SasModel view dimension |
---|
54 | self.is2D = False |
---|
55 | # Current SasModel is multishell |
---|
56 | self.model_has_shells = False |
---|
57 | # Utility variable to enable unselectable option in category combobox |
---|
58 | self._previous_category_index = 0 |
---|
59 | # Utility variable for multishell display |
---|
60 | self._last_model_row = 0 |
---|
61 | # Dictionary of {model name: model class} for the current category |
---|
62 | self.models = {} |
---|
63 | |
---|
64 | # Which tab is this widget displayed in? |
---|
65 | self.tab_id = id |
---|
66 | |
---|
67 | # Range parameters |
---|
68 | self.q_range_min = QMIN_DEFAULT |
---|
69 | self.q_range_max = QMAX_DEFAULT |
---|
70 | self.npts = NPTS_DEFAULT |
---|
71 | |
---|
72 | # Main Data[12]D holder |
---|
73 | self.logic = FittingLogic(data=data) |
---|
74 | |
---|
75 | # Main GUI setup up |
---|
76 | self.setupUi(self) |
---|
77 | self.setWindowTitle("Fitting") |
---|
78 | self.communicate = self.parent.communicate |
---|
79 | |
---|
80 | # Set the main models |
---|
81 | # We can't use a single model here, due to restrictions on flattening |
---|
82 | # the model tree with subclassed QAbstractProxyModel... |
---|
83 | self._model_model = QtGui.QStandardItemModel() |
---|
84 | self._poly_model = QtGui.QStandardItemModel() |
---|
85 | self._magnet_model = QtGui.QStandardItemModel() |
---|
86 | |
---|
87 | # Param model displayed in param list |
---|
88 | self.lstParams.setModel(self._model_model) |
---|
89 | self.readCategoryInfo() |
---|
90 | self.model_parameters = None |
---|
91 | self.lstParams.setAlternatingRowColors(True) |
---|
92 | |
---|
93 | # Poly model displayed in poly list |
---|
94 | self.lstPoly.setModel(self._poly_model) |
---|
95 | self.setPolyModel() |
---|
96 | self.setTableProperties(self.lstPoly) |
---|
97 | |
---|
98 | # Magnetism model displayed in magnetism list |
---|
99 | self.lstMagnetic.setModel(self._magnet_model) |
---|
100 | self.setMagneticModel() |
---|
101 | self.setTableProperties(self.lstMagnetic) |
---|
102 | |
---|
103 | # Defaults for the structure factors |
---|
104 | self.setDefaultStructureCombo() |
---|
105 | |
---|
106 | # Make structure factor and model CBs disabled |
---|
107 | self.disableModelCombo() |
---|
108 | self.disableStructureCombo() |
---|
109 | |
---|
110 | # Generate the category list for display |
---|
111 | category_list = sorted(self.master_category_dict.keys()) |
---|
112 | self.cbCategory.addItem(CATEGORY_DEFAULT) |
---|
113 | self.cbCategory.addItems(category_list) |
---|
114 | self.cbCategory.addItem(CATEGORY_STRUCTURE) |
---|
115 | self.cbCategory.setCurrentIndex(0) |
---|
116 | |
---|
117 | self._index = data |
---|
118 | if data is not None: |
---|
119 | self.data = data |
---|
120 | |
---|
121 | # Connect signals to controls |
---|
122 | self.initializeSignals() |
---|
123 | |
---|
124 | # Initial control state |
---|
125 | self.initializeControls() |
---|
126 | |
---|
127 | @property |
---|
128 | def data(self): |
---|
129 | return self.logic.data |
---|
130 | |
---|
131 | @data.setter |
---|
132 | def data(self, value): |
---|
133 | """ data setter """ |
---|
134 | assert isinstance(value[0], QtGui.QStandardItem) |
---|
135 | # _index contains the QIndex with data |
---|
136 | self._index = value |
---|
137 | # Update logics with data items |
---|
138 | self.logic.data = GuiUtils.dataFromItem(value[0]) |
---|
139 | |
---|
140 | self.data_is_loaded = True |
---|
141 | # Tag along functionality |
---|
142 | self.updateQRange() |
---|
143 | self.cmdFit.setEnabled(True) |
---|
144 | |
---|
145 | def acceptsData(self): |
---|
146 | """ Tells the caller this widget can accept new dataset """ |
---|
147 | return not self.data_is_loaded |
---|
148 | |
---|
149 | def disableModelCombo(self): |
---|
150 | """ Disable the combobox """ |
---|
151 | self.cbModel.setEnabled(False) |
---|
152 | self.label_3.setEnabled(False) |
---|
153 | |
---|
154 | def enableModelCombo(self): |
---|
155 | """ Enable the combobox """ |
---|
156 | self.cbModel.setEnabled(True) |
---|
157 | self.label_3.setEnabled(True) |
---|
158 | |
---|
159 | def disableStructureCombo(self): |
---|
160 | """ Disable the combobox """ |
---|
161 | self.cbStructureFactor.setEnabled(False) |
---|
162 | self.label_4.setEnabled(False) |
---|
163 | |
---|
164 | def enableStructureCombo(self): |
---|
165 | """ Enable the combobox """ |
---|
166 | self.cbStructureFactor.setEnabled(True) |
---|
167 | self.label_4.setEnabled(True) |
---|
168 | |
---|
169 | def updateQRange(self): |
---|
170 | """ |
---|
171 | Updates Q Range display |
---|
172 | """ |
---|
173 | if self.data_is_loaded: |
---|
174 | self.q_range_min, self.q_range_max, self.npts = self.logic.computeDataRange() |
---|
175 | # set Q range labels on the main tab |
---|
176 | self.lblMinRangeDef.setText(str(self.q_range_min)) |
---|
177 | self.lblMaxRangeDef.setText(str(self.q_range_max)) |
---|
178 | # set Q range labels on the options tab |
---|
179 | self.txtMaxRange.setText(str(self.q_range_max)) |
---|
180 | self.txtMinRange.setText(str(self.q_range_min)) |
---|
181 | self.txtNpts.setText(str(self.npts)) |
---|
182 | |
---|
183 | def initializeControls(self): |
---|
184 | """ |
---|
185 | Set initial control enablement |
---|
186 | """ |
---|
187 | self.cmdFit.setEnabled(False) |
---|
188 | self.cmdPlot.setEnabled(True) |
---|
189 | self.chkPolydispersity.setEnabled(True) |
---|
190 | self.chkPolydispersity.setCheckState(False) |
---|
191 | self.chk2DView.setEnabled(True) |
---|
192 | self.chk2DView.setCheckState(False) |
---|
193 | self.chkMagnetism.setEnabled(False) |
---|
194 | self.chkMagnetism.setCheckState(False) |
---|
195 | # Tabs |
---|
196 | self.tabFitting.setTabEnabled(TAB_POLY, False) |
---|
197 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, False) |
---|
198 | self.lblChi2Value.setText("---") |
---|
199 | # Update Q Ranges |
---|
200 | self.updateQRange() |
---|
201 | |
---|
202 | def initializeSignals(self): |
---|
203 | """ |
---|
204 | Connect GUI element signals |
---|
205 | """ |
---|
206 | # Comboboxes |
---|
207 | self.cbStructureFactor.currentIndexChanged.connect(self.onSelectStructureFactor) |
---|
208 | self.cbCategory.currentIndexChanged.connect(self.onSelectCategory) |
---|
209 | self.cbModel.currentIndexChanged.connect(self.onSelectModel) |
---|
210 | # Checkboxes |
---|
211 | self.chk2DView.toggled.connect(self.toggle2D) |
---|
212 | self.chkPolydispersity.toggled.connect(self.togglePoly) |
---|
213 | self.chkMagnetism.toggled.connect(self.toggleMagnetism) |
---|
214 | # Buttons |
---|
215 | self.cmdFit.clicked.connect(self.onFit) |
---|
216 | self.cmdPlot.clicked.connect(self.onPlot) |
---|
217 | # Line edits |
---|
218 | self.txtNpts.textChanged.connect(self.onNpts) |
---|
219 | self.txtMinRange.textChanged.connect(self.onMinRange) |
---|
220 | self.txtMaxRange.textChanged.connect(self.onMaxRange) |
---|
221 | |
---|
222 | # Respond to change in parameters from the UI |
---|
223 | self._model_model.itemChanged.connect(self.updateParamsFromModel) |
---|
224 | self._poly_model.itemChanged.connect(self.onPolyModelChange) |
---|
225 | # TODO after the poly_model prototype accepted |
---|
226 | #self._magnet_model.itemChanged.connect(self.onMagneticModelChange) |
---|
227 | |
---|
228 | def setDefaultStructureCombo(self): |
---|
229 | """ |
---|
230 | Fill in the structure factors combo box with defaults |
---|
231 | """ |
---|
232 | structure_factor_list = self.master_category_dict.pop(CATEGORY_STRUCTURE) |
---|
233 | structure_factors = ["None"] |
---|
234 | self.cbStructureFactor.clear() |
---|
235 | structure_factors = [factor[0] for factor in structure_factor_list] |
---|
236 | self.cbStructureFactor.addItems(sorted(structure_factors)) |
---|
237 | |
---|
238 | def onSelectCategory(self): |
---|
239 | """ |
---|
240 | Select Category from list |
---|
241 | """ |
---|
242 | category = str(self.cbCategory.currentText()) |
---|
243 | # Check if the user chose "Choose category entry" |
---|
244 | if category == CATEGORY_DEFAULT: |
---|
245 | # if the previous category was not the default, keep it. |
---|
246 | # Otherwise, just return |
---|
247 | if self._previous_category_index != 0: |
---|
248 | self.cbCategory.setCurrentIndex(self._previous_category_index) |
---|
249 | return |
---|
250 | |
---|
251 | if category == CATEGORY_STRUCTURE: |
---|
252 | self.disableModelCombo() |
---|
253 | self.enableStructureCombo() |
---|
254 | return |
---|
255 | |
---|
256 | # Safely clear and enable the model combo |
---|
257 | self.cbModel.blockSignals(True) |
---|
258 | self.cbModel.clear() |
---|
259 | self.cbModel.blockSignals(False) |
---|
260 | self.enableModelCombo() |
---|
261 | self.disableStructureCombo() |
---|
262 | |
---|
263 | self._previous_category_index = self.cbCategory.currentIndex() |
---|
264 | # Retrieve the list of models |
---|
265 | model_list = self.master_category_dict[category] |
---|
266 | models = [] |
---|
267 | # Populate the models combobox |
---|
268 | self.cbModel.addItems(sorted([model for (model,_) in model_list])) |
---|
269 | |
---|
270 | def createDefaultDataset(self): |
---|
271 | """ |
---|
272 | Generate default Dataset 1D/2D for the given model |
---|
273 | """ |
---|
274 | # Create default datasets if no data passed |
---|
275 | if self.is2D: |
---|
276 | qmax = self.q_range_max/numpy.sqrt(2) |
---|
277 | qstep = self.npts |
---|
278 | self.logic.createDefault2dData(qmax, qstep, self.tab_id) |
---|
279 | else: |
---|
280 | interval = numpy.linspace(start=self.q_range_min, stop=self.q_range_max, |
---|
281 | num=self.npts, endpoint=True) |
---|
282 | self.logic.createDefault1dData(interval, self.tab_id) |
---|
283 | |
---|
284 | def onSelectModel(self): |
---|
285 | """ |
---|
286 | Respond to select Model from list event |
---|
287 | """ |
---|
288 | model = str(self.cbModel.currentText()) |
---|
289 | |
---|
290 | # SasModel -> QModel |
---|
291 | self.SASModelToQModel(model) |
---|
292 | |
---|
293 | if self._index is None: |
---|
294 | # Create default datasets if no data passed |
---|
295 | self.createDefaultDataset() |
---|
296 | else: |
---|
297 | self.calculateDataForModel() |
---|
298 | |
---|
299 | def onSelectStructureFactor(self): |
---|
300 | """ |
---|
301 | Select Structure Factor from list |
---|
302 | """ |
---|
303 | model = str(self.cbModel.currentText()) |
---|
304 | structure = str(self.cbStructureFactor.currentText()) |
---|
305 | self.SASModelToQModel(model, structure_factor=structure) |
---|
306 | |
---|
307 | def readCategoryInfo(self): |
---|
308 | """ |
---|
309 | Reads the categories in from file |
---|
310 | """ |
---|
311 | self.master_category_dict = defaultdict(list) |
---|
312 | self.by_model_dict = defaultdict(list) |
---|
313 | self.model_enabled_dict = defaultdict(bool) |
---|
314 | |
---|
315 | categorization_file = CategoryInstaller.get_user_file() |
---|
316 | if not os.path.isfile(categorization_file): |
---|
317 | categorization_file = CategoryInstaller.get_default_file() |
---|
318 | with open(categorization_file, 'rb') as cat_file: |
---|
319 | self.master_category_dict = json.load(cat_file) |
---|
320 | self.regenerateModelDict() |
---|
321 | |
---|
322 | # Load the model dict |
---|
323 | models = load_standard_models() |
---|
324 | for model in models: |
---|
325 | self.models[model.name] = model |
---|
326 | |
---|
327 | def regenerateModelDict(self): |
---|
328 | """ |
---|
329 | Regenerates self.by_model_dict which has each model name as the |
---|
330 | key and the list of categories belonging to that model |
---|
331 | along with the enabled mapping |
---|
332 | """ |
---|
333 | self.by_model_dict = defaultdict(list) |
---|
334 | for category in self.master_category_dict: |
---|
335 | for (model, enabled) in self.master_category_dict[category]: |
---|
336 | self.by_model_dict[model].append(category) |
---|
337 | self.model_enabled_dict[model] = enabled |
---|
338 | |
---|
339 | def addBackgroundToModel(self, model): |
---|
340 | """ |
---|
341 | Adds background parameter with default values to the model |
---|
342 | """ |
---|
343 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
344 | checked_list = ['background', '0.001', '-inf', 'inf', '1/cm'] |
---|
345 | FittingUtilities.addCheckedListToModel(model, checked_list) |
---|
346 | |
---|
347 | def addScaleToModel(self, model): |
---|
348 | """ |
---|
349 | Adds scale parameter with default values to the model |
---|
350 | """ |
---|
351 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
352 | checked_list = ['scale', '1.0', '0.0', 'inf', ''] |
---|
353 | FittingUtilities.addCheckedListToModel(model, checked_list) |
---|
354 | |
---|
355 | def SASModelToQModel(self, model_name, structure_factor=None): |
---|
356 | """ |
---|
357 | Setting model parameters into table based on selected category |
---|
358 | """ |
---|
359 | # Crete/overwrite model items |
---|
360 | self._model_model.clear() |
---|
361 | |
---|
362 | kernel_module = generate.load_kernel_module(model_name) |
---|
363 | self.model_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
364 | |
---|
365 | # Instantiate the current sasmodel |
---|
366 | self.kernel_module = self.models[model_name]() |
---|
367 | |
---|
368 | # Explicitly add scale and background with default values |
---|
369 | self.addScaleToModel(self._model_model) |
---|
370 | self.addBackgroundToModel(self._model_model) |
---|
371 | |
---|
372 | # Update the QModel |
---|
373 | FittingUtilities.addParametersToModel(self.model_parameters, self._model_model) |
---|
374 | # Update the counter used for multishell display |
---|
375 | self._last_model_row = self._model_model.rowCount() |
---|
376 | |
---|
377 | FittingUtilities.addHeadersToModel(self._model_model) |
---|
378 | |
---|
379 | # Add structure factor |
---|
380 | if structure_factor is not None and structure_factor != "None": |
---|
381 | structure_module = generate.load_kernel_module(structure_factor) |
---|
382 | structure_parameters = modelinfo.make_parameter_table(getattr(structure_module, 'parameters', [])) |
---|
383 | FittingUtilities.addSimpleParametersToModel(structure_parameters, self._model_model) |
---|
384 | # Update the counter used for multishell display |
---|
385 | self._last_model_row = self._model_model.rowCount() |
---|
386 | else: |
---|
387 | self.addStructureFactor() |
---|
388 | |
---|
389 | # Multishell models need additional treatment |
---|
390 | self.addExtraShells() |
---|
391 | |
---|
392 | # Add polydispersity to the model |
---|
393 | self.setPolyModel() |
---|
394 | # Add magnetic parameters to the model |
---|
395 | self.setMagneticModel() |
---|
396 | |
---|
397 | # Now we claim the model has been loaded |
---|
398 | self.model_is_loaded = True |
---|
399 | |
---|
400 | # Update Q Ranges |
---|
401 | self.updateQRange() |
---|
402 | |
---|
403 | def onPolyModelChange(self, item): |
---|
404 | """ |
---|
405 | Callback method for updating the main model and sasmodel |
---|
406 | parameters with the GUI values in the polydispersity view |
---|
407 | """ |
---|
408 | model_column = item.column() |
---|
409 | model_row = item.row() |
---|
410 | name_index = self._poly_model.index(model_row, 0) |
---|
411 | # Extract changed value. Assumes proper validation by QValidator/Delegate |
---|
412 | value = float(item.text()) |
---|
413 | parameter_name = str(self._poly_model.data(name_index).toPyObject()) # "distribution of sld" etc. |
---|
414 | if "Distribution of" in parameter_name: |
---|
415 | parameter_name = parameter_name[16:] |
---|
416 | property_name = str(self._poly_model.headerData(model_column, 1).toPyObject()) # Value, min, max, etc. |
---|
417 | print "%s(%s) => %d" % (parameter_name, property_name, value) |
---|
418 | |
---|
419 | # Update the sasmodel |
---|
420 | #self.kernel_module.params[parameter_name] = value |
---|
421 | |
---|
422 | # Reload the main model - may not be required if no variable is shown in main view |
---|
423 | #model = str(self.cbModel.currentText()) |
---|
424 | #self.SASModelToQModel(model) |
---|
425 | |
---|
426 | pass # debug anchor |
---|
427 | |
---|
428 | def updateParamsFromModel(self, item): |
---|
429 | """ |
---|
430 | Callback method for updating the sasmodel parameters with the GUI values |
---|
431 | """ |
---|
432 | model_column = item.column() |
---|
433 | model_row = item.row() |
---|
434 | name_index = self._model_model.index(model_row, 0) |
---|
435 | |
---|
436 | if model_column == 0: |
---|
437 | # Assure we're dealing with checkboxes |
---|
438 | if not item.isCheckable(): |
---|
439 | return |
---|
440 | status = item.checkState() |
---|
441 | # If multiple rows selected - toggle all of them |
---|
442 | rows = [s.row() for s in self.lstParams.selectionModel().selectedRows()] |
---|
443 | |
---|
444 | # Switch off signaling from the model to avoid multiple calls |
---|
445 | self._model_model.blockSignals(True) |
---|
446 | # Convert to proper indices and set requested enablement |
---|
447 | items = [self._model_model.item(row, 0).setCheckState(status) for row in rows] |
---|
448 | self._model_model.blockSignals(False) |
---|
449 | return |
---|
450 | |
---|
451 | # Extract changed value. Assumes proper validation by QValidator/Delegate |
---|
452 | value = float(item.text()) |
---|
453 | parameter_name = str(self._model_model.data(name_index).toPyObject()) # sld, background etc. |
---|
454 | property_name = str(self._model_model.headerData(1, model_column).toPyObject()) # Value, min, max, etc. |
---|
455 | |
---|
456 | print "%s(%s) => %d" % (parameter_name, property_name, value) |
---|
457 | self.kernel_module.params[parameter_name] = value |
---|
458 | |
---|
459 | # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf] |
---|
460 | |
---|
461 | # magnetic params in self.kernel_module.details['M0:parameter_name'] = value |
---|
462 | # multishell params in self.kernel_module.details[??] = value |
---|
463 | |
---|
464 | |
---|
465 | def createTheoryIndex(self): |
---|
466 | """ |
---|
467 | Create a QStandardModelIndex containing default model data |
---|
468 | """ |
---|
469 | name = self.kernel_module.name |
---|
470 | if self.is2D: |
---|
471 | name += "2d" |
---|
472 | name = "M%i [%s]" % (self.tab_id, name) |
---|
473 | new_item = GuiUtils.createModelItemWithPlot(QtCore.QVariant(self.data), name=name) |
---|
474 | # Notify the GUI manager so it can update the theory model in DataExplorer |
---|
475 | self.communicate.updateTheoryFromPerspectiveSignal.emit(new_item) |
---|
476 | |
---|
477 | def onFit(self): |
---|
478 | """ |
---|
479 | Perform fitting on the current data |
---|
480 | """ |
---|
481 | #self.calculate1DForModel() |
---|
482 | pass |
---|
483 | |
---|
484 | def onPlot(self): |
---|
485 | """ |
---|
486 | Plot the current set of data |
---|
487 | """ |
---|
488 | # TODO: reimplement basepage.py/_update_paramv_on_fit |
---|
489 | if self.data is None or not self.data.is_data: |
---|
490 | self.createDefaultDataset() |
---|
491 | self.calculateDataForModel() |
---|
492 | |
---|
493 | def onNpts(self, text): |
---|
494 | """ |
---|
495 | Callback for number of points line edit update |
---|
496 | """ |
---|
497 | # assumes type/value correctness achieved with QValidator |
---|
498 | try: |
---|
499 | self.npts = int(text) |
---|
500 | except: |
---|
501 | pass |
---|
502 | |
---|
503 | def onMinRange(self, text): |
---|
504 | """ |
---|
505 | Callback for minimum range of points line edit update |
---|
506 | """ |
---|
507 | # assumes type/value correctness achieved with QValidator |
---|
508 | try: |
---|
509 | self.q_range_min = float(text) |
---|
510 | except: |
---|
511 | pass |
---|
512 | # set Q range labels on the main tab |
---|
513 | self.lblMinRangeDef.setText(str(self.q_range_min)) |
---|
514 | |
---|
515 | def onMaxRange(self, text): |
---|
516 | """ |
---|
517 | Callback for maximum range of points line edit update |
---|
518 | """ |
---|
519 | # assumes type/value correctness achieved with QValidator |
---|
520 | try: |
---|
521 | self.q_range_max = float(text) |
---|
522 | except: |
---|
523 | pass |
---|
524 | # set Q range labels on the main tab |
---|
525 | self.lblMaxRangeDef.setText(str(self.q_range_max)) |
---|
526 | |
---|
527 | def methodCalculateForData(self): |
---|
528 | '''return the method for data calculation''' |
---|
529 | return Calc1D if isinstance(self.data, Data1D) else Calc2D |
---|
530 | |
---|
531 | def methodCompleteForData(self): |
---|
532 | '''return the method for result parsin on calc complete ''' |
---|
533 | return self.complete1D if isinstance(self.data, Data1D) else self.complete2D |
---|
534 | |
---|
535 | def calculateDataForModel(self): |
---|
536 | """ |
---|
537 | Prepare the fitting data object, based on current ModelModel |
---|
538 | """ |
---|
539 | # Awful API to a backend method. |
---|
540 | method = self.methodCalculateForData()(data=self.data, |
---|
541 | model=self.kernel_module, |
---|
542 | page_id=0, |
---|
543 | qmin=self.q_range_min, |
---|
544 | qmax=self.q_range_max, |
---|
545 | smearer=None, |
---|
546 | state=None, |
---|
547 | weight=None, |
---|
548 | fid=None, |
---|
549 | toggle_mode_on=False, |
---|
550 | completefn=None, |
---|
551 | update_chisqr=True, |
---|
552 | exception_handler=self.calcException, |
---|
553 | source=None) |
---|
554 | |
---|
555 | calc_thread = threads.deferToThread(method.compute) |
---|
556 | calc_thread.addCallback(self.methodCompleteForData()) |
---|
557 | |
---|
558 | def complete1D(self, return_data): |
---|
559 | """ |
---|
560 | Plot the current 1D data |
---|
561 | """ |
---|
562 | self.logic.new1DPlot(return_data) |
---|
563 | self.createTheoryIndex() |
---|
564 | |
---|
565 | #output=self._cal_chisqr(data=data, |
---|
566 | # fid=fid, |
---|
567 | # weight=weight, |
---|
568 | # page_id=page_id, |
---|
569 | # index=index) |
---|
570 | |
---|
571 | def complete2D(self, return_data): |
---|
572 | """ |
---|
573 | Plot the current 2D data |
---|
574 | """ |
---|
575 | self.logic.new2DPlot(return_data) |
---|
576 | self.createTheoryIndex() |
---|
577 | |
---|
578 | #output=self._cal_chisqr(data=data, |
---|
579 | # weight=weight, |
---|
580 | # fid=fid, |
---|
581 | # page_id=page_id, |
---|
582 | # index=index) |
---|
583 | # self._plot_residuals(page_id=page_id, data=data, fid=fid, |
---|
584 | # index=index, weight=weight) |
---|
585 | |
---|
586 | def calcException(self, etype, value, tb): |
---|
587 | """ |
---|
588 | Something horrible happened in the deferred. Cry me a river. |
---|
589 | """ |
---|
590 | logging.error("".join(traceback.format_exception(etype, value, tb))) |
---|
591 | msg = traceback.format_exception(etype, value, tb, limit=1) |
---|
592 | |
---|
593 | def setTableProperties(self, table): |
---|
594 | """ |
---|
595 | Setting table properties |
---|
596 | """ |
---|
597 | # Table properties |
---|
598 | table.verticalHeader().setVisible(False) |
---|
599 | table.setAlternatingRowColors(True) |
---|
600 | table.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Expanding) |
---|
601 | table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) |
---|
602 | table.resizeColumnsToContents() |
---|
603 | |
---|
604 | # Header |
---|
605 | header = table.horizontalHeader() |
---|
606 | header.setResizeMode(QtGui.QHeaderView.ResizeToContents) |
---|
607 | |
---|
608 | header.ResizeMode(QtGui.QHeaderView.Interactive) |
---|
609 | header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents) |
---|
610 | header.setResizeMode(6, QtGui.QHeaderView.ResizeToContents) |
---|
611 | |
---|
612 | def setPolyModel(self): |
---|
613 | """ |
---|
614 | Set polydispersity values |
---|
615 | """ |
---|
616 | if not self.model_parameters: |
---|
617 | return |
---|
618 | self._poly_model.clear() |
---|
619 | for row, param in enumerate(self.model_parameters.form_volume_parameters): |
---|
620 | # Counters should not be included |
---|
621 | if not param.polydisperse: |
---|
622 | continue |
---|
623 | |
---|
624 | # Potential multishell params |
---|
625 | checked_list = ["Distribution of "+param.name, str(param.default), |
---|
626 | str(param.limits[0]), str(param.limits[1]), |
---|
627 | "35", "3", ""] |
---|
628 | FittingUtilities.addCheckedListToModel(self._poly_model, checked_list) |
---|
629 | |
---|
630 | #TODO: Need to find cleaner way to input functions |
---|
631 | func = QtGui.QComboBox() |
---|
632 | func.addItems(['rectangle', 'array', 'lognormal', 'gaussian', 'schulz',]) |
---|
633 | func_index = self.lstPoly.model().index(row, 6) |
---|
634 | self.lstPoly.setIndexWidget(func_index, func) |
---|
635 | |
---|
636 | FittingUtilities.addPolyHeadersToModel(self._poly_model) |
---|
637 | |
---|
638 | def setMagneticModel(self): |
---|
639 | """ |
---|
640 | Set magnetism values on model |
---|
641 | """ |
---|
642 | if not self.model_parameters: |
---|
643 | return |
---|
644 | self._magnet_model.clear() |
---|
645 | for param in self.model_parameters.call_parameters: |
---|
646 | if param.type != "magnetic": |
---|
647 | continue |
---|
648 | checked_list = [param.name, |
---|
649 | str(param.default), |
---|
650 | str(param.limits[0]), |
---|
651 | str(param.limits[1]), |
---|
652 | param.units] |
---|
653 | FittingUtilities.addCheckedListToModel(self._magnet_model, checked_list) |
---|
654 | |
---|
655 | FittingUtilities.addHeadersToModel(self._magnet_model) |
---|
656 | |
---|
657 | def addStructureFactor(self): |
---|
658 | """ |
---|
659 | Add structure factors to the list of parameters |
---|
660 | """ |
---|
661 | if self.kernel_module.is_form_factor: |
---|
662 | self.enableStructureCombo() |
---|
663 | else: |
---|
664 | self.disableStructureCombo() |
---|
665 | |
---|
666 | def addExtraShells(self): |
---|
667 | """ |
---|
668 | Add a combobox for multiple shell display |
---|
669 | """ |
---|
670 | param_name, param_length = FittingUtilities.getMultiplicity(self.model_parameters) |
---|
671 | |
---|
672 | if param_length == 0: |
---|
673 | return |
---|
674 | |
---|
675 | # cell 1: variable name |
---|
676 | item1 = QtGui.QStandardItem(param_name) |
---|
677 | |
---|
678 | func = QtGui.QComboBox() |
---|
679 | func.addItems([str(i+1) for i in xrange(param_length)]) |
---|
680 | func.currentIndexChanged.connect(self.modifyShellsInList) |
---|
681 | |
---|
682 | # cell 2: combobox |
---|
683 | item2 = QtGui.QStandardItem() |
---|
684 | self._model_model.appendRow([item1, item2]) |
---|
685 | |
---|
686 | # Beautify the row: span columns 2-4 |
---|
687 | shell_row = self._model_model.rowCount() |
---|
688 | shell_index = self._model_model.index(shell_row-1, 1) |
---|
689 | |
---|
690 | self.lstParams.setIndexWidget(shell_index, func) |
---|
691 | self._last_model_row = self._model_model.rowCount() |
---|
692 | |
---|
693 | |
---|
694 | def modifyShellsInList(self, index): |
---|
695 | """ |
---|
696 | Add/remove additional multishell parameters |
---|
697 | """ |
---|
698 | # Find row location of the combobox |
---|
699 | last_row = self._last_model_row |
---|
700 | remove_rows = self._model_model.rowCount() - last_row |
---|
701 | |
---|
702 | if remove_rows > 1: |
---|
703 | self._model_model.removeRows(last_row, remove_rows) |
---|
704 | |
---|
705 | FittingUtilities.addShellsToModel(self.model_parameters, self._model_model, index) |
---|
706 | |
---|
707 | def togglePoly(self, isChecked): |
---|
708 | """ |
---|
709 | Enable/disable the polydispersity tab |
---|
710 | """ |
---|
711 | self.tabFitting.setTabEnabled(TAB_POLY, isChecked) |
---|
712 | |
---|
713 | def toggleMagnetism(self, isChecked): |
---|
714 | """ |
---|
715 | Enable/disable the magnetism tab |
---|
716 | """ |
---|
717 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, isChecked) |
---|
718 | |
---|
719 | def toggle2D(self, isChecked): |
---|
720 | """ |
---|
721 | Enable/disable the controls dependent on 1D/2D data instance |
---|
722 | """ |
---|
723 | self.chkMagnetism.setEnabled(isChecked) |
---|
724 | self.is2D = isChecked |
---|
725 | |
---|