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