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