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