Changeset 92a5ac92 in sasview for fittingview
- Timestamp:
- Oct 11, 2011 7:28:20 PM (13 years ago)
- Branches:
- master, 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, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- d8661fb
- Parents:
- 9c1af44
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fittingview/src/sans/perspectives/fitting/fitting.py
rd7345b71 r92a5ac92 47 47 48 48 (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() 49 (BatchDrawEvent, EVT_BATCH_DRAW) = wx.lib.newevent.NewEvent() 49 50 50 51 51 if sys.platform.count("darwin")==0: … … 270 270 self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) 271 271 self.parent.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) 272 self.parent.Bind(EVT_BATCH_DRAW, self.on_display_grid)273 272 self.parent._mgr.Bind(wx.aui.EVT_AUI_PANE_CLOSE,self._onclearslicer) 274 273 #Create reader when fitting panel are created … … 809 808 except: 810 809 batch_on = False 810 811 811 # batch fit 812 812 if batch_on: … … 817 817 batch_outputs=batch_outputs, 818 818 page_id=list_page_id, 819 completefn=self._ single_fit_completed,819 completefn=self._batch_fit_complete, 820 820 ftol=self.ftol) 821 # single fit: not batch and not simul fit822 elif is_single_fit:823 if self._fit_engine == "park":824 calc_fit = FitThread(handler = handler,825 fn=fitter_list,826 pars=pars,827 batch_inputs=batch_inputs,828 batch_outputs=batch_outputs,829 page_id=list_page_id,830 completefn=self._fit_completed,831 ftol=self.ftol)832 else:833 calc_fit = FitThread(handler = handler,834 fn=fitter_list,835 pars=pars,836 batch_inputs=batch_inputs,837 batch_outputs=batch_outputs,838 page_id=list_page_id,839 completefn=self._fit_completed,840 ftol=self.ftol)841 842 # simul fit843 821 else: 844 current_page_id = self.sim_page.uid 822 # single fit: not batch and not simul fit 823 if not is_single_fit: 824 current_page_id = self.sim_page.uid 845 825 ## Perform more than 1 fit at the time 846 826 calc_fit = FitThread(handler=handler, … … 1067 1047 1068 1048 1069 def _batch_ single_fit_complete_helper(self, result, pars, page_id,1049 def _batch_fit_complete(self, result, pars, page_id, 1070 1050 batch_outputs, batch_inputs, elapsed=None): 1071 1051 """ … … 1076 1056 :param elapsed: time spent at the fitting level 1077 1057 """ 1058 self._mac_sleep(0.2) 1059 uid = page_id[0] 1060 if uid in self.fit_thread_list.keys(): 1061 del self.fit_thread_list[uid] 1062 1078 1063 self._update_fit_button(page_id) 1079 1064 msg = "Single Fitting complete " … … 1092 1077 batch_inputs["error on %s" % pars[index]] = [] 1093 1078 msg = "" 1094 for res in result: 1095 model, data = res.inputs[0] 1096 if res.fitness is None or \ 1097 not numpy.isfinite(res.fitness) or \ 1098 numpy.any(res.pvec == None) or not \ 1099 numpy.all(numpy.isfinite(res.pvec)): 1100 data_name = str(None) 1101 if data is not None: 1102 data_name = str(data.name) 1103 model_name = str(None) 1104 if model is not None: 1105 model_name = str(model.name) 1106 msg += "Data %s and Model %s did not fit.\n" % (data_name, 1107 model_name) 1108 print msg 1109 wx.PostEvent(self.parent, StatusEvent(status=msg, 1110 error="error", 1111 type="stop")) 1112 else: 1113 #Separate result in to data corresponding to each page 1114 temp_pars = [] 1115 temp_res_param = [] 1116 # Park sorts the params by itself so that we must check 1117 # param name and resort it back as it was. No effects on Scipy. 1118 if res.parameters != None: 1119 model = cpage.model 1120 for fid in self.page_finder[pid]: 1121 if fid != None: 1122 # Below works only for batch using one model 1123 model = self.page_finder[pid][fid].get_model() 1124 break 1125 for p in res.parameters: 1126 model_name, param_name = self.split_string(p.name) 1127 if model.name == model_name: 1128 p_name= model.name+"."+param_name 1129 if p.name == p_name: 1130 temp_res_param.append(p) 1131 temp_pars.append(param_name) 1132 res.parameters = temp_res_param 1133 pars = temp_pars 1134 cell = BatchCell() 1135 cell.label = res.fitness 1136 cell.value = res.fitness 1137 batch_outputs["Chi2"].append(cell) 1138 for param in model.getParamList(): 1139 if model.is_fittable(param): 1140 for index in range(len(pars)): 1141 #replug only fitted values 1142 if param != pars[index]: 1143 batch_outputs[pars[index]].append(res.pvec[index]) 1144 item = res.stderr[index] 1145 batch_inputs["error on %s" % pars[index]].append(item) 1146 if pars[index] in model.getParamList(): 1147 model.setParam(pars[index], res.pvec[index]) 1148 else: 1149 batch_outputs[str(param)].append(model.getParam(param)) 1150 1079 for list_res in result: 1080 for res in list_res: 1081 model, data = res.inputs[0] 1082 if model is not None and hasattr(model, "model"): 1083 model = model.model 1084 if data is not None and hasattr(data, "sans_data"): 1085 data = data.sans_data 1086 if res.fitness is None or \ 1087 not numpy.isfinite(res.fitness) or \ 1088 numpy.any(res.pvec == None) or not \ 1089 numpy.all(numpy.isfinite(res.pvec)): 1090 data_name = str(None) 1091 if data is not None: 1092 data_name = str(data.name) 1093 model_name = str(None) 1094 if model is not None: 1095 model_name = str(model.name) 1096 msg += "Data %s and Model %s did not fit.\n" % (data_name, 1097 model_name) 1098 print msg 1099 wx.PostEvent(self.parent, StatusEvent(status=msg, 1100 error="error", 1101 type="stop")) 1102 else: 1103 #Separate result in to data corresponding to each page 1104 temp_pars = [] 1105 temp_res_param = [] 1106 # Park sorts the params by itself so that we must check 1107 # param name and resort it back as it was. No effects on Scipy. 1108 if res.parameters != None: 1109 model = cpage.model 1110 for fid in self.page_finder[pid]: 1111 if fid != None: 1112 # Below works only for batch using one model 1113 model = self.page_finder[pid][fid].get_model() 1114 break 1115 for p in res.parameters: 1116 model_name, param_name = self.split_string(p.name) 1117 if model.name == model_name: 1118 p_name= model.name+"."+param_name 1119 if p.name == p_name: 1120 temp_res_param.append(p) 1121 temp_pars.append(param_name) 1122 res.parameters = temp_res_param 1123 pars = temp_pars 1124 cell = BatchCell() 1125 cell.label = res.fitness 1126 cell.value = res.fitness 1127 batch_outputs["Chi2"].append(cell) 1128 for param in model.getParamList(): 1129 if model.is_fittable(param): 1130 for index in range(len(pars)): 1131 #replug only fitted values 1132 if param != pars[index]: 1133 batch_outputs[pars[index]].append(res.pvec[index]) 1134 item = res.stderr[index] 1135 batch_inputs["error on %s" % pars[index]].append(item) 1136 if pars[index] in model.getParamList(): 1137 model.setParam(pars[index], res.pvec[index]) 1138 else: 1139 batch_outputs[str(param)].append(model.getParam(param)) 1140 1151 1141 self.page_finder[pid].set_batch_result(batch_inputs=batch_inputs, 1152 batch_outputs=batch_outputs)1153 cpage = self.fit_panel.get_page_by_id(pid)1154 cpage._on_fit_complete()1155 self.page_finder[pid][data.id].set_result(res)1156 fitproblem = self.page_finder[pid][data.id]1142 batch_outputs=batch_outputs) 1143 cpage = self.fit_panel.get_page_by_id(pid) 1144 cpage._on_fit_complete() 1145 self.page_finder[pid][data.id].set_result(res) 1146 fitproblem = self.page_finder[pid][data.id] 1157 1147 1158 from sans.models.qsmearing import smear_selection 1159 smearer = smear_selection(data, model) 1160 qmin, qmax = fitproblem.get_range() 1161 weight = fitproblem.get_weight() 1162 flag = issubclass(data.__class__, Data2D) 1163 """ 1164 self.draw_model(model=model, 1165 page_id=pid, 1166 data=data, 1167 smearer=smearer, 1168 enable1D=not flag, 1169 enable2D=flag, 1170 state=None, 1171 toggle_mode_on=False, 1172 fid=data.id, 1173 qmin=qmin, qmax=qmax, 1174 update_chisqr=False, 1175 weight=weight, 1176 source='fit') 1177 """ 1178 1148 qmin, qmax = fitproblem.get_range() 1149 flag = issubclass(data.__class__, Data2D) 1150 if not flag: 1151 self._complete1D(x=data.x, y=res.theory, page_id=pid, 1152 elapsed=None, 1153 index=res.index, model=model, 1154 weight=None, fid=data.id, 1155 toggle_mode_on=False, state=None, 1156 data=data, update_chisqr=False, 1157 source='model') 1158 else: 1159 self._complete2D(image=data.data, data=data, 1160 model=model, 1161 page_id=pid, elapsed=None, 1162 index=res.index, 1163 qmin=qmin, 1164 qmax=qmax, fid=data.id, weight=None, 1165 toggle_mode_on=False, state=None, 1166 update_chisqr=False, 1167 source='model') 1168 1169 self.on_set_batch_result(page_id=pid, 1170 fid=data.id, 1171 batch_outputs=batch_outputs, 1172 batch_inputs=batch_inputs) 1173 1174 wx.CallAfter(self.parent.on_set_batch_result,batch_outputs, 1175 batch_inputs, 1176 self.sub_menu) 1177 1179 1178 def on_set_batch_result(self, page_id, fid, batch_outputs, batch_inputs): 1180 1179 """ … … 1214 1213 batch_outputs["Chi2"][index].object = [residuals] 1215 1214 1216 def old_single_fit_completed(self, result, pars, page_id, batch_outputs, 1217 batch_inputs=None, elapsed=None): 1218 """ 1219 Display fit result on one page of the notebook. 1220 :param result: list of object generated when fit ends 1221 :param pars: list of names of parameters fitted 1222 :param page_id: list of page ids which called fit function 1223 :param elapsed: time spent at the fitting level 1224 """ 1225 self._mac_sleep(0.2) 1226 uid = page_id[0] 1227 if uid in self.fit_thread_list.keys(): 1228 del self.fit_thread_list[uid] 1229 1230 cpage = self.fit_panel.get_page_by_id(uid) 1231 if cpage.batch_on: 1232 wx.CallAfter(self._batch_single_fit_complete_helper, 1233 result, pars, page_id, batch_outputs, 1234 batch_inputs, elapsed) 1235 return 1236 else: 1237 try: 1238 result = result[0] 1239 if result == None: 1240 self._update_fit_button(page_id) 1241 msg= "Single Fitting did not converge!!!" 1242 wx.PostEvent(self.parent, 1243 StatusEvent(status=msg, 1244 info="warning", 1245 type="stop")) 1246 return 1247 if result.fitness is not None or \ 1248 not numpy.isfinite(result.fitness) or \ 1249 numpy.any(result.pvec == None) or \ 1250 not numpy.all(numpy.isfinite(result.pvec)): 1251 msg = "Single Fitting did not converge!!!" 1252 wx.PostEvent(self.parent, 1253 StatusEvent(status=msg, 1254 info="warning", 1255 type="stop")) 1256 self._update_fit_button(page_id) 1257 return 1258 1259 for uid in page_id: 1260 cpage = self.fit_panel.get_page_by_id(uid) 1261 # Make sure we got all results 1262 #(CallAfter is important to MAC) 1263 wx.CallAfter(cpage.onsetValues, result.fitness, pars, 1264 result.pvec, result.stderr) 1265 cpage._on_fit_complete() 1266 if result.stderr == None: 1267 msg = "Fit Abort: " 1268 else: 1269 msg = "Fitting: " 1270 msg += "Completed!!!" 1271 wx.PostEvent(self.parent, StatusEvent(status=msg)) 1272 except ValueError: 1273 raise 1274 self._update_fit_button(page_id) 1275 msg = "Single Fitting did not converge!!!" 1276 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", 1277 type="stop")) 1278 except: 1279 raise 1280 self._update_fit_button(page_id) 1281 msg = "Single Fit completed but Following" 1282 msg += " error occurred:%s" % sys.exc_value 1283 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", 1284 type="stop")) 1285 raise 1286 1215 1287 1216 def _fit_completed(self, result, page_id, batch_outputs, 1288 1217 batch_inputs=None, … … 1513 1442 caption = current_pg.window_caption 1514 1443 self.page_finder[page_id].set_fit_tab_caption(caption=caption) 1515 try: 1516 if source == 'fit': 1517 # replace model cal to fit calculation if possible 1518 new_plot.y = self.page_finder[page_id].get_result(fid=data.id).theory 1519 except: 1520 pass 1521 1444 1522 1445 self.page_finder[page_id].set_theory_data(data=new_plot, 1523 1446 fid=data.id) … … 1598 1521 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 1599 1522 title=title)) 1600 else:1601 try:1602 # replace model cal to fit calculation if possible1603 new_plot.data = self.page_finder[page_id].get_result(fid=data.id).theory1604 except:1605 pass1606 1607 1523 self.page_finder[page_id].set_theory_data(data=new_plot, fid=data.id) 1608 1524 if toggle_mode_on: … … 1862 1778 if not batch_on: 1863 1779 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) 1864 return 1865 #reset weight 1866 #self.weight = None 1867 # Need all residuals before plotting 1868 # Should be refactored 1869 n = len(self.page_finder[page_id].keys()) 1870 m = self.page_finder[page_id].nbr_residuals_computed 1871 flag = False 1872 batch_inputs, batch_outputs = self.page_finder[page_id].get_batch_result() 1873 1874 if self.page_finder[page_id].nbr_residuals_computed == -1: 1875 flag = False 1876 else: 1877 if m == n -1: 1878 flag = True 1879 else: 1880 flag = False 1881 self.page_finder[page_id].nbr_residuals_computed += 1 1882 1883 self.on_set_batch_result(page_id=page_id, 1884 fid=fid, 1885 batch_outputs=batch_outputs, 1886 batch_inputs=batch_inputs) 1887 # plot data 1888 event = BatchDrawEvent(page_id=page_id , 1889 batch_on=batch_on, 1890 fid=fid, 1891 batch_outputs=batch_outputs, 1892 batch_inputs=batch_inputs, 1893 is_displayed=flag) 1894 wx.PostEvent(self.parent, event) 1895 1896 1897 1898 def on_display_grid(self, event): 1899 """ 1900 deplay batch result 1901 """ 1902 fid = event.fid 1903 page_id = event.page_id 1904 batch_on = event.batch_on 1905 flag = event.is_displayed 1906 batch_outputs = event.batch_outputs 1907 batch_inputs = event.batch_inputs 1908 if flag and batch_on and fid is not None: 1909 self.parent.on_set_batch_result(data_outputs=batch_outputs, 1910 data_inputs=batch_inputs, 1911 plugin_name=self.sub_menu) 1912 self.page_finder[page_id].nbr_residuals_computed = -1 1780 1913 1781 1914 1782 #def profile(fn, *args, **kw):
Note: See TracChangeset
for help on using the changeset viewer.