[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 | |
---|
[a0da535] | 14 | import re |
---|
[5536035] | 15 | import sys |
---|
| 16 | import wx |
---|
| 17 | import logging |
---|
| 18 | import numpy |
---|
| 19 | import string |
---|
[c3b4dcb] | 20 | import time |
---|
[edcbd467] | 21 | from copy import deepcopy |
---|
[f83b94a] | 22 | import models |
---|
| 23 | import fitpage |
---|
| 24 | |
---|
[66ff250] | 25 | |
---|
[c21e87b] | 26 | from sans.dataloader.loader import Loader |
---|
[c3b4dcb] | 27 | from sans.guiframe.dataFitting import Data2D |
---|
| 28 | from sans.guiframe.dataFitting import Data1D |
---|
[2dbffcc] | 29 | from sans.guiframe.dataFitting import check_data_validity |
---|
[6bbeacd4] | 30 | from sans.guiframe.events import NewPlotEvent |
---|
| 31 | from sans.guiframe.events import StatusEvent |
---|
[a0da535] | 32 | from sans.guiframe.events import EVT_SLICER_PANEL |
---|
| 33 | from sans.guiframe.events import EVT_SLICER_PARS_UPDATE |
---|
[6bbeacd4] | 34 | from sans.guiframe.gui_style import GUIFRAME_ID |
---|
| 35 | from sans.guiframe.plugin_base import PluginBase |
---|
[2dbffcc] | 36 | from sans.fit.Fitting import Fit |
---|
[cc31608] | 37 | from .console import ConsoleUpdate |
---|
[2dbffcc] | 38 | from .fitproblem import FitProblemDictionary |
---|
[a0da535] | 39 | from .fitpanel import FitPanel |
---|
[cc31608] | 40 | from .fit_thread import FitThread |
---|
[a0da535] | 41 | from .pagestate import Reader |
---|
| 42 | from .fitpage import Chi2UpdateEvent |
---|
[ed2ea6a] | 43 | |
---|
[c26a64b] | 44 | MAX_NBR_DATA = 4 |
---|
[2c6b224] | 45 | SANS_F_TOL = 5e-05 |
---|
[5062bbf] | 46 | |
---|
[6bbeacd4] | 47 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
[5062bbf] | 48 | |
---|
[f6aee42] | 49 | if sys.platform.count("darwin")==0: |
---|
| 50 | ON_MAC = False |
---|
| 51 | else: |
---|
| 52 | ON_MAC = True |
---|
[3658abed] | 53 | |
---|
| 54 | class Plugin(PluginBase): |
---|
[d89f09b] | 55 | """ |
---|
[5062bbf] | 56 | Fitting plugin is used to perform fit |
---|
[d89f09b] | 57 | """ |
---|
[3658abed] | 58 | def __init__(self, standalone=False): |
---|
[908b817] | 59 | PluginBase.__init__(self, name="Fitting", standalone=standalone) |
---|
[d89f09b] | 60 | |
---|
[ed2ea6a] | 61 | #list of panel to send to guiframe |
---|
[f83b94a] | 62 | self.mypanels = [] |
---|
[ed2ea6a] | 63 | # reference to the current running thread |
---|
[f83b94a] | 64 | self.calc_2D = None |
---|
| 65 | self.calc_1D = None |
---|
[9f4f8f4] | 66 | |
---|
| 67 | self.color_dict = {} |
---|
| 68 | |
---|
[66ff250] | 69 | self.fit_thread_list = {} |
---|
[2296316] | 70 | self.residuals = None |
---|
[55bb249c] | 71 | self.weight = None |
---|
[9466f2d6] | 72 | self.fit_panel = None |
---|
[d89f09b] | 73 | # Start with a good default |
---|
| 74 | self.elapsed = 0.022 |
---|
[ed2ea6a] | 75 | # the type of optimizer selected, park or scipy |
---|
[d89f09b] | 76 | self.fitter = None |
---|
[2296316] | 77 | self.fit_panel = None |
---|
[c660493] | 78 | #let fit ready |
---|
| 79 | self.fitproblem_count = None |
---|
[ed2ea6a] | 80 | #Flag to let the plug-in know that it is running stand alone |
---|
[f83b94a] | 81 | self.standalone = True |
---|
[6f023e8] | 82 | ## dictionary of page closed and id |
---|
[f83b94a] | 83 | self.closed_page_dict = {} |
---|
[d89f09b] | 84 | ## Fit engine |
---|
[e9e914f] | 85 | self._fit_engine = 'scipy' |
---|
[2296316] | 86 | ## Relative error desired in the sum of squares (float); scipy only |
---|
[2c6b224] | 87 | self.ftol = SANS_F_TOL |
---|
[ed2ea6a] | 88 | #List of selected data |
---|
[f83b94a] | 89 | self.selected_data_list = [] |
---|
[c3435b7f] | 90 | ## list of slicer panel created to display slicer parameters and results |
---|
[f83b94a] | 91 | self.slicer_panels = [] |
---|
[32d802f] | 92 | # model 2D view |
---|
[f83b94a] | 93 | self.model2D_id = None |
---|
[ed2ea6a] | 94 | #keep reference of the simultaneous fit page |
---|
[f83b94a] | 95 | self.sim_page = None |
---|
[ae4ade7] | 96 | self.index_model = 0 |
---|
[9f4f8f4] | 97 | self.test_model_color = None |
---|
[f83b94a] | 98 | #Create a reader for fit page's state |
---|
[3bf4a032] | 99 | self.state_reader = None |
---|
[c8deee5] | 100 | self._extensions = '.fitv' |
---|
[0b6ae5f] | 101 | self.scipy_id = wx.NewId() |
---|
| 102 | self.park_id = wx.NewId() |
---|
| 103 | |
---|
[4da35bc] | 104 | self.temp_state = [] |
---|
[ef16f59] | 105 | self.state_index = 0 |
---|
[b63dc6e] | 106 | self.sfile_ext = None |
---|
[6bbeacd4] | 107 | # take care of saving data, model and page associated with each other |
---|
| 108 | self.page_finder = {} |
---|
[f83b94a] | 109 | # Log startup |
---|
| 110 | logging.info("Fitting plug-in started") |
---|
[9f4f8f4] | 111 | |
---|
[2dbffcc] | 112 | def create_fit_problem(self, page_id): |
---|
| 113 | """ |
---|
| 114 | Given an ID create a fitproblem container |
---|
| 115 | """ |
---|
| 116 | self.page_finder[page_id] = FitProblemDictionary() |
---|
| 117 | |
---|
| 118 | def delete_fit_problem(self, page_id): |
---|
| 119 | """ |
---|
| 120 | Given an ID create a fitproblem container |
---|
| 121 | """ |
---|
| 122 | if page_id in self.page_finder.iterkeys(): |
---|
| 123 | del self.page_finder[page_id] |
---|
| 124 | |
---|
[9f4f8f4] | 125 | def add_color(self, color, id): |
---|
| 126 | """ |
---|
| 127 | adds a color as a key with a plot id as its value to a dictionary |
---|
| 128 | """ |
---|
| 129 | self.color_dict[id] = color |
---|
[2a8fac1] | 130 | |
---|
[cc31608] | 131 | def on_batch_selection(self, flag): |
---|
| 132 | """ |
---|
| 133 | switch the the notebook of batch mode or not |
---|
| 134 | """ |
---|
| 135 | self.batch_on = flag |
---|
[2dbffcc] | 136 | if self.fit_panel is not None: |
---|
| 137 | self.fit_panel.batch_on = self.batch_on |
---|
[cc31608] | 138 | |
---|
[cab076b] | 139 | def populate_menu(self, owner): |
---|
[d89f09b] | 140 | """ |
---|
[5062bbf] | 141 | Create a menu for the Fitting plug-in |
---|
| 142 | |
---|
| 143 | :param id: id to create a menu |
---|
| 144 | :param owner: owner of menu |
---|
| 145 | |
---|
| 146 | :return: list of information to populate the main menu |
---|
| 147 | |
---|
[d89f09b] | 148 | """ |
---|
| 149 | #Menu for fitting |
---|
| 150 | self.menu1 = wx.Menu() |
---|
[0b6ae5f] | 151 | id1 = wx.NewId() |
---|
| 152 | simul_help = "Add new fit panel" |
---|
| 153 | self.menu1.Append(id1, '&New Fit Page',simul_help) |
---|
| 154 | wx.EVT_MENU(owner, id1, self.on_add_new_page) |
---|
[6f023e8] | 155 | self.menu1.AppendSeparator() |
---|
| 156 | id1 = wx.NewId() |
---|
[6bbeacd4] | 157 | simul_help = "Simultaneous Fit" |
---|
[2296316] | 158 | self.menu1.Append(id1, '&Simultaneous Fit',simul_help) |
---|
[6f023e8] | 159 | wx.EVT_MENU(owner, id1, self.on_add_sim_page) |
---|
[f7e9af2] | 160 | self.menu1.AppendSeparator() |
---|
[0b6ae5f] | 161 | #Set park engine |
---|
[f7e9af2] | 162 | |
---|
[0b6ae5f] | 163 | scipy_help= "Scipy Engine: Perform Simple fit. More in Help window...." |
---|
[2c6b224] | 164 | self.menu1.AppendCheckItem(self.scipy_id, "Simple FitEngine [LeastSq]", |
---|
| 165 | scipy_help) |
---|
[0b6ae5f] | 166 | wx.EVT_MENU(owner, self.scipy_id, self._onset_engine_scipy) |
---|
| 167 | |
---|
| 168 | park_help = "Park Engine: Perform Complex fit. More in Help window...." |
---|
[2c6b224] | 169 | self.menu1.AppendCheckItem(self.park_id, "Complex FitEngine [ParkMC]", |
---|
| 170 | park_help) |
---|
[0b6ae5f] | 171 | wx.EVT_MENU(owner, self.park_id, self._onset_engine_park) |
---|
| 172 | |
---|
| 173 | self.menu1.FindItemById(self.scipy_id).Check(True) |
---|
| 174 | self.menu1.FindItemById(self.park_id).Check(False) |
---|
[2c6b224] | 175 | self.menu1.AppendSeparator() |
---|
| 176 | self.id_tol = wx.NewId() |
---|
| 177 | ftol_help = "Change the current FTolerance (=%s) " % str(self.ftol) |
---|
| 178 | ftol_help += "of Simple FitEngine..." |
---|
| 179 | self.menu1.Append(self.id_tol, "Change FTolerance [LeastSq Only]", |
---|
| 180 | ftol_help) |
---|
| 181 | wx.EVT_MENU(owner, self.id_tol, self.show_ftol_dialog) |
---|
| 182 | |
---|
| 183 | |
---|
[3b19ac9] | 184 | #create menubar items |
---|
[908b817] | 185 | return [(self.menu1, self.sub_menu)] |
---|
[2db1d66] | 186 | |
---|
[51d47b5] | 187 | def on_add_sim_page(self, event): |
---|
[ed2ea6a] | 188 | """ |
---|
[5062bbf] | 189 | Create a page to access simultaneous fit option |
---|
[ed2ea6a] | 190 | """ |
---|
[a0da535] | 191 | if self.sim_page != None: |
---|
[2140e68] | 192 | msg= "Simultaneous Fit page already opened" |
---|
| 193 | wx.PostEvent(self.parent, StatusEvent(status= msg)) |
---|
| 194 | return |
---|
| 195 | |
---|
[51d47b5] | 196 | self.sim_page= self.fit_panel.add_sim_page() |
---|
[b28717b] | 197 | |
---|
[d89f09b] | 198 | def help(self, evt): |
---|
| 199 | """ |
---|
[5062bbf] | 200 | Show a general help dialog. |
---|
[d89f09b] | 201 | """ |
---|
[484faf7] | 202 | from help_panel import HelpWindow |
---|
[2268d10] | 203 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
| 204 | frame.Show(True) |
---|
| 205 | |
---|
[6bbeacd4] | 206 | def get_context_menu(self, plotpanel=None): |
---|
[d89f09b] | 207 | """ |
---|
[5062bbf] | 208 | Get the context menu items available for P(r).them allow fitting option |
---|
| 209 | for Data2D and Data1D only. |
---|
| 210 | |
---|
| 211 | :param graph: the Graph object to which we attach the context menu |
---|
| 212 | |
---|
| 213 | :return: a list of menu items with call-back function |
---|
| 214 | |
---|
| 215 | :note: if Data1D was generated from Theory1D |
---|
| 216 | the fitting option is not allowed |
---|
| 217 | |
---|
[d89f09b] | 218 | """ |
---|
[6bbeacd4] | 219 | graph = plotpanel.graph |
---|
[484faf7] | 220 | fit_option = "Select data for fitting" |
---|
| 221 | fit_hint = "Dialog with fitting parameters " |
---|
[6bbeacd4] | 222 | |
---|
| 223 | if graph.selected_plottable not in plotpanel.plots: |
---|
| 224 | return [] |
---|
| 225 | item = plotpanel.plots[graph.selected_plottable] |
---|
| 226 | if item.__class__.__name__ is "Data2D": |
---|
| 227 | if hasattr(item,"is_data"): |
---|
| 228 | if item.is_data: |
---|
| 229 | return [[fit_option, fit_hint, self._onSelect]] |
---|
| 230 | else: |
---|
| 231 | return [] |
---|
| 232 | return [[fit_option, fit_hint, self._onSelect]] |
---|
| 233 | else: |
---|
| 234 | |
---|
| 235 | # if is_data is true , this in an actual data loaded |
---|
| 236 | #else it is a data created from a theory model |
---|
| 237 | if hasattr(item,"is_data"): |
---|
| 238 | if item.is_data: |
---|
| 239 | return [[fit_option, fit_hint, |
---|
| 240 | self._onSelect]] |
---|
| 241 | else: |
---|
| 242 | return [] |
---|
[d89f09b] | 243 | return [] |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | def get_panels(self, parent): |
---|
| 247 | """ |
---|
[5062bbf] | 248 | Create and return a list of panel objects |
---|
[d89f09b] | 249 | """ |
---|
| 250 | self.parent = parent |
---|
[75fbd17] | 251 | #self.parent.Bind(EVT_FITSTATE_UPDATE, self.on_set_state_helper) |
---|
[d89f09b] | 252 | # Creation of the fit panel |
---|
[6bbeacd4] | 253 | self.fit_panel = FitPanel(parent=self.parent, manager=self) |
---|
| 254 | self.on_add_new_page(event=None) |
---|
[f93dfcb] | 255 | #Set the manager for the main panel |
---|
[d89f09b] | 256 | self.fit_panel.set_manager(self) |
---|
| 257 | # List of windows used for the perspective |
---|
| 258 | self.perspective = [] |
---|
| 259 | self.perspective.append(self.fit_panel.window_name) |
---|
[6bbeacd4] | 260 | |
---|
[3b19ac9] | 261 | #index number to create random model name |
---|
| 262 | self.index_model = 0 |
---|
[264df67] | 263 | self.index_theory= 0 |
---|
[32673ac] | 264 | self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) |
---|
[848a2ef] | 265 | self.parent.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) |
---|
[df4c3ad] | 266 | self.parent._mgr.Bind(wx.aui.EVT_AUI_PANE_CLOSE,self._onclearslicer) |
---|
[3bf4a032] | 267 | #Create reader when fitting panel are created |
---|
| 268 | self.state_reader = Reader(self.set_state) |
---|
| 269 | #append that reader to list of available reader |
---|
| 270 | loader = Loader() |
---|
| 271 | loader.associate_file_reader(".fitv", self.state_reader) |
---|
[f22e626] | 272 | #loader.associate_file_reader(".svs", self.state_reader) |
---|
[a436b2e] | 273 | #from sans.perspectives.calculator.sld_panel import SldPanel |
---|
[f93dfcb] | 274 | #Send the fitting panel to guiframe |
---|
[0912feab] | 275 | self.mypanels.append(self.fit_panel) |
---|
[a436b2e] | 276 | #self.mypanels.append(SldPanel(parent=self.parent, base=self.parent)) |
---|
[568e1a5] | 277 | return self.mypanels |
---|
[232c3db] | 278 | |
---|
[90a7bbd] | 279 | def clear_panel(self): |
---|
| 280 | """ |
---|
| 281 | """ |
---|
[81f00d7] | 282 | self.fit_panel.clear_panel() |
---|
[90a7bbd] | 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 |
---|
[a0da535] | 292 | |
---|
[17553ae] | 293 | def delete_data(self, data): |
---|
[8ee56a9] | 294 | """ |
---|
| 295 | delete the given data from panel |
---|
| 296 | """ |
---|
| 297 | self.fit_panel.delete_data(data) |
---|
| 298 | |
---|
[e88ebfd] | 299 | def set_data(self, data_list=None): |
---|
[a0da535] | 300 | """ |
---|
| 301 | receive a list of data to fit |
---|
| 302 | """ |
---|
[b2d9826] | 303 | if data_list is None: |
---|
| 304 | data_list = [] |
---|
[c26a64b] | 305 | selected_data_list = [] |
---|
[cc31608] | 306 | if self.batch_on: |
---|
| 307 | page = self.add_fit_page(data=data_list) |
---|
[c26a64b] | 308 | else: |
---|
[cc31608] | 309 | if len(data_list) > MAX_NBR_DATA: |
---|
| 310 | from fitting_widgets import DataDialog |
---|
| 311 | dlg = DataDialog(data_list=data_list, nb_data=MAX_NBR_DATA) |
---|
| 312 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 313 | selected_data_list = dlg.get_data() |
---|
| 314 | dlg.Destroy() |
---|
| 315 | |
---|
| 316 | else: |
---|
| 317 | selected_data_list = data_list |
---|
| 318 | try: |
---|
| 319 | for data in selected_data_list: |
---|
[2e42019] | 320 | page = self.add_fit_page(data=[data]) |
---|
[cc31608] | 321 | except: |
---|
| 322 | msg = "Fitting Set_data: " + str(sys.exc_value) |
---|
| 323 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[1b1bbf9] | 324 | |
---|
| 325 | def set_top_panel(self): |
---|
| 326 | """ |
---|
| 327 | Close default (welcome) panel |
---|
| 328 | """ |
---|
| 329 | if 'default' in self.parent.panels: |
---|
| 330 | self.parent.on_close_welcome_panel() |
---|
| 331 | |
---|
| 332 | |
---|
[e88ebfd] | 333 | def set_theory(self, theory_list=None): |
---|
| 334 | """ |
---|
| 335 | """ |
---|
| 336 | #set the model state for a given theory_state: |
---|
| 337 | for item in theory_list: |
---|
| 338 | try: |
---|
| 339 | _, theory_state = item |
---|
| 340 | self.fit_panel.set_model_state(theory_state) |
---|
| 341 | except: |
---|
| 342 | msg = "Fitting: cannot deal with the theory received" |
---|
| 343 | logging.error("set_theory " + msg + "\n" + str(sys.exc_value)) |
---|
| 344 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[a0da535] | 345 | |
---|
[b63dc6e] | 346 | def set_state(self, state=None, datainfo=None, format=None): |
---|
[f83b94a] | 347 | """ |
---|
[5062bbf] | 348 | Call-back method for the fit page state reader. |
---|
[8897d66] | 349 | This method is called when a .fitv/.svs file is loaded. |
---|
[5062bbf] | 350 | |
---|
[8897d66] | 351 | : param state: PageState object |
---|
| 352 | : param datainfo: data |
---|
[f83b94a] | 353 | """ |
---|
[90a7bbd] | 354 | #state = self.state_reader.get_state() |
---|
[b63dc6e] | 355 | if state != None: |
---|
[4bee68d] | 356 | state = state.clone() |
---|
[b63dc6e] | 357 | # store fitting state in temp_state |
---|
| 358 | self.temp_state.append(state) |
---|
| 359 | else: |
---|
| 360 | self.temp_state = [] |
---|
[ef16f59] | 361 | # index to start with for a new set_state |
---|
| 362 | self.state_index = 0 |
---|
[b63dc6e] | 363 | # state file format |
---|
| 364 | self.sfile_ext = format |
---|
[75fbd17] | 365 | |
---|
| 366 | self.on_set_state_helper(event=None) |
---|
[ef16f59] | 367 | |
---|
[75fbd17] | 368 | def on_set_state_helper(self,event=None): |
---|
[4da35bc] | 369 | """ |
---|
[0fd2f27] | 370 | Set_state_helper. This actually sets state |
---|
| 371 | after plotting data from state file. |
---|
[ef16f59] | 372 | |
---|
[0fd2f27] | 373 | : event: FitStateUpdateEvent called |
---|
| 374 | by dataloader.plot_data from guiframe |
---|
[4da35bc] | 375 | """ |
---|
[7a07864] | 376 | if len(self.temp_state) == 0: |
---|
[0fd2f27] | 377 | if self.state_index==0 and len(self.mypanels) <= 0 \ |
---|
| 378 | and self.sfile_ext =='.svs': |
---|
[9b18735] | 379 | self.fit_panel.add_default_pages() |
---|
[b63dc6e] | 380 | self.temp_state = [] |
---|
| 381 | self.state_index = 0 |
---|
[4da35bc] | 382 | return |
---|
[3eb2811] | 383 | |
---|
[ef16f59] | 384 | try: |
---|
| 385 | # Load fitting state |
---|
| 386 | state = self.temp_state[self.state_index] |
---|
[3eb2811] | 387 | #panel state should have model selection to set_state |
---|
| 388 | if state.formfactorcombobox != None: |
---|
| 389 | #set state |
---|
[75fbd17] | 390 | data = self.parent.create_gui_data(state.data) |
---|
| 391 | data.group_id = state.data.group_id |
---|
[f95301b] | 392 | self.parent.add_data(data_list={data.id:data}) |
---|
[75fbd17] | 393 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 394 | title=data.title)) |
---|
[f95301b] | 395 | #need to be fix later make sure we are sendind guiframe.data |
---|
| 396 | #to panel |
---|
| 397 | state.data = data |
---|
[3eb2811] | 398 | page = self.fit_panel.set_state(state) |
---|
| 399 | else: |
---|
| 400 | #just set data because set_state won't work |
---|
[75fbd17] | 401 | data = self.parent.create_gui_data(state.data) |
---|
| 402 | data.group_id = state.data.group_id |
---|
[f95301b] | 403 | self.parent.add_data(data_list={data.id:data}) |
---|
| 404 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 405 | title=data.title)) |
---|
[cc31608] | 406 | page = self.add_fit_page([data]) |
---|
[2dbffcc] | 407 | caption = page.window_caption |
---|
| 408 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
| 409 | caption=caption) |
---|
[1b1bbf9] | 410 | self.mypanels.append(page) |
---|
[3eb2811] | 411 | |
---|
[9b18735] | 412 | # get ready for the next set_state |
---|
[ef16f59] | 413 | self.state_index += 1 |
---|
[9b18735] | 414 | |
---|
[ef16f59] | 415 | #reset state variables to default when all set_state is finished. |
---|
| 416 | if len(self.temp_state) == self.state_index: |
---|
[9b18735] | 417 | |
---|
[ef16f59] | 418 | self.temp_state = [] |
---|
[b63dc6e] | 419 | #self.state_index = 0 |
---|
[ef16f59] | 420 | # Make sure the user sees the fitting panel after loading |
---|
[75fbd17] | 421 | #self.parent.set_perspective(self.perspective) |
---|
| 422 | self.on_perspective(event=None) |
---|
[ef16f59] | 423 | except: |
---|
[b63dc6e] | 424 | self.state_index==0 |
---|
[8897d66] | 425 | self.temp_state = [] |
---|
[ef16f59] | 426 | raise |
---|
[4da35bc] | 427 | |
---|
[f83b94a] | 428 | def save_fit_state(self, filepath, fitstate): |
---|
| 429 | """ |
---|
[5062bbf] | 430 | save fit page state into file |
---|
[f83b94a] | 431 | """ |
---|
| 432 | self.state_reader.write(filename=filepath, fitstate=fitstate) |
---|
[55bb249c] | 433 | |
---|
[f7ef313] | 434 | def set_fit_weight(self, uid, flag, is2d=False, fid=None): |
---|
[55bb249c] | 435 | """ |
---|
| 436 | Set the fit weights of a given page for all |
---|
| 437 | its data by default. If fid is provide then set the range |
---|
| 438 | only for the data with fid as id |
---|
| 439 | :param uid: id corresponding to a fit page |
---|
| 440 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 441 | :param weight: current dy data |
---|
| 442 | """ |
---|
| 443 | if uid in self.page_finder.keys(): |
---|
[f7ef313] | 444 | self.page_finder[uid].set_weight(flag=flag, is2d=is2d) |
---|
[55bb249c] | 445 | |
---|
[2dbffcc] | 446 | def set_fit_range(self, uid, qmin, qmax, fid=None): |
---|
[ca7a626] | 447 | """ |
---|
[2dbffcc] | 448 | Set the fitting range of a given page for all |
---|
| 449 | its data by default. If fid is provide then set the range |
---|
| 450 | only for the data with fid as id |
---|
| 451 | :param uid: id corresponding to a fit page |
---|
| 452 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 453 | :param qmin: minimum value of the fit range |
---|
| 454 | :param qmax: maximum value of the fit range |
---|
[ca7a626] | 455 | """ |
---|
[2dbffcc] | 456 | if uid in self.page_finder.keys(): |
---|
| 457 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax) |
---|
[2a8fac1] | 458 | |
---|
[2dbffcc] | 459 | def schedule_for_fit(self, value=0, uid=None): |
---|
[948add7] | 460 | """ |
---|
[5062bbf] | 461 | Set the fit problem field to 0 or 1 to schedule that problem to fit. |
---|
| 462 | Schedule the specified fitproblem or get the fit problem related to |
---|
| 463 | the current page and set value. |
---|
| 464 | :param value: integer 0 or 1 |
---|
[2dbffcc] | 465 | :param uid: the id related to a page contaning fitting information |
---|
| 466 | """ |
---|
| 467 | if uid in self.page_finder.keys(): |
---|
[66ff250] | 468 | self.page_finder[uid].schedule_tofit(value) |
---|
[ca7a626] | 469 | |
---|
[d89f09b] | 470 | def get_page_finder(self): |
---|
[5062bbf] | 471 | """ |
---|
| 472 | return self.page_finder used also by simfitpage.py |
---|
| 473 | """ |
---|
[d89f09b] | 474 | return self.page_finder |
---|
| 475 | |
---|
[3b19ac9] | 476 | def set_page_finder(self,modelname,names,values): |
---|
[d89f09b] | 477 | """ |
---|
[5062bbf] | 478 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
| 479 | |
---|
[0fd2f27] | 480 | :param modelname: the name ot the model for with the parameter |
---|
| 481 | has to reset |
---|
[5062bbf] | 482 | :param value: can be a string in this case. |
---|
| 483 | :param names: the paramter name |
---|
| 484 | |
---|
| 485 | :note: expecting park used for fit. |
---|
| 486 | |
---|
[d89f09b] | 487 | """ |
---|
[66ff250] | 488 | sim_page_id = self.sim_page.uid |
---|
| 489 | for uid, value in self.page_finder.iteritems(): |
---|
| 490 | if uid != sim_page_id: |
---|
[6bbeacd4] | 491 | list = value.get_model() |
---|
[f93dfcb] | 492 | model = list[0] |
---|
[6bbeacd4] | 493 | if model.name == modelname: |
---|
| 494 | value.set_model_param(names, values) |
---|
[d89f09b] | 495 | break |
---|
[2db1d66] | 496 | |
---|
[d89f09b] | 497 | def split_string(self,item): |
---|
| 498 | """ |
---|
[5062bbf] | 499 | receive a word containing dot and split it. used to split parameterset |
---|
| 500 | name into model name and parameter name example: :: |
---|
| 501 | |
---|
[3b19ac9] | 502 | paramaterset (item) = M1.A |
---|
[5062bbf] | 503 | Will return model_name = M1 , parameter name = A |
---|
| 504 | |
---|
[d89f09b] | 505 | """ |
---|
| 506 | if string.find(item,".")!=-1: |
---|
| 507 | param_names= re.split("\.",item) |
---|
[6d91073] | 508 | model_name=param_names[0] |
---|
| 509 | ##Assume max len is 3; eg., M0.radius.width |
---|
| 510 | if len(param_names) == 3: |
---|
| 511 | param_name=param_names[1]+"."+param_names[2] |
---|
| 512 | else: |
---|
| 513 | param_name=param_names[1] |
---|
[d89f09b] | 514 | return model_name,param_name |
---|
[2296316] | 515 | |
---|
| 516 | def set_ftol(self, ftol=None): |
---|
| 517 | """ |
---|
| 518 | Set ftol: Relative error desired in the sum of chi squares. |
---|
| 519 | """ |
---|
| 520 | # check if it is flaot |
---|
| 521 | try: |
---|
| 522 | f_tol = float(ftol) |
---|
| 523 | except: |
---|
| 524 | # default |
---|
[2c6b224] | 525 | f_tol = SANS_F_TOL |
---|
[2296316] | 526 | |
---|
| 527 | self.ftol = f_tol |
---|
[2c6b224] | 528 | # update ftol menu help strings |
---|
| 529 | ftol_help = "Change the current FTolerance (=%s) " % str(self.ftol) |
---|
| 530 | ftol_help += "of Simple FitEngine..." |
---|
| 531 | self.menu1.SetHelpString(self.id_tol, ftol_help) |
---|
| 532 | |
---|
| 533 | def show_ftol_dialog(self, event=None): |
---|
| 534 | """ |
---|
| 535 | Dialog to select ftol for Scipy |
---|
| 536 | """ |
---|
| 537 | #if event != None: |
---|
| 538 | # event.Skip() |
---|
| 539 | from ftol_dialog import ChangeFtol |
---|
| 540 | panel = ChangeFtol(self.parent, self) |
---|
| 541 | panel.ShowModal() |
---|
| 542 | |
---|
[66ff250] | 543 | def stop_fit(self, uid): |
---|
[ed2ea6a] | 544 | """ |
---|
[5062bbf] | 545 | Stop the fit engine |
---|
[ed2ea6a] | 546 | """ |
---|
[66ff250] | 547 | if uid in self.fit_thread_list.keys(): |
---|
| 548 | calc_fit = self.fit_thread_list[uid] |
---|
| 549 | if calc_fit is not None and calc_fit.isrunning(): |
---|
| 550 | calc_fit.stop() |
---|
| 551 | msg = "Fit stop!" |
---|
| 552 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[3ddc2c4] | 553 | del self.fit_thread_list[uid] |
---|
[66ff250] | 554 | #set the fit button label of page when fit stop is trigger from |
---|
| 555 | #simultaneous fit pane |
---|
| 556 | if self.sim_page is not None and uid == self.sim_page.uid: |
---|
| 557 | for uid, value in self.page_finder.iteritems(): |
---|
| 558 | if value.get_scheduled() == 1: |
---|
| 559 | if uid in self.fit_panel.opened_pages.keys(): |
---|
| 560 | panel = self.fit_panel.opened_pages[uid] |
---|
| 561 | panel. _on_fit_complete() |
---|
[6bbeacd4] | 562 | |
---|
[2dbffcc] | 563 | def set_smearer(self, uid, smearer, fid, qmin=None, qmax=None, draw=True, |
---|
| 564 | enable_smearer=False): |
---|
| 565 | """ |
---|
| 566 | Get a smear object and store it to a fit problem of fid as id. If proper |
---|
| 567 | flag is enable , will plot the theory with smearing information. |
---|
| 568 | |
---|
| 569 | :param smearer: smear object to allow smearing data of id fid |
---|
| 570 | :param enable_smearer: Define whether or not all (data, model) contained |
---|
| 571 | in the structure of id uid will be smeared before fitting. |
---|
| 572 | :param qmin: the maximum value of the theory plotting range |
---|
| 573 | :param qmax: the maximum value of the theory plotting range |
---|
| 574 | :param draw: Determine if the theory needs to be plot |
---|
[ca7a626] | 575 | """ |
---|
[66ff250] | 576 | if uid not in self.page_finder.keys(): |
---|
[2dbffcc] | 577 | return |
---|
| 578 | self.page_finder[uid].enable_smearing(flag=enable_smearer) |
---|
| 579 | self.page_finder[uid].set_smearer(smearer, fid=fid) |
---|
[6bbeacd4] | 580 | if draw: |
---|
| 581 | ## draw model 1D with smeared data |
---|
[2dbffcc] | 582 | data = self.page_finder[uid].get_fit_data(fid=fid) |
---|
| 583 | if data is None: |
---|
| 584 | msg = "set_mearer requires at least data.\n" |
---|
| 585 | msg += "Got data = %s .\n" % str(data) |
---|
| 586 | raise ValueError, msg |
---|
| 587 | model = self.page_finder[uid].get_model(fid=fid) |
---|
[6bbeacd4] | 588 | if model is None: |
---|
| 589 | return |
---|
[2dbffcc] | 590 | enable1D = issubclass(data.__class__, Data1D) |
---|
| 591 | enable2D = issubclass(data.__class__, Data2D) |
---|
[6bbeacd4] | 592 | ## if user has already selected a model to plot |
---|
| 593 | ## redraw the model with data smeared |
---|
[2dbffcc] | 594 | smear = self.page_finder[uid].get_smearer(fid=fid) |
---|
[66ff250] | 595 | self.draw_model(model=model, data=data, page_id=uid, smearer=smear, |
---|
[ba1f0b2] | 596 | enable1D=enable1D, enable2D=enable2D, |
---|
[55bb249c] | 597 | qmin=qmin, qmax=qmax, weight=None) |
---|
[f6aee42] | 598 | self._mac_sleep(0.2) |
---|
| 599 | |
---|
| 600 | def _mac_sleep(self, sec=0.2): |
---|
[2dbffcc] | 601 | """ |
---|
| 602 | Give sleep to MAC |
---|
| 603 | """ |
---|
[f6aee42] | 604 | if ON_MAC: |
---|
[2dbffcc] | 605 | time.sleep(sec) |
---|
| 606 | |
---|
[66ff250] | 607 | def draw_model(self, model, page_id, data=None, smearer=None, |
---|
[67e258c] | 608 | enable1D=True, enable2D=False, |
---|
[5ef55d2] | 609 | state=None, |
---|
[fa65e99] | 610 | toggle_mode_on=False, |
---|
[2dbffcc] | 611 | qmin=None, qmax=None, |
---|
[55bb249c] | 612 | update_chisqr=True, weight=None): |
---|
[ca7a626] | 613 | """ |
---|
[5062bbf] | 614 | Draw model. |
---|
| 615 | |
---|
| 616 | :param model: the model to draw |
---|
| 617 | :param name: the name of the model to draw |
---|
| 618 | :param data: the data on which the model is based to be drawn |
---|
| 619 | :param description: model's description |
---|
| 620 | :param enable1D: if true enable drawing model 1D |
---|
| 621 | :param enable2D: if true enable drawing model 2D |
---|
| 622 | :param qmin: Range's minimum value to draw model |
---|
| 623 | :param qmax: Range's maximum value to draw model |
---|
| 624 | :param qstep: number of step to divide the x and y-axis |
---|
[2296316] | 625 | :param update_chisqr: update chisqr [bool] |
---|
[ed2ea6a] | 626 | |
---|
[d89f09b] | 627 | """ |
---|
[55bb249c] | 628 | self.weight = weight |
---|
[ad3fa1e] | 629 | if issubclass(data.__class__, Data1D) or not enable2D: |
---|
[f72333f] | 630 | ## draw model 1D with no loaded data |
---|
[67e258c] | 631 | self._draw_model1D(model=model, |
---|
| 632 | data=data, |
---|
[66ff250] | 633 | page_id=page_id, |
---|
[67e258c] | 634 | enable1D=enable1D, |
---|
| 635 | smearer=smearer, |
---|
| 636 | qmin=qmin, |
---|
| 637 | qmax=qmax, |
---|
[fa65e99] | 638 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 639 | state=state, |
---|
[2296316] | 640 | update_chisqr=update_chisqr) |
---|
[f72333f] | 641 | else: |
---|
| 642 | ## draw model 2D with no initial data |
---|
[ba1f0b2] | 643 | self._draw_model2D(model=model, |
---|
[66ff250] | 644 | page_id=page_id, |
---|
[67e258c] | 645 | data=data, |
---|
| 646 | enable2D=enable2D, |
---|
| 647 | smearer=smearer, |
---|
| 648 | qmin=qmin, |
---|
| 649 | qmax=qmax, |
---|
[5ef55d2] | 650 | state=state, |
---|
[fa65e99] | 651 | toggle_mode_on=toggle_mode_on, |
---|
[2296316] | 652 | update_chisqr=update_chisqr) |
---|
[2db1d66] | 653 | |
---|
[922497f] | 654 | def onFit(self, uid): |
---|
[ca7a626] | 655 | """ |
---|
[2dbffcc] | 656 | Get series of data, model, associates parameters and range and send then |
---|
| 657 | to series of fit engines. Fit data and model, display result to |
---|
| 658 | corresponding panels. |
---|
| 659 | :param uid: id related to the panel currently calling this fit function. |
---|
[ca7a626] | 660 | """ |
---|
[bb18ef1] | 661 | ## count the number of fitproblem schedule to fit |
---|
[67e258c] | 662 | fitproblem_count = 0 |
---|
[66ff250] | 663 | for value in self.page_finder.values(): |
---|
[67e258c] | 664 | if value.get_scheduled() == 1: |
---|
[bb18ef1] | 665 | fitproblem_count += 1 |
---|
| 666 | ## if simultaneous fit change automatically the engine to park |
---|
[cc31608] | 667 | if fitproblem_count > 1: |
---|
[bb18ef1] | 668 | self._on_change_engine(engine='park') |
---|
[c660493] | 669 | self.fitproblem_count = fitproblem_count |
---|
[67e258c] | 670 | if self._fit_engine == "park": |
---|
| 671 | engineType = "Simultaneous Fit" |
---|
[ca7a626] | 672 | else: |
---|
[67e258c] | 673 | engineType = "Single Fit" |
---|
[922497f] | 674 | fitter_list = [] |
---|
| 675 | sim_fitter = None |
---|
| 676 | if self.sim_page is not None and self.sim_page.uid == uid: |
---|
| 677 | #simulatanous fit only one engine need to be created |
---|
| 678 | sim_fitter = Fit(self._fit_engine) |
---|
| 679 | fitter_list.append(sim_fitter) |
---|
| 680 | |
---|
[67e258c] | 681 | self.current_pg = None |
---|
[66ff250] | 682 | list_page_id = [] |
---|
[922497f] | 683 | fit_id = 0 |
---|
[33dd2e5] | 684 | batch_inputs = {} |
---|
| 685 | batch_outputs = {} |
---|
[66ff250] | 686 | for page_id, value in self.page_finder.iteritems(): |
---|
[dc613d6] | 687 | # For simulfit (uid give with None), do for-loop |
---|
| 688 | # if uid is specified (singlefit), do it only on the page. |
---|
| 689 | if engineType == "Single Fit": |
---|
| 690 | if page_id != uid: |
---|
| 691 | continue |
---|
[d89f09b] | 692 | try: |
---|
[67e258c] | 693 | if value.get_scheduled() == 1: |
---|
[f93dfcb] | 694 | #Get list of parameters name to fit |
---|
[d89f09b] | 695 | pars = [] |
---|
| 696 | templist = [] |
---|
[66ff250] | 697 | page = self.fit_panel.get_page_by_id(page_id) |
---|
[3b19ac9] | 698 | templist = page.get_param_list() |
---|
[d89f09b] | 699 | for element in templist: |
---|
[513115c] | 700 | name = str(element[1]) |
---|
[ca7a626] | 701 | pars.append(name) |
---|
[922497f] | 702 | for fitproblem in value.get_fit_problem(): |
---|
| 703 | if sim_fitter is None: |
---|
| 704 | fitter = Fit(self._fit_engine) |
---|
[33dd2e5] | 705 | self._fit_helper(fitproblem=fitproblem, |
---|
| 706 | pars=pars, |
---|
| 707 | fitter=fitter, |
---|
| 708 | fit_id=fit_id, |
---|
| 709 | batch_inputs=batch_inputs, |
---|
| 710 | batch_outputs=batch_outputs) |
---|
[922497f] | 711 | fitter_list.append(fitter) |
---|
| 712 | else: |
---|
| 713 | fitter = sim_fitter |
---|
[33dd2e5] | 714 | self._fit_helper(fitproblem=fitproblem, |
---|
| 715 | pars=pars, |
---|
| 716 | fitter=fitter, |
---|
| 717 | fit_id=fit_id, |
---|
| 718 | batch_inputs=batch_inputs, |
---|
| 719 | batch_outputs=batch_outputs) |
---|
[922497f] | 720 | fit_id += 1 |
---|
[66ff250] | 721 | list_page_id.append(page_id) |
---|
| 722 | current_page_id = page_id |
---|
[922497f] | 723 | value.clear_model_param() |
---|
[d89f09b] | 724 | except: |
---|
[33dd2e5] | 725 | raise |
---|
[ba1f0b2] | 726 | msg= "%s error: %s" % (engineType, sys.exc_value) |
---|
| 727 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 728 | type="stop")) |
---|
| 729 | return |
---|
[e54d2c32] | 730 | ## If a thread is already started, stop it |
---|
| 731 | #if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
| 732 | # self.calc_fit.stop() |
---|
[a2cb1f9] | 733 | msg = "Fitting is in progress..." |
---|
| 734 | wx.PostEvent( self.parent, StatusEvent(status=msg, type="progress" )) |
---|
| 735 | |
---|
| 736 | #Handler used for park engine displayed message |
---|
[66ff250] | 737 | handler = ConsoleUpdate(parent=self.parent, |
---|
| 738 | manager=self, |
---|
| 739 | improvement_delta=0.1) |
---|
[f6aee42] | 740 | self._mac_sleep(0.2) |
---|
[e54d2c32] | 741 | ## perform single fit |
---|
| 742 | if fitproblem_count == 1: |
---|
[58e0c83] | 743 | calc_fit = FitThread(handler = handler, |
---|
[cc31608] | 744 | fn=fitter_list, |
---|
[2296316] | 745 | pars=pars, |
---|
[33dd2e5] | 746 | batch_inputs=batch_inputs, |
---|
| 747 | batch_outputs=batch_outputs, |
---|
[2296316] | 748 | page_id=list_page_id, |
---|
| 749 | completefn=self._single_fit_completed, |
---|
| 750 | ftol=self.ftol) |
---|
[e54d2c32] | 751 | else: |
---|
[66ff250] | 752 | current_page_id = self.sim_page.uid |
---|
[e54d2c32] | 753 | ## Perform more than 1 fit at the time |
---|
[58e0c83] | 754 | calc_fit = FitThread(handler=handler, |
---|
[cc31608] | 755 | fn=fitter_list, |
---|
[33dd2e5] | 756 | batch_inputs=batch_inputs, |
---|
| 757 | batch_outputs=batch_outputs, |
---|
[66ff250] | 758 | page_id=list_page_id, |
---|
[2296316] | 759 | updatefn=handler.update_fit, |
---|
[8b481a51] | 760 | completefn=self._simul_fit_completed, |
---|
[2296316] | 761 | ftol=self.ftol) |
---|
[66ff250] | 762 | self.fit_thread_list[current_page_id] = calc_fit |
---|
[a2cb1f9] | 763 | calc_fit.queue() |
---|
[cc31608] | 764 | msg = "Fitting is in progress..." |
---|
| 765 | wx.PostEvent( self.parent, StatusEvent(status=msg, type="progress" )) |
---|
| 766 | |
---|
| 767 | self.ready_fit(calc_fit=calc_fit) |
---|
| 768 | |
---|
[662da312] | 769 | def ready_fit(self, calc_fit): |
---|
[c660493] | 770 | """ |
---|
| 771 | Ready for another fit |
---|
| 772 | """ |
---|
| 773 | if self.fitproblem_count != None and self.fitproblem_count > 1: |
---|
[662da312] | 774 | calc_fit.ready(2.5) |
---|
[c660493] | 775 | else: |
---|
| 776 | time.sleep(0.4) |
---|
[2f189dc] | 777 | |
---|
[2dbffcc] | 778 | def remove_plot(self, uid, fid=None, theory=False): |
---|
[2f189dc] | 779 | """ |
---|
[5062bbf] | 780 | remove model plot when a fit page is closed |
---|
[2dbffcc] | 781 | :param uid: the id related to the fitpage to close |
---|
| 782 | :param fid: the id of the fitproblem(data, model, range,etc) |
---|
[2f189dc] | 783 | """ |
---|
[2dbffcc] | 784 | if uid not in self.page_finder.keys(): |
---|
| 785 | return |
---|
| 786 | fitproblemList = self.page_finder[uid].get_fit_problem(fid) |
---|
| 787 | for fitproblem in fitproblemList: |
---|
| 788 | data = fitproblem.get_fit_data() |
---|
| 789 | model = fitproblem.get_model() |
---|
| 790 | plot_id = None |
---|
| 791 | if model is not None: |
---|
| 792 | plot_id = data.id + name |
---|
| 793 | if theory: |
---|
| 794 | plot_id = data.id |
---|
| 795 | group_id = data.group_id |
---|
| 796 | wx.PostEvent(self.parent, NewPlotEvent(id=plot_id, |
---|
| 797 | group_id=group_id, |
---|
| 798 | action='remove')) |
---|
[edcbd467] | 799 | |
---|
[2dbffcc] | 800 | def store_data(self, uid, data_list=None, caption=None): |
---|
[31b0c47] | 801 | """ |
---|
[2dbffcc] | 802 | Recieve a list of data and store them ans well as a caption of |
---|
| 803 | the fit page where they come from. |
---|
| 804 | :param uid: if related to a fit page |
---|
| 805 | :param data_list: list of data to fit |
---|
| 806 | :param caption: caption of the window related to these data |
---|
[31b0c47] | 807 | """ |
---|
[cc31608] | 808 | if data_list is None: |
---|
| 809 | data_list = [] |
---|
[f7ef313] | 810 | |
---|
[2dbffcc] | 811 | self.page_finder[uid].set_fit_data(data=data_list) |
---|
| 812 | if caption is not None: |
---|
| 813 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[f7ef313] | 814 | |
---|
[6bbeacd4] | 815 | def on_add_new_page(self, event=None): |
---|
[edcbd467] | 816 | """ |
---|
[6bbeacd4] | 817 | ask fit panel to create a new empty page |
---|
[edcbd467] | 818 | """ |
---|
| 819 | try: |
---|
[6bbeacd4] | 820 | page = self.fit_panel.add_empty_page() |
---|
[2dbffcc] | 821 | page_caption = page.window_caption |
---|
[6450d8c] | 822 | # add data associated to the page created |
---|
| 823 | if page != None: |
---|
[f304194] | 824 | wx.PostEvent(self.parent, StatusEvent(status="Page Created", |
---|
[31b0c47] | 825 | info="info")) |
---|
[edcbd467] | 826 | else: |
---|
[f304194] | 827 | msg = "Page was already Created" |
---|
[a0da535] | 828 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 829 | info="warning")) |
---|
[1b1bbf9] | 830 | self.set_top_panel() |
---|
[edcbd467] | 831 | except: |
---|
[2dbffcc] | 832 | msg = "Creating Fit page: %s"%sys.exc_value |
---|
| 833 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[6bbeacd4] | 834 | |
---|
| 835 | def add_fit_page(self, data): |
---|
| 836 | """ |
---|
| 837 | given a data, ask to the fitting panel to create a new fitting page, |
---|
| 838 | get this page and store it into the page_finder of this plug-in |
---|
[2dbffcc] | 839 | :param data: is a list of data |
---|
[6bbeacd4] | 840 | """ |
---|
| 841 | page = self.fit_panel.set_data(data) |
---|
[7f76f89] | 842 | # page could be None when loading state files |
---|
| 843 | if page == None: |
---|
| 844 | return page |
---|
[2dbffcc] | 845 | page_caption = page.window_caption |
---|
[6bbeacd4] | 846 | #append Data1D to the panel containing its theory |
---|
| 847 | #if theory already plotted |
---|
[66ff250] | 848 | if page.uid in self.page_finder: |
---|
[cc31608] | 849 | data = page.get_data() |
---|
[2dbffcc] | 850 | theory_data = self.page_finder[page.uid].get_theory_data(data.id) |
---|
[6bbeacd4] | 851 | if issubclass(data.__class__, Data2D): |
---|
[e88ebfd] | 852 | data.group_id = wx.NewId() |
---|
[a5701e6] | 853 | if theory_data is not None: |
---|
[66ff250] | 854 | group_id = str(page.uid) + " Model1D" |
---|
[a5701e6] | 855 | wx.PostEvent(self.parent, |
---|
| 856 | NewPlotEvent(group_id=group_id, |
---|
| 857 | action="delete")) |
---|
[0fd2f27] | 858 | self.parent.update_data(prev_data=theory_data, |
---|
| 859 | new_data=data) |
---|
[6bbeacd4] | 860 | else: |
---|
| 861 | if theory_data is not None: |
---|
[66ff250] | 862 | group_id = str(page.uid) + " Model2D" |
---|
[e88ebfd] | 863 | data.group_id = theory_data.group_id |
---|
[a5701e6] | 864 | wx.PostEvent(self.parent, |
---|
| 865 | NewPlotEvent(group_id=group_id, |
---|
| 866 | action="delete")) |
---|
[0fd2f27] | 867 | self.parent.update_data(prev_data=theory_data, |
---|
| 868 | new_data=data) |
---|
[2dbffcc] | 869 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
| 870 | caption=page.window_caption) |
---|
[33dd2e5] | 871 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 872 | self.sim_page.draw_page() |
---|
[1b1bbf9] | 873 | return page |
---|
[6bbeacd4] | 874 | |
---|
[848a2ef] | 875 | def _onEVT_SLICER_PANEL(self, event): |
---|
| 876 | """ |
---|
[5062bbf] | 877 | receive and event telling to update a panel with a name starting with |
---|
[2e08a9f] | 878 | event.panel_name. this method update slicer panel |
---|
| 879 | for a given interactor. |
---|
[5062bbf] | 880 | |
---|
[2e08a9f] | 881 | :param event: contains type of slicer , paramaters for updating |
---|
| 882 | the panel and panel_name to find the slicer 's panel concerned. |
---|
[848a2ef] | 883 | """ |
---|
| 884 | for item in self.parent.panels: |
---|
[0fd2f27] | 885 | name = event.panel_name |
---|
| 886 | if self.parent.panels[item].window_caption.startswith(name): |
---|
[848a2ef] | 887 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
[df4c3ad] | 888 | |
---|
| 889 | self.parent._mgr.Update() |
---|
[b324aa9] | 890 | |
---|
[848a2ef] | 891 | def _closed_fitpage(self, event): |
---|
| 892 | """ |
---|
[5062bbf] | 893 | request fitpanel to close a given page when its unique data is removed |
---|
| 894 | from the plot. close fitpage only when the a loaded data is removed |
---|
[848a2ef] | 895 | """ |
---|
[784e2fa] | 896 | if event is None or event.data is None: |
---|
| 897 | return |
---|
| 898 | if hasattr(event.data,"is_data"): |
---|
[a0da535] | 899 | if not event.data.is_data or \ |
---|
[66ff250] | 900 | event.data.__class__.__name__ == "Data1D": |
---|
[784e2fa] | 901 | self.fit_panel.close_page_with_data(event.data) |
---|
[2dbffcc] | 902 | |
---|
[dc613d6] | 903 | def _reset_schedule_problem(self, value=0, uid=None): |
---|
[6f023e8] | 904 | """ |
---|
[5062bbf] | 905 | unschedule or schedule all fitproblem to be fit |
---|
[6f023e8] | 906 | """ |
---|
[dc613d6] | 907 | # case that uid is not specified |
---|
| 908 | if uid == None: |
---|
| 909 | for page_id in self.page_finder.keys(): |
---|
| 910 | self.page_finder[page_id].schedule_tofit(value) |
---|
| 911 | # when uid is given |
---|
| 912 | else: |
---|
[2dbffcc] | 913 | if uid in self.page_finder.keys(): |
---|
| 914 | self.page_finder[uid].schedule_tofit(value) |
---|
[dc613d6] | 915 | |
---|
[33dd2e5] | 916 | def _fit_helper(self, fitproblem, pars, fitter, fit_id, |
---|
| 917 | batch_inputs, batch_outputs): |
---|
[2140e68] | 918 | """ |
---|
[2dbffcc] | 919 | Create and set fit engine with series of data and model |
---|
| 920 | :param pars: list of fittable parameters |
---|
| 921 | :param fitter_list: list of fit engine |
---|
| 922 | :param value: structure storing data mapped to their model, range etc.. |
---|
[2140e68] | 923 | """ |
---|
[922497f] | 924 | data = fitproblem.get_fit_data() |
---|
| 925 | model = fitproblem.get_model() |
---|
| 926 | smearer = fitproblem.get_smearer() |
---|
| 927 | qmin, qmax = fitproblem.get_range() |
---|
| 928 | #Extra list of parameters and their constraints |
---|
| 929 | listOfConstraint = [] |
---|
| 930 | param = fitproblem.get_model_param() |
---|
| 931 | if len(param) > 0: |
---|
| 932 | for item in param: |
---|
| 933 | ## check if constraint |
---|
| 934 | if item[0] != None and item[1] != None: |
---|
| 935 | listOfConstraint.append((item[0],item[1])) |
---|
[33dd2e5] | 936 | for param in model.getParamList(): |
---|
| 937 | if param not in pars: |
---|
| 938 | if param not in batch_inputs.keys(): |
---|
| 939 | batch_inputs[param] = [] |
---|
[cfcb7197] | 940 | if param not in model.non_fittable: |
---|
| 941 | batch_inputs[param].append(model.getParam(param)) |
---|
[922497f] | 942 | fitter.set_model(model, fit_id, pars, constraints=listOfConstraint) |
---|
| 943 | fitter.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin, |
---|
| 944 | qmax=qmax) |
---|
| 945 | fitter.select_problem_for_fit(id=fit_id, value=1) |
---|
[33dd2e5] | 946 | #fill batch result information |
---|
| 947 | if "Data" not in batch_outputs.keys(): |
---|
| 948 | batch_outputs["Data"] = [] |
---|
| 949 | batch_outputs["Data"].append(data.name) |
---|
| 950 | for key, value in data.meta_data.iteritems(): |
---|
| 951 | if key not in batch_inputs.keys(): |
---|
| 952 | batch_inputs[key] = [] |
---|
| 953 | batch_inputs[key].append(value) |
---|
| 954 | param = "temperature" |
---|
| 955 | if hasattr(data.sample, param): |
---|
| 956 | if param not in batch_inputs.keys(): |
---|
| 957 | batch_inputs[param] = [] |
---|
| 958 | batch_inputs[param].append(data.sample.temperature) |
---|
| 959 | |
---|
[c77d859] | 960 | def _onSelect(self,event): |
---|
| 961 | """ |
---|
[5062bbf] | 962 | when Select data to fit a new page is created .Its reference is |
---|
| 963 | added to self.page_finder |
---|
[d89f09b] | 964 | """ |
---|
[c77d859] | 965 | self.panel = event.GetEventObject() |
---|
[67e258c] | 966 | Plugin.on_perspective(self, event=event) |
---|
[6ed82c7] | 967 | for plottable in self.panel.graph.plottables: |
---|
[2fff4db] | 968 | if plottable.__class__.__name__ in ["Data1D", "Theory1D"]: |
---|
[3831ea1e] | 969 | data_id = self.panel.graph.selected_plottable |
---|
| 970 | if plottable == self.panel.plots[data_id]: |
---|
[6bbeacd4] | 971 | data = plottable |
---|
[cc31608] | 972 | self.add_fit_page(data=[data]) |
---|
[edcbd467] | 973 | return |
---|
[c77d859] | 974 | else: |
---|
[6bbeacd4] | 975 | data = plottable |
---|
[cc31608] | 976 | self.add_fit_page(data=[data]) |
---|
[1b1bbf9] | 977 | self.set_top_panel() |
---|
[1c1436d] | 978 | |
---|
[66ff250] | 979 | def update_fit(self, result=None, msg=""): |
---|
| 980 | """ |
---|
| 981 | """ |
---|
| 982 | print "update_fit result", result |
---|
| 983 | |
---|
[2dbffcc] | 984 | def _batch_single_fit_complete_helper(self, result, pars, page_id, |
---|
[33dd2e5] | 985 | batch_outputs, batch_inputs, elapsed=None): |
---|
[cc31608] | 986 | """ |
---|
[2dbffcc] | 987 | Display fit result in batch |
---|
| 988 | :param result: list of objects received fromt fit engines |
---|
| 989 | :param pars: list of fitted parameters names |
---|
| 990 | :param page_id: list of page ids which called fit function |
---|
| 991 | :param elapsed: time spent at the fitting level |
---|
[cc31608] | 992 | """ |
---|
| 993 | self._update_fit_button(page_id) |
---|
| 994 | msg = "Single Fitting complete " |
---|
| 995 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 996 | type="stop")) |
---|
[33dd2e5] | 997 | if batch_outputs is None: |
---|
| 998 | batch_outputs = {} |
---|
[cc31608] | 999 | if self.batch_on: |
---|
[33dd2e5] | 1000 | # format batch_outputs |
---|
| 1001 | batch_outputs["Chi2"] = [] |
---|
[cc31608] | 1002 | for index in range(len(pars)): |
---|
[33dd2e5] | 1003 | batch_outputs[pars[index]] = [] |
---|
| 1004 | batch_outputs["error on %s" % pars[index]] = [] |
---|
[cc31608] | 1005 | for res in result: |
---|
[59de3e43] | 1006 | if res is None: |
---|
[2e08a9f] | 1007 | null_value = numpy.nan |
---|
[33dd2e5] | 1008 | batch_outputs["Chi2"].append(null_value) |
---|
[2e08a9f] | 1009 | for index in range(len(pars)): |
---|
[33dd2e5] | 1010 | batch_outputs[pars[index]].append(null_value) |
---|
[2e08a9f] | 1011 | item = null_value |
---|
[33dd2e5] | 1012 | batch_outputs["error on %s" % pars[index]].append(item) |
---|
[2e08a9f] | 1013 | else: |
---|
[33dd2e5] | 1014 | batch_outputs["Chi2"].append(res.fitness) |
---|
[2e08a9f] | 1015 | for index in range(len(pars)): |
---|
[33dd2e5] | 1016 | batch_outputs[pars[index]].append(res.pvec[index]) |
---|
[2e08a9f] | 1017 | item = res.stderr[index] |
---|
[33dd2e5] | 1018 | batch_outputs["error on %s" % pars[index]].append(item) |
---|
[cc31608] | 1019 | pid = page_id[0] |
---|
[33dd2e5] | 1020 | self.page_finder[pid].set_result(result=batch_outputs) |
---|
| 1021 | self.parent.on_set_batch_result(data_outputs=batch_outputs, |
---|
| 1022 | data_inputs=batch_inputs, |
---|
[6446f87] | 1023 | plugin_name=self.sub_menu) |
---|
[2dbffcc] | 1024 | for uid in page_id: |
---|
| 1025 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
| 1026 | cpage._on_fit_complete() |
---|
[cc31608] | 1027 | |
---|
[33dd2e5] | 1028 | def _single_fit_completed(self, result, pars, page_id, batch_outputs, |
---|
| 1029 | batch_inputs=None, elapsed=None): |
---|
[c77d859] | 1030 | """ |
---|
[2dbffcc] | 1031 | Display fit result on one page of the notebook. |
---|
| 1032 | :param result: list of object generated when fit ends |
---|
[5062bbf] | 1033 | :param pars: list of names of parameters fitted |
---|
[2dbffcc] | 1034 | :param page_id: list of page ids which called fit function |
---|
| 1035 | :param elapsed: time spent at the fitting level |
---|
[a2cb1f9] | 1036 | """ |
---|
[f6aee42] | 1037 | self._mac_sleep(0.2) |
---|
[9e4365b] | 1038 | if page_id[0] in self.fit_thread_list.keys(): |
---|
[cc31608] | 1039 | del self.fit_thread_list[page_id[0]] |
---|
| 1040 | if self.batch_on: |
---|
[0fd2f27] | 1041 | wx.CallAfter(self._batch_single_fit_complete_helper, |
---|
[33dd2e5] | 1042 | result, pars, page_id, batch_outputs, |
---|
| 1043 | batch_inputs, elapsed) |
---|
[cc31608] | 1044 | return |
---|
| 1045 | else: |
---|
| 1046 | try: |
---|
| 1047 | result = result[0] |
---|
| 1048 | if result == None: |
---|
| 1049 | self._update_fit_button(page_id) |
---|
| 1050 | msg= "Single Fitting did not converge!!!" |
---|
| 1051 | wx.PostEvent(self.parent, |
---|
| 1052 | StatusEvent(status=msg, |
---|
| 1053 | info="warning", |
---|
| 1054 | type="stop")) |
---|
| 1055 | return |
---|
| 1056 | if not numpy.isfinite(result.fitness) or \ |
---|
| 1057 | numpy.any(result.pvec == None) or \ |
---|
| 1058 | not numpy.all(numpy.isfinite(result.pvec)): |
---|
| 1059 | msg = "Single Fitting did not converge!!!" |
---|
| 1060 | wx.PostEvent(self.parent, |
---|
| 1061 | StatusEvent(status=msg, |
---|
| 1062 | info="warning", |
---|
| 1063 | type="stop")) |
---|
| 1064 | self._update_fit_button(page_id) |
---|
| 1065 | return |
---|
| 1066 | |
---|
| 1067 | for uid in page_id: |
---|
| 1068 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
[0fd2f27] | 1069 | # Make sure we got all results |
---|
| 1070 | #(CallAfter is important to MAC) |
---|
[2dbffcc] | 1071 | wx.CallAfter(cpage.onsetValues, result.fitness, pars, |
---|
| 1072 | result.pvec, result.stderr) |
---|
[cc31608] | 1073 | cpage._on_fit_complete() |
---|
| 1074 | if result.stderr == None: |
---|
| 1075 | msg = "Fit Abort: " |
---|
| 1076 | else: |
---|
| 1077 | msg = "Fitting: " |
---|
| 1078 | msg += "Completed!!!" |
---|
| 1079 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 1080 | except ValueError: |
---|
| 1081 | self._update_fit_button(page_id) |
---|
[67e258c] | 1082 | msg = "Single Fitting did not converge!!!" |
---|
[cc31608] | 1083 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 1084 | type="stop")) |
---|
| 1085 | except: |
---|
[12cd4ec] | 1086 | self._update_fit_button(page_id) |
---|
[cc31608] | 1087 | msg = "Single Fit completed but Following" |
---|
| 1088 | msg += " error occurred:%s" % sys.exc_value |
---|
| 1089 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 1090 | type="stop")) |
---|
| 1091 | raise |
---|
[2dbffcc] | 1092 | |
---|
[33dd2e5] | 1093 | def _simul_fit_completed(self, result, page_id, batch_outputs, |
---|
| 1094 | batch_inputs=None, |
---|
| 1095 | pars=None, |
---|
[8aa5788] | 1096 | elapsed=None): |
---|
[c77d859] | 1097 | """ |
---|
[2dbffcc] | 1098 | Display result of the fit on related panel(s). |
---|
| 1099 | :param result: list of object generated when fit ends |
---|
| 1100 | :param pars: list of names of parameters fitted |
---|
| 1101 | :param page_id: list of page ids which called fit function |
---|
| 1102 | :param elapsed: time spent at the fitting level |
---|
[c77d859] | 1103 | """ |
---|
[922497f] | 1104 | result = result[0] |
---|
[dc613d6] | 1105 | self.fit_thread_list = {} |
---|
[66ff250] | 1106 | if page_id is None: |
---|
| 1107 | page_id = [] |
---|
[ca7a626] | 1108 | ## fit more than 1 model at the same time |
---|
[f6aee42] | 1109 | self._mac_sleep(0.2) |
---|
[ca7a626] | 1110 | try: |
---|
[c69b6d5] | 1111 | msg = "" |
---|
[66ff250] | 1112 | if result == None: |
---|
[12cd4ec] | 1113 | self._update_fit_button(page_id) |
---|
[2296316] | 1114 | msg= "Complex Fitting did not converge!!!" |
---|
[66ff250] | 1115 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1116 | type="stop")) |
---|
[ad6dd4c] | 1117 | return |
---|
[66ff250] | 1118 | if not numpy.isfinite(result.fitness) or \ |
---|
| 1119 | numpy.any(result.pvec == None) or not \ |
---|
| 1120 | numpy.all(numpy.isfinite(result.pvec)): |
---|
[12cd4ec] | 1121 | self._update_fit_button(page_id) |
---|
[6bbeacd4] | 1122 | msg= "Simultaneous Fitting did not converge!!!" |
---|
[ad6dd4c] | 1123 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 1124 | return |
---|
[c69b6d5] | 1125 | |
---|
[66ff250] | 1126 | for uid in page_id: |
---|
[2dbffcc] | 1127 | fpdict = self.page_finder[uid] |
---|
[c832374d] | 1128 | for value in fpdict.itervalues(): |
---|
[2dbffcc] | 1129 | model = value.get_model() |
---|
| 1130 | data = value.get_fit_data() |
---|
| 1131 | small_param_name = [] |
---|
| 1132 | small_out = [] |
---|
| 1133 | small_cov = [] |
---|
| 1134 | #Separate result in to data corresponding to each page |
---|
| 1135 | for p in result.parameters: |
---|
| 1136 | model_name, param_name = self.split_string(p.name) |
---|
| 1137 | if model.name == model_name: |
---|
| 1138 | p_name= model.name+"."+param_name |
---|
| 1139 | if p.name == p_name: |
---|
| 1140 | if p.value != None and numpy.isfinite(p.value): |
---|
| 1141 | small_out.append(p.value) |
---|
| 1142 | small_param_name.append(param_name) |
---|
| 1143 | small_cov.append(p.stderr) |
---|
[66ff250] | 1144 | # Display result on each page |
---|
| 1145 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
[59afa71] | 1146 | wx.CallAfter(cpage.onsetValues, |
---|
| 1147 | result.fitness, |
---|
[66ff250] | 1148 | small_param_name, |
---|
| 1149 | small_out,small_cov) |
---|
[59afa71] | 1150 | cpage._on_fit_complete() |
---|
[12cd4ec] | 1151 | msg = "Fit completed!" |
---|
| 1152 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[2296316] | 1153 | except Exception: |
---|
[c832374d] | 1154 | raise |
---|
[12cd4ec] | 1155 | self._update_fit_button(page_id) |
---|
[2296316] | 1156 | msg = "Complex Fitting did not converge!!!" |
---|
| 1157 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 1158 | type="stop")) |
---|
| 1159 | return |
---|
[ca7a626] | 1160 | except: |
---|
[12cd4ec] | 1161 | self._update_fit_button(page_id) |
---|
[66ff250] | 1162 | msg = "Simultaneous Fit completed" |
---|
| 1163 | msg += " but Following error occurred:%s" % sys.exc_value |
---|
| 1164 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[12cd4ec] | 1165 | |
---|
| 1166 | def _update_fit_button(self, page_id): |
---|
| 1167 | """ |
---|
| 1168 | Update Fit button when fit stopped |
---|
| 1169 | |
---|
| 1170 | : parameter page_id: fitpage where the button is |
---|
| 1171 | """ |
---|
[37290c4] | 1172 | if page_id.__class__.__name__ != 'list': |
---|
| 1173 | page_id = [page_id] |
---|
[12cd4ec] | 1174 | for uid in page_id: |
---|
| 1175 | page = self.fit_panel.get_page_by_id(uid) |
---|
| 1176 | page._on_fit_complete() |
---|
| 1177 | |
---|
[c77d859] | 1178 | def _on_show_panel(self, event): |
---|
[5062bbf] | 1179 | """ |
---|
| 1180 | """ |
---|
| 1181 | pass |
---|
| 1182 | |
---|
[c77d859] | 1183 | def _onset_engine_park(self,event): |
---|
| 1184 | """ |
---|
[5062bbf] | 1185 | set engine to park |
---|
[c77d859] | 1186 | """ |
---|
| 1187 | self._on_change_engine('park') |
---|
| 1188 | |
---|
| 1189 | def _onset_engine_scipy(self,event): |
---|
| 1190 | """ |
---|
[5062bbf] | 1191 | set engine to scipy |
---|
[c77d859] | 1192 | """ |
---|
| 1193 | self._on_change_engine('scipy') |
---|
| 1194 | |
---|
| 1195 | def _on_slicer_event(self, event): |
---|
| 1196 | """ |
---|
[5062bbf] | 1197 | Receive a panel as event and send it to guiframe |
---|
| 1198 | |
---|
| 1199 | :param event: event containing a panel |
---|
| 1200 | |
---|
[c77d859] | 1201 | """ |
---|
[5062bbf] | 1202 | if event.panel is not None: |
---|
[c77d859] | 1203 | new_panel = event.panel |
---|
[c3435b7f] | 1204 | self.slicer_panels.append(event.panel) |
---|
[c77d859] | 1205 | # Set group ID if available |
---|
| 1206 | event_id = self.parent.popup_panel(new_panel) |
---|
| 1207 | new_panel.uid = event_id |
---|
| 1208 | self.mypanels.append(new_panel) |
---|
[5062bbf] | 1209 | |
---|
[848a2ef] | 1210 | def _onclearslicer(self, event): |
---|
| 1211 | """ |
---|
[5062bbf] | 1212 | Clear the boxslicer when close the panel associate with this slicer |
---|
[848a2ef] | 1213 | """ |
---|
[df4c3ad] | 1214 | name =event.GetPane().caption |
---|
[c3435b7f] | 1215 | |
---|
| 1216 | for panel in self.slicer_panels: |
---|
| 1217 | if panel.window_caption==name: |
---|
| 1218 | |
---|
[df4c3ad] | 1219 | for item in self.parent.panels: |
---|
[2dbffcc] | 1220 | if hasattr(self.parent.panels[item], "uid"): |
---|
[c3435b7f] | 1221 | if self.parent.panels[item].uid ==panel.base.uid: |
---|
[df4c3ad] | 1222 | self.parent.panels[item].onClearSlicer(event) |
---|
| 1223 | self.parent._mgr.Update() |
---|
| 1224 | break |
---|
[c3435b7f] | 1225 | break |
---|
[5062bbf] | 1226 | |
---|
[707436d] | 1227 | def _return_engine_type(self): |
---|
| 1228 | """ |
---|
[5062bbf] | 1229 | return the current type of engine |
---|
[707436d] | 1230 | """ |
---|
| 1231 | return self._fit_engine |
---|
| 1232 | |
---|
| 1233 | |
---|
[c77d859] | 1234 | def _on_change_engine(self, engine='park'): |
---|
| 1235 | """ |
---|
[5062bbf] | 1236 | Allow to select the type of engine to perform fit |
---|
| 1237 | |
---|
| 1238 | :param engine: the key work of the engine |
---|
| 1239 | |
---|
[c77d859] | 1240 | """ |
---|
[77e23a2] | 1241 | ## saving fit engine name |
---|
[c77d859] | 1242 | self._fit_engine = engine |
---|
[707436d] | 1243 | ## change menu item state |
---|
[2dbffcc] | 1244 | if engine == "park": |
---|
[0b6ae5f] | 1245 | self.menu1.FindItemById(self.park_id).Check(True) |
---|
| 1246 | self.menu1.FindItemById(self.scipy_id).Check(False) |
---|
[707436d] | 1247 | else: |
---|
[0b6ae5f] | 1248 | self.menu1.FindItemById(self.park_id).Check(False) |
---|
| 1249 | self.menu1.FindItemById(self.scipy_id).Check(True) |
---|
[77e23a2] | 1250 | ## post a message to status bar |
---|
[a0da535] | 1251 | msg = "Engine set to: %s" % self._fit_engine |
---|
| 1252 | wx.PostEvent(self.parent, |
---|
| 1253 | StatusEvent(status=msg)) |
---|
[66ff250] | 1254 | ## send the current engine type to fitpanel |
---|
| 1255 | self.fit_panel._on_engine_change(name=self._fit_engine) |
---|
[dc613d6] | 1256 | |
---|
[77e23a2] | 1257 | |
---|
[c77d859] | 1258 | def _on_model_panel(self, evt): |
---|
| 1259 | """ |
---|
[5062bbf] | 1260 | react to model selection on any combo box or model menu.plot the model |
---|
| 1261 | |
---|
| 1262 | :param evt: wx.combobox event |
---|
| 1263 | |
---|
[c77d859] | 1264 | """ |
---|
| 1265 | model = evt.model |
---|
[66ff250] | 1266 | uid = evt.uid |
---|
[6bbeacd4] | 1267 | qmin = evt.qmin |
---|
| 1268 | qmax = evt.qmax |
---|
| 1269 | smearer = evt.smearer |
---|
[2dbffcc] | 1270 | caption = evt.caption |
---|
| 1271 | enable_smearer = evt.enable_smearer |
---|
[9237df4] | 1272 | if model == None: |
---|
[c77d859] | 1273 | return |
---|
[2dbffcc] | 1274 | if uid not in self.page_finder.keys(): |
---|
| 1275 | return |
---|
[6bbeacd4] | 1276 | # save the name containing the data name with the appropriate model |
---|
[66ff250] | 1277 | self.page_finder[uid].set_model(model) |
---|
[2dbffcc] | 1278 | self.page_finder[uid].enable_smearing(enable_smearer) |
---|
[66ff250] | 1279 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax) |
---|
[2dbffcc] | 1280 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[33dd2e5] | 1281 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 1282 | self.sim_page.draw_page() |
---|
[5062bbf] | 1283 | |
---|
[2dbffcc] | 1284 | def _update1D(self, x, output): |
---|
[bb18ef1] | 1285 | """ |
---|
[5062bbf] | 1286 | Update the output of plotting model 1D |
---|
[bb18ef1] | 1287 | """ |
---|
[58e0c83] | 1288 | msg = "Plot updating ... " |
---|
| 1289 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="update")) |
---|
[e575db9] | 1290 | |
---|
[ad3fa1e] | 1291 | def _complete1D(self, x, y, page_id, elapsed, index, model, |
---|
| 1292 | toggle_mode_on=False, state=None, |
---|
[2296316] | 1293 | data=None, update_chisqr=True): |
---|
[c77d859] | 1294 | """ |
---|
[5062bbf] | 1295 | Complete plotting 1D data |
---|
[c77d859] | 1296 | """ |
---|
| 1297 | try: |
---|
[6bbeacd4] | 1298 | new_plot = Data1D(x=x, y=y) |
---|
[dce84c0] | 1299 | new_plot.is_data = False |
---|
[6bbeacd4] | 1300 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[2dbffcc] | 1301 | _yaxis, _yunit = data.get_yaxis() |
---|
| 1302 | _xaxis, _xunit = data.get_xaxis() |
---|
| 1303 | new_plot.title = data.name |
---|
| 1304 | new_plot.group_id = data.group_id |
---|
| 1305 | new_plot.id = str(page_id) + "model" |
---|
| 1306 | if new_plot.id in self.color_dict: |
---|
| 1307 | new_plot.custom_color = self.color_dict[new_plot.id] |
---|
[6bbeacd4] | 1308 | #find if this theory was already plotted and replace that plot given |
---|
| 1309 | #the same id |
---|
[2dbffcc] | 1310 | theory_data = self.page_finder[page_id].get_theory_data(fid=data.id) |
---|
| 1311 | new_plot.name = model.name + " ["+ str(model.__class__.__name__)+"]" |
---|
[6bbeacd4] | 1312 | new_plot.xaxis(_xaxis, _xunit) |
---|
| 1313 | new_plot.yaxis(_yaxis, _yunit) |
---|
[2e08a9f] | 1314 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
| 1315 | fid=data.id) |
---|
[2dbffcc] | 1316 | self.parent.update_theory(data_id=data.id, theory=new_plot, |
---|
[bdd3739] | 1317 | state=state) |
---|
[66ff250] | 1318 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1319 | title = new_plot.title |
---|
| 1320 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[2dbffcc] | 1321 | title=str(title))) |
---|
| 1322 | caption = current_pg.window_caption |
---|
| 1323 | self.page_finder[page_id].set_fit_tab_caption(caption=caption) |
---|
| 1324 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
| 1325 | fid=data.id) |
---|
| 1326 | if toggle_mode_on: |
---|
| 1327 | wx.PostEvent(self.parent, |
---|
| 1328 | NewPlotEvent(group_id=str(page_id) + " Model2D", |
---|
| 1329 | action="Hide")) |
---|
[2296316] | 1330 | else: |
---|
[2dbffcc] | 1331 | if update_chisqr: |
---|
| 1332 | wx.PostEvent(current_pg, |
---|
[2e08a9f] | 1333 | Chi2UpdateEvent(output=self._cal_chisqr( |
---|
| 1334 | data=data, |
---|
[2dbffcc] | 1335 | page_id=page_id, |
---|
| 1336 | index=index))) |
---|
| 1337 | else: |
---|
| 1338 | self._plot_residuals(page_id, data, index) |
---|
[cfbd06a] | 1339 | |
---|
[d522635] | 1340 | msg = "Computation completed!" |
---|
[a0da535] | 1341 | wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop" )) |
---|
[c77d859] | 1342 | except: |
---|
[6bbeacd4] | 1343 | raise |
---|
| 1344 | #msg = " Error occurred when drawing %s Model 1D: " % new_plot.name |
---|
| 1345 | #msg += " %s" % sys.exc_value |
---|
| 1346 | #wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[5062bbf] | 1347 | |
---|
[c77d859] | 1348 | def _update2D(self, output,time=None): |
---|
| 1349 | """ |
---|
[5062bbf] | 1350 | Update the output of plotting model |
---|
[c77d859] | 1351 | """ |
---|
[58e0c83] | 1352 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
[a0da535] | 1353 | #updating ... ", type="update")) |
---|
[a2cb1f9] | 1354 | #self.ready_fit() |
---|
[58e0c83] | 1355 | |
---|
[66ff250] | 1356 | def _complete2D(self, image, data, model, page_id, elapsed, index, qmin, |
---|
[ad3fa1e] | 1357 | qmax, toggle_mode_on=False, state=None, |
---|
[2296316] | 1358 | update_chisqr=True): |
---|
[c77d859] | 1359 | """ |
---|
[5062bbf] | 1360 | Complete get the result of modelthread and create model 2D |
---|
| 1361 | that can be plot. |
---|
[c77d859] | 1362 | """ |
---|
[2dbffcc] | 1363 | new_plot= Data2D(image=image, err_image=data.err_data) |
---|
[fa65e99] | 1364 | new_plot.name = model.name |
---|
[818589a] | 1365 | new_plot.title = "Analytical model 2D " |
---|
[2dbffcc] | 1366 | new_plot.id = str(page_id) + "model" |
---|
[f3242cf] | 1367 | new_plot.group_id = str(page_id) + " Model2D" |
---|
[2dbffcc] | 1368 | new_plot.detector = data.detector |
---|
| 1369 | new_plot.source = data.source |
---|
| 1370 | new_plot.is_data = False |
---|
| 1371 | new_plot.qx_data = data.qx_data |
---|
| 1372 | new_plot.qy_data = data.qy_data |
---|
| 1373 | new_plot.q_data = data.q_data |
---|
| 1374 | new_plot.mask = data.mask |
---|
| 1375 | ## plot boundaries |
---|
| 1376 | new_plot.ymin = data.ymin |
---|
| 1377 | new_plot.ymax = data.ymax |
---|
| 1378 | new_plot.xmin = data.xmin |
---|
| 1379 | new_plot.xmax = data.xmax |
---|
| 1380 | title = data.title |
---|
| 1381 | if len(title) > 1: |
---|
| 1382 | new_plot.title = "Model2D for " + data.name |
---|
[dce84c0] | 1383 | new_plot.is_data = False |
---|
[53cf669] | 1384 | new_plot.name = model.name + " [" + \ |
---|
| 1385 | str(model.__class__.__name__) + "-2D]" |
---|
[fa65e99] | 1386 | theory_data = deepcopy(new_plot) |
---|
[5ef55d2] | 1387 | theory_data.name = "Unknown" |
---|
[6bbeacd4] | 1388 | |
---|
[2dbffcc] | 1389 | self.page_finder[page_id].set_theory_data(data=theory_data, fid=data.id) |
---|
| 1390 | self.parent.update_theory(data_id=data.id, |
---|
[05319e2] | 1391 | theory=new_plot, |
---|
[e88ebfd] | 1392 | state=state) |
---|
[66ff250] | 1393 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1394 | title = new_plot.title |
---|
[fa65e99] | 1395 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[ae4ade7] | 1396 | title=title)) |
---|
[2dbffcc] | 1397 | self.page_finder[page_id].set_theory_data(data=new_plot, fid=data.id) |
---|
| 1398 | if toggle_mode_on: |
---|
| 1399 | wx.PostEvent(self.parent, |
---|
| 1400 | NewPlotEvent(group_id=str(page_id) + " Model1D", |
---|
| 1401 | action="Hide")) |
---|
[2296316] | 1402 | else: |
---|
[2dbffcc] | 1403 | # Chisqr in fitpage |
---|
| 1404 | if update_chisqr: |
---|
| 1405 | wx.PostEvent(current_pg, |
---|
| 1406 | Chi2UpdateEvent(output=self._cal_chisqr(data=data, |
---|
| 1407 | page_id=page_id, |
---|
| 1408 | index=index))) |
---|
| 1409 | else: |
---|
| 1410 | self._plot_residuals(page_id, data, index) |
---|
[d522635] | 1411 | msg = "Computation completed!" |
---|
[a0da535] | 1412 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[6bbeacd4] | 1413 | |
---|
[2dbffcc] | 1414 | def _draw_model2D(self, model, page_id, qmin, |
---|
| 1415 | qmax, |
---|
| 1416 | data=None, smearer=None, |
---|
[a0da535] | 1417 | description=None, enable2D=False, |
---|
[5ef55d2] | 1418 | state=None, |
---|
[fa65e99] | 1419 | toggle_mode_on=False, |
---|
[2dbffcc] | 1420 | update_chisqr=True): |
---|
[f93dfcb] | 1421 | """ |
---|
[5062bbf] | 1422 | draw model in 2D |
---|
| 1423 | |
---|
| 1424 | :param model: instance of the model to draw |
---|
| 1425 | :param description: the description of the model |
---|
| 1426 | :param enable2D: when True allows to draw model 2D |
---|
| 1427 | :param qmin: the minimum value to draw model 2D |
---|
| 1428 | :param qmax: the maximum value to draw model 2D |
---|
| 1429 | :param qstep: the number of division of Qx and Qy of the model to draw |
---|
[f93dfcb] | 1430 | |
---|
| 1431 | """ |
---|
[c77d859] | 1432 | if not enable2D: |
---|
[2dbffcc] | 1433 | return None |
---|
[c77d859] | 1434 | try: |
---|
| 1435 | from model_thread import Calc2D |
---|
| 1436 | ## If a thread is already started, stop it |
---|
[a0da535] | 1437 | if (self.calc_2D is not None) and self.calc_2D.isrunning(): |
---|
[c77d859] | 1438 | self.calc_2D.stop() |
---|
[2dbffcc] | 1439 | self.calc_2D = Calc2D(model=model, |
---|
[a0da535] | 1440 | data=data, |
---|
[66ff250] | 1441 | page_id=page_id, |
---|
[a0da535] | 1442 | smearer=smearer, |
---|
| 1443 | qmin=qmin, |
---|
| 1444 | qmax=qmax, |
---|
[fa65e99] | 1445 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 1446 | state=state, |
---|
[2296316] | 1447 | completefn=self._complete2D, |
---|
| 1448 | update_chisqr=update_chisqr) |
---|
[c77d859] | 1449 | self.calc_2D.queue() |
---|
[f72333f] | 1450 | |
---|
[c77d859] | 1451 | except: |
---|
[6bbeacd4] | 1452 | raise |
---|
| 1453 | #msg = " Error occurred when drawing %s Model 2D: " % model.name |
---|
| 1454 | #msg += " %s" % sys.exc_value |
---|
| 1455 | #wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[f72333f] | 1456 | |
---|
[2dbffcc] | 1457 | def _draw_model1D(self, model, page_id, data, |
---|
| 1458 | qmin, qmax, smearer=None, |
---|
[5ef55d2] | 1459 | state=None, |
---|
[2dbffcc] | 1460 | toggle_mode_on=False, update_chisqr=True, |
---|
[2296316] | 1461 | enable1D=True): |
---|
[c77d859] | 1462 | """ |
---|
[5062bbf] | 1463 | Draw model 1D from loaded data1D |
---|
| 1464 | |
---|
| 1465 | :param data: loaded data |
---|
| 1466 | :param model: the model to plot |
---|
| 1467 | |
---|
[c77d859] | 1468 | """ |
---|
| 1469 | if not enable1D: |
---|
[f72333f] | 1470 | return |
---|
[c77d859] | 1471 | try: |
---|
| 1472 | from model_thread import Calc1D |
---|
| 1473 | ## If a thread is already started, stop it |
---|
[a0da535] | 1474 | if (self.calc_1D is not None) and self.calc_1D.isrunning(): |
---|
[c77d859] | 1475 | self.calc_1D.stop() |
---|
[2dbffcc] | 1476 | self.calc_1D = Calc1D(data=data, |
---|
[6bbeacd4] | 1477 | model=model, |
---|
[66ff250] | 1478 | page_id=page_id, |
---|
[a0da535] | 1479 | qmin=qmin, |
---|
| 1480 | qmax=qmax, |
---|
| 1481 | smearer=smearer, |
---|
[5ef55d2] | 1482 | state=state, |
---|
[fa65e99] | 1483 | toggle_mode_on=toggle_mode_on, |
---|
[2296316] | 1484 | completefn=self._complete1D, |
---|
| 1485 | #updatefn = self._update1D, |
---|
| 1486 | update_chisqr=update_chisqr) |
---|
[c77d859] | 1487 | self.calc_1D.queue() |
---|
| 1488 | except: |
---|
[a0da535] | 1489 | msg = " Error occurred when drawing %s Model 1D: " % model.name |
---|
| 1490 | msg += " %s" % sys.exc_value |
---|
| 1491 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[2dbffcc] | 1492 | |
---|
| 1493 | |
---|
| 1494 | |
---|
| 1495 | def _cal_chisqr(self, page_id, data, index=None): |
---|
[f72333f] | 1496 | """ |
---|
[5062bbf] | 1497 | Get handy Chisqr using the output from draw1D and 2D, |
---|
| 1498 | instead of calling expansive CalcChisqr in guithread |
---|
[f72333f] | 1499 | """ |
---|
[ca2bdae] | 1500 | data_copy = deepcopy(data) |
---|
[55bb249c] | 1501 | |
---|
[f72333f] | 1502 | # default chisqr |
---|
| 1503 | chisqr = None |
---|
[2dbffcc] | 1504 | #to compute chisq make sure data has valid data |
---|
[f72333f] | 1505 | # return None if data == None |
---|
[ca2bdae] | 1506 | if not check_data_validity(data_copy): |
---|
[2dbffcc] | 1507 | return chisqr |
---|
[f151ddf] | 1508 | |
---|
[f72333f] | 1509 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 1510 | if data_copy.__class__.__name__ == "Data2D": |
---|
[a0da535] | 1511 | if index == None: |
---|
[ca2bdae] | 1512 | index = numpy.ones(len(data_copy.data),ntype=bool) |
---|
[55bb249c] | 1513 | if self.weight != None: |
---|
[ca2bdae] | 1514 | data_copy.err_data = self.weight |
---|
[a0da535] | 1515 | # get rid of zero error points |
---|
[ca2bdae] | 1516 | index = index & (data_copy.err_data != 0) |
---|
| 1517 | index = index & (numpy.isfinite(data_copy.data)) |
---|
| 1518 | fn = data_copy.data[index] |
---|
| 1519 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[6bbeacd4] | 1520 | gn = theory_data.data[index] |
---|
[ca2bdae] | 1521 | en = data_copy.err_data[index] |
---|
[f72333f] | 1522 | else: |
---|
| 1523 | # 1 d theory from model_thread is only in the range of index |
---|
[2296316] | 1524 | if index == None: |
---|
[ca2bdae] | 1525 | index = numpy.ones(len(data_copy.y), ntype=bool) |
---|
[55bb249c] | 1526 | if self.weight != None: |
---|
[ca2bdae] | 1527 | data_copy.dy = self.weight |
---|
| 1528 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 1529 | dy = numpy.ones(len(data_copy.y)) |
---|
[f151ddf] | 1530 | else: |
---|
[a0da535] | 1531 | ## Set consitently w/AbstractFitengine: |
---|
| 1532 | # But this should be corrected later. |
---|
[ca2bdae] | 1533 | dy = deepcopy(data_copy.dy) |
---|
[a2da783] | 1534 | dy[dy==0] = numpy.ones(len(dy[dy==0])) |
---|
[ca2bdae] | 1535 | fn = data_copy.y[index] |
---|
| 1536 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[6bbeacd4] | 1537 | gn = theory_data.y |
---|
[4d5fa8c] | 1538 | en = dy[index] |
---|
[f72333f] | 1539 | # residual |
---|
[2296316] | 1540 | res = (fn - gn) / en |
---|
| 1541 | residuals = res[numpy.isfinite(res)] |
---|
[f72333f] | 1542 | # get chisqr only w/finite |
---|
[2296316] | 1543 | chisqr = numpy.average(residuals * residuals) |
---|
[ca2bdae] | 1544 | self._plot_residuals(page_id, data_copy, index) |
---|
| 1545 | |
---|
[f72333f] | 1546 | return chisqr |
---|
| 1547 | |
---|
[2296316] | 1548 | def _plot_residuals(self, page_id, data=None, index=None): |
---|
| 1549 | """ |
---|
| 1550 | Plot the residuals |
---|
| 1551 | |
---|
| 1552 | :param data: data |
---|
| 1553 | :param index: index array (bool) |
---|
| 1554 | : Note: this is different from the residuals in cal_chisqr() |
---|
| 1555 | """ |
---|
[ca2bdae] | 1556 | data_copy = deepcopy(data) |
---|
[2296316] | 1557 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 1558 | if data_copy.__class__.__name__ == "Data2D": |
---|
[2296316] | 1559 | # build residuals |
---|
| 1560 | residuals = Data2D() |
---|
| 1561 | #residuals.copy_from_datainfo(data) |
---|
| 1562 | # Not for trunk the line below, instead use the line above |
---|
[ca2bdae] | 1563 | data_copy.clone_without_data(len(data_copy.data), residuals) |
---|
[2296316] | 1564 | residuals.data = None |
---|
[ca2bdae] | 1565 | fn = data_copy.data#[index] |
---|
| 1566 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[2296316] | 1567 | gn = theory_data.data#[index] |
---|
[ca2bdae] | 1568 | en = self.weight#data_copy.err_data#[index] |
---|
[2296316] | 1569 | residuals.data = (fn - gn) / en |
---|
[ca2bdae] | 1570 | residuals.qx_data = data_copy.qx_data#[index] |
---|
| 1571 | residuals.qy_data = data_copy.qy_data #[index] |
---|
| 1572 | residuals.q_data = data_copy.q_data#[index] |
---|
[2296316] | 1573 | residuals.err_data = numpy.ones(len(residuals.data))#[index] |
---|
| 1574 | residuals.xmin = min(residuals.qx_data) |
---|
| 1575 | residuals.xmax = max(residuals.qx_data) |
---|
| 1576 | residuals.ymin = min(residuals.qy_data) |
---|
| 1577 | residuals.ymax = max(residuals.qy_data) |
---|
[ca2bdae] | 1578 | residuals.q_data = data_copy.q_data#[index] |
---|
| 1579 | residuals.mask = data_copy.mask |
---|
[2296316] | 1580 | residuals.scale = 'linear' |
---|
| 1581 | # check the lengths |
---|
| 1582 | if len(residuals.data) != len(residuals.q_data): |
---|
| 1583 | return |
---|
| 1584 | else: |
---|
| 1585 | # 1 d theory from model_thread is only in the range of index |
---|
[ca2bdae] | 1586 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 1587 | dy = numpy.ones(len(data_copy.y)) |
---|
[2296316] | 1588 | else: |
---|
| 1589 | ## Set consitently w/AbstractFitengine: |
---|
| 1590 | ## But this should be corrected later. |
---|
[ca2bdae] | 1591 | dy = self.weight#deepcopy(data_copy.dy) |
---|
[2296316] | 1592 | dy[dy==0] = 1 |
---|
[ca2bdae] | 1593 | fn = data_copy.y[index] |
---|
| 1594 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[2296316] | 1595 | gn = theory_data.y |
---|
| 1596 | en = dy[index] |
---|
| 1597 | # build residuals |
---|
| 1598 | residuals = Data1D() |
---|
| 1599 | residuals.y = (fn - gn) / en |
---|
[ca2bdae] | 1600 | residuals.x = data_copy.x[index] |
---|
[2296316] | 1601 | residuals.dy = numpy.ones(len(residuals.y)) |
---|
| 1602 | residuals.dx = None |
---|
| 1603 | residuals.dxl = None |
---|
| 1604 | residuals.dxw = None |
---|
| 1605 | residuals.ytransform = 'y' |
---|
| 1606 | # For latter scale changes |
---|
| 1607 | residuals.xaxis('\\rm{Q} ', 'A^{-1}') |
---|
| 1608 | residuals.yaxis('\\rm{Residuals} ', 'normalized') |
---|
| 1609 | new_plot = residuals |
---|
| 1610 | new_plot.name = "Residuals for " + str(data.name) |
---|
| 1611 | ## allow to highlight data when plotted |
---|
| 1612 | new_plot.interactive = True |
---|
| 1613 | ## when 2 data have the same id override the 1 st plotted |
---|
[a2da783] | 1614 | new_plot.id = "res" + str(data_copy.id)#name + " residuals" |
---|
[2296316] | 1615 | ##group_id specify on which panel to plot this data |
---|
| 1616 | new_plot.group_id = new_plot.id |
---|
| 1617 | #new_plot.is_data = True |
---|
| 1618 | ##post data to plot |
---|
| 1619 | title = new_plot.name |
---|
| 1620 | # plot data |
---|
[ca2bdae] | 1621 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) |
---|
| 1622 | #reset weight |
---|
| 1623 | self.weight = None |
---|
[5062bbf] | 1624 | #def profile(fn, *args, **kw): |
---|
| 1625 | # import cProfile, pstats, os |
---|
| 1626 | # global call_result |
---|
| 1627 | # def call(): |
---|
| 1628 | # global call_result |
---|
| 1629 | # call_result = fn(*args, **kw) |
---|
| 1630 | # cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
| 1631 | # stats = pstats.Stats('profile.out') |
---|
| 1632 | # #stats.sort_stats('time') |
---|
| 1633 | # stats.sort_stats('calls') |
---|
| 1634 | # stats.print_stats() |
---|
| 1635 | # os.unlink('profile.out') |
---|
| 1636 | # return call_result |
---|
[d89f09b] | 1637 | if __name__ == "__main__": |
---|
| 1638 | i = Plugin() |
---|
| 1639 | |
---|
| 1640 | |
---|
| 1641 | |
---|
| 1642 | |
---|