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