Changeset 233c121 in sasview for src/sans/perspectives/fitting/fitting.py
- Timestamp:
- May 21, 2014 7:19:00 AM (11 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:
- bf5e985
- Parents:
- 85a3b46 (diff), 099c355 (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/sans/perspectives/fitting/fitting.py
reff93b8 r233c121 86 86 self.elapsed = 0.022 87 87 self.fit_panel = None 88 #let fit ready89 self.fitproblem_count = None90 88 #Flag to let the plug-in know that it is running stand alone 91 89 self.standalone = True … … 983 981 :param uid: id related to the panel currently calling this fit function. 984 982 """ 985 flag = True 986 ## count the number of fitproblem schedule to fit 987 fitproblem_count = 0 988 for value in self.page_finder.values(): 989 if value.get_scheduled() == 1: 990 fitproblem_count += 1 983 if uid is None: raise RuntimeError("no page to fit") # Should never happen 984 991 985 # Remember the user selected fit engine before the fit. Simultaneous 992 986 # fitting may change the selected engine, so it needs to be restored 993 987 # when the fit is complete. 994 988 self._gui_engine = self._fit_engine 995 self.fitproblem_count = fitproblem_count 996 if self._fit_engine in ("park","bumps"): 997 engineType = "Simultaneous Fit" 989 990 sim_page_uid = getattr(self.sim_page, 'uid', None) 991 batch_page_uid = getattr(self.batch_page, 'uid', None) 992 993 if uid == sim_page_uid: 994 fit_type = 'simultaneous' 995 elif uid == batch_page_uid: 996 fit_type = 'combined_batch' 998 997 else: 999 engineType = "Single Fit" 998 fit_type = 'single' 999 1000 # if constrained fit, don't use scipy leastsq directly 1001 if fit_type == 'simultaneous': 1002 if self._fit_engine not in ("park","bumps"): 1003 self._on_change_engine(engine='bumps') 1004 1005 1000 1006 fitter_list = [] 1001 1007 sim_fitter = None 1002 is_single_fit = True 1003 batch_on = False 1004 if self.sim_page is not None and self.sim_page.uid == uid: 1008 if fit_type == 'simultaneous': 1005 1009 #simulatanous fit only one engine need to be created 1006 ## if simultaneous fit change automatically the engine to park1007 if self._fit_engine not in ("park","bumps"):1008 self._on_change_engine(engine='park')1009 1010 sim_fitter = Fit(self._fit_engine) 1010 1011 sim_fitter.fitter_id = self.sim_page.uid 1011 1012 fitter_list.append(sim_fitter) 1012 is_single_fit = False 1013 batch_on = self.sim_page.batch_on 1014 1015 self.fitproblem_count = fitproblem_count 1016 if self._fit_engine in ("park","bumps"): 1017 engineType = "Simultaneous Fit" 1018 else: 1019 engineType = "Single Fit" 1020 1013 1021 1014 self.current_pg = None 1022 1015 list_page_id = [] 1023 1016 fit_id = 0 1024 batch_inputs = {} 1025 batch_outputs = {} 1026 for page_id, value in self.page_finder.iteritems(): 1017 for page_id, page_info in self.page_finder.iteritems(): 1027 1018 # For simulfit (uid give with None), do for-loop 1028 1019 # if uid is specified (singlefit), do it only on the page. 1029 if engineType == "Single Fit": 1030 #combine more than 1 batch page on single mode 1031 if self.batch_page is None or self.batch_page.uid != uid: 1032 if page_id != uid: 1033 continue 1020 if page_id in (sim_page_uid, batch_page_uid): continue 1021 if fit_type == "single" and page_id != uid: continue 1022 1034 1023 try: 1035 if value.get_scheduled() == 1: 1036 value.nbr_residuals_computed = 0 1037 #Get list of parameters name to fit 1038 pars = [] 1039 templist = [] 1024 if page_info.get_scheduled() == 1: 1025 page_info.nbr_residuals_computed = 0 1040 1026 page = self.fit_panel.get_page_by_id(page_id) 1041 1027 self.set_fit_weight(uid=page.uid, 1042 1028 flag=page.get_weight_flag(), 1043 1029 is2d=page._is_2D()) 1044 templist = page.get_param_list() 1045 flag = page._update_paramv_on_fit() 1046 if not flag: 1030 if not page.param_toFit: 1031 msg = "No fitting parameters for %s"%page.window_caption 1032 wx.PostEvent(page.parent.parent, 1033 StatusEvent(status=msg, info="error", 1034 type="stop")) 1035 return False 1036 if not page._update_paramv_on_fit(): 1047 1037 msg = "Fitting range or parameter values are" 1048 1038 msg += " invalid in %s" % \ … … 1051 1041 StatusEvent(status=msg, info="error", 1052 1042 type="stop")) 1053 return flag 1054 for element in templist: 1055 name = str(element[1]) 1056 pars.append(name) 1057 fitproblem_list = value.values() 1043 return False 1044 1045 pars = [str(element[1]) for element in page.param_toFit] 1046 fitproblem_list = page_info.values() 1058 1047 for fitproblem in fitproblem_list: 1059 1048 if sim_fitter is None: 1060 1049 fitter = Fit(self._fit_engine) 1061 1050 fitter.fitter_id = page_id 1062 self._fit_helper(fitproblem=fitproblem,1063 pars=pars,1064 fitter=fitter,1065 fit_id=fit_id,1066 batch_inputs=batch_inputs,1067 batch_outputs=batch_outputs)1068 1051 fitter_list.append(fitter) 1069 1052 else: 1070 1053 fitter = sim_fitter 1071 self._fit_helper(fitproblem=fitproblem,1054 self._add_problem_to_fit(fitproblem=fitproblem, 1072 1055 pars=pars, 1073 1056 fitter=fitter, 1074 fit_id=fit_id, 1075 batch_inputs=batch_inputs, 1076 batch_outputs=batch_outputs) 1057 fit_id=fit_id) 1077 1058 fit_id += 1 1078 1059 list_page_id.append(page_id) 1079 current_page_id = page_id 1080 value.clear_model_param() 1060 page_info.clear_model_param() 1081 1061 except KeyboardInterrupt: 1082 flag = True1083 1062 msg = "Fitting terminated" 1084 1063 wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", 1085 1064 type="stop")) 1086 return flag1065 return True 1087 1066 except: 1088 flag = False 1089 msg = "%s error: %s" % (engineType, sys.exc_value) 1067 msg = "Fitting error: %s" % str(sys.exc_value) 1090 1068 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", 1091 1069 type="stop")) 1092 return flag1070 return False 1093 1071 ## If a thread is already started, stop it 1094 1072 #if self.calc_fit!= None and self.calc_fit.isrunning(): … … 1102 1080 improvement_delta=0.1) 1103 1081 self._mac_sleep(0.2) 1104 ## perform single fit1105 try:1106 page = self.fit_panel.get_page_by_id(uid)1107 batch_on = page.batch_on1108 except:1109 try:1110 #if the id cannot be found then we deal with a self.sim_page1111 #or a self.batch_page1112 if self.sim_page is not None and uid == self.sim_page.uid:1113 batch_on = self.sim_page.batch_on1114 if self.batch_page is not None and uid == self.batch_page.uid:1115 batch_on = self.batch_page.batch_on1116 except:1117 batch_on = False1118 1082 1119 1083 # batch fit 1120 if batch_on: 1084 batch_inputs = {} 1085 batch_outputs = {} 1086 page = self.fit_panel.get_page_by_id(uid) 1087 if page.batch_on: 1121 1088 calc_fit = FitThread(handler=handler, 1122 1089 fn=fitter_list, … … 1129 1096 reset_flag=self.batch_reset_flag) 1130 1097 else: 1131 # single fit: not batch and not simul fit1132 if not is_single_fit:1133 current_page_id = self.sim_page.uid1134 1098 ## Perform more than 1 fit at the time 1135 1099 calc_fit = FitThread(handler=handler, … … 1141 1105 completefn=self._fit_completed, 1142 1106 ftol=self.ftol) 1143 self.fit_thread_list[current_page_id] = calc_fit 1107 #self.fit_thread_list[current_page_id] = calc_fit 1108 self.fit_thread_list[uid] = calc_fit 1144 1109 calc_fit.queue() 1110 calc_fit.ready(2.5) 1145 1111 msg = "Fitting is in progress..." 1146 1112 wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) 1147 1113 1148 self.ready_fit(calc_fit=calc_fit) 1149 return flag 1150 1151 def ready_fit(self, calc_fit): 1152 """ 1153 Ready for another fit 1154 """ 1155 if self.fitproblem_count != None and self.fitproblem_count > 1: 1156 calc_fit.ready(2.5) 1157 else: 1158 time.sleep(0.4) 1159 1114 return True 1115 1160 1116 def remove_plot(self, uid, fid=None, theory=False): 1161 1117 """ … … 1297 1253 self.page_finder[uid].schedule_tofit(value) 1298 1254 1299 def _fit_helper(self, fitproblem, pars, fitter, fit_id, 1300 batch_inputs, batch_outputs): 1255 def _add_problem_to_fit(self, fitproblem, pars, fitter, fit_id): 1301 1256 """ 1302 1257 Create and set fit engine with series of data and model
Note: See TracChangeset
for help on using the changeset viewer.