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