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