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