[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, |
---|
[3fafe15] | 596 | action='remove')) |
---|
[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() |
---|
[a5701e6] | 647 | |
---|
[6bbeacd4] | 648 | if issubclass(data.__class__, Data2D): |
---|
| 649 | data.group_id.append(wx.NewId()) |
---|
[a5701e6] | 650 | if theory_data is not None: |
---|
| 651 | group_id = str(page.id) + " Model1D" |
---|
| 652 | if group_id in theory_data.group_id: |
---|
| 653 | theory_data.group_id.remove(group_id) |
---|
| 654 | wx.PostEvent(self.parent, |
---|
| 655 | NewPlotEvent(group_id=group_id, |
---|
| 656 | action="delete")) |
---|
[6bbeacd4] | 657 | else: |
---|
| 658 | if theory_data is not None: |
---|
[a5701e6] | 659 | group_id = str(page.id) + " Model2D" |
---|
| 660 | if group_id in theory_data.group_id: |
---|
| 661 | theory_data.group_id.remove(group_id) |
---|
[c399004] | 662 | data_group_id = theory_data.group_id[len(theory_data.group_id)-1] |
---|
| 663 | if data_group_id not in data.group_id: |
---|
| 664 | data.group_id.append(data_group_id) |
---|
[a5701e6] | 665 | wx.PostEvent(self.parent, |
---|
| 666 | NewPlotEvent(group_id=group_id, |
---|
| 667 | action="delete")) |
---|
| 668 | |
---|
[3fafe15] | 669 | self.parent.update_data(prev_data=theory_data, new_data=data) |
---|
[6bbeacd4] | 670 | self.store_data(id=page.id, data=data, caption=page.window_name) |
---|
| 671 | if self.sim_page is not None: |
---|
| 672 | self.sim_page.draw_page() |
---|
| 673 | |
---|
[848a2ef] | 674 | def _onEVT_SLICER_PANEL(self, event): |
---|
| 675 | """ |
---|
[5062bbf] | 676 | receive and event telling to update a panel with a name starting with |
---|
| 677 | event.panel_name. this method update slicer panel for a given interactor. |
---|
| 678 | |
---|
| 679 | :param event: contains type of slicer , paramaters for updating the panel |
---|
[848a2ef] | 680 | and panel_name to find the slicer 's panel concerned. |
---|
| 681 | """ |
---|
| 682 | for item in self.parent.panels: |
---|
[df4c3ad] | 683 | if self.parent.panels[item].window_caption.startswith(event.panel_name): |
---|
[848a2ef] | 684 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
[df4c3ad] | 685 | |
---|
| 686 | self.parent._mgr.Update() |
---|
[b324aa9] | 687 | |
---|
[848a2ef] | 688 | def _closed_fitpage(self, event): |
---|
| 689 | """ |
---|
[5062bbf] | 690 | request fitpanel to close a given page when its unique data is removed |
---|
| 691 | from the plot. close fitpage only when the a loaded data is removed |
---|
[848a2ef] | 692 | """ |
---|
[784e2fa] | 693 | if event is None or event.data is None: |
---|
| 694 | return |
---|
| 695 | |
---|
| 696 | if hasattr(event.data,"is_data"): |
---|
[a0da535] | 697 | if not event.data.is_data or \ |
---|
| 698 | event.data.__class__.__name__=="Data1D": |
---|
[784e2fa] | 699 | self.fit_panel.close_page_with_data(event.data) |
---|
[848a2ef] | 700 | |
---|
[cfc0913] | 701 | def _add_page_onmenu(self, name,fitproblem=None): |
---|
[6f023e8] | 702 | """ |
---|
[5062bbf] | 703 | Add name of a closed page of fitpanel in a menu |
---|
[6f023e8] | 704 | """ |
---|
[dcf29d7] | 705 | list = self.menu1.GetMenuItems() |
---|
| 706 | for item in list: |
---|
| 707 | if name == item.GetItemLabel(): |
---|
[cfc0913] | 708 | self.closed_page_dict[name][1] = fitproblem |
---|
[dcf29d7] | 709 | |
---|
| 710 | if not name in self.closed_page_dict.keys(): |
---|
| 711 | # Post paramters |
---|
[77e23a2] | 712 | event_id = wx.NewId() |
---|
| 713 | self.menu1.Append(event_id, name, "Show %s fit panel" % name) |
---|
[cfc0913] | 714 | self.closed_page_dict[name]= [event_id, fitproblem] |
---|
[dcf29d7] | 715 | wx.EVT_MENU(self.parent,event_id, self._open_closed_page) |
---|
[ca7a626] | 716 | |
---|
[6f023e8] | 717 | def _open_closed_page(self, event): |
---|
| 718 | """ |
---|
[5062bbf] | 719 | reopen a closed page |
---|
[6f023e8] | 720 | """ |
---|
[cfc0913] | 721 | for name, value in self.closed_page_dict.iteritems(): |
---|
[dcf29d7] | 722 | if event.GetId() in value: |
---|
[cfc0913] | 723 | id,fitproblem = value |
---|
[b787e68c] | 724 | if name !="Model": |
---|
[cfc0913] | 725 | data= fitproblem.get_fit_data() |
---|
| 726 | page = self.fit_panel.add_fit_page(data= data,reset=True) |
---|
[0f5fe6b] | 727 | if fitproblem != None: |
---|
[6bbeacd4] | 728 | self.page_finder[id] = fitproblem |
---|
[7c845cb] | 729 | if self.sim_page != None: |
---|
| 730 | self.sim_page.draw_page() |
---|
| 731 | |
---|
[0f5fe6b] | 732 | else: |
---|
[b787e68c] | 733 | model = fitproblem |
---|
| 734 | self.fit_panel.add_model_page(model=model, topmenu=True, |
---|
| 735 | reset= True) |
---|
[0f5fe6b] | 736 | break |
---|
[5062bbf] | 737 | |
---|
[883e5f5] | 738 | def _reset_schedule_problem(self, id, value=0): |
---|
[6f023e8] | 739 | """ |
---|
[5062bbf] | 740 | unschedule or schedule all fitproblem to be fit |
---|
[6f023e8] | 741 | """ |
---|
[883e5f5] | 742 | for page_id in self.page_finder.keys(): |
---|
| 743 | self.page_finder[id].schedule_tofit(value) |
---|
[6f023e8] | 744 | |
---|
[67e258c] | 745 | def _fit_helper(self, pars, value, id, title="Single Fit " ): |
---|
[2140e68] | 746 | """ |
---|
[5062bbf] | 747 | helper for fitting |
---|
[2140e68] | 748 | """ |
---|
[ca7a626] | 749 | metadata = value.get_fit_data() |
---|
[24cab5d] | 750 | model = value.get_model() |
---|
| 751 | smearer = value.get_smearer() |
---|
[67e258c] | 752 | qmin, qmax = value.get_range() |
---|
| 753 | self.fit_id = id |
---|
[24cab5d] | 754 | #Create list of parameters for fitting used |
---|
[67e258c] | 755 | templist = [] |
---|
[464fce54] | 756 | |
---|
[24cab5d] | 757 | try: |
---|
[464fce54] | 758 | #Extra list of parameters and their constraints |
---|
[67e258c] | 759 | listOfConstraint = [] |
---|
[464fce54] | 760 | |
---|
[24cab5d] | 761 | param = value.get_model_param() |
---|
[67e258c] | 762 | if len(param) > 0: |
---|
[24cab5d] | 763 | for item in param: |
---|
| 764 | ## check if constraint |
---|
[67e258c] | 765 | if item[0] != None and item[1] != None: |
---|
[464fce54] | 766 | listOfConstraint.append((item[0],item[1])) |
---|
| 767 | |
---|
[24cab5d] | 768 | #Do the single fit |
---|
[464fce54] | 769 | self.fitter.set_model(model, self.fit_id, |
---|
[67e258c] | 770 | pars, constraints=listOfConstraint) |
---|
| 771 | |
---|
| 772 | self.fitter.set_data(data=metadata, id=self.fit_id, |
---|
| 773 | smearer=smearer, qmin=qmin, qmax=qmax) |
---|
[784e2fa] | 774 | |
---|
[67e258c] | 775 | self.fitter.select_problem_for_fit(id=self.fit_id, |
---|
| 776 | value=value.get_scheduled()) |
---|
[60d6c23] | 777 | value.clear_model_param() |
---|
[24cab5d] | 778 | except: |
---|
[6bbeacd4] | 779 | raise |
---|
| 780 | #msg = title + " error: %s" % sys.exc_value |
---|
| 781 | #wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[67e258c] | 782 | |
---|
[c77d859] | 783 | def _onSelect(self,event): |
---|
| 784 | """ |
---|
[5062bbf] | 785 | when Select data to fit a new page is created .Its reference is |
---|
| 786 | added to self.page_finder |
---|
[d89f09b] | 787 | """ |
---|
[c77d859] | 788 | self.panel = event.GetEventObject() |
---|
[67e258c] | 789 | Plugin.on_perspective(self, event=event) |
---|
[6ed82c7] | 790 | for plottable in self.panel.graph.plottables: |
---|
[2fff4db] | 791 | if plottable.__class__.__name__ in ["Data1D", "Theory1D"]: |
---|
[edcbd467] | 792 | if plottable.name == self.panel.graph.selected_plottable: |
---|
[6bbeacd4] | 793 | data = plottable |
---|
[edcbd467] | 794 | self.add_fit_page(data=data) |
---|
| 795 | return |
---|
[c77d859] | 796 | else: |
---|
[6bbeacd4] | 797 | data = plottable |
---|
[edcbd467] | 798 | self.add_fit_page(data=data) |
---|
[1c1436d] | 799 | |
---|
[6bbeacd4] | 800 | def _single_fit_completed(self,result, pars, elapsed=None): |
---|
[c77d859] | 801 | """ |
---|
[5062bbf] | 802 | Display fit result on one page of the notebook. |
---|
| 803 | |
---|
| 804 | :param result: result of fit |
---|
| 805 | :param pars: list of names of parameters fitted |
---|
| 806 | :param current_pg: the page where information will be displayed |
---|
| 807 | :param qmin: the minimum value of x to replot the model |
---|
| 808 | :param qmax: the maximum value of x to replot model |
---|
[c77d859] | 809 | |
---|
[7975f2b] | 810 | """ |
---|
[c77d859] | 811 | try: |
---|
[ad6dd4c] | 812 | if result ==None: |
---|
| 813 | msg= "Simple Fitting Stop !!!" |
---|
[f304194] | 814 | wx.PostEvent(self.parent, StatusEvent(status=msg,info="warning", |
---|
| 815 | type="stop")) |
---|
[ad6dd4c] | 816 | return |
---|
[67e258c] | 817 | if not numpy.isfinite(result.fitness) or \ |
---|
| 818 | numpy.any(result.pvec == None) or \ |
---|
| 819 | not numpy.all(numpy.isfinite(result.pvec)): |
---|
| 820 | msg = "Single Fitting did not converge!!!" |
---|
| 821 | wx.PostEvent(self.parent, |
---|
| 822 | StatusEvent(status=msg, type="stop")) |
---|
[d902cf0] | 823 | return |
---|
[6bbeacd4] | 824 | page_id = None |
---|
| 825 | for id, value in self.page_finder.iteritems(): |
---|
| 826 | if value.get_scheduled()==1: |
---|
[67e258c] | 827 | model = value.get_model() |
---|
[6bbeacd4] | 828 | page_id = id |
---|
[c77d859] | 829 | break |
---|
[6d91073] | 830 | param_name = [] |
---|
[c77d859] | 831 | i = 0 |
---|
| 832 | for name in pars: |
---|
[6d91073] | 833 | param_name.append(name) |
---|
[6bbeacd4] | 834 | |
---|
| 835 | cpage = self.fit_panel.get_page_by_id(page_id) |
---|
[67e258c] | 836 | cpage.onsetValues(result.fitness, |
---|
| 837 | param_name, result.pvec,result.stderr) |
---|
[9237df4] | 838 | |
---|
[c77d859] | 839 | except: |
---|
[67e258c] | 840 | msg = "Single Fit completed but Following" |
---|
| 841 | msg += " error occurred:%s" % sys.exc_value |
---|
[f304194] | 842 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 843 | type="stop")) |
---|
[25e3fc9] | 844 | return |
---|
[c77d859] | 845 | |
---|
[6bbeacd4] | 846 | def _simul_fit_completed(self, result, pars=None, elapsed=None): |
---|
[c77d859] | 847 | """ |
---|
[5062bbf] | 848 | Parameter estimation completed, |
---|
| 849 | display the results to the user |
---|
| 850 | |
---|
| 851 | :param alpha: estimated best alpha |
---|
| 852 | :param elapsed: computation time |
---|
| 853 | |
---|
[c77d859] | 854 | """ |
---|
[ca7a626] | 855 | ## fit more than 1 model at the same time |
---|
| 856 | try: |
---|
[c69b6d5] | 857 | msg = "" |
---|
[ad6dd4c] | 858 | if result ==None: |
---|
| 859 | msg= "Complex Fitting Stop !!!" |
---|
| 860 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 861 | return |
---|
[c69b6d5] | 862 | if not numpy.isfinite(result.fitness) or numpy.any(result.pvec ==None )or not numpy.all(numpy.isfinite(result.pvec) ): |
---|
[6bbeacd4] | 863 | msg= "Simultaneous Fitting did not converge!!!" |
---|
[ad6dd4c] | 864 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 865 | return |
---|
[c69b6d5] | 866 | |
---|
[6bbeacd4] | 867 | for id, value in self.page_finder.iteritems(): |
---|
| 868 | if value.get_scheduled() == 1: |
---|
[ca7a626] | 869 | model = value.get_model() |
---|
[edcbd467] | 870 | data = value.get_fit_data() |
---|
[6d91073] | 871 | small_param_name = [] |
---|
[ca7a626] | 872 | small_out = [] |
---|
| 873 | small_cov = [] |
---|
| 874 | i = 0 |
---|
| 875 | #Separate result in to data corresponding to each page |
---|
| 876 | for p in result.parameters: |
---|
| 877 | model_name,param_name = self.split_string(p.name) |
---|
| 878 | if model.name == model_name: |
---|
| 879 | p_name= model.name+"."+param_name |
---|
[7975f2b] | 880 | if p.name == p_name: |
---|
[edd166b] | 881 | if p.value != None and numpy.isfinite(p.value): |
---|
[7975f2b] | 882 | small_out.append(p.value ) |
---|
| 883 | small_param_name.append(param_name) |
---|
| 884 | small_cov.append(p.stderr) |
---|
| 885 | |
---|
[ca7a626] | 886 | # Display result on each page |
---|
[6bbeacd4] | 887 | cpage = self.fit_panel.get_page_by_id(id) |
---|
| 888 | cpage.onsetValues(result.fitness, |
---|
[a0da535] | 889 | small_param_name, |
---|
| 890 | small_out,small_cov) |
---|
[ca7a626] | 891 | except: |
---|
[6bbeacd4] | 892 | raise |
---|
| 893 | #msg= "Simultaneous Fit completed" |
---|
| 894 | #msg +=" but Following error occurred:%s"%sys.exc_value |
---|
| 895 | #wx.PostEvent(self.parent, StatusEvent(status=msg,type="stop")) |
---|
| 896 | #return |
---|
[f93dfcb] | 897 | |
---|
[c77d859] | 898 | def _on_show_panel(self, event): |
---|
[5062bbf] | 899 | """ |
---|
| 900 | """ |
---|
| 901 | pass |
---|
| 902 | |
---|
[c77d859] | 903 | def _onset_engine_park(self,event): |
---|
| 904 | """ |
---|
[5062bbf] | 905 | set engine to park |
---|
[c77d859] | 906 | """ |
---|
| 907 | self._on_change_engine('park') |
---|
| 908 | |
---|
| 909 | def _onset_engine_scipy(self,event): |
---|
| 910 | """ |
---|
[5062bbf] | 911 | set engine to scipy |
---|
[c77d859] | 912 | """ |
---|
| 913 | self._on_change_engine('scipy') |
---|
| 914 | |
---|
| 915 | def _on_slicer_event(self, event): |
---|
| 916 | """ |
---|
[5062bbf] | 917 | Receive a panel as event and send it to guiframe |
---|
| 918 | |
---|
| 919 | :param event: event containing a panel |
---|
| 920 | |
---|
[c77d859] | 921 | """ |
---|
[5062bbf] | 922 | if event.panel is not None: |
---|
[c77d859] | 923 | new_panel = event.panel |
---|
[c3435b7f] | 924 | self.slicer_panels.append(event.panel) |
---|
[c77d859] | 925 | # Set group ID if available |
---|
| 926 | event_id = self.parent.popup_panel(new_panel) |
---|
[3fef0a8] | 927 | #self.menu3.Append(event_id, new_panel.window_caption, |
---|
| 928 | # "Show %s plot panel" % new_panel.window_caption) |
---|
[67e258c] | 929 | # Set id to allow us to reference the panel later |
---|
[df4c3ad] | 930 | |
---|
[c77d859] | 931 | new_panel.uid = event_id |
---|
| 932 | self.mypanels.append(new_panel) |
---|
[5062bbf] | 933 | |
---|
[848a2ef] | 934 | def _onclearslicer(self, event): |
---|
| 935 | """ |
---|
[5062bbf] | 936 | Clear the boxslicer when close the panel associate with this slicer |
---|
[848a2ef] | 937 | """ |
---|
[df4c3ad] | 938 | name =event.GetPane().caption |
---|
[c3435b7f] | 939 | |
---|
| 940 | for panel in self.slicer_panels: |
---|
| 941 | if panel.window_caption==name: |
---|
| 942 | |
---|
[df4c3ad] | 943 | for item in self.parent.panels: |
---|
[c3435b7f] | 944 | if hasattr(self.parent.panels[item],"uid"): |
---|
| 945 | if self.parent.panels[item].uid ==panel.base.uid: |
---|
[df4c3ad] | 946 | self.parent.panels[item].onClearSlicer(event) |
---|
| 947 | self.parent._mgr.Update() |
---|
| 948 | break |
---|
[c3435b7f] | 949 | break |
---|
[5062bbf] | 950 | |
---|
[707436d] | 951 | def _return_engine_type(self): |
---|
| 952 | """ |
---|
[5062bbf] | 953 | return the current type of engine |
---|
[707436d] | 954 | """ |
---|
| 955 | return self._fit_engine |
---|
| 956 | |
---|
| 957 | |
---|
[c77d859] | 958 | def _on_change_engine(self, engine='park'): |
---|
| 959 | """ |
---|
[5062bbf] | 960 | Allow to select the type of engine to perform fit |
---|
| 961 | |
---|
| 962 | :param engine: the key work of the engine |
---|
| 963 | |
---|
[c77d859] | 964 | """ |
---|
[77e23a2] | 965 | ## saving fit engine name |
---|
[c77d859] | 966 | self._fit_engine = engine |
---|
[707436d] | 967 | ## change menu item state |
---|
| 968 | if engine=="park": |
---|
| 969 | self.menu1.FindItemByPosition(0).Check(False) |
---|
| 970 | self.menu1.FindItemByPosition(1).Check(True) |
---|
| 971 | else: |
---|
| 972 | self.menu1.FindItemByPosition(0).Check(True) |
---|
| 973 | self.menu1.FindItemByPosition(1).Check(False) |
---|
[77e23a2] | 974 | ## post a message to status bar |
---|
[a0da535] | 975 | msg = "Engine set to: %s" % self._fit_engine |
---|
| 976 | wx.PostEvent(self.parent, |
---|
| 977 | StatusEvent(status=msg)) |
---|
| 978 | ## Bind every open fit page with a newevent to |
---|
| 979 | #know the current fitting engine |
---|
| 980 | #import fitpage |
---|
| 981 | event = fitpage.FitterTypeEvent() |
---|
[77e23a2] | 982 | event.type = self._fit_engine |
---|
[6bbeacd4] | 983 | wx.PostEvent(self.fit_panel, event) |
---|
[77e23a2] | 984 | |
---|
[c77d859] | 985 | def _on_model_panel(self, evt): |
---|
| 986 | """ |
---|
[5062bbf] | 987 | react to model selection on any combo box or model menu.plot the model |
---|
| 988 | |
---|
| 989 | :param evt: wx.combobox event |
---|
| 990 | |
---|
[c77d859] | 991 | """ |
---|
| 992 | model = evt.model |
---|
[6bbeacd4] | 993 | id = evt.id |
---|
| 994 | qmin = evt.qmin |
---|
| 995 | qmax = evt.qmax |
---|
| 996 | smearer = evt.smearer |
---|
[9237df4] | 997 | if model == None: |
---|
[c77d859] | 998 | return |
---|
[6bbeacd4] | 999 | |
---|
| 1000 | if self.page_finder[id].get_model() is None: |
---|
| 1001 | model.name = "M" + str(self.index_model) |
---|
| 1002 | self.index_model += 1 |
---|
| 1003 | else: |
---|
| 1004 | model.name = self.page_finder[id].get_model().name |
---|
| 1005 | # save the name containing the data name with the appropriate model |
---|
| 1006 | self.page_finder[id].set_model(model) |
---|
| 1007 | self.page_finder[id].set_range(qmin=qmin, qmax=qmax) |
---|
| 1008 | if self.sim_page is not None: |
---|
| 1009 | self.sim_page.draw_page() |
---|
[5062bbf] | 1010 | |
---|
[c77d859] | 1011 | def _update1D(self,x, output): |
---|
[bb18ef1] | 1012 | """ |
---|
[5062bbf] | 1013 | Update the output of plotting model 1D |
---|
[bb18ef1] | 1014 | """ |
---|
[a0da535] | 1015 | msg = "Plot updating ... " |
---|
| 1016 | wx.PostEvent(self.parent, StatusEvent(status=msg,type="update")) |
---|
[c660493] | 1017 | self.ready_fit() |
---|
| 1018 | #self.calc_thread.ready(0.01) |
---|
[bb18ef1] | 1019 | |
---|
[6bbeacd4] | 1020 | def _fill_default_model2D(self, theory,id, qmax,qstep, qmin=None): |
---|
[ed2ea6a] | 1021 | """ |
---|
[5062bbf] | 1022 | fill Data2D with default value |
---|
| 1023 | |
---|
| 1024 | :param theory: Data2D to fill |
---|
| 1025 | |
---|
[ed2ea6a] | 1026 | """ |
---|
[d15c0202] | 1027 | from DataLoader.data_info import Detector, Source |
---|
[bb18ef1] | 1028 | |
---|
[d15c0202] | 1029 | detector = Detector() |
---|
[e575db9] | 1030 | theory.detector.append(detector) |
---|
[d15c0202] | 1031 | theory.source= Source() |
---|
[f93dfcb] | 1032 | |
---|
[e575db9] | 1033 | ## Default values |
---|
| 1034 | theory.detector[0].distance= 8000 # mm |
---|
| 1035 | theory.source.wavelength= 6 # A |
---|
| 1036 | theory.detector[0].pixel_size.x= 5 # mm |
---|
| 1037 | theory.detector[0].pixel_size.y= 5 # mm |
---|
| 1038 | |
---|
[ac2cc940] | 1039 | theory.detector[0].beam_center.x= qmax |
---|
| 1040 | theory.detector[0].beam_center.y= qmax |
---|
[5062bbf] | 1041 | |
---|
[f93dfcb] | 1042 | ## create x_bins and y_bins of the model 2D |
---|
[13e120a] | 1043 | pixel_width_x = theory.detector[0].pixel_size.x |
---|
| 1044 | pixel_width_y = theory.detector[0].pixel_size.y |
---|
| 1045 | center_x = theory.detector[0].beam_center.x/pixel_width_x |
---|
| 1046 | center_y = theory.detector[0].beam_center.y/pixel_width_y |
---|
[e575db9] | 1047 | |
---|
[a0da535] | 1048 | # theory default: assume the beam |
---|
| 1049 | #center is located at the center of sqr detector |
---|
[e575db9] | 1050 | xmax = qmax |
---|
| 1051 | xmin = -qmax |
---|
| 1052 | ymax = qmax |
---|
| 1053 | ymin = -qmax |
---|
| 1054 | |
---|
| 1055 | x= numpy.linspace(start= -1*qmax, |
---|
[a0da535] | 1056 | stop=qmax, |
---|
| 1057 | num=qstep, |
---|
| 1058 | endpoint=True) |
---|
| 1059 | y = numpy.linspace(start=-1*qmax, |
---|
[e575db9] | 1060 | stop= qmax, |
---|
| 1061 | num= qstep, |
---|
[a0da535] | 1062 | endpoint=True) |
---|
[e575db9] | 1063 | |
---|
| 1064 | ## use data info instead |
---|
| 1065 | new_x = numpy.tile(x, (len(y),1)) |
---|
| 1066 | new_y = numpy.tile(y, (len(x),1)) |
---|
| 1067 | new_y = new_y.swapaxes(0,1) |
---|
[d15c0202] | 1068 | |
---|
[e575db9] | 1069 | # all data reuire now in 1d array |
---|
| 1070 | qx_data = new_x.flatten() |
---|
| 1071 | qy_data = new_y.flatten() |
---|
[13e120a] | 1072 | |
---|
[e575db9] | 1073 | q_data = numpy.sqrt(qx_data*qx_data+qy_data*qy_data) |
---|
| 1074 | # set all True (standing for unmasked) as default |
---|
| 1075 | mask = numpy.ones(len(qx_data), dtype = bool) |
---|
| 1076 | |
---|
[a0da535] | 1077 | # calculate the range of qx and qy: this way, |
---|
| 1078 | # it is a little more independent |
---|
[e575db9] | 1079 | x_size = xmax- xmin |
---|
| 1080 | y_size = ymax -ymin |
---|
| 1081 | |
---|
| 1082 | # store x and y bin centers in q space |
---|
| 1083 | x_bins = x |
---|
| 1084 | y_bins = y |
---|
| 1085 | # bin size: x- & y-directions |
---|
| 1086 | xstep = x_size/len(x_bins-1) |
---|
| 1087 | ystep = y_size/len(y_bins-1) |
---|
| 1088 | |
---|
| 1089 | #theory.data = numpy.zeros(len(mask)) |
---|
[75a8723] | 1090 | theory.err_data = numpy.ones(len(mask)) |
---|
[e575db9] | 1091 | theory.qx_data = qx_data |
---|
| 1092 | theory.qy_data = qy_data |
---|
| 1093 | theory.q_data = q_data |
---|
| 1094 | theory.mask = mask |
---|
| 1095 | theory.x_bins = x_bins |
---|
| 1096 | theory.y_bins = y_bins |
---|
| 1097 | |
---|
| 1098 | # max and min taking account of the bin sizes |
---|
[a0da535] | 1099 | theory.xmin = xmin |
---|
| 1100 | theory.xmax = xmax |
---|
| 1101 | theory.ymin = ymin |
---|
| 1102 | theory.ymax = ymax |
---|
[3fafe15] | 1103 | group_id = str(id) + " Model2D" |
---|
| 1104 | if group_id not in theory.group_id: |
---|
| 1105 | theory.group_id.append(group_id) |
---|
| 1106 | theory.id = str(id) + " Model2D" |
---|
[6bbeacd4] | 1107 | |
---|
[c77d859] | 1108 | |
---|
[fa65e99] | 1109 | def _complete1D(self, x,y, id, elapsed,index,model, |
---|
| 1110 | toggle_mode_on=False,state=None, data=None): |
---|
[c77d859] | 1111 | """ |
---|
[5062bbf] | 1112 | Complete plotting 1D data |
---|
[c77d859] | 1113 | """ |
---|
| 1114 | try: |
---|
[6bbeacd4] | 1115 | new_plot = Data1D(x=x, y=y) |
---|
| 1116 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[a0da535] | 1117 | if data != None: |
---|
[6bbeacd4] | 1118 | _xaxis, _xunit = data.get_xaxis() |
---|
| 1119 | _yaxis, _yunit = data.get_yaxis() |
---|
| 1120 | new_plot.title = data.name |
---|
| 1121 | #if the theory is already plotted use the same group id |
---|
| 1122 | #to replot |
---|
| 1123 | if id in self.page_finder: |
---|
| 1124 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1125 | if theory_data is not None: |
---|
| 1126 | group_id = theory_data.group_id[len(theory_data.group_id)-1] |
---|
| 1127 | if group_id not in data.group_id: |
---|
| 1128 | data.group_id.append(group_id) |
---|
| 1129 | #data is plotted before the theory, then take its group_id |
---|
| 1130 | #assign to the new theory |
---|
| 1131 | group_id = data.group_id[len(data.group_id)-1] |
---|
| 1132 | if group_id not in new_plot.group_id: |
---|
| 1133 | new_plot.group_id.append(group_id) |
---|
[a5701e6] | 1134 | |
---|
[6bbeacd4] | 1135 | else: |
---|
| 1136 | _xaxis, _xunit = "\\rm{Q}", 'A^{-1}' |
---|
| 1137 | _yaxis, _yunit = "\\rm{Intensity} ", "cm^{-1}" |
---|
| 1138 | new_plot.title = "Analytical model 1D " |
---|
| 1139 | #find a group id to plot theory without data |
---|
| 1140 | group_id = str(id) + " Model1D" |
---|
| 1141 | if group_id not in new_plot.group_id: |
---|
| 1142 | new_plot.group_id.append(group_id) |
---|
| 1143 | new_plot.is_data = False |
---|
| 1144 | new_plot.id = str(id) + " Model1D" |
---|
[3fafe15] | 1145 | |
---|
[6bbeacd4] | 1146 | #find if this theory was already plotted and replace that plot given |
---|
| 1147 | #the same id |
---|
| 1148 | if id in self.page_finder: |
---|
| 1149 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1150 | if theory_data is not None: |
---|
[c399004] | 1151 | new_plot.id = theory_data.id |
---|
[3fafe15] | 1152 | |
---|
[6bbeacd4] | 1153 | new_plot.name = model.name + " ["+ str(model.__class__.__name__)+ "]" |
---|
| 1154 | new_plot.xaxis(_xaxis, _xunit) |
---|
| 1155 | new_plot.yaxis(_yaxis, _yunit) |
---|
[3fafe15] | 1156 | if toggle_mode_on: |
---|
| 1157 | new_plot.id = str(id) + " Model" |
---|
| 1158 | wx.PostEvent(self.parent, |
---|
| 1159 | NewPlotEvent(group_id=str(id) + " Model2D", |
---|
| 1160 | action="Hide")) |
---|
[a5701e6] | 1161 | |
---|
[6bbeacd4] | 1162 | self.page_finder[id].set_theory_data(new_plot) |
---|
[a0da535] | 1163 | if data is None: |
---|
[5ef55d2] | 1164 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1165 | self.parent.add_data_helper(data_list=[theory_data]) |
---|
| 1166 | data_id = theory_data.id |
---|
[c77d859] | 1167 | else: |
---|
[5ef55d2] | 1168 | data_id = data.id |
---|
[3fafe15] | 1169 | |
---|
[5ef55d2] | 1170 | self.parent.append_theory(data_id=data_id, |
---|
| 1171 | theory=new_plot, state=state) |
---|
[fa65e99] | 1172 | |
---|
[5ef55d2] | 1173 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[6bbeacd4] | 1174 | title= str(new_plot.title))) |
---|
| 1175 | current_pg = self.fit_panel.get_page_by_id(id) |
---|
[a0da535] | 1176 | wx.PostEvent(current_pg, |
---|
[6bbeacd4] | 1177 | Chi2UpdateEvent(output=self._cal_chisqr(data=data, |
---|
| 1178 | id=id, |
---|
| 1179 | index=index))) |
---|
[847091f] | 1180 | msg = "Plot 1D complete !" |
---|
[a0da535] | 1181 | wx.PostEvent( self.parent, StatusEvent(status=msg, type="stop" )) |
---|
[c77d859] | 1182 | except: |
---|
[6bbeacd4] | 1183 | raise |
---|
| 1184 | #msg = " Error occurred when drawing %s Model 1D: " % new_plot.name |
---|
| 1185 | #msg += " %s" % sys.exc_value |
---|
| 1186 | #wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[5062bbf] | 1187 | |
---|
[c77d859] | 1188 | def _update2D(self, output,time=None): |
---|
| 1189 | """ |
---|
[5062bbf] | 1190 | Update the output of plotting model |
---|
[c77d859] | 1191 | """ |
---|
| 1192 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
[a0da535] | 1193 | #updating ... ", type="update")) |
---|
[c660493] | 1194 | self.ready_fit() |
---|
| 1195 | #self.calc_thread.ready(0.01) |
---|
[5062bbf] | 1196 | |
---|
[6bbeacd4] | 1197 | def _complete2D(self, image, data, model, id, elapsed, index, qmin, |
---|
[fa65e99] | 1198 | qmax, toggle_mode_on=False,state=None,qstep=DEFAULT_NPTS): |
---|
[c77d859] | 1199 | """ |
---|
[5062bbf] | 1200 | Complete get the result of modelthread and create model 2D |
---|
| 1201 | that can be plot. |
---|
[c77d859] | 1202 | """ |
---|
| 1203 | err_image = numpy.zeros(numpy.shape(image)) |
---|
[a8088d7] | 1204 | |
---|
[fa65e99] | 1205 | new_plot= Data2D(image=image, err_image=err_image) |
---|
| 1206 | new_plot.name = model.name |
---|
[c77d859] | 1207 | |
---|
[a0da535] | 1208 | if data is None: |
---|
[fa65e99] | 1209 | self._fill_default_model2D(theory=new_plot, |
---|
[a0da535] | 1210 | qmax=qmax, |
---|
[6bbeacd4] | 1211 | id=id, |
---|
[a0da535] | 1212 | qstep=qstep, |
---|
[5062bbf] | 1213 | qmin= qmin) |
---|
[6bbeacd4] | 1214 | |
---|
[c77d859] | 1215 | else: |
---|
[fa65e99] | 1216 | new_plot.id = str(id) + " Model2D" |
---|
[3fafe15] | 1217 | group_id = str(id) + " Model2D" |
---|
| 1218 | if group_id not in new_plot.group_id: |
---|
| 1219 | new_plot.group_id.append(group_id) |
---|
[6bbeacd4] | 1220 | |
---|
[fa65e99] | 1221 | new_plot.x_bins = data.x_bins |
---|
| 1222 | new_plot.y_bins = data.y_bins |
---|
| 1223 | new_plot.detector = data.detector |
---|
| 1224 | new_plot.source = data.source |
---|
| 1225 | new_plot.is_data = False |
---|
| 1226 | new_plot.qx_data = data.qx_data |
---|
| 1227 | new_plot.qy_data = data.qy_data |
---|
| 1228 | new_plot.q_data = data.q_data |
---|
[a0da535] | 1229 | #numpy.zeros(len(data.err_data))#data.err_data |
---|
[fa65e99] | 1230 | new_plot.err_data = err_image |
---|
| 1231 | new_plot.mask = data.mask |
---|
[c77d859] | 1232 | ## plot boundaries |
---|
[fa65e99] | 1233 | new_plot.ymin = data.ymin |
---|
| 1234 | new_plot.ymax = data.ymax |
---|
| 1235 | new_plot.xmin = data.xmin |
---|
| 1236 | new_plot.xmax = data.xmax |
---|
| 1237 | new_plot.name = model.name + " ["+ str(model.__class__.__name__)+ "]" |
---|
| 1238 | new_plot.title = "Analytical model 2D " |
---|
| 1239 | theory_data = deepcopy(new_plot) |
---|
[5ef55d2] | 1240 | theory_data.name = "Unknown" |
---|
[3fafe15] | 1241 | if toggle_mode_on: |
---|
| 1242 | new_plot.id = str(id) + " Model" |
---|
| 1243 | wx.PostEvent(self.parent, |
---|
| 1244 | NewPlotEvent(group_id=str(id) + " Model1D", |
---|
| 1245 | action="Hide")) |
---|
[5ef55d2] | 1246 | self.page_finder[id].set_theory_data(theory_data) |
---|
[6bbeacd4] | 1247 | |
---|
[5ef55d2] | 1248 | if data is None: |
---|
| 1249 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1250 | self.parent.add_data_helper(data_list=[theory_data]) |
---|
| 1251 | data_id = theory_data.id |
---|
| 1252 | else: |
---|
| 1253 | data_id = data.id |
---|
[3fafe15] | 1254 | |
---|
[5ef55d2] | 1255 | self.parent.append_theory(data_id=data_id, |
---|
[fa65e99] | 1256 | theory=new_plot, state=state) |
---|
[f93dfcb] | 1257 | ## plot |
---|
[fa65e99] | 1258 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[3fafe15] | 1259 | title=new_plot.title)) |
---|
[f72333f] | 1260 | # Chisqr in fitpage |
---|
[6bbeacd4] | 1261 | current_pg = self.fit_panel.get_page_by_id(id) |
---|
[5062bbf] | 1262 | wx.PostEvent(current_pg, |
---|
[6bbeacd4] | 1263 | Chi2UpdateEvent(output=self._cal_chisqr(data=data, id=id, index=index))) |
---|
[847091f] | 1264 | msg = "Plot 2D complete !" |
---|
[a0da535] | 1265 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[6bbeacd4] | 1266 | |
---|
| 1267 | def _draw_model2D(self, model, id, data=None, smearer=None, |
---|
[a0da535] | 1268 | description=None, enable2D=False, |
---|
[5ef55d2] | 1269 | state=None, |
---|
[fa65e99] | 1270 | toggle_mode_on=False, |
---|
[a0da535] | 1271 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, |
---|
| 1272 | qstep=DEFAULT_NPTS): |
---|
[f93dfcb] | 1273 | """ |
---|
[5062bbf] | 1274 | draw model in 2D |
---|
| 1275 | |
---|
| 1276 | :param model: instance of the model to draw |
---|
| 1277 | :param description: the description of the model |
---|
| 1278 | :param enable2D: when True allows to draw model 2D |
---|
| 1279 | :param qmin: the minimum value to draw model 2D |
---|
| 1280 | :param qmax: the maximum value to draw model 2D |
---|
| 1281 | :param qstep: the number of division of Qx and Qy of the model to draw |
---|
[f93dfcb] | 1282 | |
---|
| 1283 | """ |
---|
[a0da535] | 1284 | x= numpy.linspace(start=-1*qmax, |
---|
| 1285 | stop=qmax, |
---|
| 1286 | num=qstep, |
---|
| 1287 | endpoint=True) |
---|
[d15c0202] | 1288 | y = numpy.linspace(start= -1*qmax, |
---|
[a0da535] | 1289 | stop=qmax, |
---|
| 1290 | num=qstep, |
---|
| 1291 | endpoint=True) |
---|
[6bbeacd4] | 1292 | if model is None: |
---|
| 1293 | msg = "Panel with ID: %s does not contained model" % str(id) |
---|
| 1294 | raise ValueError, msg |
---|
[c77d859] | 1295 | ## use data info instead |
---|
[a0da535] | 1296 | if data is not None: |
---|
[c77d859] | 1297 | ## check if data2D to plot |
---|
| 1298 | if hasattr(data, "x_bins"): |
---|
| 1299 | enable2D = True |
---|
[a0da535] | 1300 | x = data.x_bins |
---|
| 1301 | y = data.y_bins |
---|
[f72333f] | 1302 | |
---|
[c77d859] | 1303 | if not enable2D: |
---|
[a0da535] | 1304 | return None, None |
---|
[c77d859] | 1305 | try: |
---|
| 1306 | from model_thread import Calc2D |
---|
| 1307 | ## If a thread is already started, stop it |
---|
[a0da535] | 1308 | if (self.calc_2D is not None) and self.calc_2D.isrunning(): |
---|
[c77d859] | 1309 | self.calc_2D.stop() |
---|
[f72333f] | 1310 | |
---|
[a0da535] | 1311 | self.calc_2D = Calc2D(x=x, |
---|
| 1312 | y=y, |
---|
| 1313 | model=model, |
---|
| 1314 | data=data, |
---|
[6bbeacd4] | 1315 | id=id, |
---|
[a0da535] | 1316 | smearer=smearer, |
---|
| 1317 | qmin=qmin, |
---|
| 1318 | qmax=qmax, |
---|
| 1319 | qstep=qstep, |
---|
[fa65e99] | 1320 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 1321 | state=state, |
---|
[a0da535] | 1322 | completefn=self._complete2D, |
---|
| 1323 | updatefn=self._update2D) |
---|
[c77d859] | 1324 | self.calc_2D.queue() |
---|
[f72333f] | 1325 | |
---|
[c77d859] | 1326 | except: |
---|
[6bbeacd4] | 1327 | raise |
---|
| 1328 | #msg = " Error occurred when drawing %s Model 2D: " % model.name |
---|
| 1329 | #msg += " %s" % sys.exc_value |
---|
| 1330 | #wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[f72333f] | 1331 | |
---|
[6bbeacd4] | 1332 | def _draw_model1D(self, model, id, data=None, smearer=None, |
---|
[67e258c] | 1333 | qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, |
---|
[5ef55d2] | 1334 | state=None, |
---|
[fa65e99] | 1335 | toggle_mode_on=False, |
---|
[67e258c] | 1336 | qstep=DEFAULT_NPTS, enable1D=True): |
---|
[c77d859] | 1337 | """ |
---|
[5062bbf] | 1338 | Draw model 1D from loaded data1D |
---|
| 1339 | |
---|
| 1340 | :param data: loaded data |
---|
| 1341 | :param model: the model to plot |
---|
| 1342 | |
---|
[c77d859] | 1343 | """ |
---|
[a0da535] | 1344 | x= numpy.linspace(start=qmin, |
---|
| 1345 | stop=qmax, |
---|
| 1346 | num=qstep, |
---|
[c77d859] | 1347 | endpoint=True |
---|
| 1348 | ) |
---|
[a0da535] | 1349 | if data is not None: |
---|
[c77d859] | 1350 | ## check for data2D |
---|
| 1351 | if hasattr(data,"x_bins"): |
---|
| 1352 | return |
---|
| 1353 | x = data.x |
---|
[75fbd17] | 1354 | if qmin == DEFAULT_QMIN : |
---|
| 1355 | qmin = min(data.x) |
---|
| 1356 | if qmax == DEFAULT_QMAX: |
---|
| 1357 | qmax = max(data.x) |
---|
[c77d859] | 1358 | if not enable1D: |
---|
[f72333f] | 1359 | return |
---|
[c77d859] | 1360 | try: |
---|
| 1361 | from model_thread import Calc1D |
---|
| 1362 | ## If a thread is already started, stop it |
---|
[a0da535] | 1363 | if (self.calc_1D is not None) and self.calc_1D.isrunning(): |
---|
[c77d859] | 1364 | self.calc_1D.stop() |
---|
[a0da535] | 1365 | self.calc_1D = Calc1D(x=x, |
---|
| 1366 | data=data, |
---|
[6bbeacd4] | 1367 | model=model, |
---|
| 1368 | id=id, |
---|
[a0da535] | 1369 | qmin=qmin, |
---|
| 1370 | qmax=qmax, |
---|
| 1371 | smearer=smearer, |
---|
[5ef55d2] | 1372 | state=state, |
---|
[fa65e99] | 1373 | toggle_mode_on=toggle_mode_on, |
---|
[a0da535] | 1374 | completefn=self._complete1D, |
---|
| 1375 | updatefn=self._update1D) |
---|
[c77d859] | 1376 | self.calc_1D.queue() |
---|
[f72333f] | 1377 | |
---|
[c77d859] | 1378 | except: |
---|
[a0da535] | 1379 | msg = " Error occurred when drawing %s Model 1D: " % model.name |
---|
| 1380 | msg += " %s" % sys.exc_value |
---|
| 1381 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[f72333f] | 1382 | |
---|
[6bbeacd4] | 1383 | def _cal_chisqr(self, id, data=None, index=None): |
---|
[f72333f] | 1384 | """ |
---|
[5062bbf] | 1385 | Get handy Chisqr using the output from draw1D and 2D, |
---|
| 1386 | instead of calling expansive CalcChisqr in guithread |
---|
[f72333f] | 1387 | """ |
---|
| 1388 | # default chisqr |
---|
| 1389 | chisqr = None |
---|
[f151ddf] | 1390 | |
---|
[f72333f] | 1391 | # return None if data == None |
---|
| 1392 | if data == None: return chisqr |
---|
[f151ddf] | 1393 | |
---|
[f72333f] | 1394 | # Get data: data I, theory I, and data dI in order |
---|
| 1395 | if data.__class__.__name__ =="Data2D": |
---|
[a0da535] | 1396 | if index == None: |
---|
| 1397 | index = numpy.ones(len(data.data),ntype=bool) |
---|
| 1398 | # get rid of zero error points |
---|
| 1399 | index = index & (data.err_data != 0) |
---|
[f72333f] | 1400 | fn = data.data[index] |
---|
[6bbeacd4] | 1401 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1402 | gn = theory_data.data[index] |
---|
[f72333f] | 1403 | en = data.err_data[index] |
---|
| 1404 | else: |
---|
| 1405 | # 1 d theory from model_thread is only in the range of index |
---|
[a0da535] | 1406 | if index == None: |
---|
| 1407 | index = numpy.ones(len(data.y), ntype=bool) |
---|
| 1408 | if data.dy == None or data.dy == []: |
---|
[4d5fa8c] | 1409 | dy = numpy.ones(len(data.y)) |
---|
[f151ddf] | 1410 | else: |
---|
[a0da535] | 1411 | ## Set consitently w/AbstractFitengine: |
---|
| 1412 | # But this should be corrected later. |
---|
[4d5fa8c] | 1413 | dy = data.dy |
---|
| 1414 | dy[dy==0] = 1 |
---|
[f72333f] | 1415 | fn = data.y[index] |
---|
[6bbeacd4] | 1416 | theory_data = self.page_finder[id].get_theory_data() |
---|
| 1417 | gn = theory_data.y |
---|
[4d5fa8c] | 1418 | en = dy[index] |
---|
[f72333f] | 1419 | # residual |
---|
| 1420 | res = (fn - gn)/en |
---|
| 1421 | # get chisqr only w/finite |
---|
[a0da535] | 1422 | val = res[numpy.isfinite(res)]*res[numpy.isfinite(res)] |
---|
| 1423 | chisqr = numpy.average(val) |
---|
[f72333f] | 1424 | return chisqr |
---|
| 1425 | |
---|
| 1426 | |
---|
[5062bbf] | 1427 | #def profile(fn, *args, **kw): |
---|
| 1428 | # import cProfile, pstats, os |
---|
| 1429 | # global call_result |
---|
| 1430 | # def call(): |
---|
| 1431 | # global call_result |
---|
| 1432 | # call_result = fn(*args, **kw) |
---|
| 1433 | # cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
| 1434 | # stats = pstats.Stats('profile.out') |
---|
| 1435 | # #stats.sort_stats('time') |
---|
| 1436 | # stats.sort_stats('calls') |
---|
| 1437 | # stats.print_stats() |
---|
| 1438 | # os.unlink('profile.out') |
---|
| 1439 | # return call_result |
---|
[d89f09b] | 1440 | if __name__ == "__main__": |
---|
| 1441 | i = Plugin() |
---|
| 1442 | |
---|
| 1443 | |
---|
| 1444 | |
---|
| 1445 | |
---|