Changeset b69b549 in sasview for src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
- Timestamp:
- Sep 7, 2018 10:10:42 AM (6 years ago)
- Branches:
- ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- 5fb714b
- Parents:
- bc7371fd (diff), fb560d2 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
ra758043 rb69b549 91 91 fittingFinishedSignal = QtCore.pyqtSignal(tuple) 92 92 batchFittingFinishedSignal = QtCore.pyqtSignal(tuple) 93 Calc1DFinishedSignal = QtCore.pyqtSignal( tuple)94 Calc2DFinishedSignal = QtCore.pyqtSignal( tuple)93 Calc1DFinishedSignal = QtCore.pyqtSignal(dict) 94 Calc2DFinishedSignal = QtCore.pyqtSignal(dict) 95 95 96 96 def __init__(self, parent=None, data=None, tab_id=1): … … 219 219 # Utility variable to enable unselectable option in category combobox 220 220 self._previous_category_index = 0 221 # Utility variable for multishell display 222 self._last_model_row = 0 221 # Utility variables for multishell display 222 self._n_shells_row = 0 223 self._num_shell_params = 0 223 224 # Dictionary of {model name: model class} for the current category 224 225 self.models = {} … … 247 248 # copy of current kernel model 248 249 self.kernel_module_copy = None 250 251 # dictionaries of current params 252 self.poly_params = {} 253 self.magnet_params = {} 249 254 250 255 # Page id for fitting … … 672 677 Return list of all parameters for the current model 673 678 """ 674 return [self._model_model.item(row).text() for row in range(self._model_model.rowCount())] 679 return [self._model_model.item(row).text() 680 for row in range(self._model_model.rowCount()) 681 if self.isCheckable(row)] 675 682 676 683 def modifyViewOnRow(self, row, font=None, brush=None): … … 700 707 assert isinstance(constraint, Constraint) 701 708 assert 0 <= row <= self._model_model.rowCount() 709 assert self.isCheckable(row) 702 710 703 711 item = QtGui.QStandardItem() … … 720 728 max_col = self.lstParams.itemDelegate().param_max 721 729 for row in self.selectedParameters(): 730 assert(self.isCheckable(row)) 722 731 param = self._model_model.item(row, 0).text() 723 732 value = self._model_model.item(row, 1).text() … … 762 771 max_col = self.lstParams.itemDelegate().param_max 763 772 for row in range(self._model_model.rowCount()): 773 if not self.isCheckable(row): 774 continue 764 775 if not self.rowHasConstraint(row): 765 776 continue … … 790 801 For the given row, return its constraint, if any 791 802 """ 792 try:803 if self.isCheckable(row): 793 804 item = self._model_model.item(row, 1) 794 return item.child(0).data() 795 except AttributeError: 796 # return none when no constraints 797 return None 805 try: 806 return item.child(0).data() 807 except AttributeError: 808 # return none when no constraints 809 pass 810 return None 798 811 799 812 def rowHasConstraint(self, row): … … 801 814 Finds out if row of the main model has a constraint child 802 815 """ 803 item = self._model_model.item(row, 1) 804 if item.hasChildren(): 805 c = item.child(0).data() 806 if isinstance(c, Constraint): 807 return True 816 if self.isCheckable(row): 817 item = self._model_model.item(row, 1) 818 if item.hasChildren(): 819 c = item.child(0).data() 820 if isinstance(c, Constraint): 821 return True 808 822 return False 809 823 … … 812 826 Finds out if row of the main model has an active constraint child 813 827 """ 814 item = self._model_model.item(row, 1) 815 if item.hasChildren(): 816 c = item.child(0).data() 817 if isinstance(c, Constraint) and c.active: 818 return True 828 if self.isCheckable(row): 829 item = self._model_model.item(row, 1) 830 if item.hasChildren(): 831 c = item.child(0).data() 832 if isinstance(c, Constraint) and c.active: 833 return True 819 834 return False 820 835 … … 823 838 Finds out if row of the main model has an active, nontrivial constraint child 824 839 """ 825 item = self._model_model.item(row, 1) 826 if item.hasChildren(): 827 c = item.child(0).data() 828 if isinstance(c, Constraint) and c.func and c.active: 829 return True 840 if self.isCheckable(row): 841 item = self._model_model.item(row, 1) 842 if item.hasChildren(): 843 c = item.child(0).data() 844 if isinstance(c, Constraint) and c.func and c.active: 845 return True 830 846 return False 831 847 … … 1186 1202 # Update the sasmodel 1187 1203 # PD[ratio] -> width, npts -> npts, nsigs -> nsigmas 1188 self.kernel_module.setParam(parameter_name + '.' + delegate.columnDict()[model_column], value) 1204 #self.kernel_module.setParam(parameter_name + '.' + delegate.columnDict()[model_column], value) 1205 key = parameter_name + '.' + delegate.columnDict()[model_column] 1206 self.poly_params[key] = value 1189 1207 1190 1208 # Update plot … … 1195 1213 row = self.getRowFromName(parameter_name) 1196 1214 param_item = self._model_model.item(row) 1215 self._model_model.blockSignals(True) 1197 1216 param_item.child(0).child(0, model_column).setText(item.text()) 1217 self._model_model.blockSignals(False) 1198 1218 1199 1219 def onMagnetModelChange(self, item): … … 1224 1244 # Unparsable field 1225 1245 return 1226 1227 property_index = self._magnet_model.headerData(1, model_column)-1 # Value, min, max, etc. 1228 1229 # Update the parameter value - note: this supports +/-inf as well 1230 self.kernel_module.params[parameter_name] = value 1231 1232 # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf] 1233 self.kernel_module.details[parameter_name][property_index] = value 1234 1235 # Force the chart update when actual parameters changed 1236 if model_column == 1: 1246 delegate = self.lstMagnetic.itemDelegate() 1247 1248 if model_column > 1: 1249 if model_column == delegate.mag_min: 1250 pos = 1 1251 elif model_column == delegate.mag_max: 1252 pos = 2 1253 elif model_column == delegate.mag_unit: 1254 pos = 0 1255 else: 1256 raise AttributeError("Wrong column in magnetism table.") 1257 # min/max to be changed in self.kernel_module.details[parameter_name] = ['Ang', 0.0, inf] 1258 self.kernel_module.details[parameter_name][pos] = value 1259 else: 1260 self.magnet_params[parameter_name] = value 1261 #self.kernel_module.setParam(parameter_name) = value 1262 # Force the chart update when actual parameters changed 1237 1263 self.recalculatePlotData() 1238 1264 … … 1495 1521 # Data going in 1496 1522 data = self.logic.data 1497 model = self.kernel_module1523 model = copy.deepcopy(self.kernel_module) 1498 1524 qmin = self.q_range_min 1499 1525 qmax = self.q_range_max 1526 # add polydisperse/magnet parameters if asked 1527 self.updateKernelModelWithExtraParams(model) 1500 1528 1501 1529 params_to_fit = self.main_params_to_fit … … 1561 1589 # internal so can use closure for param_dict 1562 1590 param_name = str(self._model_model.item(row, 0).text()) 1563 if param_name not in list(param_dict.keys()):1591 if not self.isCheckable(row) or param_name not in list(param_dict.keys()): 1564 1592 return 1565 1593 # modify the param value … … 1573 1601 # Utility function for updateof polydispersity part of the main model 1574 1602 param_name = str(self._model_model.item(row, 0).text())+'.width' 1575 if param_name not in list(param_dict.keys()):1603 if not self.isCheckable(row) or param_name not in list(param_dict.keys()): 1576 1604 return 1577 1605 # modify the param value … … 1950 1978 # Crete/overwrite model items 1951 1979 self._model_model.clear() 1952 1953 # First, add parameters from the main model 1954 if model_name is not None: 1980 self._poly_model.clear() 1981 self._magnet_model.clear() 1982 1983 if model_name is None: 1984 if structure_factor not in (None, "None"): 1985 # S(Q) on its own, treat the same as a form factor 1986 self.kernel_module = None 1987 self.fromStructureFactorToQModel(structure_factor) 1988 else: 1989 # No models selected 1990 return 1991 else: 1955 1992 self.fromModelToQModel(model_name) 1956 1957 # Then, add structure factor derived parameters 1958 if structure_factor is not None and structure_factor != "None": 1959 if model_name is None: 1960 # Instantiate the current sasmodel for SF-only models 1961 self.kernel_module = self.models[structure_factor]() 1962 self.fromStructureFactorToQModel(structure_factor) 1963 else: 1993 self.addExtraShells() 1994 1964 1995 # Allow the SF combobox visibility for the given sasmodel 1965 1996 self.enableStructureFactorControl(structure_factor) 1997 1998 # Add S(Q) 1966 1999 if self.cbStructureFactor.isEnabled(): 1967 2000 structure_factor = self.cbStructureFactor.currentText() 1968 2001 self.fromStructureFactorToQModel(structure_factor) 1969 2002 1970 # Then, add multishells 1971 if model_name is not None: 1972 # Multishell models need additional treatment 1973 self.addExtraShells() 1974 1975 # Add polydispersity to the model 1976 self.setPolyModel() 1977 # Add magnetic parameters to the model 1978 self.setMagneticModel() 2003 # Add polydispersity to the model 2004 self.poly_params = {} 2005 self.setPolyModel() 2006 # Add magnetic parameters to the model 2007 self.magnet_params = {} 2008 self.setMagneticModel() 1979 2009 1980 2010 # Adjust the table cells width … … 2049 2079 self.shell_names = self.shellNamesList() 2050 2080 2081 # Add heading row 2082 FittingUtilities.addHeadingRowToModel(self._model_model, model_name) 2083 2051 2084 # Update the QModel 2052 2085 FittingUtilities.addParametersToModel( … … 2057 2090 self.lstParams) 2058 2091 2059 # Update the counter used for multishell display2060 self._last_model_row = self._model_model.rowCount()2061 2062 2092 def fromStructureFactorToQModel(self, structure_factor): 2063 2093 """ … … 2066 2096 if structure_factor is None or structure_factor=="None": 2067 2097 return 2068 structure_module = generate.load_kernel_module(structure_factor) 2069 structure_parameters = modelinfo.make_parameter_table(getattr(structure_module, 'parameters', [])) 2070 2071 structure_kernel = self.models[structure_factor]() 2072 form_kernel = self.kernel_module 2073 2074 self.kernel_module = MultiplicationModel(form_kernel, structure_kernel) 2075 2076 # Update the QModel 2098 2099 s_kernel = self.models[structure_factor]() 2100 p_kernel = self.kernel_module 2101 2102 # if p_kernel is None: 2103 # # Not a product model, just S(Q) 2104 # self.kernel_module = s_kernel 2105 # params = modelinfo.ParameterTable(self.kernel_module._model_info.parameters.kernel_parameters) 2106 # FittingUtilities.addSimpleParametersToModel(params, self.is2D) 2107 # else: 2108 p_pars_len = len(p_kernel._model_info.parameters.kernel_parameters) 2109 s_pars_len = len(s_kernel._model_info.parameters.kernel_parameters) 2110 2111 self.kernel_module = MultiplicationModel(p_kernel, s_kernel) 2112 all_params = self.kernel_module._model_info.parameters.kernel_parameters 2113 all_param_names = [param.name for param in all_params] 2114 2115 # S(Q) params from the product model are not necessarily the same as those from the S(Q) model; any 2116 # conflicting names with P(Q) params will cause a rename 2117 2118 if "radius_effective_mode" in all_param_names: 2119 # Show all parameters 2120 s_params = modelinfo.ParameterTable(all_params[p_pars_len:p_pars_len+s_pars_len]) 2121 s_params_orig = modelinfo.ParameterTable(s_kernel._model_info.parameters.kernel_parameters) 2122 else: 2123 # Ensure radius_effective is not displayed 2124 s_params_orig = modelinfo.ParameterTable(s_kernel._model_info.parameters.kernel_parameters[1:]) 2125 if "radius_effective" in all_param_names: 2126 s_params = modelinfo.ParameterTable(all_params[p_pars_len+1:p_pars_len+s_pars_len]) 2127 else: 2128 s_params = modelinfo.ParameterTable(all_params[p_pars_len:p_pars_len+s_pars_len-1]) 2129 2130 # Add heading row 2131 FittingUtilities.addHeadingRowToModel(self._model_model, structure_factor) 2132 2133 # Get new rows for QModel 2134 # Any renamed parameters are stored as data in the relevant item, for later handling 2077 2135 FittingUtilities.addSimpleParametersToModel( 2078 s tructure_parameters,2136 s_params, 2079 2137 self.is2D, 2138 s_params_orig, 2080 2139 self._model_model, 2081 2140 self.lstParams) 2082 2083 # Any parameters removed from the structure factor when producing the product model, e.g. radius_effective, must2084 # be disabled (greyed out, etc.)2085 for r in range(self._last_model_row, self._model_model.rowCount()):2086 param_name = self._model_model.item(r, 0).text()2087 if param_name not in self.kernel_module.params.keys():2088 FittingUtilities.markParameterDisabled(self._model_model, r)2089 2090 # Update the counter used for multishell display2091 self._last_model_row = self._model_model.rowCount()2092 2141 2093 2142 def haveParamsToFit(self): … … 2115 2164 model_row = item.row() 2116 2165 name_index = self._model_model.index(model_row, 0) 2166 name_item = self._model_model.itemFromIndex(name_index) 2117 2167 2118 2168 # Extract changed value. … … 2123 2173 return 2124 2174 2125 parameter_name = str(self._model_model.data(name_index)) # sld, background etc. 2175 # if the item has user data, this is the actual parameter name (e.g. to handle duplicate names) 2176 if name_item.data(QtCore.Qt.UserRole): 2177 parameter_name = str(name_item.data(QtCore.Qt.UserRole)) 2178 else: 2179 parameter_name = str(self._model_model.data(name_index)) 2126 2180 2127 2181 # Update the parameter value - note: this supports +/-inf as well … … 2246 2300 return self.completed1D if isinstance(self.data, Data1D) else self.completed2D 2247 2301 2302 def updateKernelModelWithExtraParams(self, model=None): 2303 """ 2304 Updates kernel model 'model' with extra parameters from 2305 the polydisp and magnetism tab, if the tabs are enabled 2306 """ 2307 if model is None: return 2308 if not hasattr(model, 'setParam'): return 2309 2310 # add polydisperse parameters if asked 2311 if self.chkPolydispersity.isChecked(): 2312 for key, value in self.poly_params.items(): 2313 model.setParam(key, value) 2314 # add magnetic params if asked 2315 if self.chkMagnetism.isChecked(): 2316 for key, value in self.magnet_params.items(): 2317 model.setParam(key, value) 2318 2248 2319 def calculateQGridForModelExt(self, data=None, model=None, completefn=None, use_threads=True): 2249 2320 """ … … 2253 2324 data = self.data 2254 2325 if model is None: 2255 model = self.kernel_module 2326 model = copy.deepcopy(self.kernel_module) 2327 self.updateKernelModelWithExtraParams(model) 2328 2256 2329 if completefn is None: 2257 2330 completefn = self.methodCompleteForData() … … 2338 2411 new_plots.append(sq_data) 2339 2412 2413 for plot in new_plots: 2414 self.communicate.plotUpdateSignal.emit([plot]) 2415 2416 def complete2D(self, return_data): 2417 """ 2418 Plot the current 2D data 2419 """ 2420 fitted_data = self.logic.new2DPlot(return_data) 2421 residuals = self.calculateResiduals(fitted_data) 2422 self.model_data = fitted_data 2423 new_plots = [fitted_data] 2424 if residuals is not None: 2425 new_plots.append(residuals) 2426 2340 2427 # Update/generate plots 2341 2428 for plot in new_plots: 2342 2429 self.communicate.plotUpdateSignal.emit([plot]) 2343 2344 def complete2D(self, return_data):2345 """2346 Plot the current 2D data2347 """2348 fitted_data = self.logic.new2DPlot(return_data)2349 self.calculateResiduals(fitted_data)2350 self.model_data = fitted_data2351 2430 2352 2431 def calculateResiduals(self, fitted_data): … … 2479 2558 _, min, max = self.kernel_module.details[param_name] 2480 2559 2560 # Update local param dict 2561 self.poly_params[param_name + '.width'] = width 2562 self.poly_params[param_name + '.npts'] = npts 2563 self.poly_params[param_name + '.nsigmas'] = nsigs 2564 2481 2565 # Construct a row with polydisp. related variable. 2482 2566 # This will get added to the polydisp. model … … 2526 2610 def updateFunctionCaption(row): 2527 2611 # Utility function for update of polydispersity function name in the main model 2612 if not self.isCheckable(row): 2613 return 2614 self._model_model.blockSignals(True) 2528 2615 param_name = str(self._model_model.item(row, 0).text()) 2616 self._model_model.blockSignals(False) 2529 2617 if param_name != param.name: 2530 2618 return 2531 2619 # Modify the param value 2620 self._model_model.blockSignals(True) 2532 2621 if self.has_error_column: 2533 2622 # err column changes the indexing … … 2535 2624 else: 2536 2625 self._model_model.item(row, 0).child(0).child(0,4).setText(combo_string) 2626 self._model_model.blockSignals(False) 2537 2627 2538 2628 if combo_string == 'array': … … 2653 2743 param.units] 2654 2744 2745 self.magnet_params[param.name] = param.default 2746 2655 2747 FittingUtilities.addCheckedListToModel(model, checked_list) 2656 2748 … … 2692 2784 2693 2785 self.lstParams.setIndexWidget(shell_index, func) 2694 self._ last_model_row = self._model_model.rowCount()2786 self._n_shells_row = shell_row - 1 2695 2787 2696 2788 # Set the index to the state-kept value … … 2703 2795 """ 2704 2796 # Find row location of the combobox 2705 last_row = self._last_model_row2706 remove_rows = self._ model_model.rowCount() - last_row2797 first_row = self._n_shells_row + 1 2798 remove_rows = self._num_shell_params 2707 2799 2708 2800 if remove_rows > 1: 2709 self._model_model.removeRows( last_row, remove_rows)2710 2711 FittingUtilities.addShellsToModel(2801 self._model_model.removeRows(first_row, remove_rows) 2802 2803 new_rows = FittingUtilities.addShellsToModel( 2712 2804 self.model_parameters, 2713 2805 self._model_model, 2714 2806 index, 2807 first_row, 2715 2808 self.lstParams) 2716 2809 2810 self._num_shell_params = len(new_rows) 2717 2811 self.current_shell_displayed = index 2718 2812
Note: See TracChangeset
for help on using the changeset viewer.