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