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