1 | import re,copy |
---|
2 | import sys, wx, logging |
---|
3 | import string, numpy, math |
---|
4 | |
---|
5 | from danse.common.plottools.plottables import Data2D,Theory1D |
---|
6 | from sans.guiframe import dataFitting |
---|
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,ERR_DATA,EVT_REMOVE_DATA |
---|
10 | from sans.guicomm.events import EVT_SLICER_PARS_UPDATE |
---|
11 | from sans.guiframe import dataFitting |
---|
12 | from sans.fit.AbstractFitEngine import Model |
---|
13 | |
---|
14 | |
---|
15 | from fitproblem import FitProblem |
---|
16 | from fitpanel import FitPanel |
---|
17 | from fit_thread import FitThread |
---|
18 | import models |
---|
19 | import fitpage |
---|
20 | |
---|
21 | |
---|
22 | DEFAULT_BEAM = 0.005 |
---|
23 | DEFAULT_QMIN = 0.0001 |
---|
24 | DEFAULT_QMAX = 0.13 |
---|
25 | DEFAULT_NPTS = 50 |
---|
26 | import time |
---|
27 | import thread |
---|
28 | from sans.fit.AbstractFitEngine import FitAbort |
---|
29 | |
---|
30 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
31 | class PlotInfo: |
---|
32 | """ |
---|
33 | store some plotting field |
---|
34 | """ |
---|
35 | _xunit = 'A^{-1}' |
---|
36 | _xaxis= "\\rm{Q}" |
---|
37 | _yunit = "cm^{-1}" |
---|
38 | _yaxis= "\\rm{Intensity} " |
---|
39 | id = "Model" |
---|
40 | group_id = "Model" |
---|
41 | title= None |
---|
42 | info= None |
---|
43 | |
---|
44 | |
---|
45 | class Plugin: |
---|
46 | """ |
---|
47 | Fitting plugin is used to perform fit |
---|
48 | """ |
---|
49 | def __init__(self): |
---|
50 | ## Plug-in name |
---|
51 | self.sub_menu = "Fitting" |
---|
52 | |
---|
53 | ## Reference to the parent window |
---|
54 | self.parent = None |
---|
55 | #Provide list of models existing in the application |
---|
56 | self.menu_mng = models.ModelManager() |
---|
57 | ## List of panels for the simulation perspective (names) |
---|
58 | self.perspective = [] |
---|
59 | #list of panel to send to guiframe |
---|
60 | self.mypanels=[] |
---|
61 | # reference to the current running thread |
---|
62 | self.calc_2D= None |
---|
63 | self.calc_1D= None |
---|
64 | self.calc_fit= None |
---|
65 | |
---|
66 | # Start with a good default |
---|
67 | self.elapsed = 0.022 |
---|
68 | # the type of optimizer selected, park or scipy |
---|
69 | self.fitter = None |
---|
70 | #Flag to let the plug-in know that it is running stand alone |
---|
71 | self.standalone=True |
---|
72 | ## dictionary of page closed and id |
---|
73 | self.closed_page_dict ={} |
---|
74 | ## Fit engine |
---|
75 | self._fit_engine = 'scipy' |
---|
76 | #List of selected data |
---|
77 | self.selected_data_list=[] |
---|
78 | ## list of slicer panel created to display slicer parameters and results |
---|
79 | self.slicer_panels=[] |
---|
80 | # Log startup |
---|
81 | logging.info("Fitting plug-in started") |
---|
82 | # model 2D view |
---|
83 | self.model2D_id=None |
---|
84 | #keep reference of the simultaneous fit page |
---|
85 | self.sim_page=None |
---|
86 | #dictionary containing data name and error on dy of that data |
---|
87 | self.err_dy={} |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | def populate_menu(self, id, owner): |
---|
92 | """ |
---|
93 | Create a menu for the Fitting plug-in |
---|
94 | @param id: id to create a menu |
---|
95 | @param owner: owner of menu |
---|
96 | @ return : list of information to populate the main menu |
---|
97 | """ |
---|
98 | #Menu for fitting |
---|
99 | self.menu1 = wx.Menu() |
---|
100 | |
---|
101 | #Set park engine |
---|
102 | id3 = wx.NewId() |
---|
103 | scipy_help= "Scipy Engine: Perform Simple fit. More in Help window...." |
---|
104 | self.menu1.AppendCheckItem(id3, "Simple Fit [Scipy]",scipy_help) |
---|
105 | wx.EVT_MENU(owner, id3, self._onset_engine_scipy) |
---|
106 | |
---|
107 | id3 = wx.NewId() |
---|
108 | park_help = "Park Engine: Perform Complex fit. More in Help window...." |
---|
109 | self.menu1.AppendCheckItem(id3, "Complex Fit [Park]",park_help) |
---|
110 | wx.EVT_MENU(owner, id3, self._onset_engine_park) |
---|
111 | |
---|
112 | self.menu1.FindItemByPosition(0).Check(True) |
---|
113 | self.menu1.FindItemByPosition(1).Check(False) |
---|
114 | |
---|
115 | self.menu1.AppendSeparator() |
---|
116 | |
---|
117 | id1 = wx.NewId() |
---|
118 | simul_help = "Allow to edit fit engine with multiple model and data" |
---|
119 | self.menu1.Append(id1, '&Simultaneous Page',simul_help) |
---|
120 | wx.EVT_MENU(owner, id1, self.on_add_sim_page) |
---|
121 | |
---|
122 | #menu for model |
---|
123 | menu2 = wx.Menu() |
---|
124 | |
---|
125 | self.menu_mng.populate_menu(menu2, owner) |
---|
126 | id2 = wx.NewId() |
---|
127 | owner.Bind(models.EVT_MODEL,self._on_model_menu) |
---|
128 | |
---|
129 | self.fit_panel.set_owner(owner) |
---|
130 | self.fit_panel.set_model_list(self.menu_mng.get_model_list()) |
---|
131 | owner.Bind(fitpage.EVT_MODEL_BOX,self._on_model_panel) |
---|
132 | |
---|
133 | #create menubar items |
---|
134 | return [(id, self.menu1, "Fitting"), |
---|
135 | (id2, menu2, "Model")] |
---|
136 | |
---|
137 | |
---|
138 | def on_add_sim_page(self, event): |
---|
139 | """ |
---|
140 | Create a page to access simultaneous fit option |
---|
141 | """ |
---|
142 | Plugin.on_perspective(self,event=event) |
---|
143 | if self.sim_page !=None: |
---|
144 | msg= "Simultaneous Fit page already opened" |
---|
145 | wx.PostEvent(self.parent, StatusEvent(status= msg)) |
---|
146 | return |
---|
147 | |
---|
148 | self.sim_page= self.fit_panel.add_sim_page() |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | def help(self, evt): |
---|
153 | """ |
---|
154 | Show a general help dialog. |
---|
155 | TODO: replace the text with a nice image |
---|
156 | """ |
---|
157 | from helpPanel import HelpWindow |
---|
158 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
159 | frame.Show(True) |
---|
160 | |
---|
161 | |
---|
162 | def get_context_menu(self, graph=None): |
---|
163 | """ |
---|
164 | Get the context menu items available for P(r) |
---|
165 | @param graph: the Graph object to which we attach the context menu |
---|
166 | @return: a list of menu items with call-back function |
---|
167 | """ |
---|
168 | #TODO: clean this up so that the string are not copied |
---|
169 | # multiple times. |
---|
170 | self.graph=graph |
---|
171 | for item in graph.plottables: |
---|
172 | if item.__class__.__name__ is "Data2D": |
---|
173 | if hasattr(item,"is_data"): |
---|
174 | if item.is_data: |
---|
175 | return [["Select data for fitting", \ |
---|
176 | "Dialog with fitting parameters ", self._onSelect]] |
---|
177 | else: |
---|
178 | return [] |
---|
179 | return [["Select data for fitting",\ |
---|
180 | "Dialog with fitting parameters ", self._onSelect]] |
---|
181 | else: |
---|
182 | if item.name==graph.selected_plottable : |
---|
183 | if item.name !="$I_{obs}(q)$" and item.name !="$P_{fit}(r)$": |
---|
184 | if hasattr(item, "group_id"): |
---|
185 | if hasattr(item,"is_data"): |
---|
186 | if item.is_data: |
---|
187 | return [["Select data for fitting", \ |
---|
188 | "Dialog with fitting parameters ", self._onSelect]] |
---|
189 | else: |
---|
190 | return [] |
---|
191 | else: |
---|
192 | return [["Select data for fitting", \ |
---|
193 | "Dialog with fitting parameters ", self._onSelect]] |
---|
194 | return [] |
---|
195 | |
---|
196 | |
---|
197 | def get_panels(self, parent): |
---|
198 | """ |
---|
199 | Create and return a list of panel objects |
---|
200 | """ |
---|
201 | self.parent = parent |
---|
202 | # Creation of the fit panel |
---|
203 | self.fit_panel = FitPanel(self.parent, -1) |
---|
204 | #Set the manager for the main panel |
---|
205 | self.fit_panel.set_manager(self) |
---|
206 | # List of windows used for the perspective |
---|
207 | self.perspective = [] |
---|
208 | self.perspective.append(self.fit_panel.window_name) |
---|
209 | # take care of saving data, model and page associated with each other |
---|
210 | self.page_finder = {} |
---|
211 | #index number to create random model name |
---|
212 | self.index_model = 0 |
---|
213 | self.index_theory= 0 |
---|
214 | self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) |
---|
215 | self.parent.Bind( ERR_DATA, self._on_data_error) |
---|
216 | self.parent.Bind(EVT_REMOVE_DATA, self._closed_fitpage) |
---|
217 | self.parent.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) |
---|
218 | self.parent._mgr.Bind(wx.aui.EVT_AUI_PANE_CLOSE,self._onclearslicer) |
---|
219 | |
---|
220 | |
---|
221 | #Send the fitting panel to guiframe |
---|
222 | self.mypanels.append(self.fit_panel) |
---|
223 | return self.mypanels |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | def get_perspective(self): |
---|
228 | """ |
---|
229 | Get the list of panel names for this perspective |
---|
230 | """ |
---|
231 | return self.perspective |
---|
232 | |
---|
233 | |
---|
234 | def on_perspective(self, event): |
---|
235 | """ |
---|
236 | Call back function for the perspective menu item. |
---|
237 | We notify the parent window that the perspective |
---|
238 | has changed. |
---|
239 | """ |
---|
240 | self.parent.set_perspective(self.perspective) |
---|
241 | |
---|
242 | |
---|
243 | def post_init(self): |
---|
244 | """ |
---|
245 | Post initialization call back to close the loose ends |
---|
246 | [Somehow openGL needs this call] |
---|
247 | """ |
---|
248 | self.parent.set_perspective(self.perspective) |
---|
249 | |
---|
250 | |
---|
251 | def copy_data(self, item, dy=None): |
---|
252 | """ |
---|
253 | receive a data 1D and the list of errors on dy |
---|
254 | and create a new data1D data |
---|
255 | @param return |
---|
256 | """ |
---|
257 | detector=None |
---|
258 | source=None |
---|
259 | info = None |
---|
260 | id=None |
---|
261 | dxl=None |
---|
262 | dxw=None |
---|
263 | dx=None |
---|
264 | if hasattr(item, "dxl"): |
---|
265 | dxl = copy.deepcopy(item.dxl) |
---|
266 | if hasattr(item, "dxw"): |
---|
267 | dxw = copy.deepcopy(item.dxw) |
---|
268 | if hasattr(item, "detector"): |
---|
269 | detector = copy.deepcopy(item.detector) |
---|
270 | if hasattr(item, "source"): |
---|
271 | source = copy.deepcopy(item.source) |
---|
272 | if hasattr(item ,"info"): |
---|
273 | info= copy.deepcopy(item.info) |
---|
274 | if hasattr(item,"id"): |
---|
275 | id = copy.deepcopy(item.id) |
---|
276 | if hasattr(item, "dx"): |
---|
277 | dx= item.dx |
---|
278 | |
---|
279 | |
---|
280 | data= dataFitting.Data1D(x=item.x, y=item.y,dx=dx, dy=dy, dxl=dxl, dxw=dxw) |
---|
281 | |
---|
282 | data.name = item.name |
---|
283 | data.detector = detector |
---|
284 | data.source = source |
---|
285 | ## allow to highlight data when plotted |
---|
286 | data.interactive = copy.deepcopy(item.interactive) |
---|
287 | ## when 2 data have the same id override the 1 st plotted |
---|
288 | data.id = id |
---|
289 | ## info is a reference to output of dataloader that can be used |
---|
290 | ## to save data 1D as cansas xml file |
---|
291 | data.info= info |
---|
292 | ## If the data file does not tell us what the axes are, just assume... |
---|
293 | data.xaxis(copy.deepcopy(item._xaxis),copy.deepcopy(item._xunit)) |
---|
294 | data.yaxis(copy.deepcopy(item._yaxis),copy.deepcopy(item._yunit)) |
---|
295 | ##group_id specify on which panel to plot this data |
---|
296 | data.group_id = copy.deepcopy(item.group_id) |
---|
297 | return data |
---|
298 | |
---|
299 | def set_fit_range(self, page, qmin, qmax): |
---|
300 | """ |
---|
301 | Set the fitting range of a given page |
---|
302 | """ |
---|
303 | if page in self.page_finder.iterkeys(): |
---|
304 | fitproblem= self.page_finder[page] |
---|
305 | fitproblem.set_range(qmin= qmin, qmax= qmax) |
---|
306 | |
---|
307 | def schedule_for_fit(self,value=0,page=None,fitproblem =None): |
---|
308 | """ |
---|
309 | Set the fit problem field to 0 or 1 to schedule that problem to fit. |
---|
310 | Schedule the specified fitproblem or get the fit problem related to |
---|
311 | the current page and set value. |
---|
312 | @param value : integer 0 or 1 |
---|
313 | @param fitproblem: fitproblem to schedule or not to fit |
---|
314 | """ |
---|
315 | if fitproblem !=None: |
---|
316 | fitproblem.schedule_tofit(value) |
---|
317 | else: |
---|
318 | if page in self.page_finder.iterkeys(): |
---|
319 | fitproblem= self.page_finder[page] |
---|
320 | fitproblem.schedule_tofit(value) |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | def get_page_finder(self): |
---|
325 | """ @return self.page_finder used also by simfitpage.py""" |
---|
326 | return self.page_finder |
---|
327 | |
---|
328 | |
---|
329 | def set_page_finder(self,modelname,names,values): |
---|
330 | """ |
---|
331 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
332 | @param modelname: the name ot the model for with the parameter has to reset |
---|
333 | @param value: can be a string in this case. |
---|
334 | @param names: the paramter name |
---|
335 | @note: expecting park used for fit. |
---|
336 | """ |
---|
337 | sim_page= self.sim_page |
---|
338 | for page, value in self.page_finder.iteritems(): |
---|
339 | if page != sim_page: |
---|
340 | list=value.get_model() |
---|
341 | model = list[0] |
---|
342 | if model.name== modelname: |
---|
343 | value.set_model_param(names,values) |
---|
344 | break |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | def split_string(self,item): |
---|
349 | """ |
---|
350 | receive a word containing dot and split it. used to split parameterset |
---|
351 | name into model name and parameter name example: |
---|
352 | paramaterset (item) = M1.A |
---|
353 | @return model_name =M1 , parameter name =A |
---|
354 | """ |
---|
355 | if string.find(item,".")!=-1: |
---|
356 | param_names= re.split("\.",item) |
---|
357 | model_name=param_names[0] |
---|
358 | param_name=param_names[1] |
---|
359 | return model_name,param_name |
---|
360 | |
---|
361 | |
---|
362 | def stop_fit(self): |
---|
363 | """ |
---|
364 | Stop the fit engine |
---|
365 | """ |
---|
366 | if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
367 | self.calc_fit.stop() |
---|
368 | wx.PostEvent(self.parent, StatusEvent(status="Fitting \ |
---|
369 | is cancelled" , type="stop")) |
---|
370 | |
---|
371 | |
---|
372 | |
---|
373 | def set_smearer(self,smearer, qmin=None, qmax=None): |
---|
374 | """ |
---|
375 | Get a smear object and store it to a fit problem |
---|
376 | @param smearer: smear object to allow smearing data |
---|
377 | """ |
---|
378 | current_pg=self.fit_panel.get_current_page() |
---|
379 | self.page_finder[current_pg].set_smearer(smearer) |
---|
380 | ## draw model 1D with smeared data |
---|
381 | data = self.page_finder[current_pg].get_plotted_data() |
---|
382 | model = self.page_finder[current_pg].get_model() |
---|
383 | ## if user has already selected a model to plot |
---|
384 | ## redraw the model with data smeared |
---|
385 | |
---|
386 | smear =self.page_finder[current_pg].get_smearer() |
---|
387 | self.draw_model( model=model, data= data, smearer= smear, |
---|
388 | qmin= qmin, qmax= qmax) |
---|
389 | |
---|
390 | |
---|
391 | |
---|
392 | def draw_model(self, model, data= None,smearer= None, |
---|
393 | enable1D= True, enable2D= False, |
---|
394 | qmin= DEFAULT_QMIN, qmax= DEFAULT_QMAX, qstep= DEFAULT_NPTS): |
---|
395 | """ |
---|
396 | Draw model. |
---|
397 | @param model: the model to draw |
---|
398 | @param name: the name of the model to draw |
---|
399 | @param data: the data on which the model is based to be drawn |
---|
400 | @param description: model's description |
---|
401 | @param enable1D: if true enable drawing model 1D |
---|
402 | @param enable2D: if true enable drawing model 2D |
---|
403 | @param qmin: Range's minimum value to draw model |
---|
404 | @param qmax: Range's maximum value to draw model |
---|
405 | @param qstep: number of step to divide the x and y-axis |
---|
406 | |
---|
407 | """ |
---|
408 | ## draw model 1D with no loaded data |
---|
409 | self._draw_model1D( model= model, data= data,enable1D=enable1D, smearer= smearer, |
---|
410 | qmin= qmin, qmax= qmax, qstep= qstep ) |
---|
411 | ## draw model 2D with no initial data |
---|
412 | self._draw_model2D(model=model, |
---|
413 | data = data, |
---|
414 | enable2D= enable2D, |
---|
415 | qmin=qmin, |
---|
416 | qmax=qmax, |
---|
417 | qstep=qstep) |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | def onFit(self): |
---|
422 | """ |
---|
423 | perform fit |
---|
424 | """ |
---|
425 | ## count the number of fitproblem schedule to fit |
---|
426 | fitproblem_count= 0 |
---|
427 | for value in self.page_finder.itervalues(): |
---|
428 | if value.get_scheduled()==1: |
---|
429 | fitproblem_count += 1 |
---|
430 | |
---|
431 | ## if simultaneous fit change automatically the engine to park |
---|
432 | if fitproblem_count >1: |
---|
433 | self._on_change_engine(engine='park') |
---|
434 | |
---|
435 | from sans.fit.Fitting import Fit |
---|
436 | self.fitter= Fit(self._fit_engine) |
---|
437 | |
---|
438 | if self._fit_engine=="park": |
---|
439 | engineType="Simultaneous Fit" |
---|
440 | else: |
---|
441 | engineType="Single Fit" |
---|
442 | |
---|
443 | fproblemId = 0 |
---|
444 | current_pg=None |
---|
445 | for page, value in self.page_finder.iteritems(): |
---|
446 | try: |
---|
447 | if value.get_scheduled()==1: |
---|
448 | #Get list of parameters name to fit |
---|
449 | pars = [] |
---|
450 | templist = [] |
---|
451 | templist = page.get_param_list() |
---|
452 | for element in templist: |
---|
453 | name = str(element[1]) |
---|
454 | pars.append(name) |
---|
455 | #Set Engine (model , data) related to the page on |
---|
456 | self._fit_helper( current_pg=page, value=value,pars=pars, |
---|
457 | id=fproblemId, title= engineType ) |
---|
458 | fproblemId += 1 |
---|
459 | current_pg= page |
---|
460 | except: |
---|
461 | msg= "%s error: %s" % (engineType,sys.exc_value) |
---|
462 | wx.PostEvent(self.parent, StatusEvent(status= msg )) |
---|
463 | return |
---|
464 | #Do the simultaneous fit |
---|
465 | try: |
---|
466 | ## If a thread is already started, stop it |
---|
467 | if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
468 | self.calc_fit.stop() |
---|
469 | |
---|
470 | wx.PostEvent(self.parent, StatusEvent(status="Start the computation", |
---|
471 | curr_thread=self.calc_fit,type="start")) |
---|
472 | wx.PostEvent(self.parent, StatusEvent(status="Computing...", |
---|
473 | curr_thread=self.calc_fit,type="progress")) |
---|
474 | ## perform single fit |
---|
475 | if self._fit_engine=="scipy": |
---|
476 | qmin, qmax= current_pg.get_range() |
---|
477 | self.calc_fit=FitThread(parent =self.parent, |
---|
478 | fn= self.fitter, |
---|
479 | cpage=current_pg, |
---|
480 | pars= pars, |
---|
481 | completefn= self._single_fit_completed, |
---|
482 | updatefn=self._updateFit) |
---|
483 | |
---|
484 | else: |
---|
485 | ## Perform more than 1 fit at the time |
---|
486 | self.calc_fit=FitThread(parent =self.parent, |
---|
487 | fn= self.fitter, |
---|
488 | completefn= self._simul_fit_completed, |
---|
489 | updatefn=self._updateFit) |
---|
490 | self.calc_fit.queue() |
---|
491 | self.calc_fit.ready(2.5) |
---|
492 | |
---|
493 | except FitAbort: |
---|
494 | print "in pluging" |
---|
495 | except: |
---|
496 | msg= "%s error: %s" % (engineType,sys.exc_value) |
---|
497 | wx.PostEvent(self.parent, StatusEvent(status= msg ,type="stop")) |
---|
498 | return |
---|
499 | |
---|
500 | |
---|
501 | |
---|
502 | def _onEVT_SLICER_PANEL(self, event): |
---|
503 | """ |
---|
504 | receive and event telling to update a panel with a name starting with |
---|
505 | event.panel_name. this method update slicer panel for a given interactor. |
---|
506 | @param event: contains type of slicer , paramaters for updating the panel |
---|
507 | and panel_name to find the slicer 's panel concerned. |
---|
508 | """ |
---|
509 | |
---|
510 | for item in self.parent.panels: |
---|
511 | if self.parent.panels[item].window_caption.startswith(event.panel_name): |
---|
512 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
513 | |
---|
514 | self.parent._mgr.Update() |
---|
515 | |
---|
516 | |
---|
517 | |
---|
518 | def _closed_fitpage(self, event): |
---|
519 | """ |
---|
520 | request fitpanel to close a given page when its unique data is removed |
---|
521 | from the plot |
---|
522 | """ |
---|
523 | self.fit_panel._close_fitpage(event.data) |
---|
524 | |
---|
525 | |
---|
526 | def _add_page_onmenu(self, name,fitproblem=None): |
---|
527 | """ |
---|
528 | Add name of a closed page of fitpanel in a menu |
---|
529 | """ |
---|
530 | list = self.menu1.GetMenuItems() |
---|
531 | for item in list: |
---|
532 | if name == item.GetItemLabel(): |
---|
533 | self.closed_page_dict[name][1] = fitproblem |
---|
534 | |
---|
535 | if not name in self.closed_page_dict.keys(): |
---|
536 | # Post paramters |
---|
537 | event_id = wx.NewId() |
---|
538 | self.menu1.Append(event_id, name, "Show %s fit panel" % name) |
---|
539 | self.closed_page_dict[name]= [event_id, fitproblem] |
---|
540 | wx.EVT_MENU(self.parent,event_id, self._open_closed_page) |
---|
541 | |
---|
542 | |
---|
543 | def _open_closed_page(self, event): |
---|
544 | """ |
---|
545 | reopen a closed page |
---|
546 | """ |
---|
547 | for name, value in self.closed_page_dict.iteritems(): |
---|
548 | if event.GetId() in value: |
---|
549 | id,fitproblem = value |
---|
550 | if name !="Model": |
---|
551 | data= fitproblem.get_fit_data() |
---|
552 | page = self.fit_panel.add_fit_page(data= data,reset=True) |
---|
553 | if fitproblem != None: |
---|
554 | self.page_finder[page]=fitproblem |
---|
555 | if self.sim_page != None: |
---|
556 | self.sim_page.draw_page() |
---|
557 | |
---|
558 | else: |
---|
559 | model = fitproblem |
---|
560 | self.fit_panel.add_model_page(model=model, topmenu=True, |
---|
561 | reset= True) |
---|
562 | break |
---|
563 | |
---|
564 | |
---|
565 | def _reset_schedule_problem(self, value=0): |
---|
566 | """ |
---|
567 | unschedule or schedule all fitproblem to be fit |
---|
568 | """ |
---|
569 | for page, fitproblem in self.page_finder.iteritems(): |
---|
570 | fitproblem.schedule_tofit(value) |
---|
571 | |
---|
572 | def _fit_helper(self,current_pg,pars,value, id, title="Single Fit " ): |
---|
573 | """ |
---|
574 | helper for fitting |
---|
575 | """ |
---|
576 | metadata = value.get_fit_data() |
---|
577 | model = value.get_model() |
---|
578 | smearer = value.get_smearer() |
---|
579 | qmin , qmax = value.get_range() |
---|
580 | self.fit_id =id |
---|
581 | #Create list of parameters for fitting used |
---|
582 | templist=[] |
---|
583 | pars=pars |
---|
584 | try: |
---|
585 | ## create a park model and reset parameter value if constraint |
---|
586 | ## is given |
---|
587 | new_model = Model(model) |
---|
588 | param = value.get_model_param() |
---|
589 | if len(param)>0: |
---|
590 | for item in param: |
---|
591 | param_value = item[1] |
---|
592 | param_name = item[0] |
---|
593 | ## check if constraint |
---|
594 | if param_value !=None and param_name != None: |
---|
595 | new_model.parameterset[ param_name].set( param_value ) |
---|
596 | |
---|
597 | #Do the single fit |
---|
598 | self.fitter.set_model(new_model, self.fit_id, pars) |
---|
599 | |
---|
600 | self.fitter.set_data(data=metadata,Uid=self.fit_id, |
---|
601 | smearer=smearer,qmin= qmin,qmax=qmax ) |
---|
602 | |
---|
603 | self.fitter.select_problem_for_fit(Uid= self.fit_id, |
---|
604 | value= value.get_scheduled()) |
---|
605 | value.clear_model_param() |
---|
606 | except: |
---|
607 | msg= title +" error: %s" % sys.exc_value |
---|
608 | wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop")) |
---|
609 | return |
---|
610 | |
---|
611 | def _onSelect(self,event): |
---|
612 | """ |
---|
613 | when Select data to fit a new page is created .Its reference is |
---|
614 | added to self.page_finder |
---|
615 | """ |
---|
616 | self.panel = event.GetEventObject() |
---|
617 | Plugin.on_perspective(self,event=event) |
---|
618 | for plottable in self.panel.graph.plottables: |
---|
619 | if plottable.name == self.panel.graph.selected_plottable: |
---|
620 | #if not hasattr(plottable, "is_data"): |
---|
621 | |
---|
622 | if plottable.__class__.__name__=="Theory1D": |
---|
623 | dy=numpy.zeros(len(plottable.y)) |
---|
624 | if hasattr(plottable, "dy"): |
---|
625 | dy= copy.deepcopy(plottable.dy) |
---|
626 | |
---|
627 | item= self.copy_data(plottable, dy) |
---|
628 | item.group_id += "data1D" |
---|
629 | item.id +="data1D" |
---|
630 | item.is_data= False |
---|
631 | title = item.name |
---|
632 | wx.PostEvent(self.parent, NewPlotEvent(plot=item, title=str(title))) |
---|
633 | else: |
---|
634 | item= self.copy_data(plottable, plottable.dy) |
---|
635 | item.is_data=True |
---|
636 | |
---|
637 | ## put the errors values back to the model if the errors were hiden |
---|
638 | ## before sending them to the fit engine |
---|
639 | if len(self.err_dy)>0: |
---|
640 | if item.name in self.err_dy.iterkeys(): |
---|
641 | dy= self.err_dy[item.name] |
---|
642 | data= self.copy_data(item, dy) |
---|
643 | data.is_data= item.is_data |
---|
644 | else: |
---|
645 | data= self.copy_data(item, item.dy) |
---|
646 | data.is_data= item.is_data |
---|
647 | |
---|
648 | |
---|
649 | else: |
---|
650 | if item.dy==None: |
---|
651 | dy= numpy.zeros(len(item.y)) |
---|
652 | data= self.copy_data(item, dy) |
---|
653 | data.is_data=item.is_data |
---|
654 | else: |
---|
655 | data= self.copy_data(item,item.dy) |
---|
656 | data.is_data=item.is_data |
---|
657 | |
---|
658 | else: |
---|
659 | if plottable.__class__.__name__ !="Data2D": |
---|
660 | return |
---|
661 | ## Data2D case |
---|
662 | if not hasattr(plottable, "is_data"): |
---|
663 | item= copy.deepcopy(plottable) |
---|
664 | item.group_id += "data2D" |
---|
665 | item.id +="data2D" |
---|
666 | item.is_data= False |
---|
667 | title = item.name |
---|
668 | title += " Fit" |
---|
669 | data = item |
---|
670 | wx.PostEvent(self.parent, NewPlotEvent(plot=item, title=str(title))) |
---|
671 | else: |
---|
672 | item= copy.deepcopy(plottable ) |
---|
673 | data= copy.deepcopy(plottable ) |
---|
674 | item.is_data=True |
---|
675 | data.is_data=True |
---|
676 | |
---|
677 | ## create anew page |
---|
678 | if item.name == self.panel.graph.selected_plottable or\ |
---|
679 | item.__class__.__name__ is "Data2D": |
---|
680 | try: |
---|
681 | page = self.fit_panel.add_fit_page(data) |
---|
682 | # add data associated to the page created |
---|
683 | if page !=None: |
---|
684 | #create a fitproblem storing all link to data,model,page creation |
---|
685 | if not page in self.page_finder.keys(): |
---|
686 | self.page_finder[page]= FitProblem() |
---|
687 | ## item is almost the same as data but contains |
---|
688 | ## axis info for plotting |
---|
689 | self.page_finder[page].add_plotted_data(item) |
---|
690 | self.page_finder[page].add_fit_data(data) |
---|
691 | |
---|
692 | wx.PostEvent(self.parent, StatusEvent(status="Page Created")) |
---|
693 | else: |
---|
694 | wx.PostEvent(self.parent, StatusEvent(status="Page was already Created")) |
---|
695 | except: |
---|
696 | wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ |
---|
697 | %sys.exc_value)) |
---|
698 | return |
---|
699 | |
---|
700 | |
---|
701 | def _updateFit(self): |
---|
702 | """ |
---|
703 | Is called when values of result are available |
---|
704 | """ |
---|
705 | ##Sending a progess message to the status bar |
---|
706 | wx.PostEvent(self.parent, StatusEvent(status="Computing...")) |
---|
707 | |
---|
708 | def _single_fit_completed(self,result,pars,cpage, elapsed=None): |
---|
709 | """ |
---|
710 | Display fit result on one page of the notebook. |
---|
711 | @param result: result of fit |
---|
712 | @param pars: list of names of parameters fitted |
---|
713 | @param current_pg: the page where information will be displayed |
---|
714 | @param qmin: the minimum value of x to replot the model |
---|
715 | @param qmax: the maximum value of x to replot model |
---|
716 | |
---|
717 | """ |
---|
718 | wx.PostEvent(self.parent, StatusEvent(status="Single fit \ |
---|
719 | complete! " )) |
---|
720 | |
---|
721 | try: |
---|
722 | if result ==None: |
---|
723 | msg= "Simple Fitting Stop !!!" |
---|
724 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
725 | return |
---|
726 | if numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
727 | msg= "Single Fitting did not converge!!!" |
---|
728 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
729 | return |
---|
730 | for page, value in self.page_finder.iteritems(): |
---|
731 | if page==cpage : |
---|
732 | model= value.get_model() |
---|
733 | break |
---|
734 | i = 0 |
---|
735 | for name in pars: |
---|
736 | if result.pvec.__class__==numpy.float64: |
---|
737 | model.setParam(name,result.pvec) |
---|
738 | else: |
---|
739 | model.setParam(name,result.pvec[i]) |
---|
740 | i += 1 |
---|
741 | ## Reset values of the current page to fit result |
---|
742 | cpage.onsetValues(result.fitness, result.pvec,result.stderr) |
---|
743 | ## plot the current model with new param |
---|
744 | metadata = self.page_finder[cpage].get_fit_data() |
---|
745 | model = self.page_finder[cpage].get_model() |
---|
746 | qmin, qmax= self.page_finder[cpage].get_range() |
---|
747 | smearer =self.page_finder[cpage].get_smearer() |
---|
748 | #Replot models |
---|
749 | msg= "Single Fit completed. plotting... %s:"%model.name |
---|
750 | wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) |
---|
751 | self.draw_model( model=model, data= metadata, smearer= smearer, |
---|
752 | qmin= qmin, qmax= qmax) |
---|
753 | wx.PostEvent(self.parent, StatusEvent(status=" " , type="stop")) |
---|
754 | except: |
---|
755 | msg= "Single Fit completed but Following error occurred:%s"% sys.exc_value |
---|
756 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
757 | return |
---|
758 | |
---|
759 | |
---|
760 | def _simul_fit_completed(self,result,pars=None,cpage=None, elapsed=None): |
---|
761 | """ |
---|
762 | Parameter estimation completed, |
---|
763 | display the results to the user |
---|
764 | @param alpha: estimated best alpha |
---|
765 | @param elapsed: computation time |
---|
766 | """ |
---|
767 | wx.PostEvent(self.parent, StatusEvent(status="Simultaneous fit complete ")) |
---|
768 | |
---|
769 | ## fit more than 1 model at the same time |
---|
770 | try: |
---|
771 | if result ==None: |
---|
772 | msg= "Complex Fitting Stop !!!" |
---|
773 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
774 | return |
---|
775 | if numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
776 | msg= "Single Fitting did not converge!!!" |
---|
777 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
778 | return |
---|
779 | for page, value in self.page_finder.iteritems(): |
---|
780 | if value.get_scheduled()==1: |
---|
781 | model = value.get_model() |
---|
782 | metadata = value.get_plotted_data() |
---|
783 | small_out = [] |
---|
784 | small_cov = [] |
---|
785 | i = 0 |
---|
786 | #Separate result in to data corresponding to each page |
---|
787 | for p in result.parameters: |
---|
788 | model_name,param_name = self.split_string(p.name) |
---|
789 | if model.name == model_name: |
---|
790 | p_name= model.name+"."+param_name |
---|
791 | if p.name == p_name: |
---|
792 | small_out.append(p.value ) |
---|
793 | model.setParam(param_name,p.value) |
---|
794 | |
---|
795 | small_cov.append(p.stderr) |
---|
796 | else: |
---|
797 | value= model.getParam(param_name) |
---|
798 | small_out.append(value ) |
---|
799 | small_cov.append(None) |
---|
800 | # Display result on each page |
---|
801 | page.onsetValues(result.fitness, small_out,small_cov) |
---|
802 | #Replot models |
---|
803 | msg= "Simultaneous Fit completed. plotting... %s:"%model.name |
---|
804 | wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) |
---|
805 | qmin, qmax= page.get_range() |
---|
806 | smearer =self.page_finder[page].get_smearer() |
---|
807 | self.draw_model( model=model, data= metadata, smearer=smearer, |
---|
808 | qmin= qmin, qmax= qmax) |
---|
809 | wx.PostEvent(self.parent, StatusEvent(status="", type="stop")) |
---|
810 | except: |
---|
811 | msg= "Simultaneous Fit completed" |
---|
812 | msg +=" but Following error occurred:%s"%sys.exc_value |
---|
813 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
814 | return |
---|
815 | |
---|
816 | |
---|
817 | |
---|
818 | def _on_show_panel(self, event): |
---|
819 | print "_on_show_panel: fitting" |
---|
820 | |
---|
821 | |
---|
822 | def _onset_engine_park(self,event): |
---|
823 | """ |
---|
824 | set engine to park |
---|
825 | """ |
---|
826 | self._on_change_engine('park') |
---|
827 | |
---|
828 | |
---|
829 | def _onset_engine_scipy(self,event): |
---|
830 | """ |
---|
831 | set engine to scipy |
---|
832 | """ |
---|
833 | self._on_change_engine('scipy') |
---|
834 | |
---|
835 | def _on_slicer_event(self, event): |
---|
836 | """ |
---|
837 | Receive a panel as event and send it to guiframe |
---|
838 | @param event: event containing a panel |
---|
839 | """ |
---|
840 | |
---|
841 | if event.panel!=None: |
---|
842 | new_panel = event.panel |
---|
843 | self.slicer_panels.append(event.panel) |
---|
844 | # Set group ID if available |
---|
845 | event_id = self.parent.popup_panel(new_panel) |
---|
846 | #self.menu3.Append(event_id, new_panel.window_caption, |
---|
847 | # "Show %s plot panel" % new_panel.window_caption) |
---|
848 | # Set UID to allow us to reference the panel later |
---|
849 | |
---|
850 | new_panel.uid = event_id |
---|
851 | self.mypanels.append(new_panel) |
---|
852 | return |
---|
853 | |
---|
854 | def _onclearslicer(self, event): |
---|
855 | """ |
---|
856 | Clear the boxslicer when close the panel associate with this slicer |
---|
857 | """ |
---|
858 | name =event.GetPane().caption |
---|
859 | |
---|
860 | for panel in self.slicer_panels: |
---|
861 | if panel.window_caption==name: |
---|
862 | |
---|
863 | for item in self.parent.panels: |
---|
864 | if hasattr(self.parent.panels[item],"uid"): |
---|
865 | if self.parent.panels[item].uid ==panel.base.uid: |
---|
866 | self.parent.panels[item].onClearSlicer(event) |
---|
867 | self.parent._mgr.Update() |
---|
868 | break |
---|
869 | break |
---|
870 | |
---|
871 | |
---|
872 | |
---|
873 | |
---|
874 | def _return_engine_type(self): |
---|
875 | """ |
---|
876 | return the current type of engine |
---|
877 | """ |
---|
878 | return self._fit_engine |
---|
879 | |
---|
880 | |
---|
881 | def _on_change_engine(self, engine='park'): |
---|
882 | """ |
---|
883 | Allow to select the type of engine to perform fit |
---|
884 | @param engine: the key work of the engine |
---|
885 | """ |
---|
886 | ## saving fit engine name |
---|
887 | self._fit_engine = engine |
---|
888 | ## change menu item state |
---|
889 | if engine=="park": |
---|
890 | self.menu1.FindItemByPosition(0).Check(False) |
---|
891 | self.menu1.FindItemByPosition(1).Check(True) |
---|
892 | else: |
---|
893 | self.menu1.FindItemByPosition(0).Check(True) |
---|
894 | self.menu1.FindItemByPosition(1).Check(False) |
---|
895 | |
---|
896 | ## post a message to status bar |
---|
897 | wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) |
---|
898 | |
---|
899 | ## Bind every open fit page with a newevent to know the current fitting engine |
---|
900 | import fitpage |
---|
901 | event= fitpage.FitterTypeEvent() |
---|
902 | event.type = self._fit_engine |
---|
903 | for key in self.page_finder.keys(): |
---|
904 | wx.PostEvent(key, event) |
---|
905 | |
---|
906 | |
---|
907 | def _on_model_panel(self, evt): |
---|
908 | """ |
---|
909 | react to model selection on any combo box or model menu.plot the model |
---|
910 | @param evt: wx.combobox event |
---|
911 | """ |
---|
912 | model = evt.model |
---|
913 | |
---|
914 | if model ==None: |
---|
915 | return |
---|
916 | |
---|
917 | current_pg = self.fit_panel.get_current_page() |
---|
918 | ## make sure nothing is done on self.sim_page |
---|
919 | ## example trying to call set_panel on self.sim_page |
---|
920 | if current_pg != self.sim_page : |
---|
921 | |
---|
922 | if self.page_finder[current_pg].get_model()== None : |
---|
923 | |
---|
924 | model.name="M"+str(self.index_model) |
---|
925 | self.index_model += 1 |
---|
926 | else: |
---|
927 | model.name= self.page_finder[current_pg].get_model().name |
---|
928 | |
---|
929 | metadata = self.page_finder[current_pg].get_plotted_data() |
---|
930 | |
---|
931 | # save the name containing the data name with the appropriate model |
---|
932 | self.page_finder[current_pg].set_model(model) |
---|
933 | qmin, qmax= current_pg.get_range() |
---|
934 | self.page_finder[current_pg].set_range(qmin=qmin, qmax=qmax) |
---|
935 | smearer= self.page_finder[current_pg].get_smearer() |
---|
936 | # save model name |
---|
937 | self.draw_model( model=model,smearer=smearer, |
---|
938 | data= metadata, qmin=qmin, qmax=qmax) |
---|
939 | |
---|
940 | if self.sim_page!=None: |
---|
941 | self.sim_page.draw_page() |
---|
942 | |
---|
943 | |
---|
944 | |
---|
945 | def _on_model_menu(self, evt): |
---|
946 | """ |
---|
947 | Plot a theory from a model selected from the menu |
---|
948 | @param evt: wx.menu event |
---|
949 | """ |
---|
950 | model = evt.model |
---|
951 | Plugin.on_perspective(self,event=evt) |
---|
952 | # Create a model page. If a new page is created, the model |
---|
953 | # will be plotted automatically. If a page already exists, |
---|
954 | # the content will be updated and the plot refreshed |
---|
955 | self.fit_panel.add_model_page(model,topmenu=True) |
---|
956 | |
---|
957 | |
---|
958 | |
---|
959 | |
---|
960 | def _update1D(self,x, output): |
---|
961 | """ |
---|
962 | Update the output of plotting model 1D |
---|
963 | """ |
---|
964 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
965 | #updating ... ",type="update")) |
---|
966 | self.calc_thread.ready(0.01) |
---|
967 | |
---|
968 | |
---|
969 | def _fill_default_model2D(self, theory, qmax,qstep, qmin=None): |
---|
970 | """ |
---|
971 | fill Data2D with default value |
---|
972 | @param theory: Data2D to fill |
---|
973 | """ |
---|
974 | from DataLoader.data_info import Detector, Source |
---|
975 | |
---|
976 | detector = Detector() |
---|
977 | theory.detector.append(detector) |
---|
978 | |
---|
979 | theory.detector[0].distance=1e+32 |
---|
980 | theory.source= Source() |
---|
981 | theory.source.wavelength=2*math.pi/1e+32 |
---|
982 | |
---|
983 | ## Create detector for Model 2D |
---|
984 | xmax=2*theory.detector[0].distance*math.atan(\ |
---|
985 | qmax/(4*math.pi/theory.source.wavelength)) |
---|
986 | |
---|
987 | theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5) |
---|
988 | theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5) |
---|
989 | theory.detector[0].beam_center.x= qmax |
---|
990 | theory.detector[0].beam_center.y= qmax |
---|
991 | ## create x_bins and y_bins of the model 2D |
---|
992 | distance = theory.detector[0].distance |
---|
993 | pixel = qstep/2-1 |
---|
994 | theta = pixel / distance / qstep#100.0 |
---|
995 | wavelength = theory.source.wavelength |
---|
996 | pixel_width_x = theory.detector[0].pixel_size.x |
---|
997 | pixel_width_y = theory.detector[0].pixel_size.y |
---|
998 | center_x = theory.detector[0].beam_center.x/pixel_width_x |
---|
999 | center_y = theory.detector[0].beam_center.y/pixel_width_y |
---|
1000 | |
---|
1001 | |
---|
1002 | size_x, size_y= numpy.shape(theory.data) |
---|
1003 | for i_x in range(size_x): |
---|
1004 | theta = (i_x-center_x)*pixel_width_x / distance |
---|
1005 | qx = 4.0*math.pi/wavelength * math.tan(theta/2.0) |
---|
1006 | theory.x_bins.append(qx) |
---|
1007 | for i_y in range(size_y): |
---|
1008 | theta = (i_y-center_y)*pixel_width_y / distance |
---|
1009 | qy =4.0*math.pi/wavelength * math.tan(theta/2.0) |
---|
1010 | theory.y_bins.append(qy) |
---|
1011 | |
---|
1012 | theory.group_id ="Model" |
---|
1013 | theory.id ="Model" |
---|
1014 | ## determine plot boundaries |
---|
1015 | theory.xmin= -qmax |
---|
1016 | theory.xmax= qmax |
---|
1017 | theory.ymin= -qmax |
---|
1018 | theory.ymax= qmax |
---|
1019 | |
---|
1020 | |
---|
1021 | def _get_plotting_info(self, data=None): |
---|
1022 | """ |
---|
1023 | get plotting info from data if data !=None |
---|
1024 | else use some default |
---|
1025 | """ |
---|
1026 | my_info = PlotInfo() |
---|
1027 | if data !=None: |
---|
1028 | if hasattr(data,"info"): |
---|
1029 | x_name, x_units = data.get_xaxis() |
---|
1030 | y_name, y_units = data.get_yaxis() |
---|
1031 | |
---|
1032 | my_info._xunit = x_units |
---|
1033 | my_info._xaxis = x_name |
---|
1034 | my_info._yunit = y_units |
---|
1035 | my_info._yaxis = y_name |
---|
1036 | |
---|
1037 | my_info.title= data.name |
---|
1038 | if hasattr(data, "info"): |
---|
1039 | my_info.info= data.info |
---|
1040 | if hasattr(data, "group_id"): |
---|
1041 | my_info.group_id= data.group_id |
---|
1042 | |
---|
1043 | return my_info |
---|
1044 | |
---|
1045 | |
---|
1046 | def _complete1D(self, x,y, elapsed,model,data=None): |
---|
1047 | """ |
---|
1048 | Complete plotting 1D data |
---|
1049 | """ |
---|
1050 | |
---|
1051 | try: |
---|
1052 | |
---|
1053 | new_plot = Theory1D(x=x, y=y) |
---|
1054 | my_info = self._get_plotting_info( data) |
---|
1055 | new_plot.name = model.name |
---|
1056 | new_plot.id = my_info.id |
---|
1057 | new_plot.group_id = my_info.group_id |
---|
1058 | |
---|
1059 | new_plot.xaxis( my_info._xaxis, my_info._xunit) |
---|
1060 | new_plot.yaxis( my_info._yaxis, my_info._yunit) |
---|
1061 | if data!=None: |
---|
1062 | if new_plot.id == data.id: |
---|
1063 | new_plot.id += "Model" |
---|
1064 | new_plot.is_data =False |
---|
1065 | |
---|
1066 | from DataLoader import data_info |
---|
1067 | info= data_info.Data1D(x= new_plot.x, y=new_plot.y) |
---|
1068 | info.title= new_plot.name |
---|
1069 | title= my_info.title |
---|
1070 | info.xaxis(new_plot._xaxis, new_plot._xunit) |
---|
1071 | info.yaxis( new_plot._yaxis, new_plot._yunit) |
---|
1072 | new_plot.info = info |
---|
1073 | # Pass the reset flag to let the plotting event handler |
---|
1074 | # know that we are replacing the whole plot |
---|
1075 | if title== None: |
---|
1076 | title = "Analytical model 1D " |
---|
1077 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
1078 | title= str(title), reset=True )) |
---|
1079 | else: |
---|
1080 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
1081 | title= str(title))) |
---|
1082 | msg = "Plot 1D complete !" |
---|
1083 | wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) |
---|
1084 | except: |
---|
1085 | msg= " Error occurred when drawing %s Model 1D: "%new_plot.name |
---|
1086 | msg+= " %s"%sys.exc_value |
---|
1087 | wx.PostEvent( self.parent, StatusEvent(status= msg, type="stop" )) |
---|
1088 | return |
---|
1089 | |
---|
1090 | |
---|
1091 | |
---|
1092 | def _update2D(self, output,time=None): |
---|
1093 | """ |
---|
1094 | Update the output of plotting model |
---|
1095 | """ |
---|
1096 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
1097 | #updating ... ",type="update")) |
---|
1098 | self.calc_thread.ready(0.01) |
---|
1099 | |
---|
1100 | |
---|
1101 | def _complete2D(self, image,data, model, elapsed,qmin, qmax,qstep=DEFAULT_NPTS): |
---|
1102 | """ |
---|
1103 | Complete get the result of modelthread and create model 2D |
---|
1104 | that can be plot. |
---|
1105 | """ |
---|
1106 | |
---|
1107 | |
---|
1108 | err_image = numpy.zeros(numpy.shape(image)) |
---|
1109 | |
---|
1110 | theory= Data2D(image= image , err_image= err_image) |
---|
1111 | theory.name= model.name |
---|
1112 | |
---|
1113 | if data ==None: |
---|
1114 | self._fill_default_model2D(theory= theory, qmax=qmax,qstep=qstep, qmin= qmin) |
---|
1115 | |
---|
1116 | else: |
---|
1117 | theory.id= data.id+"Model" |
---|
1118 | theory.group_id= data.name+"Model" |
---|
1119 | theory.x_bins= data.x_bins |
---|
1120 | theory.y_bins= data.y_bins |
---|
1121 | theory.detector= data.detector |
---|
1122 | theory.source= data.source |
---|
1123 | theory.is_data =False |
---|
1124 | ## plot boundaries |
---|
1125 | theory.ymin= data.ymin |
---|
1126 | theory.ymax= data.ymax |
---|
1127 | theory.xmin= data.xmin |
---|
1128 | theory.xmax= data.xmax |
---|
1129 | |
---|
1130 | |
---|
1131 | ## plot |
---|
1132 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
1133 | title="Analytical model 2D ", reset=True )) |
---|
1134 | msg = "Plot 2D complete !" |
---|
1135 | wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) |
---|
1136 | |
---|
1137 | def _on_data_error(self, event): |
---|
1138 | """ |
---|
1139 | receives and event from plotting plu-gins to store the data name and |
---|
1140 | their errors of y coordinates for 1Data hide and show error |
---|
1141 | """ |
---|
1142 | self.err_dy= event.err_dy |
---|
1143 | |
---|
1144 | |
---|
1145 | def _draw_model2D(self,model,data=None,description=None, enable2D=False, |
---|
1146 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
1147 | """ |
---|
1148 | draw model in 2D |
---|
1149 | @param model: instance of the model to draw |
---|
1150 | @param description: the description of the model |
---|
1151 | @param enable2D: when True allows to draw model 2D |
---|
1152 | @param qmin: the minimum value to draw model 2D |
---|
1153 | @param qmax: the maximum value to draw model 2D |
---|
1154 | @param qstep: the number of division of Qx and Qy of the model to draw |
---|
1155 | |
---|
1156 | """ |
---|
1157 | x= numpy.linspace(start= -1*qmax, |
---|
1158 | stop= qmax, |
---|
1159 | num= qstep, |
---|
1160 | endpoint=True ) |
---|
1161 | y = numpy.linspace(start= -1*qmax, |
---|
1162 | stop= qmax, |
---|
1163 | num= qstep, |
---|
1164 | endpoint=True ) |
---|
1165 | ## use data info instead |
---|
1166 | if data !=None: |
---|
1167 | ## check if data2D to plot |
---|
1168 | if hasattr(data, "x_bins"): |
---|
1169 | enable2D = True |
---|
1170 | x= data.x_bins |
---|
1171 | y= data.y_bins |
---|
1172 | |
---|
1173 | if not enable2D: |
---|
1174 | return |
---|
1175 | try: |
---|
1176 | from model_thread import Calc2D |
---|
1177 | ## If a thread is already started, stop it |
---|
1178 | if self.calc_2D != None and self.calc_2D.isrunning(): |
---|
1179 | self.calc_2D.stop() |
---|
1180 | self.calc_2D = Calc2D( x= x, |
---|
1181 | y= y, |
---|
1182 | model= model, |
---|
1183 | data = data, |
---|
1184 | qmin= qmin, |
---|
1185 | qmax= qmax, |
---|
1186 | qstep= qstep, |
---|
1187 | completefn= self._complete2D, |
---|
1188 | updatefn= self._update2D ) |
---|
1189 | self.calc_2D.queue() |
---|
1190 | |
---|
1191 | except: |
---|
1192 | msg= " Error occurred when drawing %s Model 2D: "%model.name |
---|
1193 | msg+= " %s"%sys.exc_value |
---|
1194 | wx.PostEvent( self.parent, StatusEvent(status= msg )) |
---|
1195 | return |
---|
1196 | |
---|
1197 | def _draw_model1D(self, model, data=None, smearer= None, |
---|
1198 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep= DEFAULT_NPTS,enable1D= True): |
---|
1199 | """ |
---|
1200 | Draw model 1D from loaded data1D |
---|
1201 | @param data: loaded data |
---|
1202 | @param model: the model to plot |
---|
1203 | """ |
---|
1204 | |
---|
1205 | x= numpy.linspace(start= qmin, |
---|
1206 | stop= qmax, |
---|
1207 | num= qstep, |
---|
1208 | endpoint=True |
---|
1209 | ) |
---|
1210 | if data!=None: |
---|
1211 | ## check for data2D |
---|
1212 | if hasattr(data,"x_bins"): |
---|
1213 | return |
---|
1214 | x = data.x |
---|
1215 | if qmin == DEFAULT_QMIN : |
---|
1216 | qmin = min(data.x) |
---|
1217 | if qmax == DEFAULT_QMAX: |
---|
1218 | qmax = max(data.x) |
---|
1219 | |
---|
1220 | |
---|
1221 | if not enable1D: |
---|
1222 | return |
---|
1223 | |
---|
1224 | try: |
---|
1225 | from model_thread import Calc1D |
---|
1226 | ## If a thread is already started, stop it |
---|
1227 | if self.calc_1D!= None and self.calc_1D.isrunning(): |
---|
1228 | self.calc_1D.stop() |
---|
1229 | self.calc_1D= Calc1D( x= x, |
---|
1230 | data = data, |
---|
1231 | model= model, |
---|
1232 | qmin = qmin, |
---|
1233 | qmax = qmax, |
---|
1234 | smearer = smearer, |
---|
1235 | completefn = self._complete1D, |
---|
1236 | updatefn = self._update1D ) |
---|
1237 | self.calc_1D.queue() |
---|
1238 | |
---|
1239 | except: |
---|
1240 | msg= " Error occurred when drawing %s Model 1D: "%model.name |
---|
1241 | msg+= " %s"%sys.exc_value |
---|
1242 | wx.PostEvent( self.parent, StatusEvent(status= msg )) |
---|
1243 | return |
---|
1244 | |
---|
1245 | |
---|
1246 | if __name__ == "__main__": |
---|
1247 | i = Plugin() |
---|
1248 | |
---|
1249 | |
---|
1250 | |
---|
1251 | |
---|