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