[5612152] | 1 | |
---|
| 2 | |
---|
[5062bbf] | 3 | ################################################################################ |
---|
| 4 | #This software was developed by the University of Tennessee as part of the |
---|
| 5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 6 | #project funded by the US National Science Foundation. |
---|
| 7 | # |
---|
| 8 | #See the license text in license.txt |
---|
| 9 | # |
---|
| 10 | #copyright 2009, University of Tennessee |
---|
| 11 | ################################################################################ |
---|
| 12 | |
---|
| 13 | |
---|
[edcbd467] | 14 | import re |
---|
[5536035] | 15 | import sys |
---|
| 16 | import wx |
---|
| 17 | import logging |
---|
| 18 | import numpy |
---|
| 19 | import math |
---|
| 20 | import string |
---|
[c3b4dcb] | 21 | import time |
---|
| 22 | import thread |
---|
[edcbd467] | 23 | from copy import deepcopy |
---|
[2fff4db] | 24 | |
---|
[f83b94a] | 25 | import models |
---|
| 26 | import fitpage |
---|
| 27 | |
---|
[7f69d4d] | 28 | from DataLoader.loader import Loader |
---|
[d89f09b] | 29 | |
---|
[c3b4dcb] | 30 | from sans.guiframe.dataFitting import Data2D |
---|
| 31 | from sans.guiframe.dataFitting import Data1D |
---|
[c202d03] | 32 | from sans.guiframe.dataFitting import Theory1D |
---|
[2fff4db] | 33 | |
---|
[d89f09b] | 34 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[848a2ef] | 35 | from sans.guicomm.events import EVT_SLICER_PANEL,ERR_DATA,EVT_REMOVE_DATA |
---|
| 36 | from sans.guicomm.events import EVT_SLICER_PARS_UPDATE |
---|
[7a07864] | 37 | from sans.guicomm.events import EVT_FITSTATE_UPDATE |
---|
[6ed82c7] | 38 | |
---|
[c3b4dcb] | 39 | from sans.fit.AbstractFitEngine import Model |
---|
| 40 | from sans.fit.AbstractFitEngine import FitAbort |
---|
[e54d2c32] | 41 | from console import ConsoleUpdate |
---|
[4faf4ba] | 42 | |
---|
[d89f09b] | 43 | from fitproblem import FitProblem |
---|
[a704bb5] | 44 | from fitpanel import FitPanel |
---|
[e9b4cc4] | 45 | from fit_thread import FitThread |
---|
[f83b94a] | 46 | from pagestate import Reader |
---|
[ed2ea6a] | 47 | |
---|
[d250f7d] | 48 | DEFAULT_BEAM = 0.005 |
---|
[6ced1cc] | 49 | DEFAULT_QMIN = 0.001 |
---|
[9be7432] | 50 | DEFAULT_QMAX = 0.13 |
---|
[7b758fd] | 51 | DEFAULT_NPTS = 50 |
---|
[77e23a2] | 52 | |
---|
[6f023e8] | 53 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
[7a07864] | 54 | #(FitStateUpdateEvent, EVT_STATE_UPDATE) = wx.lib.newevent.NewEvent() |
---|
[f72333f] | 55 | from fitpage import Chi2UpdateEvent |
---|
[5062bbf] | 56 | |
---|
| 57 | |
---|
[c77d859] | 58 | class PlotInfo: |
---|
| 59 | """ |
---|
[5062bbf] | 60 | store some plotting field |
---|
[c77d859] | 61 | """ |
---|
| 62 | _xunit = 'A^{-1}' |
---|
| 63 | _xaxis= "\\rm{Q}" |
---|
| 64 | _yunit = "cm^{-1}" |
---|
| 65 | _yaxis= "\\rm{Intensity} " |
---|
| 66 | id = "Model" |
---|
| 67 | group_id = "Model" |
---|
| 68 | title= None |
---|
| 69 | info= None |
---|
| 70 | |
---|
| 71 | |
---|
[d89f09b] | 72 | class Plugin: |
---|
| 73 | """ |
---|
[5062bbf] | 74 | Fitting plugin is used to perform fit |
---|
[d89f09b] | 75 | """ |
---|
| 76 | def __init__(self): |
---|
[5062bbf] | 77 | """ |
---|
| 78 | """ |
---|
[d89f09b] | 79 | ## Plug-in name |
---|
| 80 | self.sub_menu = "Fitting" |
---|
| 81 | |
---|
| 82 | ## Reference to the parent window |
---|
| 83 | self.parent = None |
---|
[ed2ea6a] | 84 | #Provide list of models existing in the application |
---|
[d89f09b] | 85 | self.menu_mng = models.ModelManager() |
---|
| 86 | ## List of panels for the simulation perspective (names) |
---|
| 87 | self.perspective = [] |
---|
[ed2ea6a] | 88 | #list of panel to send to guiframe |
---|
[f83b94a] | 89 | self.mypanels = [] |
---|
[ed2ea6a] | 90 | # reference to the current running thread |
---|
[f83b94a] | 91 | self.calc_2D = None |
---|
| 92 | self.calc_1D = None |
---|
| 93 | self.calc_fit = None |
---|
[c77d859] | 94 | |
---|
[d89f09b] | 95 | # Start with a good default |
---|
| 96 | self.elapsed = 0.022 |
---|
[ed2ea6a] | 97 | # the type of optimizer selected, park or scipy |
---|
[d89f09b] | 98 | self.fitter = None |
---|
[c660493] | 99 | #let fit ready |
---|
| 100 | self.fitproblem_count = None |
---|
[ed2ea6a] | 101 | #Flag to let the plug-in know that it is running stand alone |
---|
[f83b94a] | 102 | self.standalone = True |
---|
[6f023e8] | 103 | ## dictionary of page closed and id |
---|
[f83b94a] | 104 | self.closed_page_dict = {} |
---|
[d89f09b] | 105 | ## Fit engine |
---|
[e9e914f] | 106 | self._fit_engine = 'scipy' |
---|
[ed2ea6a] | 107 | #List of selected data |
---|
[f83b94a] | 108 | self.selected_data_list = [] |
---|
[c3435b7f] | 109 | ## list of slicer panel created to display slicer parameters and results |
---|
[f83b94a] | 110 | self.slicer_panels = [] |
---|
[32d802f] | 111 | # model 2D view |
---|
[f83b94a] | 112 | self.model2D_id = None |
---|
[ed2ea6a] | 113 | #keep reference of the simultaneous fit page |
---|
[f83b94a] | 114 | self.sim_page = None |
---|
[ed2ea6a] | 115 | #dictionary containing data name and error on dy of that data |
---|
[484faf7] | 116 | self.err_dy = {} |
---|
[f83b94a] | 117 | self.theory_data = None |
---|
| 118 | #Create a reader for fit page's state |
---|
[3bf4a032] | 119 | self.state_reader = None |
---|
[4da35bc] | 120 | self.temp_state = [] |
---|
[ef16f59] | 121 | self.state_index = 0 |
---|
[b63dc6e] | 122 | self.sfile_ext = None |
---|
[f83b94a] | 123 | # Log startup |
---|
| 124 | logging.info("Fitting plug-in started") |
---|
[2a8fac1] | 125 | |
---|
[d89f09b] | 126 | def populate_menu(self, id, owner): |
---|
| 127 | """ |
---|
[5062bbf] | 128 | Create a menu for the Fitting plug-in |
---|
| 129 | |
---|
| 130 | :param id: id to create a menu |
---|
| 131 | :param owner: owner of menu |
---|
| 132 | |
---|
| 133 | :return: list of information to populate the main menu |
---|
| 134 | |
---|
[d89f09b] | 135 | """ |
---|
| 136 | #Menu for fitting |
---|
| 137 | self.menu1 = wx.Menu() |
---|
[6f023e8] | 138 | |
---|
[b5c537f] | 139 | #Set park engine |
---|
[3b19ac9] | 140 | id3 = wx.NewId() |
---|
[bb18ef1] | 141 | scipy_help= "Scipy Engine: Perform Simple fit. More in Help window...." |
---|
[89ef796] | 142 | self.menu1.AppendCheckItem(id3, "Simple Fit [Scipy]",scipy_help) |
---|
[bb18ef1] | 143 | wx.EVT_MENU(owner, id3, self._onset_engine_scipy) |
---|
[6f023e8] | 144 | |
---|
[bb18ef1] | 145 | id3 = wx.NewId() |
---|
| 146 | park_help = "Park Engine: Perform Complex fit. More in Help window...." |
---|
[89ef796] | 147 | self.menu1.AppendCheckItem(id3, "Complex Fit [Park]",park_help) |
---|
[bb18ef1] | 148 | wx.EVT_MENU(owner, id3, self._onset_engine_park) |
---|
[707436d] | 149 | |
---|
| 150 | self.menu1.FindItemByPosition(0).Check(True) |
---|
| 151 | self.menu1.FindItemByPosition(1).Check(False) |
---|
| 152 | |
---|
[6f023e8] | 153 | self.menu1.AppendSeparator() |
---|
| 154 | id1 = wx.NewId() |
---|
| 155 | simul_help = "Allow to edit fit engine with multiple model and data" |
---|
[89ef796] | 156 | self.menu1.Append(id1, '&Simultaneous Page',simul_help) |
---|
[6f023e8] | 157 | wx.EVT_MENU(owner, id1, self.on_add_sim_page) |
---|
[2db1d66] | 158 | |
---|
[d89f09b] | 159 | #menu for model |
---|
| 160 | menu2 = wx.Menu() |
---|
| 161 | self.menu_mng.populate_menu(menu2, owner) |
---|
| 162 | id2 = wx.NewId() |
---|
| 163 | owner.Bind(models.EVT_MODEL,self._on_model_menu) |
---|
[bb18ef1] | 164 | |
---|
[d89f09b] | 165 | self.fit_panel.set_owner(owner) |
---|
| 166 | self.fit_panel.set_model_list(self.menu_mng.get_model_list()) |
---|
[0b12abb5] | 167 | |
---|
[3b19ac9] | 168 | #create menubar items |
---|
[484faf7] | 169 | return [(id, self.menu1, "Fitting")] |
---|
[2db1d66] | 170 | |
---|
[51d47b5] | 171 | def on_add_sim_page(self, event): |
---|
[ed2ea6a] | 172 | """ |
---|
[5062bbf] | 173 | Create a page to access simultaneous fit option |
---|
[ed2ea6a] | 174 | """ |
---|
[05971e1] | 175 | Plugin.on_perspective(self,event=event) |
---|
[2140e68] | 176 | if self.sim_page !=None: |
---|
| 177 | msg= "Simultaneous Fit page already opened" |
---|
| 178 | wx.PostEvent(self.parent, StatusEvent(status= msg)) |
---|
| 179 | return |
---|
| 180 | |
---|
[51d47b5] | 181 | self.sim_page= self.fit_panel.add_sim_page() |
---|
[b28717b] | 182 | |
---|
[d89f09b] | 183 | def help(self, evt): |
---|
| 184 | """ |
---|
[5062bbf] | 185 | Show a general help dialog. |
---|
[d89f09b] | 186 | """ |
---|
[484faf7] | 187 | |
---|
| 188 | from help_panel import HelpWindow |
---|
[2268d10] | 189 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
| 190 | frame.Show(True) |
---|
| 191 | |
---|
[d89f09b] | 192 | def get_context_menu(self, graph=None): |
---|
| 193 | """ |
---|
[5062bbf] | 194 | Get the context menu items available for P(r).them allow fitting option |
---|
| 195 | for Data2D and Data1D only. |
---|
| 196 | |
---|
| 197 | :param graph: the Graph object to which we attach the context menu |
---|
| 198 | |
---|
| 199 | :return: a list of menu items with call-back function |
---|
| 200 | |
---|
| 201 | :note: if Data1D was generated from Theory1D |
---|
| 202 | the fitting option is not allowed |
---|
| 203 | |
---|
[d89f09b] | 204 | """ |
---|
[7a77859] | 205 | self.graph = graph |
---|
[484faf7] | 206 | fit_option = "Select data for fitting" |
---|
| 207 | fit_hint = "Dialog with fitting parameters " |
---|
[b324aa9] | 208 | |
---|
[d89f09b] | 209 | for item in graph.plottables: |
---|
[693ab78] | 210 | if item.__class__.__name__ is "Data2D": |
---|
[c3b4dcb] | 211 | |
---|
[d902cf0] | 212 | if hasattr(item,"is_data"): |
---|
| 213 | if item.is_data: |
---|
[484faf7] | 214 | return [[fit_option, fit_hint, self._onSelect]] |
---|
[d902cf0] | 215 | else: |
---|
| 216 | return [] |
---|
[484faf7] | 217 | return [[fit_option, fit_hint, self._onSelect]] |
---|
[6f73a08] | 218 | else: |
---|
[f71c2de] | 219 | if item.name == graph.selected_plottable : |
---|
| 220 | if item.name in ["$I_{obs}(q)$","$I_{fit}(q)$","$P_{fit}(r)$"]: |
---|
| 221 | return [] |
---|
| 222 | if hasattr(item, "group_id"): |
---|
| 223 | # if is_data is true , this in an actual data loaded |
---|
| 224 | #else it is a data created from a theory model |
---|
| 225 | if hasattr(item,"is_data"): |
---|
| 226 | if item.is_data: |
---|
| 227 | return [[fit_option, fit_hint, |
---|
| 228 | self._onSelect]] |
---|
[d902cf0] | 229 | else: |
---|
[f71c2de] | 230 | return [] |
---|
| 231 | else: |
---|
| 232 | return [[fit_option, fit_hint, self._onSelect]] |
---|
[d89f09b] | 233 | return [] |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | def get_panels(self, parent): |
---|
| 237 | """ |
---|
[5062bbf] | 238 | Create and return a list of panel objects |
---|
[d89f09b] | 239 | """ |
---|
| 240 | self.parent = parent |
---|
[7a07864] | 241 | self.parent.Bind(EVT_FITSTATE_UPDATE, self.on_set_state_helper) |
---|
[d89f09b] | 242 | # Creation of the fit panel |
---|
| 243 | self.fit_panel = FitPanel(self.parent, -1) |
---|
[f93dfcb] | 244 | #Set the manager for the main panel |
---|
[d89f09b] | 245 | self.fit_panel.set_manager(self) |
---|
| 246 | # List of windows used for the perspective |
---|
| 247 | self.perspective = [] |
---|
| 248 | self.perspective.append(self.fit_panel.window_name) |
---|
| 249 | # take care of saving data, model and page associated with each other |
---|
[3b19ac9] | 250 | self.page_finder = {} |
---|
| 251 | #index number to create random model name |
---|
| 252 | self.index_model = 0 |
---|
[264df67] | 253 | self.index_theory= 0 |
---|
[32673ac] | 254 | self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) |
---|
[edcbd467] | 255 | self.parent.Bind(ERR_DATA, self._on_data_error) |
---|
[848a2ef] | 256 | self.parent.Bind(EVT_REMOVE_DATA, self._closed_fitpage) |
---|
| 257 | self.parent.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) |
---|
[df4c3ad] | 258 | self.parent._mgr.Bind(wx.aui.EVT_AUI_PANE_CLOSE,self._onclearslicer) |
---|
[3bf4a032] | 259 | #Create reader when fitting panel are created |
---|
| 260 | self.state_reader = Reader(self.set_state) |
---|
| 261 | #append that reader to list of available reader |
---|
| 262 | loader = Loader() |
---|
| 263 | loader.associate_file_reader(".fitv", self.state_reader) |
---|
[b35d3d1] | 264 | loader.associate_file_reader(".svs", self.state_reader) |
---|
[9b18735] | 265 | |
---|
[f93dfcb] | 266 | #Send the fitting panel to guiframe |
---|
[568e1a5] | 267 | self.mypanels.append(self.fit_panel) |
---|
| 268 | return self.mypanels |
---|
[232c3db] | 269 | |
---|
[d89f09b] | 270 | def get_perspective(self): |
---|
| 271 | """ |
---|
[5062bbf] | 272 | Get the list of panel names for this perspective |
---|
[d89f09b] | 273 | """ |
---|
| 274 | return self.perspective |
---|
| 275 | |
---|
| 276 | def on_perspective(self, event): |
---|
| 277 | """ |
---|
[5062bbf] | 278 | Call back function for the perspective menu item. |
---|
| 279 | We notify the parent window that the perspective |
---|
| 280 | has changed. |
---|
[d89f09b] | 281 | """ |
---|
| 282 | self.parent.set_perspective(self.perspective) |
---|
[9237df4] | 283 | |
---|
[1610976] | 284 | def set_default_perspective(self): |
---|
| 285 | """ |
---|
[5062bbf] | 286 | Call back method that True to notify the parent that the current plug-in |
---|
| 287 | can be set as default perspective. |
---|
| 288 | when returning False, the plug-in is not candidate for an automatic |
---|
| 289 | default perspective setting |
---|
[1610976] | 290 | """ |
---|
| 291 | return True |
---|
| 292 | |
---|
[d89f09b] | 293 | def post_init(self): |
---|
| 294 | """ |
---|
[5062bbf] | 295 | Post initialization call back to close the loose ends |
---|
[d89f09b] | 296 | """ |
---|
[5612152] | 297 | pass |
---|
[f83b94a] | 298 | |
---|
[b63dc6e] | 299 | def set_state(self, state=None, datainfo=None, format=None): |
---|
[f83b94a] | 300 | """ |
---|
[5062bbf] | 301 | Call-back method for the fit page state reader. |
---|
[8897d66] | 302 | This method is called when a .fitv/.svs file is loaded. |
---|
[5062bbf] | 303 | |
---|
[8897d66] | 304 | : param state: PageState object |
---|
| 305 | : param datainfo: data |
---|
[f83b94a] | 306 | """ |
---|
[b63dc6e] | 307 | if state != None: |
---|
| 308 | # store fitting state in temp_state |
---|
| 309 | self.temp_state.append(state) |
---|
| 310 | else: |
---|
| 311 | self.temp_state = [] |
---|
[ef16f59] | 312 | # index to start with for a new set_state |
---|
| 313 | self.state_index = 0 |
---|
[b63dc6e] | 314 | # state file format |
---|
| 315 | self.sfile_ext = format |
---|
[ef16f59] | 316 | |
---|
[4da35bc] | 317 | def on_set_state_helper(self,event=None): |
---|
| 318 | """ |
---|
[8897d66] | 319 | Set_state_helper. This actually sets state after plotting data from state file. |
---|
[ef16f59] | 320 | |
---|
[9b18735] | 321 | : event: FitStateUpdateEvent called by dataloader.plot_data from guiframe |
---|
[4da35bc] | 322 | """ |
---|
[7a07864] | 323 | if len(self.temp_state) == 0: |
---|
| 324 | if self.state_index==0 and len(self.mypanels) <= 0 and self.sfile_ext =='.svs': |
---|
[9b18735] | 325 | self.fit_panel.add_default_pages() |
---|
[b63dc6e] | 326 | self.temp_state = [] |
---|
| 327 | self.state_index = 0 |
---|
[4da35bc] | 328 | return |
---|
[3eb2811] | 329 | |
---|
[ef16f59] | 330 | try: |
---|
| 331 | # Load fitting state |
---|
| 332 | state = self.temp_state[self.state_index] |
---|
[3eb2811] | 333 | #panel state should have model selection to set_state |
---|
| 334 | if state.formfactorcombobox != None: |
---|
| 335 | #set state |
---|
| 336 | page = self.fit_panel.set_state(state) |
---|
| 337 | else: |
---|
| 338 | #just set data because set_state won't work |
---|
| 339 | page_info = self.fit_panel.get_page_info(data=state.data) |
---|
[9b18735] | 340 | panel = self.fit_panel.add_page(page_info) |
---|
| 341 | self.store_page(page=panel, data=state.data) |
---|
[b63dc6e] | 342 | self.mypanels.append(panel) |
---|
[3eb2811] | 343 | |
---|
[9b18735] | 344 | # get ready for the next set_state |
---|
[ef16f59] | 345 | self.state_index += 1 |
---|
[9b18735] | 346 | |
---|
[ef16f59] | 347 | #reset state variables to default when all set_state is finished. |
---|
| 348 | if len(self.temp_state) == self.state_index: |
---|
[9b18735] | 349 | |
---|
[ef16f59] | 350 | self.temp_state = [] |
---|
[b63dc6e] | 351 | #self.state_index = 0 |
---|
[ef16f59] | 352 | # Make sure the user sees the fitting panel after loading |
---|
| 353 | self.parent.set_perspective(self.perspective) |
---|
| 354 | except: |
---|
[b63dc6e] | 355 | self.state_index==0 |
---|
[8897d66] | 356 | self.temp_state = [] |
---|
[ef16f59] | 357 | raise |
---|
[4da35bc] | 358 | |
---|
[f83b94a] | 359 | def save_fit_state(self, filepath, fitstate): |
---|
| 360 | """ |
---|
[5062bbf] | 361 | save fit page state into file |
---|
[f83b94a] | 362 | """ |
---|
| 363 | self.state_reader.write(filename=filepath, fitstate=fitstate) |
---|
| 364 | |
---|
[d902cf0] | 365 | def copy_data(self, item, dy=None): |
---|
[ed2ea6a] | 366 | """ |
---|
[5062bbf] | 367 | receive a data 1D and the list of errors on dy |
---|
| 368 | and create a new data1D data |
---|
| 369 | |
---|
[ed2ea6a] | 370 | """ |
---|
[2db1d66] | 371 | id = None |
---|
[60d6c23] | 372 | if hasattr(item,"id"): |
---|
[edcbd467] | 373 | id = item.id |
---|
[2a2af47] | 374 | |
---|
[c202d03] | 375 | data= Data1D(x=item.x, y=item.y,dx=None, dy=None) |
---|
[2a2af47] | 376 | data.copy_from_datainfo(item) |
---|
[c202d03] | 377 | item.clone_without_data(clone=data) |
---|
[e0e873b] | 378 | data.dy = deepcopy(dy) |
---|
[60d6c23] | 379 | data.name = item.name |
---|
| 380 | ## allow to highlight data when plotted |
---|
[edcbd467] | 381 | data.interactive = deepcopy(item.interactive) |
---|
[60d6c23] | 382 | ## when 2 data have the same id override the 1 st plotted |
---|
| 383 | data.id = id |
---|
[c3b4dcb] | 384 | data.group_id = item.group_id |
---|
[cce33b3] | 385 | return data |
---|
[bfe4644] | 386 | |
---|
[ca7a626] | 387 | def set_fit_range(self, page, qmin, qmax): |
---|
| 388 | """ |
---|
[5062bbf] | 389 | Set the fitting range of a given page |
---|
[ca7a626] | 390 | """ |
---|
| 391 | if page in self.page_finder.iterkeys(): |
---|
| 392 | fitproblem= self.page_finder[page] |
---|
| 393 | fitproblem.set_range(qmin= qmin, qmax= qmax) |
---|
[2a8fac1] | 394 | |
---|
[ca7a626] | 395 | def schedule_for_fit(self,value=0,page=None,fitproblem =None): |
---|
[948add7] | 396 | """ |
---|
[5062bbf] | 397 | Set the fit problem field to 0 or 1 to schedule that problem to fit. |
---|
| 398 | Schedule the specified fitproblem or get the fit problem related to |
---|
| 399 | the current page and set value. |
---|
| 400 | |
---|
| 401 | :param value: integer 0 or 1 |
---|
| 402 | :param fitproblem: fitproblem to schedule or not to fit |
---|
| 403 | |
---|
[948add7] | 404 | """ |
---|
| 405 | if fitproblem !=None: |
---|
| 406 | fitproblem.schedule_tofit(value) |
---|
| 407 | else: |
---|
[ca7a626] | 408 | if page in self.page_finder.iterkeys(): |
---|
| 409 | fitproblem= self.page_finder[page] |
---|
| 410 | fitproblem.schedule_tofit(value) |
---|
| 411 | |
---|
[d89f09b] | 412 | def get_page_finder(self): |
---|
[5062bbf] | 413 | """ |
---|
| 414 | return self.page_finder used also by simfitpage.py |
---|
| 415 | """ |
---|
[d89f09b] | 416 | return self.page_finder |
---|
| 417 | |
---|
[3b19ac9] | 418 | def set_page_finder(self,modelname,names,values): |
---|
[d89f09b] | 419 | """ |
---|
[5062bbf] | 420 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
| 421 | |
---|
| 422 | :param modelname: the name ot the model for with the parameter has to reset |
---|
| 423 | :param value: can be a string in this case. |
---|
| 424 | :param names: the paramter name |
---|
| 425 | |
---|
| 426 | :note: expecting park used for fit. |
---|
| 427 | |
---|
[d89f09b] | 428 | """ |
---|
[51d47b5] | 429 | sim_page= self.sim_page |
---|
[d89f09b] | 430 | for page, value in self.page_finder.iteritems(): |
---|
| 431 | if page != sim_page: |
---|
| 432 | list=value.get_model() |
---|
[f93dfcb] | 433 | model = list[0] |
---|
[d89f09b] | 434 | if model.name== modelname: |
---|
[3b19ac9] | 435 | value.set_model_param(names,values) |
---|
[d89f09b] | 436 | break |
---|
[2db1d66] | 437 | |
---|
[d89f09b] | 438 | def split_string(self,item): |
---|
| 439 | """ |
---|
[5062bbf] | 440 | receive a word containing dot and split it. used to split parameterset |
---|
| 441 | name into model name and parameter name example: :: |
---|
| 442 | |
---|
[3b19ac9] | 443 | paramaterset (item) = M1.A |
---|
[5062bbf] | 444 | Will return model_name = M1 , parameter name = A |
---|
| 445 | |
---|
[d89f09b] | 446 | """ |
---|
| 447 | if string.find(item,".")!=-1: |
---|
| 448 | param_names= re.split("\.",item) |
---|
[6d91073] | 449 | model_name=param_names[0] |
---|
| 450 | ##Assume max len is 3; eg., M0.radius.width |
---|
| 451 | if len(param_names) == 3: |
---|
| 452 | param_name=param_names[1]+"."+param_names[2] |
---|
| 453 | else: |
---|
| 454 | param_name=param_names[1] |
---|
[d89f09b] | 455 | return model_name,param_name |
---|
| 456 | |
---|
[51d47b5] | 457 | def stop_fit(self): |
---|
[ed2ea6a] | 458 | """ |
---|
[5062bbf] | 459 | Stop the fit engine |
---|
[ed2ea6a] | 460 | """ |
---|
[ad6dd4c] | 461 | if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
| 462 | self.calc_fit.stop() |
---|
[f3113c9] | 463 | wx.PostEvent(self.parent, StatusEvent(status="Fitting \ |
---|
[ed2ea6a] | 464 | is cancelled" , type="stop")) |
---|
[f72333f] | 465 | |
---|
| 466 | def set_smearer_nodraw(self,smearer, qmin=None, qmax=None): |
---|
| 467 | """ |
---|
[5062bbf] | 468 | Get a smear object and store it to a fit problem |
---|
| 469 | |
---|
| 470 | :param smearer: smear object to allow smearing data |
---|
| 471 | |
---|
[f72333f] | 472 | """ |
---|
| 473 | self.current_pg=self.fit_panel.get_current_page() |
---|
| 474 | self.page_finder[self.current_pg].set_smearer(smearer) |
---|
| 475 | ## draw model 1D with smeared data |
---|
| 476 | data = self.page_finder[self.current_pg].get_fit_data() |
---|
| 477 | model = self.page_finder[self.current_pg].get_model() |
---|
| 478 | ## if user has already selected a model to plot |
---|
| 479 | ## redraw the model with data smeared |
---|
| 480 | smear =self.page_finder[self.current_pg].get_smearer() |
---|
[ed2ea6a] | 481 | |
---|
[ca7a626] | 482 | def set_smearer(self,smearer, qmin=None, qmax=None): |
---|
[d89f09b] | 483 | """ |
---|
[5062bbf] | 484 | Get a smear object and store it to a fit problem |
---|
| 485 | |
---|
| 486 | :param smearer: smear object to allow smearing data |
---|
| 487 | |
---|
[ca7a626] | 488 | """ |
---|
[dd2c2ea3] | 489 | self.current_pg=self.fit_panel.get_current_page() |
---|
| 490 | self.page_finder[self.current_pg].set_smearer(smearer) |
---|
[ca7a626] | 491 | ## draw model 1D with smeared data |
---|
[edcbd467] | 492 | data = self.page_finder[self.current_pg].get_fit_data() |
---|
[dd2c2ea3] | 493 | model = self.page_finder[self.current_pg].get_model() |
---|
[ca7a626] | 494 | ## if user has already selected a model to plot |
---|
| 495 | ## redraw the model with data smeared |
---|
[f72333f] | 496 | |
---|
[67e258c] | 497 | smear = self.page_finder[self.current_pg].get_smearer() |
---|
| 498 | if model != None: |
---|
| 499 | self.draw_model(model=model, data=data, smearer=smear, |
---|
| 500 | qmin=qmin, qmax=qmax) |
---|
[ca7a626] | 501 | |
---|
[67e258c] | 502 | def draw_model(self, model, data=None, smearer=None, |
---|
| 503 | enable1D=True, enable2D=False, |
---|
| 504 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
[ca7a626] | 505 | """ |
---|
[5062bbf] | 506 | Draw model. |
---|
| 507 | |
---|
| 508 | :param model: the model to draw |
---|
| 509 | :param name: the name of the model to draw |
---|
| 510 | :param data: the data on which the model is based to be drawn |
---|
| 511 | :param description: model's description |
---|
| 512 | :param enable1D: if true enable drawing model 1D |
---|
| 513 | :param enable2D: if true enable drawing model 2D |
---|
| 514 | :param qmin: Range's minimum value to draw model |
---|
| 515 | :param qmax: Range's maximum value to draw model |
---|
| 516 | :param qstep: number of step to divide the x and y-axis |
---|
[ed2ea6a] | 517 | |
---|
[d89f09b] | 518 | """ |
---|
[67e258c] | 519 | if data.__class__.__name__ != "Data2D": |
---|
[f72333f] | 520 | ## draw model 1D with no loaded data |
---|
[67e258c] | 521 | self._draw_model1D(model=model, |
---|
| 522 | data=data, |
---|
| 523 | enable1D=enable1D, |
---|
| 524 | smearer=smearer, |
---|
| 525 | qmin=qmin, |
---|
| 526 | qmax=qmax, |
---|
| 527 | qstep=qstep) |
---|
[f72333f] | 528 | else: |
---|
| 529 | ## draw model 2D with no initial data |
---|
| 530 | self._draw_model2D(model=model, |
---|
[67e258c] | 531 | data=data, |
---|
| 532 | enable2D=enable2D, |
---|
| 533 | smearer=smearer, |
---|
| 534 | qmin=qmin, |
---|
| 535 | qmax=qmax, |
---|
| 536 | qstep=qstep) |
---|
[2db1d66] | 537 | |
---|
[ca7a626] | 538 | def onFit(self): |
---|
| 539 | """ |
---|
[5062bbf] | 540 | perform fit |
---|
[ca7a626] | 541 | """ |
---|
[bb18ef1] | 542 | ## count the number of fitproblem schedule to fit |
---|
[67e258c] | 543 | fitproblem_count = 0 |
---|
[bb18ef1] | 544 | for value in self.page_finder.itervalues(): |
---|
[67e258c] | 545 | if value.get_scheduled() == 1: |
---|
[bb18ef1] | 546 | fitproblem_count += 1 |
---|
[2140e68] | 547 | |
---|
[bb18ef1] | 548 | ## if simultaneous fit change automatically the engine to park |
---|
[67e258c] | 549 | if fitproblem_count > 1: |
---|
[bb18ef1] | 550 | self._on_change_engine(engine='park') |
---|
[c9a4377] | 551 | |
---|
[c660493] | 552 | self.fitproblem_count = fitproblem_count |
---|
| 553 | |
---|
[bb18ef1] | 554 | from sans.fit.Fitting import Fit |
---|
[67e258c] | 555 | self.fitter = Fit(self._fit_engine) |
---|
[bb18ef1] | 556 | |
---|
[67e258c] | 557 | if self._fit_engine == "park": |
---|
| 558 | engineType = "Simultaneous Fit" |
---|
[ca7a626] | 559 | else: |
---|
[67e258c] | 560 | engineType = "Single Fit" |
---|
[ca7a626] | 561 | |
---|
| 562 | fproblemId = 0 |
---|
[67e258c] | 563 | self.current_pg = None |
---|
[d89f09b] | 564 | for page, value in self.page_finder.iteritems(): |
---|
| 565 | try: |
---|
[67e258c] | 566 | if value.get_scheduled() == 1: |
---|
[f93dfcb] | 567 | #Get list of parameters name to fit |
---|
[d89f09b] | 568 | pars = [] |
---|
| 569 | templist = [] |
---|
[3b19ac9] | 570 | templist = page.get_param_list() |
---|
[d89f09b] | 571 | for element in templist: |
---|
[513115c] | 572 | name = str(element[1]) |
---|
[ca7a626] | 573 | pars.append(name) |
---|
| 574 | #Set Engine (model , data) related to the page on |
---|
[67e258c] | 575 | self._fit_helper(value=value, pars=pars, |
---|
| 576 | id=fproblemId, title=engineType) |
---|
[ca7a626] | 577 | fproblemId += 1 |
---|
[67e258c] | 578 | self.current_pg = page |
---|
[d89f09b] | 579 | except: |
---|
[67e258c] | 580 | msg= "%s error: %s" % (engineType, sys.exc_value) |
---|
[f304194] | 581 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 582 | type="stop")) |
---|
[25e3fc9] | 583 | return |
---|
[e54d2c32] | 584 | ## If a thread is already started, stop it |
---|
| 585 | #if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
| 586 | # self.calc_fit.stop() |
---|
| 587 | #Handler used for park engine displayed message |
---|
| 588 | handler = ConsoleUpdate(parent=self.parent,improvement_delta=0.1) |
---|
| 589 | ## perform single fit |
---|
| 590 | if fitproblem_count == 1: |
---|
[67e258c] | 591 | calc_fit = FitThread(parent=self.parent, |
---|
[e54d2c32] | 592 | handler = handler, |
---|
[67e258c] | 593 | fn=self.fitter, |
---|
[e54d2c32] | 594 | cpage=self.current_pg, |
---|
[67e258c] | 595 | pars=pars, |
---|
[e54d2c32] | 596 | updatefn=handler.update_fit, |
---|
[67e258c] | 597 | completefn=self._single_fit_completed) |
---|
[e54d2c32] | 598 | else: |
---|
| 599 | ## Perform more than 1 fit at the time |
---|
[67e258c] | 600 | calc_fit = FitThread(parent=self.parent, |
---|
[e54d2c32] | 601 | handler=handler, |
---|
| 602 | fn= self.fitter, |
---|
| 603 | completefn= self._simul_fit_completed, |
---|
| 604 | updatefn=handler.update_fit) |
---|
[464fce54] | 605 | |
---|
[e54d2c32] | 606 | calc_fit.queue() |
---|
| 607 | self.ready_fit(calc_fit=calc_fit) |
---|
| 608 | |
---|
[662da312] | 609 | def ready_fit(self, calc_fit): |
---|
[c660493] | 610 | """ |
---|
| 611 | Ready for another fit |
---|
| 612 | """ |
---|
| 613 | if self.fitproblem_count != None and self.fitproblem_count > 1: |
---|
[662da312] | 614 | calc_fit.ready(2.5) |
---|
[c660493] | 615 | |
---|
| 616 | else: |
---|
| 617 | time.sleep(0.4) |
---|
[2f189dc] | 618 | |
---|
| 619 | def remove_plot(self, page, theory=False): |
---|
| 620 | """ |
---|
[5062bbf] | 621 | remove model plot when a fit page is closed |
---|
[2f189dc] | 622 | """ |
---|
| 623 | fitproblem = self.page_finder[page] |
---|
| 624 | data = fitproblem.get_fit_data() |
---|
| 625 | model = fitproblem.get_model() |
---|
| 626 | if model is not None: |
---|
| 627 | name = model.name |
---|
| 628 | new_plot = Theory1D(x=[], y=[], dy=None) |
---|
| 629 | new_plot.name = name |
---|
| 630 | new_plot.xaxis(data._xaxis, data._xunit) |
---|
| 631 | new_plot.yaxis(data._yaxis, data._yunit) |
---|
| 632 | new_plot.group_id = data.group_id |
---|
| 633 | new_plot.id = data.id + name |
---|
| 634 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=data.name)) |
---|
| 635 | if theory: |
---|
| 636 | new_plot_data = Data1D(x=[], y=[], dx=None, dy=None) |
---|
| 637 | new_plot_data.name = data.name |
---|
| 638 | new_plot_data.xaxis(data._xaxis, data._xunit) |
---|
| 639 | new_plot_data.yaxis(data._yaxis, data._yunit) |
---|
| 640 | new_plot_data.group_id = data.group_id |
---|
| 641 | new_plot_data.id = data.id |
---|
| 642 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot_data, |
---|
| 643 | title=data.name)) |
---|
[edcbd467] | 644 | def create_fittable_data2D(self, data): |
---|
| 645 | """ |
---|
[5062bbf] | 646 | check if the current data is a data 2d and add dy to that data |
---|
| 647 | |
---|
| 648 | :return: Data2D |
---|
| 649 | |
---|
[edcbd467] | 650 | """ |
---|
| 651 | if data.__class__.__name__ != "Data2D": |
---|
[2fff4db] | 652 | raise ValueError, " create_fittable_data2D expects a Data2D" |
---|
[edcbd467] | 653 | ## Data2D case |
---|
| 654 | new_data = deepcopy(data) |
---|
| 655 | if not hasattr(data, "is_data"): |
---|
| 656 | new_data.group_id += "data2D" |
---|
| 657 | new_data.id +="data2D" |
---|
| 658 | new_data.is_data = False |
---|
| 659 | title = new_data.name |
---|
| 660 | title += " Fit" |
---|
| 661 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_data, |
---|
| 662 | title=str(title))) |
---|
| 663 | else: |
---|
| 664 | new_data.is_data = True |
---|
| 665 | return new_data |
---|
[2f189dc] | 666 | |
---|
[edcbd467] | 667 | def create_fittable_data1D(self, data): |
---|
| 668 | """ |
---|
[5062bbf] | 669 | check if the current data is a theory 1d and add dy to that data |
---|
| 670 | |
---|
| 671 | :return: Data1D |
---|
| 672 | |
---|
[edcbd467] | 673 | """ |
---|
[2fff4db] | 674 | class_name = data.__class__.__name__ |
---|
| 675 | if not class_name in ["Data1D", "Theory1D"]: |
---|
[edcbd467] | 676 | raise ValueError, "create_fittable_data1D expects Data1D" |
---|
[f71c2de] | 677 | |
---|
[edcbd467] | 678 | #get the appropriate dy |
---|
| 679 | dy = deepcopy(data.dy) |
---|
| 680 | if len(self.err_dy) > 0: |
---|
| 681 | if data.name in self.err_dy.iterkeys(): |
---|
| 682 | dy = self.err_dy[data.name] |
---|
| 683 | if data.__class__.__name__ == "Theory1D": |
---|
| 684 | new_data = self.copy_data(data, dy) |
---|
[2fff4db] | 685 | new_data.group_id = str(new_data.group_id)+"data1D" |
---|
| 686 | new_data.id = str(new_data.id)+"data1D" |
---|
[edcbd467] | 687 | new_data.is_data = False |
---|
| 688 | title = new_data.name |
---|
| 689 | title = 'Data created from Theory' |
---|
| 690 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_data, |
---|
| 691 | title=str(title), |
---|
| 692 | reset=True)) |
---|
| 693 | else: |
---|
[f71c2de] | 694 | new_data = self.copy_data(data, dy) |
---|
| 695 | new_data.id = data.id |
---|
[edcbd467] | 696 | new_data.is_data = True |
---|
| 697 | return new_data |
---|
| 698 | |
---|
[31b0c47] | 699 | def store_page(self, page, data): |
---|
| 700 | """ |
---|
[5062bbf] | 701 | Helper to save page reference into the plug-in |
---|
| 702 | |
---|
| 703 | :param page: page to store |
---|
| 704 | |
---|
[31b0c47] | 705 | """ |
---|
| 706 | page.set_data(data) |
---|
| 707 | #create a fitproblem storing all link to data,model,page creation |
---|
| 708 | if not page in self.page_finder.keys(): |
---|
| 709 | self.page_finder[page] = FitProblem() |
---|
| 710 | self.page_finder[page].add_fit_data(data) |
---|
| 711 | |
---|
[edcbd467] | 712 | def add_fit_page(self, data): |
---|
| 713 | """ |
---|
[5062bbf] | 714 | given a data, ask to the fitting panel to create a new fitting page, |
---|
| 715 | get this page and store it into the page_finder of this plug-in |
---|
[edcbd467] | 716 | """ |
---|
| 717 | try: |
---|
| 718 | page = self.fit_panel.add_fit_page(data) |
---|
[8898e963] | 719 | |
---|
[6450d8c] | 720 | # add data associated to the page created |
---|
| 721 | if page != None: |
---|
| 722 | self.store_page(page=page, data=data) |
---|
[f304194] | 723 | wx.PostEvent(self.parent, StatusEvent(status="Page Created", |
---|
[31b0c47] | 724 | info="info")) |
---|
[edcbd467] | 725 | else: |
---|
[f304194] | 726 | msg = "Page was already Created" |
---|
| 727 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="warning")) |
---|
[edcbd467] | 728 | except: |
---|
[6450d8c] | 729 | msg = "Creating Fit page: %s"%sys.exc_value |
---|
| 730 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[31b0c47] | 731 | |
---|
[848a2ef] | 732 | def _onEVT_SLICER_PANEL(self, event): |
---|
| 733 | """ |
---|
[5062bbf] | 734 | receive and event telling to update a panel with a name starting with |
---|
| 735 | event.panel_name. this method update slicer panel for a given interactor. |
---|
| 736 | |
---|
| 737 | :param event: contains type of slicer , paramaters for updating the panel |
---|
[848a2ef] | 738 | and panel_name to find the slicer 's panel concerned. |
---|
| 739 | """ |
---|
| 740 | for item in self.parent.panels: |
---|
[df4c3ad] | 741 | if self.parent.panels[item].window_caption.startswith(event.panel_name): |
---|
[848a2ef] | 742 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
[df4c3ad] | 743 | |
---|
| 744 | self.parent._mgr.Update() |
---|
[b324aa9] | 745 | |
---|
[848a2ef] | 746 | def _closed_fitpage(self, event): |
---|
| 747 | """ |
---|
[5062bbf] | 748 | request fitpanel to close a given page when its unique data is removed |
---|
| 749 | from the plot. close fitpage only when the a loaded data is removed |
---|
[848a2ef] | 750 | """ |
---|
[784e2fa] | 751 | if event is None or event.data is None: |
---|
| 752 | return |
---|
| 753 | |
---|
| 754 | if hasattr(event.data,"is_data"): |
---|
| 755 | if not event.data.is_data or event.data.__class__.__name__=="Data1D": |
---|
| 756 | self.fit_panel.close_page_with_data(event.data) |
---|
[848a2ef] | 757 | |
---|
[cfc0913] | 758 | def _add_page_onmenu(self, name,fitproblem=None): |
---|
[6f023e8] | 759 | """ |
---|
[5062bbf] | 760 | Add name of a closed page of fitpanel in a menu |
---|
[6f023e8] | 761 | """ |
---|
[dcf29d7] | 762 | list = self.menu1.GetMenuItems() |
---|
| 763 | for item in list: |
---|
| 764 | if name == item.GetItemLabel(): |
---|
[cfc0913] | 765 | self.closed_page_dict[name][1] = fitproblem |
---|
[dcf29d7] | 766 | |
---|
| 767 | if not name in self.closed_page_dict.keys(): |
---|
| 768 | # Post paramters |
---|
[77e23a2] | 769 | event_id = wx.NewId() |
---|
| 770 | self.menu1.Append(event_id, name, "Show %s fit panel" % name) |
---|
[cfc0913] | 771 | self.closed_page_dict[name]= [event_id, fitproblem] |
---|
[dcf29d7] | 772 | wx.EVT_MENU(self.parent,event_id, self._open_closed_page) |
---|
[ca7a626] | 773 | |
---|
[6f023e8] | 774 | def _open_closed_page(self, event): |
---|
| 775 | """ |
---|
[5062bbf] | 776 | reopen a closed page |
---|
[6f023e8] | 777 | """ |
---|
[cfc0913] | 778 | for name, value in self.closed_page_dict.iteritems(): |
---|
[dcf29d7] | 779 | if event.GetId() in value: |
---|
[cfc0913] | 780 | id,fitproblem = value |
---|
[b787e68c] | 781 | if name !="Model": |
---|
[cfc0913] | 782 | data= fitproblem.get_fit_data() |
---|
| 783 | page = self.fit_panel.add_fit_page(data= data,reset=True) |
---|
[0f5fe6b] | 784 | if fitproblem != None: |
---|
| 785 | self.page_finder[page]=fitproblem |
---|
[7c845cb] | 786 | if self.sim_page != None: |
---|
| 787 | self.sim_page.draw_page() |
---|
| 788 | |
---|
[0f5fe6b] | 789 | else: |
---|
[b787e68c] | 790 | model = fitproblem |
---|
| 791 | self.fit_panel.add_model_page(model=model, topmenu=True, |
---|
| 792 | reset= True) |
---|
[0f5fe6b] | 793 | break |
---|
[5062bbf] | 794 | |
---|
[6f023e8] | 795 | def _reset_schedule_problem(self, value=0): |
---|
| 796 | """ |
---|
[5062bbf] | 797 | unschedule or schedule all fitproblem to be fit |
---|
[6f023e8] | 798 | """ |
---|
| 799 | for page, fitproblem in self.page_finder.iteritems(): |
---|
| 800 | fitproblem.schedule_tofit(value) |
---|
| 801 | |
---|
[67e258c] | 802 | def _fit_helper(self, pars, value, id, title="Single Fit " ): |
---|
[2140e68] | 803 | """ |
---|
[5062bbf] | 804 | helper for fitting |
---|
[2140e68] | 805 | """ |
---|
[ca7a626] | 806 | metadata = value.get_fit_data() |
---|
[24cab5d] | 807 | model = value.get_model() |
---|
| 808 | smearer = value.get_smearer() |
---|
[67e258c] | 809 | qmin, qmax = value.get_range() |
---|
| 810 | self.fit_id = id |
---|
[24cab5d] | 811 | #Create list of parameters for fitting used |
---|
[67e258c] | 812 | templist = [] |
---|
[464fce54] | 813 | |
---|
[24cab5d] | 814 | try: |
---|
[464fce54] | 815 | #Extra list of parameters and their constraints |
---|
[67e258c] | 816 | listOfConstraint = [] |
---|
[464fce54] | 817 | |
---|
[24cab5d] | 818 | param = value.get_model_param() |
---|
[67e258c] | 819 | if len(param) > 0: |
---|
[24cab5d] | 820 | for item in param: |
---|
| 821 | ## check if constraint |
---|
[67e258c] | 822 | if item[0] != None and item[1] != None: |
---|
[464fce54] | 823 | listOfConstraint.append((item[0],item[1])) |
---|
| 824 | |
---|
[24cab5d] | 825 | #Do the single fit |
---|
[464fce54] | 826 | self.fitter.set_model(model, self.fit_id, |
---|
[67e258c] | 827 | pars, constraints=listOfConstraint) |
---|
| 828 | |
---|
| 829 | self.fitter.set_data(data=metadata, id=self.fit_id, |
---|
| 830 | smearer=smearer, qmin=qmin, qmax=qmax) |
---|
[784e2fa] | 831 | |
---|
[67e258c] | 832 | self.fitter.select_problem_for_fit(id=self.fit_id, |
---|
| 833 | value=value.get_scheduled()) |
---|
[60d6c23] | 834 | value.clear_model_param() |
---|
[24cab5d] | 835 | except: |
---|
[67e258c] | 836 | msg = title + " error: %s" % sys.exc_value |
---|
| 837 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
| 838 | |
---|
[c77d859] | 839 | def _onSelect(self,event): |
---|
| 840 | """ |
---|
[5062bbf] | 841 | when Select data to fit a new page is created .Its reference is |
---|
| 842 | added to self.page_finder |
---|
[d89f09b] | 843 | """ |
---|
[c77d859] | 844 | self.panel = event.GetEventObject() |
---|
[67e258c] | 845 | Plugin.on_perspective(self, event=event) |
---|
[6ed82c7] | 846 | for plottable in self.panel.graph.plottables: |
---|
[2fff4db] | 847 | if plottable.__class__.__name__ in ["Data1D", "Theory1D"]: |
---|
[edcbd467] | 848 | if plottable.name == self.panel.graph.selected_plottable: |
---|
| 849 | data = self.create_fittable_data1D(data=plottable) |
---|
| 850 | self.add_fit_page(data=data) |
---|
| 851 | return |
---|
[c77d859] | 852 | else: |
---|
[edcbd467] | 853 | data = self.create_fittable_data2D(data=plottable) |
---|
| 854 | self.add_fit_page(data=data) |
---|
[1c1436d] | 855 | |
---|
[ca7a626] | 856 | def _single_fit_completed(self,result,pars,cpage, elapsed=None): |
---|
[c77d859] | 857 | """ |
---|
[5062bbf] | 858 | Display fit result on one page of the notebook. |
---|
| 859 | |
---|
| 860 | :param result: result of fit |
---|
| 861 | :param pars: list of names of parameters fitted |
---|
| 862 | :param current_pg: the page where information will be displayed |
---|
| 863 | :param qmin: the minimum value of x to replot the model |
---|
| 864 | :param qmax: the maximum value of x to replot model |
---|
[c77d859] | 865 | |
---|
[7975f2b] | 866 | """ |
---|
[c77d859] | 867 | try: |
---|
[ad6dd4c] | 868 | if result ==None: |
---|
| 869 | msg= "Simple Fitting Stop !!!" |
---|
[f304194] | 870 | wx.PostEvent(self.parent, StatusEvent(status=msg,info="warning", |
---|
| 871 | type="stop")) |
---|
[ad6dd4c] | 872 | return |
---|
[67e258c] | 873 | if not numpy.isfinite(result.fitness) or \ |
---|
| 874 | numpy.any(result.pvec == None) or \ |
---|
| 875 | not numpy.all(numpy.isfinite(result.pvec)): |
---|
| 876 | msg = "Single Fitting did not converge!!!" |
---|
| 877 | wx.PostEvent(self.parent, |
---|
| 878 | StatusEvent(status=msg, type="stop")) |
---|
[d902cf0] | 879 | return |
---|
[c77d859] | 880 | for page, value in self.page_finder.iteritems(): |
---|
[67e258c] | 881 | if page == cpage : |
---|
| 882 | model = value.get_model() |
---|
[c77d859] | 883 | break |
---|
[6d91073] | 884 | param_name = [] |
---|
[c77d859] | 885 | i = 0 |
---|
| 886 | for name in pars: |
---|
[6d91073] | 887 | param_name.append(name) |
---|
[7975f2b] | 888 | |
---|
[67e258c] | 889 | cpage.onsetValues(result.fitness, |
---|
| 890 | param_name, result.pvec,result.stderr) |
---|
[9237df4] | 891 | |
---|
[c77d859] | 892 | except: |
---|
[67e258c] | 893 | msg = "Single Fit completed but Following" |
---|
| 894 | msg += " error occurred:%s" % sys.exc_value |
---|
[f304194] | 895 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 896 | type="stop")) |
---|
[25e3fc9] | 897 | return |
---|
[c77d859] | 898 | |
---|
[67e258c] | 899 | def _simul_fit_completed(self, result, pars=None, cpage=None, elapsed=None): |
---|
[c77d859] | 900 | """ |
---|
[5062bbf] | 901 | Parameter estimation completed, |
---|
| 902 | display the results to the user |
---|
| 903 | |
---|
| 904 | :param alpha: estimated best alpha |
---|
| 905 | :param elapsed: computation time |
---|
| 906 | |
---|
[c77d859] | 907 | """ |
---|
[ca7a626] | 908 | ## fit more than 1 model at the same time |
---|
| 909 | try: |
---|
[c69b6d5] | 910 | msg = "" |
---|
[ad6dd4c] | 911 | if result ==None: |
---|
| 912 | msg= "Complex Fitting Stop !!!" |
---|
| 913 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 914 | return |
---|
[c69b6d5] | 915 | if not numpy.isfinite(result.fitness) or numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
[ad6dd4c] | 916 | msg= "Single Fitting did not converge!!!" |
---|
| 917 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 918 | return |
---|
[c69b6d5] | 919 | |
---|
[5536035] | 920 | for page, value in self.page_finder.iteritems(): |
---|
[ca7a626] | 921 | if value.get_scheduled()==1: |
---|
| 922 | model = value.get_model() |
---|
[edcbd467] | 923 | data = value.get_fit_data() |
---|
[6d91073] | 924 | small_param_name = [] |
---|
[ca7a626] | 925 | small_out = [] |
---|
| 926 | small_cov = [] |
---|
| 927 | i = 0 |
---|
| 928 | #Separate result in to data corresponding to each page |
---|
| 929 | for p in result.parameters: |
---|
| 930 | model_name,param_name = self.split_string(p.name) |
---|
| 931 | if model.name == model_name: |
---|
| 932 | p_name= model.name+"."+param_name |
---|
[7975f2b] | 933 | if p.name == p_name: |
---|
[edd166b] | 934 | if p.value != None and numpy.isfinite(p.value): |
---|
[7975f2b] | 935 | small_out.append(p.value ) |
---|
| 936 | small_param_name.append(param_name) |
---|
| 937 | small_cov.append(p.stderr) |
---|
| 938 | |
---|
[ca7a626] | 939 | # Display result on each page |
---|
[6d91073] | 940 | page.onsetValues(result.fitness, small_param_name,small_out,small_cov) |
---|
[ca7a626] | 941 | except: |
---|
[25e3fc9] | 942 | msg= "Simultaneous Fit completed" |
---|
| 943 | msg +=" but Following error occurred:%s"%sys.exc_value |
---|
| 944 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 945 | return |
---|
[f93dfcb] | 946 | |
---|
[c77d859] | 947 | def _on_show_panel(self, event): |
---|
[5062bbf] | 948 | """ |
---|
| 949 | """ |
---|
| 950 | #print "_on_show_panel: fitting" |
---|
| 951 | pass |
---|
| 952 | |
---|
[c77d859] | 953 | def _onset_engine_park(self,event): |
---|
| 954 | """ |
---|
[5062bbf] | 955 | set engine to park |
---|
[c77d859] | 956 | """ |
---|
[484faf7] | 957 | Plugin.on_perspective(self,event=event) |
---|
[c77d859] | 958 | self._on_change_engine('park') |
---|
| 959 | |
---|
| 960 | def _onset_engine_scipy(self,event): |
---|
| 961 | """ |
---|
[5062bbf] | 962 | set engine to scipy |
---|
[c77d859] | 963 | """ |
---|
| 964 | self._on_change_engine('scipy') |
---|
| 965 | |
---|
| 966 | def _on_slicer_event(self, event): |
---|
| 967 | """ |
---|
[5062bbf] | 968 | Receive a panel as event and send it to guiframe |
---|
| 969 | |
---|
| 970 | :param event: event containing a panel |
---|
| 971 | |
---|
[c77d859] | 972 | """ |
---|
[5062bbf] | 973 | if event.panel is not None: |
---|
[c77d859] | 974 | new_panel = event.panel |
---|
[c3435b7f] | 975 | self.slicer_panels.append(event.panel) |
---|
[c77d859] | 976 | # Set group ID if available |
---|
| 977 | event_id = self.parent.popup_panel(new_panel) |
---|
[3fef0a8] | 978 | #self.menu3.Append(event_id, new_panel.window_caption, |
---|
| 979 | # "Show %s plot panel" % new_panel.window_caption) |
---|
[67e258c] | 980 | # Set id to allow us to reference the panel later |
---|
[df4c3ad] | 981 | |
---|
[c77d859] | 982 | new_panel.uid = event_id |
---|
| 983 | self.mypanels.append(new_panel) |
---|
[5062bbf] | 984 | |
---|
[848a2ef] | 985 | def _onclearslicer(self, event): |
---|
| 986 | """ |
---|
[5062bbf] | 987 | Clear the boxslicer when close the panel associate with this slicer |
---|
[848a2ef] | 988 | """ |
---|
[df4c3ad] | 989 | name =event.GetPane().caption |
---|
[c3435b7f] | 990 | |
---|
| 991 | for panel in self.slicer_panels: |
---|
| 992 | if panel.window_caption==name: |
---|
| 993 | |
---|
[df4c3ad] | 994 | for item in self.parent.panels: |
---|
[c3435b7f] | 995 | if hasattr(self.parent.panels[item],"uid"): |
---|
| 996 | if self.parent.panels[item].uid ==panel.base.uid: |
---|
[df4c3ad] | 997 | self.parent.panels[item].onClearSlicer(event) |
---|
| 998 | self.parent._mgr.Update() |
---|
| 999 | break |
---|
[c3435b7f] | 1000 | break |
---|
[5062bbf] | 1001 | |
---|
[707436d] | 1002 | def _return_engine_type(self): |
---|
| 1003 | """ |
---|
[5062bbf] | 1004 | return the current type of engine |
---|
[707436d] | 1005 | """ |
---|
| 1006 | return self._fit_engine |
---|
| 1007 | |
---|
| 1008 | |
---|
[c77d859] | 1009 | def _on_change_engine(self, engine='park'): |
---|
| 1010 | """ |
---|
[5062bbf] | 1011 | Allow to select the type of engine to perform fit |
---|
| 1012 | |
---|
| 1013 | :param engine: the key work of the engine |
---|
| 1014 | |
---|
[c77d859] | 1015 | """ |
---|
[77e23a2] | 1016 | ## saving fit engine name |
---|
[c77d859] | 1017 | self._fit_engine = engine |
---|
[707436d] | 1018 | ## change menu item state |
---|
| 1019 | if engine=="park": |
---|
| 1020 | self.menu1.FindItemByPosition(0).Check(False) |
---|
| 1021 | self.menu1.FindItemByPosition(1).Check(True) |
---|
| 1022 | else: |
---|
| 1023 | self.menu1.FindItemByPosition(0).Check(True) |
---|
| 1024 | self.menu1.FindItemByPosition(1).Check(False) |
---|
| 1025 | |
---|
[77e23a2] | 1026 | ## post a message to status bar |
---|
[c77d859] | 1027 | wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) |
---|
| 1028 | |
---|
[77e23a2] | 1029 | ## Bind every open fit page with a newevent to know the current fitting engine |
---|
| 1030 | import fitpage |
---|
| 1031 | event= fitpage.FitterTypeEvent() |
---|
| 1032 | event.type = self._fit_engine |
---|
| 1033 | for key in self.page_finder.keys(): |
---|
| 1034 | wx.PostEvent(key, event) |
---|
| 1035 | |
---|
[c77d859] | 1036 | def _on_model_panel(self, evt): |
---|
| 1037 | """ |
---|
[5062bbf] | 1038 | react to model selection on any combo box or model menu.plot the model |
---|
| 1039 | |
---|
| 1040 | :param evt: wx.combobox event |
---|
| 1041 | |
---|
[c77d859] | 1042 | """ |
---|
| 1043 | model = evt.model |
---|
[9237df4] | 1044 | if model == None: |
---|
[c77d859] | 1045 | return |
---|
[6747217] | 1046 | smearer = None |
---|
| 1047 | qmin = None |
---|
| 1048 | qmax = None |
---|
| 1049 | if hasattr(evt, "qmin"): |
---|
| 1050 | qmin = evt.qmin |
---|
| 1051 | if hasattr(evt, "qmax"): |
---|
| 1052 | qmax = evt.qmax |
---|
| 1053 | if hasattr(evt, "smearer"): |
---|
| 1054 | smearer = evt.smearer |
---|
[1350c87] | 1055 | model.origin_name = model.name |
---|
[dd2c2ea3] | 1056 | self.current_pg = self.fit_panel.get_current_page() |
---|
[c77d859] | 1057 | ## make sure nothing is done on self.sim_page |
---|
| 1058 | ## example trying to call set_panel on self.sim_page |
---|
[dd2c2ea3] | 1059 | if self.current_pg != self.sim_page : |
---|
[0b12abb5] | 1060 | if self.page_finder[self.current_pg].get_model()== None: |
---|
[c77d859] | 1061 | |
---|
[edcbd467] | 1062 | model.name = "M"+str(self.index_model) |
---|
[c77d859] | 1063 | self.index_model += 1 |
---|
| 1064 | else: |
---|
[dd2c2ea3] | 1065 | model.name= self.page_finder[self.current_pg].get_model().name |
---|
[6747217] | 1066 | |
---|
[edcbd467] | 1067 | data = self.page_finder[self.current_pg].get_fit_data() |
---|
[6859338] | 1068 | |
---|
[2140e68] | 1069 | # save the name containing the data name with the appropriate model |
---|
[dd2c2ea3] | 1070 | self.page_finder[self.current_pg].set_model(model) |
---|
| 1071 | qmin, qmax= self.current_pg.get_range() |
---|
| 1072 | self.page_finder[self.current_pg].set_range(qmin=qmin, qmax=qmax) |
---|
[0b12abb5] | 1073 | |
---|
[c77d859] | 1074 | if self.sim_page!=None: |
---|
[b28717b] | 1075 | self.sim_page.draw_page() |
---|
[6f73a08] | 1076 | |
---|
[d89f09b] | 1077 | def _on_model_menu(self, evt): |
---|
| 1078 | """ |
---|
[5062bbf] | 1079 | Plot a theory from a model selected from the menu |
---|
| 1080 | |
---|
| 1081 | :param evt: wx.menu event |
---|
| 1082 | |
---|
[d89f09b] | 1083 | """ |
---|
[c9a4377] | 1084 | model = evt.model |
---|
[05971e1] | 1085 | Plugin.on_perspective(self,event=evt) |
---|
[3dc83be] | 1086 | # Create a model page. If a new page is created, the model |
---|
| 1087 | # will be plotted automatically. If a page already exists, |
---|
| 1088 | # the content will be updated and the plot refreshed |
---|
[b787e68c] | 1089 | self.fit_panel.add_model_page(model,topmenu=True) |
---|
[5062bbf] | 1090 | |
---|
[c77d859] | 1091 | def _update1D(self,x, output): |
---|
[bb18ef1] | 1092 | """ |
---|
[5062bbf] | 1093 | Update the output of plotting model 1D |
---|
[bb18ef1] | 1094 | """ |
---|
[847091f] | 1095 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
| 1096 | #updating ... ",type="update")) |
---|
[c660493] | 1097 | self.ready_fit() |
---|
| 1098 | #self.calc_thread.ready(0.01) |
---|
[bb18ef1] | 1099 | |
---|
[c77d859] | 1100 | def _fill_default_model2D(self, theory, qmax,qstep, qmin=None): |
---|
[ed2ea6a] | 1101 | """ |
---|
[5062bbf] | 1102 | fill Data2D with default value |
---|
| 1103 | |
---|
| 1104 | :param theory: Data2D to fill |
---|
| 1105 | |
---|
[ed2ea6a] | 1106 | """ |
---|
[d15c0202] | 1107 | from DataLoader.data_info import Detector, Source |
---|
[bb18ef1] | 1108 | |
---|
[d15c0202] | 1109 | detector = Detector() |
---|
[e575db9] | 1110 | theory.detector.append(detector) |
---|
[d15c0202] | 1111 | theory.source= Source() |
---|
[f93dfcb] | 1112 | |
---|
[e575db9] | 1113 | ## Default values |
---|
| 1114 | theory.detector[0].distance= 8000 # mm |
---|
| 1115 | theory.source.wavelength= 6 # A |
---|
| 1116 | theory.detector[0].pixel_size.x= 5 # mm |
---|
| 1117 | theory.detector[0].pixel_size.y= 5 # mm |
---|
| 1118 | |
---|
[ac2cc940] | 1119 | theory.detector[0].beam_center.x= qmax |
---|
| 1120 | theory.detector[0].beam_center.y= qmax |
---|
[5062bbf] | 1121 | |
---|
[f93dfcb] | 1122 | ## create x_bins and y_bins of the model 2D |
---|
[13e120a] | 1123 | pixel_width_x = theory.detector[0].pixel_size.x |
---|
| 1124 | pixel_width_y = theory.detector[0].pixel_size.y |
---|
| 1125 | center_x = theory.detector[0].beam_center.x/pixel_width_x |
---|
| 1126 | center_y = theory.detector[0].beam_center.y/pixel_width_y |
---|
[e575db9] | 1127 | |
---|
| 1128 | # theory default: assume the beam center is located at the center of sqr detector |
---|
| 1129 | xmax = qmax |
---|
| 1130 | xmin = -qmax |
---|
| 1131 | ymax = qmax |
---|
| 1132 | ymin = -qmax |
---|
| 1133 | |
---|
| 1134 | x= numpy.linspace(start= -1*qmax, |
---|
| 1135 | stop= qmax, |
---|
| 1136 | num= qstep, |
---|
| 1137 | endpoint=True ) |
---|
| 1138 | y = numpy.linspace(start= -1*qmax, |
---|
| 1139 | stop= qmax, |
---|
| 1140 | num= qstep, |
---|
| 1141 | endpoint=True ) |
---|
| 1142 | |
---|
| 1143 | ## use data info instead |
---|
| 1144 | new_x = numpy.tile(x, (len(y),1)) |
---|
| 1145 | new_y = numpy.tile(y, (len(x),1)) |
---|
| 1146 | new_y = new_y.swapaxes(0,1) |
---|
[d15c0202] | 1147 | |
---|
[e575db9] | 1148 | # all data reuire now in 1d array |
---|
| 1149 | qx_data = new_x.flatten() |
---|
| 1150 | qy_data = new_y.flatten() |
---|
[13e120a] | 1151 | |
---|
[e575db9] | 1152 | q_data = numpy.sqrt(qx_data*qx_data+qy_data*qy_data) |
---|
| 1153 | # set all True (standing for unmasked) as default |
---|
| 1154 | mask = numpy.ones(len(qx_data), dtype = bool) |
---|
| 1155 | |
---|
| 1156 | # calculate the range of qx and qy: this way, it is a little more independent |
---|
| 1157 | x_size = xmax- xmin |
---|
| 1158 | y_size = ymax -ymin |
---|
| 1159 | |
---|
| 1160 | # store x and y bin centers in q space |
---|
| 1161 | x_bins = x |
---|
| 1162 | y_bins = y |
---|
| 1163 | # bin size: x- & y-directions |
---|
| 1164 | xstep = x_size/len(x_bins-1) |
---|
| 1165 | ystep = y_size/len(y_bins-1) |
---|
| 1166 | |
---|
| 1167 | #theory.data = numpy.zeros(len(mask)) |
---|
[75a8723] | 1168 | theory.err_data = numpy.ones(len(mask)) |
---|
[e575db9] | 1169 | theory.qx_data = qx_data |
---|
| 1170 | theory.qy_data = qy_data |
---|
| 1171 | theory.q_data = q_data |
---|
| 1172 | theory.mask = mask |
---|
| 1173 | theory.x_bins = x_bins |
---|
| 1174 | theory.y_bins = y_bins |
---|
| 1175 | |
---|
| 1176 | # max and min taking account of the bin sizes |
---|
[64017a8] | 1177 | theory.xmin= xmin |
---|
| 1178 | theory.xmax= xmax |
---|
| 1179 | theory.ymin= ymin |
---|
| 1180 | theory.ymax= ymax |
---|
[5062bbf] | 1181 | theory.group_id = "Model" |
---|
| 1182 | theory.id = "Model" |
---|
[c77d859] | 1183 | |
---|
| 1184 | def _get_plotting_info(self, data=None): |
---|
| 1185 | """ |
---|
[5062bbf] | 1186 | get plotting info from data if data !=None else use some default |
---|
[c77d859] | 1187 | """ |
---|
| 1188 | my_info = PlotInfo() |
---|
| 1189 | if data !=None: |
---|
| 1190 | if hasattr(data,"info"): |
---|
| 1191 | x_name, x_units = data.get_xaxis() |
---|
| 1192 | y_name, y_units = data.get_yaxis() |
---|
| 1193 | |
---|
| 1194 | my_info._xunit = x_units |
---|
| 1195 | my_info._xaxis = x_name |
---|
| 1196 | my_info._yunit = y_units |
---|
| 1197 | my_info._yaxis = y_name |
---|
| 1198 | |
---|
| 1199 | my_info.title= data.name |
---|
| 1200 | if hasattr(data, "info"): |
---|
| 1201 | my_info.info= data.info |
---|
| 1202 | if hasattr(data, "group_id"): |
---|
| 1203 | my_info.group_id= data.group_id |
---|
| 1204 | return my_info |
---|
| 1205 | |
---|
[f72333f] | 1206 | def _complete1D(self, x,y, elapsed,index,model,data=None): |
---|
[c77d859] | 1207 | """ |
---|
[5062bbf] | 1208 | Complete plotting 1D data |
---|
[c77d859] | 1209 | """ |
---|
| 1210 | try: |
---|
| 1211 | new_plot = Theory1D(x=x, y=y) |
---|
| 1212 | my_info = self._get_plotting_info( data) |
---|
| 1213 | new_plot.name = model.name |
---|
| 1214 | new_plot.id = my_info.id |
---|
| 1215 | new_plot.group_id = my_info.group_id |
---|
| 1216 | |
---|
| 1217 | new_plot.xaxis( my_info._xaxis, my_info._xunit) |
---|
| 1218 | new_plot.yaxis( my_info._yaxis, my_info._yunit) |
---|
[60d6c23] | 1219 | if data!=None: |
---|
| 1220 | if new_plot.id == data.id: |
---|
| 1221 | new_plot.id += "Model" |
---|
[513115c] | 1222 | new_plot.is_data =False |
---|
[d902cf0] | 1223 | |
---|
[c202d03] | 1224 | title= new_plot.name |
---|
[f72333f] | 1225 | # x, y are only in range of index |
---|
| 1226 | self.theory_data = new_plot |
---|
[2db1d66] | 1227 | #new_plot.perspective = self.get_perspective() |
---|
[d902cf0] | 1228 | # Pass the reset flag to let the plotting event handler |
---|
| 1229 | # know that we are replacing the whole plot |
---|
[c77d859] | 1230 | if title== None: |
---|
[6ed82c7] | 1231 | title = "Analytical model 1D " |
---|
[a1100c07] | 1232 | if data ==None: |
---|
[c77d859] | 1233 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[2db1d66] | 1234 | title=str(title), reset=True)) |
---|
[c77d859] | 1235 | else: |
---|
[6747217] | 1236 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot,title= str(title))) |
---|
[f72333f] | 1237 | # Chisqr in fitpage |
---|
| 1238 | current_pg=self.fit_panel.get_current_page() |
---|
| 1239 | wx.PostEvent(current_pg,Chi2UpdateEvent(output=self._cal_chisqr(data=data,index=index))) |
---|
[847091f] | 1240 | msg = "Plot 1D complete !" |
---|
[018a0bd] | 1241 | wx.PostEvent( self.parent, StatusEvent(status=msg , type="stop" )) |
---|
[c77d859] | 1242 | except: |
---|
| 1243 | msg= " Error occurred when drawing %s Model 1D: "%new_plot.name |
---|
| 1244 | msg+= " %s"%sys.exc_value |
---|
[018a0bd] | 1245 | wx.PostEvent( self.parent, StatusEvent(status= msg, type="stop")) |
---|
[5062bbf] | 1246 | |
---|
[c77d859] | 1247 | def _update2D(self, output,time=None): |
---|
| 1248 | """ |
---|
[5062bbf] | 1249 | Update the output of plotting model |
---|
[c77d859] | 1250 | """ |
---|
| 1251 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
| 1252 | #updating ... ",type="update")) |
---|
[c660493] | 1253 | self.ready_fit() |
---|
| 1254 | #self.calc_thread.ready(0.01) |
---|
[5062bbf] | 1255 | |
---|
| 1256 | def _complete2D(self, image, data, model, elapsed, index, qmin, |
---|
| 1257 | qmax, qstep=DEFAULT_NPTS): |
---|
[c77d859] | 1258 | """ |
---|
[5062bbf] | 1259 | Complete get the result of modelthread and create model 2D |
---|
| 1260 | that can be plot. |
---|
[c77d859] | 1261 | """ |
---|
| 1262 | err_image = numpy.zeros(numpy.shape(image)) |
---|
[a8088d7] | 1263 | |
---|
[c77d859] | 1264 | theory= Data2D(image= image , err_image= err_image) |
---|
[2140e68] | 1265 | theory.name= model.name |
---|
[c77d859] | 1266 | |
---|
| 1267 | if data ==None: |
---|
[5062bbf] | 1268 | self._fill_default_model2D(theory=theory, qmax=qmax, qstep=qstep, |
---|
| 1269 | qmin= qmin) |
---|
[2140e68] | 1270 | |
---|
[c77d859] | 1271 | else: |
---|
[513115c] | 1272 | theory.id= data.id+"Model" |
---|
| 1273 | theory.group_id= data.name+"Model" |
---|
[c77d859] | 1274 | theory.x_bins= data.x_bins |
---|
| 1275 | theory.y_bins= data.y_bins |
---|
| 1276 | theory.detector= data.detector |
---|
| 1277 | theory.source= data.source |
---|
[67e258c] | 1278 | theory.is_data = False |
---|
[e575db9] | 1279 | theory.qx_data = data.qx_data |
---|
| 1280 | theory.qy_data = data.qy_data |
---|
| 1281 | theory.q_data = data.q_data |
---|
[2882fb5] | 1282 | theory.err_data = err_image#numpy.zeros(len(data.err_data))#data.err_data |
---|
[e575db9] | 1283 | theory.mask = data.mask |
---|
[c77d859] | 1284 | ## plot boundaries |
---|
| 1285 | theory.ymin= data.ymin |
---|
| 1286 | theory.ymax= data.ymax |
---|
| 1287 | theory.xmin= data.xmin |
---|
| 1288 | theory.xmax= data.xmax |
---|
[f72333f] | 1289 | |
---|
| 1290 | self.theory_data = theory |
---|
[f93dfcb] | 1291 | ## plot |
---|
[20be946] | 1292 | wx.PostEvent(self.parent, NewPlotEvent(plot=theory, |
---|
[32d802f] | 1293 | title="Analytical model 2D ", reset=True )) |
---|
[f72333f] | 1294 | # Chisqr in fitpage |
---|
[5062bbf] | 1295 | current_pg = self.fit_panel.get_current_page() |
---|
| 1296 | wx.PostEvent(current_pg, |
---|
| 1297 | Chi2UpdateEvent(output=self._cal_chisqr(data=data,index=index))) |
---|
[847091f] | 1298 | msg = "Plot 2D complete !" |
---|
[5062bbf] | 1299 | wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop" )) |
---|
[f72333f] | 1300 | |
---|
[c77d859] | 1301 | def _on_data_error(self, event): |
---|
| 1302 | """ |
---|
[5062bbf] | 1303 | receives and event from plotting plu-gins to store the data name and |
---|
| 1304 | their errors of y coordinates for 1Data hide and show error |
---|
[c77d859] | 1305 | """ |
---|
[b324aa9] | 1306 | self.err_dy = event.err_dy |
---|
[20be946] | 1307 | |
---|
[f72333f] | 1308 | def _draw_model2D(self,model,data=None, smearer= None,description=None, enable2D=False, |
---|
[cfc68540] | 1309 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): |
---|
[f93dfcb] | 1310 | """ |
---|
[5062bbf] | 1311 | draw model in 2D |
---|
| 1312 | |
---|
| 1313 | :param model: instance of the model to draw |
---|
| 1314 | :param description: the description of the model |
---|
| 1315 | :param enable2D: when True allows to draw model 2D |
---|
| 1316 | :param qmin: the minimum value to draw model 2D |
---|
| 1317 | :param qmax: the maximum value to draw model 2D |
---|
| 1318 | :param qstep: the number of division of Qx and Qy of the model to draw |
---|
[f93dfcb] | 1319 | |
---|
| 1320 | """ |
---|
[d15c0202] | 1321 | x= numpy.linspace(start= -1*qmax, |
---|
| 1322 | stop= qmax, |
---|
| 1323 | num= qstep, |
---|
| 1324 | endpoint=True ) |
---|
| 1325 | y = numpy.linspace(start= -1*qmax, |
---|
| 1326 | stop= qmax, |
---|
| 1327 | num= qstep, |
---|
| 1328 | endpoint=True ) |
---|
[c77d859] | 1329 | ## use data info instead |
---|
| 1330 | if data !=None: |
---|
| 1331 | ## check if data2D to plot |
---|
| 1332 | if hasattr(data, "x_bins"): |
---|
| 1333 | enable2D = True |
---|
| 1334 | x= data.x_bins |
---|
| 1335 | y= data.y_bins |
---|
[f72333f] | 1336 | |
---|
[c77d859] | 1337 | if not enable2D: |
---|
[f72333f] | 1338 | return None,None |
---|
[c77d859] | 1339 | try: |
---|
| 1340 | from model_thread import Calc2D |
---|
| 1341 | ## If a thread is already started, stop it |
---|
| 1342 | if self.calc_2D != None and self.calc_2D.isrunning(): |
---|
| 1343 | self.calc_2D.stop() |
---|
[f72333f] | 1344 | |
---|
[c77d859] | 1345 | self.calc_2D = Calc2D( x= x, |
---|
| 1346 | y= y, |
---|
| 1347 | model= model, |
---|
| 1348 | data = data, |
---|
[f72333f] | 1349 | smearer = smearer, |
---|
[c77d859] | 1350 | qmin= qmin, |
---|
| 1351 | qmax= qmax, |
---|
| 1352 | qstep= qstep, |
---|
| 1353 | completefn= self._complete2D, |
---|
| 1354 | updatefn= self._update2D ) |
---|
| 1355 | self.calc_2D.queue() |
---|
[f72333f] | 1356 | |
---|
[c77d859] | 1357 | except: |
---|
[67e258c] | 1358 | msg= " Error occurred when drawing %s Model 2D: " % model.name |
---|
| 1359 | msg+= " %s" % sys.exc_value |
---|
| 1360 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[f72333f] | 1361 | |
---|
[67e258c] | 1362 | def _draw_model1D(self, model, data=None, smearer=None, |
---|
| 1363 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, |
---|
| 1364 | qstep=DEFAULT_NPTS, enable1D=True): |
---|
[c77d859] | 1365 | """ |
---|
[5062bbf] | 1366 | Draw model 1D from loaded data1D |
---|
| 1367 | |
---|
| 1368 | :param data: loaded data |
---|
| 1369 | :param model: the model to plot |
---|
| 1370 | |
---|
[c77d859] | 1371 | """ |
---|
| 1372 | x= numpy.linspace(start= qmin, |
---|
| 1373 | stop= qmax, |
---|
| 1374 | num= qstep, |
---|
| 1375 | endpoint=True |
---|
| 1376 | ) |
---|
| 1377 | if data!=None: |
---|
| 1378 | ## check for data2D |
---|
| 1379 | if hasattr(data,"x_bins"): |
---|
| 1380 | return |
---|
| 1381 | x = data.x |
---|
| 1382 | if qmin == DEFAULT_QMIN : |
---|
| 1383 | qmin = min(data.x) |
---|
| 1384 | if qmax == DEFAULT_QMAX: |
---|
[3370922] | 1385 | qmax = max(data.x) |
---|
| 1386 | |
---|
[c77d859] | 1387 | |
---|
| 1388 | if not enable1D: |
---|
[f72333f] | 1389 | return |
---|
[bb18ef1] | 1390 | |
---|
[c77d859] | 1391 | try: |
---|
| 1392 | from model_thread import Calc1D |
---|
| 1393 | ## If a thread is already started, stop it |
---|
| 1394 | if self.calc_1D!= None and self.calc_1D.isrunning(): |
---|
| 1395 | self.calc_1D.stop() |
---|
| 1396 | self.calc_1D= Calc1D( x= x, |
---|
| 1397 | data = data, |
---|
| 1398 | model= model, |
---|
| 1399 | qmin = qmin, |
---|
| 1400 | qmax = qmax, |
---|
| 1401 | smearer = smearer, |
---|
| 1402 | completefn = self._complete1D, |
---|
| 1403 | updatefn = self._update1D ) |
---|
| 1404 | self.calc_1D.queue() |
---|
[f72333f] | 1405 | |
---|
[c77d859] | 1406 | except: |
---|
| 1407 | msg= " Error occurred when drawing %s Model 1D: "%model.name |
---|
| 1408 | msg+= " %s"%sys.exc_value |
---|
| 1409 | wx.PostEvent( self.parent, StatusEvent(status= msg )) |
---|
[f72333f] | 1410 | |
---|
| 1411 | def _cal_chisqr(self, data=None, index=None): |
---|
| 1412 | """ |
---|
[5062bbf] | 1413 | Get handy Chisqr using the output from draw1D and 2D, |
---|
| 1414 | instead of calling expansive CalcChisqr in guithread |
---|
[f72333f] | 1415 | """ |
---|
| 1416 | # default chisqr |
---|
| 1417 | chisqr = None |
---|
[f151ddf] | 1418 | |
---|
[f72333f] | 1419 | # return None if data == None |
---|
| 1420 | if data == None: return chisqr |
---|
[f151ddf] | 1421 | |
---|
[f72333f] | 1422 | # Get data: data I, theory I, and data dI in order |
---|
| 1423 | if data.__class__.__name__ =="Data2D": |
---|
| 1424 | if index == None: index = numpy.ones(len(data.data),ntype=bool) |
---|
[9e8c150] | 1425 | index = index & (data.err_data !=0 ) # get rid of zero error points |
---|
[f72333f] | 1426 | fn = data.data[index] |
---|
| 1427 | gn = self.theory_data.data[index] |
---|
| 1428 | en = data.err_data[index] |
---|
| 1429 | else: |
---|
| 1430 | # 1 d theory from model_thread is only in the range of index |
---|
| 1431 | if index == None: index = numpy.ones(len(data.y),ntype=bool) |
---|
[f151ddf] | 1432 | if data.dy== None or data.dy ==[]: |
---|
[4d5fa8c] | 1433 | dy = numpy.ones(len(data.y)) |
---|
[f151ddf] | 1434 | else: |
---|
[4d5fa8c] | 1435 | ## Set consitently w/AbstractFitengine: But this should be corrected later. |
---|
| 1436 | dy = data.dy |
---|
| 1437 | dy[dy==0] = 1 |
---|
[f72333f] | 1438 | fn = data.y[index] |
---|
| 1439 | gn = self.theory_data.y |
---|
[4d5fa8c] | 1440 | en = dy[index] |
---|
[1b001a7] | 1441 | |
---|
[f72333f] | 1442 | # residual |
---|
| 1443 | res = (fn - gn)/en |
---|
| 1444 | # get chisqr only w/finite |
---|
| 1445 | chisqr = numpy.average(res[numpy.isfinite(res)]*res[numpy.isfinite(res)]) |
---|
| 1446 | |
---|
| 1447 | return chisqr |
---|
| 1448 | |
---|
| 1449 | |
---|
[5062bbf] | 1450 | #def profile(fn, *args, **kw): |
---|
| 1451 | # import cProfile, pstats, os |
---|
| 1452 | # global call_result |
---|
| 1453 | # def call(): |
---|
| 1454 | # global call_result |
---|
| 1455 | # call_result = fn(*args, **kw) |
---|
| 1456 | # cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
| 1457 | # stats = pstats.Stats('profile.out') |
---|
| 1458 | # #stats.sort_stats('time') |
---|
| 1459 | # stats.sort_stats('calls') |
---|
| 1460 | # stats.print_stats() |
---|
| 1461 | # os.unlink('profile.out') |
---|
| 1462 | # return call_result |
---|
[d89f09b] | 1463 | if __name__ == "__main__": |
---|
| 1464 | i = Plugin() |
---|
| 1465 | |
---|
| 1466 | |
---|
| 1467 | |
---|
| 1468 | |
---|