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