source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingWidget.py @ 8e2cd79

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 8e2cd79 was 8e2cd79, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Copy/paste fitting parameters SASVIEW-933

  • Property mode set to 100644
File size: 114.6 KB
Line 
1import json
2import os
3from collections import defaultdict
4
5import copy
6import logging
7import traceback
8from twisted.internet import threads
9import numpy as np
10import webbrowser
11
12from PyQt5 import QtCore
13from PyQt5 import QtGui
14from PyQt5 import QtWidgets
15
16from sasmodels import product
17from sasmodels import generate
18from sasmodels import modelinfo
19from sasmodels.sasview_model import load_standard_models
20from sasmodels.weights import MODELS as POLYDISPERSITY_MODELS
21
22from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit
23from sas.sascalc.fit.pagestate import PageState
24
25import sas.qtgui.Utilities.GuiUtils as GuiUtils
26import sas.qtgui.Utilities.LocalConfig as LocalConfig
27from sas.qtgui.Utilities.CategoryInstaller import CategoryInstaller
28from sas.qtgui.Plotting.PlotterData import Data1D
29from sas.qtgui.Plotting.PlotterData import Data2D
30
31from sas.qtgui.Perspectives.Fitting.UI.FittingWidgetUI import Ui_FittingWidgetUI
32from sas.qtgui.Perspectives.Fitting.FitThread import FitThread
33from sas.qtgui.Perspectives.Fitting.ConsoleUpdate import ConsoleUpdate
34
35from sas.qtgui.Perspectives.Fitting.ModelThread import Calc1D
36from sas.qtgui.Perspectives.Fitting.ModelThread import Calc2D
37from sas.qtgui.Perspectives.Fitting.FittingLogic import FittingLogic
38from sas.qtgui.Perspectives.Fitting import FittingUtilities
39from sas.qtgui.Perspectives.Fitting import ModelUtilities
40from sas.qtgui.Perspectives.Fitting.SmearingWidget import SmearingWidget
41from sas.qtgui.Perspectives.Fitting.OptionsWidget import OptionsWidget
42from sas.qtgui.Perspectives.Fitting.FitPage import FitPage
43from sas.qtgui.Perspectives.Fitting.ViewDelegate import ModelViewDelegate
44from sas.qtgui.Perspectives.Fitting.ViewDelegate import PolyViewDelegate
45from sas.qtgui.Perspectives.Fitting.ViewDelegate import MagnetismViewDelegate
46from sas.qtgui.Perspectives.Fitting.Constraint import Constraint
47from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint
48from sas.qtgui.Perspectives.Fitting.ReportPageLogic import ReportPageLogic
49
50
51
52TAB_MAGNETISM = 4
53TAB_POLY = 3
54CATEGORY_DEFAULT = "Choose category..."
55CATEGORY_STRUCTURE = "Structure Factor"
56CATEGORY_CUSTOM = "Plugin Models"
57STRUCTURE_DEFAULT = "None"
58
59DEFAULT_POLYDISP_FUNCTION = 'gaussian'
60
61
62logger = logging.getLogger(__name__)
63
64
65class ToolTippedItemModel(QtGui.QStandardItemModel):
66    """
67    Subclass from QStandardItemModel to allow displaying tooltips in
68    QTableView model.
69    """
70    def __init__(self, parent=None):
71        QtGui.QStandardItemModel.__init__(self, parent)
72
73    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
74        """
75        Displays tooltip for each column's header
76        :param section:
77        :param orientation:
78        :param role:
79        :return:
80        """
81        if role == QtCore.Qt.ToolTipRole:
82            if orientation == QtCore.Qt.Horizontal:
83                return str(self.header_tooltips[section])
84
85        return QtGui.QStandardItemModel.headerData(self, section, orientation, role)
86
87class FittingWidget(QtWidgets.QWidget, Ui_FittingWidgetUI):
88    """
89    Main widget for selecting form and structure factor models
90    """
91    constraintAddedSignal = QtCore.pyqtSignal(list)
92    newModelSignal = QtCore.pyqtSignal()
93    fittingFinishedSignal = QtCore.pyqtSignal(tuple)
94    batchFittingFinishedSignal = QtCore.pyqtSignal(tuple)
95    Calc1DFinishedSignal = QtCore.pyqtSignal(tuple)
96    Calc2DFinishedSignal = QtCore.pyqtSignal(tuple)
97
98    def __init__(self, parent=None, data=None, tab_id=1):
99
100        super(FittingWidget, self).__init__()
101
102        # Necessary globals
103        self.parent = parent
104
105        # Which tab is this widget displayed in?
106        self.tab_id = tab_id
107
108        # Globals
109        self.initializeGlobals()
110
111        # data index for the batch set
112        self.data_index = 0
113        # Main Data[12]D holders
114        # Logics.data contains a single Data1D/Data2D object
115        self._logic = [FittingLogic()]
116
117        # Main GUI setup up
118        self.setupUi(self)
119        self.setWindowTitle("Fitting")
120
121        # Set up tabs widgets
122        self.initializeWidgets()
123
124        # Set up models and views
125        self.initializeModels()
126
127        # Defaults for the structure factors
128        self.setDefaultStructureCombo()
129
130        # Make structure factor and model CBs disabled
131        self.disableModelCombo()
132        self.disableStructureCombo()
133
134        # Generate the category list for display
135        self.initializeCategoryCombo()
136
137        # Connect signals to controls
138        self.initializeSignals()
139
140        # Initial control state
141        self.initializeControls()
142
143        if data is not None:
144            self.data = data
145
146        # New font to display angstrom symbol
147        new_font = 'font-family: -apple-system, "Helvetica Neue", "Ubuntu";'
148        self.label_17.setStyleSheet(new_font)
149        self.label_19.setStyleSheet(new_font)
150
151    @property
152    def logic(self):
153        # make sure the logic contains at least one element
154        assert self._logic
155        # logic connected to the currently shown data
156        return self._logic[self.data_index]
157
158    @property
159    def data(self):
160        return self.logic.data
161
162    @data.setter
163    def data(self, value):
164        """ data setter """
165        # Value is either a list of indices for batch fitting or a simple index
166        # for standard fitting. Assure we have a list, regardless.
167        if isinstance(value, list):
168            self.is_batch_fitting = True
169        else:
170            value = [value]
171
172        assert isinstance(value[0], QtGui.QStandardItem)
173
174        # Keep reference to all datasets for batch
175        self.all_data = value
176
177        # Create logics with data items
178        # Logics.data contains only a single Data1D/Data2D object
179        if len(value) == 1:
180            # single data logic is already defined, update data on it
181            self._logic[0].data = GuiUtils.dataFromItem(value[0])
182        else:
183            # batch datasets
184            for data_item in value:
185                logic = FittingLogic(data=GuiUtils.dataFromItem(data_item))
186                self._logic.append(logic)
187
188        # Overwrite data type descriptor
189        self.is2D = True if isinstance(self.logic.data, Data2D) else False
190
191        # Let others know we're full of data now
192        self.data_is_loaded = True
193
194        # Enable/disable UI components
195        self.setEnablementOnDataLoad()
196
197    def initializeGlobals(self):
198        """
199        Initialize global variables used in this class
200        """
201        # SasModel is loaded
202        self.model_is_loaded = False
203        # Data[12]D passed and set
204        self.data_is_loaded = False
205        # Batch/single fitting
206        self.is_batch_fitting = False
207        self.is_chain_fitting = False
208        # Is the fit job running?
209        self.fit_started = False
210        # The current fit thread
211        self.calc_fit = None
212        # Current SasModel in view
213        self.kernel_module = None
214        # Current SasModel view dimension
215        self.is2D = False
216        # Current SasModel is multishell
217        self.model_has_shells = False
218        # Utility variable to enable unselectable option in category combobox
219        self._previous_category_index = 0
220        # Utility variable for multishell display
221        self._last_model_row = 0
222        # Dictionary of {model name: model class} for the current category
223        self.models = {}
224        # Parameters to fit
225        self.parameters_to_fit = None
226        # Fit options
227        self.q_range_min = 0.005
228        self.q_range_max = 0.1
229        self.npts = 25
230        self.log_points = False
231        self.weighting = 0
232        self.chi2 = None
233        # Does the control support UNDO/REDO
234        # temporarily off
235        self.undo_supported = False
236        self.page_stack = []
237        self.all_data = []
238        # custom plugin models
239        # {model.name:model}
240        self.custom_models = self.customModels()
241        # Polydisp widget table default index for function combobox
242        self.orig_poly_index = 3
243        # copy of current kernel model
244        self.kernel_module_copy = None
245
246        # Page id for fitting
247        # To keep with previous SasView values, use 200 as the start offset
248        self.page_id = 200 + self.tab_id
249
250        # Data for chosen model
251        self.model_data = None
252
253        # Which shell is being currently displayed?
254        self.current_shell_displayed = 0
255        # List of all shell-unique parameters
256        self.shell_names = []
257
258        # Error column presence in parameter display
259        self.has_error_column = False
260        self.has_poly_error_column = False
261        self.has_magnet_error_column = False
262
263        # signal communicator
264        self.communicate = self.parent.communicate
265
266    def initializeWidgets(self):
267        """
268        Initialize widgets for tabs
269        """
270        # Options widget
271        layout = QtWidgets.QGridLayout()
272        self.options_widget = OptionsWidget(self, self.logic)
273        layout.addWidget(self.options_widget)
274        self.tabOptions.setLayout(layout)
275
276        # Smearing widget
277        layout = QtWidgets.QGridLayout()
278        self.smearing_widget = SmearingWidget(self)
279        layout.addWidget(self.smearing_widget)
280        self.tabResolution.setLayout(layout)
281
282        # Define bold font for use in various controls
283        self.boldFont = QtGui.QFont()
284        self.boldFont.setBold(True)
285
286        # Set data label
287        self.label.setFont(self.boldFont)
288        self.label.setText("No data loaded")
289        self.lblFilename.setText("")
290
291        # Magnetic angles explained in one picture
292        self.magneticAnglesWidget = QtWidgets.QWidget()
293        labl = QtWidgets.QLabel(self.magneticAnglesWidget)
294        pixmap = QtGui.QPixmap(GuiUtils.IMAGES_DIRECTORY_LOCATION + '/M_angles_pic.bmp')
295        labl.setPixmap(pixmap)
296        self.magneticAnglesWidget.setFixedSize(pixmap.width(), pixmap.height())
297
298    def initializeModels(self):
299        """
300        Set up models and views
301        """
302        # Set the main models
303        # We can't use a single model here, due to restrictions on flattening
304        # the model tree with subclassed QAbstractProxyModel...
305        self._model_model = ToolTippedItemModel()
306        self._poly_model = ToolTippedItemModel()
307        self._magnet_model = ToolTippedItemModel()
308
309        # Param model displayed in param list
310        self.lstParams.setModel(self._model_model)
311        self.readCategoryInfo()
312
313        self.model_parameters = None
314
315        # Delegates for custom editing and display
316        self.lstParams.setItemDelegate(ModelViewDelegate(self))
317
318        self.lstParams.setAlternatingRowColors(True)
319        stylesheet = """
320
321            QTreeView {
322                paint-alternating-row-colors-for-empty-area:0;
323            }
324
325            QTreeView::item:hover {
326                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
327                border: 1px solid #bfcde4;
328            }
329
330            QTreeView::item:selected {
331                border: 1px solid #567dbc;
332            }
333
334            QTreeView::item:selected:active{
335                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
336            }
337
338            QTreeView::item:selected:!active {
339                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
340            }
341           """
342        self.lstParams.setStyleSheet(stylesheet)
343        self.lstParams.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
344        self.lstParams.customContextMenuRequested.connect(self.showModelContextMenu)
345        self.lstParams.setAttribute(QtCore.Qt.WA_MacShowFocusRect, False)
346        # Poly model displayed in poly list
347        self.lstPoly.setModel(self._poly_model)
348        self.setPolyModel()
349        self.setTableProperties(self.lstPoly)
350        # Delegates for custom editing and display
351        self.lstPoly.setItemDelegate(PolyViewDelegate(self))
352        # Polydispersity function combo response
353        self.lstPoly.itemDelegate().combo_updated.connect(self.onPolyComboIndexChange)
354        self.lstPoly.itemDelegate().filename_updated.connect(self.onPolyFilenameChange)
355
356        # Magnetism model displayed in magnetism list
357        self.lstMagnetic.setModel(self._magnet_model)
358        self.setMagneticModel()
359        self.setTableProperties(self.lstMagnetic)
360        # Delegates for custom editing and display
361        self.lstMagnetic.setItemDelegate(MagnetismViewDelegate(self))
362
363    def initializeCategoryCombo(self):
364        """
365        Model category combo setup
366        """
367        category_list = sorted(self.master_category_dict.keys())
368        self.cbCategory.addItem(CATEGORY_DEFAULT)
369        self.cbCategory.addItems(category_list)
370        self.cbCategory.addItem(CATEGORY_STRUCTURE)
371        self.cbCategory.setCurrentIndex(0)
372
373    def setEnablementOnDataLoad(self):
374        """
375        Enable/disable various UI elements based on data loaded
376        """
377        # Tag along functionality
378        self.label.setText("Data loaded from: ")
379        self.lblFilename.setText(self.logic.data.filename)
380        self.updateQRange()
381        # Switch off Data2D control
382        self.chk2DView.setEnabled(False)
383        self.chk2DView.setVisible(False)
384        self.chkMagnetism.setEnabled(self.is2D)
385        self.tabFitting.setTabEnabled(TAB_MAGNETISM, self.chkMagnetism.isChecked())
386        # Combo box or label for file name"
387        if self.is_batch_fitting:
388            self.lblFilename.setVisible(False)
389            for dataitem in self.all_data:
390                filename = GuiUtils.dataFromItem(dataitem).filename
391                self.cbFileNames.addItem(filename)
392            self.cbFileNames.setVisible(True)
393            self.chkChainFit.setEnabled(True)
394            self.chkChainFit.setVisible(True)
395            # This panel is not designed to view individual fits, so disable plotting
396            self.cmdPlot.setVisible(False)
397        # Similarly on other tabs
398        self.options_widget.setEnablementOnDataLoad()
399        self.onSelectModel()
400        # Smearing tab
401        self.smearing_widget.updateData(self.data)
402
403    def acceptsData(self):
404        """ Tells the caller this widget can accept new dataset """
405        return not self.data_is_loaded
406
407    def disableModelCombo(self):
408        """ Disable the combobox """
409        self.cbModel.setEnabled(False)
410        self.lblModel.setEnabled(False)
411
412    def enableModelCombo(self):
413        """ Enable the combobox """
414        self.cbModel.setEnabled(True)
415        self.lblModel.setEnabled(True)
416
417    def disableStructureCombo(self):
418        """ Disable the combobox """
419        self.cbStructureFactor.setEnabled(False)
420        self.lblStructure.setEnabled(False)
421
422    def enableStructureCombo(self):
423        """ Enable the combobox """
424        self.cbStructureFactor.setEnabled(True)
425        self.lblStructure.setEnabled(True)
426
427    def togglePoly(self, isChecked):
428        """ Enable/disable the polydispersity tab """
429        self.tabFitting.setTabEnabled(TAB_POLY, isChecked)
430
431    def toggleMagnetism(self, isChecked):
432        """ Enable/disable the magnetism tab """
433        self.tabFitting.setTabEnabled(TAB_MAGNETISM, isChecked)
434
435    def toggleChainFit(self, isChecked):
436        """ Enable/disable chain fitting """
437        self.is_chain_fitting = isChecked
438
439    def toggle2D(self, isChecked):
440        """ Enable/disable the controls dependent on 1D/2D data instance """
441        self.chkMagnetism.setEnabled(isChecked)
442        self.is2D = isChecked
443        # Reload the current model
444        if self.kernel_module:
445            self.onSelectModel()
446
447    @classmethod
448    def customModels(cls):
449        """ Reads in file names in the custom plugin directory """
450        return ModelUtilities._find_models()
451
452    def initializeControls(self):
453        """
454        Set initial control enablement
455        """
456        self.cbFileNames.setVisible(False)
457        self.cmdFit.setEnabled(False)
458        self.cmdPlot.setEnabled(False)
459        self.options_widget.cmdComputePoints.setVisible(False) # probably redundant
460        self.chkPolydispersity.setEnabled(True)
461        self.chkPolydispersity.setCheckState(False)
462        self.chk2DView.setEnabled(True)
463        self.chk2DView.setCheckState(False)
464        self.chkMagnetism.setEnabled(False)
465        self.chkMagnetism.setCheckState(False)
466        self.chkChainFit.setEnabled(False)
467        self.chkChainFit.setVisible(False)
468        # Tabs
469        self.tabFitting.setTabEnabled(TAB_POLY, False)
470        self.tabFitting.setTabEnabled(TAB_MAGNETISM, False)
471        self.lblChi2Value.setText("---")
472        # Smearing tab
473        self.smearing_widget.updateData(self.data)
474        # Line edits in the option tab
475        self.updateQRange()
476
477    def initializeSignals(self):
478        """
479        Connect GUI element signals
480        """
481        # Comboboxes
482        self.cbStructureFactor.currentIndexChanged.connect(self.onSelectStructureFactor)
483        self.cbCategory.currentIndexChanged.connect(self.onSelectCategory)
484        self.cbModel.currentIndexChanged.connect(self.onSelectModel)
485        self.cbFileNames.currentIndexChanged.connect(self.onSelectBatchFilename)
486        # Checkboxes
487        self.chk2DView.toggled.connect(self.toggle2D)
488        self.chkPolydispersity.toggled.connect(self.togglePoly)
489        self.chkMagnetism.toggled.connect(self.toggleMagnetism)
490        self.chkChainFit.toggled.connect(self.toggleChainFit)
491        # Buttons
492        self.cmdFit.clicked.connect(self.onFit)
493        self.cmdPlot.clicked.connect(self.onPlot)
494        self.cmdHelp.clicked.connect(self.onHelp)
495        self.cmdMagneticDisplay.clicked.connect(self.onDisplayMagneticAngles)
496
497        # Respond to change in parameters from the UI
498        self._model_model.itemChanged.connect(self.onMainParamsChange)
499        #self.constraintAddedSignal.connect(self.modifyViewOnConstraint)
500        self._poly_model.itemChanged.connect(self.onPolyModelChange)
501        self._magnet_model.itemChanged.connect(self.onMagnetModelChange)
502        self.lstParams.selectionModel().selectionChanged.connect(self.onSelectionChanged)
503
504        # Local signals
505        self.batchFittingFinishedSignal.connect(self.batchFitComplete)
506        self.fittingFinishedSignal.connect(self.fitComplete)
507        self.Calc1DFinishedSignal.connect(self.complete1D)
508        self.Calc2DFinishedSignal.connect(self.complete2D)
509
510        # Signals from separate tabs asking for replot
511        self.options_widget.plot_signal.connect(self.onOptionsUpdate)
512        self.options_widget.plot_signal.connect(self.onOptionsUpdate)
513
514        # Signals from other widgets
515        self.communicate.customModelDirectoryChanged.connect(self.onCustomModelChange)
516        self.communicate.saveAnalysisSignal.connect(self.savePageState)
517        self.smearing_widget.smearingChangedSignal.connect(self.onSmearingOptionsUpdate)
518        self.communicate.copyFitParamsSignal.connect(self.onParameterCopy)
519        self.communicate.pasteFitParamsSignal.connect(self.onParameterPaste)
520
521    def modelName(self):
522        """
523        Returns model name, by default M<tab#>, e.g. M1, M2
524        """
525        return "M%i" % self.tab_id
526
527    def nameForFittedData(self, name):
528        """
529        Generate name for the current fit
530        """
531        if self.is2D:
532            name += "2d"
533        name = "%s [%s]" % (self.modelName(), name)
534        return name
535
536    def showModelContextMenu(self, position):
537        """
538        Show context specific menu in the parameter table.
539        When clicked on parameter(s): fitting/constraints options
540        When clicked on white space: model description
541        """
542        rows = [s.row() for s in self.lstParams.selectionModel().selectedRows()]
543        menu = self.showModelDescription() if not rows else self.modelContextMenu(rows)
544        try:
545            menu.exec_(self.lstParams.viewport().mapToGlobal(position))
546        except AttributeError as ex:
547            logging.error("Error generating context menu: %s" % ex)
548        return
549
550    def modelContextMenu(self, rows):
551        """
552        Create context menu for the parameter selection
553        """
554        menu = QtWidgets.QMenu()
555        num_rows = len(rows)
556        if num_rows < 1:
557            return menu
558        # Select for fitting
559        param_string = "parameter " if num_rows == 1 else "parameters "
560        to_string = "to its current value" if num_rows == 1 else "to their current values"
561        has_constraints = any([self.rowHasConstraint(i) for i in rows])
562
563        self.actionSelect = QtWidgets.QAction(self)
564        self.actionSelect.setObjectName("actionSelect")
565        self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting"))
566        # Unselect from fitting
567        self.actionDeselect = QtWidgets.QAction(self)
568        self.actionDeselect.setObjectName("actionDeselect")
569        self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting"))
570
571        self.actionConstrain = QtWidgets.QAction(self)
572        self.actionConstrain.setObjectName("actionConstrain")
573        self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain "+param_string + to_string))
574
575        self.actionRemoveConstraint = QtWidgets.QAction(self)
576        self.actionRemoveConstraint.setObjectName("actionRemoveConstrain")
577        self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove constraint"))
578
579        self.actionMultiConstrain = QtWidgets.QAction(self)
580        self.actionMultiConstrain.setObjectName("actionMultiConstrain")
581        self.actionMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain selected parameters to their current values"))
582
583        self.actionMutualMultiConstrain = QtWidgets.QAction(self)
584        self.actionMutualMultiConstrain.setObjectName("actionMutualMultiConstrain")
585        self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of selected parameters..."))
586
587        menu.addAction(self.actionSelect)
588        menu.addAction(self.actionDeselect)
589        menu.addSeparator()
590
591        if has_constraints:
592            menu.addAction(self.actionRemoveConstraint)
593            #if num_rows == 1:
594            #    menu.addAction(self.actionEditConstraint)
595        else:
596            menu.addAction(self.actionConstrain)
597            if num_rows == 2:
598                menu.addAction(self.actionMutualMultiConstrain)
599
600        # Define the callbacks
601        self.actionConstrain.triggered.connect(self.addSimpleConstraint)
602        self.actionRemoveConstraint.triggered.connect(self.deleteConstraint)
603        self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint)
604        self.actionSelect.triggered.connect(self.selectParameters)
605        self.actionDeselect.triggered.connect(self.deselectParameters)
606        return menu
607
608    def showMultiConstraint(self):
609        """
610        Show the constraint widget and receive the expression
611        """
612        selected_rows = self.lstParams.selectionModel().selectedRows()
613        # There have to be only two rows selected. The caller takes care of that
614        # but let's check the correctness.
615        assert len(selected_rows) == 2
616
617        params_list = [s.data() for s in selected_rows]
618        # Create and display the widget for param1 and param2
619        mc_widget = MultiConstraint(self, params=params_list)
620        if mc_widget.exec_() != QtWidgets.QDialog.Accepted:
621            return
622
623        constraint = Constraint()
624        c_text = mc_widget.txtConstraint.text()
625
626        # widget.params[0] is the parameter we're constraining
627        constraint.param = mc_widget.params[0]
628        # parameter should have the model name preamble
629        model_name = self.kernel_module.name
630        # param_used is the parameter we're using in constraining function
631        param_used = mc_widget.params[1]
632        # Replace param_used with model_name.param_used
633        updated_param_used = model_name + "." + param_used
634        new_func = c_text.replace(param_used, updated_param_used)
635        constraint.func = new_func
636        # Which row is the constrained parameter in?
637        row = self.getRowFromName(constraint.param)
638
639        # Create a new item and add the Constraint object as a child
640        self.addConstraintToRow(constraint=constraint, row=row)
641
642    def getRowFromName(self, name):
643        """
644        Given parameter name get the row number in self._model_model
645        """
646        for row in range(self._model_model.rowCount()):
647            row_name = self._model_model.item(row).text()
648            if row_name == name:
649                return row
650        return None
651
652    def getParamNames(self):
653        """
654        Return list of all parameters for the current model
655        """
656        return [self._model_model.item(row).text() for row in range(self._model_model.rowCount())]
657
658    def modifyViewOnRow(self, row, font=None, brush=None):
659        """
660        Chage how the given row of the main model is shown
661        """
662        fields_enabled = False
663        if font is None:
664            font = QtGui.QFont()
665            fields_enabled = True
666        if brush is None:
667            brush = QtGui.QBrush()
668            fields_enabled = True
669        self._model_model.blockSignals(True)
670        # Modify font and foreground of affected rows
671        for column in range(0, self._model_model.columnCount()):
672            self._model_model.item(row, column).setForeground(brush)
673            self._model_model.item(row, column).setFont(font)
674            self._model_model.item(row, column).setEditable(fields_enabled)
675        self._model_model.blockSignals(False)
676
677    def addConstraintToRow(self, constraint=None, row=0):
678        """
679        Adds the constraint object to requested row
680        """
681        # Create a new item and add the Constraint object as a child
682        assert isinstance(constraint, Constraint)
683        assert 0 <= row <= self._model_model.rowCount()
684
685        item = QtGui.QStandardItem()
686        item.setData(constraint)
687        self._model_model.item(row, 1).setChild(0, item)
688        # Set min/max to the value constrained
689        self.constraintAddedSignal.emit([row])
690        # Show visual hints for the constraint
691        font = QtGui.QFont()
692        font.setItalic(True)
693        brush = QtGui.QBrush(QtGui.QColor('blue'))
694        self.modifyViewOnRow(row, font=font, brush=brush)
695        self.communicate.statusBarUpdateSignal.emit('Constraint added')
696
697    def addSimpleConstraint(self):
698        """
699        Adds a constraint on a single parameter.
700        """
701        min_col = self.lstParams.itemDelegate().param_min
702        max_col = self.lstParams.itemDelegate().param_max
703        for row in self.selectedParameters():
704            param = self._model_model.item(row, 0).text()
705            value = self._model_model.item(row, 1).text()
706            min_t = self._model_model.item(row, min_col).text()
707            max_t = self._model_model.item(row, max_col).text()
708            # Create a Constraint object
709            constraint = Constraint(param=param, value=value, min=min_t, max=max_t)
710            # Create a new item and add the Constraint object as a child
711            item = QtGui.QStandardItem()
712            item.setData(constraint)
713            self._model_model.item(row, 1).setChild(0, item)
714            # Assumed correctness from the validator
715            value = float(value)
716            # BUMPS calculates log(max-min) without any checks, so let's assign minor range
717            min_v = value - (value/10000.0)
718            max_v = value + (value/10000.0)
719            # Set min/max to the value constrained
720            self._model_model.item(row, min_col).setText(str(min_v))
721            self._model_model.item(row, max_col).setText(str(max_v))
722            self.constraintAddedSignal.emit([row])
723            # Show visual hints for the constraint
724            font = QtGui.QFont()
725            font.setItalic(True)
726            brush = QtGui.QBrush(QtGui.QColor('blue'))
727            self.modifyViewOnRow(row, font=font, brush=brush)
728        self.communicate.statusBarUpdateSignal.emit('Constraint added')
729
730    def deleteConstraint(self):
731        """
732        Delete constraints from selected parameters.
733        """
734        params = [s.data() for s in self.lstParams.selectionModel().selectedRows()
735                   if self.isCheckable(s.row())]
736        for param in params:
737            self.deleteConstraintOnParameter(param=param)
738
739    def deleteConstraintOnParameter(self, param=None):
740        """
741        Delete the constraint on model parameter 'param'
742        """
743        min_col = self.lstParams.itemDelegate().param_min
744        max_col = self.lstParams.itemDelegate().param_max
745        for row in range(self._model_model.rowCount()):
746            if not self.rowHasConstraint(row):
747                continue
748            # Get the Constraint object from of the model item
749            item = self._model_model.item(row, 1)
750            constraint = self.getConstraintForRow(row)
751            if constraint is None:
752                continue
753            if not isinstance(constraint, Constraint):
754                continue
755            if param and constraint.param != param:
756                continue
757            # Now we got the right row. Delete the constraint and clean up
758            # Retrieve old values and put them on the model
759            if constraint.min is not None:
760                self._model_model.item(row, min_col).setText(constraint.min)
761            if constraint.max is not None:
762                self._model_model.item(row, max_col).setText(constraint.max)
763            # Remove constraint item
764            item.removeRow(0)
765            self.constraintAddedSignal.emit([row])
766            self.modifyViewOnRow(row)
767
768        self.communicate.statusBarUpdateSignal.emit('Constraint removed')
769
770    def getConstraintForRow(self, row):
771        """
772        For the given row, return its constraint, if any
773        """
774        try:
775            item = self._model_model.item(row, 1)
776            return item.child(0).data()
777        except AttributeError:
778            # return none when no constraints
779            return None
780
781    def rowHasConstraint(self, row):
782        """
783        Finds out if row of the main model has a constraint child
784        """
785        item = self._model_model.item(row, 1)
786        if item.hasChildren():
787            c = item.child(0).data()
788            if isinstance(c, Constraint):
789                return True
790        return False
791
792    def rowHasActiveConstraint(self, row):
793        """
794        Finds out if row of the main model has an active constraint child
795        """
796        item = self._model_model.item(row, 1)
797        if item.hasChildren():
798            c = item.child(0).data()
799            if isinstance(c, Constraint) and c.active:
800                return True
801        return False
802
803    def rowHasActiveComplexConstraint(self, row):
804        """
805        Finds out if row of the main model has an active, nontrivial constraint child
806        """
807        item = self._model_model.item(row, 1)
808        if item.hasChildren():
809            c = item.child(0).data()
810            if isinstance(c, Constraint) and c.func and c.active:
811                return True
812        return False
813
814    def selectParameters(self):
815        """
816        Selected parameter is chosen for fitting
817        """
818        status = QtCore.Qt.Checked
819        self.setParameterSelection(status)
820
821    def deselectParameters(self):
822        """
823        Selected parameters are removed for fitting
824        """
825        status = QtCore.Qt.Unchecked
826        self.setParameterSelection(status)
827
828    def selectedParameters(self):
829        """ Returns list of selected (highlighted) parameters """
830        return [s.row() for s in self.lstParams.selectionModel().selectedRows()
831                if self.isCheckable(s.row())]
832
833    def setParameterSelection(self, status=QtCore.Qt.Unchecked):
834        """
835        Selected parameters are chosen for fitting
836        """
837        # Convert to proper indices and set requested enablement
838        for row in self.selectedParameters():
839            self._model_model.item(row, 0).setCheckState(status)
840
841    def getConstraintsForModel(self):
842        """
843        Return a list of tuples. Each tuple contains constraints mapped as
844        ('constrained parameter', 'function to constrain')
845        e.g. [('sld','5*sld_solvent')]
846        """
847        param_number = self._model_model.rowCount()
848        params = [(self._model_model.item(s, 0).text(),
849                    self._model_model.item(s, 1).child(0).data().func)
850                    for s in range(param_number) if self.rowHasActiveConstraint(s)]
851        return params
852
853    def getComplexConstraintsForModel(self):
854        """
855        Return a list of tuples. Each tuple contains constraints mapped as
856        ('constrained parameter', 'function to constrain')
857        e.g. [('sld','5*M2.sld_solvent')].
858        Only for constraints with defined VALUE
859        """
860        param_number = self._model_model.rowCount()
861        params = [(self._model_model.item(s, 0).text(),
862                    self._model_model.item(s, 1).child(0).data().func)
863                    for s in range(param_number) if self.rowHasActiveComplexConstraint(s)]
864        return params
865
866    def getConstraintObjectsForModel(self):
867        """
868        Returns Constraint objects present on the whole model
869        """
870        param_number = self._model_model.rowCount()
871        constraints = [self._model_model.item(s, 1).child(0).data()
872                       for s in range(param_number) if self.rowHasConstraint(s)]
873
874        return constraints
875
876    def getConstraintsForFitting(self):
877        """
878        Return a list of constraints in format ready for use in fiting
879        """
880        # Get constraints
881        constraints = self.getComplexConstraintsForModel()
882        # See if there are any constraints across models
883        multi_constraints = [cons for cons in constraints if self.isConstraintMultimodel(cons[1])]
884
885        if multi_constraints:
886            # Let users choose what to do
887            msg = "The current fit contains constraints relying on other fit pages.\n"
888            msg += "Parameters with those constraints are:\n" +\
889                '\n'.join([cons[0] for cons in multi_constraints])
890            msg += "\n\nWould you like to remove these constraints or cancel fitting?"
891            msgbox = QtWidgets.QMessageBox(self)
892            msgbox.setIcon(QtWidgets.QMessageBox.Warning)
893            msgbox.setText(msg)
894            msgbox.setWindowTitle("Existing Constraints")
895            # custom buttons
896            button_remove = QtWidgets.QPushButton("Remove")
897            msgbox.addButton(button_remove, QtWidgets.QMessageBox.YesRole)
898            button_cancel = QtWidgets.QPushButton("Cancel")
899            msgbox.addButton(button_cancel, QtWidgets.QMessageBox.RejectRole)
900            retval = msgbox.exec_()
901            if retval == QtWidgets.QMessageBox.RejectRole:
902                # cancel fit
903                raise ValueError("Fitting cancelled")
904            else:
905                # remove constraint
906                for cons in multi_constraints:
907                    self.deleteConstraintOnParameter(param=cons[0])
908                # re-read the constraints
909                constraints = self.getComplexConstraintsForModel()
910
911        return constraints
912
913    def showModelDescription(self):
914        """
915        Creates a window with model description, when right clicked in the treeview
916        """
917        msg = 'Model description:\n'
918        if self.kernel_module is not None:
919            if str(self.kernel_module.description).rstrip().lstrip() == '':
920                msg += "Sorry, no information is available for this model."
921            else:
922                msg += self.kernel_module.description + '\n'
923        else:
924            msg += "You must select a model to get information on this"
925
926        menu = QtWidgets.QMenu()
927        label = QtWidgets.QLabel(msg)
928        action = QtWidgets.QWidgetAction(self)
929        action.setDefaultWidget(label)
930        menu.addAction(action)
931        return menu
932
933    def onSelectModel(self):
934        """
935        Respond to select Model from list event
936        """
937        model = self.cbModel.currentText()
938
939        # empty combobox forced to be read
940        if not model:
941            return
942        # Reset structure factor
943        self.cbStructureFactor.setCurrentIndex(0)
944
945        # Reset parameters to fit
946        self.parameters_to_fit = None
947        self.has_error_column = False
948        self.has_poly_error_column = False
949
950        self.respondToModelStructure(model=model, structure_factor=None)
951
952    def onSelectBatchFilename(self, data_index):
953        """
954        Update the logic based on the selected file in batch fitting
955        """
956        self.data_index = data_index
957        self.updateQRange()
958
959    def onSelectStructureFactor(self):
960        """
961        Select Structure Factor from list
962        """
963        model = str(self.cbModel.currentText())
964        category = str(self.cbCategory.currentText())
965        structure = str(self.cbStructureFactor.currentText())
966        if category == CATEGORY_STRUCTURE:
967            model = None
968        self.respondToModelStructure(model=model, structure_factor=structure)
969
970    def onCustomModelChange(self):
971        """
972        Reload the custom model combobox
973        """
974        self.custom_models = self.customModels()
975        self.readCustomCategoryInfo()
976        # See if we need to update the combo in-place
977        if self.cbCategory.currentText() != CATEGORY_CUSTOM: return
978
979        current_text = self.cbModel.currentText()
980        self.cbModel.blockSignals(True)
981        self.cbModel.clear()
982        self.cbModel.blockSignals(False)
983        self.enableModelCombo()
984        self.disableStructureCombo()
985        # Retrieve the list of models
986        model_list = self.master_category_dict[CATEGORY_CUSTOM]
987        # Populate the models combobox
988        self.cbModel.addItems(sorted([model for (model, _) in model_list]))
989        new_index = self.cbModel.findText(current_text)
990        if new_index != -1:
991            self.cbModel.setCurrentIndex(self.cbModel.findText(current_text))
992
993    def onSelectionChanged(self):
994        """
995        React to parameter selection
996        """
997        rows = self.lstParams.selectionModel().selectedRows()
998        # Clean previous messages
999        self.communicate.statusBarUpdateSignal.emit("")
1000        if len(rows) == 1:
1001            # Show constraint, if present
1002            row = rows[0].row()
1003            if self.rowHasConstraint(row):
1004                func = self.getConstraintForRow(row).func
1005                if func is not None:
1006                    self.communicate.statusBarUpdateSignal.emit("Active constrain: "+func)
1007
1008    def replaceConstraintName(self, old_name, new_name=""):
1009        """
1010        Replace names of models in defined constraints
1011        """
1012        param_number = self._model_model.rowCount()
1013        # loop over parameters
1014        for row in range(param_number):
1015            if self.rowHasConstraint(row):
1016                func = self._model_model.item(row, 1).child(0).data().func
1017                if old_name in func:
1018                    new_func = func.replace(old_name, new_name)
1019                    self._model_model.item(row, 1).child(0).data().func = new_func
1020
1021    def isConstraintMultimodel(self, constraint):
1022        """
1023        Check if the constraint function text contains current model name
1024        """
1025        current_model_name = self.kernel_module.name
1026        if current_model_name in constraint:
1027            return False
1028        else:
1029            return True
1030
1031    def updateData(self):
1032        """
1033        Helper function for recalculation of data used in plotting
1034        """
1035        # Update the chart
1036        if self.data_is_loaded:
1037            self.cmdPlot.setText("Show Plot")
1038            self.calculateQGridForModel()
1039        else:
1040            self.cmdPlot.setText("Calculate")
1041            # Create default datasets if no data passed
1042            self.createDefaultDataset()
1043
1044    def respondToModelStructure(self, model=None, structure_factor=None):
1045        # Set enablement on calculate/plot
1046        self.cmdPlot.setEnabled(True)
1047
1048        # kernel parameters -> model_model
1049        self.SASModelToQModel(model, structure_factor)
1050
1051        # Update plot
1052        self.updateData()
1053
1054        # Update state stack
1055        self.updateUndo()
1056
1057        # Let others know
1058        self.newModelSignal.emit()
1059
1060    def onSelectCategory(self):
1061        """
1062        Select Category from list
1063        """
1064        category = self.cbCategory.currentText()
1065        # Check if the user chose "Choose category entry"
1066        if category == CATEGORY_DEFAULT:
1067            # if the previous category was not the default, keep it.
1068            # Otherwise, just return
1069            if self._previous_category_index != 0:
1070                # We need to block signals, or else state changes on perceived unchanged conditions
1071                self.cbCategory.blockSignals(True)
1072                self.cbCategory.setCurrentIndex(self._previous_category_index)
1073                self.cbCategory.blockSignals(False)
1074            return
1075
1076        if category == CATEGORY_STRUCTURE:
1077            self.disableModelCombo()
1078            self.enableStructureCombo()
1079            self._model_model.clear()
1080            return
1081
1082        # Safely clear and enable the model combo
1083        self.cbModel.blockSignals(True)
1084        self.cbModel.clear()
1085        self.cbModel.blockSignals(False)
1086        self.enableModelCombo()
1087        self.disableStructureCombo()
1088
1089        self._previous_category_index = self.cbCategory.currentIndex()
1090        # Retrieve the list of models
1091        model_list = self.master_category_dict[category]
1092        # Populate the models combobox
1093        self.cbModel.addItems(sorted([model for (model, _) in model_list]))
1094
1095    def onPolyModelChange(self, item):
1096        """
1097        Callback method for updating the main model and sasmodel
1098        parameters with the GUI values in the polydispersity view
1099        """
1100        model_column = item.column()
1101        model_row = item.row()
1102        name_index = self._poly_model.index(model_row, 0)
1103        parameter_name = str(name_index.data()).lower() # "distribution of sld" etc.
1104        if "distribution of" in parameter_name:
1105            # just the last word
1106            parameter_name = parameter_name.rsplit()[-1]
1107
1108        # Extract changed value.
1109        if model_column == self.lstPoly.itemDelegate().poly_parameter:
1110            # Is the parameter checked for fitting?
1111            value = item.checkState()
1112            parameter_name = parameter_name + '.width'
1113            if value == QtCore.Qt.Checked:
1114                self.parameters_to_fit.append(parameter_name)
1115            else:
1116                if parameter_name in self.parameters_to_fit:
1117                    self.parameters_to_fit.remove(parameter_name)
1118            self.cmdFit.setEnabled(self.parameters_to_fit != [] and self.logic.data_is_loaded)
1119            return
1120        elif model_column in [self.lstPoly.itemDelegate().poly_min, self.lstPoly.itemDelegate().poly_max]:
1121            try:
1122                value = GuiUtils.toDouble(item.text())
1123            except TypeError:
1124                # Can't be converted properly, bring back the old value and exit
1125                return
1126
1127            current_details = self.kernel_module.details[parameter_name]
1128            current_details[model_column-1] = value
1129        elif model_column == self.lstPoly.itemDelegate().poly_function:
1130            # name of the function - just pass
1131            return
1132        else:
1133            try:
1134                value = GuiUtils.toDouble(item.text())
1135            except TypeError:
1136                # Can't be converted properly, bring back the old value and exit
1137                return
1138
1139            # Update the sasmodel
1140            # PD[ratio] -> width, npts -> npts, nsigs -> nsigmas
1141            self.kernel_module.setParam(parameter_name + '.' + \
1142                                        self.lstPoly.itemDelegate().columnDict()[model_column], value)
1143
1144            # Update plot
1145            self.updateData()
1146
1147    def onMagnetModelChange(self, item):
1148        """
1149        Callback method for updating the sasmodel magnetic parameters with the GUI values
1150        """
1151        model_column = item.column()
1152        model_row = item.row()
1153        name_index = self._magnet_model.index(model_row, 0)
1154        parameter_name = str(self._magnet_model.data(name_index))
1155
1156        if model_column == 0:
1157            value = item.checkState()
1158            if value == QtCore.Qt.Checked:
1159                self.parameters_to_fit.append(parameter_name)
1160            else:
1161                if parameter_name in self.parameters_to_fit:
1162                    self.parameters_to_fit.remove(parameter_name)
1163            self.cmdFit.setEnabled(self.parameters_to_fit != [] and self.logic.data_is_loaded)
1164            # Update state stack
1165            self.updateUndo()
1166            return
1167
1168        # Extract changed value.
1169        try:
1170            value = GuiUtils.toDouble(item.text())
1171        except TypeError:
1172            # Unparsable field
1173            return
1174
1175        property_index = self._magnet_model.headerData(1, model_column)-1 # Value, min, max, etc.
1176
1177        # Update the parameter value - note: this supports +/-inf as well
1178        self.kernel_module.params[parameter_name] = value
1179
1180        # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf]
1181        self.kernel_module.details[parameter_name][property_index] = value
1182
1183        # Force the chart update when actual parameters changed
1184        if model_column == 1:
1185            self.recalculatePlotData()
1186
1187        # Update state stack
1188        self.updateUndo()
1189
1190    def onHelp(self):
1191        """
1192        Show the "Fitting" section of help
1193        """
1194        tree_location = "/user/qtgui/Perspectives/Fitting/"
1195
1196        # Actual file will depend on the current tab
1197        tab_id = self.tabFitting.currentIndex()
1198        helpfile = "fitting.html"
1199        if tab_id == 0:
1200            helpfile = "fitting_help.html"
1201        elif tab_id == 1:
1202            helpfile = "residuals_help.html"
1203        elif tab_id == 2:
1204            helpfile = "resolution.html"
1205        elif tab_id == 3:
1206            helpfile = "pd/polydispersity.html"
1207        elif tab_id == 4:
1208            helpfile = "magnetism/magnetism.html"
1209        help_location = tree_location + helpfile
1210
1211        self.showHelp(help_location)
1212
1213    def showHelp(self, url):
1214        """
1215        Calls parent's method for opening an HTML page
1216        """
1217        self.parent.showHelp(url)
1218
1219    def onDisplayMagneticAngles(self):
1220        """
1221        Display a simple image showing direction of magnetic angles
1222        """
1223        self.magneticAnglesWidget.show()
1224
1225    def onFit(self):
1226        """
1227        Perform fitting on the current data
1228        """
1229        if self.fit_started:
1230            self.stopFit()
1231            return
1232
1233        # initialize fitter constants
1234        fit_id = 0
1235        handler = None
1236        batch_inputs = {}
1237        batch_outputs = {}
1238        #---------------------------------
1239        if LocalConfig.USING_TWISTED:
1240            handler = None
1241            updater = None
1242        else:
1243            handler = ConsoleUpdate(parent=self.parent,
1244                                    manager=self,
1245                                    improvement_delta=0.1)
1246            updater = handler.update_fit
1247
1248        # Prepare the fitter object
1249        try:
1250            fitters, _ = self.prepareFitters()
1251        except ValueError as ex:
1252            # This should not happen! GUI explicitly forbids this situation
1253            self.communicate.statusBarUpdateSignal.emit(str(ex))
1254            return
1255
1256        # keep local copy of kernel parameters, as they will change during the update
1257        self.kernel_module_copy = copy.deepcopy(self.kernel_module)
1258
1259        # Create the fitting thread, based on the fitter
1260        completefn = self.batchFittingCompleted if self.is_batch_fitting else self.fittingCompleted
1261
1262        self.calc_fit = FitThread(handler=handler,
1263                            fn=fitters,
1264                            batch_inputs=batch_inputs,
1265                            batch_outputs=batch_outputs,
1266                            page_id=[[self.page_id]],
1267                            updatefn=updater,
1268                            completefn=completefn,
1269                            reset_flag=self.is_chain_fitting)
1270
1271        if LocalConfig.USING_TWISTED:
1272            # start the trhrhread with twisted
1273            calc_thread = threads.deferToThread(self.calc_fit.compute)
1274            calc_thread.addCallback(completefn)
1275            calc_thread.addErrback(self.fitFailed)
1276        else:
1277            # Use the old python threads + Queue
1278            self.calc_fit.queue()
1279            self.calc_fit.ready(2.5)
1280
1281        self.communicate.statusBarUpdateSignal.emit('Fitting started...')
1282        self.fit_started = True
1283        # Disable some elements
1284        self.setFittingStarted()
1285
1286    def stopFit(self):
1287        """
1288        Attempt to stop the fitting thread
1289        """
1290        if self.calc_fit is None or not self.calc_fit.isrunning():
1291            return
1292        self.calc_fit.stop()
1293        #self.fit_started=False
1294        #re-enable the Fit button
1295        self.setFittingStopped()
1296
1297        msg = "Fitting cancelled."
1298        self.communicate.statusBarUpdateSignal.emit(msg)
1299
1300    def updateFit(self):
1301        """
1302        """
1303        print("UPDATE FIT")
1304        pass
1305
1306    def fitFailed(self, reason):
1307        """
1308        """
1309        self.setFittingStopped()
1310        msg = "Fitting failed with: "+ str(reason)
1311        self.communicate.statusBarUpdateSignal.emit(msg)
1312
1313    def batchFittingCompleted(self, result):
1314        """
1315        Send the finish message from calculate threads to main thread
1316        """
1317        self.batchFittingFinishedSignal.emit(result)
1318
1319    def batchFitComplete(self, result):
1320        """
1321        Receive and display batch fitting results
1322        """
1323        #re-enable the Fit button
1324        self.setFittingStopped()
1325
1326        if result is None:
1327            msg = "Fitting failed."
1328            self.communicate.statusBarUpdateSignal.emit(msg)
1329            return
1330
1331        # Show the grid panel
1332        self.communicate.sendDataToGridSignal.emit(result[0])
1333
1334        elapsed = result[1]
1335        msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed)
1336        self.communicate.statusBarUpdateSignal.emit(msg)
1337
1338        # Run over the list of results and update the items
1339        for res_index, res_list in enumerate(result[0]):
1340            # results
1341            res = res_list[0]
1342            param_dict = self.paramDictFromResults(res)
1343
1344            # create local kernel_module
1345            kernel_module = FittingUtilities.updateKernelWithResults(self.kernel_module, param_dict)
1346            # pull out current data
1347            data = self._logic[res_index].data
1348
1349            # Switch indexes
1350            self.onSelectBatchFilename(res_index)
1351
1352            method = self.complete1D if isinstance(self.data, Data1D) else self.complete2D
1353            self.calculateQGridForModelExt(data=data, model=kernel_module, completefn=method, use_threads=False)
1354
1355        # Restore original kernel_module, so subsequent fits on the same model don't pick up the new params
1356        if self.kernel_module is not None:
1357            self.kernel_module = copy.deepcopy(self.kernel_module_copy)
1358
1359    def paramDictFromResults(self, results):
1360        """
1361        Given the fit results structure, pull out optimized parameters and return them as nicely
1362        formatted dict
1363        """
1364        if results.fitness is None or \
1365            not np.isfinite(results.fitness) or \
1366            np.any(results.pvec is None) or \
1367            not np.all(np.isfinite(results.pvec)):
1368            msg = "Fitting did not converge!"
1369            self.communicate.statusBarUpdateSignal.emit(msg)
1370            msg += results.mesg
1371            logging.error(msg)
1372            return
1373
1374        param_list = results.param_list # ['radius', 'radius.width']
1375        param_values = results.pvec     # array([ 0.36221662,  0.0146783 ])
1376        param_stderr = results.stderr   # array([ 1.71293015,  1.71294233])
1377        params_and_errors = list(zip(param_values, param_stderr))
1378        param_dict = dict(zip(param_list, params_and_errors))
1379
1380        return param_dict
1381
1382    def fittingCompleted(self, result):
1383        """
1384        Send the finish message from calculate threads to main thread
1385        """
1386        self.fittingFinishedSignal.emit(result)
1387
1388    def fitComplete(self, result):
1389        """
1390        Receive and display fitting results
1391        "result" is a tuple of actual result list and the fit time in seconds
1392        """
1393        #re-enable the Fit button
1394        self.setFittingStopped()
1395
1396        if result is None:
1397            msg = "Fitting failed."
1398            self.communicate.statusBarUpdateSignal.emit(msg)
1399            return
1400
1401        res_list = result[0][0]
1402        res = res_list[0]
1403        self.chi2 = res.fitness
1404        param_dict = self.paramDictFromResults(res)
1405
1406        elapsed = result[1]
1407        if self.calc_fit._interrupting:
1408            msg = "Fitting cancelled by user after: %s s." % GuiUtils.formatNumber(elapsed)
1409            logging.warning("\n"+msg+"\n")
1410        else:
1411            msg = "Fitting completed successfully in: %s s." % GuiUtils.formatNumber(elapsed)
1412        self.communicate.statusBarUpdateSignal.emit(msg)
1413
1414        # Dictionary of fitted parameter: value, error
1415        # e.g. param_dic = {"sld":(1.703, 0.0034), "length":(33.455, -0.0983)}
1416        self.updateModelFromList(param_dict)
1417
1418        self.updatePolyModelFromList(param_dict)
1419
1420        self.updateMagnetModelFromList(param_dict)
1421
1422        # update charts
1423        self.onPlot()
1424
1425        # Read only value - we can get away by just printing it here
1426        chi2_repr = GuiUtils.formatNumber(self.chi2, high=True)
1427        self.lblChi2Value.setText(chi2_repr)
1428
1429    def prepareFitters(self, fitter=None, fit_id=0):
1430        """
1431        Prepare the Fitter object for use in fitting
1432        """
1433        # fitter = None -> single/batch fitting
1434        # fitter = Fit() -> simultaneous fitting
1435
1436        # Data going in
1437        data = self.logic.data
1438        model = self.kernel_module
1439        qmin = self.q_range_min
1440        qmax = self.q_range_max
1441        params_to_fit = self.parameters_to_fit
1442        if not params_to_fit:
1443            raise ValueError('Fitting requires at least one parameter to optimize.')
1444
1445        # Potential smearing added
1446        # Remember that smearing_min/max can be None ->
1447        # deal with it until Python gets discriminated unions
1448        self.addWeightingToData(data)
1449
1450        # Get the constraints.
1451        constraints = self.getComplexConstraintsForModel()
1452        if fitter is None:
1453            # For single fits - check for inter-model constraints
1454            constraints = self.getConstraintsForFitting()
1455
1456        smearer = self.smearing_widget.smearer()
1457        handler = None
1458        batch_inputs = {}
1459        batch_outputs = {}
1460
1461        fitters = []
1462        for fit_index in self.all_data:
1463            fitter_single = Fit() if fitter is None else fitter
1464            data = GuiUtils.dataFromItem(fit_index)
1465            # Potential weights added directly to data
1466            self.addWeightingToData(data)
1467            try:
1468                fitter_single.set_model(model, fit_id, params_to_fit, data=data,
1469                             constraints=constraints)
1470            except ValueError as ex:
1471                raise ValueError("Setting model parameters failed with: %s" % ex)
1472
1473            qmin, qmax, _ = self.logic.computeRangeFromData(data)
1474            fitter_single.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin,
1475                            qmax=qmax)
1476            fitter_single.select_problem_for_fit(id=fit_id, value=1)
1477            if fitter is None:
1478                # Assign id to the new fitter only
1479                fitter_single.fitter_id = [self.page_id]
1480            fit_id += 1
1481            fitters.append(fitter_single)
1482
1483        return fitters, fit_id
1484
1485    def iterateOverModel(self, func):
1486        """
1487        Take func and throw it inside the model row loop
1488        """
1489        for row_i in range(self._model_model.rowCount()):
1490            func(row_i)
1491
1492    def updateModelFromList(self, param_dict):
1493        """
1494        Update the model with new parameters, create the errors column
1495        """
1496        assert isinstance(param_dict, dict)
1497        if not dict:
1498            return
1499
1500        def updateFittedValues(row):
1501            # Utility function for main model update
1502            # internal so can use closure for param_dict
1503            param_name = str(self._model_model.item(row, 0).text())
1504            if param_name not in list(param_dict.keys()):
1505                return
1506            # modify the param value
1507            param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True)
1508            self._model_model.item(row, 1).setText(param_repr)
1509            if self.has_error_column:
1510                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1511                self._model_model.item(row, 2).setText(error_repr)
1512
1513        def updatePolyValues(row):
1514            # Utility function for updateof polydispersity part of the main model
1515            param_name = str(self._model_model.item(row, 0).text())+'.width'
1516            if param_name not in list(param_dict.keys()):
1517                return
1518            # modify the param value
1519            param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True)
1520            self._model_model.item(row, 0).child(0).child(0,1).setText(param_repr)
1521
1522        def createErrorColumn(row):
1523            # Utility function for error column update
1524            item = QtGui.QStandardItem()
1525            def createItem(param_name):
1526                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1527                item.setText(error_repr)
1528            def curr_param():
1529                return str(self._model_model.item(row, 0).text())
1530
1531            [createItem(param_name) for param_name in list(param_dict.keys()) if curr_param() == param_name]
1532
1533            error_column.append(item)
1534
1535        # block signals temporarily, so we don't end up
1536        # updating charts with every single model change on the end of fitting
1537        self._model_model.blockSignals(True)
1538        self.iterateOverModel(updateFittedValues)
1539        self.iterateOverModel(updatePolyValues)
1540        self._model_model.blockSignals(False)
1541
1542        if self.has_error_column:
1543            return
1544
1545        error_column = []
1546        self.lstParams.itemDelegate().addErrorColumn()
1547        self.iterateOverModel(createErrorColumn)
1548
1549        # switch off reponse to model change
1550        self._model_model.insertColumn(2, error_column)
1551        FittingUtilities.addErrorHeadersToModel(self._model_model)
1552        # Adjust the table cells width.
1553        # TODO: find a way to dynamically adjust column width while resized expanding
1554        self.lstParams.resizeColumnToContents(0)
1555        self.lstParams.resizeColumnToContents(4)
1556        self.lstParams.resizeColumnToContents(5)
1557        self.lstParams.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding)
1558
1559        self.has_error_column = True
1560
1561    def iterateOverPolyModel(self, func):
1562        """
1563        Take func and throw it inside the poly model row loop
1564        """
1565        for row_i in range(self._poly_model.rowCount()):
1566            func(row_i)
1567
1568    def updatePolyModelFromList(self, param_dict):
1569        """
1570        Update the polydispersity model with new parameters, create the errors column
1571        """
1572        assert isinstance(param_dict, dict)
1573        if not dict:
1574            return
1575
1576        def updateFittedValues(row_i):
1577            # Utility function for main model update
1578            # internal so can use closure for param_dict
1579            if row_i >= self._poly_model.rowCount():
1580                return
1581            param_name = str(self._poly_model.item(row_i, 0).text()).rsplit()[-1] + '.width'
1582            if param_name not in list(param_dict.keys()):
1583                return
1584            # modify the param value
1585            param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True)
1586            self._poly_model.item(row_i, 1).setText(param_repr)
1587            if self.has_poly_error_column:
1588                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1589                self._poly_model.item(row_i, 2).setText(error_repr)
1590
1591
1592        def createErrorColumn(row_i):
1593            # Utility function for error column update
1594            if row_i >= self._poly_model.rowCount():
1595                return
1596            item = QtGui.QStandardItem()
1597
1598            def createItem(param_name):
1599                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1600                item.setText(error_repr)
1601
1602            def poly_param():
1603                return str(self._poly_model.item(row_i, 0).text()).rsplit()[-1] + '.width'
1604
1605            [createItem(param_name) for param_name in list(param_dict.keys()) if poly_param() == param_name]
1606
1607            error_column.append(item)
1608
1609        # block signals temporarily, so we don't end up
1610        # updating charts with every single model change on the end of fitting
1611        self._poly_model.blockSignals(True)
1612        self.iterateOverPolyModel(updateFittedValues)
1613        self._poly_model.blockSignals(False)
1614
1615        if self.has_poly_error_column:
1616            return
1617
1618        self.lstPoly.itemDelegate().addErrorColumn()
1619        error_column = []
1620        self.iterateOverPolyModel(createErrorColumn)
1621
1622        # switch off reponse to model change
1623        self._poly_model.blockSignals(True)
1624        self._poly_model.insertColumn(2, error_column)
1625        self._poly_model.blockSignals(False)
1626        FittingUtilities.addErrorPolyHeadersToModel(self._poly_model)
1627
1628        self.has_poly_error_column = True
1629
1630    def iterateOverMagnetModel(self, func):
1631        """
1632        Take func and throw it inside the magnet model row loop
1633        """
1634        for row_i in range(self._model_model.rowCount()):
1635            func(row_i)
1636
1637    def updateMagnetModelFromList(self, param_dict):
1638        """
1639        Update the magnetic model with new parameters, create the errors column
1640        """
1641        assert isinstance(param_dict, dict)
1642        if not dict:
1643            return
1644        if self._magnet_model.rowCount() == 0:
1645            return
1646
1647        def updateFittedValues(row):
1648            # Utility function for main model update
1649            # internal so can use closure for param_dict
1650            if self._magnet_model.item(row, 0) is None:
1651                return
1652            param_name = str(self._magnet_model.item(row, 0).text())
1653            if param_name not in list(param_dict.keys()):
1654                return
1655            # modify the param value
1656            param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True)
1657            self._magnet_model.item(row, 1).setText(param_repr)
1658            if self.has_magnet_error_column:
1659                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1660                self._magnet_model.item(row, 2).setText(error_repr)
1661
1662        def createErrorColumn(row):
1663            # Utility function for error column update
1664            item = QtGui.QStandardItem()
1665            def createItem(param_name):
1666                error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
1667                item.setText(error_repr)
1668            def curr_param():
1669                return str(self._magnet_model.item(row, 0).text())
1670
1671            [createItem(param_name) for param_name in list(param_dict.keys()) if curr_param() == param_name]
1672
1673            error_column.append(item)
1674
1675        # block signals temporarily, so we don't end up
1676        # updating charts with every single model change on the end of fitting
1677        self._magnet_model.blockSignals(True)
1678        self.iterateOverMagnetModel(updateFittedValues)
1679        self._magnet_model.blockSignals(False)
1680
1681        if self.has_magnet_error_column:
1682            return
1683
1684        self.lstMagnetic.itemDelegate().addErrorColumn()
1685        error_column = []
1686        self.iterateOverMagnetModel(createErrorColumn)
1687
1688        # switch off reponse to model change
1689        self._magnet_model.blockSignals(True)
1690        self._magnet_model.insertColumn(2, error_column)
1691        self._magnet_model.blockSignals(False)
1692        FittingUtilities.addErrorHeadersToModel(self._magnet_model)
1693
1694        self.has_magnet_error_column = True
1695
1696    def onPlot(self):
1697        """
1698        Plot the current set of data
1699        """
1700        # Regardless of previous state, this should now be `plot show` functionality only
1701        self.cmdPlot.setText("Show Plot")
1702        # Force data recalculation so existing charts are updated
1703        self.recalculatePlotData()
1704        self.showPlot()
1705
1706    def onSmearingOptionsUpdate(self):
1707        """
1708        React to changes in the smearing widget
1709        """
1710        self.calculateQGridForModel()
1711
1712    def recalculatePlotData(self):
1713        """
1714        Generate a new dataset for model
1715        """
1716        if not self.data_is_loaded:
1717            self.createDefaultDataset()
1718        self.calculateQGridForModel()
1719
1720    def showPlot(self):
1721        """
1722        Show the current plot in MPL
1723        """
1724        # Show the chart if ready
1725        data_to_show = self.data if self.data_is_loaded else self.model_data
1726        if data_to_show is not None:
1727            self.communicate.plotRequestedSignal.emit([data_to_show])
1728
1729    def onOptionsUpdate(self):
1730        """
1731        Update local option values and replot
1732        """
1733        self.q_range_min, self.q_range_max, self.npts, self.log_points, self.weighting = \
1734            self.options_widget.state()
1735        # set Q range labels on the main tab
1736        self.lblMinRangeDef.setText(str(self.q_range_min))
1737        self.lblMaxRangeDef.setText(str(self.q_range_max))
1738        self.recalculatePlotData()
1739
1740    def setDefaultStructureCombo(self):
1741        """
1742        Fill in the structure factors combo box with defaults
1743        """
1744        structure_factor_list = self.master_category_dict.pop(CATEGORY_STRUCTURE)
1745        factors = [factor[0] for factor in structure_factor_list]
1746        factors.insert(0, STRUCTURE_DEFAULT)
1747        self.cbStructureFactor.clear()
1748        self.cbStructureFactor.addItems(sorted(factors))
1749
1750    def createDefaultDataset(self):
1751        """
1752        Generate default Dataset 1D/2D for the given model
1753        """
1754        # Create default datasets if no data passed
1755        if self.is2D:
1756            qmax = self.q_range_max/np.sqrt(2)
1757            qstep = self.npts
1758            self.logic.createDefault2dData(qmax, qstep, self.tab_id)
1759            return
1760        elif self.log_points:
1761            qmin = -10.0 if self.q_range_min < 1.e-10 else np.log10(self.q_range_min)
1762            qmax = 10.0 if self.q_range_max > 1.e10 else np.log10(self.q_range_max)
1763            interval = np.logspace(start=qmin, stop=qmax, num=self.npts, endpoint=True, base=10.0)
1764        else:
1765            interval = np.linspace(start=self.q_range_min, stop=self.q_range_max,
1766                                   num=self.npts, endpoint=True)
1767        self.logic.createDefault1dData(interval, self.tab_id)
1768
1769    def readCategoryInfo(self):
1770        """
1771        Reads the categories in from file
1772        """
1773        self.master_category_dict = defaultdict(list)
1774        self.by_model_dict = defaultdict(list)
1775        self.model_enabled_dict = defaultdict(bool)
1776
1777        categorization_file = CategoryInstaller.get_user_file()
1778        if not os.path.isfile(categorization_file):
1779            categorization_file = CategoryInstaller.get_default_file()
1780        with open(categorization_file, 'rb') as cat_file:
1781            self.master_category_dict = json.load(cat_file)
1782            self.regenerateModelDict()
1783
1784        # Load the model dict
1785        models = load_standard_models()
1786        for model in models:
1787            self.models[model.name] = model
1788
1789        self.readCustomCategoryInfo()
1790
1791    def readCustomCategoryInfo(self):
1792        """
1793        Reads the custom model category
1794        """
1795        #Looking for plugins
1796        self.plugins = list(self.custom_models.values())
1797        plugin_list = []
1798        for name, plug in self.custom_models.items():
1799            self.models[name] = plug
1800            plugin_list.append([name, True])
1801        self.master_category_dict[CATEGORY_CUSTOM] = plugin_list
1802
1803    def regenerateModelDict(self):
1804        """
1805        Regenerates self.by_model_dict which has each model name as the
1806        key and the list of categories belonging to that model
1807        along with the enabled mapping
1808        """
1809        self.by_model_dict = defaultdict(list)
1810        for category in self.master_category_dict:
1811            for (model, enabled) in self.master_category_dict[category]:
1812                self.by_model_dict[model].append(category)
1813                self.model_enabled_dict[model] = enabled
1814
1815    def addBackgroundToModel(self, model):
1816        """
1817        Adds background parameter with default values to the model
1818        """
1819        assert isinstance(model, QtGui.QStandardItemModel)
1820        checked_list = ['background', '0.001', '-inf', 'inf', '1/cm']
1821        FittingUtilities.addCheckedListToModel(model, checked_list)
1822        last_row = model.rowCount()-1
1823        model.item(last_row, 0).setEditable(False)
1824        model.item(last_row, 4).setEditable(False)
1825
1826    def addScaleToModel(self, model):
1827        """
1828        Adds scale parameter with default values to the model
1829        """
1830        assert isinstance(model, QtGui.QStandardItemModel)
1831        checked_list = ['scale', '1.0', '0.0', 'inf', '']
1832        FittingUtilities.addCheckedListToModel(model, checked_list)
1833        last_row = model.rowCount()-1
1834        model.item(last_row, 0).setEditable(False)
1835        model.item(last_row, 4).setEditable(False)
1836
1837    def addWeightingToData(self, data):
1838        """
1839        Adds weighting contribution to fitting data
1840        """
1841        # Send original data for weighting
1842        weight = FittingUtilities.getWeight(data=data, is2d=self.is2D, flag=self.weighting)
1843        if self.is2D:
1844            data.err_data = weight
1845        else:
1846            data.dy = weight
1847        pass
1848
1849    def updateQRange(self):
1850        """
1851        Updates Q Range display
1852        """
1853        if self.data_is_loaded:
1854            self.q_range_min, self.q_range_max, self.npts = self.logic.computeDataRange()
1855        # set Q range labels on the main tab
1856        self.lblMinRangeDef.setText(str(self.q_range_min))
1857        self.lblMaxRangeDef.setText(str(self.q_range_max))
1858        # set Q range labels on the options tab
1859        self.options_widget.updateQRange(self.q_range_min, self.q_range_max, self.npts)
1860
1861    def SASModelToQModel(self, model_name, structure_factor=None):
1862        """
1863        Setting model parameters into table based on selected category
1864        """
1865        # Crete/overwrite model items
1866        self._model_model.clear()
1867
1868        # First, add parameters from the main model
1869        if model_name is not None:
1870            self.fromModelToQModel(model_name)
1871
1872        # Then, add structure factor derived parameters
1873        if structure_factor is not None and structure_factor != "None":
1874            if model_name is None:
1875                # Instantiate the current sasmodel for SF-only models
1876                self.kernel_module = self.models[structure_factor]()
1877            self.fromStructureFactorToQModel(structure_factor)
1878        else:
1879            # Allow the SF combobox visibility for the given sasmodel
1880            self.enableStructureFactorControl(structure_factor)
1881
1882        # Then, add multishells
1883        if model_name is not None:
1884            # Multishell models need additional treatment
1885            self.addExtraShells()
1886
1887        # Add polydispersity to the model
1888        self.setPolyModel()
1889        # Add magnetic parameters to the model
1890        self.setMagneticModel()
1891
1892        # Adjust the table cells width
1893        self.lstParams.resizeColumnToContents(0)
1894        self.lstParams.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding)
1895
1896        # Now we claim the model has been loaded
1897        self.model_is_loaded = True
1898        # Change the model name to a monicker
1899        self.kernel_module.name = self.modelName()
1900        # Update the smearing tab
1901        self.smearing_widget.updateKernelModel(kernel_model=self.kernel_module)
1902
1903        # (Re)-create headers
1904        FittingUtilities.addHeadersToModel(self._model_model)
1905        self.lstParams.header().setFont(self.boldFont)
1906
1907        # Update Q Ranges
1908        self.updateQRange()
1909
1910    def fromModelToQModel(self, model_name):
1911        """
1912        Setting model parameters into QStandardItemModel based on selected _model_
1913        """
1914        name = model_name
1915        if self.cbCategory.currentText() == CATEGORY_CUSTOM:
1916            # custom kernel load requires full path
1917            name = os.path.join(ModelUtilities.find_plugins_dir(), model_name+".py")
1918        kernel_module = generate.load_kernel_module(name)
1919
1920        if hasattr(kernel_module, 'parameters'):
1921            # built-in and custom models
1922            self.model_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
1923
1924        elif hasattr(kernel_module, 'model_info'):
1925            # for sum/multiply models
1926            self.model_parameters = kernel_module.model_info.parameters
1927
1928        elif hasattr(kernel_module, 'Model') and hasattr(kernel_module.Model, "_model_info"):
1929            # this probably won't work if there's no model_info, but just in case
1930            self.model_parameters = kernel_module.Model._model_info.parameters
1931        else:
1932            # no parameters - default to blank table
1933            msg = "No parameters found in model '{}'.".format(model_name)
1934            logger.warning(msg)
1935            self.model_parameters = modelinfo.ParameterTable([])
1936
1937        # Instantiate the current sasmodel
1938        self.kernel_module = self.models[model_name]()
1939
1940        # Explicitly add scale and background with default values
1941        temp_undo_state = self.undo_supported
1942        self.undo_supported = False
1943        self.addScaleToModel(self._model_model)
1944        self.addBackgroundToModel(self._model_model)
1945        self.undo_supported = temp_undo_state
1946
1947        self.shell_names = self.shellNamesList()
1948
1949        # Update the QModel
1950        new_rows = FittingUtilities.addParametersToModel(self.model_parameters, self.kernel_module, self.is2D)
1951
1952        for row in new_rows:
1953            self._model_model.appendRow(row)
1954        # Update the counter used for multishell display
1955        self._last_model_row = self._model_model.rowCount()
1956
1957    def fromStructureFactorToQModel(self, structure_factor):
1958        """
1959        Setting model parameters into QStandardItemModel based on selected _structure factor_
1960        """
1961        structure_module = generate.load_kernel_module(structure_factor)
1962        structure_parameters = modelinfo.make_parameter_table(getattr(structure_module, 'parameters', []))
1963        structure_kernel = self.models[structure_factor]()
1964
1965        self.kernel_module._model_info = product.make_product_info(self.kernel_module._model_info, structure_kernel._model_info)
1966
1967        new_rows = FittingUtilities.addSimpleParametersToModel(structure_parameters, self.is2D)
1968        for row in new_rows:
1969            self._model_model.appendRow(row)
1970        # Update the counter used for multishell display
1971        self._last_model_row = self._model_model.rowCount()
1972
1973    def onMainParamsChange(self, item):
1974        """
1975        Callback method for updating the sasmodel parameters with the GUI values
1976        """
1977        model_column = item.column()
1978
1979        if model_column == 0:
1980            self.checkboxSelected(item)
1981            self.cmdFit.setEnabled(self.parameters_to_fit != [] and self.logic.data_is_loaded)
1982            # Update state stack
1983            self.updateUndo()
1984            return
1985
1986        model_row = item.row()
1987        name_index = self._model_model.index(model_row, 0)
1988
1989        # Extract changed value.
1990        try:
1991            value = GuiUtils.toDouble(item.text())
1992        except TypeError:
1993            # Unparsable field
1994            return
1995
1996        parameter_name = str(self._model_model.data(name_index)) # sld, background etc.
1997
1998        # Update the parameter value - note: this supports +/-inf as well
1999        self.kernel_module.params[parameter_name] = value
2000
2001        # Update the parameter value - note: this supports +/-inf as well
2002        param_column = self.lstParams.itemDelegate().param_value
2003        min_column = self.lstParams.itemDelegate().param_min
2004        max_column = self.lstParams.itemDelegate().param_max
2005        if model_column == param_column:
2006            self.kernel_module.setParam(parameter_name, value)
2007        elif model_column == min_column:
2008            # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf]
2009            self.kernel_module.details[parameter_name][1] = value
2010        elif model_column == max_column:
2011            self.kernel_module.details[parameter_name][2] = value
2012        else:
2013            # don't update the chart
2014            return
2015
2016        # TODO: magnetic params in self.kernel_module.details['M0:parameter_name'] = value
2017        # TODO: multishell params in self.kernel_module.details[??] = value
2018
2019        # Force the chart update when actual parameters changed
2020        if model_column == 1:
2021            self.recalculatePlotData()
2022
2023        # Update state stack
2024        self.updateUndo()
2025
2026    def isCheckable(self, row):
2027        return self._model_model.item(row, 0).isCheckable()
2028
2029    def checkboxSelected(self, item):
2030        # Assure we're dealing with checkboxes
2031        if not item.isCheckable():
2032            return
2033        status = item.checkState()
2034
2035        # If multiple rows selected - toggle all of them, filtering uncheckable
2036        # Switch off signaling from the model to avoid recursion
2037        self._model_model.blockSignals(True)
2038        # Convert to proper indices and set requested enablement
2039        self.setParameterSelection(status)
2040        #[self._model_model.item(row, 0).setCheckState(status) for row in self.selectedParameters()]
2041        self._model_model.blockSignals(False)
2042
2043        # update the list of parameters to fit
2044        main_params = self.checkedListFromModel(self._model_model)
2045        poly_params = self.checkedListFromModel(self._poly_model)
2046        magnet_params = self.checkedListFromModel(self._magnet_model)
2047
2048        # Retrieve poly params names
2049        poly_params = [param.rsplit()[-1] + '.width' for param in poly_params]
2050
2051        self.parameters_to_fit = main_params + poly_params + magnet_params
2052
2053    def checkedListFromModel(self, model):
2054        """
2055        Returns list of checked parameters for given model
2056        """
2057        def isChecked(row):
2058            return model.item(row, 0).checkState() == QtCore.Qt.Checked
2059
2060        return [str(model.item(row_index, 0).text())
2061                for row_index in range(model.rowCount())
2062                if isChecked(row_index)]
2063
2064    def createNewIndex(self, fitted_data):
2065        """
2066        Create a model or theory index with passed Data1D/Data2D
2067        """
2068        if self.data_is_loaded:
2069            if not fitted_data.name:
2070                name = self.nameForFittedData(self.data.filename)
2071                fitted_data.title = name
2072                fitted_data.name = name
2073                fitted_data.filename = name
2074                fitted_data.symbol = "Line"
2075            self.updateModelIndex(fitted_data)
2076        else:
2077            name = self.nameForFittedData(self.kernel_module.id)
2078            fitted_data.title = name
2079            fitted_data.name = name
2080            fitted_data.filename = name
2081            fitted_data.symbol = "Line"
2082            self.createTheoryIndex(fitted_data)
2083
2084    def updateModelIndex(self, fitted_data):
2085        """
2086        Update a QStandardModelIndex containing model data
2087        """
2088        name = self.nameFromData(fitted_data)
2089        # Make this a line if no other defined
2090        if hasattr(fitted_data, 'symbol') and fitted_data.symbol is None:
2091            fitted_data.symbol = 'Line'
2092        # Notify the GUI manager so it can update the main model in DataExplorer
2093        GuiUtils.updateModelItemWithPlot(self.all_data[self.data_index], fitted_data, name)
2094
2095    def createTheoryIndex(self, fitted_data):
2096        """
2097        Create a QStandardModelIndex containing model data
2098        """
2099        name = self.nameFromData(fitted_data)
2100        # Notify the GUI manager so it can create the theory model in DataExplorer
2101        new_item = GuiUtils.createModelItemWithPlot(fitted_data, name=name)
2102        self.communicate.updateTheoryFromPerspectiveSignal.emit(new_item)
2103
2104    def nameFromData(self, fitted_data):
2105        """
2106        Return name for the dataset. Terribly impure function.
2107        """
2108        if fitted_data.name is None:
2109            name = self.nameForFittedData(self.logic.data.filename)
2110            fitted_data.title = name
2111            fitted_data.name = name
2112            fitted_data.filename = name
2113        else:
2114            name = fitted_data.name
2115        return name
2116
2117    def methodCalculateForData(self):
2118        '''return the method for data calculation'''
2119        return Calc1D if isinstance(self.data, Data1D) else Calc2D
2120
2121    def methodCompleteForData(self):
2122        '''return the method for result parsin on calc complete '''
2123        return self.completed1D if isinstance(self.data, Data1D) else self.completed2D
2124
2125    def calculateQGridForModelExt(self, data=None, model=None, completefn=None, use_threads=True):
2126        """
2127        Wrapper for Calc1D/2D calls
2128        """
2129        if data is None:
2130            data = self.data
2131        if model is None:
2132            model = self.kernel_module
2133        if completefn is None:
2134            completefn = self.methodCompleteForData()
2135        smearer = self.smearing_widget.smearer()
2136        # Awful API to a backend method.
2137        calc_thread = self.methodCalculateForData()(data=data,
2138                                               model=model,
2139                                               page_id=0,
2140                                               qmin=self.q_range_min,
2141                                               qmax=self.q_range_max,
2142                                               smearer=smearer,
2143                                               state=None,
2144                                               weight=None,
2145                                               fid=None,
2146                                               toggle_mode_on=False,
2147                                               completefn=completefn,
2148                                               update_chisqr=True,
2149                                               exception_handler=self.calcException,
2150                                               source=None)
2151        if use_threads:
2152            if LocalConfig.USING_TWISTED:
2153                # start the thread with twisted
2154                thread = threads.deferToThread(calc_thread.compute)
2155                thread.addCallback(completefn)
2156                thread.addErrback(self.calculateDataFailed)
2157            else:
2158                # Use the old python threads + Queue
2159                calc_thread.queue()
2160                calc_thread.ready(2.5)
2161        else:
2162            results = calc_thread.compute()
2163            completefn(results)
2164
2165    def calculateQGridForModel(self):
2166        """
2167        Prepare the fitting data object, based on current ModelModel
2168        """
2169        if self.kernel_module is None:
2170            return
2171        self.calculateQGridForModelExt()
2172
2173    def calculateDataFailed(self, reason):
2174        """
2175        Thread returned error
2176        """
2177        print("Calculate Data failed with ", reason)
2178
2179    def completed1D(self, return_data):
2180        self.Calc1DFinishedSignal.emit(return_data)
2181
2182    def completed2D(self, return_data):
2183        self.Calc2DFinishedSignal.emit(return_data)
2184
2185    def complete1D(self, return_data):
2186        """
2187        Plot the current 1D data
2188        """
2189        fitted_data = self.logic.new1DPlot(return_data, self.tab_id)
2190        self.calculateResiduals(fitted_data)
2191        self.model_data = fitted_data
2192
2193    def complete2D(self, return_data):
2194        """
2195        Plot the current 2D data
2196        """
2197        fitted_data = self.logic.new2DPlot(return_data)
2198        self.calculateResiduals(fitted_data)
2199        self.model_data = fitted_data
2200
2201    def calculateResiduals(self, fitted_data):
2202        """
2203        Calculate and print Chi2 and display chart of residuals
2204        """
2205        # Create a new index for holding data
2206        fitted_data.symbol = "Line"
2207
2208        # Modify fitted_data with weighting
2209        self.addWeightingToData(fitted_data)
2210
2211        self.createNewIndex(fitted_data)
2212        # Calculate difference between return_data and logic.data
2213        self.chi2 = FittingUtilities.calculateChi2(fitted_data, self.logic.data)
2214        # Update the control
2215        chi2_repr = "---" if self.chi2 is None else GuiUtils.formatNumber(self.chi2, high=True)
2216        self.lblChi2Value.setText(chi2_repr)
2217
2218        self.communicate.plotUpdateSignal.emit([fitted_data])
2219
2220        # Plot residuals if actual data
2221        if not self.data_is_loaded:
2222            return
2223
2224        residuals_plot = FittingUtilities.plotResiduals(self.data, fitted_data)
2225        residuals_plot.id = "Residual " + residuals_plot.id
2226        self.createNewIndex(residuals_plot)
2227
2228    def calcException(self, etype, value, tb):
2229        """
2230        Thread threw an exception.
2231        """
2232        # TODO: remimplement thread cancellation
2233        logging.error("".join(traceback.format_exception(etype, value, tb)))
2234
2235    def setTableProperties(self, table):
2236        """
2237        Setting table properties
2238        """
2239        # Table properties
2240        table.verticalHeader().setVisible(False)
2241        table.setAlternatingRowColors(True)
2242        table.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding)
2243        table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
2244        table.resizeColumnsToContents()
2245
2246        # Header
2247        header = table.horizontalHeader()
2248        header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
2249        header.ResizeMode(QtWidgets.QHeaderView.Interactive)
2250
2251        # Qt5: the following 2 lines crash - figure out why!
2252        # Resize column 0 and 7 to content
2253        #header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
2254        #header.setSectionResizeMode(7, QtWidgets.QHeaderView.ResizeToContents)
2255
2256    def setPolyModel(self):
2257        """
2258        Set polydispersity values
2259        """
2260        if not self.model_parameters:
2261            return
2262        self._poly_model.clear()
2263
2264        [self.setPolyModelParameters(i, param) for i, param in \
2265            enumerate(self.model_parameters.form_volume_parameters) if param.polydisperse]
2266        FittingUtilities.addPolyHeadersToModel(self._poly_model)
2267
2268    def setPolyModelParameters(self, i, param):
2269        """
2270        Standard of multishell poly parameter driver
2271        """
2272        param_name = param.name
2273        # see it the parameter is multishell
2274        if '[' in param.name:
2275            # Skip empty shells
2276            if self.current_shell_displayed == 0:
2277                return
2278            else:
2279                # Create as many entries as current shells
2280                for ishell in range(1, self.current_shell_displayed+1):
2281                    # Remove [n] and add the shell numeral
2282                    name = param_name[0:param_name.index('[')] + str(ishell)
2283                    self.addNameToPolyModel(i, name)
2284        else:
2285            # Just create a simple param entry
2286            self.addNameToPolyModel(i, param_name)
2287
2288    def addNameToPolyModel(self, i, param_name):
2289        """
2290        Creates a checked row in the poly model with param_name
2291        """
2292        # Polydisp. values from the sasmodel
2293        width = self.kernel_module.getParam(param_name + '.width')
2294        npts = self.kernel_module.getParam(param_name + '.npts')
2295        nsigs = self.kernel_module.getParam(param_name + '.nsigmas')
2296        _, min, max = self.kernel_module.details[param_name]
2297
2298        # Construct a row with polydisp. related variable.
2299        # This will get added to the polydisp. model
2300        # Note: last argument needs extra space padding for decent display of the control
2301        checked_list = ["Distribution of " + param_name, str(width),
2302                        str(min), str(max),
2303                        str(npts), str(nsigs), "gaussian      ",'']
2304        FittingUtilities.addCheckedListToModel(self._poly_model, checked_list)
2305
2306        # All possible polydisp. functions as strings in combobox
2307        func = QtWidgets.QComboBox()
2308        func.addItems([str(name_disp) for name_disp in POLYDISPERSITY_MODELS.keys()])
2309        # Set the default index
2310        func.setCurrentIndex(func.findText(DEFAULT_POLYDISP_FUNCTION))
2311        ind = self._poly_model.index(i,self.lstPoly.itemDelegate().poly_function)
2312        self.lstPoly.setIndexWidget(ind, func)
2313        func.currentIndexChanged.connect(lambda: self.onPolyComboIndexChange(str(func.currentText()), i))
2314
2315    def onPolyFilenameChange(self, row_index):
2316        """
2317        Respond to filename_updated signal from the delegate
2318        """
2319        # For the given row, invoke the "array" combo handler
2320        array_caption = 'array'
2321
2322        # Get the combo box reference
2323        ind = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_function)
2324        widget = self.lstPoly.indexWidget(ind)
2325
2326        # Update the combo box so it displays "array"
2327        widget.blockSignals(True)
2328        widget.setCurrentIndex(self.lstPoly.itemDelegate().POLYDISPERSE_FUNCTIONS.index(array_caption))
2329        widget.blockSignals(False)
2330
2331        # Invoke the file reader
2332        self.onPolyComboIndexChange(array_caption, row_index)
2333
2334    def onPolyComboIndexChange(self, combo_string, row_index):
2335        """
2336        Modify polydisp. defaults on function choice
2337        """
2338        # Get npts/nsigs for current selection
2339        param = self.model_parameters.form_volume_parameters[row_index]
2340        file_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_function)
2341        combo_box = self.lstPoly.indexWidget(file_index)
2342
2343        def updateFunctionCaption(row):
2344            # Utility function for update of polydispersity function name in the main model
2345            param_name = str(self._model_model.item(row, 0).text())
2346            if param_name !=  param.name:
2347                return
2348            # Modify the param value
2349            self._model_model.item(row, 0).child(0).child(0,4).setText(combo_string)
2350
2351        if combo_string == 'array':
2352            try:
2353                self.loadPolydispArray(row_index)
2354                # Update main model for display
2355                self.iterateOverModel(updateFunctionCaption)
2356                # disable the row
2357                lo = self.lstPoly.itemDelegate().poly_pd
2358                hi = self.lstPoly.itemDelegate().poly_function
2359                [self._poly_model.item(row_index, i).setEnabled(False) for i in range(lo, hi)]
2360                return
2361            except IOError:
2362                combo_box.setCurrentIndex(self.orig_poly_index)
2363                # Pass for cancel/bad read
2364                pass
2365
2366        # Enable the row in case it was disabled by Array
2367        self._poly_model.blockSignals(True)
2368        max_range = self.lstPoly.itemDelegate().poly_filename
2369        [self._poly_model.item(row_index, i).setEnabled(True) for i in range(7)]
2370        file_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_filename)
2371        self._poly_model.setData(file_index, "")
2372        self._poly_model.blockSignals(False)
2373
2374        npts_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_npts)
2375        nsigs_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_nsigs)
2376
2377        npts = POLYDISPERSITY_MODELS[str(combo_string)].default['npts']
2378        nsigs = POLYDISPERSITY_MODELS[str(combo_string)].default['nsigmas']
2379
2380        self._poly_model.setData(npts_index, npts)
2381        self._poly_model.setData(nsigs_index, nsigs)
2382
2383        self.iterateOverModel(updateFunctionCaption)
2384        self.orig_poly_index = combo_box.currentIndex()
2385
2386    def loadPolydispArray(self, row_index):
2387        """
2388        Show the load file dialog and loads requested data into state
2389        """
2390        datafile = QtWidgets.QFileDialog.getOpenFileName(
2391            self, "Choose a weight file", "", "All files (*.*)", None,
2392            QtWidgets.QFileDialog.DontUseNativeDialog)[0]
2393
2394        if not datafile:
2395            logging.info("No weight data chosen.")
2396            raise IOError
2397
2398        values = []
2399        weights = []
2400        def appendData(data_tuple):
2401            """
2402            Fish out floats from a tuple of strings
2403            """
2404            try:
2405                values.append(float(data_tuple[0]))
2406                weights.append(float(data_tuple[1]))
2407            except (ValueError, IndexError):
2408                # just pass through if line with bad data
2409                return
2410
2411        with open(datafile, 'r') as column_file:
2412            column_data = [line.rstrip().split() for line in column_file.readlines()]
2413            [appendData(line) for line in column_data]
2414
2415        # If everything went well - update the sasmodel values
2416        self.disp_model = POLYDISPERSITY_MODELS['array']()
2417        self.disp_model.set_weights(np.array(values), np.array(weights))
2418        # + update the cell with filename
2419        fname = os.path.basename(str(datafile))
2420        fname_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_filename)
2421        self._poly_model.setData(fname_index, fname)
2422
2423    def setMagneticModel(self):
2424        """
2425        Set magnetism values on model
2426        """
2427        if not self.model_parameters:
2428            return
2429        self._magnet_model.clear()
2430        [self.addCheckedMagneticListToModel(param, self._magnet_model) for param in \
2431            self.model_parameters.call_parameters if param.type == 'magnetic']
2432        FittingUtilities.addHeadersToModel(self._magnet_model)
2433
2434    def shellNamesList(self):
2435        """
2436        Returns list of names of all multi-shell parameters
2437        E.g. for sld[n], radius[n], n=1..3 it will return
2438        [sld1, sld2, sld3, radius1, radius2, radius3]
2439        """
2440        multi_names = [p.name[:p.name.index('[')] for p in self.model_parameters.iq_parameters if '[' in p.name]
2441        top_index = self.kernel_module.multiplicity_info.number
2442        shell_names = []
2443        for i in range(1, top_index+1):
2444            for name in multi_names:
2445                shell_names.append(name+str(i))
2446        return shell_names
2447
2448    def addCheckedMagneticListToModel(self, param, model):
2449        """
2450        Wrapper for model update with a subset of magnetic parameters
2451        """
2452        if param.name[param.name.index(':')+1:] in self.shell_names:
2453            # check if two-digit shell number
2454            try:
2455                shell_index = int(param.name[-2:])
2456            except ValueError:
2457                shell_index = int(param.name[-1:])
2458
2459            if shell_index > self.current_shell_displayed:
2460                return
2461
2462        checked_list = [param.name,
2463                        str(param.default),
2464                        str(param.limits[0]),
2465                        str(param.limits[1]),
2466                        param.units]
2467
2468        FittingUtilities.addCheckedListToModel(model, checked_list)
2469
2470    def enableStructureFactorControl(self, structure_factor):
2471        """
2472        Add structure factors to the list of parameters
2473        """
2474        if self.kernel_module.is_form_factor or structure_factor == 'None':
2475            self.enableStructureCombo()
2476        else:
2477            self.disableStructureCombo()
2478
2479    def addExtraShells(self):
2480        """
2481        Add a combobox for multiple shell display
2482        """
2483        param_name, param_length = FittingUtilities.getMultiplicity(self.model_parameters)
2484
2485        if param_length == 0:
2486            return
2487
2488        # cell 1: variable name
2489        item1 = QtGui.QStandardItem(param_name)
2490
2491        func = QtWidgets.QComboBox()
2492        # Available range of shells displayed in the combobox
2493        func.addItems([str(i) for i in range(param_length+1)])
2494
2495        # Respond to index change
2496        func.currentIndexChanged.connect(self.modifyShellsInList)
2497
2498        # cell 2: combobox
2499        item2 = QtGui.QStandardItem()
2500        self._model_model.appendRow([item1, item2])
2501
2502        # Beautify the row:  span columns 2-4
2503        shell_row = self._model_model.rowCount()
2504        shell_index = self._model_model.index(shell_row-1, 1)
2505
2506        self.lstParams.setIndexWidget(shell_index, func)
2507        self._last_model_row = self._model_model.rowCount()
2508
2509        # Set the index to the state-kept value
2510        func.setCurrentIndex(self.current_shell_displayed
2511                             if self.current_shell_displayed < func.count() else 0)
2512
2513    def modifyShellsInList(self, index):
2514        """
2515        Add/remove additional multishell parameters
2516        """
2517        # Find row location of the combobox
2518        last_row = self._last_model_row
2519        remove_rows = self._model_model.rowCount() - last_row
2520
2521        if remove_rows > 1:
2522            self._model_model.removeRows(last_row, remove_rows)
2523
2524        FittingUtilities.addShellsToModel(self.model_parameters, self._model_model, index)
2525        self.current_shell_displayed = index
2526
2527        # Update relevant models
2528        self.setPolyModel()
2529        self.setMagneticModel()
2530
2531    def setFittingStarted(self):
2532        """
2533        Set buttion caption on fitting start
2534        """
2535        # Notify the user that fitting is being run
2536        # Allow for stopping the job
2537        self.cmdFit.setStyleSheet('QPushButton {color: red;}')
2538        self.cmdFit.setText('Stop fit')
2539
2540    def setFittingStopped(self):
2541        """
2542        Set button caption on fitting stop
2543        """
2544        # Notify the user that fitting is available
2545        self.cmdFit.setStyleSheet('QPushButton {color: black;}')
2546        self.cmdFit.setText("Fit")
2547        self.fit_started = False
2548
2549    def readFitPage(self, fp):
2550        """
2551        Read in state from a fitpage object and update GUI
2552        """
2553        assert isinstance(fp, FitPage)
2554        # Main tab info
2555        self.logic.data.filename = fp.filename
2556        self.data_is_loaded = fp.data_is_loaded
2557        self.chkPolydispersity.setCheckState(fp.is_polydisperse)
2558        self.chkMagnetism.setCheckState(fp.is_magnetic)
2559        self.chk2DView.setCheckState(fp.is2D)
2560
2561        # Update the comboboxes
2562        self.cbCategory.setCurrentIndex(self.cbCategory.findText(fp.current_category))
2563        self.cbModel.setCurrentIndex(self.cbModel.findText(fp.current_model))
2564        if fp.current_factor:
2565            self.cbStructureFactor.setCurrentIndex(self.cbStructureFactor.findText(fp.current_factor))
2566
2567        self.chi2 = fp.chi2
2568
2569        # Options tab
2570        self.q_range_min = fp.fit_options[fp.MIN_RANGE]
2571        self.q_range_max = fp.fit_options[fp.MAX_RANGE]
2572        self.npts = fp.fit_options[fp.NPTS]
2573        self.log_points = fp.fit_options[fp.LOG_POINTS]
2574        self.weighting = fp.fit_options[fp.WEIGHTING]
2575
2576        # Models
2577        self._model_model = fp.model_model
2578        self._poly_model = fp.poly_model
2579        self._magnet_model = fp.magnetism_model
2580
2581        # Resolution tab
2582        smearing = fp.smearing_options[fp.SMEARING_OPTION]
2583        accuracy = fp.smearing_options[fp.SMEARING_ACCURACY]
2584        smearing_min = fp.smearing_options[fp.SMEARING_MIN]
2585        smearing_max = fp.smearing_options[fp.SMEARING_MAX]
2586        self.smearing_widget.setState(smearing, accuracy, smearing_min, smearing_max)
2587
2588        # TODO: add polidyspersity and magnetism
2589
2590    def saveToFitPage(self, fp):
2591        """
2592        Write current state to the given fitpage
2593        """
2594        assert isinstance(fp, FitPage)
2595
2596        # Main tab info
2597        fp.filename = self.logic.data.filename
2598        fp.data_is_loaded = self.data_is_loaded
2599        fp.is_polydisperse = self.chkPolydispersity.isChecked()
2600        fp.is_magnetic = self.chkMagnetism.isChecked()
2601        fp.is2D = self.chk2DView.isChecked()
2602        fp.data = self.data
2603
2604        # Use current models - they contain all the required parameters
2605        fp.model_model = self._model_model
2606        fp.poly_model = self._poly_model
2607        fp.magnetism_model = self._magnet_model
2608
2609        if self.cbCategory.currentIndex() != 0:
2610            fp.current_category = str(self.cbCategory.currentText())
2611            fp.current_model = str(self.cbModel.currentText())
2612
2613        if self.cbStructureFactor.isEnabled() and self.cbStructureFactor.currentIndex() != 0:
2614            fp.current_factor = str(self.cbStructureFactor.currentText())
2615        else:
2616            fp.current_factor = ''
2617
2618        fp.chi2 = self.chi2
2619        fp.parameters_to_fit = self.parameters_to_fit
2620        fp.kernel_module = self.kernel_module
2621
2622        # Algorithm options
2623        # fp.algorithm = self.parent.fit_options.selected_id
2624
2625        # Options tab
2626        fp.fit_options[fp.MIN_RANGE] = self.q_range_min
2627        fp.fit_options[fp.MAX_RANGE] = self.q_range_max
2628        fp.fit_options[fp.NPTS] = self.npts
2629        #fp.fit_options[fp.NPTS_FIT] = self.npts_fit
2630        fp.fit_options[fp.LOG_POINTS] = self.log_points
2631        fp.fit_options[fp.WEIGHTING] = self.weighting
2632
2633        # Resolution tab
2634        smearing, accuracy, smearing_min, smearing_max = self.smearing_widget.state()
2635        fp.smearing_options[fp.SMEARING_OPTION] = smearing
2636        fp.smearing_options[fp.SMEARING_ACCURACY] = accuracy
2637        fp.smearing_options[fp.SMEARING_MIN] = smearing_min
2638        fp.smearing_options[fp.SMEARING_MAX] = smearing_max
2639
2640        # TODO: add polidyspersity and magnetism
2641
2642
2643    def updateUndo(self):
2644        """
2645        Create a new state page and add it to the stack
2646        """
2647        if self.undo_supported:
2648            self.pushFitPage(self.currentState())
2649
2650    def currentState(self):
2651        """
2652        Return fit page with current state
2653        """
2654        new_page = FitPage()
2655        self.saveToFitPage(new_page)
2656
2657        return new_page
2658
2659    def pushFitPage(self, new_page):
2660        """
2661        Add a new fit page object with current state
2662        """
2663        self.page_stack.append(new_page)
2664
2665    def popFitPage(self):
2666        """
2667        Remove top fit page from stack
2668        """
2669        if self.page_stack:
2670            self.page_stack.pop()
2671
2672    def getReport(self):
2673        """
2674        Create and return HTML report with parameters and charts
2675        """
2676        index = None
2677        if self.all_data:
2678            index = self.all_data[self.data_index]
2679        report_logic = ReportPageLogic(self,
2680                                       kernel_module=self.kernel_module,
2681                                       data=self.data,
2682                                       index=index,
2683                                       model=self._model_model)
2684
2685        return report_logic.reportList()
2686
2687    def savePageState(self):
2688        """
2689        Create and serialize local PageState
2690        """
2691        from sas.sascalc.fit.pagestate import Reader
2692        model = self.kernel_module
2693
2694        # Old style PageState object
2695        state = PageState(model=model, data=self.data)
2696
2697        # Add parameter data to the state
2698        self.getCurrentFitState(state)
2699
2700        # Create the filewriter, aptly named 'Reader'
2701        state_reader = Reader(self.loadPageStateCallback)
2702        filepath = self.saveAsAnalysisFile()
2703        if filepath is None:
2704            return
2705        state_reader.write(filename=filepath, fitstate=state)
2706        pass
2707
2708    def saveAsAnalysisFile(self):
2709        """
2710        Show the save as... dialog and return the chosen filepath
2711        """
2712        default_name = "FitPage"+str(self.tab_id)+".fitv"
2713
2714        wildcard = "fitv files (*.fitv)"
2715        kwargs = {
2716            'caption'   : 'Save As',
2717            'directory' : default_name,
2718            'filter'    : wildcard,
2719            'parent'    : None,
2720        }
2721        # Query user for filename.
2722        filename_tuple = QtWidgets.QFileDialog.getSaveFileName(**kwargs)
2723        filename = filename_tuple[0]
2724        return filename
2725
2726    def loadPageStateCallback(self,state=None, datainfo=None, format=None):
2727        """
2728        This is a callback method called from the CANSAS reader.
2729        We need the instance of this reader only for writing out a file,
2730        so there's nothing here.
2731        Until Load Analysis is implemented, that is.
2732        """
2733        pass
2734
2735    def loadPageState(self, pagestate=None):
2736        """
2737        Load the PageState object and update the current widget
2738        """
2739        pass
2740
2741    def getCurrentFitState(self, state=None):
2742        """
2743        Store current state for fit_page
2744        """
2745        # save model option
2746        #if self.model is not None:
2747        #    self.disp_list = self.getDispParamList()
2748        #    state.disp_list = copy.deepcopy(self.disp_list)
2749        #    #state.model = self.model.clone()
2750
2751        # Comboboxes
2752        state.categorycombobox = self.cbCategory.currentText()
2753        state.formfactorcombobox = self.cbModel.currentText()
2754        if self.cbStructureFactor.isEnabled():
2755            state.structureCombobox = self.cbStructureFactor.currentText()
2756        state.tcChi = self.chi2
2757
2758        state.enable2D = self.is2D
2759
2760        #state.weights = copy.deepcopy(self.weights)
2761        # save data
2762        state.data = copy.deepcopy(self.data)
2763
2764        # save plotting range
2765        state.qmin = self.q_range_min
2766        state.qmax = self.q_range_max
2767        state.npts = self.npts
2768
2769        #    self.state.enable_disp = self.enable_disp.GetValue()
2770        #    self.state.disable_disp = self.disable_disp.GetValue()
2771
2772        #    self.state.enable_smearer = \
2773        #                        copy.deepcopy(self.enable_smearer.GetValue())
2774        #    self.state.disable_smearer = \
2775        #                        copy.deepcopy(self.disable_smearer.GetValue())
2776
2777        #self.state.pinhole_smearer = \
2778        #                        copy.deepcopy(self.pinhole_smearer.GetValue())
2779        #self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue())
2780        #self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue())
2781        #self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue())
2782        #self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue())
2783        #self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue())
2784
2785        p = self.model_parameters
2786        # save checkbutton state and txtcrtl values
2787        state.parameters = FittingUtilities.getStandardParam()
2788        state.orientation_params_disp = FittingUtilities.getOrientationParam()
2789
2790        #self._copy_parameters_state(self.orientation_params_disp, self.state.orientation_params_disp)
2791        #self._copy_parameters_state(self.parameters, self.state.parameters)
2792        #self._copy_parameters_state(self.fittable_param, self.state.fittable_param)
2793        #self._copy_parameters_state(self.fixed_param, self.state.fixed_param)
2794
2795    def onParameterCopy(self, format=None):
2796        """
2797        Copy current parameters into the clipboard
2798        """
2799        # run a loop over all parameters and pull out
2800        # first - regular params
2801        param_list = []
2802        def gatherParams(row):
2803            """
2804            Create list of main parameters based on _model_model
2805            """
2806            param_name = str(self._model_model.item(row, 0).text())
2807            param_checked = str(self._model_model.item(row, 0).checkState() == QtCore.Qt.Checked)
2808            param_value = str(self._model_model.item(row, 1).text())
2809            param_error = None
2810            column_offset = 0
2811            if self.has_error_column:
2812                param_error = str(self._model_model.item(row, 2).text())
2813                column_offset = 1
2814            param_min = str(self._model_model.item(row, 2+column_offset).text())
2815            param_max = str(self._model_model.item(row, 3+column_offset).text())
2816            param_list.append([param_name, param_checked, param_value, param_error, param_min, param_max])
2817
2818        def gatherPolyParams(row):
2819            """
2820            Create list of polydisperse parameters based on _poly_model
2821            """
2822            param_name = str(self._poly_model.item(row, 0).text()).split()[-1]
2823            param_checked = str(self._poly_model.item(row, 0).checkState() == QtCore.Qt.Checked)
2824            param_value = str(self._poly_model.item(row, 1).text())
2825            param_error = None
2826            column_offset = 0
2827            if self.has_poly_error_column:
2828                param_error = str(self._poly_model.item(row, 2).text())
2829                column_offset = 1
2830            param_min   = str(self._poly_model.item(row, 2+column_offset).text())
2831            param_max   = str(self._poly_model.item(row, 3+column_offset).text())
2832            param_npts  = str(self._poly_model.item(row, 4+column_offset).text())
2833            param_nsigs = str(self._poly_model.item(row, 5+column_offset).text())
2834            param_fun   = str(self._poly_model.item(row, 6+column_offset).text()).rstrip()
2835            # width
2836            name = param_name+".width"
2837            param_list.append([name, param_checked, param_value, param_error,
2838                                param_npts, param_nsigs, param_min, param_max, param_fun])
2839
2840        def gatherMagnetParams(row):
2841            """
2842            Create list of magnetic parameters based on _magnet_model
2843            """
2844            param_name = str(self._magnet_model.item(row, 0).text())
2845            param_checked = str(self._magnet_model.item(row, 0).checkState() == QtCore.Qt.Checked)
2846            param_value = str(self._magnet_model.item(row, 1).text())
2847            param_error = None
2848            column_offset = 0
2849            if self.has_magnet_error_column:
2850                param_error = str(self._magnet_model.item(row, 2).text())
2851                column_offset = 1
2852            param_min = str(self._magnet_model.item(row, 2+column_offset).text())
2853            param_max = str(self._magnet_model.item(row, 3+column_offset).text())
2854            param_list.append([param_name, param_checked, param_value, param_error, param_min, param_max])
2855
2856        self.iterateOverModel(gatherParams)
2857        if self.chkPolydispersity.isChecked():
2858            self.iterateOverPolyModel(gatherPolyParams)
2859        if self.chkMagnetism.isChecked() and self.chkMagnetism.isEnabled():
2860            self.iterateOverMagnetModel(sgatherMagnetParams)
2861
2862        if format=="":
2863            formatted_output = FittingUtilities.formatParameters(param_list)
2864        elif format == "Excel":
2865            formatted_output = FittingUtilities.formatParametersExcel(param_list)
2866        elif format == "Latex":
2867            formatted_output = FittingUtilities.formatParametersLatex(param_list)
2868        else:
2869            raise AttributeError("Bad format specifier.")
2870
2871        # Dump formatted_output to the clipboard
2872        cb = QtWidgets.QApplication.clipboard()
2873        cb.setText(formatted_output)
2874
2875    def onParameterPaste(self):
2876        """
2877        Use the clipboard to update fit state
2878        """
2879        # Check if the clipboard contains right stuff
2880        cb = QtWidgets.QApplication.clipboard()
2881        cb_text = cb.text()
2882
2883        context = {}
2884        # put the text into dictionary
2885        lines = cb_text.split(':')
2886        if lines[0] != 'sasview_parameter_values':
2887            return False
2888        for line in lines[1:-1]:
2889            if len(line) != 0:
2890                item = line.split(',')
2891                check = item[1]
2892                name = item[0]
2893                value = item[2]
2894                # Transfer the text to content[dictionary]
2895                context[name] = [check, value]
2896
2897                # limits
2898                limit_lo = item[3]
2899                context[name].append(limit_lo)
2900                limit_hi = item[4]
2901                context[name].append(limit_hi)
2902
2903                # Polydisp
2904                if len(item) > 5:
2905                    value = item[5]
2906                    context[name].append(value)
2907                    try:
2908                        value = item[6]
2909                        context[name].append(value)
2910                        value = item[7]
2911                        context[name].append(value)
2912                    except IndexError:
2913                        pass
2914
2915        self.updateFullModel(context)
2916        self.updateFullPolyModel(context)
2917
2918    def updateFullModel(self, param_dict):
2919        """
2920        Update the model with new parameters
2921        """
2922        assert isinstance(param_dict, dict)
2923        if not dict:
2924            return
2925
2926        def updateFittedValues(row):
2927            # Utility function for main model update
2928            # internal so can use closure for param_dict
2929            param_name = str(self._model_model.item(row, 0).text())
2930            if param_name not in list(param_dict.keys()):
2931                return
2932            # checkbox state
2933            param_checked = QtCore.Qt.Checked if param_dict[param_name][0] == "True" else QtCore.Qt.Unchecked
2934            self._model_model.item(row, 0).setCheckState(param_checked)
2935
2936            # modify the param value
2937            param_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
2938            self._model_model.item(row, 1).setText(param_repr)
2939
2940            # Potentially the error column
2941            ioffset = 0
2942            if len(param_dict[param_name])>4 and self.has_error_column:
2943                # error values are not editable - no need to update
2944                #error_repr = GuiUtils.formatNumber(param_dict[param_name][2], high=True)
2945                #self._model_model.item(row, 2).setText(error_repr)
2946                ioffset = 1
2947            # min/max
2948            param_repr = GuiUtils.formatNumber(param_dict[param_name][2+ioffset], high=True)
2949            self._model_model.item(row, 2+ioffset).setText(param_repr)
2950            param_repr = GuiUtils.formatNumber(param_dict[param_name][3+ioffset], high=True)
2951            self._model_model.item(row, 3+ioffset).setText(param_repr)
2952
2953        # block signals temporarily, so we don't end up
2954        # updating charts with every single model change on the end of fitting
2955        self._model_model.blockSignals(True)
2956        self.iterateOverModel(updateFittedValues)
2957        self._model_model.blockSignals(False)
2958
2959    def updateFullPolyModel(self, param_dict):
2960        """
2961        Update the polydispersity model with new parameters, create the errors column
2962        """
2963        assert isinstance(param_dict, dict)
2964        if not dict:
2965            return
2966
2967        def updateFittedValues(row):
2968            # Utility function for main model update
2969            # internal so can use closure for param_dict
2970            if row >= self._poly_model.rowCount():
2971                return
2972            param_name = str(self._poly_model.item(row, 0).text()).rsplit()[-1] + '.width'
2973            if param_name not in list(param_dict.keys()):
2974                return
2975            # checkbox state
2976            param_checked = QtCore.Qt.Checked if param_dict[param_name][0] == "True" else QtCore.Qt.Unchecked
2977            self._poly_model.item(row,0).setCheckState(param_checked)
2978
2979            # modify the param value
2980            param_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True)
2981            self._poly_model.item(row, 1).setText(param_repr)
2982
2983            # Potentially the error column
2984            ioffset = 0
2985            if len(param_dict[param_name])>4 and self.has_poly_error_column:
2986                ioffset = 1
2987            # min
2988            param_repr = GuiUtils.formatNumber(param_dict[param_name][2+ioffset], high=True)
2989            self._poly_model.item(row, 2+ioffset).setText(param_repr)
2990            # max
2991            param_repr = GuiUtils.formatNumber(param_dict[param_name][3+ioffset], high=True)
2992            self._poly_model.item(row, 3+ioffset).setText(param_repr)
2993            # Npts
2994            param_repr = GuiUtils.formatNumber(param_dict[param_name][4+ioffset], high=True)
2995            self._poly_model.item(row, 4+ioffset).setText(param_repr)
2996            # Nsigs
2997            param_repr = GuiUtils.formatNumber(param_dict[param_name][5+ioffset], high=True)
2998            self._poly_model.item(row, 5+ioffset).setText(param_repr)
2999
3000            param_repr = GuiUtils.formatNumber(param_dict[param_name][5+ioffset], high=True)
3001            self._poly_model.item(row, 5+ioffset).setText(param_repr)
3002
3003        # block signals temporarily, so we don't end up
3004        # updating charts with every single model change on the end of fitting
3005        self._poly_model.blockSignals(True)
3006        self.iterateOverPolyModel(updateFittedValues)
3007        self._poly_model.blockSignals(False)
3008
3009
Note: See TracBrowser for help on using the repository browser.