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