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 | ## Data2D case |
---|
624 | if not hasattr(plottable, "is_data"): |
---|
625 | item= copy.deepcopy(plottable) |
---|
626 | item.group_id += "data2D" |
---|
627 | item.id +="data2D" |
---|
628 | item.is_data= False |
---|
629 | title = item.name |
---|
630 | title += " Fit" |
---|
631 | data = item |
---|
632 | wx.PostEvent(self.parent, NewPlotEvent(plot=item, title=str(title))) |
---|
633 | else: |
---|
634 | item= copy.deepcopy(plottable ) |
---|
635 | data= copy.deepcopy(plottable ) |
---|
636 | item.is_data=True |
---|
637 | data.is_data=True |
---|
638 | |
---|
639 | ## create anew page |
---|
640 | if item.name == self.panel.graph.selected_plottable or\ |
---|
641 | item.__class__.__name__ is "Data2D": |
---|
642 | try: |
---|
643 | page = self.fit_panel.add_fit_page(data) |
---|
644 | # add data associated to the page created |
---|
645 | if page !=None: |
---|
646 | #create a fitproblem storing all link to data,model,page creation |
---|
647 | if not page in self.page_finder.keys(): |
---|
648 | self.page_finder[page]= FitProblem() |
---|
649 | ## item is almost the same as data but contains |
---|
650 | ## axis info for plotting |
---|
651 | self.page_finder[page].add_plotted_data(item) |
---|
652 | self.page_finder[page].add_fit_data(data) |
---|
653 | |
---|
654 | wx.PostEvent(self.parent, StatusEvent(status="Page Created")) |
---|
655 | else: |
---|
656 | wx.PostEvent(self.parent, StatusEvent(status="Page was already Created")) |
---|
657 | except: |
---|
658 | wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ |
---|
659 | %sys.exc_value)) |
---|
660 | return |
---|
661 | |
---|
662 | |
---|
663 | def _updateFit(self): |
---|
664 | """ |
---|
665 | Is called when values of result are available |
---|
666 | """ |
---|
667 | ##Sending a progess message to the status bar |
---|
668 | wx.PostEvent(self.parent, StatusEvent(status="Computing...")) |
---|
669 | |
---|
670 | def _single_fit_completed(self,result,pars,cpage, elapsed=None): |
---|
671 | """ |
---|
672 | Display fit result on one page of the notebook. |
---|
673 | @param result: result of fit |
---|
674 | @param pars: list of names of parameters fitted |
---|
675 | @param current_pg: the page where information will be displayed |
---|
676 | @param qmin: the minimum value of x to replot the model |
---|
677 | @param qmax: the maximum value of x to replot model |
---|
678 | |
---|
679 | """ |
---|
680 | wx.PostEvent(self.parent, StatusEvent(status="Single fit \ |
---|
681 | complete! " )) |
---|
682 | |
---|
683 | try: |
---|
684 | if result ==None: |
---|
685 | msg= "Simple Fitting Stop !!!" |
---|
686 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
687 | return |
---|
688 | if numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
689 | msg= "Single Fitting did not converge!!!" |
---|
690 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
691 | return |
---|
692 | for page, value in self.page_finder.iteritems(): |
---|
693 | if page==cpage : |
---|
694 | model= value.get_model() |
---|
695 | break |
---|
696 | i = 0 |
---|
697 | for name in pars: |
---|
698 | if result.pvec.__class__==numpy.float64: |
---|
699 | model.setParam(name,result.pvec) |
---|
700 | else: |
---|
701 | model.setParam(name,result.pvec[i]) |
---|
702 | i += 1 |
---|
703 | ## Reset values of the current page to fit result |
---|
704 | cpage.onsetValues(result.fitness, result.pvec,result.stderr) |
---|
705 | ## plot the current model with new param |
---|
706 | metadata = self.page_finder[cpage].get_fit_data() |
---|
707 | model = self.page_finder[cpage].get_model() |
---|
708 | qmin, qmax= self.page_finder[cpage].get_range() |
---|
709 | smearer =self.page_finder[cpage].get_smearer() |
---|
710 | #Replot models |
---|
711 | msg= "Single Fit completed. plotting... %s:"%model.name |
---|
712 | wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) |
---|
713 | self.draw_model( model=model, data= metadata, smearer= smearer, |
---|
714 | qmin= qmin, qmax= qmax) |
---|
715 | wx.PostEvent(self.parent, StatusEvent(status=" " , type="stop")) |
---|
716 | except: |
---|
717 | msg= "Single Fit completed but Following error occurred:%s"% sys.exc_value |
---|
718 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
719 | return |
---|
720 | |
---|
721 | |
---|
722 | def _simul_fit_completed(self,result,pars=None,cpage=None, elapsed=None): |
---|
723 | """ |
---|
724 | Parameter estimation completed, |
---|
725 | display the results to the user |
---|
726 | @param alpha: estimated best alpha |
---|
727 | @param elapsed: computation time |
---|
728 | """ |
---|
729 | wx.PostEvent(self.parent, StatusEvent(status="Simultaneous fit \ |
---|
730 | complete ")) |
---|
731 | |
---|
732 | ## fit more than 1 model at the same time |
---|
733 | try: |
---|
734 | if result ==None: |
---|
735 | msg= "Complex Fitting Stop !!!" |
---|
736 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
737 | return |
---|
738 | if numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
739 | msg= "Single Fitting did not converge!!!" |
---|
740 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
741 | return |
---|
742 | for page, value in self.page_finder.iteritems(): |
---|
743 | if value.get_scheduled()==1: |
---|
744 | model = value.get_model() |
---|
745 | metadata = value.get_plotted_data() |
---|
746 | small_out = [] |
---|
747 | small_cov = [] |
---|
748 | i = 0 |
---|
749 | #Separate result in to data corresponding to each page |
---|
750 | for p in result.parameters: |
---|
751 | model_name,param_name = self.split_string(p.name) |
---|
752 | if model.name == model_name: |
---|
753 | p_name= model.name+"."+param_name |
---|
754 | if p.name == p_name: |
---|
755 | small_out.append(p.value ) |
---|
756 | model.setParam(param_name,p.value) |
---|
757 | |
---|
758 | small_cov.append(p.stderr) |
---|
759 | else: |
---|
760 | value= model.getParam(param_name) |
---|
761 | small_out.append(value ) |
---|
762 | small_cov.append(None) |
---|
763 | # Display result on each page |
---|
764 | page.onsetValues(result.fitness, small_out,small_cov) |
---|
765 | #Replot models |
---|
766 | msg= "Simultaneous Fit completed. plotting... %s:"%model.name |
---|
767 | wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) |
---|
768 | qmin, qmax= page.get_range() |
---|
769 | smearer =self.page_finder[page].get_smearer() |
---|
770 | self.draw_model( model=model, data= metadata, smearer=smearer, |
---|
771 | qmin= qmin, qmax= qmax) |
---|
772 | wx.PostEvent(self.parent, StatusEvent(status="", type="stop")) |
---|
773 | except: |
---|
774 | msg= "Simultaneous Fit completed" |
---|
775 | msg +=" but Following error occurred:%s"%sys.exc_value |
---|
776 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
777 | return |
---|
778 | |
---|
779 | |
---|
780 | |
---|
781 | def _on_show_panel(self, event): |
---|
782 | print "_on_show_panel: fitting" |
---|
783 | |
---|
784 | |
---|
785 | def _onset_engine_park(self,event): |
---|
786 | """ |
---|
787 | set engine to park |
---|
788 | """ |
---|
789 | self._on_change_engine('park') |
---|
790 | |
---|
791 | |
---|
792 | def _onset_engine_scipy(self,event): |
---|
793 | """ |
---|
794 | set engine to scipy |
---|
795 | """ |
---|
796 | self._on_change_engine('scipy') |
---|
797 | |
---|
798 | def _on_slicer_event(self, event): |
---|
799 | """ |
---|
800 | Receive a panel as event and send it to guiframe |
---|
801 | @param event: event containing a panel |
---|
802 | """ |
---|
803 | if event.panel!=None: |
---|
804 | new_panel = event.panel |
---|
805 | # Set group ID if available |
---|
806 | event_id = self.parent.popup_panel(new_panel) |
---|
807 | #self.menu3.Append(event_id, new_panel.window_caption, |
---|
808 | # "Show %s plot panel" % new_panel.window_caption) |
---|
809 | # Set UID to allow us to reference the panel later |
---|
810 | new_panel.uid = event_id |
---|
811 | self.mypanels.append(new_panel) |
---|
812 | return |
---|
813 | |
---|
814 | |
---|
815 | def _return_engine_type(self): |
---|
816 | """ |
---|
817 | return the current type of engine |
---|
818 | """ |
---|
819 | return self._fit_engine |
---|
820 | |
---|
821 | |
---|
822 | def _on_change_engine(self, engine='park'): |
---|
823 | """ |
---|
824 | Allow to select the type of engine to perform fit |
---|
825 | @param engine: the key work of the engine |
---|
826 | """ |
---|
827 | ## saving fit engine name |
---|
828 | self._fit_engine = engine |
---|
829 | ## change menu item state |
---|
830 | if engine=="park": |
---|
831 | self.menu1.FindItemByPosition(0).Check(False) |
---|
832 | self.menu1.FindItemByPosition(1).Check(True) |
---|
833 | else: |
---|
834 | self.menu1.FindItemByPosition(0).Check(True) |
---|
835 | self.menu1.FindItemByPosition(1).Check(False) |
---|
836 | |
---|
837 | ## post a message to status bar |
---|
838 | wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) |
---|
839 | |
---|
840 | ## Bind every open fit page with a newevent to know the current fitting engine |
---|
841 | import fitpage |
---|
842 | event= fitpage.FitterTypeEvent() |
---|
843 | event.type = self._fit_engine |
---|
844 | for key in self.page_finder.keys(): |
---|
845 | wx.PostEvent(key, event) |
---|
846 | |
---|
847 | |
---|
848 | def _on_model_panel(self, evt): |
---|
849 | """ |
---|
850 | react to model selection on any combo box or model menu.plot the model |
---|
851 | @param evt: wx.combobox event |
---|
852 | """ |
---|
853 | model = evt.model |
---|
854 | |
---|
855 | if model ==None: |
---|
856 | return |
---|
857 | |
---|
858 | current_pg = self.fit_panel.get_current_page() |
---|
859 | ## make sure nothing is done on self.sim_page |
---|
860 | ## example trying to call set_panel on self.sim_page |
---|
861 | if current_pg != self.sim_page : |
---|
862 | |
---|
863 | if self.page_finder[current_pg].get_model()== None : |
---|
864 | |
---|
865 | model.name="M"+str(self.index_model) |
---|
866 | self.index_model += 1 |
---|
867 | else: |
---|
868 | model.name= self.page_finder[current_pg].get_model().name |
---|
869 | |
---|
870 | metadata = self.page_finder[current_pg].get_plotted_data() |
---|
871 | |
---|
872 | # save the name containing the data name with the appropriate model |
---|
873 | self.page_finder[current_pg].set_model(model) |
---|
874 | qmin, qmax= current_pg.get_range() |
---|
875 | self.page_finder[current_pg].set_range(qmin=qmin, qmax=qmax) |
---|
876 | smearer= self.page_finder[current_pg].get_smearer() |
---|
877 | # save model name |
---|
878 | self.draw_model( model=model,smearer=smearer, |
---|
879 | data= metadata, qmin=qmin, qmax=qmax) |
---|
880 | |
---|
881 | if self.sim_page!=None: |
---|
882 | self.sim_page.draw_page() |
---|
883 | |
---|
884 | |
---|
885 | |
---|
886 | def _on_model_menu(self, evt): |
---|
887 | """ |
---|
888 | Plot a theory from a model selected from the menu |
---|
889 | @param evt: wx.menu event |
---|
890 | """ |
---|
891 | model = evt.model |
---|
892 | |
---|
893 | # Create a model page. If a new page is created, the model |
---|
894 | # will be plotted automatically. If a page already exists, |
---|
895 | # the content will be updated and the plot refreshed |
---|
896 | self.fit_panel.add_model_page(model,topmenu=True) |
---|
897 | |
---|
898 | |
---|
899 | |
---|
900 | |
---|
901 | def _update1D(self,x, output): |
---|
902 | """ |
---|
903 | Update the output of plotting model 1D |
---|
904 | """ |
---|
905 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
906 | #updating ... ",type="update")) |
---|
907 | self.calc_thread.ready(0.01) |
---|
908 | |
---|
909 | def _fill_default_model2D(self, theory, qmax,qstep, qmin=None): |
---|
910 | """ |
---|
911 | fill Data2D with default value |
---|
912 | @param theory: Data2D to fill |
---|
913 | """ |
---|
914 | from DataLoader.data_info import Detector, Source |
---|
915 | |
---|
916 | detector = Detector() |
---|
917 | theory.detector.append(detector) |
---|
918 | |
---|
919 | theory.detector[0].distance=1e+32 |
---|
920 | theory.source= Source() |
---|
921 | theory.source.wavelength=2*math.pi/1e+32 |
---|
922 | |
---|
923 | ## Create detector for Model 2D |
---|
924 | xmax=2*theory.detector[0].distance*math.atan(\ |
---|
925 | qmax/(4*math.pi/theory.source.wavelength)) |
---|
926 | |
---|
927 | theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5) |
---|
928 | theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5) |
---|
929 | theory.detector[0].beam_center.x= qmax |
---|
930 | theory.detector[0].beam_center.y= qmax |
---|
931 | ## create x_bins and y_bins of the model 2D |
---|
932 | distance = theory.detector[0].distance |
---|
933 | pixel = qstep/2-1 |
---|
934 | theta = pixel / distance / qstep#100.0 |
---|
935 | wavelength = theory.source.wavelength |
---|
936 | pixel_width_x = theory.detector[0].pixel_size.x |
---|
937 | pixel_width_y = theory.detector[0].pixel_size.y |
---|
938 | center_x = theory.detector[0].beam_center.x/pixel_width_x |
---|
939 | center_y = theory.detector[0].beam_center.y/pixel_width_y |
---|
940 | |
---|
941 | |
---|
942 | size_x, size_y= numpy.shape(theory.data) |
---|
943 | for i_x in range(size_x): |
---|
944 | theta = (i_x-center_x)*pixel_width_x / distance |
---|
945 | qx = 4.0*math.pi/wavelength * math.tan(theta/2.0) |
---|
946 | theory.x_bins.append(qx) |
---|
947 | for i_y in range(size_y): |
---|
948 | theta = (i_y-center_y)*pixel_width_y / distance |
---|
949 | qy =4.0*math.pi/wavelength * math.tan(theta/2.0) |
---|
950 | theory.y_bins.append(qy) |
---|
951 | |
---|
952 | theory.group_id ="Model" |
---|
953 | theory.id ="Model" |
---|
954 | ## determine plot boundaries |
---|
955 | theory.xmin= -qmax |
---|
956 | theory.xmax= qmax |
---|
957 | theory.ymin= -qmax |
---|
958 | theory.ymax= qmax |
---|
959 | |
---|
960 | |
---|
961 | def _get_plotting_info(self, data=None): |
---|
962 | """ |
---|
963 | get plotting info from data if data !=None |
---|
964 | else use some default |
---|
965 | """ |
---|
966 | my_info = PlotInfo() |
---|
967 | if data !=None: |
---|
968 | if hasattr(data,"info"): |
---|
969 | x_name, x_units = data.get_xaxis() |
---|
970 | y_name, y_units = data.get_yaxis() |
---|
971 | |
---|
972 | my_info._xunit = x_units |
---|
973 | my_info._xaxis = x_name |
---|
974 | my_info._yunit = y_units |
---|
975 | my_info._yaxis = y_name |
---|
976 | |
---|
977 | my_info.title= data.name |
---|
978 | if hasattr(data, "info"): |
---|
979 | my_info.info= data.info |
---|
980 | if hasattr(data, "group_id"): |
---|
981 | my_info.group_id= data.group_id |
---|
982 | |
---|
983 | return my_info |
---|
984 | |
---|
985 | def _complete1D(self, x,y, elapsed,model,data=None): |
---|
986 | """ |
---|
987 | Complete plotting 1D data |
---|
988 | """ |
---|
989 | |
---|
990 | try: |
---|
991 | |
---|
992 | new_plot = Theory1D(x=x, y=y) |
---|
993 | my_info = self._get_plotting_info( data) |
---|
994 | new_plot.name = model.name |
---|
995 | new_plot.id = my_info.id |
---|
996 | new_plot.group_id = my_info.group_id |
---|
997 | |
---|
998 | new_plot.xaxis( my_info._xaxis, my_info._xunit) |
---|
999 | new_plot.yaxis( my_info._yaxis, my_info._yunit) |
---|
1000 | if data!=None: |
---|
1001 | if new_plot.id == data.id: |
---|
1002 | new_plot.id += "Model" |
---|
1003 | new_plot.is_data =False |
---|
1004 | |
---|
1005 | from DataLoader import data_info |
---|
1006 | info= data_info.Data1D(x= new_plot.x, y=new_plot.y) |
---|
1007 | info.title= new_plot.name |
---|
1008 | title= my_info.title |
---|
1009 | info.xaxis(new_plot._xaxis, new_plot._xunit) |
---|
1010 | info.yaxis( new_plot._yaxis, new_plot._yunit) |
---|
1011 | new_plot.info = info |
---|
1012 | # Pass the reset flag to let the plotting event handler |
---|
1013 | # know that we are replacing the whole plot |
---|
1014 | if title== None: |
---|
1015 | title = "Analytical model 1D " |
---|
1016 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
1017 | title= str(title), reset=True )) |
---|
1018 | else: |
---|
1019 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
1020 | title= str(title))) |
---|
1021 | msg = "Plot 1D complete !" |
---|
1022 | wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) |
---|
1023 | except: |
---|
1024 | msg= " Error occurred when drawing %s Model 1D: "%new_plot.name |
---|
1025 | msg+= " %s"%sys.exc_value |
---|
1026 | wx.PostEvent( self.parent, StatusEvent(status= msg, type="stop" )) |
---|
1027 | return |
---|
1028 | |
---|
1029 | |
---|
1030 | |
---|
1031 | |
---|
1032 | def _update2D(self, output,time=None): |
---|
1033 | """ |
---|
1034 | Update the output of plotting model |
---|
1035 | """ |
---|
1036 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
1037 | #updating ... ",type="update")) |
---|
1038 | self.calc_thread.ready(0.01) |
---|
1039 | |
---|
1040 | |
---|
1041 | def _complete2D(self, image,data, model, elapsed,qmin, qmax,qstep=DEFAULT_NPTS): |
---|
1042 | """ |
---|
1043 | Complete get the result of modelthread and create model 2D |
---|
1044 | that can be plot. |
---|
1045 | """ |
---|
1046 | |
---|
1047 | |
---|
1048 | err_image = numpy.zeros(numpy.shape(image)) |
---|
1049 | |
---|
1050 | theory= Data2D(image= image , err_image= err_image) |
---|
1051 | theory.name= model.name |
---|
1052 | |
---|
1053 | if data ==None: |
---|
1054 | self._fill_default_model2D(theory= theory, qmax=qmax,qstep=qstep, qmin= qmin) |
---|
1055 | |
---|
1056 | else: |
---|
1057 | theory.id= data.id+"Model" |
---|
1058 | theory.group_id= data.name+"Model" |
---|
1059 | theory.x_bins= data.x_bins |
---|
1060 | theory.y_bins= data.y_bins |
---|
1061 | theory.detector= data.detector |
---|
1062 | theory.source= data.source |
---|
1063 | theory.is_data =False |
---|
1064 | ## plot boundaries |
---|
1065 | theory.ymin= data.ymin |
---|
1066 | theory.ymax= data.ymax |
---|
1067 | theory.xmin= data.xmin |
---|
1068 | theory.xmax= data.xmax |
---|
1069 | |
---|
1070 | |
---|
1071 | ## plot |
---|
1072 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
1073 | title="Analytical model 2D ", reset=True )) |
---|
1074 | msg = "Plot 2D complete !" |
---|
1075 | wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) |
---|
1076 | |
---|
1077 | def _on_data_error(self, event): |
---|
1078 | """ |
---|
1079 | receives and event from plotting plu-gins to store the data name and |
---|
1080 | their errors of y coordinates for 1Data hide and show error |
---|
1081 | """ |
---|
1082 | self.err_dy= event.err_dy |
---|
1083 | |
---|
1084 | def _draw_model2D(self,model,data=None,description=None, enable2D=False, |
---|
1085 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
1086 | """ |
---|
1087 | draw model in 2D |
---|
1088 | @param model: instance of the model to draw |
---|
1089 | @param description: the description of the model |
---|
1090 | @param enable2D: when True allows to draw model 2D |
---|
1091 | @param qmin: the minimum value to draw model 2D |
---|
1092 | @param qmax: the maximum value to draw model 2D |
---|
1093 | @param qstep: the number of division of Qx and Qy of the model to draw |
---|
1094 | |
---|
1095 | """ |
---|
1096 | |
---|
1097 | x= numpy.linspace(start= -1*qmax, |
---|
1098 | stop= qmax, |
---|
1099 | num= qstep, |
---|
1100 | endpoint=True ) |
---|
1101 | y = numpy.linspace(start= -1*qmax, |
---|
1102 | stop= qmax, |
---|
1103 | num= qstep, |
---|
1104 | endpoint=True ) |
---|
1105 | ## use data info instead |
---|
1106 | if data !=None: |
---|
1107 | ## check if data2D to plot |
---|
1108 | if hasattr(data, "x_bins"): |
---|
1109 | enable2D = True |
---|
1110 | x= data.x_bins |
---|
1111 | y= data.y_bins |
---|
1112 | |
---|
1113 | if not enable2D: |
---|
1114 | return |
---|
1115 | try: |
---|
1116 | from model_thread import Calc2D |
---|
1117 | ## If a thread is already started, stop it |
---|
1118 | if self.calc_2D != None and self.calc_2D.isrunning(): |
---|
1119 | self.calc_2D.stop() |
---|
1120 | self.calc_2D = Calc2D( x= x, |
---|
1121 | y= y, |
---|
1122 | model= model, |
---|
1123 | data = data, |
---|
1124 | qmin= qmin, |
---|
1125 | qmax= qmax, |
---|
1126 | qstep= qstep, |
---|
1127 | completefn= self._complete2D, |
---|
1128 | updatefn= self._update2D ) |
---|
1129 | self.calc_2D.queue() |
---|
1130 | |
---|
1131 | except: |
---|
1132 | msg= " Error occurred when drawing %s Model 2D: "%model.name |
---|
1133 | msg+= " %s"%sys.exc_value |
---|
1134 | wx.PostEvent( self.parent, StatusEvent(status= msg )) |
---|
1135 | return |
---|
1136 | |
---|
1137 | def _draw_model1D(self, model, data=None, smearer= None, |
---|
1138 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep= DEFAULT_NPTS,enable1D= True): |
---|
1139 | """ |
---|
1140 | Draw model 1D from loaded data1D |
---|
1141 | @param data: loaded data |
---|
1142 | @param model: the model to plot |
---|
1143 | """ |
---|
1144 | |
---|
1145 | x= numpy.linspace(start= qmin, |
---|
1146 | stop= qmax, |
---|
1147 | num= qstep, |
---|
1148 | endpoint=True |
---|
1149 | ) |
---|
1150 | if data!=None: |
---|
1151 | ## check for data2D |
---|
1152 | if hasattr(data,"x_bins"): |
---|
1153 | return |
---|
1154 | x = data.x |
---|
1155 | if qmin == DEFAULT_QMIN : |
---|
1156 | qmin = min(data.x) |
---|
1157 | if qmax == DEFAULT_QMAX: |
---|
1158 | qmax = max(data.x) |
---|
1159 | |
---|
1160 | |
---|
1161 | if not enable1D: |
---|
1162 | return |
---|
1163 | |
---|
1164 | try: |
---|
1165 | from model_thread import Calc1D |
---|
1166 | ## If a thread is already started, stop it |
---|
1167 | if self.calc_1D!= None and self.calc_1D.isrunning(): |
---|
1168 | self.calc_1D.stop() |
---|
1169 | self.calc_1D= Calc1D( x= x, |
---|
1170 | data = data, |
---|
1171 | model= model, |
---|
1172 | qmin = qmin, |
---|
1173 | qmax = qmax, |
---|
1174 | smearer = smearer, |
---|
1175 | completefn = self._complete1D, |
---|
1176 | updatefn = self._update1D ) |
---|
1177 | self.calc_1D.queue() |
---|
1178 | |
---|
1179 | except: |
---|
1180 | msg= " Error occurred when drawing %s Model 1D: "%model.name |
---|
1181 | msg+= " %s"%sys.exc_value |
---|
1182 | wx.PostEvent( self.parent, StatusEvent(status= msg )) |
---|
1183 | return |
---|
1184 | |
---|
1185 | |
---|
1186 | |
---|
1187 | |
---|
1188 | |
---|
1189 | if __name__ == "__main__": |
---|
1190 | i = Plugin() |
---|
1191 | |
---|
1192 | |
---|
1193 | |
---|
1194 | |
---|