Changeset a0da535 in sasview for sansview/perspectives/fitting
- Timestamp:
- Jan 19, 2011 6:10:35 PM (14 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:
- c91c29e
- Parents:
- 1d78e4b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitting.py
r3658abed ra0da535 12 12 13 13 14 import 14 import re 15 15 import sys 16 16 import wx … … 27 27 28 28 from DataLoader.loader import Loader 29 30 29 from sans.guiframe.dataFitting import Data2D 31 30 from sans.guiframe.dataFitting import Data1D 32 31 from sans.guiframe.dataFitting import Theory1D 33 34 from sans.guicomm.events import NewPlotEvent, StatusEvent 35 from sans.guicomm.events import EVT_SLICER_PANEL,ERR_DATA,EVT_REMOVE_DATA 36 from sans.guicomm.events import EVT_SLICER_PARS_UPDATE 37 from sans.guicomm.events import EVT_FITSTATE_UPDATE 32 from sans.guiframe.events import NewPlotEvent, StatusEvent 33 from sans.guiframe.events import EVT_SLICER_PANEL 34 from sans.guiframe.events import EVT_REMOVE_DATA 35 from sans.guiframe.events import ERR_DATA 36 from sans.guiframe.events import EVT_SLICER_PARS_UPDATE 37 from sans.guiframe.events import EVT_FITSTATE_UPDATE 38 38 39 39 from sans.fit.AbstractFitEngine import Model 40 40 from sans.fit.AbstractFitEngine import FitAbort 41 from console import ConsoleUpdate42 43 from fitproblem import FitProblem44 from fitpanel import FitPanel45 from fit_thread import FitThread46 from pagestate import Reader41 from .console import ConsoleUpdate 42 from .fitproblem import FitProblem 43 from .fitpanel import FitPanel 44 from .fit_thread import FitThread 45 from .pagestate import Reader 46 from .fitpage import Chi2UpdateEvent 47 47 48 48 DEFAULT_BEAM = 0.005 … … 52 52 53 53 (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() 54 #(FitStateUpdateEvent, EVT_STATE_UPDATE) = wx.lib.newevent.NewEvent()55 from fitpage import Chi2UpdateEvent56 54 57 55 … … 169 167 """ 170 168 Plugin.on_perspective(self,event=event) 171 if self.sim_page != None:169 if self.sim_page != None: 172 170 msg= "Simultaneous Fit page already opened" 173 171 wx.PostEvent(self.parent, StatusEvent(status= msg)) … … 180 178 Show a general help dialog. 181 179 """ 182 183 180 from help_panel import HelpWindow 184 181 frame = HelpWindow(None, -1, 'HelpWindow') … … 213 210 else: 214 211 if item.name == graph.selected_plottable : 215 if item.name in ["$I_{obs}(q)$","$I_{fit}(q)$","$P_{fit}(r)$"]: 212 if item.name in ["$I_{obs}(q)$","$I_{fit}(q)$", 213 "$P_{fit}(r)$"]: 216 214 return [] 217 215 if hasattr(item, "group_id"): … … 271 269 """ 272 270 return True 273 271 272 def set_data(self, data_list): 273 """ 274 receive a list of data to fit 275 """ 276 for data in data_list: 277 self.add_fit_page(data=data) 278 wx.PostEvent(self.parent, NewPlotEvent(plot=data, 279 title=str(data.title))) 280 281 274 282 def set_state(self, state=None, datainfo=None, format=None): 275 283 """ … … 607 615 new_plot.group_id = data.group_id 608 616 new_plot.id = data.id + name 609 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=data.name)) 617 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 618 title=data.name)) 610 619 if theory: 611 620 new_plot_data = Data1D(x=[], y=[], dx=None, dy=None) … … 700 709 else: 701 710 msg = "Page was already Created" 702 wx.PostEvent(self.parent, StatusEvent(status=msg, info="warning")) 711 wx.PostEvent(self.parent, StatusEvent(status=msg, 712 info="warning")) 703 713 except: 704 714 msg = "Creating Fit page: %s"%sys.exc_value … … 728 738 729 739 if hasattr(event.data,"is_data"): 730 if not event.data.is_data or event.data.__class__.__name__=="Data1D": 740 if not event.data.is_data or \ 741 event.data.__class__.__name__=="Data1D": 731 742 self.fit_panel.close_page_with_data(event.data) 732 743 … … 913 924 914 925 # Display result on each page 915 page.onsetValues(result.fitness, small_param_name,small_out,small_cov) 926 page.onsetValues(result.fitness, 927 small_param_name, 928 small_out,small_cov) 916 929 except: 917 930 msg= "Simultaneous Fit completed" … … 998 1011 self.menu1.FindItemByPosition(0).Check(True) 999 1012 self.menu1.FindItemByPosition(1).Check(False) 1000 1001 1013 ## post a message to status bar 1002 wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) 1003 1004 ## Bind every open fit page with a newevent to know the current fitting engine 1005 import fitpage 1006 event= fitpage.FitterTypeEvent() 1014 msg = "Engine set to: %s" % self._fit_engine 1015 wx.PostEvent(self.parent, 1016 StatusEvent(status=msg)) 1017 ## Bind every open fit page with a newevent to 1018 #know the current fitting engine 1019 #import fitpage 1020 event = fitpage.FitterTypeEvent() 1007 1021 event.type = self._fit_engine 1008 1022 for key in self.page_finder.keys(): … … 1033 1047 ## example trying to call set_panel on self.sim_page 1034 1048 if self.current_pg != self.sim_page : 1035 if self.page_finder[self.current_pg].get_model()== None: 1036 1037 model.name = "M"+str(self.index_model) 1049 if self.page_finder[self.current_pg].get_model() is None: 1050 model.name = "M" + str(self.index_model) 1038 1051 self.index_model += 1 1039 1052 else: 1040 1053 model.name= self.page_finder[self.current_pg].get_model().name 1041 1042 1054 data = self.page_finder[self.current_pg].get_fit_data() 1043 1044 1055 # save the name containing the data name with the appropriate model 1045 1056 self.page_finder[self.current_pg].set_model(model) … … 1047 1058 self.page_finder[self.current_pg].set_range(qmin=qmin, qmax=qmax) 1048 1059 1049 if self.sim_page !=None:1060 if self.sim_page != None: 1050 1061 self.sim_page.draw_page() 1051 1062 … … 1062 1073 # will be plotted automatically. If a page already exists, 1063 1074 # the content will be updated and the plot refreshed 1064 self.fit_panel.add_model_page(model, topmenu=True)1075 self.fit_panel.add_model_page(model, topmenu=True) 1065 1076 1066 1077 def _update1D(self,x, output): … … 1068 1079 Update the output of plotting model 1D 1069 1080 """ 1070 wx.PostEvent(self.parent, StatusEvent(status="Plot \1071 #updating ... ",type="update"))1081 msg = "Plot updating ... " 1082 wx.PostEvent(self.parent, StatusEvent(status=msg,type="update")) 1072 1083 self.ready_fit() 1073 1084 #self.calc_thread.ready(0.01) … … 1101 1112 center_y = theory.detector[0].beam_center.y/pixel_width_y 1102 1113 1103 # theory default: assume the beam center is located at the center of sqr detector 1114 # theory default: assume the beam 1115 #center is located at the center of sqr detector 1104 1116 xmax = qmax 1105 1117 xmin = -qmax … … 1108 1120 1109 1121 x= numpy.linspace(start= -1*qmax, 1122 stop=qmax, 1123 num=qstep, 1124 endpoint=True) 1125 y = numpy.linspace(start=-1*qmax, 1110 1126 stop= qmax, 1111 1127 num= qstep, 1112 endpoint=True ) 1113 y = numpy.linspace(start= -1*qmax, 1114 stop= qmax, 1115 num= qstep, 1116 endpoint=True ) 1128 endpoint=True) 1117 1129 1118 1130 ## use data info instead … … 1129 1141 mask = numpy.ones(len(qx_data), dtype = bool) 1130 1142 1131 # calculate the range of qx and qy: this way, it is a little more independent 1143 # calculate the range of qx and qy: this way, 1144 # it is a little more independent 1132 1145 x_size = xmax- xmin 1133 1146 y_size = ymax -ymin … … 1150 1163 1151 1164 # max and min taking account of the bin sizes 1152 theory.xmin = xmin1153 theory.xmax = xmax1154 theory.ymin = ymin1155 theory.ymax = ymax1165 theory.xmin = xmin 1166 theory.xmax = xmax 1167 theory.ymin = ymin 1168 theory.ymax = ymax 1156 1169 theory.group_id = "Model" 1157 1170 theory.id = "Model" … … 1190 1203 new_plot.group_id = my_info.group_id 1191 1204 1192 new_plot.xaxis( 1193 new_plot.yaxis( 1194 if data !=None:1205 new_plot.xaxis(my_info._xaxis, my_info._xunit) 1206 new_plot.yaxis(my_info._yaxis, my_info._yunit) 1207 if data != None: 1195 1208 if new_plot.id == data.id: 1196 1209 new_plot.id += "Model" 1197 1210 new_plot.is_data =False 1198 1211 1199 title = new_plot.name1212 title = new_plot.name 1200 1213 # x, y are only in range of index 1201 1214 self.theory_data = new_plot … … 1203 1216 # Pass the reset flag to let the plotting event handler 1204 1217 # know that we are replacing the whole plot 1205 if title ==None:1218 if title is None: 1206 1219 title = "Analytical model 1D " 1207 if data ==None:1220 if data is None: 1208 1221 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 1209 1222 title=str(title), reset=True)) 1210 1223 else: 1211 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot,title= str(title))) 1224 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 1225 title= str(title))) 1212 1226 # Chisqr in fitpage 1213 current_pg=self.fit_panel.get_current_page() 1214 wx.PostEvent(current_pg,Chi2UpdateEvent(output=self._cal_chisqr(data=data,index=index))) 1227 current_pg = self.fit_panel.get_current_page() 1228 wx.PostEvent(current_pg, 1229 Chi2UpdateEvent(output=self._cal_chisqr(data=data, 1230 index=index))) 1215 1231 msg = "Plot 1D complete !" 1216 wx.PostEvent( self.parent, StatusEvent(status=msg 1232 wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop" )) 1217 1233 except: 1218 msg = " Error occurred when drawing %s Model 1D: "%new_plot.name1219 msg += " %s"%sys.exc_value1220 wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop"))1234 msg = " Error occurred when drawing %s Model 1D: " % new_plot.name 1235 msg += " %s" % sys.exc_value 1236 wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) 1221 1237 1222 1238 def _update2D(self, output,time=None): … … 1225 1241 """ 1226 1242 wx.PostEvent(self.parent, StatusEvent(status="Plot \ 1227 #updating ... ", type="update"))1243 #updating ... ", type="update")) 1228 1244 self.ready_fit() 1229 1245 #self.calc_thread.ready(0.01) … … 1237 1253 err_image = numpy.zeros(numpy.shape(image)) 1238 1254 1239 theory= Data2D(image= image , err_image= err_image) 1240 theory.name= model.name 1241 1242 if data ==None: 1243 self._fill_default_model2D(theory=theory, qmax=qmax, qstep=qstep, 1255 theory= Data2D(image=image, err_image=err_image) 1256 theory.name = model.name 1257 1258 if data is None: 1259 self._fill_default_model2D(theory=theory, 1260 qmax=qmax, 1261 qstep=qstep, 1244 1262 qmin= qmin) 1245 1263 1246 1264 else: 1247 theory.id = data.id+"Model"1248 theory.group_id = data.name+"Model"1249 theory.x_bins = data.x_bins1250 theory.y_bins = data.y_bins1251 theory.detector = data.detector1252 theory.source = data.source1265 theory.id = data.id + "Model" 1266 theory.group_id = data.name + "Model" 1267 theory.x_bins = data.x_bins 1268 theory.y_bins = data.y_bins 1269 theory.detector = data.detector 1270 theory.source = data.source 1253 1271 theory.is_data = False 1254 1272 theory.qx_data = data.qx_data 1255 1273 theory.qy_data = data.qy_data 1256 1274 theory.q_data = data.q_data 1257 theory.err_data = err_image#numpy.zeros(len(data.err_data))#data.err_data 1275 #numpy.zeros(len(data.err_data))#data.err_data 1276 theory.err_data = err_image 1258 1277 theory.mask = data.mask 1259 1278 ## plot boundaries 1260 theory.ymin= data.ymin 1261 theory.ymax= data.ymax 1262 theory.xmin= data.xmin 1263 theory.xmax= data.xmax 1264 1279 theory.ymin = data.ymin 1280 theory.ymax = data.ymax 1281 theory.xmin = data.xmin 1282 theory.xmax = data.xmax 1265 1283 self.theory_data = theory 1266 1284 ## plot 1267 1285 wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 1268 title="Analytical model 2D ", reset=True 1286 title="Analytical model 2D ", reset=True)) 1269 1287 # Chisqr in fitpage 1270 1288 current_pg = self.fit_panel.get_current_page() 1271 1289 wx.PostEvent(current_pg, 1272 Chi2UpdateEvent(output=self._cal_chisqr(data=data, index=index)))1290 Chi2UpdateEvent(output=self._cal_chisqr(data=data, index=index))) 1273 1291 msg = "Plot 2D complete !" 1274 wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop"))1292 wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) 1275 1293 1276 1294 def _on_data_error(self, event): … … 1281 1299 self.err_dy = event.err_dy 1282 1300 1283 def _draw_model2D(self,model,data=None, smearer= None,description=None, enable2D=False, 1284 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 1301 def _draw_model2D(self, model, data=None, smearer=None, 1302 description=None, enable2D=False, 1303 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, 1304 qstep=DEFAULT_NPTS): 1285 1305 """ 1286 1306 draw model in 2D … … 1294 1314 1295 1315 """ 1296 x= numpy.linspace(start= 1297 stop= 1298 num= 1299 endpoint=True 1316 x= numpy.linspace(start=-1*qmax, 1317 stop=qmax, 1318 num=qstep, 1319 endpoint=True) 1300 1320 y = numpy.linspace(start= -1*qmax, 1301 stop= 1302 num= 1303 endpoint=True 1321 stop=qmax, 1322 num=qstep, 1323 endpoint=True) 1304 1324 ## use data info instead 1305 if data !=None:1325 if data is not None: 1306 1326 ## check if data2D to plot 1307 1327 if hasattr(data, "x_bins"): 1308 1328 enable2D = True 1309 x = data.x_bins1310 y = data.y_bins1329 x = data.x_bins 1330 y = data.y_bins 1311 1331 1312 1332 if not enable2D: 1313 return None, None1333 return None, None 1314 1334 try: 1315 1335 from model_thread import Calc2D 1316 1336 ## If a thread is already started, stop it 1317 if self.calc_2D != Noneand self.calc_2D.isrunning():1337 if (self.calc_2D is not None) and self.calc_2D.isrunning(): 1318 1338 self.calc_2D.stop() 1319 1339 1320 self.calc_2D = Calc2D( x=x,1321 y= 1322 model= 1323 data =data,1324 smearer =smearer,1325 qmin= 1326 qmax= 1327 qstep= 1328 completefn= 1329 updatefn= self._update2D)1340 self.calc_2D = Calc2D(x=x, 1341 y=y, 1342 model=model, 1343 data=data, 1344 smearer=smearer, 1345 qmin=qmin, 1346 qmax=qmax, 1347 qstep=qstep, 1348 completefn=self._complete2D, 1349 updatefn=self._update2D) 1330 1350 self.calc_2D.queue() 1331 1351 1332 1352 except: 1333 msg = " Error occurred when drawing %s Model 2D: " % model.name1334 msg += " %s" % sys.exc_value1353 msg = " Error occurred when drawing %s Model 2D: " % model.name 1354 msg += " %s" % sys.exc_value 1335 1355 wx.PostEvent(self.parent, StatusEvent(status=msg)) 1336 1356 … … 1345 1365 1346 1366 """ 1347 x= numpy.linspace(start= 1348 stop= 1349 num= 1367 x= numpy.linspace(start=qmin, 1368 stop=qmax, 1369 num=qstep, 1350 1370 endpoint=True 1351 1371 ) 1352 if data !=None:1372 if data is not None: 1353 1373 ## check for data2D 1354 1374 if hasattr(data,"x_bins"): … … 1359 1379 if qmax == DEFAULT_QMAX: 1360 1380 qmax = max(data.x) 1361 1362 1363 1381 if not enable1D: 1364 1382 return 1365 1366 1383 try: 1367 1384 from model_thread import Calc1D 1368 1385 ## If a thread is already started, stop it 1369 if self.calc_1D!= Noneand self.calc_1D.isrunning():1386 if (self.calc_1D is not None) and self.calc_1D.isrunning(): 1370 1387 self.calc_1D.stop() 1371 self.calc_1D = Calc1D( x=x,1372 data =data,1373 model= 1374 qmin =qmin,1375 qmax =qmax,1376 smearer =smearer,1377 completefn =self._complete1D,1378 updatefn = self._update1D)1388 self.calc_1D = Calc1D(x=x, 1389 data=data, 1390 model=model, 1391 qmin=qmin, 1392 qmax=qmax, 1393 smearer=smearer, 1394 completefn=self._complete1D, 1395 updatefn=self._update1D) 1379 1396 self.calc_1D.queue() 1380 1397 1381 1398 except: 1382 msg = " Error occurred when drawing %s Model 1D: "%model.name1383 msg += " %s"%sys.exc_value1384 wx.PostEvent( self.parent, StatusEvent(status= msg))1399 msg = " Error occurred when drawing %s Model 1D: " % model.name 1400 msg += " %s" % sys.exc_value 1401 wx.PostEvent(self.parent, StatusEvent(status=msg)) 1385 1402 1386 1403 def _cal_chisqr(self, data=None, index=None): … … 1397 1414 # Get data: data I, theory I, and data dI in order 1398 1415 if data.__class__.__name__ =="Data2D": 1399 if index == None: index = numpy.ones(len(data.data),ntype=bool) 1400 index = index & (data.err_data !=0 ) # get rid of zero error points 1416 if index == None: 1417 index = numpy.ones(len(data.data),ntype=bool) 1418 # get rid of zero error points 1419 index = index & (data.err_data != 0) 1401 1420 fn = data.data[index] 1402 1421 gn = self.theory_data.data[index] … … 1404 1423 else: 1405 1424 # 1 d theory from model_thread is only in the range of index 1406 if index == None: index = numpy.ones(len(data.y),ntype=bool) 1407 if data.dy== None or data.dy ==[]: 1425 if index == None: 1426 index = numpy.ones(len(data.y), ntype=bool) 1427 if data.dy == None or data.dy == []: 1408 1428 dy = numpy.ones(len(data.y)) 1409 1429 else: 1410 ## Set consitently w/AbstractFitengine: But this should be corrected later. 1430 ## Set consitently w/AbstractFitengine: 1431 # But this should be corrected later. 1411 1432 dy = data.dy 1412 1433 dy[dy==0] = 1 … … 1414 1435 gn = self.theory_data.y 1415 1436 en = dy[index] 1416 1417 1437 # residual 1418 1438 res = (fn - gn)/en 1419 1439 # get chisqr only w/finite 1420 chisqr = numpy.average(res[numpy.isfinite(res)]*res[numpy.isfinite(res)])1421 1440 val = res[numpy.isfinite(res)]*res[numpy.isfinite(res)] 1441 chisqr = numpy.average(val) 1422 1442 return chisqr 1423 1443
Note: See TracChangeset
for help on using the changeset viewer.