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