Changeset 116dd4c1 in sasview for src/sas/qtgui/Perspectives
- Timestamp:
- Jan 24, 2018 10:07:08 AM (7 years ago)
- Branches:
- ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- c6343a5
- Parents:
- eb1a386
- Location:
- src/sas/qtgui/Perspectives/Fitting
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/ConstraintWidget.py
reb1a386 r116dd4c1 2 2 import sys 3 3 4 from twisted.internet import threads 5 4 6 import sas.qtgui.Utilities.GuiUtils as GuiUtils 5 7 from PyQt5 import QtGui, QtCore, QtWidgets 6 8 9 from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit 10 7 11 import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary 8 9 12 from sas.qtgui.Perspectives.Fitting.UI.ConstraintWidgetUI import Ui_ConstraintWidgetUI 10 13 from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget 14 from sas.qtgui.Perspectives.Fitting.FitThread import FitThread 15 from sas.qtgui.Perspectives.Fitting.ConsoleUpdate import ConsoleUpdate 11 16 from sas.qtgui.Perspectives.Fitting.ComplexConstraint import ComplexConstraint 12 17 from sas.qtgui.Perspectives.Fitting.Constraints import Constraint … … 17 22 """ 18 23 19 def __init__(self, parent=None ):24 def __init__(self, parent=None, tab_id=1): 20 25 super(ConstraintWidget, self).__init__() 21 26 self.parent = parent 22 27 self.setupUi(self) 23 28 self.currentType = "FitPage" 29 self.tab_id = tab_id 30 # Page id for fitting 31 # To keep with previous SasView values, use 300 as the start offset 32 self.page_id = 300 + self.tab_id 24 33 25 34 # Remember previous content of modified cell … … 114 123 Perform the constrained/simultaneous fit 115 124 """ 116 pass 125 # Find out all tabs to fit 126 tabs_to_fit = [tab for tab in self.tabs_for_fitting if self.tabs_for_fitting[tab]] 127 128 # Single fitter for the simultaneous run 129 sim_fitter = Fit() 130 sim_fitter.fitter_id = self.page_id 131 132 # prepare fitting problems for each tab 133 # 134 page_ids = [] 135 fitter_id = 0 136 sim_fitter=[sim_fitter] 137 for tab in tabs_to_fit: 138 tab_object = ObjectLibrary.getObject(tab) 139 sim_fitter, fitter_id = tab_object.prepareFitters(fitter=sim_fitter[0], fit_id=fitter_id) 140 page_ids.append([tab_object.page_id]) 141 142 # Create the fitting thread, based on the fitter 143 completefn = self.onBatchFitComplete if self.currentType=='BatchPage' else self.onFitComplete 144 145 #if USING_TWISTED: 146 handler = None 147 updater = None 148 #else: 149 # handler = ConsoleUpdate(parent=self.parent, 150 # manager=self, 151 # improvement_delta=0.1) 152 # updater = handler.update_fit 153 154 batch_inputs = {} 155 batch_outputs = {} 156 157 # new fit thread object 158 calc_fit = FitThread(handler=handler, 159 fn=sim_fitter, 160 batch_inputs=batch_inputs, 161 batch_outputs=batch_outputs, 162 page_id=page_ids, 163 updatefn=updater, 164 completefn=completefn) 165 166 #if USING_TWISTED: 167 # start the trhrhread with twisted 168 calc_thread = threads.deferToThread(calc_fit.compute) 169 calc_thread.addCallback(self.onFitComplete) 170 calc_thread.addErrback(self.onFitFailed) 171 #else: 172 # # Use the old python threads + Queue 173 # calc_fit.queue() 174 # calc_fit.ready(2.5) 175 176 177 #disable the Fit button 178 self.cmdFit.setText('Running...') 179 self.parent.communicate.statusBarUpdateSignal.emit('Fitting started...') 180 self.cmdFit.setEnabled(False) 117 181 118 182 def onHelp(self): … … 172 236 def onConstraintChange(self, row, column): 173 237 """ 174 Modify the constraint in-place.238 Modify the constraint's "active" instance variable. 175 239 """ 176 240 item = self.tblConstraints.item(row, column) … … 208 272 pass 209 273 274 def onFitComplete(self, result): 275 """ 276 Respond to the successful fit complete signal 277 """ 278 pass 279 280 def onBatchFitComplete(self, result): 281 """ 282 Respond to the successful batch fit complete signal 283 """ 284 pass 285 286 def onFitFailed(self, reason): 287 """ 288 """ 289 print("FIT FAILED: ", reason) 290 pass 291 210 292 def isTabImportable(self, tab): 211 293 """ -
src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py
rbe8f4b0 r116dd4c1 124 124 Add a new C&S fitting tab 125 125 """ 126 tab = ConstraintWidget(parent=self )126 tab = ConstraintWidget(parent=self, tab_id=self.maxCSIndex) 127 127 # Add this tab to the object library so it can be retrieved by scripting/jupyter 128 128 tab_name = self.getCSTabName() # TODO update the tab name scheme -
src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
rba01ad1 r116dd4c1 212 212 self.orig_poly_index = 3 213 213 214 # Page id for fitting 215 # To keep with previous SasView values, use 200 as the start offset 216 self.page_id = 200 + self.tab_id 217 214 218 # Data for chosen model 215 219 self.model_data = None … … 732 736 return True 733 737 return False 734 #return True if (item.hasChildren() and isinstance(item.child(0).data(), Constraint)) else False 738 739 def rowHasActiveConstraint(self, row): 740 """ 741 Finds out if row of the main model has an active constraint child 742 """ 743 item = self._model_model.item(row,1) 744 if item.hasChildren(): 745 c = item.child(0).data() 746 if isinstance(c, Constraint) and c.func and c.active: 747 return True 748 return False 735 749 736 750 def selectParameters(self): … … 780 794 #preamble(s) +self._model_model.item(s, 1).child(0).data().func) 781 795 self._model_model.item(s, 1).child(0).data().func) 782 for s in range(param_number) if self.rowHas Constraint(s)]796 for s in range(param_number) if self.rowHasActiveConstraint(s)] 783 797 return params 784 798 … … 1056 1070 Perform fitting on the current data 1057 1071 """ 1058 1059 # Data going in 1060 data = self.logic.data 1061 model = self.kernel_module 1062 qmin = self.q_range_min 1063 qmax = self.q_range_max 1064 params_to_fit = self.parameters_to_fit 1065 1066 # Potential weights added directly to data 1067 self.addWeightingToData(data) 1068 1069 # Potential smearing added 1070 # Remember that smearing_min/max can be None -> 1071 # deal with it until Python gets discriminated unions 1072 smearing, accuracy, smearing_min, smearing_max = self.smearing_widget.state() 1073 1074 # These should be updating somehow? 1072 # initialize fitter constants 1075 1073 fit_id = 0 1076 constraints = self.getConstraintsForModel()1077 smearer = None1078 page_id = [210]1079 1074 handler = None 1080 1075 batch_inputs = {} 1081 1076 batch_outputs = {} 1082 list_page_id = [page_id]1083 1077 #--------------------------------- 1084 1078 if USING_TWISTED: … … 1091 1085 updater = handler.update_fit 1092 1086 1093 # Parameterize the fitter 1094 fitters = [] 1095 for fit_index in self.all_data: 1096 fitter = Fit() 1097 data = GuiUtils.dataFromItem(fit_index) 1098 try: 1099 fitter.set_model(model, fit_id, params_to_fit, data=data, 1100 constraints=constraints) 1101 except ValueError as ex: 1102 logging.error("Setting model parameters failed with: %s" % ex) 1103 return 1104 1105 qmin, qmax, _ = self.logic.computeRangeFromData(data) 1106 fitter.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin, 1107 qmax=qmax) 1108 fitter.select_problem_for_fit(id=fit_id, value=1) 1109 fitter.fitter_id = page_id 1110 fit_id += 1 1111 fitters.append(fitter) 1087 # Prepare the fitter object 1088 fitters, _ = self.prepareFitters() 1112 1089 1113 1090 # Create the fitting thread, based on the fitter … … 1115 1092 1116 1093 calc_fit = FitThread(handler=handler, 1117 1118 1119 1120 page_id=list_page_id,1121 1122 1094 fn=fitters, 1095 batch_inputs=batch_inputs, 1096 batch_outputs=batch_outputs, 1097 page_id=[[self.page_id]], 1098 updatefn=updater, 1099 completefn=completefn) 1123 1100 1124 1101 if USING_TWISTED: … … 1209 1186 chi2_repr = GuiUtils.formatNumber(self.chi2, high=True) 1210 1187 self.lblChi2Value.setText(chi2_repr) 1188 1189 def prepareFitters(self, fitter=None, fit_id=0): 1190 """ 1191 Prepare the Fitter object for use in fitting 1192 """ 1193 # fitter = None -> single/batch fitting 1194 # fitter = Fit() -> simultaneous fitting 1195 1196 # Data going in 1197 data = self.logic.data 1198 model = self.kernel_module 1199 qmin = self.q_range_min 1200 qmax = self.q_range_max 1201 params_to_fit = self.parameters_to_fit 1202 1203 # Potential weights added directly to data 1204 self.addWeightingToData(data) 1205 1206 # Potential smearing added 1207 # Remember that smearing_min/max can be None -> 1208 # deal with it until Python gets discriminated unions 1209 smearing, accuracy, smearing_min, smearing_max = self.smearing_widget.state() 1210 1211 constraints = self.getConstraintsForModel() 1212 smearer = None 1213 handler = None 1214 batch_inputs = {} 1215 batch_outputs = {} 1216 1217 fitters = [] 1218 for fit_index in self.all_data: 1219 fitter_single = Fit() if fitter is None else fitter 1220 data = GuiUtils.dataFromItem(fit_index) 1221 try: 1222 fitter_single.set_model(model, fit_id, params_to_fit, data=data, 1223 constraints=constraints) 1224 except ValueError as ex: 1225 logging.error("Setting model parameters failed with: %s" % ex) 1226 return 1227 1228 qmin, qmax, _ = self.logic.computeRangeFromData(data) 1229 fitter_single.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin, 1230 qmax=qmax) 1231 fitter_single.select_problem_for_fit(id=fit_id, value=1) 1232 if fitter is None: 1233 # Assign id to the new fitter only 1234 fitter_single.fitter_id = [self.page_id] 1235 fit_id += 1 1236 fitters.append(fitter_single) 1237 1238 return fitters, fit_id 1211 1239 1212 1240 def iterateOverModel(self, func):
Note: See TracChangeset
for help on using the changeset viewer.