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