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