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