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