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