[c80c06f] | 1 | import os,os.path, re |
---|
| 2 | import sys, wx, logging |
---|
| 3 | import string, numpy, math |
---|
| 4 | |
---|
| 5 | from copy import deepcopy |
---|
| 6 | from danse.common.plottools.plottables import Data1D, Theory1D,Data2D |
---|
| 7 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
| 8 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
| 9 | from sans.guicomm.events import EVT_SLICER_PANEL,EVT_MODEL2D_PANEL |
---|
| 10 | |
---|
| 11 | from sans.fit.AbstractFitEngine import Model,Data,FitData1D,FitData2D |
---|
| 12 | from fitproblem import FitProblem |
---|
| 13 | from fitpanel import FitPanel |
---|
| 14 | from fit_thread import FitThread |
---|
| 15 | import models,modelpage |
---|
| 16 | import fitpage1D,fitpage2D |
---|
| 17 | import park |
---|
| 18 | DEFAULT_BEAM = 0.005 |
---|
| 19 | DEFAULT_QMIN = 0.0 |
---|
| 20 | DEFAULT_QMAX = 0.15 |
---|
| 21 | DEFAULT_NPTS = 40 |
---|
| 22 | import time |
---|
| 23 | import thread |
---|
| 24 | print "main",thread.get_ident() |
---|
| 25 | |
---|
| 26 | class Plugin: |
---|
| 27 | """ |
---|
| 28 | Fitting plugin is used to perform fit |
---|
| 29 | """ |
---|
| 30 | def __init__(self): |
---|
| 31 | ## Plug-in name |
---|
| 32 | self.sub_menu = "Fitting" |
---|
| 33 | |
---|
| 34 | ## Reference to the parent window |
---|
| 35 | self.parent = None |
---|
| 36 | self.menu_mng = models.ModelManager() |
---|
| 37 | ## List of panels for the simulation perspective (names) |
---|
| 38 | self.perspective = [] |
---|
| 39 | self.mypanels=[] |
---|
| 40 | self.calc_thread = None |
---|
| 41 | self.done = False |
---|
| 42 | # Start with a good default |
---|
| 43 | self.elapsed = 0.022 |
---|
| 44 | self.fitter = None |
---|
| 45 | |
---|
| 46 | #Flag to let the plug-in know that it is running standalone |
---|
| 47 | self.standalone=True |
---|
| 48 | ## Fit engine |
---|
| 49 | self._fit_engine = 'scipy' |
---|
| 50 | self.enable_model2D=False |
---|
| 51 | # Log startup |
---|
| 52 | logging.info("Fitting plug-in started") |
---|
| 53 | # model 2D view |
---|
| 54 | self.model2D_id=None |
---|
| 55 | |
---|
| 56 | def populate_menu(self, id, owner): |
---|
| 57 | """ |
---|
| 58 | Create a menu for the Fitting plug-in |
---|
| 59 | @param id: id to create a menu |
---|
| 60 | @param owner: owner of menu |
---|
| 61 | @ return : list of information to populate the main menu |
---|
| 62 | """ |
---|
| 63 | self.parent.Bind(EVT_MODEL2D_PANEL, self._on_model2D_show) |
---|
| 64 | #Menu for fitting |
---|
| 65 | self.menu1 = wx.Menu() |
---|
| 66 | id1 = wx.NewId() |
---|
| 67 | self.menu1.Append(id1, '&Show fit panel') |
---|
| 68 | wx.EVT_MENU(owner, id1, self.on_perspective) |
---|
| 69 | id3 = wx.NewId() |
---|
| 70 | self.menu1.AppendCheckItem(id3, "park") |
---|
| 71 | wx.EVT_MENU(owner, id3, self._onset_engine) |
---|
| 72 | |
---|
| 73 | #menu for model |
---|
| 74 | menu2 = wx.Menu() |
---|
| 75 | |
---|
| 76 | self.menu_mng.populate_menu(menu2, owner) |
---|
| 77 | id2 = wx.NewId() |
---|
| 78 | owner.Bind(models.EVT_MODEL,self._on_model_menu) |
---|
| 79 | #owner.Bind(modelpage.EVT_MODEL,self._on_model_menu) |
---|
| 80 | self.fit_panel.set_owner(owner) |
---|
| 81 | self.fit_panel.set_model_list(self.menu_mng.get_model_list()) |
---|
| 82 | owner.Bind(fitpage1D.EVT_MODEL_BOX,self._on_model_panel) |
---|
| 83 | owner.Bind(fitpage2D.EVT_MODEL_BOX,self._on_model_panel) |
---|
| 84 | #create menubar items |
---|
| 85 | return [(id, self.menu1, "Fitting"),(id2, menu2, "Model")] |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | def help(self, evt): |
---|
| 89 | """ |
---|
| 90 | Show a general help dialog. |
---|
| 91 | TODO: replace the text with a nice image |
---|
| 92 | """ |
---|
| 93 | from helpDialog import HelpWindow |
---|
| 94 | dialog = HelpWindow(None, -1, 'HelpWindow') |
---|
| 95 | if dialog.ShowModal() == wx.ID_OK: |
---|
| 96 | pass |
---|
| 97 | dialog.Destroy() |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | def get_context_menu(self, graph=None): |
---|
| 101 | """ |
---|
| 102 | Get the context menu items available for P(r) |
---|
| 103 | @param graph: the Graph object to which we attach the context menu |
---|
| 104 | @return: a list of menu items with call-back function |
---|
| 105 | """ |
---|
| 106 | self.graph=graph |
---|
| 107 | for item in graph.plottables: |
---|
| 108 | if item.__class__.__name__ is "Data2D": |
---|
| 109 | return [["Select data for Fitting",\ |
---|
| 110 | "Dialog with fitting parameters ", self._onSelect]] |
---|
| 111 | else: |
---|
| 112 | if item.name==graph.selected_plottable and\ |
---|
| 113 | item.__class__.__name__ is "Data1D": |
---|
| 114 | return [["Select data for Fitting", \ |
---|
| 115 | "Dialog with fitting parameters ", self._onSelect]] |
---|
| 116 | return [] |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | def get_panels(self, parent): |
---|
| 120 | """ |
---|
| 121 | Create and return a list of panel objects |
---|
| 122 | """ |
---|
| 123 | self.parent = parent |
---|
| 124 | # Creation of the fit panel |
---|
| 125 | self.fit_panel = FitPanel(self.parent, -1) |
---|
| 126 | #Set the manager forthe main panel |
---|
| 127 | self.fit_panel.set_manager(self) |
---|
| 128 | # List of windows used for the perspective |
---|
| 129 | self.perspective = [] |
---|
| 130 | self.perspective.append(self.fit_panel.window_name) |
---|
| 131 | # take care of saving data, model and page associated with each other |
---|
| 132 | self.page_finder = {} |
---|
| 133 | #index number to create random model name |
---|
| 134 | self.index_model = 0 |
---|
| 135 | self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) |
---|
| 136 | |
---|
| 137 | #create the fitting panel |
---|
| 138 | #return [self.fit_panel] |
---|
| 139 | |
---|
| 140 | self.mypanels.append(self.fit_panel) |
---|
| 141 | return self.mypanels |
---|
| 142 | def _on_model2D_show(self, event ): |
---|
| 143 | print "model2D get the id ", event.id |
---|
| 144 | def _on_slicer_event(self, event): |
---|
| 145 | print "fitting:slicer event ", event.panel |
---|
| 146 | new_panel = event.panel |
---|
| 147 | # Set group ID if available |
---|
| 148 | event_id = self.parent.popup_panel(new_panel) |
---|
| 149 | self.menu1.Append(event_id, new_panel.window_caption, |
---|
| 150 | "Show %s plot panel" % new_panel.window_caption) |
---|
| 151 | # Set UID to allow us to reference the panel later |
---|
| 152 | new_panel.uid = event_id |
---|
| 153 | new_panel |
---|
| 154 | self.mypanels.append(new_panel) |
---|
| 155 | return |
---|
| 156 | def _on_show_panel(self, event): |
---|
| 157 | print "_on_show_panel: fitting" |
---|
| 158 | |
---|
| 159 | def get_perspective(self): |
---|
| 160 | """ |
---|
| 161 | Get the list of panel names for this perspective |
---|
| 162 | """ |
---|
| 163 | return self.perspective |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | def on_perspective(self, event): |
---|
| 167 | """ |
---|
| 168 | Call back function for the perspective menu item. |
---|
| 169 | We notify the parent window that the perspective |
---|
| 170 | has changed. |
---|
| 171 | """ |
---|
| 172 | self.parent.set_perspective(self.perspective) |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | def post_init(self): |
---|
| 176 | """ |
---|
| 177 | Post initialization call back to close the loose ends |
---|
| 178 | [Somehow openGL needs this call] |
---|
| 179 | """ |
---|
| 180 | self.parent.set_perspective(self.perspective) |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | def _onSelect(self,event): |
---|
| 184 | """ |
---|
| 185 | when Select data to fit a new page is created .Its reference is |
---|
| 186 | added to self.page_finder |
---|
| 187 | """ |
---|
| 188 | self.panel = event.GetEventObject() |
---|
| 189 | for item in self.panel.graph.plottables: |
---|
| 190 | if item.name == self.panel.graph.selected_plottable or\ |
---|
| 191 | item.__class__.__name__ is "Data2D": |
---|
| 192 | #find a name for the page created for notebook |
---|
| 193 | try: |
---|
| 194 | page, model_name = self.fit_panel.add_fit_page(item) |
---|
| 195 | # add data associated to the page created |
---|
| 196 | |
---|
| 197 | if page !=None: |
---|
| 198 | #create a fitproblem storing all link to data,model,page creation |
---|
| 199 | self.page_finder[page]= FitProblem() |
---|
| 200 | self.page_finder[page].save_model_name(model_name) |
---|
| 201 | self.page_finder[page].add_data(item) |
---|
| 202 | except: |
---|
| 203 | raise |
---|
| 204 | wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ |
---|
| 205 | %sys.exc_value)) |
---|
| 206 | def schedule_for_fit(self,value=0,fitproblem =None): |
---|
| 207 | """ |
---|
| 208 | |
---|
| 209 | """ |
---|
| 210 | if fitproblem !=None: |
---|
| 211 | fitproblem.schedule_tofit(value) |
---|
| 212 | else: |
---|
| 213 | current_pg=self.fit_panel.get_current_page() |
---|
| 214 | for page, val in self.page_finder.iteritems(): |
---|
| 215 | if page ==current_pg : |
---|
| 216 | val.schedule_tofit(value) |
---|
| 217 | break |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | def get_page_finder(self): |
---|
| 221 | """ @return self.page_finder used also by simfitpage.py""" |
---|
| 222 | return self.page_finder |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | def set_page_finder(self,modelname,names,values): |
---|
| 226 | """ |
---|
| 227 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
| 228 | @param modelname: the name ot the model for with the parameter has to reset |
---|
| 229 | @param value: can be a string in this case. |
---|
| 230 | @param names: the paramter name |
---|
| 231 | @note: expecting park used for fit. |
---|
| 232 | """ |
---|
| 233 | sim_page=self.fit_panel.get_page(0) |
---|
| 234 | for page, value in self.page_finder.iteritems(): |
---|
| 235 | if page != sim_page: |
---|
| 236 | list=value.get_model() |
---|
| 237 | model=list[0] |
---|
| 238 | #print "fitting",model.name,modelname |
---|
| 239 | if model.name== modelname: |
---|
| 240 | value.set_model_param(names,values) |
---|
| 241 | break |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | def split_string(self,item): |
---|
| 246 | """ |
---|
| 247 | receive a word containing dot and split it. used to split parameterset |
---|
| 248 | name into model name and parameter name example: |
---|
| 249 | paramaterset (item) = M1.A |
---|
| 250 | @return model_name =M1 , parameter name =A |
---|
| 251 | """ |
---|
| 252 | if string.find(item,".")!=-1: |
---|
| 253 | param_names= re.split("\.",item) |
---|
| 254 | model_name=param_names[0] |
---|
| 255 | param_name=param_names[1] |
---|
| 256 | return model_name,param_name |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | def _single_fit_completed(self,result,pars,cpage,qmin,qmax,elapsed,ymin=None, ymax=None): |
---|
| 260 | """ |
---|
| 261 | Display fit result on one page of the notebook. |
---|
| 262 | @param result: result of fit |
---|
| 263 | @param pars: list of names of parameters fitted |
---|
| 264 | @param current_pg: the page where information will be displayed |
---|
| 265 | @param qmin: the minimum value of x to replot the model |
---|
| 266 | @param qmax: the maximum value of x to replot model |
---|
| 267 | |
---|
| 268 | """ |
---|
| 269 | #print "single fit ", pars,result.pvec,result.stderr,result.fitness |
---|
| 270 | #self.done = True |
---|
| 271 | #wx.PostEvent(self.parent, StatusEvent(status="Fitting Completed: %g" % elapsed)) |
---|
| 272 | try: |
---|
| 273 | for page, value in self.page_finder.iteritems(): |
---|
| 274 | if page==cpage : |
---|
| 275 | #fitdata = value.get_data() |
---|
| 276 | list = value.get_model() |
---|
| 277 | model= list[0] |
---|
| 278 | break |
---|
| 279 | i = 0 |
---|
| 280 | #print "fitting: single fit pars ", pars |
---|
| 281 | for name in pars: |
---|
| 282 | if result.pvec.__class__==numpy.float64: |
---|
| 283 | model.setParam(name,result.pvec) |
---|
| 284 | else: |
---|
| 285 | model.setParam(name,result.pvec[i]) |
---|
| 286 | # print "fitting: single fit", name, result.pvec[i] |
---|
| 287 | i += 1 |
---|
| 288 | # print "fitting result : chisqr",result.fitness |
---|
| 289 | # print "fitting result : pvec",result.pvec |
---|
| 290 | # print "fitting result : stderr",result.stderr |
---|
| 291 | |
---|
| 292 | cpage.onsetValues(result.fitness, result.pvec,result.stderr) |
---|
| 293 | self.plot_helper(currpage=cpage,qmin=qmin,qmax=qmax,ymin=ymin, ymax=ymax, title="Fitted model 2D ") |
---|
| 294 | except: |
---|
| 295 | #raise |
---|
| 296 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | def _simul_fit_completed(self,result,qmin,qmax, elapsed,pars=None,cpage=None, ymin=None, ymax=None): |
---|
| 300 | """ |
---|
| 301 | Parameter estimation completed, |
---|
| 302 | display the results to the user |
---|
| 303 | @param alpha: estimated best alpha |
---|
| 304 | @param elapsed: computation time |
---|
| 305 | """ |
---|
| 306 | wx.PostEvent(self.parent, StatusEvent(status="Fitting Completed: %g" % elapsed)) |
---|
| 307 | try: |
---|
| 308 | for page, value in self.page_finder.iteritems(): |
---|
| 309 | if value.get_scheduled()==1: |
---|
| 310 | #fitdata = value.get_data() |
---|
| 311 | list = value.get_model() |
---|
| 312 | model= list[0] |
---|
| 313 | |
---|
| 314 | small_out = [] |
---|
| 315 | small_cov = [] |
---|
| 316 | i = 0 |
---|
| 317 | #Separate result in to data corresponding to each page |
---|
| 318 | for p in result.parameters: |
---|
| 319 | model_name,param_name = self.split_string(p.name) |
---|
| 320 | if model.name == model_name: |
---|
| 321 | small_out.append(p.value ) |
---|
| 322 | small_cov.append(p.stderr) |
---|
| 323 | model.setParam(param_name,p.value) |
---|
| 324 | # Display result on each page |
---|
| 325 | page.onsetValues(result.fitness, small_out,small_cov) |
---|
| 326 | #Replot model |
---|
| 327 | self.plot_helper(currpage= page,qmin= qmin,qmax= qmax,ymin=ymin, ymax=ymax) |
---|
| 328 | except: |
---|
| 329 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | def _on_single_fit(self,id=None,qmin=None,qmax=None,ymin=None,ymax=None): |
---|
| 333 | """ |
---|
| 334 | perform fit for the current page and return chisqr,out and cov |
---|
| 335 | @param engineName: type of fit to be performed |
---|
| 336 | @param id: unique id corresponding to a fit problem(model, set of data) |
---|
| 337 | @param model: model to fit |
---|
| 338 | |
---|
| 339 | """ |
---|
| 340 | #print "in single fitting" |
---|
| 341 | #set an engine to perform fit |
---|
| 342 | from sans.fit.Fitting import Fit |
---|
| 343 | self.fitter= Fit(self._fit_engine) |
---|
| 344 | #Setting an id to store model and data in fit engine |
---|
| 345 | if id==None: |
---|
| 346 | id=0 |
---|
| 347 | self.id = id |
---|
| 348 | page_fitted=None |
---|
| 349 | fit_problem=None |
---|
| 350 | #Get information (model , data) related to the page on |
---|
| 351 | #with the fit will be perform |
---|
| 352 | #current_pg=self.fit_panel.get_current_page() |
---|
| 353 | #simul_pg=self.fit_panel.get_page(0) |
---|
| 354 | |
---|
| 355 | for page, value in self.page_finder.iteritems(): |
---|
| 356 | if value.get_scheduled() ==1 : |
---|
| 357 | metadata = value.get_data() |
---|
| 358 | list=value.get_model() |
---|
| 359 | model=list[0] |
---|
| 360 | smearer= value.get_smearer() |
---|
| 361 | print "single fit", model, smearer |
---|
| 362 | #Create list of parameters for fitting used |
---|
| 363 | pars=[] |
---|
| 364 | templist=[] |
---|
| 365 | try: |
---|
| 366 | #templist=current_pg.get_param_list() |
---|
| 367 | templist=page.get_param_list() |
---|
| 368 | for element in templist: |
---|
| 369 | pars.append(str(element[0].GetLabelText())) |
---|
| 370 | pars.sort() |
---|
| 371 | #print "single fit start pars:", pars |
---|
| 372 | #Do the single fit |
---|
| 373 | self.fitter.set_model(Model(model), self.id, pars) |
---|
| 374 | |
---|
| 375 | self.fitter.set_data(metadata,self.id,smearer, qmin,qmax) |
---|
| 376 | self.fitter.select_problem_for_fit(Uid=self.id,value=value.get_scheduled()) |
---|
| 377 | page_fitted=page |
---|
| 378 | self.id+=1 |
---|
| 379 | self.schedule_for_fit( 0,value) |
---|
| 380 | except: |
---|
| 381 | raise |
---|
| 382 | #wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 383 | return |
---|
| 384 | # make sure to keep an alphabetic order |
---|
| 385 | #of parameter names in the list |
---|
| 386 | try: |
---|
| 387 | # If a thread is already started, stop it |
---|
| 388 | if self.calc_thread != None and self.calc_thread.isrunning(): |
---|
| 389 | self.calc_thread.stop() |
---|
| 390 | |
---|
| 391 | self.calc_thread =FitThread(parent =self.parent, |
---|
| 392 | fn= self.fitter, |
---|
| 393 | pars= pars, |
---|
| 394 | cpage= page_fitted, |
---|
| 395 | qmin=qmin, |
---|
| 396 | qmax=qmax, |
---|
| 397 | ymin= ymin, |
---|
| 398 | ymax= ymax, |
---|
| 399 | completefn=self._single_fit_completed, |
---|
| 400 | updatefn=None) |
---|
| 401 | self.calc_thread.queue() |
---|
| 402 | self.calc_thread.ready(2.5) |
---|
| 403 | #while not self.done: |
---|
| 404 | #print "when here" |
---|
| 405 | # time.sleep(1) |
---|
| 406 | |
---|
| 407 | |
---|
| 408 | except: |
---|
| 409 | raise |
---|
| 410 | wx.PostEvent(self.parent, StatusEvent(status="Single Fit error: %s" % sys.exc_value)) |
---|
| 411 | return |
---|
| 412 | |
---|
| 413 | def _on_simul_fit(self, id=None,qmin=None,qmax=None, ymin=None, ymax=None): |
---|
| 414 | """ |
---|
| 415 | perform fit for all the pages selected on simpage and return chisqr,out and cov |
---|
| 416 | @param engineName: type of fit to be performed |
---|
| 417 | @param id: unique id corresponding to a fit problem(model, set of data) |
---|
| 418 | in park_integration |
---|
| 419 | @param model: model to fit |
---|
| 420 | |
---|
| 421 | """ |
---|
| 422 | #set an engine to perform fit |
---|
| 423 | from sans.fit.Fitting import Fit |
---|
| 424 | self.fitter= Fit(self._fit_engine) |
---|
| 425 | |
---|
| 426 | #Setting an id to store model and data |
---|
| 427 | if id==None: |
---|
| 428 | id = 0 |
---|
| 429 | self.id = id |
---|
| 430 | |
---|
| 431 | for page, value in self.page_finder.iteritems(): |
---|
| 432 | try: |
---|
| 433 | if value.get_scheduled()==1: |
---|
| 434 | metadata = value.get_data() |
---|
| 435 | list = value.get_model() |
---|
| 436 | model= list[0] |
---|
| 437 | #Create dictionary of parameters for fitting used |
---|
| 438 | pars = [] |
---|
| 439 | templist = [] |
---|
| 440 | templist = page.get_param_list() |
---|
| 441 | for element in templist: |
---|
| 442 | try: |
---|
| 443 | name = str(element[0].GetLabelText()) |
---|
| 444 | pars.append(name) |
---|
| 445 | except: |
---|
| 446 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 447 | return |
---|
| 448 | # need to check this print "new model " |
---|
| 449 | new_model=Model(model) |
---|
| 450 | param=value.get_model_param() |
---|
| 451 | |
---|
| 452 | if len(param)>0: |
---|
| 453 | for item in param: |
---|
| 454 | param_value = item[1] |
---|
| 455 | param_name = item[0] |
---|
| 456 | #print "fitting ", param,param_name, param_value |
---|
| 457 | |
---|
| 458 | #new_model.set( model.getParam(param_name[0])= param_value) |
---|
| 459 | #new_model.set( exec"%s=%s"%(param_name[0], param_value)) |
---|
| 460 | #new_model.set( exec "%s"%(param_nam) = param_value) |
---|
| 461 | new_model.parameterset[ param_name].set( param_value ) |
---|
| 462 | |
---|
| 463 | self.fitter.set_model(new_model, self.id, pars) |
---|
| 464 | self.fitter.set_data(metadata,self.id,qmin,qmax,ymin,ymax) |
---|
| 465 | self.fitter.select_problem_for_fit(Uid=self.id,value=value.get_scheduled()) |
---|
| 466 | self.id += 1 |
---|
| 467 | except: |
---|
| 468 | #raise |
---|
| 469 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 470 | return |
---|
| 471 | #Do the simultaneous fit |
---|
| 472 | try: |
---|
| 473 | # If a thread is already started, stop it |
---|
| 474 | if self.calc_thread != None and self.calc_thread.isrunning(): |
---|
| 475 | self.calc_thread.stop() |
---|
| 476 | |
---|
| 477 | self.calc_thread =FitThread(parent =self.parent, |
---|
| 478 | fn= self.fitter, |
---|
| 479 | qmin=qmin, |
---|
| 480 | qmax=qmax, |
---|
| 481 | ymin= ymin, |
---|
| 482 | ymax= ymax, |
---|
| 483 | completefn= self._simul_fit_completed, |
---|
| 484 | updatefn=None) |
---|
| 485 | self.calc_thread.queue() |
---|
| 486 | self.calc_thread.ready(2.5) |
---|
| 487 | |
---|
| 488 | except: |
---|
| 489 | #raise |
---|
| 490 | wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) |
---|
| 491 | return |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | def _onset_engine(self,event): |
---|
| 495 | """ set engine to scipy""" |
---|
| 496 | if self._fit_engine== 'park': |
---|
| 497 | self._on_change_engine('scipy') |
---|
| 498 | else: |
---|
| 499 | self._on_change_engine('park') |
---|
| 500 | wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) |
---|
| 501 | |
---|
| 502 | |
---|
| 503 | def _on_change_engine(self, engine='park'): |
---|
| 504 | """ |
---|
| 505 | Allow to select the type of engine to perform fit |
---|
| 506 | @param engine: the key work of the engine |
---|
| 507 | """ |
---|
| 508 | self._fit_engine = engine |
---|
| 509 | |
---|
| 510 | |
---|
| 511 | def _on_model_panel(self, evt): |
---|
| 512 | """ |
---|
| 513 | react to model selection on any combo box or model menu.plot the model |
---|
| 514 | """ |
---|
| 515 | |
---|
| 516 | model = evt.model |
---|
| 517 | name = evt.name |
---|
| 518 | |
---|
| 519 | sim_page=self.fit_panel.GetPage(0) |
---|
| 520 | current_pg = self.fit_panel.get_current_page() |
---|
| 521 | if current_pg != sim_page: |
---|
| 522 | current_pg.set_panel(model) |
---|
| 523 | model.name = self.page_finder[current_pg].get_name() |
---|
| 524 | try: |
---|
| 525 | metadata=self.page_finder[current_pg].get_data() |
---|
| 526 | M_name=model.name+"= "+name+"("+metadata.group_id+")" |
---|
| 527 | except: |
---|
| 528 | M_name=model.name+"= "+name |
---|
| 529 | #model.name="M"+str(self.index_model) |
---|
| 530 | self.index_model += 1 |
---|
| 531 | # save model name |
---|
| 532 | |
---|
| 533 | # save the name containing the data name with the appropriate model |
---|
| 534 | self.page_finder[current_pg].set_model(model,M_name) |
---|
| 535 | self.plot_helper(currpage= current_pg,qmin= None,qmax= None) |
---|
| 536 | sim_page.add_model(self.page_finder) |
---|
| 537 | |
---|
| 538 | def set_smearer(self,smearer): |
---|
| 539 | current_pg=self.fit_panel.get_current_page() |
---|
| 540 | self.page_finder[current_pg].set_smearer(smearer) |
---|
| 541 | |
---|
| 542 | def redraw_model(self,qmin= None,qmax= None): |
---|
| 543 | """ |
---|
| 544 | Draw a theory according to model changes or data range. |
---|
| 545 | @param qmin: the minimum value plotted for theory |
---|
| 546 | @param qmax: the maximum value plotted for theory |
---|
| 547 | """ |
---|
| 548 | current_pg=self.fit_panel.get_current_page() |
---|
| 549 | for page, value in self.page_finder.iteritems(): |
---|
| 550 | if page ==current_pg : |
---|
| 551 | break |
---|
| 552 | self.plot_helper(currpage=page,qmin= qmin,qmax= qmax) |
---|
| 553 | |
---|
| 554 | def plot_helper(self,currpage, fitModel=None, qmin=None,qmax=None, |
---|
| 555 | ymin=None,ymax=None, title=None ): |
---|
| 556 | """ |
---|
| 557 | Plot a theory given a model and data |
---|
| 558 | @param model: the model from where the theory is derived |
---|
| 559 | @param currpage: page in a dictionary referring to some data |
---|
| 560 | """ |
---|
| 561 | if self.fit_panel.GetPageCount() >1: |
---|
| 562 | for page in self.page_finder.iterkeys(): |
---|
| 563 | if page==currpage : |
---|
| 564 | data=self.page_finder[page].get_data() |
---|
| 565 | list=self.page_finder[page].get_model() |
---|
| 566 | model=list[0] |
---|
| 567 | break |
---|
| 568 | |
---|
| 569 | if data!=None and data.__class__.__name__ != 'Data2D': |
---|
| 570 | theory = Theory1D(x=[], y=[]) |
---|
| 571 | theory.name = model.name |
---|
| 572 | theory.group_id = data.group_id |
---|
| 573 | theory.id = "Model" |
---|
| 574 | x_name, x_units = data.get_xaxis() |
---|
| 575 | y_name, y_units = data.get_yaxis() |
---|
| 576 | theory.xaxis(x_name, x_units) |
---|
| 577 | theory.yaxis(y_name, y_units) |
---|
| 578 | if qmin == None : |
---|
| 579 | qmin = min(data.x) |
---|
| 580 | if qmax == None : |
---|
| 581 | qmax = max(data.x) |
---|
| 582 | try: |
---|
| 583 | tempx = qmin |
---|
| 584 | tempy = model.run(qmin) |
---|
| 585 | theory.x.append(tempx) |
---|
| 586 | theory.y.append(tempy) |
---|
| 587 | except : |
---|
| 588 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
| 589 | skipping point x %g %s" %(qmin, sys.exc_value))) |
---|
| 590 | |
---|
| 591 | for i in range(len(data.x)): |
---|
| 592 | try: |
---|
| 593 | if data.x[i]> qmin and data.x[i]< qmax: |
---|
| 594 | tempx = data.x[i] |
---|
| 595 | tempy = model.run(tempx) |
---|
| 596 | theory.x.append(tempx) |
---|
| 597 | theory.y.append(tempy) |
---|
| 598 | |
---|
| 599 | except: |
---|
| 600 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
| 601 | skipping point x %g %s" %(data.x[i], sys.exc_value))) |
---|
| 602 | try: |
---|
| 603 | tempx = qmax |
---|
| 604 | tempy = model.run(qmax) |
---|
| 605 | theory.x.append(tempx) |
---|
| 606 | theory.y.append(tempy) |
---|
| 607 | except: |
---|
| 608 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
| 609 | skipping point x %g %s" %(qmin, sys.exc_value)) ) |
---|
| 610 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
| 611 | title=str(data.name))) |
---|
| 612 | else: |
---|
| 613 | |
---|
| 614 | theory=Data2D(data.data, data.err_data) |
---|
| 615 | theory.name= model.name |
---|
| 616 | if title !=None: |
---|
| 617 | self.title = title |
---|
| 618 | theory.id= self.title |
---|
| 619 | theory.group_id= self.title+data.name |
---|
| 620 | else: |
---|
| 621 | self.title= "Analytical model 2D " |
---|
| 622 | theory.id= "Model" |
---|
| 623 | theory.group_id= "Model"+data.name |
---|
| 624 | theory.x_bins= data.x_bins |
---|
| 625 | theory.y_bins= data.y_bins |
---|
| 626 | tempy=[] |
---|
| 627 | if qmin==None: |
---|
| 628 | qmin=data.xmin |
---|
| 629 | if qmax==None: |
---|
| 630 | qmax=data.xmax |
---|
| 631 | if ymin==None: |
---|
| 632 | ymin=data.ymin |
---|
| 633 | if ymax==None: |
---|
| 634 | ymax=data.ymax |
---|
| 635 | |
---|
| 636 | theory.data = numpy.zeros((len(data.y_bins),len(data.x_bins))) |
---|
| 637 | for i in range(len(data.y_bins)): |
---|
| 638 | if data.y_bins[i]>= ymin and data.y_bins[i]<= ymax: |
---|
| 639 | for j in range(len(data.x_bins)): |
---|
| 640 | if data.x_bins[i]>= qmin and data.x_bins[i]<= qmax: |
---|
| 641 | theory.data[j][i]=model.runXY([data.x_bins[j],data.y_bins[i]]) |
---|
| 642 | |
---|
| 643 | #print "fitting : plot_helper:", theory.image |
---|
| 644 | #print data.image |
---|
| 645 | #print "fitting : plot_helper:",theory.image |
---|
| 646 | theory.detector= data.detector |
---|
| 647 | theory.source= data.source |
---|
| 648 | theory.zmin= data.zmin |
---|
| 649 | theory.zmax= data.zmax |
---|
| 650 | theory.xmin= qmin |
---|
| 651 | theory.xmax= qmax |
---|
| 652 | theory.ymin= ymin |
---|
| 653 | theory.ymax= ymax |
---|
| 654 | |
---|
| 655 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
| 656 | title=self.title +str(data.name))) |
---|
| 657 | |
---|
| 658 | |
---|
| 659 | def _on_model_menu(self, evt): |
---|
| 660 | """ |
---|
| 661 | Plot a theory from a model selected from the menu |
---|
| 662 | """ |
---|
| 663 | name = evt.model.__name__ |
---|
| 664 | if hasattr(evt.model, "name"): |
---|
| 665 | name = evt.model.name |
---|
| 666 | print "on model menu", name |
---|
| 667 | model=evt.model() |
---|
| 668 | description=model.description |
---|
| 669 | |
---|
| 670 | # Create a model page. If a new page is created, the model |
---|
| 671 | # will be plotted automatically. If a page already exists, |
---|
| 672 | # the content will be updated and the plot refreshed |
---|
| 673 | self.fit_panel.add_model_page(model,description,name,topmenu=True) |
---|
| 674 | |
---|
| 675 | def draw_model(self,model,name ,data=None,description=None,enable1D=True, enable2D=False, |
---|
| 676 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
| 677 | """ |
---|
| 678 | draw model with default data value |
---|
| 679 | """ |
---|
| 680 | if data !=None: |
---|
| 681 | self.redraw_model(qmin,qmax) |
---|
| 682 | return |
---|
| 683 | self._draw_model2D(model=model, |
---|
| 684 | description=model.description, |
---|
| 685 | enable2D= enable2D, |
---|
| 686 | qmin=qmin, |
---|
| 687 | qmax=qmax, |
---|
| 688 | qstep=qstep) |
---|
| 689 | self._draw_model1D(model,name,model.description, enable1D,qmin,qmax, qstep) |
---|
| 690 | |
---|
| 691 | def _draw_model1D(self,model,name,description=None, enable1D=True, |
---|
| 692 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
| 693 | |
---|
| 694 | if enable1D: |
---|
| 695 | x= numpy.linspace(start= qmin, |
---|
| 696 | stop= qmax, |
---|
| 697 | num= qstep, |
---|
| 698 | endpoint=True |
---|
| 699 | ) |
---|
| 700 | xlen= len(x) |
---|
| 701 | y = numpy.zeros(xlen) |
---|
| 702 | if not enable1D: |
---|
| 703 | for i in range(xlen): |
---|
| 704 | y[i] = model.run(x[i]) |
---|
| 705 | |
---|
| 706 | try: |
---|
| 707 | new_plot = Theory1D(x, y) |
---|
| 708 | |
---|
| 709 | new_plot.name = name |
---|
| 710 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 711 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 712 | new_plot.id = "Model" |
---|
| 713 | new_plot.group_id ="Model" |
---|
| 714 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Analytical model 1D")) |
---|
| 715 | except: |
---|
| 716 | wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) |
---|
| 717 | #raise |
---|
| 718 | else: |
---|
| 719 | for i in range(xlen): |
---|
| 720 | y[i] = model.run(x[i]) |
---|
| 721 | #print x, y |
---|
| 722 | try: |
---|
| 723 | new_plot = Theory1D(x, y) |
---|
| 724 | print "draw model 1D", name |
---|
| 725 | new_plot.name = name |
---|
| 726 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 727 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 728 | new_plot.id ="Model" |
---|
| 729 | new_plot.group_id ="Model" |
---|
| 730 | |
---|
| 731 | # Pass the reset flag to let the plotting event handler |
---|
| 732 | # know that we are replacing the whole plot |
---|
| 733 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
| 734 | title="Analytical model 1D ", reset=True )) |
---|
| 735 | |
---|
| 736 | except: |
---|
| 737 | #raise |
---|
| 738 | wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) |
---|
| 739 | def update(self, output,time): |
---|
| 740 | pass |
---|
| 741 | |
---|
| 742 | def complete(self, output, elapsed, model, qmin, qmax,qstep=DEFAULT_NPTS): |
---|
| 743 | |
---|
| 744 | wx.PostEvent(self.parent, StatusEvent(status="Calc \ |
---|
| 745 | complete in %g sec" % elapsed)) |
---|
| 746 | #print "complete",output, model,qmin, qmax |
---|
| 747 | data = output |
---|
| 748 | theory= Data2D(data) |
---|
| 749 | #print data.detector |
---|
| 750 | #theory.detector= data.detector |
---|
| 751 | from DataLoader.data_info import Detector, Source |
---|
| 752 | |
---|
| 753 | detector = Detector() |
---|
| 754 | theory.detector=[] |
---|
| 755 | theory.detector.append(detector) |
---|
| 756 | |
---|
| 757 | theory.detector[0].pixel_size.x= qmax/(qstep/2-1)#5.0 |
---|
| 758 | theory.detector[0].pixel_size.y= qmax/(qstep/2-1)#5.0 |
---|
| 759 | theory.detector[0].beam_center.x= qmax |
---|
| 760 | theory.detector[0].beam_center.y= qmax |
---|
| 761 | theory.detector[0].distance= 1e+12#13705.0 |
---|
| 762 | theory.source= Source() |
---|
| 763 | theory.source.wavelength= 2*math.pi/1e+12#8.4 |
---|
| 764 | |
---|
| 765 | theory.name= model.name |
---|
| 766 | theory.group_id ="Model" |
---|
| 767 | theory.id ="Model" |
---|
| 768 | theory.xmin= -qmax |
---|
| 769 | theory.xmax= qmax |
---|
| 770 | theory.ymin= -qmax |
---|
| 771 | theory.ymax= qmax |
---|
| 772 | |
---|
| 773 | print "model draw comptele xmax",theory.xmax,model.name |
---|
| 774 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
| 775 | title="Analytical model 2D ", reset=True )) |
---|
| 776 | |
---|
| 777 | |
---|
| 778 | |
---|
| 779 | def _draw_model2D(self,model,description=None, enable2D=False, |
---|
| 780 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
| 781 | |
---|
| 782 | x= numpy.linspace(start= -1*qmax, |
---|
| 783 | stop= qmax, |
---|
| 784 | num= qstep, |
---|
| 785 | endpoint=True ) |
---|
| 786 | y = numpy.linspace(start= -1*qmax, |
---|
| 787 | stop= qmax, |
---|
| 788 | num= qstep, |
---|
| 789 | endpoint=True ) |
---|
| 790 | |
---|
| 791 | lx = len(x) |
---|
| 792 | #print x |
---|
| 793 | data=numpy.zeros([len(x),len(y)]) |
---|
| 794 | self.model= model |
---|
| 795 | if enable2D: |
---|
| 796 | from model_thread import Calc2D |
---|
| 797 | self.calc_thread = Calc2D(parent =self.parent,x=x, |
---|
| 798 | y=y,model= self.model, |
---|
| 799 | qmin=qmin, |
---|
| 800 | qmax=qmax, |
---|
| 801 | qstep=qstep, |
---|
| 802 | completefn=self.complete, |
---|
| 803 | updatefn=None) |
---|
| 804 | self.calc_thread.queue() |
---|
| 805 | self.calc_thread.ready(2.5) |
---|
| 806 | |
---|
| 807 | def show_panel2D(self, id=None ): |
---|
| 808 | self.parent.show_panel(self.model2D_id) |
---|
| 809 | |
---|
| 810 | |
---|
| 811 | if __name__ == "__main__": |
---|
| 812 | i = Plugin() |
---|
| 813 | |
---|
| 814 | |
---|
| 815 | |
---|
| 816 | |
---|