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