- Timestamp:
- Sep 16, 2008 4:33:54 PM (16 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:
- ca6d914
- Parents:
- 14d05ba
- Location:
- sansview/perspectives/fitting
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitproblem.py
r1b07935d r3b19ac9 19 19 self.theory_name=None 20 20 self.model_list=[] 21 self.schedule='False' 21 22 22 23 def set_model(self,model,name): … … 76 77 self.model_list[0]=model 77 78 78 79 def schedule_tofit(self, schedule='False'): 80 """ 81 set schedule to true to decide if this fit must be performed 82 """ 83 self.schedule=schedule 84 def get_scheduled(self): 85 """ return true or false if a problem as being schedule for fitting""" 86 return self.schedule 87 88 -
sansview/perspectives/fitting/fitting.py
r4fc6dbf r3b19ac9 3 3 import string, numpy, pylab, math 4 4 5 from copy import deepcopy 5 6 from sans.guitools.plottables import Data1D, Theory1D 6 7 from sans.guitools.PlotPanel import PlotPanel 7 8 from sans.guicomm.events import NewPlotEvent, StatusEvent 8 from copy import deepcopy 9 from sans.fit.AbstractFitEngine import Model 10 from sans.fit.AbstractFitEngine import Data 9 from sans.fit.AbstractFitEngine import Model,Data 11 10 from fitproblem import FitProblem 12 11 from fitpanel import FitPanel … … 14 13 import models 15 14 import fitpage 15 16 16 class PlottableData(Data,Data1D): 17 """ class plottable data """17 """ class plottable data: class allowing to plot Data type on panel""" 18 18 def __init__(self,data,data1d): 19 19 self.x = data1d.x 20 20 self.y = data1d.y 21 self.dx = data1d.dx22 self.dy = data1d.dy21 self.dx = data1d.dx 22 self.dy = data1d.dy 23 23 24 self.group_id = data1d.group_id24 self.group_id = data1d.group_id 25 25 x_name, x_units = data1d.get_xaxis() 26 26 y_name, y_units = data1d.get_yaxis() 27 27 self.xaxis( x_name, x_units) 28 28 self.yaxis( y_name, y_units ) 29 self.qmin=data.qmin 30 self.qmax=data.qmax 29 self.qmin = data.qmin 30 self.qmax = data.qmax 31 31 32 class Plugin: 32 33 """ 33 34 """ 34 35 35 def __init__(self): 36 36 ## Plug-in name … … 49 49 self.standalone=True 50 50 ## Fit engine 51 self._fit_engine = ' park'51 self._fit_engine = 'scipy' 52 52 # Log startup 53 53 logging.info("Fitting plug-in started") … … 65 65 self.menu1.Append(id1, '&Show fit panel') 66 66 wx.EVT_MENU(owner, id1, self.on_perspective) 67 id3 = wx.NewId() 68 self.menu1.Append(id3,'&scipy \ park','toggle engine to park or scipy') 69 wx.EVT_MENU(owner, id3, self._onset_engine) 67 70 68 71 #menu for model … … 74 77 self.fit_panel.set_model_list(self.menu_mng.get_model_list()) 75 78 owner.Bind(fitpage.EVT_MODEL_BOX,self._on_model_panel) 76 #owner.Bind(modelpage.EVT_MODEL_DRAW,self.draw_model) 77 79 #create menubar items 78 80 return [(id, self.menu1, "Fitting"),(id2, menu2, "Model")] 79 81 … … 113 115 self.perspective.append(self.fit_panel.window_name) 114 116 # take care of saving data, model and page associated with each other 115 self.page_finder={} 116 self.index_model=0 117 self.page_finder = {} 118 #index number to create random model name 119 self.index_model = 0 120 #create the fitting panel 117 121 return [self.fit_panel] 118 122 … … 150 154 for item in self.panel.graph.plottables: 151 155 if item.name == self.panel.graph.selected_plottable: 156 #find a name for the page created for notebook 152 157 try: 153 158 name = item.group_id # item in Data1D … … 156 161 try: 157 162 page = self.fit_panel.add_fit_page(name) 163 # add data associated to the page created 158 164 page.set_data_name(item) 165 #create a fitproblem storing all link to data,model,page creation 159 166 self.page_finder[page]= FitProblem() 160 #creating Data type 161 data1= Data(sans_data=item) 162 datas=PlottableData(data=data1,data1d=item) 163 self.page_finder[page].add_data(datas) 167 data_for_park= Data(sans_data=item) 168 datap=PlottableData(data=data_for_park,data1d=item) 169 self.page_finder[page].add_data(datap) 164 170 except: 165 171 raise … … 173 179 174 180 175 def set_page_finder(self,modelname, param,values):181 def set_page_finder(self,modelname,names,values): 176 182 """ 177 183 Used by simfitpage.py to reset a parameter given the string constrainst. 178 184 @param modelname: the name ot the model for with the parameter has to reset 179 185 @param value: can be a string in this case. 186 @param names: the paramter name 180 187 @note: expecting park used for fit. 181 188 """ … … 186 193 model=list[0] 187 194 if model.name== modelname: 188 value.set_model_param(param,values) 195 value.set_model_param(names,values) 196 189 197 break 190 198 … … 193 201 def split_string(self,item): 194 202 """ 195 recieve a word containing "" 196 """ 197 #print "fitting: split :went here" 203 receive a word containing dot and split it. used to split parameterset 204 name into model name and parameter name example: 205 paramaterset (item) = M1.A 206 @return model_name =M1 , parameter name =A 207 """ 198 208 if string.find(item,".")!=-1: 199 209 param_names= re.split("\.",item) … … 205 215 def _single_fit_completed(self,result,pars,current_pg,qmin,qmax): 206 216 """ 217 Display fit result on one page of the notebook. 218 @param result: result of fit 219 @param pars: list of names of parameters fitted 220 @param current_pg: the page where information will be displayed 221 @param qmin: the minimum value of x to replot the model 222 @param qmax: the maximum value of x to replot model 207 223 208 224 """ … … 214 230 model= list[0] 215 231 break 216 #print "fitting : result",result,result.pvec,result.cov,result.fitness217 232 i = 0 218 233 for name in pars: … … 221 236 else: 222 237 model.setParam(name,result.pvec[i]) 223 #print "fitting: i name out[i]", i,name,float(result.pvec[i])224 238 i += 1 225 new_cov=[] 226 if result.cov !=None: 227 for j in range(len(result.cov)): 228 new_cov.append(result.cov[j][j]) 229 else: 230 new_cov=None 231 current_pg.onsetValues(result.fitness, result.pvec,new_cov) 239 current_pg.onsetValues(result.fitness, result.pvec,result.stderr) 232 240 self.plot_helper(currpage=current_pg,qmin=qmin,qmax=qmax) 233 241 except: 234 raise235 242 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 236 243 … … 245 252 try: 246 253 for page, value in self.page_finder.iteritems(): 247 data = value.get_data() 248 list = value.get_model() 249 model= list[0] 250 251 small_out = [] 252 small_cov = [] 253 i = 0 254 for p in result.parameters: 255 print "fitting: fit in park fitting", p.name, p.value,p.stderr 256 model_name,param_name = self.split_string(p.name) 257 print "fitting: simultfit",model.name,model_name,param_name ,p.name,param_name,p.value 258 if model.name == model_name: 259 print "fitting: hello",p.name,param_name,p.value 260 small_out.append(p.value ) 261 small_cov.append(p.stderr) 262 model.setParam(param_name,p.value) 263 #print "fitting: out of each page",page,small_out,small_cov,data.group_id 264 page.onsetValues(result.fitness, small_out,small_cov) 265 self.plot_helper(currpage= page,qmin= qmin,qmax= qmax) 254 print "fitting : simultaneous scheduled ",value.get_scheduled() 255 if value.get_scheduled()=='True': 256 data = value.get_data() 257 list = value.get_model() 258 model= list[0] 259 260 small_out = [] 261 small_cov = [] 262 i = 0 263 #Separate result in to data corresponding to each page 264 for p in result.parameters: 265 model_name,param_name = self.split_string(p.name) 266 if model.name == model_name: 267 small_out.append(p.value ) 268 small_cov.append(p.stderr) 269 model.setParam(param_name,p.value) 270 # Display result on each page 271 page.onsetValues(result.fitness, small_out,small_cov) 272 #Replot model 273 self.plot_helper(currpage= page,qmin= qmin,qmax= qmax) 266 274 except: 267 275 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) … … 279 287 from sans.fit.Fitting import Fit 280 288 self.fitter= Fit(self._fit_engine) 281 #Setting an id to store model and data 289 #Setting an id to store model and data in fit engine 282 290 if id==None: 283 291 id=0 284 self.id=id 292 self.id = id 293 #Get information (model , data) related to the page on 294 #with the fit will be perform 285 295 current_pg=self.fit_panel.get_current_page() 286 296 for page, value in self.page_finder.iteritems(): 287 297 if page ==current_pg : 288 #print "fitting: self.page_finder",self.page_finder289 298 data = value.get_data() 290 299 list=value.get_model() 291 300 model=list[0] 292 301 293 #Create dictionaryof parameters for fitting used302 #Create list of parameters for fitting used 294 303 pars=[] 295 304 templist=[] … … 300 309 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 301 310 return 302 #for element in templist: 303 # print "fitting: templist",str(element[0].GetLabelText()) 304 #print "fitting: model list",model.getParamList() 311 305 312 for element in templist: 306 313 try: 307 314 pars.append(str(element[0].GetLabelText())) 308 315 except: 309 raise310 316 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 311 317 return 312 #print "fitting: pars",pars 318 # make sure to keep an alphabetic order 319 #of parameter names in the list 313 320 pars.sort() 314 print "fitting: model ",model.__class__.__name__315 self.fitter.set_model(Model(model),model.name, self.id, pars)316 self.fitter.set_data(data,self.id,qmin,qmax)317 #Do the fit SCIPY318 try:321 #Do the single fit 322 try: 323 self.fitter.set_model(Model(model), self.id, pars) 324 self.fitter.set_data(data,self.id,qmin,qmax) 325 319 326 result=self.fitter.fit() 320 327 self._single_fit_completed(result,pars,current_pg,qmin,qmax) … … 333 340 334 341 """ 335 #print "fitting: probabily breaking on fit call"336 342 #set an engine to perform fit 337 343 from sans.fit.Fitting import Fit 338 344 self.fitter= Fit(self._fit_engine) 345 339 346 #Setting an id to store model and data 340 347 if id==None: 341 348 id = 0 342 349 self.id = id 350 343 351 for page, value in self.page_finder.iteritems(): 344 352 try: 345 #print "fitting: self.page_finder",value 346 data = value.get_data() 347 #print "fitting: data",data 348 list = value.get_model() 349 model= list[0] 350 if data != None : 353 print "fitting : simultaneous scheduled ",value.get_scheduled(),value.schedule 354 if value.get_scheduled()=='True': 355 print "fitting: self.page_finder",value.get_scheduled() 356 data = value.get_data() 357 #print "fitting: data",data 358 list = value.get_model() 359 model= list[0] 351 360 #Create dictionary of parameters for fitting used 352 361 pars = [] 353 362 templist = [] 354 try: 355 templist = page.get_param_list() 356 except: 357 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 358 return 359 363 templist = page.get_param_list() 360 364 for element in templist: 361 365 try: … … 365 369 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 366 370 return 371 self.fitter.set_model(Model(model), self.id, pars) 372 self.fitter.set_data(data,self.id,qmin,qmax) 367 373 368 print "fitting: pars",pars,model.name 369 #print "fitter",self.fitter 370 #print "fitting: model name",model.name 371 self.fitter.set_model(Model(model),model.name, self.id, pars) 372 self.fitter.set_data(data,self.id,qmin,qmax) 373 else: 374 raise ValueError," Fitting: cannot set model with empty parameter" 375 self.id += 1 374 self.id += 1 376 375 except: 377 raise 378 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 379 return 380 #Do the fit SCIPY 376 wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) 377 return 378 #Do the simultaneous fit 381 379 try: 382 380 result=self.fitter.fit() 383 381 self._simul_fit_completed(result,qmin,qmax) 384 382 except: 385 raise386 383 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) 387 384 return 388 385 386 387 def _onset_engine(self,event): 388 """ set engine to scipy""" 389 if self._fit_engine== 'park': 390 self._on_change_engine('scipy') 391 else: 392 self._on_change_engine('park') 393 wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) 394 395 396 389 397 def _on_change_engine(self, engine='park'): 390 398 """ … … 439 447 @param currpage: page in a dictionary referring to some data 440 448 """ 441 #print "fitting: plot helper"442 449 if self.fit_panel.get_page_count() >1: 443 450 for page in self.page_finder.iterkeys(): … … 492 499 try: 493 500 from sans.guicomm.events import NewPlotEvent 494 #print"fitting: theory",theory495 501 wx.PostEvent(self.parent, NewPlotEvent(plot=theory, title="Analytical model")) 496 502 except: … … 503 509 Plot a theory from a model selected from the menu 504 510 """ 505 print "_on_model_menu done"506 #name = evt.model.__class__.__name__507 511 name="Model View" 508 512 model=evt.modelinfo.model() … … 512 516 513 517 def draw_model(self,model): 518 """ 519 draw model with default data value 520 """ 514 521 x = pylab.arange(0.001, 0.1, 0.001) 515 522 xlen = len(x) … … 523 530 524 531 new_plot = Theory1D(x, y) 525 #new_plot.name = evt.model.__class__.__name__526 532 new_plot.name ="Fitness" 527 533 new_plot.xaxis("\\rm{Q}", 'A^{-1}') -
sansview/perspectives/fitting/models.py
r49b7efa r3b19ac9 40 40 try: 41 41 (file, path, info) = imp.find_module(name, path) 42 print "models:module ",file43 42 module = imp.load_module( name, file, item, info ) 44 print "models:module ",module45 43 if hasattr(module, "Model"): 46 44 try: … … 125 123 id = wx.NewId() 126 124 if len(self.indep_model_list) == 0: 127 print "models: self.indep_model",self.indep_model128 125 for item in self.indep_model: 129 126 if item not in self.indep_model_list.values(): … … 139 136 id = wx.NewId() 140 137 modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") 138 139 140 141 141 model_info="additional models" 142 142 id = wx.NewId() … … 156 156 id = wx.NewId() 157 157 modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") 158 159 160 158 return 0 161 159 -
sansview/perspectives/fitting/simfitpage.py
r1b07935d r3b19ac9 91 91 for item in self.model_list: 92 92 item[0].SetValue(True) 93 item[1].schedule_tofit('True') 93 94 self.model_toFit.append(item) 94 95 else: 95 96 for item in self.model_list: 96 97 item[0].SetValue(False) 98 item[1].schedule_tofit('False') 97 99 self.model_toFit=[] 98 100 … … 163 165 for item in self.model_list: 164 166 if item[0].GetValue()==True: 167 item[1].schedule_tofit('True') 165 168 self.model_toFit.append(item) 166 169 else: 167 170 if item in self.model_toFit: 168 171 self.model_toFit.remove(item) 172 item[1].schedule_tofit('False') 169 173 self.cb1.SetValue(False) 170 174 if len(self.model_list)==len(self.model_toFit):
Note: See TracChangeset
for help on using the changeset viewer.