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