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