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