1 | import os,os.path, re |
---|
2 | import sys, wx, logging |
---|
3 | import string, numpy, math |
---|
4 | |
---|
5 | from copy import deepcopy |
---|
6 | from danse.common.plottools.plottables import Data1D, Theory1D,Data2D |
---|
7 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
8 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
9 | from sans.fit.AbstractFitEngine import Model,Data,FitData1D,FitData2D |
---|
10 | from fitproblem import FitProblem |
---|
11 | from fitpanel import FitPanel |
---|
12 | |
---|
13 | import models,modelpage |
---|
14 | import fitpage1D,fitpage2D |
---|
15 | import park |
---|
16 | DEFAULT_BEAM = 0.005 |
---|
17 | class Plugin: |
---|
18 | """ |
---|
19 | Fitting plugin is used to perform fit |
---|
20 | """ |
---|
21 | def __init__(self): |
---|
22 | ## Plug-in name |
---|
23 | self.sub_menu = "Fitting" |
---|
24 | |
---|
25 | ## Reference to the parent window |
---|
26 | self.parent = None |
---|
27 | self.menu_mng = models.ModelManager() |
---|
28 | ## List of panels for the simulation perspective (names) |
---|
29 | self.perspective = [] |
---|
30 | # Start with a good default |
---|
31 | self.elapsed = 0.022 |
---|
32 | self.fitter = None |
---|
33 | |
---|
34 | #Flag to let the plug-in know that it is running standalone |
---|
35 | self.standalone=True |
---|
36 | ## Fit engine |
---|
37 | self._fit_engine = 'scipy' |
---|
38 | self.enable_model2D=False |
---|
39 | # Log startup |
---|
40 | logging.info("Fitting plug-in started") |
---|
41 | |
---|
42 | def populate_menu(self, id, owner): |
---|
43 | """ |
---|
44 | Create a menu for the Fitting plug-in |
---|
45 | @param id: id to create a menu |
---|
46 | @param owner: owner of menu |
---|
47 | @ return : list of information to populate the main menu |
---|
48 | """ |
---|
49 | #Menu for fitting |
---|
50 | self.menu1 = wx.Menu() |
---|
51 | id1 = wx.NewId() |
---|
52 | self.menu1.Append(id1, '&Show fit panel') |
---|
53 | wx.EVT_MENU(owner, id1, self.on_perspective) |
---|
54 | id3 = wx.NewId() |
---|
55 | self.menu1.AppendCheckItem(id3, "park") |
---|
56 | wx.EVT_MENU(owner, id3, self._onset_engine) |
---|
57 | |
---|
58 | #menu for model |
---|
59 | menu2 = wx.Menu() |
---|
60 | |
---|
61 | self.menu_mng.populate_menu(menu2, owner) |
---|
62 | id2 = wx.NewId() |
---|
63 | owner.Bind(models.EVT_MODEL,self._on_model_menu) |
---|
64 | #owner.Bind(modelpage.EVT_MODEL,self._on_model_menu) |
---|
65 | self.fit_panel.set_owner(owner) |
---|
66 | self.fit_panel.set_model_list(self.menu_mng.get_model_list()) |
---|
67 | owner.Bind(fitpage1D.EVT_MODEL_BOX,self._on_model_panel) |
---|
68 | owner.Bind(fitpage2D.EVT_MODEL_BOX,self._on_model_panel) |
---|
69 | #create menubar items |
---|
70 | return [(id, self.menu1, "Fitting"),(id2, menu2, "Model")] |
---|
71 | |
---|
72 | |
---|
73 | def help(self, evt): |
---|
74 | """ |
---|
75 | Show a general help dialog. |
---|
76 | TODO: replace the text with a nice image |
---|
77 | """ |
---|
78 | from helpDialog import HelpWindow |
---|
79 | dialog = HelpWindow(None, -1, 'HelpWindow') |
---|
80 | if dialog.ShowModal() == wx.ID_OK: |
---|
81 | pass |
---|
82 | dialog.Destroy() |
---|
83 | |
---|
84 | |
---|
85 | def get_context_menu(self, graph=None): |
---|
86 | """ |
---|
87 | Get the context menu items available for P(r) |
---|
88 | @param graph: the Graph object to which we attach the context menu |
---|
89 | @return: a list of menu items with call-back function |
---|
90 | """ |
---|
91 | self.graph=graph |
---|
92 | for item in graph.plottables: |
---|
93 | if item.__class__.__name__ is "Data2D": |
---|
94 | return [["Select data for Fitting",\ |
---|
95 | "Dialog with fitting parameters ", self._onSelect]] |
---|
96 | else: |
---|
97 | if item.name==graph.selected_plottable and\ |
---|
98 | item.__class__.__name__ is "Data1D": |
---|
99 | return [["Select data for Fitting", \ |
---|
100 | "Dialog with fitting parameters ", self._onSelect]] |
---|
101 | return [] |
---|
102 | |
---|
103 | |
---|
104 | def get_panels(self, parent): |
---|
105 | """ |
---|
106 | Create and return a list of panel objects |
---|
107 | """ |
---|
108 | self.parent = parent |
---|
109 | # Creation of the fit panel |
---|
110 | self.fit_panel = FitPanel(self.parent, -1) |
---|
111 | #Set the manager forthe main panel |
---|
112 | self.fit_panel.set_manager(self) |
---|
113 | # List of windows used for the perspective |
---|
114 | self.perspective = [] |
---|
115 | self.perspective.append(self.fit_panel.window_name) |
---|
116 | # take care of saving data, model and page associated with each other |
---|
117 | self.page_finder = {} |
---|
118 | #index number to create random model name |
---|
119 | self.index_model = 0 |
---|
120 | #create the fitting panel |
---|
121 | return [self.fit_panel] |
---|
122 | |
---|
123 | |
---|
124 | def get_perspective(self): |
---|
125 | """ |
---|
126 | Get the list of panel names for this perspective |
---|
127 | """ |
---|
128 | return self.perspective |
---|
129 | |
---|
130 | |
---|
131 | def on_perspective(self, event): |
---|
132 | """ |
---|
133 | Call back function for the perspective menu item. |
---|
134 | We notify the parent window that the perspective |
---|
135 | has changed. |
---|
136 | """ |
---|
137 | self.parent.set_perspective(self.perspective) |
---|
138 | |
---|
139 | |
---|
140 | def post_init(self): |
---|
141 | """ |
---|
142 | Post initialization call back to close the loose ends |
---|
143 | [Somehow openGL needs this call] |
---|
144 | """ |
---|
145 | self.parent.set_perspective(self.perspective) |
---|
146 | |
---|
147 | |
---|
148 | def _onSelect(self,event): |
---|
149 | """ |
---|
150 | when Select data to fit a new page is created .Its reference is |
---|
151 | added to self.page_finder |
---|
152 | """ |
---|
153 | self.panel = event.GetEventObject() |
---|
154 | for item in self.panel.graph.plottables: |
---|
155 | if item.name == self.panel.graph.selected_plottable or\ |
---|
156 | item.__class__.__name__ is "Data2D": |
---|
157 | #find a name for the page created for notebook |
---|
158 | try: |
---|
159 | page, model_name = self.fit_panel.add_fit_page(item) |
---|
160 | # add data associated to the page created |
---|
161 | |
---|
162 | if page !=None: |
---|
163 | |
---|
164 | #create a fitproblem storing all link to data,model,page creation |
---|
165 | self.page_finder[page]= FitProblem() |
---|
166 | self.page_finder[page].save_model_name(model_name) |
---|
167 | self.page_finder[page].add_data(item) |
---|
168 | except: |
---|
169 | wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ |
---|
170 | %sys.exc_value)) |
---|
171 | def schedule_for_fit(self,value=0,fitproblem =None): |
---|
172 | """ |
---|
173 | |
---|
174 | """ |
---|
175 | if fitproblem !=None: |
---|
176 | fitproblem.schedule_tofit(value) |
---|
177 | else: |
---|
178 | current_pg=self.fit_panel.get_current_page() |
---|
179 | for page, val in self.page_finder.iteritems(): |
---|
180 | if page ==current_pg : |
---|
181 | val.schedule_tofit(value) |
---|
182 | break |
---|
183 | |
---|
184 | |
---|
185 | def get_page_finder(self): |
---|
186 | """ @return self.page_finder used also by simfitpage.py""" |
---|
187 | return self.page_finder |
---|
188 | |
---|
189 | |
---|
190 | def set_page_finder(self,modelname,names,values): |
---|
191 | """ |
---|
192 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
193 | @param modelname: the name ot the model for with the parameter has to reset |
---|
194 | @param value: can be a string in this case. |
---|
195 | @param names: the paramter name |
---|
196 | @note: expecting park used for fit. |
---|
197 | """ |
---|
198 | sim_page=self.fit_panel.get_page(0) |
---|
199 | for page, value in self.page_finder.iteritems(): |
---|
200 | if page != sim_page: |
---|
201 | list=value.get_model() |
---|
202 | model=list[0] |
---|
203 | #print "fitting",model.name,modelname |
---|
204 | if model.name== modelname: |
---|
205 | value.set_model_param(names,values) |
---|
206 | break |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | def split_string(self,item): |
---|
211 | """ |
---|
212 | receive a word containing dot and split it. used to split parameterset |
---|
213 | name into model name and parameter name example: |
---|
214 | paramaterset (item) = M1.A |
---|
215 | @return model_name =M1 , parameter name =A |
---|
216 | """ |
---|
217 | if string.find(item,".")!=-1: |
---|
218 | param_names= re.split("\.",item) |
---|
219 | model_name=param_names[0] |
---|
220 | param_name=param_names[1] |
---|
221 | return model_name,param_name |
---|
222 | |
---|
223 | |
---|
224 | def _single_fit_completed(self,result,pars,cpage,qmin,qmax,ymin=None, ymax=None): |
---|
225 | """ |
---|
226 | Display fit result on one page of the notebook. |
---|
227 | @param result: result of fit |
---|
228 | @param pars: list of names of parameters fitted |
---|
229 | @param current_pg: the page where information will be displayed |
---|
230 | @param qmin: the minimum value of x to replot the model |
---|
231 | @param qmax: the maximum value of x to replot model |
---|
232 | |
---|
233 | """ |
---|
234 | try: |
---|
235 | for page, value in self.page_finder.iteritems(): |
---|
236 | if page==cpage : |
---|
237 | #fitdata = value.get_data() |
---|
238 | list = value.get_model() |
---|
239 | model= list[0] |
---|
240 | break |
---|
241 | i = 0 |
---|
242 | # print "fitting: single fit pars ", pars |
---|
243 | for name in pars: |
---|
244 | if result.pvec.__class__==numpy.float64: |
---|
245 | model.setParam(name,result.pvec) |
---|
246 | else: |
---|
247 | model.setParam(name,result.pvec[i]) |
---|
248 | # print "fitting: single fit", name, result.pvec[i] |
---|
249 | i += 1 |
---|
250 | # print "fitting result : chisqr",result.fitness |
---|
251 | # print "fitting result : pvec",result.pvec |
---|
252 | # print "fitting result : stderr",result.stderr |
---|
253 | |
---|
254 | cpage.onsetValues(result.fitness, result.pvec,result.stderr) |
---|
255 | self.plot_helper(currpage=cpage,qmin=qmin,qmax=qmax,ymin=ymin, ymax=ymax) |
---|
256 | except: |
---|
257 | raise |
---|
258 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
259 | |
---|
260 | |
---|
261 | def _simul_fit_completed(self,result,qmin,qmax,ymin=None, ymax=None): |
---|
262 | """ |
---|
263 | Parameter estimation completed, |
---|
264 | display the results to the user |
---|
265 | @param alpha: estimated best alpha |
---|
266 | @param elapsed: computation time |
---|
267 | """ |
---|
268 | try: |
---|
269 | for page, value in self.page_finder.iteritems(): |
---|
270 | if value.get_scheduled()==1: |
---|
271 | #fitdata = value.get_data() |
---|
272 | list = value.get_model() |
---|
273 | model= list[0] |
---|
274 | |
---|
275 | small_out = [] |
---|
276 | small_cov = [] |
---|
277 | i = 0 |
---|
278 | #Separate result in to data corresponding to each page |
---|
279 | for p in result.parameters: |
---|
280 | model_name,param_name = self.split_string(p.name) |
---|
281 | if model.name == model_name: |
---|
282 | small_out.append(p.value ) |
---|
283 | small_cov.append(p.stderr) |
---|
284 | model.setParam(param_name,p.value) |
---|
285 | # Display result on each page |
---|
286 | page.onsetValues(result.fitness, small_out,small_cov) |
---|
287 | #Replot model |
---|
288 | self.plot_helper(currpage= page,qmin= qmin,qmax= qmax,ymin=ymin, ymax=ymax) |
---|
289 | except: |
---|
290 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
291 | |
---|
292 | |
---|
293 | def _on_single_fit(self,id=None,qmin=None,qmax=None,ymin=None,ymax=None): |
---|
294 | """ |
---|
295 | perform fit for the current page and return chisqr,out and cov |
---|
296 | @param engineName: type of fit to be performed |
---|
297 | @param id: unique id corresponding to a fit problem(model, set of data) |
---|
298 | @param model: model to fit |
---|
299 | |
---|
300 | """ |
---|
301 | #print "in single fitting" |
---|
302 | #set an engine to perform fit |
---|
303 | from sans.fit.Fitting import Fit |
---|
304 | self.fitter= Fit(self._fit_engine) |
---|
305 | #Setting an id to store model and data in fit engine |
---|
306 | if id==None: |
---|
307 | id=0 |
---|
308 | self.id = id |
---|
309 | page_fitted=None |
---|
310 | fit_problem=None |
---|
311 | #Get information (model , data) related to the page on |
---|
312 | #with the fit will be perform |
---|
313 | #current_pg=self.fit_panel.get_current_page() |
---|
314 | #simul_pg=self.fit_panel.get_page(0) |
---|
315 | |
---|
316 | for page, value in self.page_finder.iteritems(): |
---|
317 | if value.get_scheduled() ==1 : |
---|
318 | metadata = value.get_data() |
---|
319 | list=value.get_model() |
---|
320 | model=list[0] |
---|
321 | smearer= value.get_smearer() |
---|
322 | #Create list of parameters for fitting used |
---|
323 | pars=[] |
---|
324 | templist=[] |
---|
325 | try: |
---|
326 | #templist=current_pg.get_param_list() |
---|
327 | templist=page.get_param_list() |
---|
328 | for element in templist: |
---|
329 | pars.append(str(element[0].GetLabelText())) |
---|
330 | pars.sort() |
---|
331 | #Do the single fit |
---|
332 | self.fitter.set_model(Model(model), self.id, pars) |
---|
333 | |
---|
334 | self.fitter.set_data(metadata,self.id,smearer, qmin,qmax) |
---|
335 | self.fitter.select_problem_for_fit(Uid=self.id,value=value.get_scheduled()) |
---|
336 | page_fitted=page |
---|
337 | self.id+=1 |
---|
338 | self.schedule_for_fit( 0,value) |
---|
339 | except: |
---|
340 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
341 | return |
---|
342 | # make sure to keep an alphabetic order |
---|
343 | #of parameter names in the list |
---|
344 | try: |
---|
345 | result=self.fitter.fit() |
---|
346 | #self._single_fit_completed(result,pars,current_pg,qmin,qmax) |
---|
347 | #print "single_fit: result",result.fitness,result.pvec,result.stderr |
---|
348 | #self._single_fit_completed(result,pars,page,qmin,qmax) |
---|
349 | self._single_fit_completed(result,pars,page_fitted,qmin,qmax,ymin,ymax) |
---|
350 | except: |
---|
351 | raise |
---|
352 | wx.PostEvent(self.parent, StatusEvent(status="Single Fit error: %s" % sys.exc_value)) |
---|
353 | return |
---|
354 | |
---|
355 | def _on_simul_fit(self, id=None,qmin=None,qmax=None, ymin=None, ymax=None): |
---|
356 | """ |
---|
357 | perform fit for all the pages selected on simpage and return chisqr,out and cov |
---|
358 | @param engineName: type of fit to be performed |
---|
359 | @param id: unique id corresponding to a fit problem(model, set of data) |
---|
360 | in park_integration |
---|
361 | @param model: model to fit |
---|
362 | |
---|
363 | """ |
---|
364 | #set an engine to perform fit |
---|
365 | from sans.fit.Fitting import Fit |
---|
366 | self.fitter= Fit(self._fit_engine) |
---|
367 | |
---|
368 | #Setting an id to store model and data |
---|
369 | if id==None: |
---|
370 | id = 0 |
---|
371 | self.id = id |
---|
372 | |
---|
373 | for page, value in self.page_finder.iteritems(): |
---|
374 | try: |
---|
375 | if value.get_scheduled()==1: |
---|
376 | metadata = value.get_data() |
---|
377 | list = value.get_model() |
---|
378 | model= list[0] |
---|
379 | #Create dictionary of parameters for fitting used |
---|
380 | pars = [] |
---|
381 | templist = [] |
---|
382 | templist = page.get_param_list() |
---|
383 | for element in templist: |
---|
384 | try: |
---|
385 | name = str(element[0].GetLabelText()) |
---|
386 | pars.append(name) |
---|
387 | except: |
---|
388 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
389 | return |
---|
390 | new_model=Model(model) |
---|
391 | param=value.get_model_param() |
---|
392 | |
---|
393 | if len(param)>0: |
---|
394 | for item in param: |
---|
395 | param_value = item[1] |
---|
396 | param_name = item[0] |
---|
397 | #print "fitting ", param,param_name, param_value |
---|
398 | |
---|
399 | #new_model.set( model.getParam(param_name[0])= param_value) |
---|
400 | #new_model.set( exec"%s=%s"%(param_name[0], param_value)) |
---|
401 | #new_model.set( exec "%s"%(param_nam) = param_value) |
---|
402 | new_model.parameterset[ param_name].set( param_value ) |
---|
403 | |
---|
404 | self.fitter.set_model(new_model, self.id, pars) |
---|
405 | self.fitter.set_data(metadata,self.id,qmin,qmax,ymin,ymax) |
---|
406 | self.fitter.select_problem_for_fit(Uid=self.id,value=value.get_scheduled()) |
---|
407 | self.id += 1 |
---|
408 | except: |
---|
409 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
410 | return |
---|
411 | #Do the simultaneous fit |
---|
412 | try: |
---|
413 | result=self.fitter.fit() |
---|
414 | self._simul_fit_completed(result,qmin,qmax,ymin,ymax) |
---|
415 | except: |
---|
416 | wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) |
---|
417 | return |
---|
418 | |
---|
419 | |
---|
420 | def _onset_engine(self,event): |
---|
421 | """ set engine to scipy""" |
---|
422 | if self._fit_engine== 'park': |
---|
423 | self._on_change_engine('scipy') |
---|
424 | else: |
---|
425 | self._on_change_engine('park') |
---|
426 | wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) |
---|
427 | |
---|
428 | |
---|
429 | def _on_change_engine(self, engine='park'): |
---|
430 | """ |
---|
431 | Allow to select the type of engine to perform fit |
---|
432 | @param engine: the key work of the engine |
---|
433 | """ |
---|
434 | self._fit_engine = engine |
---|
435 | |
---|
436 | |
---|
437 | def _on_model_panel(self, evt): |
---|
438 | """ |
---|
439 | react to model selection on any combo box or model menu.plot the model |
---|
440 | """ |
---|
441 | |
---|
442 | model = evt.model |
---|
443 | name = evt.name |
---|
444 | sim_page=self.fit_panel.get_page(0) |
---|
445 | current_pg = self.fit_panel.get_current_page() |
---|
446 | selected_page = self.fit_panel.get_selected_page() |
---|
447 | if current_pg != sim_page: |
---|
448 | current_pg.set_panel(model) |
---|
449 | model.name = self.page_finder[current_pg].get_name() |
---|
450 | try: |
---|
451 | metadata=self.page_finder[current_pg].get_data() |
---|
452 | M_name=model.name+"= "+name+"("+metadata.group_id+")" |
---|
453 | except: |
---|
454 | M_name=model.name+"= "+name |
---|
455 | #model.name="M"+str(self.index_model) |
---|
456 | self.index_model += 1 |
---|
457 | # save model name |
---|
458 | |
---|
459 | # save the name containing the data name with the appropriate model |
---|
460 | self.page_finder[current_pg].set_model(model,M_name) |
---|
461 | self.plot_helper(currpage= current_pg,qmin= None,qmax= None) |
---|
462 | sim_page.add_model(self.page_finder) |
---|
463 | |
---|
464 | def set_smearer(self,smearer): |
---|
465 | current_pg=self.fit_panel.get_current_page() |
---|
466 | self.page_finder[current_pg].set_smearer(smearer) |
---|
467 | |
---|
468 | def redraw_model(self,qmin= None,qmax= None): |
---|
469 | """ |
---|
470 | Draw a theory according to model changes or data range. |
---|
471 | @param qmin: the minimum value plotted for theory |
---|
472 | @param qmax: the maximum value plotted for theory |
---|
473 | """ |
---|
474 | current_pg=self.fit_panel.get_current_page() |
---|
475 | for page, value in self.page_finder.iteritems(): |
---|
476 | if page ==current_pg : |
---|
477 | break |
---|
478 | self.plot_helper(currpage=page,qmin= qmin,qmax= qmax) |
---|
479 | |
---|
480 | def plot_helper(self,currpage,qmin=None,qmax=None,ymin=None,ymax=None): |
---|
481 | """ |
---|
482 | Plot a theory given a model and data |
---|
483 | @param model: the model from where the theory is derived |
---|
484 | @param currpage: page in a dictionary referring to some data |
---|
485 | """ |
---|
486 | if self.fit_panel.get_page_count() >1: |
---|
487 | for page in self.page_finder.iterkeys(): |
---|
488 | if page==currpage : |
---|
489 | data=self.page_finder[page].get_data() |
---|
490 | list=self.page_finder[page].get_model() |
---|
491 | model=list[0] |
---|
492 | break |
---|
493 | |
---|
494 | if data!=None and data.__class__.__name__ != 'Data2D': |
---|
495 | theory = Theory1D(x=[], y=[]) |
---|
496 | theory.name = model.name |
---|
497 | theory.group_id = data.group_id |
---|
498 | theory.id = "Model" |
---|
499 | x_name, x_units = data.get_xaxis() |
---|
500 | y_name, y_units = data.get_yaxis() |
---|
501 | theory.xaxis(x_name, x_units) |
---|
502 | theory.yaxis(y_name, y_units) |
---|
503 | if qmin == None : |
---|
504 | qmin = min(data.x) |
---|
505 | if qmax == None : |
---|
506 | qmax = max(data.x) |
---|
507 | try: |
---|
508 | tempx = qmin |
---|
509 | tempy = model.run(qmin) |
---|
510 | theory.x.append(tempx) |
---|
511 | theory.y.append(tempy) |
---|
512 | except : |
---|
513 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
514 | skipping point x %g %s" %(qmin, sys.exc_value))) |
---|
515 | |
---|
516 | for i in range(len(data.x)): |
---|
517 | try: |
---|
518 | if data.x[i]> qmin and data.x[i]< qmax: |
---|
519 | tempx = data.x[i] |
---|
520 | tempy = model.run(tempx) |
---|
521 | theory.x.append(tempx) |
---|
522 | theory.y.append(tempy) |
---|
523 | |
---|
524 | except: |
---|
525 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
526 | skipping point x %g %s" %(data.x[i], sys.exc_value))) |
---|
527 | try: |
---|
528 | tempx = qmax |
---|
529 | tempy = model.run(qmax) |
---|
530 | theory.x.append(tempx) |
---|
531 | theory.y.append(tempy) |
---|
532 | except: |
---|
533 | wx.PostEvent(self.parent, StatusEvent(status="fitting \ |
---|
534 | skipping point x %g %s" %(qmax, sys.exc_value))) |
---|
535 | |
---|
536 | else: |
---|
537 | theory=Data2D(data.data, data.err_data) |
---|
538 | theory.name= model.name |
---|
539 | theory.id= "Model" |
---|
540 | theory.group_id= "Model"+data.name |
---|
541 | theory.x_bins= data.x_bins |
---|
542 | theory.y_bins= data.y_bins |
---|
543 | tempy=[] |
---|
544 | if qmin==None: |
---|
545 | qmin=data.xmin |
---|
546 | if qmax==None: |
---|
547 | qmax=data.xmax |
---|
548 | if ymin==None: |
---|
549 | ymin=data.ymin |
---|
550 | if ymax==None: |
---|
551 | ymax=data.ymax |
---|
552 | |
---|
553 | theory.data = numpy.zeros((len(data.y_bins),len(data.x_bins))) |
---|
554 | for i in range(len(data.y_bins)): |
---|
555 | if data.y_bins[i]>= ymin and data.y_bins[i]<= ymax: |
---|
556 | for j in range(len(data.x_bins)): |
---|
557 | if data.x_bins[i]>= qmin and data.x_bins[i]<= qmax: |
---|
558 | theory.data[j][i]=model.runXY([data.x_bins[j],data.y_bins[i]]) |
---|
559 | |
---|
560 | #print "fitting : plot_helper:", theory.image |
---|
561 | #print data.image |
---|
562 | #print "fitting : plot_helper:",theory.image |
---|
563 | theory.detector= data.detector |
---|
564 | theory.source= data.source |
---|
565 | theory.zmin= data.zmin |
---|
566 | theory.zmax= data.zmax |
---|
567 | theory.xmin= qmin |
---|
568 | theory.xmax= qmax |
---|
569 | theory.ymin= ymin |
---|
570 | theory.ymax= ymax |
---|
571 | |
---|
572 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
573 | title="Analytical model %s"%str(data.name))) |
---|
574 | |
---|
575 | |
---|
576 | def _on_model_menu(self, evt): |
---|
577 | """ |
---|
578 | Plot a theory from a model selected from the menu |
---|
579 | """ |
---|
580 | model=evt.model() |
---|
581 | #name="Model View" |
---|
582 | #print "mon menu",model.name |
---|
583 | description=model.description |
---|
584 | #self.fit_panel.add_model_page(model,description,name) |
---|
585 | |
---|
586 | self.draw_model(model,self.enable_model2D) |
---|
587 | |
---|
588 | def draw_model(self,model,description=None,enable1D=True, enable2D=False,qmin=None, qmax=None,qstep=None): |
---|
589 | """ |
---|
590 | draw model with default data value |
---|
591 | """ |
---|
592 | self.fit_panel.add_model_page(model,model.description,model.name) |
---|
593 | self._draw_model2D(model,model.description, enable2D,qmin,qmax,qstep) |
---|
594 | self._draw_model1D(model,model.description, enable1D,qmin,qmax, qstep) |
---|
595 | |
---|
596 | def _draw_model1D(self,model,description=None, enable1D=True,qmin=None,qmax=None, qstep=None): |
---|
597 | |
---|
598 | if enable1D: |
---|
599 | if qmin==None: |
---|
600 | qmin= 0.001 |
---|
601 | if qmax==None: |
---|
602 | qmax= 1.0 |
---|
603 | if qstep ==None: |
---|
604 | qstep =0.001 |
---|
605 | |
---|
606 | print "x in data1D",qmin,qmax |
---|
607 | x = numpy.arange(qmin, qmax, qstep) |
---|
608 | xlen= len(x) |
---|
609 | y = numpy.zeros(xlen) |
---|
610 | if not enable1D: |
---|
611 | for i in range(xlen): |
---|
612 | y[i] = model.run(x[i]) |
---|
613 | |
---|
614 | try: |
---|
615 | new_plot = Theory1D(x, y) |
---|
616 | #new_plot.name = model.name |
---|
617 | new_plot.name = model.name |
---|
618 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
619 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
620 | new_plot.id = "Model" |
---|
621 | new_plot.group_id ="Model" |
---|
622 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Analytical model 1D")) |
---|
623 | |
---|
624 | except: |
---|
625 | raise |
---|
626 | else: |
---|
627 | for i in range(xlen): |
---|
628 | y[i] = model.run(x[i]) |
---|
629 | |
---|
630 | try: |
---|
631 | new_plot = Theory1D(x, y) |
---|
632 | new_plot.name = model.name |
---|
633 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
634 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
635 | new_plot.id ="Model" |
---|
636 | new_plot.group_id ="Model" |
---|
637 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
638 | title="Analytical model 1D %s"%str(model.name) )) |
---|
639 | |
---|
640 | except: |
---|
641 | raise |
---|
642 | def _draw_model2D(self,model,description=None, enable2D=False,qmin=None,qmax=None, qstep=None): |
---|
643 | if qmin==None: |
---|
644 | qmin= -0.05 |
---|
645 | if qmax==None: |
---|
646 | qmax= 0.05 |
---|
647 | if qstep ==None: |
---|
648 | qstep =0.001 |
---|
649 | x = numpy.arange(qmin,qmax, qstep) |
---|
650 | y = numpy.arange(qmin,qmax,qstep) |
---|
651 | lx = len(x) |
---|
652 | #print x |
---|
653 | data=numpy.zeros([len(x),len(y)]) |
---|
654 | if enable2D: |
---|
655 | |
---|
656 | for i_x in range(int(len(x))): |
---|
657 | for i_y in range(len(y)): |
---|
658 | try: |
---|
659 | value = model.runXY([x[i_x],y[i_y]]) |
---|
660 | data[i_y][i_x] = value |
---|
661 | except: |
---|
662 | wx.PostEvent(self.parent, StatusEvent(status="\ |
---|
663 | Error computing %s at [%g,%g] :%s" %(model.name,x[i_x],y[i_y], sys.exc_value))) |
---|
664 | |
---|
665 | theory= Data2D(data) |
---|
666 | theory.name= model.name |
---|
667 | theory.group_id ="Model" |
---|
668 | theory.id ="Model" |
---|
669 | theory.xmin= qmin |
---|
670 | theory.xmax= qmax |
---|
671 | theory.ymin= qmin |
---|
672 | theory.ymax= qmax |
---|
673 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
674 | title="Analytical model 2D %s" %str(model.name))) |
---|
675 | |
---|
676 | |
---|
677 | |
---|
678 | def H_draw_model2D(self,model,description=None, enable2D=False,qmin=None,qmax=None, qstep=None): |
---|
679 | if qmin==None: |
---|
680 | qmin= -0.05 |
---|
681 | if qmax==None: |
---|
682 | qmax= 0.05 |
---|
683 | if qstep ==None: |
---|
684 | qstep =0.001 |
---|
685 | x = numpy.arange(qmin,qmax, qstep) |
---|
686 | y = numpy.arange(qmin,qmax,qstep) |
---|
687 | lx = len(self.x) |
---|
688 | if enable2D: |
---|
689 | data=numpy.zeros([len(x),len(y)]) |
---|
690 | for i in range(len(x)): |
---|
691 | for j in range(len(x)): |
---|
692 | try: |
---|
693 | data[i][j]=model.runXY([j,i]) |
---|
694 | except: |
---|
695 | wx.PostEvent(self.parent, StatusEvent(status="\ |
---|
696 | Model 2D cannot be plot %g %s %s" %(data[i][j],model.name, sys.exc_value))) |
---|
697 | theory = Data2D(data) |
---|
698 | theory.group_id =str(model.name)+" 2D" |
---|
699 | theory.xmin= qmin |
---|
700 | theory.xmax= qmax |
---|
701 | theory.ymin= qmin |
---|
702 | theory.ymax= qmax |
---|
703 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, title="Analytical model 2D")) |
---|
704 | |
---|
705 | |
---|
706 | def on_draw_model2D(self, event): |
---|
707 | """ |
---|
708 | plot view model 2D |
---|
709 | """ |
---|
710 | |
---|
711 | if self.enable_model2D== True: |
---|
712 | self.enable_model2D=False |
---|
713 | else: |
---|
714 | self.enable_model2D=True |
---|
715 | print "self.enable_model2D",self.enable_model2D |
---|
716 | if __name__ == "__main__": |
---|
717 | i = Plugin() |
---|
718 | |
---|
719 | |
---|
720 | |
---|
721 | |
---|