[60af928] | 1 | import json |
---|
[cd31251] | 2 | import os |
---|
[60af928] | 3 | from collections import defaultdict |
---|
[b3e8629] | 4 | |
---|
[d4dac80] | 5 | import copy |
---|
[5236449] | 6 | import logging |
---|
| 7 | import traceback |
---|
[cbcdd2c] | 8 | from twisted.internet import threads |
---|
[1bc27f1] | 9 | import numpy as np |
---|
[e90988c] | 10 | import webbrowser |
---|
[5236449] | 11 | |
---|
[4992ff2] | 12 | from PyQt5 import QtCore |
---|
| 13 | from PyQt5 import QtGui |
---|
| 14 | from PyQt5 import QtWidgets |
---|
[60af928] | 15 | |
---|
| 16 | from sasmodels import generate |
---|
| 17 | from sasmodels import modelinfo |
---|
[5236449] | 18 | from sasmodels.sasview_model import load_standard_models |
---|
[18d5c94a] | 19 | from sasmodels.sasview_model import MultiplicationModel |
---|
[358b39d] | 20 | from sasmodels.weights import MODELS as POLYDISPERSITY_MODELS |
---|
| 21 | |
---|
[f182f93] | 22 | from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit |
---|
[57be490] | 23 | from sas.sascalc.fit.pagestate import PageState |
---|
[5236449] | 24 | |
---|
[83eb5208] | 25 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[14ec91c5] | 26 | import sas.qtgui.Utilities.LocalConfig as LocalConfig |
---|
[dc5ef15] | 27 | from sas.qtgui.Utilities.CategoryInstaller import CategoryInstaller |
---|
| 28 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 29 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[86d3207] | 30 | from sas.qtgui.Plotting.Plotter import PlotterWidget |
---|
[5236449] | 31 | |
---|
[1bc27f1] | 32 | from sas.qtgui.Perspectives.Fitting.UI.FittingWidgetUI import Ui_FittingWidgetUI |
---|
[dc5ef15] | 33 | from sas.qtgui.Perspectives.Fitting.FitThread import FitThread |
---|
[7adc2a8] | 34 | from sas.qtgui.Perspectives.Fitting.ConsoleUpdate import ConsoleUpdate |
---|
| 35 | |
---|
[dc5ef15] | 36 | from sas.qtgui.Perspectives.Fitting.ModelThread import Calc1D |
---|
| 37 | from sas.qtgui.Perspectives.Fitting.ModelThread import Calc2D |
---|
[4d457df] | 38 | from sas.qtgui.Perspectives.Fitting.FittingLogic import FittingLogic |
---|
| 39 | from sas.qtgui.Perspectives.Fitting import FittingUtilities |
---|
[3b3b40b] | 40 | from sas.qtgui.Perspectives.Fitting import ModelUtilities |
---|
[1bc27f1] | 41 | from sas.qtgui.Perspectives.Fitting.SmearingWidget import SmearingWidget |
---|
| 42 | from sas.qtgui.Perspectives.Fitting.OptionsWidget import OptionsWidget |
---|
| 43 | from sas.qtgui.Perspectives.Fitting.FitPage import FitPage |
---|
[ad6b4e2] | 44 | from sas.qtgui.Perspectives.Fitting.ViewDelegate import ModelViewDelegate |
---|
[6011788] | 45 | from sas.qtgui.Perspectives.Fitting.ViewDelegate import PolyViewDelegate |
---|
[b00414d] | 46 | from sas.qtgui.Perspectives.Fitting.ViewDelegate import MagnetismViewDelegate |
---|
[14ec91c5] | 47 | from sas.qtgui.Perspectives.Fitting.Constraint import Constraint |
---|
[eae226b] | 48 | from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint |
---|
[57be490] | 49 | from sas.qtgui.Perspectives.Fitting.ReportPageLogic import ReportPageLogic |
---|
| 50 | |
---|
[60af928] | 51 | TAB_MAGNETISM = 4 |
---|
| 52 | TAB_POLY = 3 |
---|
[cbcdd2c] | 53 | CATEGORY_DEFAULT = "Choose category..." |
---|
[2eeda93] | 54 | MODEL_DEFAULT = "Choose model..." |
---|
[4d457df] | 55 | CATEGORY_STRUCTURE = "Structure Factor" |
---|
[3b3b40b] | 56 | CATEGORY_CUSTOM = "Plugin Models" |
---|
[351b53e] | 57 | STRUCTURE_DEFAULT = "None" |
---|
[60af928] | 58 | |
---|
[358b39d] | 59 | DEFAULT_POLYDISP_FUNCTION = 'gaussian' |
---|
| 60 | |
---|
[aa82f54] | 61 | # CRUFT: remove when new release of sasmodels is available |
---|
| 62 | # https://github.com/SasView/sasview/pull/181#discussion_r218135162 |
---|
| 63 | from sasmodels.sasview_model import SasviewModel |
---|
| 64 | if not hasattr(SasviewModel, 'get_weights'): |
---|
| 65 | def get_weights(self, name): |
---|
[a8b3e2c] | 66 | """ |
---|
| 67 | Returns the polydispersity distribution for parameter *name* as *value* and *weight* arrays. |
---|
| 68 | """ |
---|
| 69 | # type: (str) -> Tuple(np.ndarray, np.ndarray) |
---|
| 70 | _, x, w = self._get_weights(self._model_info.parameters[name]) |
---|
| 71 | return x, w |
---|
[aa82f54] | 72 | |
---|
| 73 | SasviewModel.get_weights = get_weights |
---|
[7adc2a8] | 74 | |
---|
[dc71408] | 75 | logger = logging.getLogger(__name__) |
---|
| 76 | |
---|
[13cd397] | 77 | class ToolTippedItemModel(QtGui.QStandardItemModel): |
---|
[f54ce30] | 78 | """ |
---|
| 79 | Subclass from QStandardItemModel to allow displaying tooltips in |
---|
| 80 | QTableView model. |
---|
| 81 | """ |
---|
[d0dfcb2] | 82 | def __init__(self, parent=None): |
---|
[8e2cd79] | 83 | QtGui.QStandardItemModel.__init__(self, parent) |
---|
[13cd397] | 84 | |
---|
[fbfc488] | 85 | def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole): |
---|
[f54ce30] | 86 | """ |
---|
| 87 | Displays tooltip for each column's header |
---|
| 88 | :param section: |
---|
| 89 | :param orientation: |
---|
| 90 | :param role: |
---|
| 91 | :return: |
---|
| 92 | """ |
---|
[13cd397] | 93 | if role == QtCore.Qt.ToolTipRole: |
---|
| 94 | if orientation == QtCore.Qt.Horizontal: |
---|
[b3e8629] | 95 | return str(self.header_tooltips[section]) |
---|
[ca7c6bd] | 96 | |
---|
[a95c44b] | 97 | return QtGui.QStandardItemModel.headerData(self, section, orientation, role) |
---|
[13cd397] | 98 | |
---|
[4992ff2] | 99 | class FittingWidget(QtWidgets.QWidget, Ui_FittingWidgetUI): |
---|
[60af928] | 100 | """ |
---|
[f46f6dc] | 101 | Main widget for selecting form and structure factor models |
---|
[60af928] | 102 | """ |
---|
[be8f4b0] | 103 | constraintAddedSignal = QtCore.pyqtSignal(list) |
---|
| 104 | newModelSignal = QtCore.pyqtSignal() |
---|
[3b3b40b] | 105 | fittingFinishedSignal = QtCore.pyqtSignal(tuple) |
---|
| 106 | batchFittingFinishedSignal = QtCore.pyqtSignal(tuple) |
---|
[dcabba7] | 107 | Calc1DFinishedSignal = QtCore.pyqtSignal(dict) |
---|
| 108 | Calc2DFinishedSignal = QtCore.pyqtSignal(dict) |
---|
[3b3b40b] | 109 | |
---|
[672c0e97] | 110 | MAGNETIC_MODELS = ['sphere', 'core_shell_sphere', 'core_multi_shell', 'cylinder', 'parallelepiped'] |
---|
| 111 | |
---|
[1bc27f1] | 112 | def __init__(self, parent=None, data=None, tab_id=1): |
---|
[60af928] | 113 | |
---|
| 114 | super(FittingWidget, self).__init__() |
---|
| 115 | |
---|
[86f88d1] | 116 | # Necessary globals |
---|
[cbcdd2c] | 117 | self.parent = parent |
---|
[2a432e7] | 118 | |
---|
| 119 | # Which tab is this widget displayed in? |
---|
| 120 | self.tab_id = tab_id |
---|
| 121 | |
---|
[d2007a8] | 122 | import sys |
---|
| 123 | sys.excepthook = self.info |
---|
| 124 | |
---|
[2a432e7] | 125 | # Globals |
---|
| 126 | self.initializeGlobals() |
---|
| 127 | |
---|
[d4dac80] | 128 | # data index for the batch set |
---|
| 129 | self.data_index = 0 |
---|
| 130 | # Main Data[12]D holders |
---|
| 131 | # Logics.data contains a single Data1D/Data2D object |
---|
| 132 | self._logic = [FittingLogic()] |
---|
| 133 | |
---|
[2a432e7] | 134 | # Main GUI setup up |
---|
| 135 | self.setupUi(self) |
---|
| 136 | self.setWindowTitle("Fitting") |
---|
| 137 | |
---|
| 138 | # Set up tabs widgets |
---|
| 139 | self.initializeWidgets() |
---|
| 140 | |
---|
| 141 | # Set up models and views |
---|
| 142 | self.initializeModels() |
---|
| 143 | |
---|
| 144 | # Defaults for the structure factors |
---|
| 145 | self.setDefaultStructureCombo() |
---|
| 146 | |
---|
| 147 | # Make structure factor and model CBs disabled |
---|
| 148 | self.disableModelCombo() |
---|
| 149 | self.disableStructureCombo() |
---|
| 150 | |
---|
| 151 | # Generate the category list for display |
---|
| 152 | self.initializeCategoryCombo() |
---|
| 153 | |
---|
| 154 | # Initial control state |
---|
| 155 | self.initializeControls() |
---|
| 156 | |
---|
[ebfe223] | 157 | QtWidgets.QApplication.processEvents() |
---|
| 158 | |
---|
| 159 | # Connect signals to controls |
---|
| 160 | self.initializeSignals() |
---|
| 161 | |
---|
[d4dac80] | 162 | if data is not None: |
---|
| 163 | self.data = data |
---|
[2a432e7] | 164 | |
---|
[457d961] | 165 | # New font to display angstrom symbol |
---|
| 166 | new_font = 'font-family: -apple-system, "Helvetica Neue", "Ubuntu";' |
---|
| 167 | self.label_17.setStyleSheet(new_font) |
---|
| 168 | self.label_19.setStyleSheet(new_font) |
---|
| 169 | |
---|
[d2007a8] | 170 | def info(self, type, value, tb): |
---|
| 171 | logger.error("SasView threw exception: " + str(value)) |
---|
| 172 | traceback.print_exception(type, value, tb) |
---|
| 173 | |
---|
[d4dac80] | 174 | @property |
---|
| 175 | def logic(self): |
---|
| 176 | # make sure the logic contains at least one element |
---|
[8e2cd79] | 177 | assert self._logic |
---|
[d4dac80] | 178 | # logic connected to the currently shown data |
---|
| 179 | return self._logic[self.data_index] |
---|
[2a432e7] | 180 | |
---|
| 181 | @property |
---|
| 182 | def data(self): |
---|
| 183 | return self.logic.data |
---|
| 184 | |
---|
| 185 | @data.setter |
---|
| 186 | def data(self, value): |
---|
| 187 | """ data setter """ |
---|
[f7d14a1] | 188 | # Value is either a list of indices for batch fitting or a simple index |
---|
| 189 | # for standard fitting. Assure we have a list, regardless. |
---|
[ee18d33] | 190 | if isinstance(value, list): |
---|
| 191 | self.is_batch_fitting = True |
---|
| 192 | else: |
---|
| 193 | value = [value] |
---|
| 194 | |
---|
| 195 | assert isinstance(value[0], QtGui.QStandardItem) |
---|
| 196 | |
---|
| 197 | # Keep reference to all datasets for batch |
---|
| 198 | self.all_data = value |
---|
[2a432e7] | 199 | |
---|
[d4dac80] | 200 | # Create logics with data items |
---|
[f7d14a1] | 201 | # Logics.data contains only a single Data1D/Data2D object |
---|
[87dfca4] | 202 | if len(value) == 1: |
---|
| 203 | # single data logic is already defined, update data on it |
---|
| 204 | self._logic[0].data = GuiUtils.dataFromItem(value[0]) |
---|
| 205 | else: |
---|
| 206 | # batch datasets |
---|
[54492dc] | 207 | self._logic = [] |
---|
[87dfca4] | 208 | for data_item in value: |
---|
| 209 | logic = FittingLogic(data=GuiUtils.dataFromItem(data_item)) |
---|
| 210 | self._logic.append(logic) |
---|
[2a432e7] | 211 | |
---|
| 212 | # Overwrite data type descriptor |
---|
[339e22b] | 213 | |
---|
[2a432e7] | 214 | self.is2D = True if isinstance(self.logic.data, Data2D) else False |
---|
| 215 | |
---|
[f7d14a1] | 216 | # Let others know we're full of data now |
---|
[2a432e7] | 217 | self.data_is_loaded = True |
---|
| 218 | |
---|
| 219 | # Enable/disable UI components |
---|
| 220 | self.setEnablementOnDataLoad() |
---|
| 221 | |
---|
| 222 | def initializeGlobals(self): |
---|
| 223 | """ |
---|
| 224 | Initialize global variables used in this class |
---|
| 225 | """ |
---|
[cbcdd2c] | 226 | # SasModel is loaded |
---|
[60af928] | 227 | self.model_is_loaded = False |
---|
[cbcdd2c] | 228 | # Data[12]D passed and set |
---|
[5236449] | 229 | self.data_is_loaded = False |
---|
[ee18d33] | 230 | # Batch/single fitting |
---|
| 231 | self.is_batch_fitting = False |
---|
[7fd20fc] | 232 | self.is_chain_fitting = False |
---|
[ded5e77] | 233 | # Is the fit job running? |
---|
[8e2cd79] | 234 | self.fit_started = False |
---|
[ded5e77] | 235 | # The current fit thread |
---|
| 236 | self.calc_fit = None |
---|
[cbcdd2c] | 237 | # Current SasModel in view |
---|
[5236449] | 238 | self.kernel_module = None |
---|
[cbcdd2c] | 239 | # Current SasModel view dimension |
---|
[60af928] | 240 | self.is2D = False |
---|
[cbcdd2c] | 241 | # Current SasModel is multishell |
---|
[86f88d1] | 242 | self.model_has_shells = False |
---|
[cbcdd2c] | 243 | # Utility variable to enable unselectable option in category combobox |
---|
[86f88d1] | 244 | self._previous_category_index = 0 |
---|
[70f4458] | 245 | # Utility variables for multishell display |
---|
| 246 | self._n_shells_row = 0 |
---|
| 247 | self._num_shell_params = 0 |
---|
[cbcdd2c] | 248 | # Dictionary of {model name: model class} for the current category |
---|
[5236449] | 249 | self.models = {} |
---|
[f182f93] | 250 | # Parameters to fit |
---|
[6dbff18] | 251 | self.main_params_to_fit = [] |
---|
| 252 | self.poly_params_to_fit = [] |
---|
| 253 | self.magnet_params_to_fit = [] |
---|
| 254 | |
---|
[180bd54] | 255 | # Fit options |
---|
| 256 | self.q_range_min = 0.005 |
---|
| 257 | self.q_range_max = 0.1 |
---|
| 258 | self.npts = 25 |
---|
| 259 | self.log_points = False |
---|
| 260 | self.weighting = 0 |
---|
[2add354] | 261 | self.chi2 = None |
---|
[6011788] | 262 | # Does the control support UNDO/REDO |
---|
| 263 | # temporarily off |
---|
[2241130] | 264 | self.undo_supported = False |
---|
| 265 | self.page_stack = [] |
---|
[377ade1] | 266 | self.all_data = [] |
---|
[3b3b40b] | 267 | # custom plugin models |
---|
| 268 | # {model.name:model} |
---|
| 269 | self.custom_models = self.customModels() |
---|
[8222f171] | 270 | # Polydisp widget table default index for function combobox |
---|
| 271 | self.orig_poly_index = 3 |
---|
[d4dac80] | 272 | # copy of current kernel model |
---|
| 273 | self.kernel_module_copy = None |
---|
[6011788] | 274 | |
---|
[66d4370] | 275 | # dictionaries of current params |
---|
| 276 | self.poly_params = {} |
---|
| 277 | self.magnet_params = {} |
---|
| 278 | |
---|
[116dd4c1] | 279 | # Page id for fitting |
---|
| 280 | # To keep with previous SasView values, use 200 as the start offset |
---|
| 281 | self.page_id = 200 + self.tab_id |
---|
| 282 | |
---|
[d48cc19] | 283 | # Data for chosen model |
---|
| 284 | self.model_data = None |
---|
| 285 | |
---|
[a9b568c] | 286 | # Which shell is being currently displayed? |
---|
| 287 | self.current_shell_displayed = 0 |
---|
[0d13814] | 288 | # List of all shell-unique parameters |
---|
| 289 | self.shell_names = [] |
---|
[b00414d] | 290 | |
---|
| 291 | # Error column presence in parameter display |
---|
[f182f93] | 292 | self.has_error_column = False |
---|
[aca8418] | 293 | self.has_poly_error_column = False |
---|
[b00414d] | 294 | self.has_magnet_error_column = False |
---|
[a9b568c] | 295 | |
---|
[cb90b65] | 296 | # If the widget generated theory item, save it |
---|
| 297 | self.theory_item = None |
---|
| 298 | |
---|
[f37cab0] | 299 | # list column widths |
---|
| 300 | self.lstParamHeaderSizes = {} |
---|
| 301 | |
---|
[2a432e7] | 302 | # signal communicator |
---|
[cbcdd2c] | 303 | self.communicate = self.parent.communicate |
---|
[60af928] | 304 | |
---|
[2a432e7] | 305 | def initializeWidgets(self): |
---|
| 306 | """ |
---|
| 307 | Initialize widgets for tabs |
---|
| 308 | """ |
---|
[180bd54] | 309 | # Options widget |
---|
[4992ff2] | 310 | layout = QtWidgets.QGridLayout() |
---|
[180bd54] | 311 | self.options_widget = OptionsWidget(self, self.logic) |
---|
[1bc27f1] | 312 | layout.addWidget(self.options_widget) |
---|
[180bd54] | 313 | self.tabOptions.setLayout(layout) |
---|
| 314 | |
---|
[e1e3e09] | 315 | # Smearing widget |
---|
[4992ff2] | 316 | layout = QtWidgets.QGridLayout() |
---|
[e1e3e09] | 317 | self.smearing_widget = SmearingWidget(self) |
---|
[1bc27f1] | 318 | layout.addWidget(self.smearing_widget) |
---|
[180bd54] | 319 | self.tabResolution.setLayout(layout) |
---|
[e1e3e09] | 320 | |
---|
[b1e36a3] | 321 | # Define bold font for use in various controls |
---|
[1bc27f1] | 322 | self.boldFont = QtGui.QFont() |
---|
[a0f5c36] | 323 | self.boldFont.setBold(True) |
---|
| 324 | |
---|
| 325 | # Set data label |
---|
[b1e36a3] | 326 | self.label.setFont(self.boldFont) |
---|
| 327 | self.label.setText("No data loaded") |
---|
| 328 | self.lblFilename.setText("") |
---|
| 329 | |
---|
[6ff2eb3] | 330 | # Magnetic angles explained in one picture |
---|
[4992ff2] | 331 | self.magneticAnglesWidget = QtWidgets.QWidget() |
---|
| 332 | labl = QtWidgets.QLabel(self.magneticAnglesWidget) |
---|
[6d87082] | 333 | pixmap = QtGui.QPixmap(GuiUtils.IMAGES_DIRECTORY_LOCATION + '/M_angles_pic.png') |
---|
[6ff2eb3] | 334 | labl.setPixmap(pixmap) |
---|
| 335 | self.magneticAnglesWidget.setFixedSize(pixmap.width(), pixmap.height()) |
---|
| 336 | |
---|
[2a432e7] | 337 | def initializeModels(self): |
---|
| 338 | """ |
---|
| 339 | Set up models and views |
---|
| 340 | """ |
---|
[86f88d1] | 341 | # Set the main models |
---|
[cd31251] | 342 | # We can't use a single model here, due to restrictions on flattening |
---|
| 343 | # the model tree with subclassed QAbstractProxyModel... |
---|
[13cd397] | 344 | self._model_model = ToolTippedItemModel() |
---|
| 345 | self._poly_model = ToolTippedItemModel() |
---|
| 346 | self._magnet_model = ToolTippedItemModel() |
---|
[60af928] | 347 | |
---|
| 348 | # Param model displayed in param list |
---|
| 349 | self.lstParams.setModel(self._model_model) |
---|
[5236449] | 350 | self.readCategoryInfo() |
---|
[4992ff2] | 351 | |
---|
[60af928] | 352 | self.model_parameters = None |
---|
[ad6b4e2] | 353 | |
---|
| 354 | # Delegates for custom editing and display |
---|
| 355 | self.lstParams.setItemDelegate(ModelViewDelegate(self)) |
---|
| 356 | |
---|
[86f88d1] | 357 | self.lstParams.setAlternatingRowColors(True) |
---|
[61a92d4] | 358 | stylesheet = """ |
---|
[457d961] | 359 | |
---|
| 360 | QTreeView { |
---|
| 361 | paint-alternating-row-colors-for-empty-area:0; |
---|
| 362 | } |
---|
| 363 | |
---|
[97df8a9] | 364 | QTreeView::item { |
---|
| 365 | border: 1px; |
---|
[ebfe223] | 366 | padding: 2px 1px; |
---|
[97df8a9] | 367 | } |
---|
| 368 | |
---|
[2a432e7] | 369 | QTreeView::item:hover { |
---|
| 370 | background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); |
---|
| 371 | border: 1px solid #bfcde4; |
---|
[61a92d4] | 372 | } |
---|
[2a432e7] | 373 | |
---|
| 374 | QTreeView::item:selected { |
---|
| 375 | border: 1px solid #567dbc; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | QTreeView::item:selected:active{ |
---|
| 379 | background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | QTreeView::item:selected:!active { |
---|
| 383 | background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf); |
---|
| 384 | } |
---|
| 385 | """ |
---|
[61a92d4] | 386 | self.lstParams.setStyleSheet(stylesheet) |
---|
[672b8ab] | 387 | self.lstParams.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
[7fd20fc] | 388 | self.lstParams.customContextMenuRequested.connect(self.showModelContextMenu) |
---|
[457d961] | 389 | self.lstParams.setAttribute(QtCore.Qt.WA_MacShowFocusRect, False) |
---|
[f37cab0] | 390 | # Column resize signals |
---|
| 391 | self.lstParams.header().sectionResized.connect(self.onColumnWidthUpdate) |
---|
| 392 | |
---|
[60af928] | 393 | # Poly model displayed in poly list |
---|
[811bec1] | 394 | self.lstPoly.setModel(self._poly_model) |
---|
[60af928] | 395 | self.setPolyModel() |
---|
| 396 | self.setTableProperties(self.lstPoly) |
---|
[6011788] | 397 | # Delegates for custom editing and display |
---|
[aca8418] | 398 | self.lstPoly.setItemDelegate(PolyViewDelegate(self)) |
---|
| 399 | # Polydispersity function combo response |
---|
| 400 | self.lstPoly.itemDelegate().combo_updated.connect(self.onPolyComboIndexChange) |
---|
[e43fc91] | 401 | self.lstPoly.itemDelegate().filename_updated.connect(self.onPolyFilenameChange) |
---|
[60af928] | 402 | |
---|
| 403 | # Magnetism model displayed in magnetism list |
---|
| 404 | self.lstMagnetic.setModel(self._magnet_model) |
---|
| 405 | self.setMagneticModel() |
---|
| 406 | self.setTableProperties(self.lstMagnetic) |
---|
[b00414d] | 407 | # Delegates for custom editing and display |
---|
| 408 | self.lstMagnetic.setItemDelegate(MagnetismViewDelegate(self)) |
---|
[60af928] | 409 | |
---|
[2a432e7] | 410 | def initializeCategoryCombo(self): |
---|
| 411 | """ |
---|
| 412 | Model category combo setup |
---|
| 413 | """ |
---|
[60af928] | 414 | category_list = sorted(self.master_category_dict.keys()) |
---|
[86f88d1] | 415 | self.cbCategory.addItem(CATEGORY_DEFAULT) |
---|
[60af928] | 416 | self.cbCategory.addItems(category_list) |
---|
[3d18691] | 417 | if CATEGORY_STRUCTURE not in category_list: |
---|
| 418 | self.cbCategory.addItem(CATEGORY_STRUCTURE) |
---|
[6f7f652] | 419 | self.cbCategory.setCurrentIndex(0) |
---|
[60af928] | 420 | |
---|
[e1e3e09] | 421 | def setEnablementOnDataLoad(self): |
---|
| 422 | """ |
---|
| 423 | Enable/disable various UI elements based on data loaded |
---|
| 424 | """ |
---|
[cbcdd2c] | 425 | # Tag along functionality |
---|
[b1e36a3] | 426 | self.label.setText("Data loaded from: ") |
---|
[6b50296] | 427 | if self.logic.data.filename: |
---|
| 428 | self.lblFilename.setText(self.logic.data.filename) |
---|
| 429 | else: |
---|
| 430 | self.lblFilename.setText(self.logic.data.name) |
---|
[5236449] | 431 | self.updateQRange() |
---|
[e1e3e09] | 432 | # Switch off Data2D control |
---|
| 433 | self.chk2DView.setEnabled(False) |
---|
| 434 | self.chk2DView.setVisible(False) |
---|
[672c0e97] | 435 | self.chkMagnetism.setEnabled(False) |
---|
[e20870bc] | 436 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, self.chkMagnetism.isChecked()) |
---|
[ee18d33] | 437 | # Combo box or label for file name" |
---|
| 438 | if self.is_batch_fitting: |
---|
| 439 | self.lblFilename.setVisible(False) |
---|
| 440 | for dataitem in self.all_data: |
---|
| 441 | filename = GuiUtils.dataFromItem(dataitem).filename |
---|
| 442 | self.cbFileNames.addItem(filename) |
---|
| 443 | self.cbFileNames.setVisible(True) |
---|
[7fd20fc] | 444 | self.chkChainFit.setEnabled(True) |
---|
| 445 | self.chkChainFit.setVisible(True) |
---|
[38eb433] | 446 | # This panel is not designed to view individual fits, so disable plotting |
---|
| 447 | self.cmdPlot.setVisible(False) |
---|
[180bd54] | 448 | # Similarly on other tabs |
---|
| 449 | self.options_widget.setEnablementOnDataLoad() |
---|
[f7d14a1] | 450 | self.onSelectModel() |
---|
[e1e3e09] | 451 | # Smearing tab |
---|
[9a7c81c] | 452 | self.smearing_widget.updateData(self.data) |
---|
[60af928] | 453 | |
---|
[f46f6dc] | 454 | def acceptsData(self): |
---|
| 455 | """ Tells the caller this widget can accept new dataset """ |
---|
[5236449] | 456 | return not self.data_is_loaded |
---|
[f46f6dc] | 457 | |
---|
[6f7f652] | 458 | def disableModelCombo(self): |
---|
[cbcdd2c] | 459 | """ Disable the combobox """ |
---|
[6f7f652] | 460 | self.cbModel.setEnabled(False) |
---|
[b1e36a3] | 461 | self.lblModel.setEnabled(False) |
---|
[6f7f652] | 462 | |
---|
| 463 | def enableModelCombo(self): |
---|
[cbcdd2c] | 464 | """ Enable the combobox """ |
---|
[6f7f652] | 465 | self.cbModel.setEnabled(True) |
---|
[b1e36a3] | 466 | self.lblModel.setEnabled(True) |
---|
[6f7f652] | 467 | |
---|
| 468 | def disableStructureCombo(self): |
---|
[cbcdd2c] | 469 | """ Disable the combobox """ |
---|
[6f7f652] | 470 | self.cbStructureFactor.setEnabled(False) |
---|
[b1e36a3] | 471 | self.lblStructure.setEnabled(False) |
---|
[6f7f652] | 472 | |
---|
| 473 | def enableStructureCombo(self): |
---|
[cbcdd2c] | 474 | """ Enable the combobox """ |
---|
[6f7f652] | 475 | self.cbStructureFactor.setEnabled(True) |
---|
[b1e36a3] | 476 | self.lblStructure.setEnabled(True) |
---|
[6f7f652] | 477 | |
---|
[0268aed] | 478 | def togglePoly(self, isChecked): |
---|
[454670d] | 479 | """ Enable/disable the polydispersity tab """ |
---|
[0268aed] | 480 | self.tabFitting.setTabEnabled(TAB_POLY, isChecked) |
---|
[d4216ac] | 481 | # Check if any parameters are ready for fitting |
---|
| 482 | self.cmdFit.setEnabled(self.haveParamsToFit()) |
---|
[0268aed] | 483 | |
---|
| 484 | def toggleMagnetism(self, isChecked): |
---|
[454670d] | 485 | """ Enable/disable the magnetism tab """ |
---|
[0268aed] | 486 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, isChecked) |
---|
[d4216ac] | 487 | # Check if any parameters are ready for fitting |
---|
| 488 | self.cmdFit.setEnabled(self.haveParamsToFit()) |
---|
[0268aed] | 489 | |
---|
[7fd20fc] | 490 | def toggleChainFit(self, isChecked): |
---|
| 491 | """ Enable/disable chain fitting """ |
---|
| 492 | self.is_chain_fitting = isChecked |
---|
| 493 | |
---|
[0268aed] | 494 | def toggle2D(self, isChecked): |
---|
[454670d] | 495 | """ Enable/disable the controls dependent on 1D/2D data instance """ |
---|
[0268aed] | 496 | self.chkMagnetism.setEnabled(isChecked) |
---|
| 497 | self.is2D = isChecked |
---|
[1970780] | 498 | # Reload the current model |
---|
[e1e3e09] | 499 | if self.kernel_module: |
---|
| 500 | self.onSelectModel() |
---|
[5236449] | 501 | |
---|
[8b480d27] | 502 | @classmethod |
---|
| 503 | def customModels(cls): |
---|
[3b3b40b] | 504 | """ Reads in file names in the custom plugin directory """ |
---|
| 505 | return ModelUtilities._find_models() |
---|
| 506 | |
---|
[86f88d1] | 507 | def initializeControls(self): |
---|
| 508 | """ |
---|
| 509 | Set initial control enablement |
---|
| 510 | """ |
---|
[ee18d33] | 511 | self.cbFileNames.setVisible(False) |
---|
[86f88d1] | 512 | self.cmdFit.setEnabled(False) |
---|
[d48cc19] | 513 | self.cmdPlot.setEnabled(False) |
---|
[180bd54] | 514 | self.options_widget.cmdComputePoints.setVisible(False) # probably redundant |
---|
[86f88d1] | 515 | self.chkPolydispersity.setEnabled(True) |
---|
| 516 | self.chkPolydispersity.setCheckState(False) |
---|
| 517 | self.chk2DView.setEnabled(True) |
---|
| 518 | self.chk2DView.setCheckState(False) |
---|
| 519 | self.chkMagnetism.setEnabled(False) |
---|
| 520 | self.chkMagnetism.setCheckState(False) |
---|
[7fd20fc] | 521 | self.chkChainFit.setEnabled(False) |
---|
| 522 | self.chkChainFit.setVisible(False) |
---|
[cbcdd2c] | 523 | # Tabs |
---|
[86f88d1] | 524 | self.tabFitting.setTabEnabled(TAB_POLY, False) |
---|
| 525 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, False) |
---|
| 526 | self.lblChi2Value.setText("---") |
---|
[e1e3e09] | 527 | # Smearing tab |
---|
[9a7c81c] | 528 | self.smearing_widget.updateData(self.data) |
---|
[180bd54] | 529 | # Line edits in the option tab |
---|
| 530 | self.updateQRange() |
---|
[86f88d1] | 531 | |
---|
| 532 | def initializeSignals(self): |
---|
| 533 | """ |
---|
| 534 | Connect GUI element signals |
---|
| 535 | """ |
---|
[cbcdd2c] | 536 | # Comboboxes |
---|
[cd31251] | 537 | self.cbStructureFactor.currentIndexChanged.connect(self.onSelectStructureFactor) |
---|
| 538 | self.cbCategory.currentIndexChanged.connect(self.onSelectCategory) |
---|
| 539 | self.cbModel.currentIndexChanged.connect(self.onSelectModel) |
---|
[ee18d33] | 540 | self.cbFileNames.currentIndexChanged.connect(self.onSelectBatchFilename) |
---|
[cbcdd2c] | 541 | # Checkboxes |
---|
[86f88d1] | 542 | self.chk2DView.toggled.connect(self.toggle2D) |
---|
| 543 | self.chkPolydispersity.toggled.connect(self.togglePoly) |
---|
| 544 | self.chkMagnetism.toggled.connect(self.toggleMagnetism) |
---|
[7fd20fc] | 545 | self.chkChainFit.toggled.connect(self.toggleChainFit) |
---|
[cbcdd2c] | 546 | # Buttons |
---|
[5236449] | 547 | self.cmdFit.clicked.connect(self.onFit) |
---|
[cbcdd2c] | 548 | self.cmdPlot.clicked.connect(self.onPlot) |
---|
[2add354] | 549 | self.cmdHelp.clicked.connect(self.onHelp) |
---|
[6ff2eb3] | 550 | self.cmdMagneticDisplay.clicked.connect(self.onDisplayMagneticAngles) |
---|
[cbcdd2c] | 551 | |
---|
| 552 | # Respond to change in parameters from the UI |
---|
[d2007a8] | 553 | self._model_model.dataChanged.connect(self.onMainParamsChange) |
---|
| 554 | self._poly_model.dataChanged.connect(self.onPolyModelChange) |
---|
| 555 | self._magnet_model.dataChanged.connect(self.onMagnetModelChange) |
---|
[3b3b40b] | 556 | self.lstParams.selectionModel().selectionChanged.connect(self.onSelectionChanged) |
---|
| 557 | |
---|
| 558 | # Local signals |
---|
| 559 | self.batchFittingFinishedSignal.connect(self.batchFitComplete) |
---|
| 560 | self.fittingFinishedSignal.connect(self.fitComplete) |
---|
[d4dac80] | 561 | self.Calc1DFinishedSignal.connect(self.complete1D) |
---|
| 562 | self.Calc2DFinishedSignal.connect(self.complete2D) |
---|
[86f88d1] | 563 | |
---|
[180bd54] | 564 | # Signals from separate tabs asking for replot |
---|
| 565 | self.options_widget.plot_signal.connect(self.onOptionsUpdate) |
---|
| 566 | |
---|
[3b3b40b] | 567 | # Signals from other widgets |
---|
| 568 | self.communicate.customModelDirectoryChanged.connect(self.onCustomModelChange) |
---|
[9a7c81c] | 569 | self.smearing_widget.smearingChangedSignal.connect(self.onSmearingOptionsUpdate) |
---|
[3b3b40b] | 570 | |
---|
[3d18691] | 571 | # Communicator signal |
---|
| 572 | self.communicate.updateModelCategoriesSignal.connect(self.onCategoriesChanged) |
---|
| 573 | |
---|
[d3c0b95] | 574 | def modelName(self): |
---|
| 575 | """ |
---|
| 576 | Returns model name, by default M<tab#>, e.g. M1, M2 |
---|
| 577 | """ |
---|
| 578 | return "M%i" % self.tab_id |
---|
| 579 | |
---|
| 580 | def nameForFittedData(self, name): |
---|
| 581 | """ |
---|
| 582 | Generate name for the current fit |
---|
| 583 | """ |
---|
| 584 | if self.is2D: |
---|
| 585 | name += "2d" |
---|
| 586 | name = "%s [%s]" % (self.modelName(), name) |
---|
| 587 | return name |
---|
[7fd20fc] | 588 | |
---|
[d3c0b95] | 589 | def showModelContextMenu(self, position): |
---|
| 590 | """ |
---|
| 591 | Show context specific menu in the parameter table. |
---|
| 592 | When clicked on parameter(s): fitting/constraints options |
---|
| 593 | When clicked on white space: model description |
---|
| 594 | """ |
---|
[50cafe7] | 595 | rows = [s.row() for s in self.lstParams.selectionModel().selectedRows() |
---|
| 596 | if self.isCheckable(s.row())] |
---|
[7fd20fc] | 597 | menu = self.showModelDescription() if not rows else self.modelContextMenu(rows) |
---|
| 598 | try: |
---|
| 599 | menu.exec_(self.lstParams.viewport().mapToGlobal(position)) |
---|
| 600 | except AttributeError as ex: |
---|
[bb477f5] | 601 | logger.error("Error generating context menu: %s" % ex) |
---|
[7fd20fc] | 602 | return |
---|
| 603 | |
---|
| 604 | def modelContextMenu(self, rows): |
---|
[eae226b] | 605 | """ |
---|
[d3c0b95] | 606 | Create context menu for the parameter selection |
---|
[eae226b] | 607 | """ |
---|
[7fd20fc] | 608 | menu = QtWidgets.QMenu() |
---|
[eae226b] | 609 | num_rows = len(rows) |
---|
[63319b0] | 610 | if num_rows < 1: |
---|
| 611 | return menu |
---|
[7fd20fc] | 612 | # Select for fitting |
---|
[8e2cd79] | 613 | param_string = "parameter " if num_rows == 1 else "parameters " |
---|
| 614 | to_string = "to its current value" if num_rows == 1 else "to their current values" |
---|
[d3c0b95] | 615 | has_constraints = any([self.rowHasConstraint(i) for i in rows]) |
---|
[eae226b] | 616 | |
---|
[7fd20fc] | 617 | self.actionSelect = QtWidgets.QAction(self) |
---|
| 618 | self.actionSelect.setObjectName("actionSelect") |
---|
[eae226b] | 619 | self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) |
---|
[7fd20fc] | 620 | # Unselect from fitting |
---|
| 621 | self.actionDeselect = QtWidgets.QAction(self) |
---|
| 622 | self.actionDeselect.setObjectName("actionDeselect") |
---|
[eae226b] | 623 | self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) |
---|
[7fd20fc] | 624 | |
---|
| 625 | self.actionConstrain = QtWidgets.QAction(self) |
---|
| 626 | self.actionConstrain.setObjectName("actionConstrain") |
---|
[eae226b] | 627 | self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain "+param_string + to_string)) |
---|
| 628 | |
---|
| 629 | self.actionRemoveConstraint = QtWidgets.QAction(self) |
---|
| 630 | self.actionRemoveConstraint.setObjectName("actionRemoveConstrain") |
---|
| 631 | self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove constraint")) |
---|
[7fd20fc] | 632 | |
---|
| 633 | self.actionMultiConstrain = QtWidgets.QAction(self) |
---|
| 634 | self.actionMultiConstrain.setObjectName("actionMultiConstrain") |
---|
| 635 | self.actionMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain selected parameters to their current values")) |
---|
| 636 | |
---|
| 637 | self.actionMutualMultiConstrain = QtWidgets.QAction(self) |
---|
| 638 | self.actionMutualMultiConstrain.setObjectName("actionMutualMultiConstrain") |
---|
| 639 | self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of selected parameters...")) |
---|
| 640 | |
---|
| 641 | menu.addAction(self.actionSelect) |
---|
| 642 | menu.addAction(self.actionDeselect) |
---|
| 643 | menu.addSeparator() |
---|
| 644 | |
---|
[d3c0b95] | 645 | if has_constraints: |
---|
[eae226b] | 646 | menu.addAction(self.actionRemoveConstraint) |
---|
[d3c0b95] | 647 | #if num_rows == 1: |
---|
| 648 | # menu.addAction(self.actionEditConstraint) |
---|
[eae226b] | 649 | else: |
---|
[7fd20fc] | 650 | menu.addAction(self.actionConstrain) |
---|
[d3c0b95] | 651 | if num_rows == 2: |
---|
| 652 | menu.addAction(self.actionMutualMultiConstrain) |
---|
[7fd20fc] | 653 | |
---|
| 654 | # Define the callbacks |
---|
[0595bb7] | 655 | self.actionConstrain.triggered.connect(self.addSimpleConstraint) |
---|
[eae226b] | 656 | self.actionRemoveConstraint.triggered.connect(self.deleteConstraint) |
---|
[0595bb7] | 657 | self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) |
---|
[7fd20fc] | 658 | self.actionSelect.triggered.connect(self.selectParameters) |
---|
| 659 | self.actionDeselect.triggered.connect(self.deselectParameters) |
---|
| 660 | return menu |
---|
| 661 | |
---|
[0595bb7] | 662 | def showMultiConstraint(self): |
---|
[7fd20fc] | 663 | """ |
---|
| 664 | Show the constraint widget and receive the expression |
---|
| 665 | """ |
---|
[0595bb7] | 666 | selected_rows = self.lstParams.selectionModel().selectedRows() |
---|
[6e58f2f] | 667 | # There have to be only two rows selected. The caller takes care of that |
---|
| 668 | # but let's check the correctness. |
---|
[8e2cd79] | 669 | assert len(selected_rows) == 2 |
---|
[0595bb7] | 670 | |
---|
| 671 | params_list = [s.data() for s in selected_rows] |
---|
[eae226b] | 672 | # Create and display the widget for param1 and param2 |
---|
[7fd20fc] | 673 | mc_widget = MultiConstraint(self, params=params_list) |
---|
[305114c] | 674 | # Check if any of the parameters are polydisperse |
---|
| 675 | if not np.any([FittingUtilities.isParamPolydisperse(p, self.model_parameters, is2D=self.is2D) for p in params_list]): |
---|
| 676 | # no parameters are pd - reset the text to not show the warning |
---|
| 677 | mc_widget.lblWarning.setText("") |
---|
[eae226b] | 678 | if mc_widget.exec_() != QtWidgets.QDialog.Accepted: |
---|
| 679 | return |
---|
| 680 | |
---|
[0595bb7] | 681 | constraint = Constraint() |
---|
| 682 | c_text = mc_widget.txtConstraint.text() |
---|
| 683 | |
---|
[eae226b] | 684 | # widget.params[0] is the parameter we're constraining |
---|
| 685 | constraint.param = mc_widget.params[0] |
---|
[3b3b40b] | 686 | # parameter should have the model name preamble |
---|
[06234fc] | 687 | model_name = self.kernel_module.name |
---|
[3b3b40b] | 688 | # param_used is the parameter we're using in constraining function |
---|
| 689 | param_used = mc_widget.params[1] |
---|
| 690 | # Replace param_used with model_name.param_used |
---|
| 691 | updated_param_used = model_name + "." + param_used |
---|
| 692 | new_func = c_text.replace(param_used, updated_param_used) |
---|
| 693 | constraint.func = new_func |
---|
[d3c0b95] | 694 | # Which row is the constrained parameter in? |
---|
[2d466e4] | 695 | row = self.getRowFromName(constraint.param) |
---|
[eae226b] | 696 | |
---|
[6e58f2f] | 697 | # Create a new item and add the Constraint object as a child |
---|
| 698 | self.addConstraintToRow(constraint=constraint, row=row) |
---|
[eae226b] | 699 | |
---|
[2d466e4] | 700 | def getRowFromName(self, name): |
---|
[eae226b] | 701 | """ |
---|
[d3c0b95] | 702 | Given parameter name get the row number in self._model_model |
---|
[eae226b] | 703 | """ |
---|
| 704 | for row in range(self._model_model.rowCount()): |
---|
| 705 | row_name = self._model_model.item(row).text() |
---|
| 706 | if row_name == name: |
---|
| 707 | return row |
---|
| 708 | return None |
---|
| 709 | |
---|
[2d466e4] | 710 | def getParamNames(self): |
---|
| 711 | """ |
---|
| 712 | Return list of all parameters for the current model |
---|
| 713 | """ |
---|
[00b7ddf0] | 714 | return [self._model_model.item(row).text() |
---|
| 715 | for row in range(self._model_model.rowCount()) |
---|
| 716 | if self.isCheckable(row)] |
---|
[2d466e4] | 717 | |
---|
[eae226b] | 718 | def modifyViewOnRow(self, row, font=None, brush=None): |
---|
| 719 | """ |
---|
| 720 | Chage how the given row of the main model is shown |
---|
| 721 | """ |
---|
| 722 | fields_enabled = False |
---|
| 723 | if font is None: |
---|
| 724 | font = QtGui.QFont() |
---|
| 725 | fields_enabled = True |
---|
| 726 | if brush is None: |
---|
| 727 | brush = QtGui.QBrush() |
---|
| 728 | fields_enabled = True |
---|
[0595bb7] | 729 | self._model_model.blockSignals(True) |
---|
| 730 | # Modify font and foreground of affected rows |
---|
| 731 | for column in range(0, self._model_model.columnCount()): |
---|
| 732 | self._model_model.item(row, column).setForeground(brush) |
---|
| 733 | self._model_model.item(row, column).setFont(font) |
---|
[eae226b] | 734 | self._model_model.item(row, column).setEditable(fields_enabled) |
---|
[0595bb7] | 735 | self._model_model.blockSignals(False) |
---|
| 736 | |
---|
[c5a2722f] | 737 | def addConstraintToRow(self, constraint=None, row=0): |
---|
| 738 | """ |
---|
| 739 | Adds the constraint object to requested row |
---|
| 740 | """ |
---|
| 741 | # Create a new item and add the Constraint object as a child |
---|
[8e2cd79] | 742 | assert isinstance(constraint, Constraint) |
---|
| 743 | assert 0 <= row <= self._model_model.rowCount() |
---|
[00b7ddf0] | 744 | assert self.isCheckable(row) |
---|
[c5a2722f] | 745 | |
---|
| 746 | item = QtGui.QStandardItem() |
---|
| 747 | item.setData(constraint) |
---|
| 748 | self._model_model.item(row, 1).setChild(0, item) |
---|
| 749 | # Set min/max to the value constrained |
---|
| 750 | self.constraintAddedSignal.emit([row]) |
---|
| 751 | # Show visual hints for the constraint |
---|
| 752 | font = QtGui.QFont() |
---|
| 753 | font.setItalic(True) |
---|
| 754 | brush = QtGui.QBrush(QtGui.QColor('blue')) |
---|
| 755 | self.modifyViewOnRow(row, font=font, brush=brush) |
---|
| 756 | self.communicate.statusBarUpdateSignal.emit('Constraint added') |
---|
| 757 | |
---|
[0595bb7] | 758 | def addSimpleConstraint(self): |
---|
[7fd20fc] | 759 | """ |
---|
| 760 | Adds a constraint on a single parameter. |
---|
[2add354] | 761 | """ |
---|
[d3c0b95] | 762 | min_col = self.lstParams.itemDelegate().param_min |
---|
| 763 | max_col = self.lstParams.itemDelegate().param_max |
---|
[0595bb7] | 764 | for row in self.selectedParameters(): |
---|
[00b7ddf0] | 765 | assert(self.isCheckable(row)) |
---|
[0595bb7] | 766 | param = self._model_model.item(row, 0).text() |
---|
| 767 | value = self._model_model.item(row, 1).text() |
---|
[235d766] | 768 | min_t = self._model_model.item(row, min_col).text() |
---|
| 769 | max_t = self._model_model.item(row, max_col).text() |
---|
[eae226b] | 770 | # Create a Constraint object |
---|
[235d766] | 771 | constraint = Constraint(param=param, value=value, min=min_t, max=max_t) |
---|
[eae226b] | 772 | # Create a new item and add the Constraint object as a child |
---|
[0595bb7] | 773 | item = QtGui.QStandardItem() |
---|
| 774 | item.setData(constraint) |
---|
| 775 | self._model_model.item(row, 1).setChild(0, item) |
---|
[235d766] | 776 | # Assumed correctness from the validator |
---|
| 777 | value = float(value) |
---|
| 778 | # BUMPS calculates log(max-min) without any checks, so let's assign minor range |
---|
| 779 | min_v = value - (value/10000.0) |
---|
| 780 | max_v = value + (value/10000.0) |
---|
[eae226b] | 781 | # Set min/max to the value constrained |
---|
[235d766] | 782 | self._model_model.item(row, min_col).setText(str(min_v)) |
---|
| 783 | self._model_model.item(row, max_col).setText(str(max_v)) |
---|
[be8f4b0] | 784 | self.constraintAddedSignal.emit([row]) |
---|
[eae226b] | 785 | # Show visual hints for the constraint |
---|
| 786 | font = QtGui.QFont() |
---|
| 787 | font.setItalic(True) |
---|
| 788 | brush = QtGui.QBrush(QtGui.QColor('blue')) |
---|
| 789 | self.modifyViewOnRow(row, font=font, brush=brush) |
---|
[7fd20fc] | 790 | self.communicate.statusBarUpdateSignal.emit('Constraint added') |
---|
| 791 | |
---|
[eae226b] | 792 | def deleteConstraint(self): |
---|
| 793 | """ |
---|
| 794 | Delete constraints from selected parameters. |
---|
| 795 | """ |
---|
[8e2cd79] | 796 | params = [s.data() for s in self.lstParams.selectionModel().selectedRows() |
---|
[3b3b40b] | 797 | if self.isCheckable(s.row())] |
---|
| 798 | for param in params: |
---|
| 799 | self.deleteConstraintOnParameter(param=param) |
---|
[be8f4b0] | 800 | |
---|
| 801 | def deleteConstraintOnParameter(self, param=None): |
---|
| 802 | """ |
---|
| 803 | Delete the constraint on model parameter 'param' |
---|
| 804 | """ |
---|
[d3c0b95] | 805 | min_col = self.lstParams.itemDelegate().param_min |
---|
| 806 | max_col = self.lstParams.itemDelegate().param_max |
---|
[be8f4b0] | 807 | for row in range(self._model_model.rowCount()): |
---|
[00b7ddf0] | 808 | if not self.isCheckable(row): |
---|
| 809 | continue |
---|
[3b3b40b] | 810 | if not self.rowHasConstraint(row): |
---|
| 811 | continue |
---|
[eae226b] | 812 | # Get the Constraint object from of the model item |
---|
| 813 | item = self._model_model.item(row, 1) |
---|
[3b3b40b] | 814 | constraint = self.getConstraintForRow(row) |
---|
[d3c0b95] | 815 | if constraint is None: |
---|
| 816 | continue |
---|
| 817 | if not isinstance(constraint, Constraint): |
---|
| 818 | continue |
---|
[be8f4b0] | 819 | if param and constraint.param != param: |
---|
| 820 | continue |
---|
| 821 | # Now we got the right row. Delete the constraint and clean up |
---|
[eae226b] | 822 | # Retrieve old values and put them on the model |
---|
| 823 | if constraint.min is not None: |
---|
[d3c0b95] | 824 | self._model_model.item(row, min_col).setText(constraint.min) |
---|
[eae226b] | 825 | if constraint.max is not None: |
---|
[d3c0b95] | 826 | self._model_model.item(row, max_col).setText(constraint.max) |
---|
[eae226b] | 827 | # Remove constraint item |
---|
| 828 | item.removeRow(0) |
---|
[be8f4b0] | 829 | self.constraintAddedSignal.emit([row]) |
---|
[eae226b] | 830 | self.modifyViewOnRow(row) |
---|
[be8f4b0] | 831 | |
---|
[eae226b] | 832 | self.communicate.statusBarUpdateSignal.emit('Constraint removed') |
---|
[be8f4b0] | 833 | |
---|
[d3c0b95] | 834 | def getConstraintForRow(self, row): |
---|
| 835 | """ |
---|
[c4b23dd] | 836 | For the given row, return its constraint, if any (otherwise None) |
---|
[d3c0b95] | 837 | """ |
---|
[c4b23dd] | 838 | if not self.isCheckable(row): |
---|
| 839 | return None |
---|
| 840 | item = self._model_model.item(row, 1) |
---|
| 841 | try: |
---|
| 842 | return item.child(0).data() |
---|
| 843 | except AttributeError: |
---|
| 844 | return None |
---|
[d3c0b95] | 845 | |
---|
[eae226b] | 846 | def rowHasConstraint(self, row): |
---|
| 847 | """ |
---|
| 848 | Finds out if row of the main model has a constraint child |
---|
| 849 | """ |
---|
[c4b23dd] | 850 | if not self.isCheckable(row): |
---|
| 851 | return False |
---|
| 852 | item = self._model_model.item(row, 1) |
---|
| 853 | if not item.hasChildren(): |
---|
| 854 | return False |
---|
| 855 | c = item.child(0).data() |
---|
| 856 | if isinstance(c, Constraint): |
---|
| 857 | return True |
---|
[be8f4b0] | 858 | return False |
---|
[116dd4c1] | 859 | |
---|
| 860 | def rowHasActiveConstraint(self, row): |
---|
| 861 | """ |
---|
| 862 | Finds out if row of the main model has an active constraint child |
---|
| 863 | """ |
---|
[c4b23dd] | 864 | if not self.isCheckable(row): |
---|
| 865 | return False |
---|
| 866 | item = self._model_model.item(row, 1) |
---|
| 867 | if not item.hasChildren(): |
---|
| 868 | return False |
---|
| 869 | c = item.child(0).data() |
---|
| 870 | if isinstance(c, Constraint) and c.active: |
---|
| 871 | return True |
---|
[235d766] | 872 | return False |
---|
| 873 | |
---|
| 874 | def rowHasActiveComplexConstraint(self, row): |
---|
| 875 | """ |
---|
| 876 | Finds out if row of the main model has an active, nontrivial constraint child |
---|
| 877 | """ |
---|
[c4b23dd] | 878 | if not self.isCheckable(row): |
---|
| 879 | return False |
---|
| 880 | item = self._model_model.item(row, 1) |
---|
| 881 | if not item.hasChildren(): |
---|
| 882 | return False |
---|
| 883 | c = item.child(0).data() |
---|
| 884 | if isinstance(c, Constraint) and c.func and c.active: |
---|
| 885 | return True |
---|
[116dd4c1] | 886 | return False |
---|
[eae226b] | 887 | |
---|
[7fd20fc] | 888 | def selectParameters(self): |
---|
| 889 | """ |
---|
[d3c0b95] | 890 | Selected parameter is chosen for fitting |
---|
[7fd20fc] | 891 | """ |
---|
| 892 | status = QtCore.Qt.Checked |
---|
| 893 | self.setParameterSelection(status) |
---|
| 894 | |
---|
| 895 | def deselectParameters(self): |
---|
| 896 | """ |
---|
| 897 | Selected parameters are removed for fitting |
---|
| 898 | """ |
---|
| 899 | status = QtCore.Qt.Unchecked |
---|
| 900 | self.setParameterSelection(status) |
---|
| 901 | |
---|
| 902 | def selectedParameters(self): |
---|
| 903 | """ Returns list of selected (highlighted) parameters """ |
---|
[d3c0b95] | 904 | return [s.row() for s in self.lstParams.selectionModel().selectedRows() |
---|
| 905 | if self.isCheckable(s.row())] |
---|
[7fd20fc] | 906 | |
---|
| 907 | def setParameterSelection(self, status=QtCore.Qt.Unchecked): |
---|
| 908 | """ |
---|
| 909 | Selected parameters are chosen for fitting |
---|
| 910 | """ |
---|
| 911 | # Convert to proper indices and set requested enablement |
---|
| 912 | for row in self.selectedParameters(): |
---|
| 913 | self._model_model.item(row, 0).setCheckState(status) |
---|
[d3c0b95] | 914 | |
---|
| 915 | def getConstraintsForModel(self): |
---|
| 916 | """ |
---|
| 917 | Return a list of tuples. Each tuple contains constraints mapped as |
---|
| 918 | ('constrained parameter', 'function to constrain') |
---|
| 919 | e.g. [('sld','5*sld_solvent')] |
---|
| 920 | """ |
---|
| 921 | param_number = self._model_model.rowCount() |
---|
| 922 | params = [(self._model_model.item(s, 0).text(), |
---|
[c5a2722f] | 923 | self._model_model.item(s, 1).child(0).data().func) |
---|
[116dd4c1] | 924 | for s in range(param_number) if self.rowHasActiveConstraint(s)] |
---|
[d3c0b95] | 925 | return params |
---|
[7fd20fc] | 926 | |
---|
[235d766] | 927 | def getComplexConstraintsForModel(self): |
---|
| 928 | """ |
---|
| 929 | Return a list of tuples. Each tuple contains constraints mapped as |
---|
| 930 | ('constrained parameter', 'function to constrain') |
---|
[06234fc] | 931 | e.g. [('sld','5*M2.sld_solvent')]. |
---|
[235d766] | 932 | Only for constraints with defined VALUE |
---|
| 933 | """ |
---|
| 934 | param_number = self._model_model.rowCount() |
---|
| 935 | params = [(self._model_model.item(s, 0).text(), |
---|
| 936 | self._model_model.item(s, 1).child(0).data().func) |
---|
| 937 | for s in range(param_number) if self.rowHasActiveComplexConstraint(s)] |
---|
| 938 | return params |
---|
| 939 | |
---|
[ba01ad1] | 940 | def getConstraintObjectsForModel(self): |
---|
| 941 | """ |
---|
| 942 | Returns Constraint objects present on the whole model |
---|
| 943 | """ |
---|
| 944 | param_number = self._model_model.rowCount() |
---|
| 945 | constraints = [self._model_model.item(s, 1).child(0).data() |
---|
| 946 | for s in range(param_number) if self.rowHasConstraint(s)] |
---|
| 947 | |
---|
| 948 | return constraints |
---|
| 949 | |
---|
[3b3b40b] | 950 | def getConstraintsForFitting(self): |
---|
| 951 | """ |
---|
| 952 | Return a list of constraints in format ready for use in fiting |
---|
| 953 | """ |
---|
| 954 | # Get constraints |
---|
| 955 | constraints = self.getComplexConstraintsForModel() |
---|
| 956 | # See if there are any constraints across models |
---|
| 957 | multi_constraints = [cons for cons in constraints if self.isConstraintMultimodel(cons[1])] |
---|
| 958 | |
---|
| 959 | if multi_constraints: |
---|
| 960 | # Let users choose what to do |
---|
| 961 | msg = "The current fit contains constraints relying on other fit pages.\n" |
---|
| 962 | msg += "Parameters with those constraints are:\n" +\ |
---|
| 963 | '\n'.join([cons[0] for cons in multi_constraints]) |
---|
| 964 | msg += "\n\nWould you like to remove these constraints or cancel fitting?" |
---|
| 965 | msgbox = QtWidgets.QMessageBox(self) |
---|
| 966 | msgbox.setIcon(QtWidgets.QMessageBox.Warning) |
---|
| 967 | msgbox.setText(msg) |
---|
| 968 | msgbox.setWindowTitle("Existing Constraints") |
---|
| 969 | # custom buttons |
---|
| 970 | button_remove = QtWidgets.QPushButton("Remove") |
---|
| 971 | msgbox.addButton(button_remove, QtWidgets.QMessageBox.YesRole) |
---|
| 972 | button_cancel = QtWidgets.QPushButton("Cancel") |
---|
| 973 | msgbox.addButton(button_cancel, QtWidgets.QMessageBox.RejectRole) |
---|
| 974 | retval = msgbox.exec_() |
---|
| 975 | if retval == QtWidgets.QMessageBox.RejectRole: |
---|
| 976 | # cancel fit |
---|
| 977 | raise ValueError("Fitting cancelled") |
---|
| 978 | else: |
---|
| 979 | # remove constraint |
---|
| 980 | for cons in multi_constraints: |
---|
| 981 | self.deleteConstraintOnParameter(param=cons[0]) |
---|
| 982 | # re-read the constraints |
---|
| 983 | constraints = self.getComplexConstraintsForModel() |
---|
| 984 | |
---|
| 985 | return constraints |
---|
| 986 | |
---|
[7fd20fc] | 987 | def showModelDescription(self): |
---|
| 988 | """ |
---|
| 989 | Creates a window with model description, when right clicked in the treeview |
---|
[2add354] | 990 | """ |
---|
| 991 | msg = 'Model description:\n' |
---|
| 992 | if self.kernel_module is not None: |
---|
| 993 | if str(self.kernel_module.description).rstrip().lstrip() == '': |
---|
| 994 | msg += "Sorry, no information is available for this model." |
---|
| 995 | else: |
---|
| 996 | msg += self.kernel_module.description + '\n' |
---|
| 997 | else: |
---|
| 998 | msg += "You must select a model to get information on this" |
---|
| 999 | |
---|
[4992ff2] | 1000 | menu = QtWidgets.QMenu() |
---|
| 1001 | label = QtWidgets.QLabel(msg) |
---|
[d6b8a1d] | 1002 | action = QtWidgets.QWidgetAction(self) |
---|
[672b8ab] | 1003 | action.setDefaultWidget(label) |
---|
| 1004 | menu.addAction(action) |
---|
[7fd20fc] | 1005 | return menu |
---|
[2add354] | 1006 | |
---|
[a98014ab] | 1007 | def canHaveMagnetism(self): |
---|
| 1008 | """ |
---|
| 1009 | Checks if the current model has magnetic scattering implemented |
---|
| 1010 | """ |
---|
| 1011 | current_model = self.cbModel.currentText() |
---|
| 1012 | return self.is2D and current_model in self.MAGNETIC_MODELS |
---|
| 1013 | |
---|
[0268aed] | 1014 | def onSelectModel(self): |
---|
[cbcdd2c] | 1015 | """ |
---|
[0268aed] | 1016 | Respond to select Model from list event |
---|
[cbcdd2c] | 1017 | """ |
---|
[d6b8a1d] | 1018 | model = self.cbModel.currentText() |
---|
[0268aed] | 1019 | |
---|
[2eeda93] | 1020 | if model == MODEL_DEFAULT: |
---|
| 1021 | # if the previous category was not the default, keep it. |
---|
| 1022 | # Otherwise, just return |
---|
| 1023 | if self._previous_model_index != 0: |
---|
| 1024 | # We need to block signals, or else state changes on perceived unchanged conditions |
---|
| 1025 | self.cbModel.blockSignals(True) |
---|
| 1026 | self.cbModel.setCurrentIndex(self._previous_model_index) |
---|
| 1027 | self.cbModel.blockSignals(False) |
---|
| 1028 | return |
---|
| 1029 | |
---|
[f4aa7a8] | 1030 | # Assure the control is active |
---|
| 1031 | if not self.cbModel.isEnabled(): |
---|
| 1032 | return |
---|
| 1033 | # Empty combobox forced to be read |
---|
[b3e8629] | 1034 | if not model: |
---|
| 1035 | return |
---|
[a98014ab] | 1036 | self.chkMagnetism.setEnabled(self.canHaveMagnetism()) |
---|
[2eeda93] | 1037 | self.chkMagnetism.setEnabled(self.canHaveMagnetism()) |
---|
[a98014ab] | 1038 | self.tabFitting.setTabEnabled(TAB_MAGNETISM, self.chkMagnetism.isChecked() and self.canHaveMagnetism()) |
---|
[2eeda93] | 1039 | self._previous_model_index = self.cbModel.currentIndex() |
---|
[a98014ab] | 1040 | |
---|
[f182f93] | 1041 | # Reset parameters to fit |
---|
[6dbff18] | 1042 | self.resetParametersToFit() |
---|
[d7ff531] | 1043 | self.has_error_column = False |
---|
[aca8418] | 1044 | self.has_poly_error_column = False |
---|
[f182f93] | 1045 | |
---|
[605d944] | 1046 | structure = None |
---|
| 1047 | if self.cbStructureFactor.isEnabled(): |
---|
| 1048 | structure = str(self.cbStructureFactor.currentText()) |
---|
| 1049 | self.respondToModelStructure(model=model, structure_factor=structure) |
---|
[fd1ae6d1] | 1050 | |
---|
[ee18d33] | 1051 | def onSelectBatchFilename(self, data_index): |
---|
| 1052 | """ |
---|
| 1053 | Update the logic based on the selected file in batch fitting |
---|
| 1054 | """ |
---|
[d4dac80] | 1055 | self.data_index = data_index |
---|
[ee18d33] | 1056 | self.updateQRange() |
---|
| 1057 | |
---|
[fd1ae6d1] | 1058 | def onSelectStructureFactor(self): |
---|
| 1059 | """ |
---|
| 1060 | Select Structure Factor from list |
---|
| 1061 | """ |
---|
| 1062 | model = str(self.cbModel.currentText()) |
---|
| 1063 | category = str(self.cbCategory.currentText()) |
---|
| 1064 | structure = str(self.cbStructureFactor.currentText()) |
---|
| 1065 | if category == CATEGORY_STRUCTURE: |
---|
| 1066 | model = None |
---|
[e11106e] | 1067 | |
---|
| 1068 | # Reset parameters to fit |
---|
[6dbff18] | 1069 | self.resetParametersToFit() |
---|
[e11106e] | 1070 | self.has_error_column = False |
---|
| 1071 | self.has_poly_error_column = False |
---|
| 1072 | |
---|
[fd1ae6d1] | 1073 | self.respondToModelStructure(model=model, structure_factor=structure) |
---|
| 1074 | |
---|
[6dbff18] | 1075 | def resetParametersToFit(self): |
---|
| 1076 | """ |
---|
| 1077 | Clears the list of parameters to be fitted |
---|
| 1078 | """ |
---|
| 1079 | self.main_params_to_fit = [] |
---|
| 1080 | self.poly_params_to_fit = [] |
---|
| 1081 | self.magnet_params_to_fit = [] |
---|
| 1082 | |
---|
[3b3b40b] | 1083 | def onCustomModelChange(self): |
---|
| 1084 | """ |
---|
| 1085 | Reload the custom model combobox |
---|
| 1086 | """ |
---|
| 1087 | self.custom_models = self.customModels() |
---|
| 1088 | self.readCustomCategoryInfo() |
---|
[2dce1d8] | 1089 | self.onCategoriesChanged() |
---|
| 1090 | |
---|
[3b3b40b] | 1091 | # See if we need to update the combo in-place |
---|
| 1092 | if self.cbCategory.currentText() != CATEGORY_CUSTOM: return |
---|
| 1093 | |
---|
| 1094 | current_text = self.cbModel.currentText() |
---|
| 1095 | self.cbModel.blockSignals(True) |
---|
| 1096 | self.cbModel.clear() |
---|
| 1097 | self.cbModel.blockSignals(False) |
---|
| 1098 | self.enableModelCombo() |
---|
| 1099 | self.disableStructureCombo() |
---|
| 1100 | # Retrieve the list of models |
---|
| 1101 | model_list = self.master_category_dict[CATEGORY_CUSTOM] |
---|
| 1102 | # Populate the models combobox |
---|
| 1103 | self.cbModel.addItems(sorted([model for (model, _) in model_list])) |
---|
| 1104 | new_index = self.cbModel.findText(current_text) |
---|
| 1105 | if new_index != -1: |
---|
| 1106 | self.cbModel.setCurrentIndex(self.cbModel.findText(current_text)) |
---|
| 1107 | |
---|
| 1108 | def onSelectionChanged(self): |
---|
| 1109 | """ |
---|
| 1110 | React to parameter selection |
---|
| 1111 | """ |
---|
| 1112 | rows = self.lstParams.selectionModel().selectedRows() |
---|
| 1113 | # Clean previous messages |
---|
| 1114 | self.communicate.statusBarUpdateSignal.emit("") |
---|
| 1115 | if len(rows) == 1: |
---|
| 1116 | # Show constraint, if present |
---|
| 1117 | row = rows[0].row() |
---|
[c4b23dd] | 1118 | if not self.rowHasConstraint(row): |
---|
| 1119 | return |
---|
[d1e4689] | 1120 | constr = self.getConstraintForRow(row) |
---|
[c4b23dd] | 1121 | func = self.getConstraintForRow(row).func |
---|
[d1e4689] | 1122 | if constr.func is not None: |
---|
| 1123 | # inter-parameter constraint |
---|
| 1124 | update_text = "Active constraint: "+func |
---|
| 1125 | elif constr.param == rows[0].data(): |
---|
| 1126 | # current value constraint |
---|
| 1127 | update_text = "Value constrained to: " + str(constr.value) |
---|
| 1128 | else: |
---|
| 1129 | # ill defined constraint |
---|
| 1130 | return |
---|
| 1131 | self.communicate.statusBarUpdateSignal.emit(update_text) |
---|
[3b3b40b] | 1132 | |
---|
[47d7d2d] | 1133 | def replaceConstraintName(self, old_name, new_name=""): |
---|
| 1134 | """ |
---|
| 1135 | Replace names of models in defined constraints |
---|
| 1136 | """ |
---|
| 1137 | param_number = self._model_model.rowCount() |
---|
| 1138 | # loop over parameters |
---|
| 1139 | for row in range(param_number): |
---|
| 1140 | if self.rowHasConstraint(row): |
---|
| 1141 | func = self._model_model.item(row, 1).child(0).data().func |
---|
| 1142 | if old_name in func: |
---|
| 1143 | new_func = func.replace(old_name, new_name) |
---|
| 1144 | self._model_model.item(row, 1).child(0).data().func = new_func |
---|
| 1145 | |
---|
[3b3b40b] | 1146 | def isConstraintMultimodel(self, constraint): |
---|
| 1147 | """ |
---|
| 1148 | Check if the constraint function text contains current model name |
---|
| 1149 | """ |
---|
| 1150 | current_model_name = self.kernel_module.name |
---|
| 1151 | if current_model_name in constraint: |
---|
| 1152 | return False |
---|
| 1153 | else: |
---|
| 1154 | return True |
---|
| 1155 | |
---|
[9c0ce68] | 1156 | def updateData(self): |
---|
| 1157 | """ |
---|
| 1158 | Helper function for recalculation of data used in plotting |
---|
| 1159 | """ |
---|
| 1160 | # Update the chart |
---|
[0268aed] | 1161 | if self.data_is_loaded: |
---|
[d48cc19] | 1162 | self.cmdPlot.setText("Show Plot") |
---|
[0268aed] | 1163 | self.calculateQGridForModel() |
---|
| 1164 | else: |
---|
[d48cc19] | 1165 | self.cmdPlot.setText("Calculate") |
---|
[0268aed] | 1166 | # Create default datasets if no data passed |
---|
| 1167 | self.createDefaultDataset() |
---|
[33d5956] | 1168 | self.theory_item = None # ensure theory is recalc. before plot, see showTheoryPlot() |
---|
[0268aed] | 1169 | |
---|
[9c0ce68] | 1170 | def respondToModelStructure(self, model=None, structure_factor=None): |
---|
| 1171 | # Set enablement on calculate/plot |
---|
| 1172 | self.cmdPlot.setEnabled(True) |
---|
| 1173 | |
---|
| 1174 | # kernel parameters -> model_model |
---|
| 1175 | self.SASModelToQModel(model, structure_factor) |
---|
| 1176 | |
---|
[f37cab0] | 1177 | for column, width in self.lstParamHeaderSizes.items(): |
---|
| 1178 | self.lstParams.setColumnWidth(column, width) |
---|
| 1179 | |
---|
[9c0ce68] | 1180 | # Update plot |
---|
| 1181 | self.updateData() |
---|
| 1182 | |
---|
[6011788] | 1183 | # Update state stack |
---|
[00b3b40] | 1184 | self.updateUndo() |
---|
[2add354] | 1185 | |
---|
[be8f4b0] | 1186 | # Let others know |
---|
| 1187 | self.newModelSignal.emit() |
---|
| 1188 | |
---|
[cd31251] | 1189 | def onSelectCategory(self): |
---|
[60af928] | 1190 | """ |
---|
| 1191 | Select Category from list |
---|
| 1192 | """ |
---|
[d6b8a1d] | 1193 | category = self.cbCategory.currentText() |
---|
[86f88d1] | 1194 | # Check if the user chose "Choose category entry" |
---|
[4d457df] | 1195 | if category == CATEGORY_DEFAULT: |
---|
[86f88d1] | 1196 | # if the previous category was not the default, keep it. |
---|
| 1197 | # Otherwise, just return |
---|
| 1198 | if self._previous_category_index != 0: |
---|
[351b53e] | 1199 | # We need to block signals, or else state changes on perceived unchanged conditions |
---|
| 1200 | self.cbCategory.blockSignals(True) |
---|
[86f88d1] | 1201 | self.cbCategory.setCurrentIndex(self._previous_category_index) |
---|
[351b53e] | 1202 | self.cbCategory.blockSignals(False) |
---|
[86f88d1] | 1203 | return |
---|
| 1204 | |
---|
[4d457df] | 1205 | if category == CATEGORY_STRUCTURE: |
---|
[6f7f652] | 1206 | self.disableModelCombo() |
---|
| 1207 | self.enableStructureCombo() |
---|
[f4aa7a8] | 1208 | # set the index to 0 |
---|
| 1209 | self.cbStructureFactor.setCurrentIndex(0) |
---|
| 1210 | self.model_parameters = None |
---|
[29eb947] | 1211 | self._model_model.clear() |
---|
[6f7f652] | 1212 | return |
---|
[2eeda93] | 1213 | # Wipe out the parameter model |
---|
| 1214 | self._model_model.clear() |
---|
[cbcdd2c] | 1215 | # Safely clear and enable the model combo |
---|
[6f7f652] | 1216 | self.cbModel.blockSignals(True) |
---|
| 1217 | self.cbModel.clear() |
---|
| 1218 | self.cbModel.blockSignals(False) |
---|
| 1219 | self.enableModelCombo() |
---|
| 1220 | self.disableStructureCombo() |
---|
| 1221 | |
---|
[86f88d1] | 1222 | self._previous_category_index = self.cbCategory.currentIndex() |
---|
[cbcdd2c] | 1223 | # Retrieve the list of models |
---|
[4d457df] | 1224 | model_list = self.master_category_dict[category] |
---|
[cbcdd2c] | 1225 | # Populate the models combobox |
---|
[2eeda93] | 1226 | self.cbModel.blockSignals(True) |
---|
| 1227 | self.cbModel.addItem(MODEL_DEFAULT) |
---|
[b1e36a3] | 1228 | self.cbModel.addItems(sorted([model for (model, _) in model_list])) |
---|
[2eeda93] | 1229 | self.cbModel.blockSignals(False) |
---|
[4d457df] | 1230 | |
---|
[d2007a8] | 1231 | def onPolyModelChange(self, top, bottom): |
---|
[0268aed] | 1232 | """ |
---|
| 1233 | Callback method for updating the main model and sasmodel |
---|
| 1234 | parameters with the GUI values in the polydispersity view |
---|
| 1235 | """ |
---|
[d2007a8] | 1236 | item = self._poly_model.itemFromIndex(top) |
---|
[0268aed] | 1237 | model_column = item.column() |
---|
| 1238 | model_row = item.row() |
---|
| 1239 | name_index = self._poly_model.index(model_row, 0) |
---|
[287d356] | 1240 | parameter_name = str(name_index.data()) # "distribution of sld" etc. |
---|
| 1241 | if "istribution of" in parameter_name: |
---|
[358b39d] | 1242 | # just the last word |
---|
| 1243 | parameter_name = parameter_name.rsplit()[-1] |
---|
[c1e380e] | 1244 | |
---|
[906e0c7] | 1245 | delegate = self.lstPoly.itemDelegate() |
---|
| 1246 | |
---|
[06b0138] | 1247 | # Extract changed value. |
---|
[906e0c7] | 1248 | if model_column == delegate.poly_parameter: |
---|
[00b3b40] | 1249 | # Is the parameter checked for fitting? |
---|
[0268aed] | 1250 | value = item.checkState() |
---|
[1643d8ed] | 1251 | parameter_name = parameter_name + '.width' |
---|
[c1e380e] | 1252 | if value == QtCore.Qt.Checked: |
---|
[6dbff18] | 1253 | self.poly_params_to_fit.append(parameter_name) |
---|
[c1e380e] | 1254 | else: |
---|
[6dbff18] | 1255 | if parameter_name in self.poly_params_to_fit: |
---|
| 1256 | self.poly_params_to_fit.remove(parameter_name) |
---|
| 1257 | self.cmdFit.setEnabled(self.haveParamsToFit()) |
---|
[906e0c7] | 1258 | |
---|
| 1259 | elif model_column in [delegate.poly_min, delegate.poly_max]: |
---|
[aca8418] | 1260 | try: |
---|
[fbfc488] | 1261 | value = GuiUtils.toDouble(item.text()) |
---|
[0261bc1] | 1262 | except TypeError: |
---|
[aca8418] | 1263 | # Can't be converted properly, bring back the old value and exit |
---|
| 1264 | return |
---|
| 1265 | |
---|
| 1266 | current_details = self.kernel_module.details[parameter_name] |
---|
[906e0c7] | 1267 | if self.has_poly_error_column: |
---|
| 1268 | # err column changes the indexing |
---|
| 1269 | current_details[model_column-2] = value |
---|
| 1270 | else: |
---|
| 1271 | current_details[model_column-1] = value |
---|
| 1272 | |
---|
| 1273 | elif model_column == delegate.poly_function: |
---|
[919d47c] | 1274 | # name of the function - just pass |
---|
[906e0c7] | 1275 | pass |
---|
| 1276 | |
---|
[0268aed] | 1277 | else: |
---|
| 1278 | try: |
---|
[fbfc488] | 1279 | value = GuiUtils.toDouble(item.text()) |
---|
[0261bc1] | 1280 | except TypeError: |
---|
[0268aed] | 1281 | # Can't be converted properly, bring back the old value and exit |
---|
| 1282 | return |
---|
| 1283 | |
---|
[aca8418] | 1284 | # Update the sasmodel |
---|
| 1285 | # PD[ratio] -> width, npts -> npts, nsigs -> nsigmas |
---|
[66d4370] | 1286 | #self.kernel_module.setParam(parameter_name + '.' + delegate.columnDict()[model_column], value) |
---|
| 1287 | key = parameter_name + '.' + delegate.columnDict()[model_column] |
---|
| 1288 | self.poly_params[key] = value |
---|
[0268aed] | 1289 | |
---|
[9c0ce68] | 1290 | # Update plot |
---|
| 1291 | self.updateData() |
---|
| 1292 | |
---|
[906e0c7] | 1293 | # update in param model |
---|
| 1294 | if model_column in [delegate.poly_pd, delegate.poly_error, delegate.poly_min, delegate.poly_max]: |
---|
| 1295 | row = self.getRowFromName(parameter_name) |
---|
[f3cc979] | 1296 | param_item = self._model_model.item(row).child(0).child(0, model_column) |
---|
| 1297 | if param_item is None: |
---|
| 1298 | return |
---|
[73665a8] | 1299 | self._model_model.blockSignals(True) |
---|
[f3cc979] | 1300 | param_item.setText(item.text()) |
---|
[73665a8] | 1301 | self._model_model.blockSignals(False) |
---|
[906e0c7] | 1302 | |
---|
[d2007a8] | 1303 | def onMagnetModelChange(self, top, bottom): |
---|
[b00414d] | 1304 | """ |
---|
| 1305 | Callback method for updating the sasmodel magnetic parameters with the GUI values |
---|
| 1306 | """ |
---|
[d2007a8] | 1307 | item = self._magnet_model.itemFromIndex(top) |
---|
[b00414d] | 1308 | model_column = item.column() |
---|
| 1309 | model_row = item.row() |
---|
| 1310 | name_index = self._magnet_model.index(model_row, 0) |
---|
[fbfc488] | 1311 | parameter_name = str(self._magnet_model.data(name_index)) |
---|
[b00414d] | 1312 | |
---|
| 1313 | if model_column == 0: |
---|
| 1314 | value = item.checkState() |
---|
| 1315 | if value == QtCore.Qt.Checked: |
---|
[6dbff18] | 1316 | self.magnet_params_to_fit.append(parameter_name) |
---|
[b00414d] | 1317 | else: |
---|
[6dbff18] | 1318 | if parameter_name in self.magnet_params_to_fit: |
---|
| 1319 | self.magnet_params_to_fit.remove(parameter_name) |
---|
| 1320 | self.cmdFit.setEnabled(self.haveParamsToFit()) |
---|
[b00414d] | 1321 | # Update state stack |
---|
| 1322 | self.updateUndo() |
---|
| 1323 | return |
---|
| 1324 | |
---|
| 1325 | # Extract changed value. |
---|
| 1326 | try: |
---|
[fbfc488] | 1327 | value = GuiUtils.toDouble(item.text()) |
---|
[0261bc1] | 1328 | except TypeError: |
---|
[b00414d] | 1329 | # Unparsable field |
---|
| 1330 | return |
---|
[66d4370] | 1331 | delegate = self.lstMagnetic.itemDelegate() |
---|
| 1332 | |
---|
| 1333 | if model_column > 1: |
---|
| 1334 | if model_column == delegate.mag_min: |
---|
| 1335 | pos = 1 |
---|
| 1336 | elif model_column == delegate.mag_max: |
---|
| 1337 | pos = 2 |
---|
| 1338 | elif model_column == delegate.mag_unit: |
---|
| 1339 | pos = 0 |
---|
| 1340 | else: |
---|
| 1341 | raise AttributeError("Wrong column in magnetism table.") |
---|
| 1342 | # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf] |
---|
| 1343 | self.kernel_module.details[parameter_name][pos] = value |
---|
| 1344 | else: |
---|
| 1345 | self.magnet_params[parameter_name] = value |
---|
| 1346 | #self.kernel_module.setParam(parameter_name) = value |
---|
| 1347 | # Force the chart update when actual parameters changed |
---|
[b00414d] | 1348 | self.recalculatePlotData() |
---|
| 1349 | |
---|
| 1350 | # Update state stack |
---|
| 1351 | self.updateUndo() |
---|
| 1352 | |
---|
[2add354] | 1353 | def onHelp(self): |
---|
| 1354 | """ |
---|
| 1355 | Show the "Fitting" section of help |
---|
| 1356 | """ |
---|
[aed0532] | 1357 | tree_location = "/user/qtgui/Perspectives/Fitting/" |
---|
[70080a0] | 1358 | |
---|
| 1359 | # Actual file will depend on the current tab |
---|
| 1360 | tab_id = self.tabFitting.currentIndex() |
---|
| 1361 | helpfile = "fitting.html" |
---|
| 1362 | if tab_id == 0: |
---|
| 1363 | helpfile = "fitting_help.html" |
---|
| 1364 | elif tab_id == 1: |
---|
| 1365 | helpfile = "residuals_help.html" |
---|
| 1366 | elif tab_id == 2: |
---|
[e90988c] | 1367 | helpfile = "resolution.html" |
---|
[70080a0] | 1368 | elif tab_id == 3: |
---|
[e90988c] | 1369 | helpfile = "pd/polydispersity.html" |
---|
[70080a0] | 1370 | elif tab_id == 4: |
---|
[e90988c] | 1371 | helpfile = "magnetism/magnetism.html" |
---|
[70080a0] | 1372 | help_location = tree_location + helpfile |
---|
[d6b8a1d] | 1373 | |
---|
[e90988c] | 1374 | self.showHelp(help_location) |
---|
| 1375 | |
---|
| 1376 | def showHelp(self, url): |
---|
| 1377 | """ |
---|
| 1378 | Calls parent's method for opening an HTML page |
---|
| 1379 | """ |
---|
| 1380 | self.parent.showHelp(url) |
---|
[2add354] | 1381 | |
---|
[6ff2eb3] | 1382 | def onDisplayMagneticAngles(self): |
---|
| 1383 | """ |
---|
| 1384 | Display a simple image showing direction of magnetic angles |
---|
| 1385 | """ |
---|
| 1386 | self.magneticAnglesWidget.show() |
---|
| 1387 | |
---|
[0268aed] | 1388 | def onFit(self): |
---|
| 1389 | """ |
---|
| 1390 | Perform fitting on the current data |
---|
| 1391 | """ |
---|
[ded5e77] | 1392 | if self.fit_started: |
---|
| 1393 | self.stopFit() |
---|
| 1394 | return |
---|
| 1395 | |
---|
[116dd4c1] | 1396 | # initialize fitter constants |
---|
[f182f93] | 1397 | fit_id = 0 |
---|
| 1398 | handler = None |
---|
| 1399 | batch_inputs = {} |
---|
| 1400 | batch_outputs = {} |
---|
| 1401 | #--------------------------------- |
---|
[14ec91c5] | 1402 | if LocalConfig.USING_TWISTED: |
---|
[7adc2a8] | 1403 | handler = None |
---|
| 1404 | updater = None |
---|
| 1405 | else: |
---|
| 1406 | handler = ConsoleUpdate(parent=self.parent, |
---|
| 1407 | manager=self, |
---|
| 1408 | improvement_delta=0.1) |
---|
| 1409 | updater = handler.update_fit |
---|
[f182f93] | 1410 | |
---|
[116dd4c1] | 1411 | # Prepare the fitter object |
---|
[c6343a5] | 1412 | try: |
---|
| 1413 | fitters, _ = self.prepareFitters() |
---|
| 1414 | except ValueError as ex: |
---|
| 1415 | # This should not happen! GUI explicitly forbids this situation |
---|
[3b3b40b] | 1416 | self.communicate.statusBarUpdateSignal.emit(str(ex)) |
---|
[c6343a5] | 1417 | return |
---|
[f182f93] | 1418 | |
---|
[d4dac80] | 1419 | # keep local copy of kernel parameters, as they will change during the update |
---|
| 1420 | self.kernel_module_copy = copy.deepcopy(self.kernel_module) |
---|
| 1421 | |
---|
[f182f93] | 1422 | # Create the fitting thread, based on the fitter |
---|
[3b3b40b] | 1423 | completefn = self.batchFittingCompleted if self.is_batch_fitting else self.fittingCompleted |
---|
[ee18d33] | 1424 | |
---|
[ded5e77] | 1425 | self.calc_fit = FitThread(handler=handler, |
---|
[116dd4c1] | 1426 | fn=fitters, |
---|
| 1427 | batch_inputs=batch_inputs, |
---|
| 1428 | batch_outputs=batch_outputs, |
---|
| 1429 | page_id=[[self.page_id]], |
---|
| 1430 | updatefn=updater, |
---|
[91ad45c] | 1431 | completefn=completefn, |
---|
| 1432 | reset_flag=self.is_chain_fitting) |
---|
[7adc2a8] | 1433 | |
---|
[14ec91c5] | 1434 | if LocalConfig.USING_TWISTED: |
---|
[7adc2a8] | 1435 | # start the trhrhread with twisted |
---|
[ded5e77] | 1436 | calc_thread = threads.deferToThread(self.calc_fit.compute) |
---|
[14ec91c5] | 1437 | calc_thread.addCallback(completefn) |
---|
[7adc2a8] | 1438 | calc_thread.addErrback(self.fitFailed) |
---|
| 1439 | else: |
---|
| 1440 | # Use the old python threads + Queue |
---|
[ded5e77] | 1441 | self.calc_fit.queue() |
---|
| 1442 | self.calc_fit.ready(2.5) |
---|
[f182f93] | 1443 | |
---|
[d7ff531] | 1444 | self.communicate.statusBarUpdateSignal.emit('Fitting started...') |
---|
[ded5e77] | 1445 | self.fit_started = True |
---|
[557fc498] | 1446 | |
---|
[14ec91c5] | 1447 | # Disable some elements |
---|
[557fc498] | 1448 | self.disableInteractiveElements() |
---|
[0268aed] | 1449 | |
---|
[ded5e77] | 1450 | def stopFit(self): |
---|
| 1451 | """ |
---|
| 1452 | Attempt to stop the fitting thread |
---|
| 1453 | """ |
---|
| 1454 | if self.calc_fit is None or not self.calc_fit.isrunning(): |
---|
| 1455 | return |
---|
| 1456 | self.calc_fit.stop() |
---|
| 1457 | #re-enable the Fit button |
---|
[557fc498] | 1458 | self.enableInteractiveElements() |
---|
[ded5e77] | 1459 | |
---|
| 1460 | msg = "Fitting cancelled." |
---|
| 1461 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1462 | |
---|
[f182f93] | 1463 | def updateFit(self): |
---|
| 1464 | """ |
---|
| 1465 | """ |
---|
[b3e8629] | 1466 | print("UPDATE FIT") |
---|
[0268aed] | 1467 | pass |
---|
| 1468 | |
---|
[02ddfb4] | 1469 | def fitFailed(self, reason): |
---|
| 1470 | """ |
---|
| 1471 | """ |
---|
[557fc498] | 1472 | self.enableInteractiveElements() |
---|
[ded5e77] | 1473 | msg = "Fitting failed with: "+ str(reason) |
---|
| 1474 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
[02ddfb4] | 1475 | |
---|
[3b3b40b] | 1476 | def batchFittingCompleted(self, result): |
---|
| 1477 | """ |
---|
| 1478 | Send the finish message from calculate threads to main thread |
---|
| 1479 | """ |
---|
[a2cc8b97] | 1480 | if result is None: |
---|
| 1481 | result = tuple() |
---|
[3b3b40b] | 1482 | self.batchFittingFinishedSignal.emit(result) |
---|
| 1483 | |
---|
[ee18d33] | 1484 | def batchFitComplete(self, result): |
---|
| 1485 | """ |
---|
| 1486 | Receive and display batch fitting results |
---|
| 1487 | """ |
---|
| 1488 | #re-enable the Fit button |
---|
[557fc498] | 1489 | self.enableInteractiveElements() |
---|
[d4dac80] | 1490 | |
---|
[a2cc8b97] | 1491 | if len(result) == 0: |
---|
[d4dac80] | 1492 | msg = "Fitting failed." |
---|
| 1493 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1494 | return |
---|
| 1495 | |
---|
[3b3b40b] | 1496 | # Show the grid panel |
---|
[d4dac80] | 1497 | self.communicate.sendDataToGridSignal.emit(result[0]) |
---|
| 1498 | |
---|
| 1499 | elapsed = result[1] |
---|
| 1500 | msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed) |
---|
| 1501 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1502 | |
---|
| 1503 | # Run over the list of results and update the items |
---|
| 1504 | for res_index, res_list in enumerate(result[0]): |
---|
| 1505 | # results |
---|
| 1506 | res = res_list[0] |
---|
| 1507 | param_dict = self.paramDictFromResults(res) |
---|
| 1508 | |
---|
| 1509 | # create local kernel_module |
---|
| 1510 | kernel_module = FittingUtilities.updateKernelWithResults(self.kernel_module, param_dict) |
---|
| 1511 | # pull out current data |
---|
| 1512 | data = self._logic[res_index].data |
---|
| 1513 | |
---|
| 1514 | # Switch indexes |
---|
| 1515 | self.onSelectBatchFilename(res_index) |
---|
| 1516 | |
---|
| 1517 | method = self.complete1D if isinstance(self.data, Data1D) else self.complete2D |
---|
| 1518 | self.calculateQGridForModelExt(data=data, model=kernel_module, completefn=method, use_threads=False) |
---|
| 1519 | |
---|
| 1520 | # Restore original kernel_module, so subsequent fits on the same model don't pick up the new params |
---|
| 1521 | if self.kernel_module is not None: |
---|
| 1522 | self.kernel_module = copy.deepcopy(self.kernel_module_copy) |
---|
| 1523 | |
---|
| 1524 | def paramDictFromResults(self, results): |
---|
| 1525 | """ |
---|
| 1526 | Given the fit results structure, pull out optimized parameters and return them as nicely |
---|
| 1527 | formatted dict |
---|
| 1528 | """ |
---|
| 1529 | if results.fitness is None or \ |
---|
| 1530 | not np.isfinite(results.fitness) or \ |
---|
| 1531 | np.any(results.pvec is None) or \ |
---|
| 1532 | not np.all(np.isfinite(results.pvec)): |
---|
| 1533 | msg = "Fitting did not converge!" |
---|
| 1534 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1535 | msg += results.mesg |
---|
[bb477f5] | 1536 | logger.error(msg) |
---|
[d4dac80] | 1537 | return |
---|
| 1538 | |
---|
| 1539 | param_list = results.param_list # ['radius', 'radius.width'] |
---|
| 1540 | param_values = results.pvec # array([ 0.36221662, 0.0146783 ]) |
---|
| 1541 | param_stderr = results.stderr # array([ 1.71293015, 1.71294233]) |
---|
| 1542 | params_and_errors = list(zip(param_values, param_stderr)) |
---|
| 1543 | param_dict = dict(zip(param_list, params_and_errors)) |
---|
| 1544 | |
---|
| 1545 | return param_dict |
---|
[3b3b40b] | 1546 | |
---|
| 1547 | def fittingCompleted(self, result): |
---|
| 1548 | """ |
---|
| 1549 | Send the finish message from calculate threads to main thread |
---|
| 1550 | """ |
---|
[a2cc8b97] | 1551 | if result is None: |
---|
| 1552 | result = tuple() |
---|
[3b3b40b] | 1553 | self.fittingFinishedSignal.emit(result) |
---|
[ee18d33] | 1554 | |
---|
[f182f93] | 1555 | def fitComplete(self, result): |
---|
| 1556 | """ |
---|
| 1557 | Receive and display fitting results |
---|
| 1558 | "result" is a tuple of actual result list and the fit time in seconds |
---|
| 1559 | """ |
---|
| 1560 | #re-enable the Fit button |
---|
[557fc498] | 1561 | self.enableInteractiveElements() |
---|
[d7ff531] | 1562 | |
---|
[a2cc8b97] | 1563 | if len(result) == 0: |
---|
[3b3b40b] | 1564 | msg = "Fitting failed." |
---|
[06234fc] | 1565 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1566 | return |
---|
[d7ff531] | 1567 | |
---|
[ee18d33] | 1568 | res_list = result[0][0] |
---|
[f182f93] | 1569 | res = res_list[0] |
---|
[d4dac80] | 1570 | self.chi2 = res.fitness |
---|
| 1571 | param_dict = self.paramDictFromResults(res) |
---|
[f182f93] | 1572 | |
---|
[14acf92] | 1573 | if param_dict is None: |
---|
| 1574 | return |
---|
| 1575 | |
---|
[f182f93] | 1576 | elapsed = result[1] |
---|
[9d130f3] | 1577 | if self.calc_fit is not None and self.calc_fit._interrupting: |
---|
[ded5e77] | 1578 | msg = "Fitting cancelled by user after: %s s." % GuiUtils.formatNumber(elapsed) |
---|
[bb477f5] | 1579 | logger.warning("\n"+msg+"\n") |
---|
[ded5e77] | 1580 | else: |
---|
| 1581 | msg = "Fitting completed successfully in: %s s." % GuiUtils.formatNumber(elapsed) |
---|
[f182f93] | 1582 | self.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 1583 | |
---|
| 1584 | # Dictionary of fitted parameter: value, error |
---|
| 1585 | # e.g. param_dic = {"sld":(1.703, 0.0034), "length":(33.455, -0.0983)} |
---|
| 1586 | self.updateModelFromList(param_dict) |
---|
| 1587 | |
---|
[aca8418] | 1588 | self.updatePolyModelFromList(param_dict) |
---|
| 1589 | |
---|
[b00414d] | 1590 | self.updateMagnetModelFromList(param_dict) |
---|
| 1591 | |
---|
[d7ff531] | 1592 | # update charts |
---|
| 1593 | self.onPlot() |
---|
[c2f3ca2] | 1594 | #self.recalculatePlotData() |
---|
| 1595 | |
---|
[d7ff531] | 1596 | |
---|
[f182f93] | 1597 | # Read only value - we can get away by just printing it here |
---|
[2add354] | 1598 | chi2_repr = GuiUtils.formatNumber(self.chi2, high=True) |
---|
[f182f93] | 1599 | self.lblChi2Value.setText(chi2_repr) |
---|
| 1600 | |
---|
[116dd4c1] | 1601 | def prepareFitters(self, fitter=None, fit_id=0): |
---|
| 1602 | """ |
---|
| 1603 | Prepare the Fitter object for use in fitting |
---|
| 1604 | """ |
---|
| 1605 | # fitter = None -> single/batch fitting |
---|
| 1606 | # fitter = Fit() -> simultaneous fitting |
---|
| 1607 | |
---|
| 1608 | # Data going in |
---|
| 1609 | data = self.logic.data |
---|
[66d4370] | 1610 | model = copy.deepcopy(self.kernel_module) |
---|
[116dd4c1] | 1611 | qmin = self.q_range_min |
---|
| 1612 | qmax = self.q_range_max |
---|
[66d4370] | 1613 | # add polydisperse/magnet parameters if asked |
---|
| 1614 | self.updateKernelModelWithExtraParams(model) |
---|
[6dbff18] | 1615 | |
---|
[d4216ac] | 1616 | params_to_fit = copy.deepcopy(self.main_params_to_fit) |
---|
[6dbff18] | 1617 | if self.chkPolydispersity.isChecked(): |
---|
| 1618 | params_to_fit += self.poly_params_to_fit |
---|
[a98014ab] | 1619 | if self.chkMagnetism.isChecked() and self.canHaveMagnetism(): |
---|
[6dbff18] | 1620 | params_to_fit += self.magnet_params_to_fit |
---|
[8e2cd79] | 1621 | if not params_to_fit: |
---|
[c6343a5] | 1622 | raise ValueError('Fitting requires at least one parameter to optimize.') |
---|
[116dd4c1] | 1623 | |
---|
[8b480d27] | 1624 | # Get the constraints. |
---|
| 1625 | constraints = self.getComplexConstraintsForModel() |
---|
| 1626 | if fitter is None: |
---|
| 1627 | # For single fits - check for inter-model constraints |
---|
| 1628 | constraints = self.getConstraintsForFitting() |
---|
[3b3b40b] | 1629 | |
---|
[9a7c81c] | 1630 | smearer = self.smearing_widget.smearer() |
---|
[116dd4c1] | 1631 | handler = None |
---|
| 1632 | batch_inputs = {} |
---|
| 1633 | batch_outputs = {} |
---|
| 1634 | |
---|
| 1635 | fitters = [] |
---|
| 1636 | for fit_index in self.all_data: |
---|
| 1637 | fitter_single = Fit() if fitter is None else fitter |
---|
| 1638 | data = GuiUtils.dataFromItem(fit_index) |
---|
[9a7c81c] | 1639 | # Potential weights added directly to data |
---|
[b764ae5] | 1640 | weighted_data = self.addWeightingToData(data) |
---|
[116dd4c1] | 1641 | try: |
---|
[b764ae5] | 1642 | fitter_single.set_model(model, fit_id, params_to_fit, data=weighted_data, |
---|
[116dd4c1] | 1643 | constraints=constraints) |
---|
| 1644 | except ValueError as ex: |
---|
[3b3b40b] | 1645 | raise ValueError("Setting model parameters failed with: %s" % ex) |
---|
[116dd4c1] | 1646 | |
---|
[b764ae5] | 1647 | qmin, qmax, _ = self.logic.computeRangeFromData(weighted_data) |
---|
| 1648 | fitter_single.set_data(data=weighted_data, id=fit_id, smearer=smearer, qmin=qmin, |
---|
[116dd4c1] | 1649 | qmax=qmax) |
---|
| 1650 | fitter_single.select_problem_for_fit(id=fit_id, value=1) |
---|
| 1651 | if fitter is None: |
---|
| 1652 | # Assign id to the new fitter only |
---|
| 1653 | fitter_single.fitter_id = [self.page_id] |
---|
| 1654 | fit_id += 1 |
---|
| 1655 | fitters.append(fitter_single) |
---|
| 1656 | |
---|
| 1657 | return fitters, fit_id |
---|
| 1658 | |
---|
[f182f93] | 1659 | def iterateOverModel(self, func): |
---|
| 1660 | """ |
---|
| 1661 | Take func and throw it inside the model row loop |
---|
| 1662 | """ |
---|
[b3e8629] | 1663 | for row_i in range(self._model_model.rowCount()): |
---|
[f182f93] | 1664 | func(row_i) |
---|
| 1665 | |
---|
| 1666 | def updateModelFromList(self, param_dict): |
---|
| 1667 | """ |
---|
| 1668 | Update the model with new parameters, create the errors column |
---|
| 1669 | """ |
---|
| 1670 | assert isinstance(param_dict, dict) |
---|
| 1671 | if not dict: |
---|
| 1672 | return |
---|
| 1673 | |
---|
[919d47c] | 1674 | def updateFittedValues(row): |
---|
[f182f93] | 1675 | # Utility function for main model update |
---|
[d7ff531] | 1676 | # internal so can use closure for param_dict |
---|
[919d47c] | 1677 | param_name = str(self._model_model.item(row, 0).text()) |
---|
[00b7ddf0] | 1678 | if not self.isCheckable(row) or param_name not in list(param_dict.keys()): |
---|
[f182f93] | 1679 | return |
---|
| 1680 | # modify the param value |
---|
[454670d] | 1681 | param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True) |
---|
[919d47c] | 1682 | self._model_model.item(row, 1).setText(param_repr) |
---|
[c2f3ca2] | 1683 | self.kernel_module.setParam(param_name, param_dict[param_name][0]) |
---|
[f182f93] | 1684 | if self.has_error_column: |
---|
[454670d] | 1685 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
[919d47c] | 1686 | self._model_model.item(row, 2).setText(error_repr) |
---|
[f182f93] | 1687 | |
---|
[919d47c] | 1688 | def updatePolyValues(row): |
---|
| 1689 | # Utility function for updateof polydispersity part of the main model |
---|
| 1690 | param_name = str(self._model_model.item(row, 0).text())+'.width' |
---|
[00b7ddf0] | 1691 | if not self.isCheckable(row) or param_name not in list(param_dict.keys()): |
---|
[919d47c] | 1692 | return |
---|
| 1693 | # modify the param value |
---|
| 1694 | param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True) |
---|
| 1695 | self._model_model.item(row, 0).child(0).child(0,1).setText(param_repr) |
---|
[906e0c7] | 1696 | # modify the param error |
---|
| 1697 | if self.has_error_column: |
---|
| 1698 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1699 | self._model_model.item(row, 0).child(0).child(0,2).setText(error_repr) |
---|
[919d47c] | 1700 | |
---|
| 1701 | def createErrorColumn(row): |
---|
[f182f93] | 1702 | # Utility function for error column update |
---|
| 1703 | item = QtGui.QStandardItem() |
---|
[919d47c] | 1704 | def createItem(param_name): |
---|
[f182f93] | 1705 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1706 | item.setText(error_repr) |
---|
[919d47c] | 1707 | def curr_param(): |
---|
| 1708 | return str(self._model_model.item(row, 0).text()) |
---|
| 1709 | |
---|
[b3e8629] | 1710 | [createItem(param_name) for param_name in list(param_dict.keys()) if curr_param() == param_name] |
---|
[919d47c] | 1711 | |
---|
[f182f93] | 1712 | error_column.append(item) |
---|
| 1713 | |
---|
[906e0c7] | 1714 | def createPolyErrorColumn(row): |
---|
| 1715 | # Utility function for error column update in the polydispersity sub-rows |
---|
| 1716 | # NOTE: only creates empty items; updatePolyValues adds the error value |
---|
| 1717 | item = self._model_model.item(row, 0) |
---|
| 1718 | if not item.hasChildren(): |
---|
| 1719 | return |
---|
| 1720 | poly_item = item.child(0) |
---|
| 1721 | if not poly_item.hasChildren(): |
---|
| 1722 | return |
---|
| 1723 | poly_item.insertColumn(2, [QtGui.QStandardItem("")]) |
---|
| 1724 | |
---|
| 1725 | if not self.has_error_column: |
---|
| 1726 | # create top-level error column |
---|
| 1727 | error_column = [] |
---|
| 1728 | self.lstParams.itemDelegate().addErrorColumn() |
---|
| 1729 | self.iterateOverModel(createErrorColumn) |
---|
| 1730 | |
---|
| 1731 | self._model_model.insertColumn(2, error_column) |
---|
| 1732 | |
---|
| 1733 | FittingUtilities.addErrorHeadersToModel(self._model_model) |
---|
| 1734 | |
---|
| 1735 | # create error column in polydispersity sub-rows |
---|
| 1736 | self.iterateOverModel(createPolyErrorColumn) |
---|
| 1737 | |
---|
| 1738 | self.has_error_column = True |
---|
| 1739 | |
---|
[c2f3ca2] | 1740 | # block signals temporarily, so we don't end up |
---|
| 1741 | # updating charts with every single model change on the end of fitting |
---|
[d2007a8] | 1742 | self._model_model.dataChanged.disconnect() |
---|
[d7ff531] | 1743 | self.iterateOverModel(updateFittedValues) |
---|
[919d47c] | 1744 | self.iterateOverModel(updatePolyValues) |
---|
[d2007a8] | 1745 | self._model_model.dataChanged.connect(self.onMainParamsChange) |
---|
[f182f93] | 1746 | |
---|
[8e2cd79] | 1747 | def iterateOverPolyModel(self, func): |
---|
| 1748 | """ |
---|
| 1749 | Take func and throw it inside the poly model row loop |
---|
| 1750 | """ |
---|
| 1751 | for row_i in range(self._poly_model.rowCount()): |
---|
| 1752 | func(row_i) |
---|
| 1753 | |
---|
[aca8418] | 1754 | def updatePolyModelFromList(self, param_dict): |
---|
| 1755 | """ |
---|
| 1756 | Update the polydispersity model with new parameters, create the errors column |
---|
| 1757 | """ |
---|
| 1758 | assert isinstance(param_dict, dict) |
---|
| 1759 | if not dict: |
---|
| 1760 | return |
---|
| 1761 | |
---|
| 1762 | def updateFittedValues(row_i): |
---|
| 1763 | # Utility function for main model update |
---|
| 1764 | # internal so can use closure for param_dict |
---|
| 1765 | if row_i >= self._poly_model.rowCount(): |
---|
| 1766 | return |
---|
| 1767 | param_name = str(self._poly_model.item(row_i, 0).text()).rsplit()[-1] + '.width' |
---|
[b3e8629] | 1768 | if param_name not in list(param_dict.keys()): |
---|
[aca8418] | 1769 | return |
---|
| 1770 | # modify the param value |
---|
| 1771 | param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True) |
---|
| 1772 | self._poly_model.item(row_i, 1).setText(param_repr) |
---|
[c2f3ca2] | 1773 | self.kernel_module.setParam(param_name, param_dict[param_name][0]) |
---|
[aca8418] | 1774 | if self.has_poly_error_column: |
---|
| 1775 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1776 | self._poly_model.item(row_i, 2).setText(error_repr) |
---|
| 1777 | |
---|
| 1778 | def createErrorColumn(row_i): |
---|
| 1779 | # Utility function for error column update |
---|
| 1780 | if row_i >= self._poly_model.rowCount(): |
---|
| 1781 | return |
---|
| 1782 | item = QtGui.QStandardItem() |
---|
[919d47c] | 1783 | |
---|
| 1784 | def createItem(param_name): |
---|
[aca8418] | 1785 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1786 | item.setText(error_repr) |
---|
[919d47c] | 1787 | |
---|
| 1788 | def poly_param(): |
---|
| 1789 | return str(self._poly_model.item(row_i, 0).text()).rsplit()[-1] + '.width' |
---|
| 1790 | |
---|
[b3e8629] | 1791 | [createItem(param_name) for param_name in list(param_dict.keys()) if poly_param() == param_name] |
---|
[919d47c] | 1792 | |
---|
[aca8418] | 1793 | error_column.append(item) |
---|
| 1794 | |
---|
| 1795 | # block signals temporarily, so we don't end up |
---|
| 1796 | # updating charts with every single model change on the end of fitting |
---|
[d2007a8] | 1797 | self._poly_model.dataChanged.disconnect() |
---|
[8e2cd79] | 1798 | self.iterateOverPolyModel(updateFittedValues) |
---|
[d2007a8] | 1799 | self._poly_model.dataChanged.connect(self.onPolyModelChange) |
---|
[aca8418] | 1800 | |
---|
| 1801 | if self.has_poly_error_column: |
---|
| 1802 | return |
---|
| 1803 | |
---|
[8eaa101] | 1804 | self.lstPoly.itemDelegate().addErrorColumn() |
---|
[aca8418] | 1805 | error_column = [] |
---|
[8e2cd79] | 1806 | self.iterateOverPolyModel(createErrorColumn) |
---|
[aca8418] | 1807 | |
---|
| 1808 | # switch off reponse to model change |
---|
| 1809 | self._poly_model.insertColumn(2, error_column) |
---|
| 1810 | FittingUtilities.addErrorPolyHeadersToModel(self._poly_model) |
---|
| 1811 | |
---|
| 1812 | self.has_poly_error_column = True |
---|
| 1813 | |
---|
[8e2cd79] | 1814 | def iterateOverMagnetModel(self, func): |
---|
| 1815 | """ |
---|
| 1816 | Take func and throw it inside the magnet model row loop |
---|
| 1817 | """ |
---|
[30339cc] | 1818 | for row_i in range(self._magnet_model.rowCount()): |
---|
[8e2cd79] | 1819 | func(row_i) |
---|
| 1820 | |
---|
[b00414d] | 1821 | def updateMagnetModelFromList(self, param_dict): |
---|
| 1822 | """ |
---|
| 1823 | Update the magnetic model with new parameters, create the errors column |
---|
| 1824 | """ |
---|
| 1825 | assert isinstance(param_dict, dict) |
---|
| 1826 | if not dict: |
---|
| 1827 | return |
---|
[3b3b40b] | 1828 | if self._magnet_model.rowCount() == 0: |
---|
[cee5c78] | 1829 | return |
---|
[b00414d] | 1830 | |
---|
| 1831 | def updateFittedValues(row): |
---|
| 1832 | # Utility function for main model update |
---|
| 1833 | # internal so can use closure for param_dict |
---|
[cee5c78] | 1834 | if self._magnet_model.item(row, 0) is None: |
---|
| 1835 | return |
---|
[b00414d] | 1836 | param_name = str(self._magnet_model.item(row, 0).text()) |
---|
[b3e8629] | 1837 | if param_name not in list(param_dict.keys()): |
---|
[b00414d] | 1838 | return |
---|
| 1839 | # modify the param value |
---|
| 1840 | param_repr = GuiUtils.formatNumber(param_dict[param_name][0], high=True) |
---|
| 1841 | self._magnet_model.item(row, 1).setText(param_repr) |
---|
[c2f3ca2] | 1842 | self.kernel_module.setParam(param_name, param_dict[param_name][0]) |
---|
[b00414d] | 1843 | if self.has_magnet_error_column: |
---|
| 1844 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1845 | self._magnet_model.item(row, 2).setText(error_repr) |
---|
| 1846 | |
---|
| 1847 | def createErrorColumn(row): |
---|
| 1848 | # Utility function for error column update |
---|
| 1849 | item = QtGui.QStandardItem() |
---|
| 1850 | def createItem(param_name): |
---|
| 1851 | error_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 1852 | item.setText(error_repr) |
---|
| 1853 | def curr_param(): |
---|
| 1854 | return str(self._magnet_model.item(row, 0).text()) |
---|
| 1855 | |
---|
[b3e8629] | 1856 | [createItem(param_name) for param_name in list(param_dict.keys()) if curr_param() == param_name] |
---|
[b00414d] | 1857 | |
---|
| 1858 | error_column.append(item) |
---|
| 1859 | |
---|
| 1860 | # block signals temporarily, so we don't end up |
---|
| 1861 | # updating charts with every single model change on the end of fitting |
---|
[d2007a8] | 1862 | self._magnet_model.dataChanged.disconnect() |
---|
[8e2cd79] | 1863 | self.iterateOverMagnetModel(updateFittedValues) |
---|
[d2007a8] | 1864 | self._magnet_model.dataChanged.connect(self.onMagnetModelChange) |
---|
[b00414d] | 1865 | |
---|
| 1866 | if self.has_magnet_error_column: |
---|
| 1867 | return |
---|
| 1868 | |
---|
| 1869 | self.lstMagnetic.itemDelegate().addErrorColumn() |
---|
| 1870 | error_column = [] |
---|
[8e2cd79] | 1871 | self.iterateOverMagnetModel(createErrorColumn) |
---|
[b00414d] | 1872 | |
---|
| 1873 | # switch off reponse to model change |
---|
| 1874 | self._magnet_model.insertColumn(2, error_column) |
---|
| 1875 | FittingUtilities.addErrorHeadersToModel(self._magnet_model) |
---|
| 1876 | |
---|
| 1877 | self.has_magnet_error_column = True |
---|
| 1878 | |
---|
[0268aed] | 1879 | def onPlot(self): |
---|
| 1880 | """ |
---|
| 1881 | Plot the current set of data |
---|
| 1882 | """ |
---|
[d48cc19] | 1883 | # Regardless of previous state, this should now be `plot show` functionality only |
---|
| 1884 | self.cmdPlot.setText("Show Plot") |
---|
[88e1f57] | 1885 | # Force data recalculation so existing charts are updated |
---|
[d1e4689] | 1886 | if not self.data_is_loaded: |
---|
| 1887 | self.showTheoryPlot() |
---|
| 1888 | else: |
---|
| 1889 | self.showPlot() |
---|
[0cd98a1] | 1890 | # This is an important processEvent. |
---|
| 1891 | # This allows charts to be properly updated in order |
---|
| 1892 | # of plots being applied. |
---|
| 1893 | QtWidgets.QApplication.processEvents() |
---|
[33d5956] | 1894 | self.recalculatePlotData() # recalc+plot theory again (2nd) |
---|
[d48cc19] | 1895 | |
---|
[9a7c81c] | 1896 | def onSmearingOptionsUpdate(self): |
---|
| 1897 | """ |
---|
| 1898 | React to changes in the smearing widget |
---|
| 1899 | """ |
---|
| 1900 | self.calculateQGridForModel() |
---|
| 1901 | |
---|
[d48cc19] | 1902 | def recalculatePlotData(self): |
---|
| 1903 | """ |
---|
| 1904 | Generate a new dataset for model |
---|
| 1905 | """ |
---|
[180bd54] | 1906 | if not self.data_is_loaded: |
---|
[0268aed] | 1907 | self.createDefaultDataset() |
---|
| 1908 | self.calculateQGridForModel() |
---|
| 1909 | |
---|
[d1e4689] | 1910 | def showTheoryPlot(self): |
---|
| 1911 | """ |
---|
| 1912 | Show the current theory plot in MPL |
---|
| 1913 | """ |
---|
| 1914 | # Show the chart if ready |
---|
| 1915 | if self.theory_item is None: |
---|
| 1916 | self.recalculatePlotData() |
---|
[33d5956] | 1917 | elif self.model_data: |
---|
[f9be147] | 1918 | self._requestPlots(self.model_data.filename, self.theory_item.model()) |
---|
[d1e4689] | 1919 | |
---|
[d48cc19] | 1920 | def showPlot(self): |
---|
| 1921 | """ |
---|
| 1922 | Show the current plot in MPL |
---|
| 1923 | """ |
---|
| 1924 | # Show the chart if ready |
---|
[d1e4689] | 1925 | data_to_show = self.data |
---|
[9ce69ec] | 1926 | # Any models for this page |
---|
| 1927 | current_index = self.all_data[self.data_index] |
---|
[f9be147] | 1928 | item = self._requestPlots(self.data.filename, current_index.model()) |
---|
| 1929 | if item: |
---|
| 1930 | # fit+data has not been shown - show just data |
---|
| 1931 | self.communicate.plotRequestedSignal.emit([item, data_to_show], self.tab_id) |
---|
| 1932 | |
---|
| 1933 | def _requestPlots(self, item_name, item_model): |
---|
| 1934 | """ |
---|
| 1935 | Emits plotRequestedSignal for all plots found in the given model under the provided item name. |
---|
| 1936 | """ |
---|
| 1937 | fitpage_name = "" if self.tab_id is None else "M"+str(self.tab_id) |
---|
| 1938 | plots = GuiUtils.plotsFromFilename(item_name, item_model) |
---|
[9ce69ec] | 1939 | # Has the fitted data been shown? |
---|
| 1940 | data_shown = False |
---|
[f9be147] | 1941 | item = None |
---|
[9ce69ec] | 1942 | for item, plot in plots.items(): |
---|
| 1943 | if fitpage_name in plot.name: |
---|
| 1944 | data_shown = True |
---|
| 1945 | self.communicate.plotRequestedSignal.emit([item, plot], self.tab_id) |
---|
[f9be147] | 1946 | # return the last data item seen, if nothing was plotted; supposed to be just data) |
---|
| 1947 | return None if data_shown else item |
---|
[d48cc19] | 1948 | |
---|
[180bd54] | 1949 | def onOptionsUpdate(self): |
---|
[0268aed] | 1950 | """ |
---|
[180bd54] | 1951 | Update local option values and replot |
---|
[0268aed] | 1952 | """ |
---|
[180bd54] | 1953 | self.q_range_min, self.q_range_max, self.npts, self.log_points, self.weighting = \ |
---|
| 1954 | self.options_widget.state() |
---|
[61a92d4] | 1955 | # set Q range labels on the main tab |
---|
[34e5170] | 1956 | self.lblMinRangeDef.setText(GuiUtils.formatNumber(self.q_range_min, high=True)) |
---|
| 1957 | self.lblMaxRangeDef.setText(GuiUtils.formatNumber(self.q_range_max, high=True)) |
---|
[d48cc19] | 1958 | self.recalculatePlotData() |
---|
[6c8fb2c] | 1959 | |
---|
[0268aed] | 1960 | def setDefaultStructureCombo(self): |
---|
| 1961 | """ |
---|
| 1962 | Fill in the structure factors combo box with defaults |
---|
| 1963 | """ |
---|
| 1964 | structure_factor_list = self.master_category_dict.pop(CATEGORY_STRUCTURE) |
---|
| 1965 | factors = [factor[0] for factor in structure_factor_list] |
---|
| 1966 | factors.insert(0, STRUCTURE_DEFAULT) |
---|
| 1967 | self.cbStructureFactor.clear() |
---|
| 1968 | self.cbStructureFactor.addItems(sorted(factors)) |
---|
| 1969 | |
---|
[4d457df] | 1970 | def createDefaultDataset(self): |
---|
| 1971 | """ |
---|
| 1972 | Generate default Dataset 1D/2D for the given model |
---|
| 1973 | """ |
---|
| 1974 | # Create default datasets if no data passed |
---|
| 1975 | if self.is2D: |
---|
[180bd54] | 1976 | qmax = self.q_range_max/np.sqrt(2) |
---|
[4d457df] | 1977 | qstep = self.npts |
---|
| 1978 | self.logic.createDefault2dData(qmax, qstep, self.tab_id) |
---|
[180bd54] | 1979 | return |
---|
| 1980 | elif self.log_points: |
---|
| 1981 | qmin = -10.0 if self.q_range_min < 1.e-10 else np.log10(self.q_range_min) |
---|
[1bc27f1] | 1982 | qmax = 10.0 if self.q_range_max > 1.e10 else np.log10(self.q_range_max) |
---|
[180bd54] | 1983 | interval = np.logspace(start=qmin, stop=qmax, num=self.npts, endpoint=True, base=10.0) |
---|
[4d457df] | 1984 | else: |
---|
[180bd54] | 1985 | interval = np.linspace(start=self.q_range_min, stop=self.q_range_max, |
---|
[1bc27f1] | 1986 | num=self.npts, endpoint=True) |
---|
[180bd54] | 1987 | self.logic.createDefault1dData(interval, self.tab_id) |
---|
[60af928] | 1988 | |
---|
[5236449] | 1989 | def readCategoryInfo(self): |
---|
[60af928] | 1990 | """ |
---|
| 1991 | Reads the categories in from file |
---|
| 1992 | """ |
---|
| 1993 | self.master_category_dict = defaultdict(list) |
---|
| 1994 | self.by_model_dict = defaultdict(list) |
---|
| 1995 | self.model_enabled_dict = defaultdict(bool) |
---|
| 1996 | |
---|
[cbcdd2c] | 1997 | categorization_file = CategoryInstaller.get_user_file() |
---|
| 1998 | if not os.path.isfile(categorization_file): |
---|
| 1999 | categorization_file = CategoryInstaller.get_default_file() |
---|
| 2000 | with open(categorization_file, 'rb') as cat_file: |
---|
[60af928] | 2001 | self.master_category_dict = json.load(cat_file) |
---|
[5236449] | 2002 | self.regenerateModelDict() |
---|
[60af928] | 2003 | |
---|
[5236449] | 2004 | # Load the model dict |
---|
| 2005 | models = load_standard_models() |
---|
| 2006 | for model in models: |
---|
| 2007 | self.models[model.name] = model |
---|
| 2008 | |
---|
[3b3b40b] | 2009 | self.readCustomCategoryInfo() |
---|
| 2010 | |
---|
| 2011 | def readCustomCategoryInfo(self): |
---|
| 2012 | """ |
---|
| 2013 | Reads the custom model category |
---|
| 2014 | """ |
---|
| 2015 | #Looking for plugins |
---|
| 2016 | self.plugins = list(self.custom_models.values()) |
---|
| 2017 | plugin_list = [] |
---|
| 2018 | for name, plug in self.custom_models.items(): |
---|
| 2019 | self.models[name] = plug |
---|
| 2020 | plugin_list.append([name, True]) |
---|
[2dce1d8] | 2021 | if plugin_list: |
---|
| 2022 | self.master_category_dict[CATEGORY_CUSTOM] = plugin_list |
---|
[3b3b40b] | 2023 | |
---|
[5236449] | 2024 | def regenerateModelDict(self): |
---|
[60af928] | 2025 | """ |
---|
[cbcdd2c] | 2026 | Regenerates self.by_model_dict which has each model name as the |
---|
[60af928] | 2027 | key and the list of categories belonging to that model |
---|
| 2028 | along with the enabled mapping |
---|
| 2029 | """ |
---|
| 2030 | self.by_model_dict = defaultdict(list) |
---|
| 2031 | for category in self.master_category_dict: |
---|
| 2032 | for (model, enabled) in self.master_category_dict[category]: |
---|
| 2033 | self.by_model_dict[model].append(category) |
---|
| 2034 | self.model_enabled_dict[model] = enabled |
---|
| 2035 | |
---|
[86f88d1] | 2036 | def addBackgroundToModel(self, model): |
---|
| 2037 | """ |
---|
| 2038 | Adds background parameter with default values to the model |
---|
| 2039 | """ |
---|
[cbcdd2c] | 2040 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
[86f88d1] | 2041 | checked_list = ['background', '0.001', '-inf', 'inf', '1/cm'] |
---|
[4d457df] | 2042 | FittingUtilities.addCheckedListToModel(model, checked_list) |
---|
[2add354] | 2043 | last_row = model.rowCount()-1 |
---|
| 2044 | model.item(last_row, 0).setEditable(False) |
---|
| 2045 | model.item(last_row, 4).setEditable(False) |
---|
[86f88d1] | 2046 | |
---|
| 2047 | def addScaleToModel(self, model): |
---|
| 2048 | """ |
---|
| 2049 | Adds scale parameter with default values to the model |
---|
| 2050 | """ |
---|
[cbcdd2c] | 2051 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
[86f88d1] | 2052 | checked_list = ['scale', '1.0', '0.0', 'inf', ''] |
---|
[4d457df] | 2053 | FittingUtilities.addCheckedListToModel(model, checked_list) |
---|
[2add354] | 2054 | last_row = model.rowCount()-1 |
---|
| 2055 | model.item(last_row, 0).setEditable(False) |
---|
| 2056 | model.item(last_row, 4).setEditable(False) |
---|
[86f88d1] | 2057 | |
---|
[9d266d2] | 2058 | def addWeightingToData(self, data): |
---|
| 2059 | """ |
---|
| 2060 | Adds weighting contribution to fitting data |
---|
[1bc27f1] | 2061 | """ |
---|
[b764ae5] | 2062 | new_data = copy.deepcopy(data) |
---|
[e1e3e09] | 2063 | # Send original data for weighting |
---|
[dc5ef15] | 2064 | weight = FittingUtilities.getWeight(data=data, is2d=self.is2D, flag=self.weighting) |
---|
[9a7c81c] | 2065 | if self.is2D: |
---|
[b764ae5] | 2066 | new_data.err_data = weight |
---|
[9a7c81c] | 2067 | else: |
---|
[b764ae5] | 2068 | new_data.dy = weight |
---|
| 2069 | |
---|
| 2070 | return new_data |
---|
[9d266d2] | 2071 | |
---|
[0268aed] | 2072 | def updateQRange(self): |
---|
| 2073 | """ |
---|
| 2074 | Updates Q Range display |
---|
| 2075 | """ |
---|
| 2076 | if self.data_is_loaded: |
---|
| 2077 | self.q_range_min, self.q_range_max, self.npts = self.logic.computeDataRange() |
---|
| 2078 | # set Q range labels on the main tab |
---|
[a486918] | 2079 | self.lblMinRangeDef.setText(GuiUtils.formatNumber(self.q_range_min, high=True)) |
---|
| 2080 | self.lblMaxRangeDef.setText(GuiUtils.formatNumber(self.q_range_max, high=True)) |
---|
[0268aed] | 2081 | # set Q range labels on the options tab |
---|
[180bd54] | 2082 | self.options_widget.updateQRange(self.q_range_min, self.q_range_max, self.npts) |
---|
[0268aed] | 2083 | |
---|
[4d457df] | 2084 | def SASModelToQModel(self, model_name, structure_factor=None): |
---|
[60af928] | 2085 | """ |
---|
[cbcdd2c] | 2086 | Setting model parameters into table based on selected category |
---|
[60af928] | 2087 | """ |
---|
| 2088 | # Crete/overwrite model items |
---|
| 2089 | self._model_model.clear() |
---|
[f3a19ad] | 2090 | self._poly_model.clear() |
---|
| 2091 | self._magnet_model.clear() |
---|
[5236449] | 2092 | |
---|
[f3a19ad] | 2093 | if model_name is None: |
---|
| 2094 | if structure_factor not in (None, "None"): |
---|
| 2095 | # S(Q) on its own, treat the same as a form factor |
---|
| 2096 | self.kernel_module = None |
---|
| 2097 | self.fromStructureFactorToQModel(structure_factor) |
---|
| 2098 | else: |
---|
| 2099 | # No models selected |
---|
| 2100 | return |
---|
[cd31251] | 2101 | else: |
---|
[fd1ae6d1] | 2102 | self.fromModelToQModel(model_name) |
---|
[70f4458] | 2103 | self.addExtraShells() |
---|
[5236449] | 2104 | |
---|
[fd1ae6d1] | 2105 | # Allow the SF combobox visibility for the given sasmodel |
---|
| 2106 | self.enableStructureFactorControl(structure_factor) |
---|
[4ea8020] | 2107 | |
---|
| 2108 | # Add S(Q) |
---|
[605d944] | 2109 | if self.cbStructureFactor.isEnabled(): |
---|
| 2110 | structure_factor = self.cbStructureFactor.currentText() |
---|
| 2111 | self.fromStructureFactorToQModel(structure_factor) |
---|
[cd31251] | 2112 | |
---|
[f3a19ad] | 2113 | # Add polydispersity to the model |
---|
[4ea8020] | 2114 | self.poly_params = {} |
---|
[f3a19ad] | 2115 | self.setPolyModel() |
---|
| 2116 | # Add magnetic parameters to the model |
---|
[4ea8020] | 2117 | self.magnet_params = {} |
---|
[f3a19ad] | 2118 | self.setMagneticModel() |
---|
[5236449] | 2119 | |
---|
| 2120 | # Now we claim the model has been loaded |
---|
[86f88d1] | 2121 | self.model_is_loaded = True |
---|
[be8f4b0] | 2122 | # Change the model name to a monicker |
---|
| 2123 | self.kernel_module.name = self.modelName() |
---|
[9a7c81c] | 2124 | # Update the smearing tab |
---|
| 2125 | self.smearing_widget.updateKernelModel(kernel_model=self.kernel_module) |
---|
[86f88d1] | 2126 | |
---|
[fd1ae6d1] | 2127 | # (Re)-create headers |
---|
| 2128 | FittingUtilities.addHeadersToModel(self._model_model) |
---|
[6964d44] | 2129 | self.lstParams.header().setFont(self.boldFont) |
---|
[fd1ae6d1] | 2130 | |
---|
[5236449] | 2131 | # Update Q Ranges |
---|
| 2132 | self.updateQRange() |
---|
| 2133 | |
---|
[fd1ae6d1] | 2134 | def fromModelToQModel(self, model_name): |
---|
| 2135 | """ |
---|
| 2136 | Setting model parameters into QStandardItemModel based on selected _model_ |
---|
| 2137 | """ |
---|
[3b3b40b] | 2138 | name = model_name |
---|
[e3df84e] | 2139 | kernel_module = None |
---|
[3b3b40b] | 2140 | if self.cbCategory.currentText() == CATEGORY_CUSTOM: |
---|
| 2141 | # custom kernel load requires full path |
---|
| 2142 | name = os.path.join(ModelUtilities.find_plugins_dir(), model_name+".py") |
---|
[060413c] | 2143 | try: |
---|
| 2144 | kernel_module = generate.load_kernel_module(name) |
---|
[e3df84e] | 2145 | except ModuleNotFoundError as ex: |
---|
| 2146 | pass |
---|
[4f8c17f] | 2147 | except FileNotFoundError as ex: |
---|
| 2148 | # can happen when name attribute not the same as actual filename |
---|
| 2149 | pass |
---|
[e3df84e] | 2150 | |
---|
| 2151 | if kernel_module is None: |
---|
| 2152 | # mismatch between "name" attribute and actual filename. |
---|
| 2153 | curr_model = self.models[model_name] |
---|
| 2154 | name, _ = os.path.splitext(os.path.basename(curr_model.filename)) |
---|
| 2155 | try: |
---|
| 2156 | kernel_module = generate.load_kernel_module(name) |
---|
| 2157 | except ModuleNotFoundError as ex: |
---|
[bb477f5] | 2158 | logger.error("Can't find the model "+ str(ex)) |
---|
[e3df84e] | 2159 | return |
---|
[dc71408] | 2160 | |
---|
| 2161 | if hasattr(kernel_module, 'parameters'): |
---|
| 2162 | # built-in and custom models |
---|
| 2163 | self.model_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
| 2164 | |
---|
| 2165 | elif hasattr(kernel_module, 'model_info'): |
---|
| 2166 | # for sum/multiply models |
---|
| 2167 | self.model_parameters = kernel_module.model_info.parameters |
---|
| 2168 | |
---|
| 2169 | elif hasattr(kernel_module, 'Model') and hasattr(kernel_module.Model, "_model_info"): |
---|
| 2170 | # this probably won't work if there's no model_info, but just in case |
---|
| 2171 | self.model_parameters = kernel_module.Model._model_info.parameters |
---|
| 2172 | else: |
---|
| 2173 | # no parameters - default to blank table |
---|
| 2174 | msg = "No parameters found in model '{}'.".format(model_name) |
---|
| 2175 | logger.warning(msg) |
---|
| 2176 | self.model_parameters = modelinfo.ParameterTable([]) |
---|
[fd1ae6d1] | 2177 | |
---|
| 2178 | # Instantiate the current sasmodel |
---|
| 2179 | self.kernel_module = self.models[model_name]() |
---|
| 2180 | |
---|
[9ce69ec] | 2181 | # Change the model name to a monicker |
---|
| 2182 | self.kernel_module.name = self.modelName() |
---|
| 2183 | |
---|
[fd1ae6d1] | 2184 | # Explicitly add scale and background with default values |
---|
[6964d44] | 2185 | temp_undo_state = self.undo_supported |
---|
| 2186 | self.undo_supported = False |
---|
[fd1ae6d1] | 2187 | self.addScaleToModel(self._model_model) |
---|
| 2188 | self.addBackgroundToModel(self._model_model) |
---|
[6964d44] | 2189 | self.undo_supported = temp_undo_state |
---|
[fd1ae6d1] | 2190 | |
---|
[0d13814] | 2191 | self.shell_names = self.shellNamesList() |
---|
| 2192 | |
---|
[00b7ddf0] | 2193 | # Add heading row |
---|
| 2194 | FittingUtilities.addHeadingRowToModel(self._model_model, model_name) |
---|
[aca8418] | 2195 | |
---|
[fd1ae6d1] | 2196 | # Update the QModel |
---|
[04f775d] | 2197 | FittingUtilities.addParametersToModel( |
---|
| 2198 | self.model_parameters, |
---|
| 2199 | self.kernel_module, |
---|
[a758043] | 2200 | self.is2D, |
---|
| 2201 | self._model_model, |
---|
| 2202 | self.lstParams) |
---|
[fd1ae6d1] | 2203 | |
---|
| 2204 | def fromStructureFactorToQModel(self, structure_factor): |
---|
| 2205 | """ |
---|
| 2206 | Setting model parameters into QStandardItemModel based on selected _structure factor_ |
---|
| 2207 | """ |
---|
[605d944] | 2208 | if structure_factor is None or structure_factor=="None": |
---|
| 2209 | return |
---|
[18d5c94a] | 2210 | |
---|
[01b4877] | 2211 | product_params = None |
---|
| 2212 | |
---|
[5fb714b] | 2213 | if self.kernel_module is None: |
---|
| 2214 | # Structure factor is the only selected model; build it and show all its params |
---|
| 2215 | self.kernel_module = self.models[structure_factor]() |
---|
[9ce69ec] | 2216 | self.kernel_module.name = self.modelName() |
---|
[5fb714b] | 2217 | s_params = self.kernel_module._model_info.parameters |
---|
| 2218 | s_params_orig = s_params |
---|
[f3a19ad] | 2219 | else: |
---|
[5fb714b] | 2220 | s_kernel = self.models[structure_factor]() |
---|
| 2221 | p_kernel = self.kernel_module |
---|
[9562a6b] | 2222 | # need to reset multiplicity to get the right product |
---|
[672c0e97] | 2223 | if p_kernel.is_multiplicity_model: |
---|
| 2224 | p_kernel.multiplicity = p_kernel.multiplicity_info.number |
---|
[5d1440e1] | 2225 | |
---|
[f3a19ad] | 2226 | p_pars_len = len(p_kernel._model_info.parameters.kernel_parameters) |
---|
| 2227 | s_pars_len = len(s_kernel._model_info.parameters.kernel_parameters) |
---|
[fd1ae6d1] | 2228 | |
---|
[f3a19ad] | 2229 | self.kernel_module = MultiplicationModel(p_kernel, s_kernel) |
---|
[9ce69ec] | 2230 | # Modify the name to correspond to shown items |
---|
| 2231 | self.kernel_module.name = self.modelName() |
---|
[f3a19ad] | 2232 | all_params = self.kernel_module._model_info.parameters.kernel_parameters |
---|
| 2233 | all_param_names = [param.name for param in all_params] |
---|
[18d5c94a] | 2234 | |
---|
[f3a19ad] | 2235 | # S(Q) params from the product model are not necessarily the same as those from the S(Q) model; any |
---|
[5fb714b] | 2236 | # conflicting names with P(Q) params will cause a rename |
---|
[fd1ae6d1] | 2237 | |
---|
[f3a19ad] | 2238 | if "radius_effective_mode" in all_param_names: |
---|
| 2239 | # Show all parameters |
---|
[01b4877] | 2240 | # In this case, radius_effective is NOT pruned by sasmodels.product |
---|
[f3a19ad] | 2241 | s_params = modelinfo.ParameterTable(all_params[p_pars_len:p_pars_len+s_pars_len]) |
---|
| 2242 | s_params_orig = modelinfo.ParameterTable(s_kernel._model_info.parameters.kernel_parameters) |
---|
[01b4877] | 2243 | product_params = modelinfo.ParameterTable( |
---|
| 2244 | self.kernel_module._model_info.parameters.kernel_parameters[p_pars_len+s_pars_len:]) |
---|
[f3a19ad] | 2245 | else: |
---|
| 2246 | # Ensure radius_effective is not displayed |
---|
| 2247 | s_params_orig = modelinfo.ParameterTable(s_kernel._model_info.parameters.kernel_parameters[1:]) |
---|
| 2248 | if "radius_effective" in all_param_names: |
---|
[01b4877] | 2249 | # In this case, radius_effective is NOT pruned by sasmodels.product |
---|
[f3a19ad] | 2250 | s_params = modelinfo.ParameterTable(all_params[p_pars_len+1:p_pars_len+s_pars_len]) |
---|
[01b4877] | 2251 | product_params = modelinfo.ParameterTable( |
---|
| 2252 | self.kernel_module._model_info.parameters.kernel_parameters[p_pars_len+s_pars_len:]) |
---|
[f3a19ad] | 2253 | else: |
---|
[01b4877] | 2254 | # In this case, radius_effective is pruned by sasmodels.product |
---|
[f3a19ad] | 2255 | s_params = modelinfo.ParameterTable(all_params[p_pars_len:p_pars_len+s_pars_len-1]) |
---|
[01b4877] | 2256 | product_params = modelinfo.ParameterTable( |
---|
| 2257 | self.kernel_module._model_info.parameters.kernel_parameters[p_pars_len+s_pars_len-1:]) |
---|
[f3a19ad] | 2258 | |
---|
[00b7ddf0] | 2259 | # Add heading row |
---|
| 2260 | FittingUtilities.addHeadingRowToModel(self._model_model, structure_factor) |
---|
[0d72cac] | 2261 | |
---|
[b69b549] | 2262 | # Get new rows for QModel |
---|
| 2263 | # Any renamed parameters are stored as data in the relevant item, for later handling |
---|
[04f775d] | 2264 | FittingUtilities.addSimpleParametersToModel( |
---|
[01b4877] | 2265 | parameters=s_params, |
---|
| 2266 | is2D=self.is2D, |
---|
| 2267 | parameters_original=s_params_orig, |
---|
| 2268 | model=self._model_model, |
---|
| 2269 | view=self.lstParams) |
---|
| 2270 | |
---|
| 2271 | # Insert product-only params into QModel |
---|
| 2272 | if product_params: |
---|
| 2273 | prod_rows = FittingUtilities.addSimpleParametersToModel( |
---|
| 2274 | parameters=product_params, |
---|
| 2275 | is2D=self.is2D, |
---|
| 2276 | parameters_original=None, |
---|
| 2277 | model=self._model_model, |
---|
| 2278 | view=self.lstParams, |
---|
| 2279 | row_num=2) |
---|
| 2280 | |
---|
| 2281 | # Since this all happens after shells are dealt with and we've inserted rows, fix this counter |
---|
| 2282 | self._n_shells_row += len(prod_rows) |
---|
[fd1ae6d1] | 2283 | |
---|
[6dbff18] | 2284 | def haveParamsToFit(self): |
---|
| 2285 | """ |
---|
| 2286 | Finds out if there are any parameters ready to be fitted |
---|
| 2287 | """ |
---|
[d4216ac] | 2288 | if not self.logic.data_is_loaded: |
---|
| 2289 | return False |
---|
| 2290 | if self.main_params_to_fit: |
---|
| 2291 | return True |
---|
| 2292 | if self.chkPolydispersity.isChecked() and self.poly_params_to_fit: |
---|
| 2293 | return True |
---|
[a98014ab] | 2294 | if self.chkMagnetism.isChecked() and self.canHaveMagnetism() and self.magnet_params_to_fit: |
---|
[d4216ac] | 2295 | return True |
---|
| 2296 | return False |
---|
[6dbff18] | 2297 | |
---|
[d2007a8] | 2298 | def onMainParamsChange(self, top, bottom): |
---|
[cd31251] | 2299 | """ |
---|
| 2300 | Callback method for updating the sasmodel parameters with the GUI values |
---|
| 2301 | """ |
---|
[d2007a8] | 2302 | item = self._model_model.itemFromIndex(top) |
---|
| 2303 | |
---|
[cbcdd2c] | 2304 | model_column = item.column() |
---|
[cd31251] | 2305 | |
---|
| 2306 | if model_column == 0: |
---|
[f182f93] | 2307 | self.checkboxSelected(item) |
---|
[6dbff18] | 2308 | self.cmdFit.setEnabled(self.haveParamsToFit()) |
---|
[6964d44] | 2309 | # Update state stack |
---|
| 2310 | self.updateUndo() |
---|
[cd31251] | 2311 | return |
---|
| 2312 | |
---|
[f182f93] | 2313 | model_row = item.row() |
---|
| 2314 | name_index = self._model_model.index(model_row, 0) |
---|
[700b03b] | 2315 | name_item = self._model_model.itemFromIndex(name_index) |
---|
[f182f93] | 2316 | |
---|
[b00414d] | 2317 | # Extract changed value. |
---|
[2add354] | 2318 | try: |
---|
[fbfc488] | 2319 | value = GuiUtils.toDouble(item.text()) |
---|
[0261bc1] | 2320 | except TypeError: |
---|
[2add354] | 2321 | # Unparsable field |
---|
| 2322 | return |
---|
[fbfc488] | 2323 | |
---|
[700b03b] | 2324 | # if the item has user data, this is the actual parameter name (e.g. to handle duplicate names) |
---|
| 2325 | if name_item.data(QtCore.Qt.UserRole): |
---|
| 2326 | parameter_name = str(name_item.data(QtCore.Qt.UserRole)) |
---|
| 2327 | else: |
---|
| 2328 | parameter_name = str(self._model_model.data(name_index)) |
---|
[cbcdd2c] | 2329 | |
---|
[00b3b40] | 2330 | # Update the parameter value - note: this supports +/-inf as well |
---|
[8f2548c] | 2331 | param_column = self.lstParams.itemDelegate().param_value |
---|
| 2332 | min_column = self.lstParams.itemDelegate().param_min |
---|
| 2333 | max_column = self.lstParams.itemDelegate().param_max |
---|
| 2334 | if model_column == param_column: |
---|
[8a32a6ff] | 2335 | self.kernel_module.setParam(parameter_name, value) |
---|
[8f2548c] | 2336 | elif model_column == min_column: |
---|
[8a32a6ff] | 2337 | # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf] |
---|
[8f2548c] | 2338 | self.kernel_module.details[parameter_name][1] = value |
---|
| 2339 | elif model_column == max_column: |
---|
| 2340 | self.kernel_module.details[parameter_name][2] = value |
---|
| 2341 | else: |
---|
| 2342 | # don't update the chart |
---|
| 2343 | return |
---|
[00b3b40] | 2344 | |
---|
| 2345 | # TODO: magnetic params in self.kernel_module.details['M0:parameter_name'] = value |
---|
| 2346 | # TODO: multishell params in self.kernel_module.details[??] = value |
---|
[cbcdd2c] | 2347 | |
---|
[5a96a72] | 2348 | # handle display of effective radius parameter according to radius_effective_mode; pass ER into model if |
---|
| 2349 | # necessary |
---|
| 2350 | self.processEffectiveRadius() |
---|
| 2351 | |
---|
[d7ff531] | 2352 | # Force the chart update when actual parameters changed |
---|
| 2353 | if model_column == 1: |
---|
[d48cc19] | 2354 | self.recalculatePlotData() |
---|
[7d077d1] | 2355 | |
---|
[2241130] | 2356 | # Update state stack |
---|
[00b3b40] | 2357 | self.updateUndo() |
---|
[2241130] | 2358 | |
---|
[5a96a72] | 2359 | def processEffectiveRadius(self): |
---|
| 2360 | """ |
---|
| 2361 | Checks the value of radius_effective_mode, if existent, and processes radius_effective as necessary. |
---|
| 2362 | * mode == 0: This means 'unconstrained'; ensure use can specify ER. |
---|
| 2363 | * mode > 0: This means it is constrained to a P(Q)-computed value in sasmodels; prevent user from editing ER. |
---|
| 2364 | |
---|
| 2365 | Note: If ER has been computed, it is passed back to SasView as an intermediate result. That value must be |
---|
| 2366 | displayed for the user; that is not dealt with here, but in complete1D. |
---|
| 2367 | """ |
---|
| 2368 | ER_row = self.getRowFromName("radius_effective") |
---|
| 2369 | if ER_row is None: |
---|
| 2370 | return |
---|
| 2371 | |
---|
| 2372 | ER_mode_row = self.getRowFromName("radius_effective_mode") |
---|
| 2373 | if ER_mode_row is None: |
---|
| 2374 | return |
---|
| 2375 | |
---|
| 2376 | try: |
---|
| 2377 | ER_mode = int(self._model_model.item(ER_mode_row, 1).text()) |
---|
| 2378 | except ValueError: |
---|
| 2379 | logging.error("radius_effective_mode was set to an invalid value.") |
---|
| 2380 | return |
---|
| 2381 | |
---|
| 2382 | if ER_mode == 0: |
---|
| 2383 | # ensure the ER value can be modified by user |
---|
| 2384 | self.setParamEditableByRow(ER_row, True) |
---|
| 2385 | elif ER_mode > 0: |
---|
| 2386 | # ensure the ER value cannot be modified by user |
---|
| 2387 | self.setParamEditableByRow(ER_row, False) |
---|
| 2388 | else: |
---|
| 2389 | logging.error("radius_effective_mode was set to an invalid value.") |
---|
| 2390 | |
---|
| 2391 | def setParamEditableByRow(self, row, editable=True): |
---|
| 2392 | """ |
---|
| 2393 | Sets whether the user can edit a parameter in the table. If they cannot, the parameter name's font is changed, |
---|
| 2394 | the value itself cannot be edited if clicked on, and the parameter may not be fitted. |
---|
| 2395 | """ |
---|
| 2396 | item_name = self._model_model.item(row, 0) |
---|
| 2397 | item_value = self._model_model.item(row, 1) |
---|
| 2398 | |
---|
| 2399 | item_value.setEditable(editable) |
---|
| 2400 | |
---|
| 2401 | if editable: |
---|
| 2402 | # reset font |
---|
| 2403 | item_name.setFont(QtGui.QFont()) |
---|
| 2404 | # reset colour |
---|
| 2405 | item_name.setForeground(QtGui.QBrush()) |
---|
| 2406 | # make checkable |
---|
| 2407 | item_name.setCheckable(True) |
---|
| 2408 | else: |
---|
| 2409 | # change font |
---|
| 2410 | font = QtGui.QFont() |
---|
| 2411 | font.setItalic(True) |
---|
| 2412 | item_name.setFont(font) |
---|
| 2413 | # change colour |
---|
| 2414 | item_name.setForeground(QtGui.QBrush(QtGui.QColor(50, 50, 50))) |
---|
| 2415 | # make not checkable (and uncheck) |
---|
| 2416 | item_name.setCheckState(QtCore.Qt.Unchecked) |
---|
| 2417 | item_name.setCheckable(False) |
---|
| 2418 | |
---|
[7fd20fc] | 2419 | def isCheckable(self, row): |
---|
| 2420 | return self._model_model.item(row, 0).isCheckable() |
---|
| 2421 | |
---|
[f182f93] | 2422 | def checkboxSelected(self, item): |
---|
| 2423 | # Assure we're dealing with checkboxes |
---|
| 2424 | if not item.isCheckable(): |
---|
| 2425 | return |
---|
| 2426 | status = item.checkState() |
---|
| 2427 | |
---|
| 2428 | # If multiple rows selected - toggle all of them, filtering uncheckable |
---|
| 2429 | # Switch off signaling from the model to avoid recursion |
---|
| 2430 | self._model_model.blockSignals(True) |
---|
| 2431 | # Convert to proper indices and set requested enablement |
---|
[7fd20fc] | 2432 | self.setParameterSelection(status) |
---|
[f182f93] | 2433 | self._model_model.blockSignals(False) |
---|
| 2434 | |
---|
| 2435 | # update the list of parameters to fit |
---|
[6dbff18] | 2436 | self.main_params_to_fit = self.checkedListFromModel(self._model_model) |
---|
[c1e380e] | 2437 | |
---|
| 2438 | def checkedListFromModel(self, model): |
---|
| 2439 | """ |
---|
| 2440 | Returns list of checked parameters for given model |
---|
| 2441 | """ |
---|
| 2442 | def isChecked(row): |
---|
| 2443 | return model.item(row, 0).checkState() == QtCore.Qt.Checked |
---|
| 2444 | |
---|
| 2445 | return [str(model.item(row_index, 0).text()) |
---|
[b3e8629] | 2446 | for row_index in range(model.rowCount()) |
---|
[c1e380e] | 2447 | if isChecked(row_index)] |
---|
[f182f93] | 2448 | |
---|
[6fd4e36] | 2449 | def createNewIndex(self, fitted_data): |
---|
| 2450 | """ |
---|
| 2451 | Create a model or theory index with passed Data1D/Data2D |
---|
| 2452 | """ |
---|
| 2453 | if self.data_is_loaded: |
---|
[0268aed] | 2454 | if not fitted_data.name: |
---|
| 2455 | name = self.nameForFittedData(self.data.filename) |
---|
| 2456 | fitted_data.title = name |
---|
| 2457 | fitted_data.name = name |
---|
| 2458 | fitted_data.filename = name |
---|
[7d077d1] | 2459 | fitted_data.symbol = "Line" |
---|
[6fd4e36] | 2460 | self.updateModelIndex(fitted_data) |
---|
| 2461 | else: |
---|
[3ae9179] | 2462 | if not fitted_data.name: |
---|
| 2463 | name = self.nameForFittedData(self.kernel_module.id) |
---|
| 2464 | else: |
---|
| 2465 | name = fitted_data.name |
---|
[0268aed] | 2466 | fitted_data.title = name |
---|
| 2467 | fitted_data.filename = name |
---|
| 2468 | fitted_data.symbol = "Line" |
---|
[6fd4e36] | 2469 | self.createTheoryIndex(fitted_data) |
---|
[5d28d6b] | 2470 | # Switch to the theory tab for user's glee |
---|
| 2471 | self.communicate.changeDataExplorerTabSignal.emit(1) |
---|
[6fd4e36] | 2472 | |
---|
| 2473 | def updateModelIndex(self, fitted_data): |
---|
| 2474 | """ |
---|
| 2475 | Update a QStandardModelIndex containing model data |
---|
| 2476 | """ |
---|
[00b3b40] | 2477 | name = self.nameFromData(fitted_data) |
---|
[0268aed] | 2478 | # Make this a line if no other defined |
---|
[7d077d1] | 2479 | if hasattr(fitted_data, 'symbol') and fitted_data.symbol is None: |
---|
[0268aed] | 2480 | fitted_data.symbol = 'Line' |
---|
[6fd4e36] | 2481 | # Notify the GUI manager so it can update the main model in DataExplorer |
---|
[d4dac80] | 2482 | GuiUtils.updateModelItemWithPlot(self.all_data[self.data_index], fitted_data, name) |
---|
[6fd4e36] | 2483 | |
---|
| 2484 | def createTheoryIndex(self, fitted_data): |
---|
| 2485 | """ |
---|
| 2486 | Create a QStandardModelIndex containing model data |
---|
| 2487 | """ |
---|
[00b3b40] | 2488 | name = self.nameFromData(fitted_data) |
---|
| 2489 | # Notify the GUI manager so it can create the theory model in DataExplorer |
---|
[cb90b65] | 2490 | self.theory_item = GuiUtils.createModelItemWithPlot(fitted_data, name=name) |
---|
| 2491 | self.communicate.updateTheoryFromPerspectiveSignal.emit(self.theory_item) |
---|
[00b3b40] | 2492 | |
---|
| 2493 | def nameFromData(self, fitted_data): |
---|
| 2494 | """ |
---|
| 2495 | Return name for the dataset. Terribly impure function. |
---|
| 2496 | """ |
---|
[0268aed] | 2497 | if fitted_data.name is None: |
---|
[00b3b40] | 2498 | name = self.nameForFittedData(self.logic.data.filename) |
---|
[0268aed] | 2499 | fitted_data.title = name |
---|
| 2500 | fitted_data.name = name |
---|
| 2501 | fitted_data.filename = name |
---|
| 2502 | else: |
---|
| 2503 | name = fitted_data.name |
---|
[00b3b40] | 2504 | return name |
---|
[5236449] | 2505 | |
---|
[4d457df] | 2506 | def methodCalculateForData(self): |
---|
| 2507 | '''return the method for data calculation''' |
---|
| 2508 | return Calc1D if isinstance(self.data, Data1D) else Calc2D |
---|
| 2509 | |
---|
| 2510 | def methodCompleteForData(self): |
---|
| 2511 | '''return the method for result parsin on calc complete ''' |
---|
[d4dac80] | 2512 | return self.completed1D if isinstance(self.data, Data1D) else self.completed2D |
---|
[4d457df] | 2513 | |
---|
[66d4370] | 2514 | def updateKernelModelWithExtraParams(self, model=None): |
---|
| 2515 | """ |
---|
| 2516 | Updates kernel model 'model' with extra parameters from |
---|
| 2517 | the polydisp and magnetism tab, if the tabs are enabled |
---|
| 2518 | """ |
---|
| 2519 | if model is None: return |
---|
| 2520 | if not hasattr(model, 'setParam'): return |
---|
| 2521 | |
---|
| 2522 | # add polydisperse parameters if asked |
---|
[2d47985] | 2523 | if self.chkPolydispersity.isChecked() and self._poly_model.rowCount() > 0: |
---|
[66d4370] | 2524 | for key, value in self.poly_params.items(): |
---|
| 2525 | model.setParam(key, value) |
---|
| 2526 | # add magnetic params if asked |
---|
[a98014ab] | 2527 | if self.chkMagnetism.isChecked() and self.canHaveMagnetism() and self._magnet_model.rowCount() > 0: |
---|
[91f4884] | 2528 | for key, value in self.magnet_params.items(): |
---|
[66d4370] | 2529 | model.setParam(key, value) |
---|
| 2530 | |
---|
[d4dac80] | 2531 | def calculateQGridForModelExt(self, data=None, model=None, completefn=None, use_threads=True): |
---|
[86f88d1] | 2532 | """ |
---|
[d4dac80] | 2533 | Wrapper for Calc1D/2D calls |
---|
[86f88d1] | 2534 | """ |
---|
[d4dac80] | 2535 | if data is None: |
---|
| 2536 | data = self.data |
---|
| 2537 | if model is None: |
---|
[66d4370] | 2538 | model = copy.deepcopy(self.kernel_module) |
---|
| 2539 | self.updateKernelModelWithExtraParams(model) |
---|
| 2540 | |
---|
[d4dac80] | 2541 | if completefn is None: |
---|
| 2542 | completefn = self.methodCompleteForData() |
---|
[9a7c81c] | 2543 | smearer = self.smearing_widget.smearer() |
---|
[b764ae5] | 2544 | weight = FittingUtilities.getWeight(data=data, is2d=self.is2D, flag=self.weighting) |
---|
| 2545 | |
---|
[557fc498] | 2546 | # Disable buttons/table |
---|
| 2547 | self.disableInteractiveElements() |
---|
[4d457df] | 2548 | # Awful API to a backend method. |
---|
[d4dac80] | 2549 | calc_thread = self.methodCalculateForData()(data=data, |
---|
| 2550 | model=model, |
---|
[1bc27f1] | 2551 | page_id=0, |
---|
| 2552 | qmin=self.q_range_min, |
---|
| 2553 | qmax=self.q_range_max, |
---|
[9a7c81c] | 2554 | smearer=smearer, |
---|
[1bc27f1] | 2555 | state=None, |
---|
[b764ae5] | 2556 | weight=weight, |
---|
[1bc27f1] | 2557 | fid=None, |
---|
| 2558 | toggle_mode_on=False, |
---|
[d4dac80] | 2559 | completefn=completefn, |
---|
[1bc27f1] | 2560 | update_chisqr=True, |
---|
| 2561 | exception_handler=self.calcException, |
---|
| 2562 | source=None) |
---|
[d4dac80] | 2563 | if use_threads: |
---|
| 2564 | if LocalConfig.USING_TWISTED: |
---|
| 2565 | # start the thread with twisted |
---|
| 2566 | thread = threads.deferToThread(calc_thread.compute) |
---|
| 2567 | thread.addCallback(completefn) |
---|
| 2568 | thread.addErrback(self.calculateDataFailed) |
---|
| 2569 | else: |
---|
| 2570 | # Use the old python threads + Queue |
---|
| 2571 | calc_thread.queue() |
---|
| 2572 | calc_thread.ready(2.5) |
---|
| 2573 | else: |
---|
| 2574 | results = calc_thread.compute() |
---|
| 2575 | completefn(results) |
---|
[4d457df] | 2576 | |
---|
[d4dac80] | 2577 | def calculateQGridForModel(self): |
---|
| 2578 | """ |
---|
| 2579 | Prepare the fitting data object, based on current ModelModel |
---|
| 2580 | """ |
---|
| 2581 | if self.kernel_module is None: |
---|
| 2582 | return |
---|
| 2583 | self.calculateQGridForModelExt() |
---|
[6964d44] | 2584 | |
---|
[aca8418] | 2585 | def calculateDataFailed(self, reason): |
---|
[6964d44] | 2586 | """ |
---|
[c1e380e] | 2587 | Thread returned error |
---|
[6964d44] | 2588 | """ |
---|
[557fc498] | 2589 | # Bring the GUI to normal state |
---|
| 2590 | self.enableInteractiveElements() |
---|
[b3e8629] | 2591 | print("Calculate Data failed with ", reason) |
---|
[5236449] | 2592 | |
---|
[d4dac80] | 2593 | def completed1D(self, return_data): |
---|
| 2594 | self.Calc1DFinishedSignal.emit(return_data) |
---|
| 2595 | |
---|
| 2596 | def completed2D(self, return_data): |
---|
| 2597 | self.Calc2DFinishedSignal.emit(return_data) |
---|
| 2598 | |
---|
[cbcdd2c] | 2599 | def complete1D(self, return_data): |
---|
[5236449] | 2600 | """ |
---|
[4d457df] | 2601 | Plot the current 1D data |
---|
| 2602 | """ |
---|
[557fc498] | 2603 | # Bring the GUI to normal state |
---|
| 2604 | self.enableInteractiveElements() |
---|
[9ce69ec] | 2605 | if return_data is None: |
---|
| 2606 | return |
---|
[d48cc19] | 2607 | fitted_data = self.logic.new1DPlot(return_data, self.tab_id) |
---|
[d1e4689] | 2608 | |
---|
[712db9e] | 2609 | # assure the current index is set properly for batch |
---|
| 2610 | if len(self._logic) > 1: |
---|
| 2611 | for i, logic in enumerate(self._logic): |
---|
| 2612 | if logic.data.name in fitted_data.name: |
---|
| 2613 | self.data_index = i |
---|
| 2614 | |
---|
[0fe7e5b] | 2615 | residuals = self.calculateResiduals(fitted_data) |
---|
[d48cc19] | 2616 | self.model_data = fitted_data |
---|
[5aad7a5] | 2617 | new_plots = [fitted_data] |
---|
| 2618 | if residuals is not None: |
---|
| 2619 | new_plots.append(residuals) |
---|
[cbcdd2c] | 2620 | |
---|
[fd7ef36] | 2621 | if self.data_is_loaded: |
---|
[3ae70f9] | 2622 | # delete any plots associated with the data that were not updated |
---|
| 2623 | # (e.g. to remove beta(Q), S_eff(Q)) |
---|
[0dcb71d] | 2624 | GuiUtils.deleteRedundantPlots(self.all_data[self.data_index], new_plots) |
---|
[5b144c6] | 2625 | pass |
---|
[fd7ef36] | 2626 | else: |
---|
[3ae70f9] | 2627 | # delete theory items for the model, in order to get rid of any |
---|
| 2628 | # redundant items, e.g. beta(Q), S_eff(Q) |
---|
[fd7ef36] | 2629 | self.communicate.deleteIntermediateTheoryPlotsSignal.emit(self.kernel_module.id) |
---|
[0fe7e5b] | 2630 | |
---|
[d8d81ea] | 2631 | # Create plots for parameters with enabled polydispersity |
---|
| 2632 | for plot in FittingUtilities.plotPolydispersities(return_data.get('model', None)): |
---|
| 2633 | data_id = fitted_data.id.split() |
---|
| 2634 | plot.id = "{} [{}] {}".format(data_id[0], plot.name, " ".join(data_id[1:])) |
---|
| 2635 | data_name = fitted_data.name.split() |
---|
| 2636 | plot.name = " ".join([data_name[0], plot.name] + data_name[1:]) |
---|
| 2637 | self.createNewIndex(plot) |
---|
| 2638 | new_plots.append(plot) |
---|
| 2639 | |
---|
[3ae9179] | 2640 | # Create plots for intermediate product data |
---|
[40975f8] | 2641 | plots = self.logic.new1DProductPlots(return_data, self.tab_id) |
---|
| 2642 | for plot in plots: |
---|
| 2643 | plot.symbol = "Line" |
---|
| 2644 | self.createNewIndex(plot) |
---|
| 2645 | new_plots.append(plot) |
---|
[0fe7e5b] | 2646 | |
---|
[9463ca2] | 2647 | for plot in new_plots: |
---|
[5aad7a5] | 2648 | self.communicate.plotUpdateSignal.emit([plot]) |
---|
[9463ca2] | 2649 | |
---|
[5a96a72] | 2650 | # Update radius_effective if relevant |
---|
[d99b1a5] | 2651 | self.updateEffectiveRadius(return_data) |
---|
[5a96a72] | 2652 | |
---|
[cbcdd2c] | 2653 | def complete2D(self, return_data): |
---|
| 2654 | """ |
---|
[4d457df] | 2655 | Plot the current 2D data |
---|
| 2656 | """ |
---|
[557fc498] | 2657 | # Bring the GUI to normal state |
---|
| 2658 | self.enableInteractiveElements() |
---|
| 2659 | |
---|
[8a09457] | 2660 | if return_data is None: |
---|
| 2661 | return |
---|
| 2662 | |
---|
[6fd4e36] | 2663 | fitted_data = self.logic.new2DPlot(return_data) |
---|
[8a09457] | 2664 | # assure the current index is set properly for batch |
---|
| 2665 | if len(self._logic) > 1: |
---|
| 2666 | for i, logic in enumerate(self._logic): |
---|
| 2667 | if logic.data.name in fitted_data.name: |
---|
| 2668 | self.data_index = i |
---|
| 2669 | |
---|
[66d4370] | 2670 | residuals = self.calculateResiduals(fitted_data) |
---|
[d48cc19] | 2671 | self.model_data = fitted_data |
---|
[66d4370] | 2672 | new_plots = [fitted_data] |
---|
| 2673 | if residuals is not None: |
---|
| 2674 | new_plots.append(residuals) |
---|
| 2675 | |
---|
| 2676 | # Update/generate plots |
---|
| 2677 | for plot in new_plots: |
---|
| 2678 | self.communicate.plotUpdateSignal.emit([plot]) |
---|
[6fd4e36] | 2679 | |
---|
[d99b1a5] | 2680 | def updateEffectiveRadius(self, return_data): |
---|
[80a327d] | 2681 | """ |
---|
| 2682 | Given return data from sasmodels, update the effective radius parameter in the GUI table with the new |
---|
| 2683 | calculated value as returned by sasmodels (if the value was returned). |
---|
| 2684 | """ |
---|
| 2685 | ER_mode_row = self.getRowFromName("radius_effective_mode") |
---|
| 2686 | if ER_mode_row is None: |
---|
| 2687 | return |
---|
| 2688 | try: |
---|
| 2689 | ER_mode = int(self._model_model.item(ER_mode_row, 1).text()) |
---|
| 2690 | except ValueError: |
---|
| 2691 | logging.error("radius_effective_mode was set to an invalid value.") |
---|
| 2692 | return |
---|
| 2693 | if ER_mode < 1: |
---|
| 2694 | # does not need updating if it is not being computed |
---|
| 2695 | return |
---|
| 2696 | |
---|
| 2697 | ER_row = self.getRowFromName("radius_effective") |
---|
| 2698 | if ER_row is None: |
---|
| 2699 | return |
---|
| 2700 | |
---|
| 2701 | scalar_results = self.logic.getScalarIntermediateResults(return_data) |
---|
| 2702 | ER_value = scalar_results.get("effective_radius") # note name of key |
---|
| 2703 | if ER_value is None: |
---|
| 2704 | return |
---|
| 2705 | # ensure the model does not recompute when updating the value |
---|
| 2706 | self._model_model.blockSignals(True) |
---|
| 2707 | self._model_model.item(ER_row, 1).setText(str(ER_value)) |
---|
| 2708 | self._model_model.blockSignals(False) |
---|
| 2709 | # ensure the view is updated immediately |
---|
| 2710 | self._model_model.layoutChanged.emit() |
---|
| 2711 | |
---|
[6fd4e36] | 2712 | def calculateResiduals(self, fitted_data): |
---|
| 2713 | """ |
---|
[9463ca2] | 2714 | Calculate and print Chi2 and display chart of residuals. Returns residuals plot object. |
---|
[6fd4e36] | 2715 | """ |
---|
| 2716 | # Create a new index for holding data |
---|
[7d077d1] | 2717 | fitted_data.symbol = "Line" |
---|
[6964d44] | 2718 | |
---|
| 2719 | # Modify fitted_data with weighting |
---|
[b764ae5] | 2720 | weighted_data = self.addWeightingToData(fitted_data) |
---|
[6964d44] | 2721 | |
---|
[b764ae5] | 2722 | self.createNewIndex(weighted_data) |
---|
[712db9e] | 2723 | |
---|
[6fd4e36] | 2724 | # Calculate difference between return_data and logic.data |
---|
[712db9e] | 2725 | self.chi2 = FittingUtilities.calculateChi2(weighted_data, self.data) |
---|
[6fd4e36] | 2726 | # Update the control |
---|
[2add354] | 2727 | chi2_repr = "---" if self.chi2 is None else GuiUtils.formatNumber(self.chi2, high=True) |
---|
[f182f93] | 2728 | self.lblChi2Value.setText(chi2_repr) |
---|
[cbcdd2c] | 2729 | |
---|
[0268aed] | 2730 | # Plot residuals if actual data |
---|
[aca8418] | 2731 | if not self.data_is_loaded: |
---|
| 2732 | return |
---|
| 2733 | |
---|
[b764ae5] | 2734 | residuals_plot = FittingUtilities.plotResiduals(self.data, weighted_data) |
---|
[712db9e] | 2735 | if residuals_plot is None: |
---|
| 2736 | return |
---|
[aca8418] | 2737 | residuals_plot.id = "Residual " + residuals_plot.id |
---|
[a54bbf2b] | 2738 | residuals_plot.plot_role = Data1D.ROLE_RESIDUAL |
---|
[aca8418] | 2739 | self.createNewIndex(residuals_plot) |
---|
[0fe7e5b] | 2740 | return residuals_plot |
---|
[5236449] | 2741 | |
---|
[3d18691] | 2742 | def onCategoriesChanged(self): |
---|
| 2743 | """ |
---|
| 2744 | Reload the category/model comboboxes |
---|
| 2745 | """ |
---|
| 2746 | # Store the current combo indices |
---|
| 2747 | current_cat = self.cbCategory.currentText() |
---|
| 2748 | current_model = self.cbModel.currentText() |
---|
| 2749 | |
---|
| 2750 | # reread the category file and repopulate the combo |
---|
| 2751 | self.cbCategory.blockSignals(True) |
---|
| 2752 | self.cbCategory.clear() |
---|
| 2753 | self.readCategoryInfo() |
---|
| 2754 | self.initializeCategoryCombo() |
---|
| 2755 | |
---|
| 2756 | # Scroll back to the original index in Categories |
---|
| 2757 | new_index = self.cbCategory.findText(current_cat) |
---|
| 2758 | if new_index != -1: |
---|
| 2759 | self.cbCategory.setCurrentIndex(new_index) |
---|
| 2760 | self.cbCategory.blockSignals(False) |
---|
| 2761 | # ...and in the Models |
---|
| 2762 | self.cbModel.blockSignals(True) |
---|
| 2763 | new_index = self.cbModel.findText(current_model) |
---|
| 2764 | if new_index != -1: |
---|
| 2765 | self.cbModel.setCurrentIndex(new_index) |
---|
| 2766 | self.cbModel.blockSignals(False) |
---|
| 2767 | |
---|
| 2768 | return |
---|
| 2769 | |
---|
[5236449] | 2770 | def calcException(self, etype, value, tb): |
---|
| 2771 | """ |
---|
[c1e380e] | 2772 | Thread threw an exception. |
---|
[5236449] | 2773 | """ |
---|
[557fc498] | 2774 | # Bring the GUI to normal state |
---|
| 2775 | self.enableInteractiveElements() |
---|
[c1e380e] | 2776 | # TODO: remimplement thread cancellation |
---|
[bb477f5] | 2777 | logger.error("".join(traceback.format_exception(etype, value, tb))) |
---|
[60af928] | 2778 | |
---|
| 2779 | def setTableProperties(self, table): |
---|
| 2780 | """ |
---|
| 2781 | Setting table properties |
---|
| 2782 | """ |
---|
| 2783 | # Table properties |
---|
| 2784 | table.verticalHeader().setVisible(False) |
---|
| 2785 | table.setAlternatingRowColors(True) |
---|
[4992ff2] | 2786 | table.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding) |
---|
| 2787 | table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) |
---|
[f46f6dc] | 2788 | table.resizeColumnsToContents() |
---|
| 2789 | |
---|
[60af928] | 2790 | # Header |
---|
| 2791 | header = table.horizontalHeader() |
---|
[4992ff2] | 2792 | header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents) |
---|
| 2793 | header.ResizeMode(QtWidgets.QHeaderView.Interactive) |
---|
[f46f6dc] | 2794 | |
---|
[4992ff2] | 2795 | # Qt5: the following 2 lines crash - figure out why! |
---|
[e43fc91] | 2796 | # Resize column 0 and 7 to content |
---|
[4992ff2] | 2797 | #header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) |
---|
| 2798 | #header.setSectionResizeMode(7, QtWidgets.QHeaderView.ResizeToContents) |
---|
[60af928] | 2799 | |
---|
| 2800 | def setPolyModel(self): |
---|
| 2801 | """ |
---|
| 2802 | Set polydispersity values |
---|
| 2803 | """ |
---|
[86f88d1] | 2804 | if not self.model_parameters: |
---|
| 2805 | return |
---|
| 2806 | self._poly_model.clear() |
---|
| 2807 | |
---|
[f53bc04] | 2808 | parameters = self.model_parameters.form_volume_parameters |
---|
| 2809 | if self.is2D: |
---|
| 2810 | parameters += self.model_parameters.orientation_parameters |
---|
| 2811 | |
---|
[e43fc91] | 2812 | [self.setPolyModelParameters(i, param) for i, param in \ |
---|
[f53bc04] | 2813 | enumerate(parameters) if param.polydisperse] |
---|
| 2814 | |
---|
[4d457df] | 2815 | FittingUtilities.addPolyHeadersToModel(self._poly_model) |
---|
[60af928] | 2816 | |
---|
[e43fc91] | 2817 | def setPolyModelParameters(self, i, param): |
---|
[aca8418] | 2818 | """ |
---|
[0d13814] | 2819 | Standard of multishell poly parameter driver |
---|
[aca8418] | 2820 | """ |
---|
[0d13814] | 2821 | param_name = param.name |
---|
| 2822 | # see it the parameter is multishell |
---|
[06b0138] | 2823 | if '[' in param.name: |
---|
[0d13814] | 2824 | # Skip empty shells |
---|
| 2825 | if self.current_shell_displayed == 0: |
---|
| 2826 | return |
---|
| 2827 | else: |
---|
| 2828 | # Create as many entries as current shells |
---|
[b3e8629] | 2829 | for ishell in range(1, self.current_shell_displayed+1): |
---|
[0d13814] | 2830 | # Remove [n] and add the shell numeral |
---|
| 2831 | name = param_name[0:param_name.index('[')] + str(ishell) |
---|
[e43fc91] | 2832 | self.addNameToPolyModel(i, name) |
---|
[0d13814] | 2833 | else: |
---|
| 2834 | # Just create a simple param entry |
---|
[e43fc91] | 2835 | self.addNameToPolyModel(i, param_name) |
---|
[0d13814] | 2836 | |
---|
[e43fc91] | 2837 | def addNameToPolyModel(self, i, param_name): |
---|
[0d13814] | 2838 | """ |
---|
| 2839 | Creates a checked row in the poly model with param_name |
---|
| 2840 | """ |
---|
[144ec831] | 2841 | # Polydisp. values from the sasmodel |
---|
[0d13814] | 2842 | width = self.kernel_module.getParam(param_name + '.width') |
---|
| 2843 | npts = self.kernel_module.getParam(param_name + '.npts') |
---|
| 2844 | nsigs = self.kernel_module.getParam(param_name + '.nsigmas') |
---|
| 2845 | _, min, max = self.kernel_module.details[param_name] |
---|
[144ec831] | 2846 | |
---|
[66d4370] | 2847 | # Update local param dict |
---|
| 2848 | self.poly_params[param_name + '.width'] = width |
---|
| 2849 | self.poly_params[param_name + '.npts'] = npts |
---|
| 2850 | self.poly_params[param_name + '.nsigmas'] = nsigs |
---|
| 2851 | |
---|
[144ec831] | 2852 | # Construct a row with polydisp. related variable. |
---|
| 2853 | # This will get added to the polydisp. model |
---|
| 2854 | # Note: last argument needs extra space padding for decent display of the control |
---|
[0d13814] | 2855 | checked_list = ["Distribution of " + param_name, str(width), |
---|
| 2856 | str(min), str(max), |
---|
[e43fc91] | 2857 | str(npts), str(nsigs), "gaussian ",''] |
---|
[aca8418] | 2858 | FittingUtilities.addCheckedListToModel(self._poly_model, checked_list) |
---|
| 2859 | |
---|
| 2860 | # All possible polydisp. functions as strings in combobox |
---|
[4992ff2] | 2861 | func = QtWidgets.QComboBox() |
---|
[b3e8629] | 2862 | func.addItems([str(name_disp) for name_disp in POLYDISPERSITY_MODELS.keys()]) |
---|
[e43fc91] | 2863 | # Set the default index |
---|
[aca8418] | 2864 | func.setCurrentIndex(func.findText(DEFAULT_POLYDISP_FUNCTION)) |
---|
[e43fc91] | 2865 | ind = self._poly_model.index(i,self.lstPoly.itemDelegate().poly_function) |
---|
| 2866 | self.lstPoly.setIndexWidget(ind, func) |
---|
| 2867 | func.currentIndexChanged.connect(lambda: self.onPolyComboIndexChange(str(func.currentText()), i)) |
---|
| 2868 | |
---|
| 2869 | def onPolyFilenameChange(self, row_index): |
---|
| 2870 | """ |
---|
| 2871 | Respond to filename_updated signal from the delegate |
---|
| 2872 | """ |
---|
| 2873 | # For the given row, invoke the "array" combo handler |
---|
| 2874 | array_caption = 'array' |
---|
[8222f171] | 2875 | |
---|
[e43fc91] | 2876 | # Get the combo box reference |
---|
| 2877 | ind = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_function) |
---|
| 2878 | widget = self.lstPoly.indexWidget(ind) |
---|
[8222f171] | 2879 | |
---|
[e43fc91] | 2880 | # Update the combo box so it displays "array" |
---|
| 2881 | widget.blockSignals(True) |
---|
| 2882 | widget.setCurrentIndex(self.lstPoly.itemDelegate().POLYDISPERSE_FUNCTIONS.index(array_caption)) |
---|
| 2883 | widget.blockSignals(False) |
---|
[aca8418] | 2884 | |
---|
[8222f171] | 2885 | # Invoke the file reader |
---|
| 2886 | self.onPolyComboIndexChange(array_caption, row_index) |
---|
| 2887 | |
---|
[aca8418] | 2888 | def onPolyComboIndexChange(self, combo_string, row_index): |
---|
| 2889 | """ |
---|
| 2890 | Modify polydisp. defaults on function choice |
---|
| 2891 | """ |
---|
[144ec831] | 2892 | # Get npts/nsigs for current selection |
---|
[aca8418] | 2893 | param = self.model_parameters.form_volume_parameters[row_index] |
---|
[e43fc91] | 2894 | file_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_function) |
---|
| 2895 | combo_box = self.lstPoly.indexWidget(file_index) |
---|
[aca8418] | 2896 | |
---|
[919d47c] | 2897 | def updateFunctionCaption(row): |
---|
| 2898 | # Utility function for update of polydispersity function name in the main model |
---|
[00b7ddf0] | 2899 | if not self.isCheckable(row): |
---|
| 2900 | return |
---|
[73665a8] | 2901 | self._model_model.blockSignals(True) |
---|
[1643d8ed] | 2902 | param_name = str(self._model_model.item(row, 0).text()) |
---|
[73665a8] | 2903 | self._model_model.blockSignals(False) |
---|
[919d47c] | 2904 | if param_name != param.name: |
---|
| 2905 | return |
---|
[144ec831] | 2906 | # Modify the param value |
---|
[73665a8] | 2907 | self._model_model.blockSignals(True) |
---|
[906e0c7] | 2908 | if self.has_error_column: |
---|
| 2909 | # err column changes the indexing |
---|
| 2910 | self._model_model.item(row, 0).child(0).child(0,5).setText(combo_string) |
---|
| 2911 | else: |
---|
| 2912 | self._model_model.item(row, 0).child(0).child(0,4).setText(combo_string) |
---|
[73665a8] | 2913 | self._model_model.blockSignals(False) |
---|
[919d47c] | 2914 | |
---|
[aca8418] | 2915 | if combo_string == 'array': |
---|
| 2916 | try: |
---|
[e43fc91] | 2917 | self.loadPolydispArray(row_index) |
---|
[919d47c] | 2918 | # Update main model for display |
---|
| 2919 | self.iterateOverModel(updateFunctionCaption) |
---|
[e43fc91] | 2920 | # disable the row |
---|
| 2921 | lo = self.lstPoly.itemDelegate().poly_pd |
---|
| 2922 | hi = self.lstPoly.itemDelegate().poly_function |
---|
[b3e8629] | 2923 | [self._poly_model.item(row_index, i).setEnabled(False) for i in range(lo, hi)] |
---|
[aca8418] | 2924 | return |
---|
[e43fc91] | 2925 | except IOError: |
---|
[8222f171] | 2926 | combo_box.setCurrentIndex(self.orig_poly_index) |
---|
[e43fc91] | 2927 | # Pass for cancel/bad read |
---|
| 2928 | pass |
---|
[aca8418] | 2929 | |
---|
| 2930 | # Enable the row in case it was disabled by Array |
---|
[919d47c] | 2931 | self._poly_model.blockSignals(True) |
---|
[e43fc91] | 2932 | max_range = self.lstPoly.itemDelegate().poly_filename |
---|
[b3e8629] | 2933 | [self._poly_model.item(row_index, i).setEnabled(True) for i in range(7)] |
---|
[e43fc91] | 2934 | file_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_filename) |
---|
[b3e8629] | 2935 | self._poly_model.setData(file_index, "") |
---|
[919d47c] | 2936 | self._poly_model.blockSignals(False) |
---|
[aca8418] | 2937 | |
---|
[8eaa101] | 2938 | npts_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_npts) |
---|
| 2939 | nsigs_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_nsigs) |
---|
[aca8418] | 2940 | |
---|
| 2941 | npts = POLYDISPERSITY_MODELS[str(combo_string)].default['npts'] |
---|
| 2942 | nsigs = POLYDISPERSITY_MODELS[str(combo_string)].default['nsigmas'] |
---|
| 2943 | |
---|
[b3e8629] | 2944 | self._poly_model.setData(npts_index, npts) |
---|
| 2945 | self._poly_model.setData(nsigs_index, nsigs) |
---|
[aca8418] | 2946 | |
---|
[919d47c] | 2947 | self.iterateOverModel(updateFunctionCaption) |
---|
[8222f171] | 2948 | self.orig_poly_index = combo_box.currentIndex() |
---|
[919d47c] | 2949 | |
---|
[e43fc91] | 2950 | def loadPolydispArray(self, row_index): |
---|
[aca8418] | 2951 | """ |
---|
| 2952 | Show the load file dialog and loads requested data into state |
---|
| 2953 | """ |
---|
[4992ff2] | 2954 | datafile = QtWidgets.QFileDialog.getOpenFileName( |
---|
| 2955 | self, "Choose a weight file", "", "All files (*.*)", None, |
---|
[fbfc488] | 2956 | QtWidgets.QFileDialog.DontUseNativeDialog)[0] |
---|
[72f4834] | 2957 | |
---|
[fbfc488] | 2958 | if not datafile: |
---|
[bb477f5] | 2959 | logger.info("No weight data chosen.") |
---|
[1643d8ed] | 2960 | raise IOError |
---|
[72f4834] | 2961 | |
---|
[aca8418] | 2962 | values = [] |
---|
| 2963 | weights = [] |
---|
[919d47c] | 2964 | def appendData(data_tuple): |
---|
| 2965 | """ |
---|
| 2966 | Fish out floats from a tuple of strings |
---|
| 2967 | """ |
---|
| 2968 | try: |
---|
| 2969 | values.append(float(data_tuple[0])) |
---|
| 2970 | weights.append(float(data_tuple[1])) |
---|
| 2971 | except (ValueError, IndexError): |
---|
| 2972 | # just pass through if line with bad data |
---|
| 2973 | return |
---|
| 2974 | |
---|
[aca8418] | 2975 | with open(datafile, 'r') as column_file: |
---|
| 2976 | column_data = [line.rstrip().split() for line in column_file.readlines()] |
---|
[919d47c] | 2977 | [appendData(line) for line in column_data] |
---|
[aca8418] | 2978 | |
---|
[1643d8ed] | 2979 | # If everything went well - update the sasmodel values |
---|
[aca8418] | 2980 | self.disp_model = POLYDISPERSITY_MODELS['array']() |
---|
| 2981 | self.disp_model.set_weights(np.array(values), np.array(weights)) |
---|
[e43fc91] | 2982 | # + update the cell with filename |
---|
| 2983 | fname = os.path.basename(str(datafile)) |
---|
| 2984 | fname_index = self._poly_model.index(row_index, self.lstPoly.itemDelegate().poly_filename) |
---|
[b3e8629] | 2985 | self._poly_model.setData(fname_index, fname) |
---|
[aca8418] | 2986 | |
---|
[f37cab0] | 2987 | def onColumnWidthUpdate(self, index, old_size, new_size): |
---|
| 2988 | """ |
---|
| 2989 | Simple state update of the current column widths in the param list |
---|
| 2990 | """ |
---|
| 2991 | self.lstParamHeaderSizes[index] = new_size |
---|
| 2992 | |
---|
[60af928] | 2993 | def setMagneticModel(self): |
---|
| 2994 | """ |
---|
| 2995 | Set magnetism values on model |
---|
| 2996 | """ |
---|
[86f88d1] | 2997 | if not self.model_parameters: |
---|
| 2998 | return |
---|
| 2999 | self._magnet_model.clear() |
---|
[a98014ab] | 3000 | # default initial value |
---|
[658795d] | 3001 | m0 = 0.5 |
---|
[a98014ab] | 3002 | for param in self.model_parameters.call_parameters: |
---|
| 3003 | if param.type != 'magnetic': continue |
---|
| 3004 | if "M0" in param.name: |
---|
[658795d] | 3005 | m0 += 0.5 |
---|
[a98014ab] | 3006 | value = m0 |
---|
| 3007 | else: |
---|
| 3008 | value = param.default |
---|
| 3009 | self.addCheckedMagneticListToModel(param, value) |
---|
| 3010 | |
---|
[4d457df] | 3011 | FittingUtilities.addHeadersToModel(self._magnet_model) |
---|
[60af928] | 3012 | |
---|
[0d13814] | 3013 | def shellNamesList(self): |
---|
| 3014 | """ |
---|
| 3015 | Returns list of names of all multi-shell parameters |
---|
| 3016 | E.g. for sld[n], radius[n], n=1..3 it will return |
---|
| 3017 | [sld1, sld2, sld3, radius1, radius2, radius3] |
---|
| 3018 | """ |
---|
| 3019 | multi_names = [p.name[:p.name.index('[')] for p in self.model_parameters.iq_parameters if '[' in p.name] |
---|
| 3020 | top_index = self.kernel_module.multiplicity_info.number |
---|
| 3021 | shell_names = [] |
---|
[b3e8629] | 3022 | for i in range(1, top_index+1): |
---|
[0d13814] | 3023 | for name in multi_names: |
---|
| 3024 | shell_names.append(name+str(i)) |
---|
| 3025 | return shell_names |
---|
| 3026 | |
---|
[a98014ab] | 3027 | def addCheckedMagneticListToModel(self, param, value): |
---|
[aca8418] | 3028 | """ |
---|
| 3029 | Wrapper for model update with a subset of magnetic parameters |
---|
| 3030 | """ |
---|
[8ac8717] | 3031 | try: |
---|
| 3032 | basename, _ = param.name.rsplit('_', 1) |
---|
| 3033 | except ValueError: |
---|
| 3034 | basename = param.name |
---|
| 3035 | if basename in self.shell_names: |
---|
[0d13814] | 3036 | try: |
---|
[8ac8717] | 3037 | shell_index = int(basename[-2:]) |
---|
[0d13814] | 3038 | except ValueError: |
---|
[8ac8717] | 3039 | shell_index = int(basename[-1:]) |
---|
[0d13814] | 3040 | |
---|
| 3041 | if shell_index > self.current_shell_displayed: |
---|
| 3042 | return |
---|
| 3043 | |
---|
[aca8418] | 3044 | checked_list = [param.name, |
---|
[a98014ab] | 3045 | str(value), |
---|
[aca8418] | 3046 | str(param.limits[0]), |
---|
| 3047 | str(param.limits[1]), |
---|
| 3048 | param.units] |
---|
| 3049 | |
---|
[a98014ab] | 3050 | self.magnet_params[param.name] = value |
---|
[66d4370] | 3051 | |
---|
[a98014ab] | 3052 | FittingUtilities.addCheckedListToModel(self._magnet_model, checked_list) |
---|
[aca8418] | 3053 | |
---|
[fd1ae6d1] | 3054 | def enableStructureFactorControl(self, structure_factor): |
---|
[cd31251] | 3055 | """ |
---|
| 3056 | Add structure factors to the list of parameters |
---|
| 3057 | """ |
---|
[fd1ae6d1] | 3058 | if self.kernel_module.is_form_factor or structure_factor == 'None': |
---|
[cd31251] | 3059 | self.enableStructureCombo() |
---|
| 3060 | else: |
---|
| 3061 | self.disableStructureCombo() |
---|
| 3062 | |
---|
[60af928] | 3063 | def addExtraShells(self): |
---|
| 3064 | """ |
---|
[f46f6dc] | 3065 | Add a combobox for multiple shell display |
---|
[60af928] | 3066 | """ |
---|
[4d457df] | 3067 | param_name, param_length = FittingUtilities.getMultiplicity(self.model_parameters) |
---|
[f46f6dc] | 3068 | |
---|
| 3069 | if param_length == 0: |
---|
| 3070 | return |
---|
| 3071 | |
---|
[6f7f652] | 3072 | # cell 1: variable name |
---|
[f46f6dc] | 3073 | item1 = QtGui.QStandardItem(param_name) |
---|
| 3074 | |
---|
[4992ff2] | 3075 | func = QtWidgets.QComboBox() |
---|
[60af928] | 3076 | |
---|
[6f7f652] | 3077 | # cell 2: combobox |
---|
[f46f6dc] | 3078 | item2 = QtGui.QStandardItem() |
---|
[13dd7d2] | 3079 | |
---|
| 3080 | # cell 3: min value |
---|
| 3081 | item3 = QtGui.QStandardItem() |
---|
| 3082 | |
---|
| 3083 | # cell 4: max value |
---|
| 3084 | item4 = QtGui.QStandardItem() |
---|
| 3085 | |
---|
[86d3207] | 3086 | # cell 4: SLD button |
---|
| 3087 | item5 = QtGui.QStandardItem() |
---|
| 3088 | button = QtWidgets.QPushButton() |
---|
| 3089 | button.setText("Show SLD Profile") |
---|
| 3090 | |
---|
| 3091 | self._model_model.appendRow([item1, item2, item3, item4, item5]) |
---|
[60af928] | 3092 | |
---|
[6f7f652] | 3093 | # Beautify the row: span columns 2-4 |
---|
[60af928] | 3094 | shell_row = self._model_model.rowCount() |
---|
[f46f6dc] | 3095 | shell_index = self._model_model.index(shell_row-1, 1) |
---|
[86d3207] | 3096 | button_index = self._model_model.index(shell_row-1, 4) |
---|
[86f88d1] | 3097 | |
---|
[4d457df] | 3098 | self.lstParams.setIndexWidget(shell_index, func) |
---|
[86d3207] | 3099 | self.lstParams.setIndexWidget(button_index, button) |
---|
[70f4458] | 3100 | self._n_shells_row = shell_row - 1 |
---|
[86f88d1] | 3101 | |
---|
[f712bf30] | 3102 | # Get the default number of shells for the model |
---|
| 3103 | kernel_pars = self.kernel_module._model_info.parameters.kernel_parameters |
---|
| 3104 | shell_par = None |
---|
| 3105 | for par in kernel_pars: |
---|
| 3106 | if par.name == param_name: |
---|
| 3107 | shell_par = par |
---|
| 3108 | break |
---|
| 3109 | if not shell_par: |
---|
| 3110 | logger.error("Could not find %s in kernel parameters.", param_name) |
---|
| 3111 | default_shell_count = shell_par.default |
---|
[13dd7d2] | 3112 | shell_min = 0 |
---|
| 3113 | shell_max = 0 |
---|
| 3114 | try: |
---|
| 3115 | shell_min = int(shell_par.limits[0]) |
---|
| 3116 | shell_max = int(shell_par.limits[1]) |
---|
| 3117 | except IndexError as ex: |
---|
| 3118 | # no info about limits |
---|
| 3119 | pass |
---|
[62c6dc0] | 3120 | # don't update the kernel here - this data is display only |
---|
| 3121 | self._model_model.blockSignals(True) |
---|
[13dd7d2] | 3122 | item3.setText(str(shell_min)) |
---|
| 3123 | item4.setText(str(shell_max)) |
---|
[62c6dc0] | 3124 | self._model_model.blockSignals(False) |
---|
[13dd7d2] | 3125 | |
---|
| 3126 | # Respond to index change |
---|
| 3127 | func.currentTextChanged.connect(self.modifyShellsInList) |
---|
| 3128 | |
---|
[86d3207] | 3129 | # Respond to button press |
---|
| 3130 | button.clicked.connect(self.onShowSLDProfile) |
---|
| 3131 | |
---|
[13dd7d2] | 3132 | # Available range of shells displayed in the combobox |
---|
| 3133 | func.addItems([str(i) for i in range(shell_min, shell_max+1)]) |
---|
[f712bf30] | 3134 | |
---|
| 3135 | # Add default number of shells to the model |
---|
[13dd7d2] | 3136 | func.setCurrentText(str(default_shell_count)) |
---|
[a9b568c] | 3137 | |
---|
[13dd7d2] | 3138 | def modifyShellsInList(self, text): |
---|
[86f88d1] | 3139 | """ |
---|
| 3140 | Add/remove additional multishell parameters |
---|
| 3141 | """ |
---|
| 3142 | # Find row location of the combobox |
---|
[70f4458] | 3143 | first_row = self._n_shells_row + 1 |
---|
| 3144 | remove_rows = self._num_shell_params |
---|
[13dd7d2] | 3145 | try: |
---|
| 3146 | index = int(text) |
---|
| 3147 | except ValueError: |
---|
| 3148 | # bad text on the control! |
---|
| 3149 | index = 0 |
---|
| 3150 | logger.error("Multiplicity incorrect! Setting to 0") |
---|
[86d3207] | 3151 | self.kernel_module.multiplicity = index |
---|
[86f88d1] | 3152 | if remove_rows > 1: |
---|
[70f4458] | 3153 | self._model_model.removeRows(first_row, remove_rows) |
---|
| 3154 | |
---|
[b69b549] | 3155 | new_rows = FittingUtilities.addShellsToModel( |
---|
[88ada06] | 3156 | self.model_parameters, |
---|
| 3157 | self._model_model, |
---|
[a758043] | 3158 | index, |
---|
[b69b549] | 3159 | first_row, |
---|
[a758043] | 3160 | self.lstParams) |
---|
[86f88d1] | 3161 | |
---|
[b69b549] | 3162 | self._num_shell_params = len(new_rows) |
---|
[a9b568c] | 3163 | self.current_shell_displayed = index |
---|
[60af928] | 3164 | |
---|
[daf7c9c] | 3165 | # Param values for existing shells were reset to default; force all changes into kernel module |
---|
| 3166 | for row in new_rows: |
---|
| 3167 | par = row[0].text() |
---|
| 3168 | val = GuiUtils.toDouble(row[1].text()) |
---|
| 3169 | self.kernel_module.setParam(par, val) |
---|
| 3170 | |
---|
| 3171 | # Change 'n' in the parameter model; also causes recalculation |
---|
[3090270] | 3172 | self._model_model.item(self._n_shells_row, 1).setText(str(index)) |
---|
| 3173 | |
---|
[0d13814] | 3174 | # Update relevant models |
---|
| 3175 | self.setPolyModel() |
---|
[d2007a8] | 3176 | if self.canHaveMagnetism(): |
---|
| 3177 | self.setMagneticModel() |
---|
[0d13814] | 3178 | |
---|
[86d3207] | 3179 | def onShowSLDProfile(self): |
---|
| 3180 | """ |
---|
| 3181 | Show a quick plot of SLD profile |
---|
| 3182 | """ |
---|
| 3183 | # get profile data |
---|
| 3184 | x, y = self.kernel_module.getProfile() |
---|
| 3185 | y *= 1.0e6 |
---|
| 3186 | profile_data = Data1D(x=x, y=y) |
---|
| 3187 | profile_data.name = "SLD" |
---|
| 3188 | profile_data.scale = 'linear' |
---|
| 3189 | profile_data.symbol = 'Line' |
---|
| 3190 | profile_data.hide_error = True |
---|
| 3191 | profile_data._xaxis = "R(\AA)" |
---|
| 3192 | profile_data._yaxis = "SLD(10^{-6}\AA^{-2})" |
---|
| 3193 | |
---|
| 3194 | plotter = PlotterWidget(self, quickplot=True) |
---|
| 3195 | plotter.data = profile_data |
---|
| 3196 | plotter.showLegend = True |
---|
| 3197 | plotter.plot(hide_error=True, marker='-') |
---|
| 3198 | |
---|
| 3199 | self.plot_widget = QtWidgets.QWidget() |
---|
| 3200 | self.plot_widget.setWindowTitle("Scattering Length Density Profile") |
---|
| 3201 | layout = QtWidgets.QVBoxLayout() |
---|
| 3202 | layout.addWidget(plotter) |
---|
| 3203 | self.plot_widget.setLayout(layout) |
---|
| 3204 | self.plot_widget.show() |
---|
| 3205 | |
---|
[557fc498] | 3206 | def setInteractiveElements(self, enabled=True): |
---|
[14ec91c5] | 3207 | """ |
---|
[557fc498] | 3208 | Switch interactive GUI elements on/off |
---|
[14ec91c5] | 3209 | """ |
---|
[557fc498] | 3210 | assert isinstance(enabled, bool) |
---|
| 3211 | |
---|
| 3212 | self.lstParams.setEnabled(enabled) |
---|
| 3213 | self.lstPoly.setEnabled(enabled) |
---|
| 3214 | self.lstMagnetic.setEnabled(enabled) |
---|
| 3215 | |
---|
| 3216 | self.cbCategory.setEnabled(enabled) |
---|
| 3217 | self.cbModel.setEnabled(enabled) |
---|
[3ab2105] | 3218 | self.cmdPlot.setEnabled(enabled) |
---|
[14ec91c5] | 3219 | |
---|
[557fc498] | 3220 | def enableInteractiveElements(self): |
---|
[14ec91c5] | 3221 | """ |
---|
[557fc498] | 3222 | Set buttion caption on fitting/calculate finish |
---|
| 3223 | Enable the param table(s) |
---|
[14ec91c5] | 3224 | """ |
---|
[ded5e77] | 3225 | # Notify the user that fitting is available |
---|
| 3226 | self.cmdFit.setStyleSheet('QPushButton {color: black;}') |
---|
[14ec91c5] | 3227 | self.cmdFit.setText("Fit") |
---|
[ded5e77] | 3228 | self.fit_started = False |
---|
[557fc498] | 3229 | self.setInteractiveElements(True) |
---|
| 3230 | |
---|
| 3231 | def disableInteractiveElements(self): |
---|
| 3232 | """ |
---|
| 3233 | Set buttion caption on fitting/calculate start |
---|
| 3234 | Disable the param table(s) |
---|
| 3235 | """ |
---|
| 3236 | # Notify the user that fitting is being run |
---|
| 3237 | # Allow for stopping the job |
---|
| 3238 | self.cmdFit.setStyleSheet('QPushButton {color: red;}') |
---|
| 3239 | self.cmdFit.setText('Stop fit') |
---|
| 3240 | self.setInteractiveElements(False) |
---|
[14ec91c5] | 3241 | |
---|
[672b8ab] | 3242 | def readFitPage(self, fp): |
---|
| 3243 | """ |
---|
| 3244 | Read in state from a fitpage object and update GUI |
---|
| 3245 | """ |
---|
| 3246 | assert isinstance(fp, FitPage) |
---|
| 3247 | # Main tab info |
---|
| 3248 | self.logic.data.filename = fp.filename |
---|
| 3249 | self.data_is_loaded = fp.data_is_loaded |
---|
| 3250 | self.chkPolydispersity.setCheckState(fp.is_polydisperse) |
---|
| 3251 | self.chkMagnetism.setCheckState(fp.is_magnetic) |
---|
| 3252 | self.chk2DView.setCheckState(fp.is2D) |
---|
| 3253 | |
---|
| 3254 | # Update the comboboxes |
---|
| 3255 | self.cbCategory.setCurrentIndex(self.cbCategory.findText(fp.current_category)) |
---|
| 3256 | self.cbModel.setCurrentIndex(self.cbModel.findText(fp.current_model)) |
---|
| 3257 | if fp.current_factor: |
---|
| 3258 | self.cbStructureFactor.setCurrentIndex(self.cbStructureFactor.findText(fp.current_factor)) |
---|
| 3259 | |
---|
| 3260 | self.chi2 = fp.chi2 |
---|
| 3261 | |
---|
| 3262 | # Options tab |
---|
| 3263 | self.q_range_min = fp.fit_options[fp.MIN_RANGE] |
---|
| 3264 | self.q_range_max = fp.fit_options[fp.MAX_RANGE] |
---|
| 3265 | self.npts = fp.fit_options[fp.NPTS] |
---|
| 3266 | self.log_points = fp.fit_options[fp.LOG_POINTS] |
---|
| 3267 | self.weighting = fp.fit_options[fp.WEIGHTING] |
---|
| 3268 | |
---|
| 3269 | # Models |
---|
[d60da0c] | 3270 | self._model_model = fp.model_model |
---|
| 3271 | self._poly_model = fp.poly_model |
---|
| 3272 | self._magnet_model = fp.magnetism_model |
---|
[672b8ab] | 3273 | |
---|
| 3274 | # Resolution tab |
---|
| 3275 | smearing = fp.smearing_options[fp.SMEARING_OPTION] |
---|
| 3276 | accuracy = fp.smearing_options[fp.SMEARING_ACCURACY] |
---|
| 3277 | smearing_min = fp.smearing_options[fp.SMEARING_MIN] |
---|
| 3278 | smearing_max = fp.smearing_options[fp.SMEARING_MAX] |
---|
| 3279 | self.smearing_widget.setState(smearing, accuracy, smearing_min, smearing_max) |
---|
| 3280 | |
---|
| 3281 | # TODO: add polidyspersity and magnetism |
---|
| 3282 | |
---|
| 3283 | def saveToFitPage(self, fp): |
---|
| 3284 | """ |
---|
| 3285 | Write current state to the given fitpage |
---|
| 3286 | """ |
---|
| 3287 | assert isinstance(fp, FitPage) |
---|
| 3288 | |
---|
| 3289 | # Main tab info |
---|
| 3290 | fp.filename = self.logic.data.filename |
---|
| 3291 | fp.data_is_loaded = self.data_is_loaded |
---|
| 3292 | fp.is_polydisperse = self.chkPolydispersity.isChecked() |
---|
| 3293 | fp.is_magnetic = self.chkMagnetism.isChecked() |
---|
| 3294 | fp.is2D = self.chk2DView.isChecked() |
---|
| 3295 | fp.data = self.data |
---|
| 3296 | |
---|
| 3297 | # Use current models - they contain all the required parameters |
---|
| 3298 | fp.model_model = self._model_model |
---|
| 3299 | fp.poly_model = self._poly_model |
---|
| 3300 | fp.magnetism_model = self._magnet_model |
---|
| 3301 | |
---|
| 3302 | if self.cbCategory.currentIndex() != 0: |
---|
| 3303 | fp.current_category = str(self.cbCategory.currentText()) |
---|
| 3304 | fp.current_model = str(self.cbModel.currentText()) |
---|
| 3305 | |
---|
| 3306 | if self.cbStructureFactor.isEnabled() and self.cbStructureFactor.currentIndex() != 0: |
---|
| 3307 | fp.current_factor = str(self.cbStructureFactor.currentText()) |
---|
| 3308 | else: |
---|
| 3309 | fp.current_factor = '' |
---|
| 3310 | |
---|
| 3311 | fp.chi2 = self.chi2 |
---|
[6dbff18] | 3312 | fp.main_params_to_fit = self.main_params_to_fit |
---|
| 3313 | fp.poly_params_to_fit = self.poly_params_to_fit |
---|
| 3314 | fp.magnet_params_to_fit = self.magnet_params_to_fit |
---|
[6964d44] | 3315 | fp.kernel_module = self.kernel_module |
---|
[672b8ab] | 3316 | |
---|
[6ff2eb3] | 3317 | # Algorithm options |
---|
| 3318 | # fp.algorithm = self.parent.fit_options.selected_id |
---|
| 3319 | |
---|
[672b8ab] | 3320 | # Options tab |
---|
| 3321 | fp.fit_options[fp.MIN_RANGE] = self.q_range_min |
---|
| 3322 | fp.fit_options[fp.MAX_RANGE] = self.q_range_max |
---|
| 3323 | fp.fit_options[fp.NPTS] = self.npts |
---|
| 3324 | #fp.fit_options[fp.NPTS_FIT] = self.npts_fit |
---|
| 3325 | fp.fit_options[fp.LOG_POINTS] = self.log_points |
---|
| 3326 | fp.fit_options[fp.WEIGHTING] = self.weighting |
---|
| 3327 | |
---|
| 3328 | # Resolution tab |
---|
| 3329 | smearing, accuracy, smearing_min, smearing_max = self.smearing_widget.state() |
---|
| 3330 | fp.smearing_options[fp.SMEARING_OPTION] = smearing |
---|
| 3331 | fp.smearing_options[fp.SMEARING_ACCURACY] = accuracy |
---|
| 3332 | fp.smearing_options[fp.SMEARING_MIN] = smearing_min |
---|
| 3333 | fp.smearing_options[fp.SMEARING_MAX] = smearing_max |
---|
| 3334 | |
---|
| 3335 | # TODO: add polidyspersity and magnetism |
---|
| 3336 | |
---|
[00b3b40] | 3337 | def updateUndo(self): |
---|
| 3338 | """ |
---|
| 3339 | Create a new state page and add it to the stack |
---|
| 3340 | """ |
---|
| 3341 | if self.undo_supported: |
---|
| 3342 | self.pushFitPage(self.currentState()) |
---|
| 3343 | |
---|
[672b8ab] | 3344 | def currentState(self): |
---|
| 3345 | """ |
---|
| 3346 | Return fit page with current state |
---|
| 3347 | """ |
---|
| 3348 | new_page = FitPage() |
---|
| 3349 | self.saveToFitPage(new_page) |
---|
| 3350 | |
---|
| 3351 | return new_page |
---|
| 3352 | |
---|
| 3353 | def pushFitPage(self, new_page): |
---|
| 3354 | """ |
---|
| 3355 | Add a new fit page object with current state |
---|
| 3356 | """ |
---|
[6011788] | 3357 | self.page_stack.append(new_page) |
---|
[672b8ab] | 3358 | |
---|
| 3359 | def popFitPage(self): |
---|
| 3360 | """ |
---|
| 3361 | Remove top fit page from stack |
---|
| 3362 | """ |
---|
[6011788] | 3363 | if self.page_stack: |
---|
| 3364 | self.page_stack.pop() |
---|
[672b8ab] | 3365 | |
---|
[57be490] | 3366 | def getReport(self): |
---|
| 3367 | """ |
---|
| 3368 | Create and return HTML report with parameters and charts |
---|
| 3369 | """ |
---|
| 3370 | index = None |
---|
| 3371 | if self.all_data: |
---|
| 3372 | index = self.all_data[self.data_index] |
---|
[cb90b65] | 3373 | else: |
---|
| 3374 | index = self.theory_item |
---|
[57be490] | 3375 | report_logic = ReportPageLogic(self, |
---|
| 3376 | kernel_module=self.kernel_module, |
---|
| 3377 | data=self.data, |
---|
| 3378 | index=index, |
---|
| 3379 | model=self._model_model) |
---|
| 3380 | |
---|
| 3381 | return report_logic.reportList() |
---|
| 3382 | |
---|
| 3383 | def loadPageStateCallback(self,state=None, datainfo=None, format=None): |
---|
| 3384 | """ |
---|
| 3385 | This is a callback method called from the CANSAS reader. |
---|
| 3386 | We need the instance of this reader only for writing out a file, |
---|
| 3387 | so there's nothing here. |
---|
| 3388 | Until Load Analysis is implemented, that is. |
---|
| 3389 | """ |
---|
| 3390 | pass |
---|
| 3391 | |
---|
| 3392 | def loadPageState(self, pagestate=None): |
---|
| 3393 | """ |
---|
| 3394 | Load the PageState object and update the current widget |
---|
| 3395 | """ |
---|
[6040cd7] | 3396 | filepath = self.loadAnalysisFile() |
---|
| 3397 | if filepath is None or filepath == "": |
---|
| 3398 | return |
---|
[57be490] | 3399 | |
---|
[6040cd7] | 3400 | with open(filepath, 'r') as statefile: |
---|
[9a42ea1] | 3401 | #column_data = [line.rstrip().split() for line in statefile.readlines()] |
---|
| 3402 | lines = statefile.readlines() |
---|
[57be490] | 3403 | |
---|
[6040cd7] | 3404 | # convert into list of lists |
---|
[9a42ea1] | 3405 | pass |
---|
[57be490] | 3406 | |
---|
[6040cd7] | 3407 | def loadAnalysisFile(self): |
---|
| 3408 | """ |
---|
| 3409 | Called when the "Open Project" menu item chosen. |
---|
| 3410 | """ |
---|
| 3411 | default_name = "FitPage"+str(self.tab_id)+".fitv" |
---|
| 3412 | wildcard = "fitv files (*.fitv)" |
---|
| 3413 | kwargs = { |
---|
| 3414 | 'caption' : 'Open Analysis', |
---|
| 3415 | 'directory' : default_name, |
---|
| 3416 | 'filter' : wildcard, |
---|
| 3417 | 'parent' : self, |
---|
| 3418 | } |
---|
| 3419 | filename = QtWidgets.QFileDialog.getOpenFileName(**kwargs)[0] |
---|
| 3420 | return filename |
---|
[57be490] | 3421 | |
---|
[d2007a8] | 3422 | def onCopyToClipboard(self, format=None): |
---|
| 3423 | """ |
---|
| 3424 | Copy current fitting parameters into the clipboard |
---|
| 3425 | using requested formatting: |
---|
| 3426 | plain, excel, latex |
---|
| 3427 | """ |
---|
| 3428 | param_list = self.getFitParameters() |
---|
| 3429 | if format=="": |
---|
[9a42ea1] | 3430 | param_list = self.getFitPage() |
---|
| 3431 | param_list += self.getFitModel() |
---|
[d2007a8] | 3432 | formatted_output = FittingUtilities.formatParameters(param_list) |
---|
| 3433 | elif format == "Excel": |
---|
| 3434 | formatted_output = FittingUtilities.formatParametersExcel(param_list[1:]) |
---|
| 3435 | elif format == "Latex": |
---|
| 3436 | formatted_output = FittingUtilities.formatParametersLatex(param_list[1:]) |
---|
| 3437 | else: |
---|
| 3438 | raise AttributeError("Bad parameter output format specifier.") |
---|
| 3439 | |
---|
| 3440 | # Dump formatted_output to the clipboard |
---|
| 3441 | cb = QtWidgets.QApplication.clipboard() |
---|
| 3442 | cb.setText(formatted_output) |
---|
| 3443 | |
---|
[6040cd7] | 3444 | def getFitModel(self): |
---|
[8e2cd79] | 3445 | """ |
---|
[9a42ea1] | 3446 | serializes combobox state |
---|
[6040cd7] | 3447 | """ |
---|
| 3448 | param_list = [] |
---|
| 3449 | model = str(self.cbModel.currentText()) |
---|
| 3450 | category = str(self.cbCategory.currentText()) |
---|
| 3451 | structure = str(self.cbStructureFactor.currentText()) |
---|
| 3452 | param_list.append(['fitpage_category', category]) |
---|
| 3453 | param_list.append(['fitpage_model', model]) |
---|
| 3454 | param_list.append(['fitpage_structure', structure]) |
---|
[9a42ea1] | 3455 | |
---|
[6040cd7] | 3456 | return param_list |
---|
| 3457 | |
---|
| 3458 | def getFitPage(self): |
---|
| 3459 | """ |
---|
| 3460 | serializes full state of this fit page |
---|
[8e2cd79] | 3461 | """ |
---|
| 3462 | # run a loop over all parameters and pull out |
---|
| 3463 | # first - regular params |
---|
[6040cd7] | 3464 | param_list = self.getFitParameters() |
---|
| 3465 | |
---|
[9a42ea1] | 3466 | param_list.append(['is_data', str(self.data_is_loaded)]) |
---|
| 3467 | if self.data_is_loaded: |
---|
| 3468 | param_list.append(['data_id', str(self.logic.data.id)]) |
---|
| 3469 | param_list.append(['data_name', str(self.logic.data.filename)]) |
---|
| 3470 | |
---|
[6040cd7] | 3471 | # option tab |
---|
| 3472 | param_list.append(['q_range_min', str(self.q_range_min)]) |
---|
| 3473 | param_list.append(['q_range_max', str(self.q_range_max)]) |
---|
| 3474 | param_list.append(['q_weighting', str(self.weighting)]) |
---|
[9a42ea1] | 3475 | param_list.append(['weighting', str(self.options_widget.weighting)]) |
---|
[6040cd7] | 3476 | |
---|
| 3477 | # resolution |
---|
| 3478 | smearing, accuracy, smearing_min, smearing_max = self.smearing_widget.state() |
---|
| 3479 | index = self.smearing_widget.cbSmearing.currentIndex() |
---|
| 3480 | param_list.append(['smearing', str(index)]) |
---|
| 3481 | param_list.append(['smearing_min', str(smearing_min)]) |
---|
| 3482 | param_list.append(['smearing_max', str(smearing_max)]) |
---|
| 3483 | |
---|
| 3484 | # checkboxes, if required |
---|
| 3485 | has_polydisp = self.chkPolydispersity.isChecked() |
---|
| 3486 | has_magnetism = self.chkMagnetism.isChecked() |
---|
| 3487 | has_chain = self.chkChainFit.isChecked() |
---|
| 3488 | has_2D = self.chk2DView.isChecked() |
---|
| 3489 | param_list.append(['polydisperse_params', str(has_polydisp)]) |
---|
| 3490 | param_list.append(['magnetic_params', str(has_magnetism)]) |
---|
| 3491 | param_list.append(['chainfit_params', str(has_chain)]) |
---|
| 3492 | param_list.append(['2D_params', str(has_2D)]) |
---|
| 3493 | |
---|
| 3494 | return param_list |
---|
| 3495 | |
---|
| 3496 | def getFitParameters(self): |
---|
| 3497 | """ |
---|
| 3498 | serializes current parameters |
---|
| 3499 | """ |
---|
[8e2cd79] | 3500 | param_list = [] |
---|
[2eeda93] | 3501 | if self.kernel_module is None: |
---|
| 3502 | return param_list |
---|
| 3503 | |
---|
[0eff615] | 3504 | param_list.append(['model_name', str(self.cbModel.currentText())]) |
---|
[fc9d9a3] | 3505 | |
---|
[8e2cd79] | 3506 | def gatherParams(row): |
---|
| 3507 | """ |
---|
| 3508 | Create list of main parameters based on _model_model |
---|
| 3509 | """ |
---|
| 3510 | param_name = str(self._model_model.item(row, 0).text()) |
---|
[685602a] | 3511 | |
---|
[d2007a8] | 3512 | # Assure this is a parameter - must contain a checkbox |
---|
| 3513 | if not self._model_model.item(row, 0).isCheckable(): |
---|
| 3514 | # maybe it is a combobox item (multiplicity) |
---|
| 3515 | try: |
---|
| 3516 | index = self._model_model.index(row, 1) |
---|
| 3517 | widget = self.lstParams.indexWidget(index) |
---|
| 3518 | if widget is None: |
---|
| 3519 | return |
---|
| 3520 | if isinstance(widget, QtWidgets.QComboBox): |
---|
| 3521 | # find the index of the combobox |
---|
| 3522 | current_index = widget.currentIndex() |
---|
| 3523 | param_list.append([param_name, 'None', str(current_index)]) |
---|
| 3524 | except Exception as ex: |
---|
| 3525 | pass |
---|
| 3526 | return |
---|
| 3527 | |
---|
| 3528 | param_checked = str(self._model_model.item(row, 0).checkState() == QtCore.Qt.Checked) |
---|
| 3529 | # Value of the parameter. In some cases this is the text of the combobox choice. |
---|
| 3530 | param_value = str(self._model_model.item(row, 1).text()) |
---|
| 3531 | param_error = None |
---|
| 3532 | param_min = None |
---|
| 3533 | param_max = None |
---|
| 3534 | column_offset = 0 |
---|
| 3535 | if self.has_error_column: |
---|
| 3536 | column_offset = 1 |
---|
[6040cd7] | 3537 | param_error = str(self._model_model.item(row, 1+column_offset).text()) |
---|
[d2007a8] | 3538 | try: |
---|
| 3539 | param_min = str(self._model_model.item(row, 2+column_offset).text()) |
---|
| 3540 | param_max = str(self._model_model.item(row, 3+column_offset).text()) |
---|
| 3541 | except: |
---|
[fc9d9a3] | 3542 | pass |
---|
[21fa45b] | 3543 | |
---|
[6040cd7] | 3544 | param_list.append([param_name, param_checked, param_value, param_error, param_min, param_max]) |
---|
[8e2cd79] | 3545 | |
---|
| 3546 | def gatherPolyParams(row): |
---|
| 3547 | """ |
---|
| 3548 | Create list of polydisperse parameters based on _poly_model |
---|
| 3549 | """ |
---|
| 3550 | param_name = str(self._poly_model.item(row, 0).text()).split()[-1] |
---|
| 3551 | param_checked = str(self._poly_model.item(row, 0).checkState() == QtCore.Qt.Checked) |
---|
| 3552 | param_value = str(self._poly_model.item(row, 1).text()) |
---|
| 3553 | param_error = None |
---|
| 3554 | column_offset = 0 |
---|
| 3555 | if self.has_poly_error_column: |
---|
| 3556 | column_offset = 1 |
---|
[6040cd7] | 3557 | param_error = str(self._poly_model.item(row, 1+column_offset).text()) |
---|
[8e2cd79] | 3558 | param_min = str(self._poly_model.item(row, 2+column_offset).text()) |
---|
| 3559 | param_max = str(self._poly_model.item(row, 3+column_offset).text()) |
---|
| 3560 | param_npts = str(self._poly_model.item(row, 4+column_offset).text()) |
---|
| 3561 | param_nsigs = str(self._poly_model.item(row, 5+column_offset).text()) |
---|
| 3562 | param_fun = str(self._poly_model.item(row, 6+column_offset).text()).rstrip() |
---|
[d2007a8] | 3563 | index = self._poly_model.index(row, 6+column_offset) |
---|
| 3564 | widget = self.lstPoly.indexWidget(index) |
---|
| 3565 | if widget is not None and isinstance(widget, QtWidgets.QComboBox): |
---|
| 3566 | param_fun = widget.currentText() |
---|
[8e2cd79] | 3567 | # width |
---|
| 3568 | name = param_name+".width" |
---|
[6040cd7] | 3569 | param_list.append([name, param_checked, param_value, param_error, |
---|
| 3570 | param_min, param_max, param_npts, param_nsigs, param_fun]) |
---|
[8e2cd79] | 3571 | |
---|
| 3572 | def gatherMagnetParams(row): |
---|
| 3573 | """ |
---|
| 3574 | Create list of magnetic parameters based on _magnet_model |
---|
| 3575 | """ |
---|
| 3576 | param_name = str(self._magnet_model.item(row, 0).text()) |
---|
| 3577 | param_checked = str(self._magnet_model.item(row, 0).checkState() == QtCore.Qt.Checked) |
---|
| 3578 | param_value = str(self._magnet_model.item(row, 1).text()) |
---|
| 3579 | param_error = None |
---|
| 3580 | column_offset = 0 |
---|
| 3581 | if self.has_magnet_error_column: |
---|
| 3582 | column_offset = 1 |
---|
[6040cd7] | 3583 | param_error = str(self._magnet_model.item(row, 1+column_offset).text()) |
---|
[8e2cd79] | 3584 | param_min = str(self._magnet_model.item(row, 2+column_offset).text()) |
---|
| 3585 | param_max = str(self._magnet_model.item(row, 3+column_offset).text()) |
---|
[6040cd7] | 3586 | param_list.append([param_name, param_checked, param_value, |
---|
| 3587 | param_error, param_min, param_max]) |
---|
[8e2cd79] | 3588 | |
---|
| 3589 | self.iterateOverModel(gatherParams) |
---|
| 3590 | if self.chkPolydispersity.isChecked(): |
---|
| 3591 | self.iterateOverPolyModel(gatherPolyParams) |
---|
[a98014ab] | 3592 | if self.chkMagnetism.isChecked() and self.canHaveMagnetism(): |
---|
[b259485] | 3593 | self.iterateOverMagnetModel(gatherMagnetParams) |
---|
[8e2cd79] | 3594 | |
---|
[d2007a8] | 3595 | if self.kernel_module.is_multiplicity_model: |
---|
| 3596 | param_list.append(['multiplicity', str(self.kernel_module.multiplicity)]) |
---|
[8e2cd79] | 3597 | |
---|
[d2007a8] | 3598 | return param_list |
---|
[8e2cd79] | 3599 | |
---|
| 3600 | def onParameterPaste(self): |
---|
| 3601 | """ |
---|
| 3602 | Use the clipboard to update fit state |
---|
| 3603 | """ |
---|
| 3604 | # Check if the clipboard contains right stuff |
---|
| 3605 | cb = QtWidgets.QApplication.clipboard() |
---|
| 3606 | cb_text = cb.text() |
---|
| 3607 | |
---|
| 3608 | lines = cb_text.split(':') |
---|
| 3609 | if lines[0] != 'sasview_parameter_values': |
---|
| 3610 | return False |
---|
[0eff615] | 3611 | |
---|
[d2007a8] | 3612 | # put the text into dictionary |
---|
| 3613 | line_dict = {} |
---|
| 3614 | for line in lines[1:]: |
---|
| 3615 | content = line.split(',') |
---|
| 3616 | if len(content) > 1: |
---|
| 3617 | line_dict[content[0]] = content[1:] |
---|
[0eff615] | 3618 | |
---|
[2eeda93] | 3619 | self.updatePageWithParameters(line_dict) |
---|
[21fa45b] | 3620 | |
---|
[2eeda93] | 3621 | def createPageForParameters(self, line_dict): |
---|
| 3622 | """ |
---|
| 3623 | Sets up page with requested model/str factor |
---|
| 3624 | and fills it up with sent parameters |
---|
| 3625 | """ |
---|
| 3626 | if 'fitpage_category' in line_dict: |
---|
| 3627 | self.cbCategory.setCurrentIndex(self.cbCategory.findText(line_dict['fitpage_category'][0])) |
---|
| 3628 | if 'fitpage_model' in line_dict: |
---|
| 3629 | self.cbModel.setCurrentIndex(self.cbModel.findText(line_dict['fitpage_model'][0])) |
---|
| 3630 | if 'fitpage_structure' in line_dict: |
---|
| 3631 | self.cbStructureFactor.setCurrentIndex(self.cbStructureFactor.findText(line_dict['fitpage_structure'][0])) |
---|
| 3632 | |
---|
| 3633 | # Now that the page is ready for parameters, fill it up |
---|
| 3634 | self.updatePageWithParameters(line_dict) |
---|
| 3635 | |
---|
| 3636 | def updatePageWithParameters(self, line_dict): |
---|
| 3637 | """ |
---|
| 3638 | Update FitPage with parameters in line_dict |
---|
| 3639 | """ |
---|
[d2007a8] | 3640 | if 'model_name' not in line_dict.keys(): |
---|
[2eeda93] | 3641 | return |
---|
| 3642 | model = line_dict['model_name'][0] |
---|
| 3643 | context = {} |
---|
[21fa45b] | 3644 | |
---|
[d2007a8] | 3645 | if 'multiplicity' in line_dict.keys(): |
---|
| 3646 | multip = int(line_dict['multiplicity'][0], 0) |
---|
| 3647 | # reset the model with multiplicity, so further updates are saved |
---|
| 3648 | if self.kernel_module.is_multiplicity_model: |
---|
| 3649 | self.kernel_module.multiplicity=multip |
---|
| 3650 | self.updateMultiplicityCombo(multip) |
---|
| 3651 | |
---|
| 3652 | if 'polydisperse_params' in line_dict.keys(): |
---|
| 3653 | self.chkPolydispersity.setChecked(line_dict['polydisperse_params'][0]=='True') |
---|
| 3654 | if 'magnetic_params' in line_dict.keys(): |
---|
| 3655 | self.chkMagnetism.setChecked(line_dict['magnetic_params'][0]=='True') |
---|
| 3656 | if 'chainfit_params' in line_dict.keys(): |
---|
| 3657 | self.chkChainFit.setChecked(line_dict['chainfit_params'][0]=='True') |
---|
| 3658 | if '2D_params' in line_dict.keys(): |
---|
| 3659 | self.chk2DView.setChecked(line_dict['2D_params'][0]=='True') |
---|
| 3660 | |
---|
| 3661 | # Create the context dictionary for parameters |
---|
| 3662 | context['model_name'] = model |
---|
| 3663 | for key, value in line_dict.items(): |
---|
| 3664 | if len(value) > 2: |
---|
| 3665 | context[key] = value |
---|
| 3666 | |
---|
| 3667 | if str(self.cbModel.currentText()) != str(context['model_name']): |
---|
[0eff615] | 3668 | msg = QtWidgets.QMessageBox() |
---|
| 3669 | msg.setIcon(QtWidgets.QMessageBox.Information) |
---|
| 3670 | msg.setText("The model in the clipboard is not the same as the currently loaded model. \ |
---|
| 3671 | Not all parameters saved may paste correctly.") |
---|
| 3672 | msg.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel) |
---|
| 3673 | result = msg.exec_() |
---|
| 3674 | if result == QtWidgets.QMessageBox.Ok: |
---|
| 3675 | pass |
---|
| 3676 | else: |
---|
| 3677 | return |
---|
| 3678 | |
---|
[d2007a8] | 3679 | if 'smearing' in line_dict.keys(): |
---|
| 3680 | try: |
---|
| 3681 | index = int(line_dict['smearing'][0]) |
---|
| 3682 | self.smearing_widget.cbSmearing.setCurrentIndex(index) |
---|
| 3683 | except ValueError: |
---|
| 3684 | pass |
---|
| 3685 | if 'smearing_min' in line_dict.keys(): |
---|
| 3686 | try: |
---|
| 3687 | self.smearing_widget.dq_l = float(line_dict['smearing_min'][0]) |
---|
| 3688 | except ValueError: |
---|
| 3689 | pass |
---|
| 3690 | if 'smearing_max' in line_dict.keys(): |
---|
| 3691 | try: |
---|
| 3692 | self.smearing_widget.dq_r = float(line_dict['smearing_max'][0]) |
---|
| 3693 | except ValueError: |
---|
| 3694 | pass |
---|
| 3695 | |
---|
| 3696 | if 'q_range_max' in line_dict.keys(): |
---|
| 3697 | try: |
---|
| 3698 | self.q_range_min = float(line_dict['q_range_min'][0]) |
---|
| 3699 | self.q_range_max = float(line_dict['q_range_max'][0]) |
---|
| 3700 | except ValueError: |
---|
| 3701 | pass |
---|
| 3702 | self.options_widget.updateQRange(self.q_range_min, self.q_range_max, self.npts) |
---|
[9a42ea1] | 3703 | try: |
---|
| 3704 | button_id = int(line_dict['weighting'][0]) |
---|
| 3705 | for button in self.options_widget.weightingGroup.buttons(): |
---|
| 3706 | if abs(self.options_widget.weightingGroup.id(button)) == button_id+2: |
---|
| 3707 | button.setChecked(True) |
---|
| 3708 | break |
---|
| 3709 | except ValueError: |
---|
| 3710 | pass |
---|
[d2007a8] | 3711 | |
---|
[8e2cd79] | 3712 | self.updateFullModel(context) |
---|
| 3713 | self.updateFullPolyModel(context) |
---|
[d2007a8] | 3714 | self.updateFullMagnetModel(context) |
---|
| 3715 | |
---|
| 3716 | def updateMultiplicityCombo(self, multip): |
---|
| 3717 | """ |
---|
| 3718 | Find and update the multiplicity combobox |
---|
| 3719 | """ |
---|
| 3720 | index = self._model_model.index(self._n_shells_row, 1) |
---|
| 3721 | widget = self.lstParams.indexWidget(index) |
---|
| 3722 | if widget is not None and isinstance(widget, QtWidgets.QComboBox): |
---|
| 3723 | widget.setCurrentIndex(widget.findText(str(multip))) |
---|
| 3724 | self.current_shell_displayed = multip |
---|
[8e2cd79] | 3725 | |
---|
| 3726 | def updateFullModel(self, param_dict): |
---|
| 3727 | """ |
---|
| 3728 | Update the model with new parameters |
---|
| 3729 | """ |
---|
| 3730 | assert isinstance(param_dict, dict) |
---|
| 3731 | if not dict: |
---|
| 3732 | return |
---|
| 3733 | |
---|
| 3734 | def updateFittedValues(row): |
---|
| 3735 | # Utility function for main model update |
---|
| 3736 | # internal so can use closure for param_dict |
---|
| 3737 | param_name = str(self._model_model.item(row, 0).text()) |
---|
| 3738 | if param_name not in list(param_dict.keys()): |
---|
| 3739 | return |
---|
[d2007a8] | 3740 | # Special case of combo box in the cell (multiplicity) |
---|
| 3741 | param_line = param_dict[param_name] |
---|
| 3742 | if len(param_line) == 1: |
---|
| 3743 | # modify the shells value |
---|
| 3744 | try: |
---|
| 3745 | combo_index = int(param_line[0]) |
---|
| 3746 | except ValueError: |
---|
| 3747 | # quietly pass |
---|
| 3748 | return |
---|
| 3749 | index = self._model_model.index(row, 1) |
---|
| 3750 | widget = self.lstParams.indexWidget(index) |
---|
| 3751 | if widget is not None and isinstance(widget, QtWidgets.QComboBox): |
---|
| 3752 | #widget.setCurrentIndex(combo_index) |
---|
| 3753 | return |
---|
[8e2cd79] | 3754 | # checkbox state |
---|
| 3755 | param_checked = QtCore.Qt.Checked if param_dict[param_name][0] == "True" else QtCore.Qt.Unchecked |
---|
| 3756 | self._model_model.item(row, 0).setCheckState(param_checked) |
---|
| 3757 | |
---|
[d2007a8] | 3758 | # parameter value can be either just a value or text on the combobox |
---|
| 3759 | param_text = param_dict[param_name][1] |
---|
| 3760 | index = self._model_model.index(row, 1) |
---|
| 3761 | widget = self.lstParams.indexWidget(index) |
---|
| 3762 | if widget is not None and isinstance(widget, QtWidgets.QComboBox): |
---|
| 3763 | # Find the right index based on text |
---|
| 3764 | combo_index = int(param_text, 0) |
---|
| 3765 | widget.setCurrentIndex(combo_index) |
---|
| 3766 | else: |
---|
| 3767 | # modify the param value |
---|
| 3768 | param_repr = GuiUtils.formatNumber(param_text, high=True) |
---|
| 3769 | self._model_model.item(row, 1).setText(param_repr) |
---|
[8e2cd79] | 3770 | |
---|
| 3771 | # Potentially the error column |
---|
| 3772 | ioffset = 0 |
---|
[6040cd7] | 3773 | joffset = 0 |
---|
[b8dccb8] | 3774 | if len(param_dict[param_name])>4: |
---|
[8e2cd79] | 3775 | # error values are not editable - no need to update |
---|
| 3776 | ioffset = 1 |
---|
[6040cd7] | 3777 | if self.has_error_column: |
---|
| 3778 | joffset = 1 |
---|
[8e2cd79] | 3779 | # min/max |
---|
[685602a] | 3780 | try: |
---|
| 3781 | param_repr = GuiUtils.formatNumber(param_dict[param_name][2+ioffset], high=True) |
---|
[6040cd7] | 3782 | self._model_model.item(row, 2+joffset).setText(param_repr) |
---|
[685602a] | 3783 | param_repr = GuiUtils.formatNumber(param_dict[param_name][3+ioffset], high=True) |
---|
[6040cd7] | 3784 | self._model_model.item(row, 3+joffset).setText(param_repr) |
---|
[685602a] | 3785 | except: |
---|
| 3786 | pass |
---|
| 3787 | |
---|
[0eff615] | 3788 | self.setFocus() |
---|
| 3789 | |
---|
[8e2cd79] | 3790 | self.iterateOverModel(updateFittedValues) |
---|
[0eff615] | 3791 | |
---|
[8e2cd79] | 3792 | def updateFullPolyModel(self, param_dict): |
---|
| 3793 | """ |
---|
| 3794 | Update the polydispersity model with new parameters, create the errors column |
---|
| 3795 | """ |
---|
| 3796 | assert isinstance(param_dict, dict) |
---|
| 3797 | if not dict: |
---|
| 3798 | return |
---|
| 3799 | |
---|
| 3800 | def updateFittedValues(row): |
---|
| 3801 | # Utility function for main model update |
---|
| 3802 | # internal so can use closure for param_dict |
---|
| 3803 | if row >= self._poly_model.rowCount(): |
---|
| 3804 | return |
---|
| 3805 | param_name = str(self._poly_model.item(row, 0).text()).rsplit()[-1] + '.width' |
---|
| 3806 | if param_name not in list(param_dict.keys()): |
---|
| 3807 | return |
---|
| 3808 | # checkbox state |
---|
| 3809 | param_checked = QtCore.Qt.Checked if param_dict[param_name][0] == "True" else QtCore.Qt.Unchecked |
---|
| 3810 | self._poly_model.item(row,0).setCheckState(param_checked) |
---|
| 3811 | |
---|
| 3812 | # modify the param value |
---|
| 3813 | param_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 3814 | self._poly_model.item(row, 1).setText(param_repr) |
---|
| 3815 | |
---|
| 3816 | # Potentially the error column |
---|
| 3817 | ioffset = 0 |
---|
[6040cd7] | 3818 | joffset = 0 |
---|
[b8dccb8] | 3819 | if len(param_dict[param_name])>7: |
---|
[8e2cd79] | 3820 | ioffset = 1 |
---|
[6040cd7] | 3821 | if self.has_poly_error_column: |
---|
| 3822 | joffset = 1 |
---|
[8e2cd79] | 3823 | # min |
---|
| 3824 | param_repr = GuiUtils.formatNumber(param_dict[param_name][2+ioffset], high=True) |
---|
[6040cd7] | 3825 | self._poly_model.item(row, 2+joffset).setText(param_repr) |
---|
[8e2cd79] | 3826 | # max |
---|
| 3827 | param_repr = GuiUtils.formatNumber(param_dict[param_name][3+ioffset], high=True) |
---|
[6040cd7] | 3828 | self._poly_model.item(row, 3+joffset).setText(param_repr) |
---|
[8e2cd79] | 3829 | # Npts |
---|
| 3830 | param_repr = GuiUtils.formatNumber(param_dict[param_name][4+ioffset], high=True) |
---|
[6040cd7] | 3831 | self._poly_model.item(row, 4+joffset).setText(param_repr) |
---|
[8e2cd79] | 3832 | # Nsigs |
---|
| 3833 | param_repr = GuiUtils.formatNumber(param_dict[param_name][5+ioffset], high=True) |
---|
[6040cd7] | 3834 | self._poly_model.item(row, 5+joffset).setText(param_repr) |
---|
[8e2cd79] | 3835 | |
---|
[0eff615] | 3836 | self.setFocus() |
---|
[8e2cd79] | 3837 | |
---|
| 3838 | self.iterateOverPolyModel(updateFittedValues) |
---|
| 3839 | |
---|
[d2007a8] | 3840 | def updateFullMagnetModel(self, param_dict): |
---|
| 3841 | """ |
---|
| 3842 | Update the magnetism model with new parameters, create the errors column |
---|
| 3843 | """ |
---|
| 3844 | assert isinstance(param_dict, dict) |
---|
| 3845 | if not dict: |
---|
| 3846 | return |
---|
| 3847 | |
---|
| 3848 | def updateFittedValues(row): |
---|
| 3849 | # Utility function for main model update |
---|
| 3850 | # internal so can use closure for param_dict |
---|
| 3851 | if row >= self._magnet_model.rowCount(): |
---|
| 3852 | return |
---|
| 3853 | param_name = str(self._magnet_model.item(row, 0).text()).rsplit()[-1] |
---|
| 3854 | if param_name not in list(param_dict.keys()): |
---|
| 3855 | return |
---|
| 3856 | # checkbox state |
---|
| 3857 | param_checked = QtCore.Qt.Checked if param_dict[param_name][0] == "True" else QtCore.Qt.Unchecked |
---|
| 3858 | self._magnet_model.item(row,0).setCheckState(param_checked) |
---|
| 3859 | |
---|
| 3860 | # modify the param value |
---|
| 3861 | param_repr = GuiUtils.formatNumber(param_dict[param_name][1], high=True) |
---|
| 3862 | self._magnet_model.item(row, 1).setText(param_repr) |
---|
[339e22b] | 3863 | |
---|
[d2007a8] | 3864 | # Potentially the error column |
---|
| 3865 | ioffset = 0 |
---|
[6040cd7] | 3866 | joffset = 0 |
---|
[b8dccb8] | 3867 | if len(param_dict[param_name])>4: |
---|
[d2007a8] | 3868 | ioffset = 1 |
---|
[6040cd7] | 3869 | if self.has_magnet_error_column: |
---|
| 3870 | joffset = 1 |
---|
[d2007a8] | 3871 | # min |
---|
| 3872 | param_repr = GuiUtils.formatNumber(param_dict[param_name][2+ioffset], high=True) |
---|
[6040cd7] | 3873 | self._magnet_model.item(row, 2+joffset).setText(param_repr) |
---|
[d2007a8] | 3874 | # max |
---|
| 3875 | param_repr = GuiUtils.formatNumber(param_dict[param_name][3+ioffset], high=True) |
---|
[6040cd7] | 3876 | self._magnet_model.item(row, 3+joffset).setText(param_repr) |
---|
[339e22b] | 3877 | |
---|
[d2007a8] | 3878 | self.iterateOverMagnetModel(updateFittedValues) |
---|
[6040cd7] | 3879 | |
---|
| 3880 | def getCurrentFitState(self, state=None): |
---|
| 3881 | """ |
---|
| 3882 | Store current state for fit_page |
---|
| 3883 | """ |
---|
| 3884 | # save model option |
---|
| 3885 | #if self.model is not None: |
---|
| 3886 | # self.disp_list = self.getDispParamList() |
---|
| 3887 | # state.disp_list = copy.deepcopy(self.disp_list) |
---|
| 3888 | # #state.model = self.model.clone() |
---|
| 3889 | |
---|
| 3890 | # Comboboxes |
---|
| 3891 | state.categorycombobox = self.cbCategory.currentText() |
---|
| 3892 | state.formfactorcombobox = self.cbModel.currentText() |
---|
| 3893 | if self.cbStructureFactor.isEnabled(): |
---|
| 3894 | state.structurecombobox = self.cbStructureFactor.currentText() |
---|
| 3895 | state.tcChi = self.chi2 |
---|
| 3896 | |
---|
| 3897 | state.enable2D = self.is2D |
---|
| 3898 | |
---|
| 3899 | #state.weights = copy.deepcopy(self.weights) |
---|
| 3900 | # save data |
---|
| 3901 | state.data = copy.deepcopy(self.data) |
---|
| 3902 | |
---|
| 3903 | # save plotting range |
---|
| 3904 | state.qmin = self.q_range_min |
---|
| 3905 | state.qmax = self.q_range_max |
---|
| 3906 | state.npts = self.npts |
---|
| 3907 | |
---|
| 3908 | # self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 3909 | # self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 3910 | |
---|
| 3911 | # self.state.enable_smearer = \ |
---|
| 3912 | # copy.deepcopy(self.enable_smearer.GetValue()) |
---|
| 3913 | # self.state.disable_smearer = \ |
---|
| 3914 | # copy.deepcopy(self.disable_smearer.GetValue()) |
---|
| 3915 | |
---|
| 3916 | #self.state.pinhole_smearer = \ |
---|
| 3917 | # copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
| 3918 | #self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
| 3919 | #self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue()) |
---|
| 3920 | #self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue()) |
---|
| 3921 | #self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue()) |
---|
| 3922 | #self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) |
---|
| 3923 | |
---|
| 3924 | p = self.model_parameters |
---|
| 3925 | # save checkbutton state and txtcrtl values |
---|
| 3926 | state.parameters = FittingUtilities.getStandardParam(self._model_model) |
---|
| 3927 | state.orientation_params_disp = FittingUtilities.getOrientationParam(self.kernel_module) |
---|
| 3928 | |
---|
| 3929 | #self._copy_parameters_state(self.orientation_params_disp, self.state.orientation_params_disp) |
---|
| 3930 | #self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
| 3931 | #self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
| 3932 | #self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
| 3933 | |
---|