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