[6d8bad4] | 1 | """ |
---|
| 2 | Fitting perspective |
---|
| 3 | """ |
---|
[5062bbf] | 4 | ################################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[f32d144] | 7 | #project funded by the US National Science Foundation. |
---|
[5062bbf] | 8 | # |
---|
| 9 | #See the license text in license.txt |
---|
| 10 | # |
---|
| 11 | #copyright 2009, University of Tennessee |
---|
| 12 | ################################################################################ |
---|
[a0da535] | 13 | import re |
---|
[5536035] | 14 | import sys |
---|
[056b569] | 15 | import os |
---|
[5536035] | 16 | import wx |
---|
| 17 | import logging |
---|
| 18 | import numpy |
---|
[c3b4dcb] | 19 | import time |
---|
[edcbd467] | 20 | from copy import deepcopy |
---|
[f83b94a] | 21 | import models |
---|
| 22 | |
---|
[c21e87b] | 23 | from sans.dataloader.loader import Loader |
---|
[c3b4dcb] | 24 | from sans.guiframe.dataFitting import Data2D |
---|
| 25 | from sans.guiframe.dataFitting import Data1D |
---|
[2dbffcc] | 26 | from sans.guiframe.dataFitting import check_data_validity |
---|
[6bbeacd4] | 27 | from sans.guiframe.events import NewPlotEvent |
---|
| 28 | from sans.guiframe.events import StatusEvent |
---|
[a0da535] | 29 | from sans.guiframe.events import EVT_SLICER_PANEL |
---|
| 30 | from sans.guiframe.events import EVT_SLICER_PARS_UPDATE |
---|
[6bbeacd4] | 31 | from sans.guiframe.gui_style import GUIFRAME_ID |
---|
| 32 | from sans.guiframe.plugin_base import PluginBase |
---|
[e68bfef] | 33 | from sans.guiframe.data_processor import BatchCell |
---|
[2dbffcc] | 34 | from sans.fit.Fitting import Fit |
---|
[cc31608] | 35 | from .console import ConsoleUpdate |
---|
[2dbffcc] | 36 | from .fitproblem import FitProblemDictionary |
---|
[a0da535] | 37 | from .fitpanel import FitPanel |
---|
[cc31608] | 38 | from .fit_thread import FitThread |
---|
[a0da535] | 39 | from .pagestate import Reader |
---|
| 40 | from .fitpage import Chi2UpdateEvent |
---|
[6f140f2] | 41 | from sans.perspectives.calculator.model_editor import TextDialog |
---|
| 42 | from sans.perspectives.calculator.model_editor import EditorWindow |
---|
[ed2ea6a] | 43 | |
---|
[c26a64b] | 44 | MAX_NBR_DATA = 4 |
---|
[2c6b224] | 45 | SANS_F_TOL = 5e-05 |
---|
[5062bbf] | 46 | |
---|
[f32d144] | 47 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
[92a5ac92] | 48 | |
---|
[5062bbf] | 49 | |
---|
[f32d144] | 50 | if sys.platform.count("darwin") == 0: |
---|
[f6aee42] | 51 | ON_MAC = False |
---|
| 52 | else: |
---|
[f32d144] | 53 | ON_MAC = True |
---|
| 54 | |
---|
[3658abed] | 55 | |
---|
| 56 | class Plugin(PluginBase): |
---|
[d89f09b] | 57 | """ |
---|
[f32d144] | 58 | Fitting plugin is used to perform fit |
---|
[d89f09b] | 59 | """ |
---|
[3658abed] | 60 | def __init__(self, standalone=False): |
---|
[908b817] | 61 | PluginBase.__init__(self, name="Fitting", standalone=standalone) |
---|
[d89f09b] | 62 | |
---|
[ed2ea6a] | 63 | #list of panel to send to guiframe |
---|
[f83b94a] | 64 | self.mypanels = [] |
---|
[ed2ea6a] | 65 | # reference to the current running thread |
---|
[f83b94a] | 66 | self.calc_2D = None |
---|
| 67 | self.calc_1D = None |
---|
[9f4f8f4] | 68 | |
---|
| 69 | self.color_dict = {} |
---|
| 70 | |
---|
[66ff250] | 71 | self.fit_thread_list = {} |
---|
[2296316] | 72 | self.residuals = None |
---|
[55bb249c] | 73 | self.weight = None |
---|
[9466f2d6] | 74 | self.fit_panel = None |
---|
[d89f09b] | 75 | # Start with a good default |
---|
| 76 | self.elapsed = 0.022 |
---|
[ed2ea6a] | 77 | # the type of optimizer selected, park or scipy |
---|
[f32d144] | 78 | self.fitter = None |
---|
[2296316] | 79 | self.fit_panel = None |
---|
[c660493] | 80 | #let fit ready |
---|
| 81 | self.fitproblem_count = None |
---|
[ed2ea6a] | 82 | #Flag to let the plug-in know that it is running stand alone |
---|
[f83b94a] | 83 | self.standalone = True |
---|
[f32d144] | 84 | ## dictionary of page closed and id |
---|
[f83b94a] | 85 | self.closed_page_dict = {} |
---|
[d89f09b] | 86 | ## Fit engine |
---|
[e9e914f] | 87 | self._fit_engine = 'scipy' |
---|
[7db52f1] | 88 | self._gui_engine = None |
---|
[2296316] | 89 | ## Relative error desired in the sum of squares (float); scipy only |
---|
[2c6b224] | 90 | self.ftol = SANS_F_TOL |
---|
[7db52f1] | 91 | self.batch_reset_flag = True |
---|
[ed2ea6a] | 92 | #List of selected data |
---|
[f83b94a] | 93 | self.selected_data_list = [] |
---|
[c3435b7f] | 94 | ## list of slicer panel created to display slicer parameters and results |
---|
[f83b94a] | 95 | self.slicer_panels = [] |
---|
[32d802f] | 96 | # model 2D view |
---|
[f83b94a] | 97 | self.model2D_id = None |
---|
[ed2ea6a] | 98 | #keep reference of the simultaneous fit page |
---|
[f83b94a] | 99 | self.sim_page = None |
---|
[fa02d95] | 100 | self.sim_menu = None |
---|
| 101 | self.batch_page = None |
---|
| 102 | self.batch_menu = None |
---|
[ae4ade7] | 103 | self.index_model = 0 |
---|
[9f4f8f4] | 104 | self.test_model_color = None |
---|
[f83b94a] | 105 | #Create a reader for fit page's state |
---|
[f32d144] | 106 | self.state_reader = None |
---|
[c8deee5] | 107 | self._extensions = '.fitv' |
---|
[0b6ae5f] | 108 | self.scipy_id = wx.NewId() |
---|
| 109 | self.park_id = wx.NewId() |
---|
[dafc36f] | 110 | self.menu1 = None |
---|
[6f140f2] | 111 | self.new_model_frame = None |
---|
[0b6ae5f] | 112 | |
---|
[4da35bc] | 113 | self.temp_state = [] |
---|
[ef16f59] | 114 | self.state_index = 0 |
---|
[b63dc6e] | 115 | self.sfile_ext = None |
---|
[6bbeacd4] | 116 | # take care of saving data, model and page associated with each other |
---|
| 117 | self.page_finder = {} |
---|
[f83b94a] | 118 | # Log startup |
---|
[f32d144] | 119 | logging.info("Fitting plug-in started") |
---|
[7360816] | 120 | self.batch_capable = self.get_batch_capable() |
---|
| 121 | |
---|
| 122 | def get_batch_capable(self): |
---|
| 123 | """ |
---|
| 124 | Check if the plugin has a batch capability |
---|
| 125 | """ |
---|
| 126 | return True |
---|
[9f4f8f4] | 127 | |
---|
[2dbffcc] | 128 | def create_fit_problem(self, page_id): |
---|
| 129 | """ |
---|
| 130 | Given an ID create a fitproblem container |
---|
| 131 | """ |
---|
| 132 | self.page_finder[page_id] = FitProblemDictionary() |
---|
| 133 | |
---|
| 134 | def delete_fit_problem(self, page_id): |
---|
| 135 | """ |
---|
| 136 | Given an ID create a fitproblem container |
---|
| 137 | """ |
---|
| 138 | if page_id in self.page_finder.iterkeys(): |
---|
| 139 | del self.page_finder[page_id] |
---|
| 140 | |
---|
[9f4f8f4] | 141 | def add_color(self, color, id): |
---|
| 142 | """ |
---|
| 143 | adds a color as a key with a plot id as its value to a dictionary |
---|
| 144 | """ |
---|
| 145 | self.color_dict[id] = color |
---|
[2a8fac1] | 146 | |
---|
[cc31608] | 147 | def on_batch_selection(self, flag): |
---|
| 148 | """ |
---|
| 149 | switch the the notebook of batch mode or not |
---|
| 150 | """ |
---|
| 151 | self.batch_on = flag |
---|
[2dbffcc] | 152 | if self.fit_panel is not None: |
---|
| 153 | self.fit_panel.batch_on = self.batch_on |
---|
[cc31608] | 154 | |
---|
[cab076b] | 155 | def populate_menu(self, owner): |
---|
[d89f09b] | 156 | """ |
---|
[5062bbf] | 157 | Create a menu for the Fitting plug-in |
---|
| 158 | |
---|
| 159 | :param id: id to create a menu |
---|
| 160 | :param owner: owner of menu |
---|
| 161 | |
---|
| 162 | :return: list of information to populate the main menu |
---|
| 163 | |
---|
[d89f09b] | 164 | """ |
---|
| 165 | #Menu for fitting |
---|
| 166 | self.menu1 = wx.Menu() |
---|
[0b6ae5f] | 167 | id1 = wx.NewId() |
---|
| 168 | simul_help = "Add new fit panel" |
---|
[f32d144] | 169 | self.menu1.Append(id1, '&New Fit Page', simul_help) |
---|
[0b6ae5f] | 170 | wx.EVT_MENU(owner, id1, self.on_add_new_page) |
---|
[6f023e8] | 171 | self.menu1.AppendSeparator() |
---|
[dafc36f] | 172 | self.id_simfit = wx.NewId() |
---|
[6bbeacd4] | 173 | simul_help = "Simultaneous Fit" |
---|
[f32d144] | 174 | self.menu1.Append(self.id_simfit, '&Simultaneous Fit', simul_help) |
---|
[dafc36f] | 175 | wx.EVT_MENU(owner, self.id_simfit, self.on_add_sim_page) |
---|
[fa02d95] | 176 | self.sim_menu = self.menu1.FindItemById(self.id_simfit) |
---|
[f32d144] | 177 | self.sim_menu.Enable(False) |
---|
[fa02d95] | 178 | #combined Batch |
---|
| 179 | self.id_batchfit = wx.NewId() |
---|
[7b1ca97] | 180 | batch_help = "Combined Batch" |
---|
[fa02d95] | 181 | self.menu1.Append(self.id_batchfit, '&Combine Batch Fit', batch_help) |
---|
[f32d144] | 182 | wx.EVT_MENU(owner, self.id_batchfit, self.on_add_sim_page) |
---|
[fa02d95] | 183 | self.batch_menu = self.menu1.FindItemById(self.id_batchfit) |
---|
[f32d144] | 184 | self.batch_menu.Enable(False) |
---|
[f7e9af2] | 185 | self.menu1.AppendSeparator() |
---|
[0b6ae5f] | 186 | #Set park engine |
---|
[fa02d95] | 187 | scipy_help = "Scipy Engine: Perform Simple fit. More in Help window...." |
---|
[2c6b224] | 188 | self.menu1.AppendCheckItem(self.scipy_id, "Simple FitEngine [LeastSq]", |
---|
[f32d144] | 189 | scipy_help) |
---|
| 190 | wx.EVT_MENU(owner, self.scipy_id, self._onset_engine_scipy) |
---|
[0b6ae5f] | 191 | |
---|
| 192 | park_help = "Park Engine: Perform Complex fit. More in Help window...." |
---|
[2c6b224] | 193 | self.menu1.AppendCheckItem(self.park_id, "Complex FitEngine [ParkMC]", |
---|
[f32d144] | 194 | park_help) |
---|
| 195 | wx.EVT_MENU(owner, self.park_id, self._onset_engine_park) |
---|
[0b6ae5f] | 196 | |
---|
| 197 | self.menu1.FindItemById(self.scipy_id).Check(True) |
---|
| 198 | self.menu1.FindItemById(self.park_id).Check(False) |
---|
[2c6b224] | 199 | self.menu1.AppendSeparator() |
---|
| 200 | self.id_tol = wx.NewId() |
---|
| 201 | ftol_help = "Change the current FTolerance (=%s) " % str(self.ftol) |
---|
[f32d144] | 202 | ftol_help += "of Simple FitEngine..." |
---|
| 203 | self.menu1.Append(self.id_tol, "Change FTolerance", |
---|
| 204 | ftol_help) |
---|
| 205 | wx.EVT_MENU(owner, self.id_tol, self.show_ftol_dialog) |
---|
[da6fd1a] | 206 | self.menu1.AppendSeparator() |
---|
[2c6b224] | 207 | |
---|
[da6fd1a] | 208 | self.id_reset_flag = wx.NewId() |
---|
| 209 | resetf_help = "BatchFit: If checked, the initial param values will be " |
---|
[f32d144] | 210 | resetf_help += "propagated from the previous results. " |
---|
[da6fd1a] | 211 | resetf_help += "Otherwise, the same initial param values will be used " |
---|
[f32d144] | 212 | resetf_help += "for all fittings." |
---|
| 213 | self.menu1.AppendCheckItem(self.id_reset_flag, |
---|
| 214 | "Chain Fitting [BatchFit Only]", |
---|
| 215 | resetf_help) |
---|
| 216 | wx.EVT_MENU(owner, self.id_reset_flag, self.on_reset_batch_flag) |
---|
[70e439f] | 217 | chain_menu = self.menu1.FindItemById(self.id_reset_flag) |
---|
| 218 | chain_menu.Check(not self.batch_reset_flag) |
---|
| 219 | chain_menu.Enable(self.batch_on) |
---|
[056b569] | 220 | |
---|
| 221 | self.menu1.AppendSeparator() |
---|
[5d1c1f4] | 222 | self.edit_model_menu = wx.Menu() |
---|
| 223 | # Find and put files name in menu |
---|
| 224 | try: |
---|
| 225 | self.set_edit_menu(owner=owner) |
---|
| 226 | except: |
---|
| 227 | raise |
---|
[056b569] | 228 | |
---|
[5d1c1f4] | 229 | self.id_edit = wx.NewId() |
---|
[f32d144] | 230 | editmodel_help = "Edit customized model sample file" |
---|
| 231 | self.menu1.AppendMenu(self.id_edit, "Edit Custom Model", |
---|
[5d1c1f4] | 232 | self.edit_model_menu, editmodel_help) |
---|
[3b19ac9] | 233 | #create menubar items |
---|
[908b817] | 234 | return [(self.menu1, self.sub_menu)] |
---|
[056b569] | 235 | |
---|
| 236 | def edit_custom_model(self, event): |
---|
| 237 | """ |
---|
| 238 | Get the python editor panel |
---|
| 239 | """ |
---|
[5d1c1f4] | 240 | id = event.GetId() |
---|
[7c8d3093] | 241 | label = self.edit_menu.GetLabel(id) |
---|
[5d1c1f4] | 242 | from sans.perspectives.calculator.pyconsole import PyConsole |
---|
| 243 | filename = os.path.join(models.find_plugins_dir(), label) |
---|
[f32d144] | 244 | frame = PyConsole(parent=self.parent, manager=self, |
---|
| 245 | panel=self.fit_panel, |
---|
| 246 | title='Advanced Custom Model Editor', |
---|
| 247 | filename=filename) |
---|
[056b569] | 248 | self.put_icon(frame) |
---|
[f32d144] | 249 | frame.Show(True) |
---|
[7c8d3093] | 250 | |
---|
[b25caad] | 251 | def delete_custom_model(self, event): |
---|
| 252 | """ |
---|
| 253 | Delete custom model file |
---|
| 254 | """ |
---|
| 255 | id = event.GetId() |
---|
| 256 | label = self.delete_menu.GetLabel(id) |
---|
| 257 | toks = os.path.splitext(label) |
---|
| 258 | path = os.path.join(models.find_plugins_dir(), toks[0]) |
---|
| 259 | try: |
---|
| 260 | for ext in ['.py', '.pyc']: |
---|
| 261 | p_path = path + ext |
---|
| 262 | os.remove(p_path) |
---|
| 263 | self.update_custom_combo() |
---|
[19e614a] | 264 | if os.path.isfile(p_path): |
---|
| 265 | msg = "Sorry! Could not be able to delete the default " |
---|
| 266 | msg += "custom model... \n" |
---|
| 267 | msg += "Please manually remove the files (.py, .pyc) " |
---|
| 268 | msg += "in the 'plugin_models' folder \n" |
---|
| 269 | msg += "inside of the SansView application, " |
---|
| 270 | msg += "and try it again." |
---|
| 271 | wx.MessageBox(msg, 'Info') |
---|
| 272 | #wx.PostEvent(self.parent, StatusEvent(status=msg, type='stop', |
---|
| 273 | # info='warning')) |
---|
| 274 | else: |
---|
| 275 | self.delete_menu.Delete(id) |
---|
| 276 | for item in self.edit_menu.GetMenuItems(): |
---|
| 277 | if item.GetLabel() == label: |
---|
| 278 | self.edit_menu.DeleteItem(item) |
---|
| 279 | msg = "The custom model, %s, has been deleted."% label |
---|
| 280 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 281 | type='stop', info='info')) |
---|
| 282 | break |
---|
[b25caad] | 283 | except: |
---|
| 284 | msg ='Delete Error: \nCould not delete the file; Check if in use.' |
---|
[19e614a] | 285 | wx.MessageBox(msg, 'Error') |
---|
[b25caad] | 286 | |
---|
[7c8d3093] | 287 | def make_sum_model(self, event): |
---|
| 288 | """ |
---|
| 289 | Edit summodel template and make one |
---|
| 290 | """ |
---|
| 291 | id = event.GetId() |
---|
| 292 | model_manager = models.ModelManager() |
---|
| 293 | model_list = model_manager.get_model_name_list() |
---|
[796c4d4] | 294 | plug_dir = models.find_plugins_dir() |
---|
| 295 | textdial = TextDialog(None, self, -1, 'Easy Sum(p1, p2)', |
---|
| 296 | model_list, plug_dir) |
---|
[7c8d3093] | 297 | self.put_icon(textdial) |
---|
[f32d144] | 298 | textdial.ShowModal() |
---|
[7c8d3093] | 299 | textdial.Destroy() |
---|
[6f140f2] | 300 | |
---|
| 301 | def make_new_model(self, event): |
---|
| 302 | """ |
---|
[f32d144] | 303 | Make new model |
---|
[6f140f2] | 304 | """ |
---|
| 305 | if self.new_model_frame != None and self.new_model_frame.IsShown(): |
---|
| 306 | self.new_model_frame.Show(False) |
---|
| 307 | else: |
---|
| 308 | id = event.GetId() |
---|
| 309 | dir_path = models.find_plugins_dir() |
---|
| 310 | title = "New Custom Model Function" |
---|
[f32d144] | 311 | self.new_model_frame = EditorWindow(parent=self, base=self, |
---|
[6f140f2] | 312 | path=dir_path, title=title) |
---|
| 313 | self.put_icon(self.new_model_frame) |
---|
| 314 | self.new_model_frame.Show(True) |
---|
[7c8d3093] | 315 | |
---|
[ed8fa6a3] | 316 | def update_custom_combo(self): |
---|
| 317 | """ |
---|
| 318 | Update custom model list in the fitpage combo box |
---|
| 319 | """ |
---|
| 320 | try: |
---|
[b25caad] | 321 | # Update edit menus |
---|
| 322 | self.set_edit_menu_helper(self.parent, self.edit_custom_model) |
---|
| 323 | self.set_edit_menu_helper(self.parent, self.delete_custom_model) |
---|
[ed8fa6a3] | 324 | temp = self.fit_panel.reset_pmodel_list() |
---|
| 325 | if temp: |
---|
| 326 | # Set the new custom model list for all fit pages |
---|
| 327 | for uid, page in self.fit_panel.opened_pages.iteritems(): |
---|
| 328 | if hasattr(page, "formfactorbox"): |
---|
| 329 | page.model_list_box = temp |
---|
[19e614a] | 330 | current_val = page.formfactorbox.GetLabel() |
---|
| 331 | #pos = page.formfactorbox.GetSelection() |
---|
[ed8fa6a3] | 332 | page._show_combox_helper() |
---|
[19e614a] | 333 | new_val = page.formfactorbox.GetLabel() |
---|
[b25caad] | 334 | if current_val != new_val and new_val != '': |
---|
[19e614a] | 335 | page.formfactorbox.SetLabel(new_val) |
---|
[b25caad] | 336 | else: |
---|
[19e614a] | 337 | page.formfactorbox.SetLabel(current_val) |
---|
[ed8fa6a3] | 338 | except: |
---|
| 339 | pass |
---|
| 340 | |
---|
| 341 | |
---|
[f32d144] | 342 | def set_edit_menu(self, owner): |
---|
[5d1c1f4] | 343 | """ |
---|
| 344 | Set list of the edit model menu labels |
---|
| 345 | """ |
---|
[7c8d3093] | 346 | id = wx.NewId() |
---|
[6f140f2] | 347 | #new_model_menu = wx.Menu() |
---|
[f32d144] | 348 | self.edit_model_menu.Append(id, 'New', |
---|
| 349 | 'Add a new model function') |
---|
| 350 | wx.EVT_MENU(owner, id, self.make_new_model) |
---|
[6f140f2] | 351 | id = wx.NewId() |
---|
[f32d144] | 352 | self.edit_model_menu.Append(id, 'Sum(p1, p2)', |
---|
| 353 | 'Sum of two model functions') |
---|
| 354 | wx.EVT_MENU(owner, id, self.make_sum_model) |
---|
[7c8d3093] | 355 | e_id = wx.NewId() |
---|
[f32d144] | 356 | self.edit_menu = wx.Menu() |
---|
[7c8d3093] | 357 | self.edit_model_menu.AppendMenu(e_id, |
---|
[b25caad] | 358 | 'Advanced', self.edit_menu) |
---|
| 359 | self.set_edit_menu_helper(owner, self.edit_custom_model) |
---|
| 360 | |
---|
| 361 | d_id = wx.NewId() |
---|
[f32d144] | 362 | self.delete_menu = wx.Menu() |
---|
| 363 | self.edit_model_menu.AppendMenu(d_id, |
---|
| 364 | 'Delete', self.delete_menu) |
---|
[b25caad] | 365 | self.set_edit_menu_helper(owner, self.delete_custom_model) |
---|
[7c8d3093] | 366 | |
---|
[d83549c] | 367 | def set_edit_menu_helper(self, owner=None, menu=None): |
---|
[7c8d3093] | 368 | """ |
---|
| 369 | help for setting list of the edit model menu labels |
---|
| 370 | """ |
---|
[d83549c] | 371 | if menu == None: |
---|
| 372 | menu = self.edit_custom_model |
---|
[5d1c1f4] | 373 | list_fnames = os.listdir(models.find_plugins_dir()) |
---|
[d2843a9] | 374 | list_fnames.sort() |
---|
| 375 | for f_item in list_fnames: |
---|
| 376 | name = os.path.basename(f_item) |
---|
[5d1c1f4] | 377 | toks = os.path.splitext(name) |
---|
[f32d144] | 378 | if toks[-1] == '.py' and not toks[0] == '__init__': |
---|
[b25caad] | 379 | if menu == self.edit_custom_model: |
---|
[f32d144] | 380 | if toks[0] == 'easy_sum_of_p1_p2': |
---|
[b25caad] | 381 | continue |
---|
| 382 | submenu = self.edit_menu |
---|
| 383 | else: |
---|
| 384 | submenu = self.delete_menu |
---|
[5d1c1f4] | 385 | has_file = False |
---|
[b25caad] | 386 | for item in submenu.GetMenuItems(): |
---|
| 387 | if name == submenu.GetLabel(item.GetId()): |
---|
[5d1c1f4] | 388 | has_file = True |
---|
| 389 | if not has_file: |
---|
| 390 | id = wx.NewId() |
---|
[f32d144] | 391 | submenu.Append(id, name) |
---|
| 392 | wx.EVT_MENU(owner, id, menu) |
---|
[5d1c1f4] | 393 | has_file = False |
---|
| 394 | |
---|
[056b569] | 395 | def put_icon(self, frame): |
---|
| 396 | """ |
---|
| 397 | Put icon in the frame title bar |
---|
| 398 | """ |
---|
| 399 | if hasattr(frame, "IsIconized"): |
---|
| 400 | if not frame.IsIconized(): |
---|
| 401 | try: |
---|
| 402 | icon = self.parent.GetIcon() |
---|
| 403 | frame.SetIcon(icon) |
---|
| 404 | except: |
---|
[f32d144] | 405 | pass |
---|
| 406 | |
---|
[51d47b5] | 407 | def on_add_sim_page(self, event): |
---|
[ed2ea6a] | 408 | """ |
---|
[5062bbf] | 409 | Create a page to access simultaneous fit option |
---|
[ed2ea6a] | 410 | """ |
---|
[fa02d95] | 411 | id = event.GetId() |
---|
| 412 | caption = "Simultaneous Fit" |
---|
| 413 | page = self.sim_page |
---|
| 414 | if id == self.id_batchfit: |
---|
[7b1ca97] | 415 | caption = "Combined Batch" |
---|
[fa02d95] | 416 | page = self.batch_page |
---|
| 417 | |
---|
| 418 | def set_focus_page(page): |
---|
| 419 | page.Show(True) |
---|
| 420 | page.Refresh() |
---|
| 421 | page.SetFocus() |
---|
[581d1d4] | 422 | self.parent._mgr.Update() |
---|
[fa02d95] | 423 | msg = "%s already opened\n" % str(page.window_caption) |
---|
| 424 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
| 425 | |
---|
| 426 | if page != None: |
---|
| 427 | return set_focus_page(page) |
---|
| 428 | if caption == "Simultaneous Fit": |
---|
| 429 | self.sim_page = self.fit_panel.add_sim_page(caption=caption) |
---|
| 430 | else: |
---|
| 431 | self.batch_page = self.fit_panel.add_sim_page(caption=caption) |
---|
[b28717b] | 432 | |
---|
[d89f09b] | 433 | def help(self, evt): |
---|
| 434 | """ |
---|
[f32d144] | 435 | Show a general help dialog. |
---|
[d89f09b] | 436 | """ |
---|
[484faf7] | 437 | from help_panel import HelpWindow |
---|
[f32d144] | 438 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
[34ffeed] | 439 | if hasattr(frame, "IsIconized"): |
---|
| 440 | if not frame.IsIconized(): |
---|
| 441 | try: |
---|
| 442 | icon = self.parent.GetIcon() |
---|
| 443 | frame.SetIcon(icon) |
---|
| 444 | except: |
---|
[f32d144] | 445 | pass |
---|
[2268d10] | 446 | frame.Show(True) |
---|
| 447 | |
---|
[6bbeacd4] | 448 | def get_context_menu(self, plotpanel=None): |
---|
[d89f09b] | 449 | """ |
---|
[5062bbf] | 450 | Get the context menu items available for P(r).them allow fitting option |
---|
| 451 | for Data2D and Data1D only. |
---|
| 452 | |
---|
| 453 | :param graph: the Graph object to which we attach the context menu |
---|
| 454 | |
---|
| 455 | :return: a list of menu items with call-back function |
---|
| 456 | |
---|
[f32d144] | 457 | :note: if Data1D was generated from Theory1D |
---|
[5062bbf] | 458 | the fitting option is not allowed |
---|
| 459 | |
---|
[d89f09b] | 460 | """ |
---|
[6bbeacd4] | 461 | graph = plotpanel.graph |
---|
[484faf7] | 462 | fit_option = "Select data for fitting" |
---|
| 463 | fit_hint = "Dialog with fitting parameters " |
---|
[6bbeacd4] | 464 | |
---|
| 465 | if graph.selected_plottable not in plotpanel.plots: |
---|
| 466 | return [] |
---|
| 467 | item = plotpanel.plots[graph.selected_plottable] |
---|
[f32d144] | 468 | if item.__class__.__name__ is "Data2D": |
---|
| 469 | if hasattr(item, "is_data"): |
---|
[6bbeacd4] | 470 | if item.is_data: |
---|
| 471 | return [[fit_option, fit_hint, self._onSelect]] |
---|
| 472 | else: |
---|
| 473 | return [] |
---|
| 474 | return [[fit_option, fit_hint, self._onSelect]] |
---|
| 475 | else: |
---|
| 476 | |
---|
| 477 | # if is_data is true , this in an actual data loaded |
---|
| 478 | #else it is a data created from a theory model |
---|
[f32d144] | 479 | if hasattr(item, "is_data"): |
---|
[6bbeacd4] | 480 | if item.is_data: |
---|
[f32d144] | 481 | return [[fit_option, fit_hint, self._onSelect]] |
---|
[6bbeacd4] | 482 | else: |
---|
[f32d144] | 483 | return [] |
---|
| 484 | return [] |
---|
[d89f09b] | 485 | |
---|
| 486 | def get_panels(self, parent): |
---|
| 487 | """ |
---|
[5062bbf] | 488 | Create and return a list of panel objects |
---|
[d89f09b] | 489 | """ |
---|
| 490 | self.parent = parent |
---|
[75fbd17] | 491 | #self.parent.Bind(EVT_FITSTATE_UPDATE, self.on_set_state_helper) |
---|
[d89f09b] | 492 | # Creation of the fit panel |
---|
[6bbeacd4] | 493 | self.fit_panel = FitPanel(parent=self.parent, manager=self) |
---|
| 494 | self.on_add_new_page(event=None) |
---|
[f93dfcb] | 495 | #Set the manager for the main panel |
---|
[d89f09b] | 496 | self.fit_panel.set_manager(self) |
---|
| 497 | # List of windows used for the perspective |
---|
| 498 | self.perspective = [] |
---|
| 499 | self.perspective.append(self.fit_panel.window_name) |
---|
[6bbeacd4] | 500 | |
---|
[3b19ac9] | 501 | #index number to create random model name |
---|
| 502 | self.index_model = 0 |
---|
[f32d144] | 503 | self.index_theory = 0 |
---|
[32673ac] | 504 | self.parent.Bind(EVT_SLICER_PANEL, self._on_slicer_event) |
---|
[848a2ef] | 505 | self.parent.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) |
---|
[f32d144] | 506 | self.parent._mgr.Bind(wx.aui.EVT_AUI_PANE_CLOSE,self._onclearslicer) |
---|
[3bf4a032] | 507 | #Create reader when fitting panel are created |
---|
[f32d144] | 508 | self.state_reader = Reader(self.set_state) |
---|
| 509 | #append that reader to list of available reader |
---|
[3bf4a032] | 510 | loader = Loader() |
---|
| 511 | loader.associate_file_reader(".fitv", self.state_reader) |
---|
[f93dfcb] | 512 | #Send the fitting panel to guiframe |
---|
[f32d144] | 513 | self.mypanels.append(self.fit_panel) |
---|
[568e1a5] | 514 | return self.mypanels |
---|
[232c3db] | 515 | |
---|
[90a7bbd] | 516 | def clear_panel(self): |
---|
| 517 | """ |
---|
| 518 | """ |
---|
[81f00d7] | 519 | self.fit_panel.clear_panel() |
---|
[90a7bbd] | 520 | |
---|
[1610976] | 521 | def set_default_perspective(self): |
---|
| 522 | """ |
---|
[5062bbf] | 523 | Call back method that True to notify the parent that the current plug-in |
---|
[f32d144] | 524 | can be set as default perspective. |
---|
| 525 | when returning False, the plug-in is not candidate for an automatic |
---|
[5062bbf] | 526 | default perspective setting |
---|
[1610976] | 527 | """ |
---|
| 528 | return True |
---|
[a0da535] | 529 | |
---|
[17553ae] | 530 | def delete_data(self, data): |
---|
[8ee56a9] | 531 | """ |
---|
| 532 | delete the given data from panel |
---|
| 533 | """ |
---|
| 534 | self.fit_panel.delete_data(data) |
---|
| 535 | |
---|
[e88ebfd] | 536 | def set_data(self, data_list=None): |
---|
[a0da535] | 537 | """ |
---|
| 538 | receive a list of data to fit |
---|
| 539 | """ |
---|
[b2d9826] | 540 | if data_list is None: |
---|
| 541 | data_list = [] |
---|
[c26a64b] | 542 | selected_data_list = [] |
---|
[cc31608] | 543 | if self.batch_on: |
---|
| 544 | page = self.add_fit_page(data=data_list) |
---|
[c26a64b] | 545 | else: |
---|
[cc31608] | 546 | if len(data_list) > MAX_NBR_DATA: |
---|
| 547 | from fitting_widgets import DataDialog |
---|
| 548 | dlg = DataDialog(data_list=data_list, nb_data=MAX_NBR_DATA) |
---|
| 549 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 550 | selected_data_list = dlg.get_data() |
---|
| 551 | dlg.Destroy() |
---|
| 552 | |
---|
| 553 | else: |
---|
| 554 | selected_data_list = data_list |
---|
| 555 | try: |
---|
[5e48acb] | 556 | group_id = wx.NewId() |
---|
[cc31608] | 557 | for data in selected_data_list: |
---|
[5e48acb] | 558 | if data is not None: |
---|
[33b7954] | 559 | # 2D has no same group_id |
---|
| 560 | if data.__class__.__name__ == 'Data2D': |
---|
| 561 | group_id = wx.NewId() |
---|
[5e48acb] | 562 | data.group_id = group_id |
---|
[14774db] | 563 | if group_id not in data.list_group_id: |
---|
| 564 | data.list_group_id.append(group_id) |
---|
| 565 | page = self.add_fit_page(data=[data]) |
---|
[cc31608] | 566 | except: |
---|
| 567 | msg = "Fitting Set_data: " + str(sys.exc_value) |
---|
| 568 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[1b1bbf9] | 569 | |
---|
| 570 | def set_top_panel(self): |
---|
| 571 | """ |
---|
| 572 | Close default (welcome) panel |
---|
| 573 | """ |
---|
| 574 | if 'default' in self.parent.panels: |
---|
| 575 | self.parent.on_close_welcome_panel() |
---|
| 576 | |
---|
[5e48acb] | 577 | |
---|
[f32d144] | 578 | def set_theory(self, theory_list=None): |
---|
[e88ebfd] | 579 | """ |
---|
| 580 | """ |
---|
| 581 | #set the model state for a given theory_state: |
---|
| 582 | for item in theory_list: |
---|
| 583 | try: |
---|
| 584 | _, theory_state = item |
---|
| 585 | self.fit_panel.set_model_state(theory_state) |
---|
| 586 | except: |
---|
| 587 | msg = "Fitting: cannot deal with the theory received" |
---|
| 588 | logging.error("set_theory " + msg + "\n" + str(sys.exc_value)) |
---|
[f32d144] | 589 | wx.PostEvent(self.parent, |
---|
| 590 | StatusEvent(status=msg, info="error")) |
---|
[a0da535] | 591 | |
---|
[b63dc6e] | 592 | def set_state(self, state=None, datainfo=None, format=None): |
---|
[f83b94a] | 593 | """ |
---|
[5062bbf] | 594 | Call-back method for the fit page state reader. |
---|
[8897d66] | 595 | This method is called when a .fitv/.svs file is loaded. |
---|
[5062bbf] | 596 | |
---|
[8897d66] | 597 | : param state: PageState object |
---|
| 598 | : param datainfo: data |
---|
[f83b94a] | 599 | """ |
---|
[90a7bbd] | 600 | #state = self.state_reader.get_state() |
---|
[b63dc6e] | 601 | if state != None: |
---|
[4bee68d] | 602 | state = state.clone() |
---|
[b63dc6e] | 603 | # store fitting state in temp_state |
---|
[f32d144] | 604 | self.temp_state.append(state) |
---|
[b63dc6e] | 605 | else: |
---|
| 606 | self.temp_state = [] |
---|
[ef16f59] | 607 | # index to start with for a new set_state |
---|
| 608 | self.state_index = 0 |
---|
[b63dc6e] | 609 | # state file format |
---|
| 610 | self.sfile_ext = format |
---|
[75fbd17] | 611 | |
---|
| 612 | self.on_set_state_helper(event=None) |
---|
[ef16f59] | 613 | |
---|
[f32d144] | 614 | def on_set_state_helper(self, event=None): |
---|
[4da35bc] | 615 | """ |
---|
[f32d144] | 616 | Set_state_helper. This actually sets state |
---|
[0fd2f27] | 617 | after plotting data from state file. |
---|
[ef16f59] | 618 | |
---|
[f32d144] | 619 | : event: FitStateUpdateEvent called |
---|
[0fd2f27] | 620 | by dataloader.plot_data from guiframe |
---|
[4da35bc] | 621 | """ |
---|
[7a07864] | 622 | if len(self.temp_state) == 0: |
---|
[f32d144] | 623 | if self.state_index == 0 and len(self.mypanels) <= 0 \ |
---|
| 624 | and self.sfile_ext == '.svs': |
---|
[6d8bad4] | 625 | #TODO: Why was the following line left in the code |
---|
| 626 | # if add_default_pages doesn't exist? |
---|
| 627 | try: |
---|
| 628 | self.fit_panel.add_default_pages() |
---|
| 629 | except: |
---|
| 630 | print sys.exc_value |
---|
[b63dc6e] | 631 | self.temp_state = [] |
---|
| 632 | self.state_index = 0 |
---|
[4da35bc] | 633 | return |
---|
[3eb2811] | 634 | |
---|
[ef16f59] | 635 | try: |
---|
| 636 | # Load fitting state |
---|
| 637 | state = self.temp_state[self.state_index] |
---|
[3eb2811] | 638 | #panel state should have model selection to set_state |
---|
| 639 | if state.formfactorcombobox != None: |
---|
| 640 | #set state |
---|
[75fbd17] | 641 | data = self.parent.create_gui_data(state.data) |
---|
| 642 | data.group_id = state.data.group_id |
---|
[f32d144] | 643 | self.parent.add_data(data_list={data.id: data}) |
---|
[75fbd17] | 644 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 645 | title=data.title)) |
---|
[f95301b] | 646 | #need to be fix later make sure we are sendind guiframe.data |
---|
| 647 | #to panel |
---|
| 648 | state.data = data |
---|
[f32d144] | 649 | page = self.fit_panel.set_state(state) |
---|
[3eb2811] | 650 | else: |
---|
| 651 | #just set data because set_state won't work |
---|
[75fbd17] | 652 | data = self.parent.create_gui_data(state.data) |
---|
| 653 | data.group_id = state.data.group_id |
---|
[f32d144] | 654 | self.parent.add_data(data_list={data.id: data}) |
---|
[f95301b] | 655 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 656 | title=data.title)) |
---|
[cc31608] | 657 | page = self.add_fit_page([data]) |
---|
[2dbffcc] | 658 | caption = page.window_caption |
---|
[f32d144] | 659 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
[2dbffcc] | 660 | caption=caption) |
---|
[f32d144] | 661 | self.mypanels.append(page) |
---|
[3eb2811] | 662 | |
---|
[9b18735] | 663 | # get ready for the next set_state |
---|
[ef16f59] | 664 | self.state_index += 1 |
---|
[9b18735] | 665 | |
---|
[f32d144] | 666 | #reset state variables to default when all set_state is finished. |
---|
[ef16f59] | 667 | if len(self.temp_state) == self.state_index: |
---|
[9b18735] | 668 | |
---|
[ef16f59] | 669 | self.temp_state = [] |
---|
[b63dc6e] | 670 | #self.state_index = 0 |
---|
[ef16f59] | 671 | # Make sure the user sees the fitting panel after loading |
---|
[f32d144] | 672 | #self.parent.set_perspective(self.perspective) |
---|
[75fbd17] | 673 | self.on_perspective(event=None) |
---|
[ef16f59] | 674 | except: |
---|
[f32d144] | 675 | self.state_index = 0 |
---|
[8897d66] | 676 | self.temp_state = [] |
---|
[ef16f59] | 677 | raise |
---|
[1b14795] | 678 | |
---|
| 679 | def set_param2fit(self, uid, param2fit): |
---|
| 680 | """ |
---|
| 681 | Set the list of param names to fit for fitprobelm |
---|
| 682 | """ |
---|
| 683 | self.page_finder[uid].set_param2fit(param2fit) |
---|
[5e48acb] | 684 | |
---|
| 685 | def set_graph_id(self, uid, graph_id): |
---|
| 686 | """ |
---|
| 687 | Set graph_id for fitprobelm |
---|
| 688 | """ |
---|
| 689 | self.page_finder[uid].set_graph_id(graph_id) |
---|
| 690 | |
---|
| 691 | def get_graph_id(self, uid): |
---|
| 692 | """ |
---|
| 693 | Set graph_id for fitprobelm |
---|
| 694 | """ |
---|
[f32d144] | 695 | return self.page_finder[uid].get_graph_id() |
---|
[5e48acb] | 696 | |
---|
[f32d144] | 697 | def save_fit_state(self, filepath, fitstate): |
---|
[f83b94a] | 698 | """ |
---|
[5062bbf] | 699 | save fit page state into file |
---|
[f83b94a] | 700 | """ |
---|
| 701 | self.state_reader.write(filename=filepath, fitstate=fitstate) |
---|
[55bb249c] | 702 | |
---|
[f7ef313] | 703 | def set_fit_weight(self, uid, flag, is2d=False, fid=None): |
---|
[55bb249c] | 704 | """ |
---|
| 705 | Set the fit weights of a given page for all |
---|
[f32d144] | 706 | its data by default. If fid is provide then set the range |
---|
[55bb249c] | 707 | only for the data with fid as id |
---|
| 708 | :param uid: id corresponding to a fit page |
---|
| 709 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 710 | :param weight: current dy data |
---|
| 711 | """ |
---|
| 712 | if uid in self.page_finder.keys(): |
---|
[f7ef313] | 713 | self.page_finder[uid].set_weight(flag=flag, is2d=is2d) |
---|
[55bb249c] | 714 | |
---|
[2dbffcc] | 715 | def set_fit_range(self, uid, qmin, qmax, fid=None): |
---|
[ca7a626] | 716 | """ |
---|
[2dbffcc] | 717 | Set the fitting range of a given page for all |
---|
[f32d144] | 718 | its data by default. If fid is provide then set the range |
---|
[2dbffcc] | 719 | only for the data with fid as id |
---|
| 720 | :param uid: id corresponding to a fit page |
---|
| 721 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 722 | :param qmin: minimum value of the fit range |
---|
| 723 | :param qmax: maximum value of the fit range |
---|
[ca7a626] | 724 | """ |
---|
[2dbffcc] | 725 | if uid in self.page_finder.keys(): |
---|
[f6933b8] | 726 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax, fid=fid) |
---|
| 727 | |
---|
[f32d144] | 728 | def schedule_for_fit(self, value=0, uid=None): |
---|
[948add7] | 729 | """ |
---|
[5062bbf] | 730 | Set the fit problem field to 0 or 1 to schedule that problem to fit. |
---|
[f32d144] | 731 | Schedule the specified fitproblem or get the fit problem related to |
---|
[5062bbf] | 732 | the current page and set value. |
---|
[f32d144] | 733 | :param value: integer 0 or 1 |
---|
[2dbffcc] | 734 | :param uid: the id related to a page contaning fitting information |
---|
| 735 | """ |
---|
[f32d144] | 736 | if uid in self.page_finder.keys(): |
---|
[66ff250] | 737 | self.page_finder[uid].schedule_tofit(value) |
---|
[ca7a626] | 738 | |
---|
[d89f09b] | 739 | def get_page_finder(self): |
---|
[5062bbf] | 740 | """ |
---|
| 741 | return self.page_finder used also by simfitpage.py |
---|
[f32d144] | 742 | """ |
---|
| 743 | return self.page_finder |
---|
[d89f09b] | 744 | |
---|
[f32d144] | 745 | def set_page_finder(self, modelname, names, values): |
---|
[d89f09b] | 746 | """ |
---|
[5062bbf] | 747 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
| 748 | |
---|
[f32d144] | 749 | :param modelname: the name ot the model for with the parameter |
---|
[0fd2f27] | 750 | has to reset |
---|
[5062bbf] | 751 | :param value: can be a string in this case. |
---|
| 752 | :param names: the paramter name |
---|
| 753 | |
---|
| 754 | :note: expecting park used for fit. |
---|
| 755 | |
---|
[f32d144] | 756 | """ |
---|
[66ff250] | 757 | sim_page_id = self.sim_page.uid |
---|
| 758 | for uid, value in self.page_finder.iteritems(): |
---|
[fa02d95] | 759 | if uid != sim_page_id and uid != self.batch_page.uid: |
---|
[6bbeacd4] | 760 | list = value.get_model() |
---|
[f93dfcb] | 761 | model = list[0] |
---|
[6bbeacd4] | 762 | if model.name == modelname: |
---|
| 763 | value.set_model_param(names, values) |
---|
[d89f09b] | 764 | break |
---|
[2db1d66] | 765 | |
---|
[f32d144] | 766 | def split_string(self, item): |
---|
[d89f09b] | 767 | """ |
---|
[5062bbf] | 768 | receive a word containing dot and split it. used to split parameterset |
---|
| 769 | name into model name and parameter name example: :: |
---|
| 770 | |
---|
[3b19ac9] | 771 | paramaterset (item) = M1.A |
---|
[5062bbf] | 772 | Will return model_name = M1 , parameter name = A |
---|
| 773 | |
---|
[d89f09b] | 774 | """ |
---|
[6d8bad4] | 775 | if item.find(".") >= 0: |
---|
[7c8d3093] | 776 | param_names = re.split("\.", item) |
---|
[f32d144] | 777 | model_name = param_names[0] |
---|
[6d91073] | 778 | ##Assume max len is 3; eg., M0.radius.width |
---|
| 779 | if len(param_names) == 3: |
---|
[7c8d3093] | 780 | param_name = param_names[1] + "." + param_names[2] |
---|
[6d91073] | 781 | else: |
---|
[f32d144] | 782 | param_name = param_names[1] |
---|
[7c8d3093] | 783 | return model_name, param_name |
---|
[2296316] | 784 | |
---|
| 785 | def set_ftol(self, ftol=None): |
---|
| 786 | """ |
---|
[f32d144] | 787 | Set ftol: Relative error desired in the sum of chi squares. |
---|
[2296316] | 788 | """ |
---|
| 789 | # check if it is flaot |
---|
| 790 | try: |
---|
| 791 | f_tol = float(ftol) |
---|
| 792 | except: |
---|
| 793 | # default |
---|
[2c6b224] | 794 | f_tol = SANS_F_TOL |
---|
[2296316] | 795 | |
---|
| 796 | self.ftol = f_tol |
---|
[2c6b224] | 797 | # update ftol menu help strings |
---|
| 798 | ftol_help = "Change the current FTolerance (=%s) " % str(self.ftol) |
---|
[f32d144] | 799 | ftol_help += "of Simple FitEngine..." |
---|
[dafc36f] | 800 | if self.menu1 != None: |
---|
| 801 | self.menu1.SetHelpString(self.id_tol, ftol_help) |
---|
[2c6b224] | 802 | |
---|
| 803 | def show_ftol_dialog(self, event=None): |
---|
| 804 | """ |
---|
| 805 | Dialog to select ftol for Scipy |
---|
| 806 | """ |
---|
| 807 | #if event != None: |
---|
| 808 | # event.Skip() |
---|
| 809 | from ftol_dialog import ChangeFtol |
---|
| 810 | panel = ChangeFtol(self.parent, self) |
---|
| 811 | panel.ShowModal() |
---|
| 812 | |
---|
[66ff250] | 813 | def stop_fit(self, uid): |
---|
[ed2ea6a] | 814 | """ |
---|
[5062bbf] | 815 | Stop the fit engine |
---|
[ed2ea6a] | 816 | """ |
---|
[66ff250] | 817 | if uid in self.fit_thread_list.keys(): |
---|
| 818 | calc_fit = self.fit_thread_list[uid] |
---|
| 819 | if calc_fit is not None and calc_fit.isrunning(): |
---|
| 820 | calc_fit.stop() |
---|
| 821 | msg = "Fit stop!" |
---|
| 822 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[3ddc2c4] | 823 | del self.fit_thread_list[uid] |
---|
[66ff250] | 824 | #set the fit button label of page when fit stop is trigger from |
---|
| 825 | #simultaneous fit pane |
---|
[fa02d95] | 826 | sim_flag = self.sim_page is not None and uid == self.sim_page.uid |
---|
| 827 | batch_flag = self.batch_page is not None and uid == self.batch_page.uid |
---|
| 828 | if sim_flag or batch_flag: |
---|
[66ff250] | 829 | for uid, value in self.page_finder.iteritems(): |
---|
| 830 | if value.get_scheduled() == 1: |
---|
| 831 | if uid in self.fit_panel.opened_pages.keys(): |
---|
| 832 | panel = self.fit_panel.opened_pages[uid] |
---|
[fa02d95] | 833 | panel._on_fit_complete() |
---|
[6bbeacd4] | 834 | |
---|
[2dbffcc] | 835 | def set_smearer(self, uid, smearer, fid, qmin=None, qmax=None, draw=True, |
---|
| 836 | enable_smearer=False): |
---|
| 837 | """ |
---|
| 838 | Get a smear object and store it to a fit problem of fid as id. If proper |
---|
| 839 | flag is enable , will plot the theory with smearing information. |
---|
| 840 | |
---|
| 841 | :param smearer: smear object to allow smearing data of id fid |
---|
| 842 | :param enable_smearer: Define whether or not all (data, model) contained |
---|
| 843 | in the structure of id uid will be smeared before fitting. |
---|
| 844 | :param qmin: the maximum value of the theory plotting range |
---|
| 845 | :param qmax: the maximum value of the theory plotting range |
---|
| 846 | :param draw: Determine if the theory needs to be plot |
---|
[f32d144] | 847 | """ |
---|
[66ff250] | 848 | if uid not in self.page_finder.keys(): |
---|
[2dbffcc] | 849 | return |
---|
| 850 | self.page_finder[uid].enable_smearing(flag=enable_smearer) |
---|
| 851 | self.page_finder[uid].set_smearer(smearer, fid=fid) |
---|
[6bbeacd4] | 852 | if draw: |
---|
| 853 | ## draw model 1D with smeared data |
---|
[f32d144] | 854 | data = self.page_finder[uid].get_fit_data(fid=fid) |
---|
[2dbffcc] | 855 | if data is None: |
---|
| 856 | msg = "set_mearer requires at least data.\n" |
---|
| 857 | msg += "Got data = %s .\n" % str(data) |
---|
[269df2b] | 858 | return |
---|
| 859 | #raise ValueError, msg |
---|
[2dbffcc] | 860 | model = self.page_finder[uid].get_model(fid=fid) |
---|
[6bbeacd4] | 861 | if model is None: |
---|
| 862 | return |
---|
[2dbffcc] | 863 | enable1D = issubclass(data.__class__, Data1D) |
---|
| 864 | enable2D = issubclass(data.__class__, Data2D) |
---|
[6bbeacd4] | 865 | ## if user has already selected a model to plot |
---|
| 866 | ## redraw the model with data smeared |
---|
[2dbffcc] | 867 | smear = self.page_finder[uid].get_smearer(fid=fid) |
---|
[342dc442] | 868 | |
---|
| 869 | # compute weight for the current data |
---|
| 870 | weight = self.page_finder[uid].get_weight(fid=fid) |
---|
| 871 | |
---|
[66ff250] | 872 | self.draw_model(model=model, data=data, page_id=uid, smearer=smear, |
---|
[ba1f0b2] | 873 | enable1D=enable1D, enable2D=enable2D, |
---|
[342dc442] | 874 | qmin=qmin, qmax=qmax, weight=weight) |
---|
[f6aee42] | 875 | self._mac_sleep(0.2) |
---|
| 876 | |
---|
| 877 | def _mac_sleep(self, sec=0.2): |
---|
[2dbffcc] | 878 | """ |
---|
| 879 | Give sleep to MAC |
---|
| 880 | """ |
---|
[f6aee42] | 881 | if ON_MAC: |
---|
[f32d144] | 882 | time.sleep(sec) |
---|
[2dbffcc] | 883 | |
---|
[66ff250] | 884 | def draw_model(self, model, page_id, data=None, smearer=None, |
---|
[67e258c] | 885 | enable1D=True, enable2D=False, |
---|
[5ef55d2] | 886 | state=None, |
---|
[f64a4b7] | 887 | fid=None, |
---|
[fa65e99] | 888 | toggle_mode_on=False, |
---|
[f32d144] | 889 | qmin=None, qmax=None, |
---|
[e3f6ef5] | 890 | update_chisqr=True, weight=None, source='model'): |
---|
[ca7a626] | 891 | """ |
---|
[5062bbf] | 892 | Draw model. |
---|
| 893 | |
---|
| 894 | :param model: the model to draw |
---|
| 895 | :param name: the name of the model to draw |
---|
| 896 | :param data: the data on which the model is based to be drawn |
---|
| 897 | :param description: model's description |
---|
| 898 | :param enable1D: if true enable drawing model 1D |
---|
| 899 | :param enable2D: if true enable drawing model 2D |
---|
| 900 | :param qmin: Range's minimum value to draw model |
---|
| 901 | :param qmax: Range's maximum value to draw model |
---|
| 902 | :param qstep: number of step to divide the x and y-axis |
---|
[2296316] | 903 | :param update_chisqr: update chisqr [bool] |
---|
[ed2ea6a] | 904 | |
---|
[d89f09b] | 905 | """ |
---|
[62f851f] | 906 | #self.weight = weight |
---|
[f32d144] | 907 | if issubclass(data.__class__, Data1D) or not enable2D: |
---|
[f72333f] | 908 | ## draw model 1D with no loaded data |
---|
[f32d144] | 909 | self._draw_model1D(model=model, |
---|
[67e258c] | 910 | data=data, |
---|
[66ff250] | 911 | page_id=page_id, |
---|
[f32d144] | 912 | enable1D=enable1D, |
---|
[67e258c] | 913 | smearer=smearer, |
---|
| 914 | qmin=qmin, |
---|
[f32d144] | 915 | qmax=qmax, |
---|
[f64a4b7] | 916 | fid=fid, |
---|
[62f851f] | 917 | weight=weight, |
---|
[fa65e99] | 918 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 919 | state=state, |
---|
[e3f6ef5] | 920 | update_chisqr=update_chisqr, |
---|
| 921 | source=source) |
---|
[f32d144] | 922 | else: |
---|
[f72333f] | 923 | ## draw model 2D with no initial data |
---|
[ba1f0b2] | 924 | self._draw_model2D(model=model, |
---|
[66ff250] | 925 | page_id=page_id, |
---|
[67e258c] | 926 | data=data, |
---|
| 927 | enable2D=enable2D, |
---|
| 928 | smearer=smearer, |
---|
| 929 | qmin=qmin, |
---|
| 930 | qmax=qmax, |
---|
[f64a4b7] | 931 | fid=fid, |
---|
[d682c92] | 932 | weight=weight, |
---|
[5ef55d2] | 933 | state=state, |
---|
[fa65e99] | 934 | toggle_mode_on=toggle_mode_on, |
---|
[e3f6ef5] | 935 | update_chisqr=update_chisqr, |
---|
| 936 | source=source) |
---|
[2db1d66] | 937 | |
---|
[922497f] | 938 | def onFit(self, uid): |
---|
[ca7a626] | 939 | """ |
---|
[2dbffcc] | 940 | Get series of data, model, associates parameters and range and send then |
---|
[f32d144] | 941 | to series of fit engines. Fit data and model, display result to |
---|
| 942 | corresponding panels. |
---|
[2dbffcc] | 943 | :param uid: id related to the panel currently calling this fit function. |
---|
[ca7a626] | 944 | """ |
---|
[31469d50] | 945 | flag = True |
---|
[f32d144] | 946 | ## count the number of fitproblem schedule to fit |
---|
[67e258c] | 947 | fitproblem_count = 0 |
---|
[66ff250] | 948 | for value in self.page_finder.values(): |
---|
[67e258c] | 949 | if value.get_scheduled() == 1: |
---|
[bb18ef1] | 950 | fitproblem_count += 1 |
---|
[f32d144] | 951 | self._gui_engine = self._return_engine_type() |
---|
| 952 | self.fitproblem_count = fitproblem_count |
---|
[67e258c] | 953 | if self._fit_engine == "park": |
---|
| 954 | engineType = "Simultaneous Fit" |
---|
[ca7a626] | 955 | else: |
---|
[67e258c] | 956 | engineType = "Single Fit" |
---|
[f32d144] | 957 | fitter_list = [] |
---|
| 958 | sim_fitter = None |
---|
[284f6fe] | 959 | is_single_fit = True |
---|
[fa02d95] | 960 | batch_on = False |
---|
[922497f] | 961 | if self.sim_page is not None and self.sim_page.uid == uid: |
---|
[f32d144] | 962 | #simulatanous fit only one engine need to be created |
---|
[284f6fe] | 963 | ## if simultaneous fit change automatically the engine to park |
---|
[f32d144] | 964 | self._on_change_engine(engine='park') |
---|
| 965 | sim_fitter = Fit(self._fit_engine) |
---|
[fa02d95] | 966 | sim_fitter.fitter_id = self.sim_page.uid |
---|
[f32d144] | 967 | fitter_list.append(sim_fitter) |
---|
[284f6fe] | 968 | is_single_fit = False |
---|
[fa02d95] | 969 | batch_on = self.sim_page.batch_on |
---|
[7db52f1] | 970 | |
---|
[f32d144] | 971 | self.fitproblem_count = fitproblem_count |
---|
[284f6fe] | 972 | if self._fit_engine == "park": |
---|
| 973 | engineType = "Simultaneous Fit" |
---|
| 974 | else: |
---|
| 975 | engineType = "Single Fit" |
---|
[922497f] | 976 | |
---|
[67e258c] | 977 | self.current_pg = None |
---|
[66ff250] | 978 | list_page_id = [] |
---|
[922497f] | 979 | fit_id = 0 |
---|
[33dd2e5] | 980 | batch_inputs = {} |
---|
| 981 | batch_outputs = {} |
---|
[66ff250] | 982 | for page_id, value in self.page_finder.iteritems(): |
---|
[dc613d6] | 983 | # For simulfit (uid give with None), do for-loop |
---|
| 984 | # if uid is specified (singlefit), do it only on the page. |
---|
| 985 | if engineType == "Single Fit": |
---|
[fa02d95] | 986 | #combine more than 1 batch page on single mode |
---|
| 987 | if self.batch_page is None or self.batch_page.uid != uid: |
---|
| 988 | if page_id != uid: |
---|
| 989 | continue |
---|
[d89f09b] | 990 | try: |
---|
[67e258c] | 991 | if value.get_scheduled() == 1: |
---|
[f64a4b7] | 992 | value.nbr_residuals_computed = 0 |
---|
[f93dfcb] | 993 | #Get list of parameters name to fit |
---|
[d89f09b] | 994 | pars = [] |
---|
| 995 | templist = [] |
---|
[66ff250] | 996 | page = self.fit_panel.get_page_by_id(page_id) |
---|
[f32d144] | 997 | self.set_fit_weight(uid=page.uid, |
---|
[7ee79cb] | 998 | flag=page.get_weight_flag(), |
---|
[f32d144] | 999 | is2d=page._is_2D()) |
---|
[3b19ac9] | 1000 | templist = page.get_param_list() |
---|
[f32d144] | 1001 | flag = page._update_paramv_on_fit() |
---|
[31469d50] | 1002 | if not flag: |
---|
| 1003 | msg = "Fitting range or parameter values are" |
---|
[f32d144] | 1004 | msg += " invalid in %s" % \ |
---|
[31469d50] | 1005 | page.window_caption |
---|
[f32d144] | 1006 | wx.PostEvent(page.parent.parent, |
---|
| 1007 | StatusEvent(status=msg, info="error", |
---|
[31469d50] | 1008 | type="stop")) |
---|
| 1009 | return flag |
---|
[d89f09b] | 1010 | for element in templist: |
---|
[513115c] | 1011 | name = str(element[1]) |
---|
[ca7a626] | 1012 | pars.append(name) |
---|
[7bd6cfae] | 1013 | fitproblem_list = value.values() |
---|
| 1014 | for fitproblem in fitproblem_list: |
---|
[922497f] | 1015 | if sim_fitter is None: |
---|
[f32d144] | 1016 | fitter = Fit(self._fit_engine) |
---|
[fa02d95] | 1017 | fitter.fitter_id = page_id |
---|
[f32d144] | 1018 | self._fit_helper(fitproblem=fitproblem, |
---|
| 1019 | pars=pars, |
---|
| 1020 | fitter=fitter, |
---|
| 1021 | fit_id=fit_id, |
---|
| 1022 | batch_inputs=batch_inputs, |
---|
| 1023 | batch_outputs=batch_outputs) |
---|
| 1024 | fitter_list.append(fitter) |
---|
[922497f] | 1025 | else: |
---|
| 1026 | fitter = sim_fitter |
---|
[f32d144] | 1027 | self._fit_helper(fitproblem=fitproblem, |
---|
| 1028 | pars=pars, |
---|
| 1029 | fitter=fitter, |
---|
| 1030 | fit_id=fit_id, |
---|
| 1031 | batch_inputs=batch_inputs, |
---|
| 1032 | batch_outputs=batch_outputs) |
---|
[922497f] | 1033 | fit_id += 1 |
---|
[66ff250] | 1034 | list_page_id.append(page_id) |
---|
| 1035 | current_page_id = page_id |
---|
[922497f] | 1036 | value.clear_model_param() |
---|
[d89f09b] | 1037 | except: |
---|
[31469d50] | 1038 | flag = False |
---|
[f32d144] | 1039 | msg = "%s error: %s" % (engineType, sys.exc_value) |
---|
[ba1f0b2] | 1040 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 1041 | type="stop")) |
---|
[31469d50] | 1042 | return flag |
---|
[e54d2c32] | 1043 | ## If a thread is already started, stop it |
---|
| 1044 | #if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
| 1045 | # self.calc_fit.stop() |
---|
[a2cb1f9] | 1046 | msg = "Fitting is in progress..." |
---|
[f32d144] | 1047 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) |
---|
[a2cb1f9] | 1048 | |
---|
| 1049 | #Handler used for park engine displayed message |
---|
[66ff250] | 1050 | handler = ConsoleUpdate(parent=self.parent, |
---|
| 1051 | manager=self, |
---|
| 1052 | improvement_delta=0.1) |
---|
[f6aee42] | 1053 | self._mac_sleep(0.2) |
---|
[e54d2c32] | 1054 | ## perform single fit |
---|
[05fb6d12] | 1055 | try: |
---|
| 1056 | page = self.fit_panel.get_page_by_id(uid) |
---|
| 1057 | batch_on = page.batch_on |
---|
| 1058 | except: |
---|
[fa02d95] | 1059 | try: |
---|
[f32d144] | 1060 | #if the id cannot be found then we deal with a self.sim_page |
---|
[fa02d95] | 1061 | #or a self.batch_page |
---|
| 1062 | if self.sim_page is not None and uid == self.sim_page.uid: |
---|
[7b1ca97] | 1063 | batch_on = self.sim_page.batch_on |
---|
[fa02d95] | 1064 | if self.batch_page is not None and uid == self.batch_page.uid: |
---|
[7b1ca97] | 1065 | batch_on = self.batch_page.batch_on |
---|
[fa02d95] | 1066 | except: |
---|
[f32d144] | 1067 | batch_on = False |
---|
| 1068 | |
---|
[05fb6d12] | 1069 | # batch fit |
---|
| 1070 | if batch_on: |
---|
[f32d144] | 1071 | calc_fit = FitThread(handler=handler, |
---|
| 1072 | fn=fitter_list, |
---|
| 1073 | pars=pars, |
---|
| 1074 | batch_inputs=batch_inputs, |
---|
| 1075 | batch_outputs=batch_outputs, |
---|
| 1076 | page_id=list_page_id, |
---|
| 1077 | completefn=self._batch_fit_complete, |
---|
| 1078 | ftol=self.ftol, |
---|
| 1079 | reset_flag=self.batch_reset_flag) |
---|
[e54d2c32] | 1080 | else: |
---|
[f32d144] | 1081 | # single fit: not batch and not simul fit |
---|
[92a5ac92] | 1082 | if not is_single_fit: |
---|
| 1083 | current_page_id = self.sim_page.uid |
---|
[e54d2c32] | 1084 | ## Perform more than 1 fit at the time |
---|
[58e0c83] | 1085 | calc_fit = FitThread(handler=handler, |
---|
[cc31608] | 1086 | fn=fitter_list, |
---|
[33dd2e5] | 1087 | batch_inputs=batch_inputs, |
---|
| 1088 | batch_outputs=batch_outputs, |
---|
[66ff250] | 1089 | page_id=list_page_id, |
---|
[2296316] | 1090 | updatefn=handler.update_fit, |
---|
[6a4002d] | 1091 | completefn=self._fit_completed, |
---|
[2296316] | 1092 | ftol=self.ftol) |
---|
[66ff250] | 1093 | self.fit_thread_list[current_page_id] = calc_fit |
---|
[a2cb1f9] | 1094 | calc_fit.queue() |
---|
[cc31608] | 1095 | msg = "Fitting is in progress..." |
---|
[f32d144] | 1096 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) |
---|
[cc31608] | 1097 | |
---|
| 1098 | self.ready_fit(calc_fit=calc_fit) |
---|
[31469d50] | 1099 | return flag |
---|
| 1100 | |
---|
[662da312] | 1101 | def ready_fit(self, calc_fit): |
---|
[c660493] | 1102 | """ |
---|
| 1103 | Ready for another fit |
---|
| 1104 | """ |
---|
| 1105 | if self.fitproblem_count != None and self.fitproblem_count > 1: |
---|
[662da312] | 1106 | calc_fit.ready(2.5) |
---|
[c660493] | 1107 | else: |
---|
| 1108 | time.sleep(0.4) |
---|
[2f189dc] | 1109 | |
---|
[2dbffcc] | 1110 | def remove_plot(self, uid, fid=None, theory=False): |
---|
[2f189dc] | 1111 | """ |
---|
[5062bbf] | 1112 | remove model plot when a fit page is closed |
---|
[2dbffcc] | 1113 | :param uid: the id related to the fitpage to close |
---|
| 1114 | :param fid: the id of the fitproblem(data, model, range,etc) |
---|
[2f189dc] | 1115 | """ |
---|
[2dbffcc] | 1116 | if uid not in self.page_finder.keys(): |
---|
| 1117 | return |
---|
| 1118 | fitproblemList = self.page_finder[uid].get_fit_problem(fid) |
---|
| 1119 | for fitproblem in fitproblemList: |
---|
| 1120 | data = fitproblem.get_fit_data() |
---|
| 1121 | model = fitproblem.get_model() |
---|
| 1122 | plot_id = None |
---|
| 1123 | if model is not None: |
---|
[6d8bad4] | 1124 | plot_id = data.id + model.name |
---|
[2dbffcc] | 1125 | if theory: |
---|
[e1bc51d] | 1126 | plot_id = data.id + model.name |
---|
[2dbffcc] | 1127 | group_id = data.group_id |
---|
| 1128 | wx.PostEvent(self.parent, NewPlotEvent(id=plot_id, |
---|
[f32d144] | 1129 | group_id=group_id, |
---|
| 1130 | action='remove')) |
---|
[edcbd467] | 1131 | |
---|
[2dbffcc] | 1132 | def store_data(self, uid, data_list=None, caption=None): |
---|
[31b0c47] | 1133 | """ |
---|
[2dbffcc] | 1134 | Recieve a list of data and store them ans well as a caption of |
---|
| 1135 | the fit page where they come from. |
---|
| 1136 | :param uid: if related to a fit page |
---|
| 1137 | :param data_list: list of data to fit |
---|
| 1138 | :param caption: caption of the window related to these data |
---|
[31b0c47] | 1139 | """ |
---|
[cc31608] | 1140 | if data_list is None: |
---|
| 1141 | data_list = [] |
---|
[f7ef313] | 1142 | |
---|
[2dbffcc] | 1143 | self.page_finder[uid].set_fit_data(data=data_list) |
---|
| 1144 | if caption is not None: |
---|
| 1145 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[f7ef313] | 1146 | |
---|
[6bbeacd4] | 1147 | def on_add_new_page(self, event=None): |
---|
[edcbd467] | 1148 | """ |
---|
[6bbeacd4] | 1149 | ask fit panel to create a new empty page |
---|
[edcbd467] | 1150 | """ |
---|
| 1151 | try: |
---|
[6bbeacd4] | 1152 | page = self.fit_panel.add_empty_page() |
---|
[6450d8c] | 1153 | # add data associated to the page created |
---|
[f32d144] | 1154 | if page != None: |
---|
[f304194] | 1155 | wx.PostEvent(self.parent, StatusEvent(status="Page Created", |
---|
[31b0c47] | 1156 | info="info")) |
---|
[edcbd467] | 1157 | else: |
---|
[f304194] | 1158 | msg = "Page was already Created" |
---|
[a0da535] | 1159 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1160 | info="warning")) |
---|
[1b1bbf9] | 1161 | self.set_top_panel() |
---|
[edcbd467] | 1162 | except: |
---|
[2dbffcc] | 1163 | msg = "Creating Fit page: %s"%sys.exc_value |
---|
| 1164 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[6bbeacd4] | 1165 | |
---|
| 1166 | def add_fit_page(self, data): |
---|
| 1167 | """ |
---|
| 1168 | given a data, ask to the fitting panel to create a new fitting page, |
---|
| 1169 | get this page and store it into the page_finder of this plug-in |
---|
[2dbffcc] | 1170 | :param data: is a list of data |
---|
[6bbeacd4] | 1171 | """ |
---|
| 1172 | page = self.fit_panel.set_data(data) |
---|
[7f76f89] | 1173 | # page could be None when loading state files |
---|
| 1174 | if page == None: |
---|
| 1175 | return page |
---|
[6bbeacd4] | 1176 | #append Data1D to the panel containing its theory |
---|
| 1177 | #if theory already plotted |
---|
[66ff250] | 1178 | if page.uid in self.page_finder: |
---|
[cc31608] | 1179 | data = page.get_data() |
---|
[2dbffcc] | 1180 | theory_data = self.page_finder[page.uid].get_theory_data(data.id) |
---|
[6bbeacd4] | 1181 | if issubclass(data.__class__, Data2D): |
---|
[e88ebfd] | 1182 | data.group_id = wx.NewId() |
---|
[a5701e6] | 1183 | if theory_data is not None: |
---|
[66ff250] | 1184 | group_id = str(page.uid) + " Model1D" |
---|
[f32d144] | 1185 | wx.PostEvent(self.parent, |
---|
[a5701e6] | 1186 | NewPlotEvent(group_id=group_id, |
---|
| 1187 | action="delete")) |
---|
[0fd2f27] | 1188 | self.parent.update_data(prev_data=theory_data, |
---|
[f32d144] | 1189 | new_data=data) |
---|
[6bbeacd4] | 1190 | else: |
---|
| 1191 | if theory_data is not None: |
---|
[66ff250] | 1192 | group_id = str(page.uid) + " Model2D" |
---|
[e88ebfd] | 1193 | data.group_id = theory_data.group_id |
---|
[a5701e6] | 1194 | wx.PostEvent(self.parent, |
---|
| 1195 | NewPlotEvent(group_id=group_id, |
---|
| 1196 | action="delete")) |
---|
[0fd2f27] | 1197 | self.parent.update_data(prev_data=theory_data, |
---|
[f32d144] | 1198 | new_data=data) |
---|
| 1199 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
[2dbffcc] | 1200 | caption=page.window_caption) |
---|
[33dd2e5] | 1201 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 1202 | self.sim_page.draw_page() |
---|
[fa02d95] | 1203 | if self.batch_page is not None and self.batch_on: |
---|
| 1204 | self.batch_page.draw_page() |
---|
| 1205 | |
---|
[1b1bbf9] | 1206 | return page |
---|
[6bbeacd4] | 1207 | |
---|
[848a2ef] | 1208 | def _onEVT_SLICER_PANEL(self, event): |
---|
| 1209 | """ |
---|
[f32d144] | 1210 | receive and event telling to update a panel with a name starting with |
---|
| 1211 | event.panel_name. this method update slicer panel |
---|
[2e08a9f] | 1212 | for a given interactor. |
---|
[5062bbf] | 1213 | |
---|
[f32d144] | 1214 | :param event: contains type of slicer , paramaters for updating |
---|
[2e08a9f] | 1215 | the panel and panel_name to find the slicer 's panel concerned. |
---|
[848a2ef] | 1216 | """ |
---|
| 1217 | for item in self.parent.panels: |
---|
[0fd2f27] | 1218 | name = event.panel_name |
---|
| 1219 | if self.parent.panels[item].window_caption.startswith(name): |
---|
[848a2ef] | 1220 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
[df4c3ad] | 1221 | |
---|
| 1222 | self.parent._mgr.Update() |
---|
[b324aa9] | 1223 | |
---|
[f32d144] | 1224 | def _closed_fitpage(self, event): |
---|
[848a2ef] | 1225 | """ |
---|
[f32d144] | 1226 | request fitpanel to close a given page when its unique data is removed |
---|
[5062bbf] | 1227 | from the plot. close fitpage only when the a loaded data is removed |
---|
[f32d144] | 1228 | """ |
---|
[784e2fa] | 1229 | if event is None or event.data is None: |
---|
| 1230 | return |
---|
[f32d144] | 1231 | if hasattr(event.data, "is_data"): |
---|
[a0da535] | 1232 | if not event.data.is_data or \ |
---|
[66ff250] | 1233 | event.data.__class__.__name__ == "Data1D": |
---|
[f32d144] | 1234 | self.fit_panel.close_page_with_data(event.data) |
---|
[2dbffcc] | 1235 | |
---|
[dc613d6] | 1236 | def _reset_schedule_problem(self, value=0, uid=None): |
---|
[6f023e8] | 1237 | """ |
---|
[5062bbf] | 1238 | unschedule or schedule all fitproblem to be fit |
---|
[6f023e8] | 1239 | """ |
---|
[dc613d6] | 1240 | # case that uid is not specified |
---|
| 1241 | if uid == None: |
---|
| 1242 | for page_id in self.page_finder.keys(): |
---|
| 1243 | self.page_finder[page_id].schedule_tofit(value) |
---|
| 1244 | # when uid is given |
---|
| 1245 | else: |
---|
[2dbffcc] | 1246 | if uid in self.page_finder.keys(): |
---|
| 1247 | self.page_finder[uid].schedule_tofit(value) |
---|
[dc613d6] | 1248 | |
---|
[33dd2e5] | 1249 | def _fit_helper(self, fitproblem, pars, fitter, fit_id, |
---|
| 1250 | batch_inputs, batch_outputs): |
---|
[2140e68] | 1251 | """ |
---|
[2dbffcc] | 1252 | Create and set fit engine with series of data and model |
---|
| 1253 | :param pars: list of fittable parameters |
---|
| 1254 | :param fitter_list: list of fit engine |
---|
[f32d144] | 1255 | :param value: structure storing data mapped to their model, range etc. |
---|
[2140e68] | 1256 | """ |
---|
[922497f] | 1257 | data = fitproblem.get_fit_data() |
---|
| 1258 | model = fitproblem.get_model() |
---|
| 1259 | smearer = fitproblem.get_smearer() |
---|
| 1260 | qmin, qmax = fitproblem.get_range() |
---|
[29a4024] | 1261 | |
---|
[922497f] | 1262 | #Extra list of parameters and their constraints |
---|
| 1263 | listOfConstraint = [] |
---|
| 1264 | param = fitproblem.get_model_param() |
---|
| 1265 | if len(param) > 0: |
---|
| 1266 | for item in param: |
---|
| 1267 | ## check if constraint |
---|
| 1268 | if item[0] != None and item[1] != None: |
---|
[f32d144] | 1269 | listOfConstraint.append((item[0], item[1])) |
---|
| 1270 | new_model = model |
---|
[41661a0] | 1271 | fitter.set_model(new_model, fit_id, pars, data=data, |
---|
| 1272 | constraints=listOfConstraint) |
---|
[f32d144] | 1273 | fitter.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin, |
---|
[922497f] | 1274 | qmax=qmax) |
---|
| 1275 | fitter.select_problem_for_fit(id=fit_id, value=1) |
---|
[7bd6cfae] | 1276 | |
---|
[f32d144] | 1277 | def _onSelect(self, event): |
---|
| 1278 | """ |
---|
| 1279 | when Select data to fit a new page is created .Its reference is |
---|
[5062bbf] | 1280 | added to self.page_finder |
---|
[d89f09b] | 1281 | """ |
---|
[c77d859] | 1282 | self.panel = event.GetEventObject() |
---|
[67e258c] | 1283 | Plugin.on_perspective(self, event=event) |
---|
[5e48acb] | 1284 | self.select_data(self.panel) |
---|
| 1285 | |
---|
| 1286 | def select_data(self, panel): |
---|
| 1287 | """ |
---|
| 1288 | """ |
---|
| 1289 | self.panel = panel |
---|
[6ed82c7] | 1290 | for plottable in self.panel.graph.plottables: |
---|
[2fff4db] | 1291 | if plottable.__class__.__name__ in ["Data1D", "Theory1D"]: |
---|
[3831ea1e] | 1292 | data_id = self.panel.graph.selected_plottable |
---|
| 1293 | if plottable == self.panel.plots[data_id]: |
---|
[6bbeacd4] | 1294 | data = plottable |
---|
[cc31608] | 1295 | self.add_fit_page(data=[data]) |
---|
[edcbd467] | 1296 | return |
---|
[c77d859] | 1297 | else: |
---|
[6bbeacd4] | 1298 | data = plottable |
---|
[cc31608] | 1299 | self.add_fit_page(data=[data]) |
---|
[1b1bbf9] | 1300 | self.set_top_panel() |
---|
[1c1436d] | 1301 | |
---|
[66ff250] | 1302 | def update_fit(self, result=None, msg=""): |
---|
| 1303 | """ |
---|
| 1304 | """ |
---|
| 1305 | print "update_fit result", result |
---|
| 1306 | |
---|
[f32d144] | 1307 | def _batch_fit_complete(self, result, pars, page_id, |
---|
[33dd2e5] | 1308 | batch_outputs, batch_inputs, elapsed=None): |
---|
[cc31608] | 1309 | """ |
---|
[f32d144] | 1310 | Display fit result in batch |
---|
[2dbffcc] | 1311 | :param result: list of objects received fromt fit engines |
---|
| 1312 | :param pars: list of fitted parameters names |
---|
| 1313 | :param page_id: list of page ids which called fit function |
---|
| 1314 | :param elapsed: time spent at the fitting level |
---|
[cc31608] | 1315 | """ |
---|
[92a5ac92] | 1316 | self._mac_sleep(0.2) |
---|
| 1317 | uid = page_id[0] |
---|
| 1318 | if uid in self.fit_thread_list.keys(): |
---|
[f32d144] | 1319 | del self.fit_thread_list[uid] |
---|
[92a5ac92] | 1320 | |
---|
[cc31608] | 1321 | self._update_fit_button(page_id) |
---|
[2bb37c3] | 1322 | t1 = time.time() |
---|
| 1323 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
| 1324 | msg = "Fit completed on %s \n" % str_time |
---|
| 1325 | msg += "Duration time: %s s.\n" % str(elapsed) |
---|
[cc31608] | 1326 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
[f32d144] | 1327 | type="stop")) |
---|
[fa02d95] | 1328 | |
---|
[33dd2e5] | 1329 | if batch_outputs is None: |
---|
| 1330 | batch_outputs = {} |
---|
[fa02d95] | 1331 | |
---|
| 1332 | # format batch_outputs |
---|
| 1333 | batch_outputs["Chi2"] = [] |
---|
| 1334 | #Don't like these loops |
---|
[f32d144] | 1335 | # Need to create dictionary of all fitted parameters |
---|
[fa02d95] | 1336 | # since the number of parameters can differ between each fit result |
---|
| 1337 | for list_res in result: |
---|
| 1338 | for res in list_res: |
---|
| 1339 | model, data = res.inputs[0] |
---|
| 1340 | if model is not None and hasattr(model, "model"): |
---|
| 1341 | model = model.model |
---|
| 1342 | #get all fittable parameters of the current model |
---|
| 1343 | for param in model.getParamList(): |
---|
| 1344 | if param not in batch_outputs.keys(): |
---|
| 1345 | batch_outputs[param] = [] |
---|
| 1346 | for param in model.getDispParamList(): |
---|
| 1347 | if not model.is_fittable(param) and \ |
---|
| 1348 | param in batch_outputs.keys(): |
---|
| 1349 | del batch_outputs[param] |
---|
| 1350 | # Add fitted parameters and their error |
---|
| 1351 | for param in res.param_list: |
---|
| 1352 | if param not in batch_outputs.keys(): |
---|
| 1353 | batch_outputs[param] = [] |
---|
| 1354 | err_param = "error on %s" % str(param) |
---|
| 1355 | if err_param not in batch_inputs.keys(): |
---|
| 1356 | batch_inputs[err_param] = [] |
---|
| 1357 | msg = "" |
---|
| 1358 | for list_res in result: |
---|
| 1359 | for res in list_res: |
---|
| 1360 | pid = res.fitter_id |
---|
| 1361 | model, data = res.inputs[0] |
---|
| 1362 | correct_result = False |
---|
| 1363 | if model is not None and hasattr(model, "model"): |
---|
| 1364 | model = model.model |
---|
| 1365 | if data is not None and hasattr(data, "sans_data"): |
---|
| 1366 | data = data.sans_data |
---|
| 1367 | |
---|
| 1368 | is_data2d = issubclass(data.__class__, Data2D) |
---|
| 1369 | #check consistency of arrays |
---|
| 1370 | if not is_data2d: |
---|
| 1371 | if len(res.theory) == len(res.index[res.index]) and \ |
---|
| 1372 | len(res.index) == len(data.y): |
---|
[46c4ccb] | 1373 | correct_result = True |
---|
[fa02d95] | 1374 | else: |
---|
| 1375 | copy_data = deepcopy(data) |
---|
| 1376 | new_theory = copy_data.data |
---|
| 1377 | new_theory[res.index] = res.theory |
---|
[f32d144] | 1378 | new_theory[res.index == False] = numpy.nan |
---|
[fa02d95] | 1379 | correct_result = True |
---|
| 1380 | #get all fittable parameters of the current model |
---|
| 1381 | param_list = model.getParamList() |
---|
| 1382 | for param in model.getDispParamList(): |
---|
| 1383 | if not model.is_fittable(param) and \ |
---|
| 1384 | param in param_list: |
---|
[f32d144] | 1385 | param_list.remove(param) |
---|
[fa02d95] | 1386 | if not correct_result or res.fitness is None or \ |
---|
| 1387 | not numpy.isfinite(res.fitness) or \ |
---|
| 1388 | numpy.any(res.pvec == None) or not \ |
---|
| 1389 | numpy.all(numpy.isfinite(res.pvec)): |
---|
| 1390 | data_name = str(None) |
---|
| 1391 | if data is not None: |
---|
| 1392 | data_name = str(data.name) |
---|
| 1393 | model_name = str(None) |
---|
| 1394 | if model is not None: |
---|
| 1395 | model_name = str(model.name) |
---|
[f32d144] | 1396 | msg += "Data %s and Model %s did not fit.\n" % (data_name, |
---|
[fa02d95] | 1397 | model_name) |
---|
| 1398 | ERROR = numpy.NAN |
---|
| 1399 | cell = BatchCell() |
---|
| 1400 | cell.label = res.fitness |
---|
| 1401 | cell.value = res.fitness |
---|
| 1402 | batch_outputs["Chi2"].append(ERROR) |
---|
| 1403 | for param in param_list: |
---|
| 1404 | # save value of fixed parameters |
---|
| 1405 | if param not in res.param_list: |
---|
| 1406 | batch_outputs[str(param)].append(ERROR) |
---|
| 1407 | else: |
---|
[4a52223] | 1408 | #save only fitted values |
---|
[fa02d95] | 1409 | batch_outputs[param].append(ERROR) |
---|
| 1410 | batch_inputs["error on %s" % str(param)].append(ERROR) |
---|
| 1411 | else: |
---|
[f32d144] | 1412 | # ToDo: Why sometimes res.pvec comes with numpy.float64? |
---|
[26e1001] | 1413 | # Need to fix it within ScipyEngine |
---|
[f32d144] | 1414 | if res.pvec.__class__ == numpy.float64: |
---|
[26e1001] | 1415 | res.pvec = [res.pvec] |
---|
| 1416 | |
---|
[fa02d95] | 1417 | cell = BatchCell() |
---|
| 1418 | cell.label = res.fitness |
---|
| 1419 | cell.value = res.fitness |
---|
| 1420 | batch_outputs["Chi2"].append(cell) |
---|
| 1421 | # add parameters to batch_results |
---|
| 1422 | for param in param_list: |
---|
| 1423 | # save value of fixed parameters |
---|
| 1424 | if param not in res.param_list: |
---|
| 1425 | batch_outputs[str(param)].append(model.getParam(param)) |
---|
| 1426 | else: |
---|
| 1427 | index = res.param_list.index(param) |
---|
[47d9be79] | 1428 | #save only fitted values |
---|
[fa02d95] | 1429 | batch_outputs[param].append(res.pvec[index]) |
---|
| 1430 | if res.stderr is not None and \ |
---|
| 1431 | len(res.stderr) == len(res.param_list): |
---|
[15f68ce] | 1432 | item = res.stderr[index] |
---|
[fa02d95] | 1433 | batch_inputs["error on %s" % param].append(item) |
---|
[356d2d3] | 1434 | else: |
---|
| 1435 | batch_inputs["error on %s" % param].append('-') |
---|
[fa02d95] | 1436 | model.setParam(param, res.pvec[index]) |
---|
[f32d144] | 1437 | #fill the batch result with emtpy value if not in the current |
---|
[fa02d95] | 1438 | #model |
---|
| 1439 | EMPTY = "-" |
---|
| 1440 | for key in batch_outputs.keys(): |
---|
| 1441 | if key not in param_list and key not in ["Chi2", "Data"]: |
---|
| 1442 | batch_outputs[key].append(EMPTY) |
---|
| 1443 | |
---|
| 1444 | self.page_finder[pid].set_batch_result(batch_inputs=batch_inputs, |
---|
[f32d144] | 1445 | batch_outputs=batch_outputs) |
---|
[fa02d95] | 1446 | |
---|
| 1447 | cpage = self.fit_panel.get_page_by_id(pid) |
---|
| 1448 | cpage._on_fit_complete() |
---|
| 1449 | self.page_finder[pid][data.id].set_result(res) |
---|
| 1450 | fitproblem = self.page_finder[pid][data.id] |
---|
| 1451 | qmin, qmax = fitproblem.get_range() |
---|
[a5f6748] | 1452 | plot_result = False |
---|
| 1453 | if correct_result: |
---|
[fa02d95] | 1454 | if not is_data2d: |
---|
[f32d144] | 1455 | self._complete1D(x=data.x, y=res.theory, page_id=pid, |
---|
| 1456 | elapsed=None, |
---|
| 1457 | index=res.index, model=model, |
---|
| 1458 | weight=None, fid=data.id, |
---|
| 1459 | toggle_mode_on=False, state=None, |
---|
| 1460 | data=data, update_chisqr=False, |
---|
| 1461 | source='fit', plot_result=plot_result) |
---|
[fa02d95] | 1462 | else: |
---|
| 1463 | self._complete2D(image=new_theory, data=data, |
---|
[f32d144] | 1464 | model=model, |
---|
| 1465 | page_id=pid, elapsed=None, |
---|
| 1466 | index=res.index, |
---|
| 1467 | qmin=qmin, |
---|
| 1468 | qmax=qmax, fid=data.id, weight=None, |
---|
| 1469 | toggle_mode_on=False, state=None, |
---|
| 1470 | update_chisqr=False, |
---|
| 1471 | source='fit', plot_result=plot_result) |
---|
| 1472 | self.on_set_batch_result(page_id=pid, |
---|
| 1473 | fid=data.id, |
---|
| 1474 | batch_outputs=batch_outputs, |
---|
[fa02d95] | 1475 | batch_inputs=batch_inputs) |
---|
[4a52223] | 1476 | |
---|
[7643ba2] | 1477 | wx.PostEvent(self.parent, StatusEvent(status=msg, error="error", |
---|
[f32d144] | 1478 | type="stop")) |
---|
[1ac202f] | 1479 | # Remove parameters that are not shown |
---|
| 1480 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
| 1481 | tbatch_outputs = {} |
---|
| 1482 | shownkeystr = cpage.get_copy_params() |
---|
| 1483 | for key in batch_outputs.keys(): |
---|
[f32d144] | 1484 | if key in ["Chi2", "Data"] or shownkeystr.count(key) > 0: |
---|
[1ac202f] | 1485 | tbatch_outputs[key] = batch_outputs[key] |
---|
| 1486 | |
---|
[f32d144] | 1487 | wx.CallAfter(self.parent.on_set_batch_result, tbatch_outputs, |
---|
| 1488 | batch_inputs, self.sub_menu) |
---|
[92a5ac92] | 1489 | |
---|
[f64a4b7] | 1490 | def on_set_batch_result(self, page_id, fid, batch_outputs, batch_inputs): |
---|
| 1491 | """ |
---|
| 1492 | """ |
---|
[f32d144] | 1493 | pid = page_id |
---|
[f64a4b7] | 1494 | if fid not in self.page_finder[pid]: |
---|
| 1495 | return |
---|
| 1496 | fitproblem = self.page_finder[pid][fid] |
---|
| 1497 | index = self.page_finder[pid].nbr_residuals_computed - 1 |
---|
[f32d144] | 1498 | residuals = fitproblem.get_residuals() |
---|
[f64a4b7] | 1499 | theory_data = fitproblem.get_theory_data() |
---|
| 1500 | data = fitproblem.get_fit_data() |
---|
| 1501 | model = fitproblem.get_model() |
---|
| 1502 | #fill batch result information |
---|
| 1503 | if "Data" not in batch_outputs.keys(): |
---|
| 1504 | batch_outputs["Data"] = [] |
---|
| 1505 | from sans.guiframe.data_processor import BatchCell |
---|
[ec9a635] | 1506 | cell = BatchCell() |
---|
| 1507 | cell.label = data.name |
---|
| 1508 | cell.value = index |
---|
[6b3edc1] | 1509 | |
---|
[ec9a635] | 1510 | if theory_data != None: |
---|
[6b3edc1] | 1511 | #Suucessful fit |
---|
| 1512 | theory_data.id = wx.NewId() |
---|
[b5fe787] | 1513 | theory_data.name = model.name + "[%s]" % str(data.name) |
---|
[e80e704] | 1514 | if issubclass(theory_data.__class__, Data2D): |
---|
| 1515 | group_id = wx.NewId() |
---|
| 1516 | theory_data.group_id = group_id |
---|
| 1517 | if group_id not in theory_data.list_group_id: |
---|
| 1518 | theory_data.list_group_id.append(group_id) |
---|
| 1519 | |
---|
[ec9a635] | 1520 | try: |
---|
| 1521 | # associate residuals plot |
---|
[e80e704] | 1522 | if issubclass(residuals.__class__, Data2D): |
---|
| 1523 | group_id = wx.NewId() |
---|
| 1524 | residuals.group_id = group_id |
---|
| 1525 | if group_id not in residuals.list_group_id: |
---|
| 1526 | residuals.list_group_id.append(group_id) |
---|
[ec9a635] | 1527 | batch_outputs["Chi2"][index].object = [residuals] |
---|
| 1528 | except: |
---|
| 1529 | pass |
---|
| 1530 | |
---|
| 1531 | cell.object = [data, theory_data] |
---|
| 1532 | batch_outputs["Data"].append(cell) |
---|
| 1533 | for key, value in data.meta_data.iteritems(): |
---|
| 1534 | if key not in batch_inputs.keys(): |
---|
| 1535 | batch_inputs[key] = [] |
---|
[98fdccd] | 1536 | #if key.lower().strip() != "loader": |
---|
| 1537 | batch_inputs[key].append(value) |
---|
[ec9a635] | 1538 | param = "temperature" |
---|
| 1539 | if hasattr(data.sample, param): |
---|
| 1540 | if param not in batch_inputs.keys(): |
---|
[f32d144] | 1541 | batch_inputs[param] = [] |
---|
[ec9a635] | 1542 | batch_inputs[param].append(data.sample.temperature) |
---|
| 1543 | |
---|
[6a4002d] | 1544 | def _fit_completed(self, result, page_id, batch_outputs, |
---|
[f32d144] | 1545 | batch_inputs=None, pars=None, elapsed=None): |
---|
[c77d859] | 1546 | """ |
---|
[2dbffcc] | 1547 | Display result of the fit on related panel(s). |
---|
| 1548 | :param result: list of object generated when fit ends |
---|
| 1549 | :param pars: list of names of parameters fitted |
---|
| 1550 | :param page_id: list of page ids which called fit function |
---|
| 1551 | :param elapsed: time spent at the fitting level |
---|
[c77d859] | 1552 | """ |
---|
[2bb37c3] | 1553 | t1 = time.time() |
---|
| 1554 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
| 1555 | msg = "Fit completed on %s \n" % str_time |
---|
| 1556 | msg += "Duration time: %s s.\n" % str(elapsed) |
---|
| 1557 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 1558 | type="stop")) |
---|
[bb8a180] | 1559 | # reset fit_engine if changed by simul_fit |
---|
| 1560 | if self._fit_engine != self._gui_engine: |
---|
| 1561 | self._on_change_engine(self._gui_engine) |
---|
[2bb37c3] | 1562 | self._update_fit_button(page_id) |
---|
[922497f] | 1563 | result = result[0] |
---|
[dc613d6] | 1564 | self.fit_thread_list = {} |
---|
[66ff250] | 1565 | if page_id is None: |
---|
| 1566 | page_id = [] |
---|
[f32d144] | 1567 | ## fit more than 1 model at the same time |
---|
| 1568 | self._mac_sleep(0.2) |
---|
[ca7a626] | 1569 | try: |
---|
[6a4002d] | 1570 | index = 0 |
---|
| 1571 | for uid in page_id: |
---|
| 1572 | res = result[index] |
---|
[7643ba2] | 1573 | if res.fitness is None or \ |
---|
[6a4002d] | 1574 | not numpy.isfinite(res.fitness) or \ |
---|
| 1575 | numpy.any(res.pvec == None) or \ |
---|
| 1576 | not numpy.all(numpy.isfinite(res.pvec)): |
---|
| 1577 | msg = "Fitting did not converge!!!" |
---|
[f32d144] | 1578 | wx.PostEvent(self.parent, |
---|
| 1579 | StatusEvent(status=msg, |
---|
[6a4002d] | 1580 | info="warning", |
---|
| 1581 | type="stop")) |
---|
| 1582 | self._update_fit_button(page_id) |
---|
| 1583 | else: |
---|
[0438933] | 1584 | #set the panel when fit result are float not list |
---|
[f32d144] | 1585 | if res.pvec.__class__ == numpy.float64: |
---|
[0438933] | 1586 | pvec = [res.pvec] |
---|
| 1587 | else: |
---|
| 1588 | pvec = res.pvec |
---|
[f32d144] | 1589 | if res.stderr.__class__ == numpy.float64: |
---|
[0438933] | 1590 | stderr = [res.stderr] |
---|
| 1591 | else: |
---|
| 1592 | stderr = res.stderr |
---|
[7643ba2] | 1593 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
[f32d144] | 1594 | # Make sure we got all results |
---|
[7643ba2] | 1595 | #(CallAfter is important to MAC) |
---|
[8b1d759] | 1596 | try: |
---|
| 1597 | #if res != None: |
---|
[f32d144] | 1598 | wx.CallAfter(cpage.onsetValues, res.fitness, |
---|
| 1599 | res.param_list, |
---|
[8b1d759] | 1600 | pvec, stderr) |
---|
| 1601 | index += 1 |
---|
| 1602 | wx.CallAfter(cpage._on_fit_complete) |
---|
| 1603 | except: |
---|
[2d0756a5] | 1604 | msg = "Singular point: Fitting Error occurred." |
---|
[f32d144] | 1605 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1606 | info="error", |
---|
| 1607 | type="stop")) |
---|
[8b1d759] | 1608 | |
---|
[6a4002d] | 1609 | except: |
---|
[d7345b71] | 1610 | msg = "Fit completed but Following" |
---|
[6a4002d] | 1611 | msg += " error occurred:%s" % sys.exc_value |
---|
[2296316] | 1612 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 1613 | type="stop")) |
---|
[6a4002d] | 1614 | |
---|
[12cd4ec] | 1615 | def _update_fit_button(self, page_id): |
---|
| 1616 | """ |
---|
| 1617 | Update Fit button when fit stopped |
---|
| 1618 | |
---|
| 1619 | : parameter page_id: fitpage where the button is |
---|
| 1620 | """ |
---|
[37290c4] | 1621 | if page_id.__class__.__name__ != 'list': |
---|
| 1622 | page_id = [page_id] |
---|
[f32d144] | 1623 | for uid in page_id: |
---|
[12cd4ec] | 1624 | page = self.fit_panel.get_page_by_id(uid) |
---|
| 1625 | page._on_fit_complete() |
---|
| 1626 | |
---|
[c77d859] | 1627 | def _on_show_panel(self, event): |
---|
[5062bbf] | 1628 | """ |
---|
| 1629 | """ |
---|
| 1630 | pass |
---|
[da6fd1a] | 1631 | |
---|
| 1632 | def on_reset_batch_flag(self, event): |
---|
| 1633 | """ |
---|
| 1634 | Set batch_reset_flag |
---|
| 1635 | """ |
---|
| 1636 | event.Skip() |
---|
[dafc36f] | 1637 | if self.menu1 == None: |
---|
| 1638 | return |
---|
[da6fd1a] | 1639 | menu_item = self.menu1.FindItemById(self.id_reset_flag) |
---|
| 1640 | flag = menu_item.IsChecked() |
---|
| 1641 | if not flag: |
---|
| 1642 | menu_item.Check(False) |
---|
| 1643 | self.batch_reset_flag = True |
---|
| 1644 | else: |
---|
| 1645 | menu_item.Check(True) |
---|
| 1646 | self.batch_reset_flag = False |
---|
[5062bbf] | 1647 | |
---|
[da6fd1a] | 1648 | ## post a message to status bar |
---|
| 1649 | msg = "Set Chain Fitting: %s" % str(not self.batch_reset_flag) |
---|
[f32d144] | 1650 | wx.PostEvent(self.parent, |
---|
[da6fd1a] | 1651 | StatusEvent(status=msg)) |
---|
| 1652 | |
---|
[f32d144] | 1653 | def _onset_engine_park(self, event): |
---|
[c77d859] | 1654 | """ |
---|
[5062bbf] | 1655 | set engine to park |
---|
[c77d859] | 1656 | """ |
---|
| 1657 | self._on_change_engine('park') |
---|
| 1658 | |
---|
[f32d144] | 1659 | def _onset_engine_scipy(self, event): |
---|
[c77d859] | 1660 | """ |
---|
[5062bbf] | 1661 | set engine to scipy |
---|
[c77d859] | 1662 | """ |
---|
| 1663 | self._on_change_engine('scipy') |
---|
| 1664 | |
---|
| 1665 | def _on_slicer_event(self, event): |
---|
| 1666 | """ |
---|
[f32d144] | 1667 | Receive a panel as event and send it to guiframe |
---|
[5062bbf] | 1668 | |
---|
| 1669 | :param event: event containing a panel |
---|
| 1670 | |
---|
[c77d859] | 1671 | """ |
---|
[5062bbf] | 1672 | if event.panel is not None: |
---|
[c77d859] | 1673 | new_panel = event.panel |
---|
[c3435b7f] | 1674 | self.slicer_panels.append(event.panel) |
---|
[c77d859] | 1675 | # Set group ID if available |
---|
| 1676 | event_id = self.parent.popup_panel(new_panel) |
---|
| 1677 | new_panel.uid = event_id |
---|
[f32d144] | 1678 | self.mypanels.append(new_panel) |
---|
[5062bbf] | 1679 | |
---|
[848a2ef] | 1680 | def _onclearslicer(self, event): |
---|
| 1681 | """ |
---|
[5062bbf] | 1682 | Clear the boxslicer when close the panel associate with this slicer |
---|
[848a2ef] | 1683 | """ |
---|
[f32d144] | 1684 | name = event.GetPane().caption |
---|
[c3435b7f] | 1685 | |
---|
| 1686 | for panel in self.slicer_panels: |
---|
[f32d144] | 1687 | if panel.window_caption == name: |
---|
[c3435b7f] | 1688 | |
---|
[df4c3ad] | 1689 | for item in self.parent.panels: |
---|
[2dbffcc] | 1690 | if hasattr(self.parent.panels[item], "uid"): |
---|
[f32d144] | 1691 | if self.parent.panels[item].uid == panel.base.uid: |
---|
[df4c3ad] | 1692 | self.parent.panels[item].onClearSlicer(event) |
---|
| 1693 | self.parent._mgr.Update() |
---|
[f32d144] | 1694 | break |
---|
[c3435b7f] | 1695 | break |
---|
[5062bbf] | 1696 | |
---|
[707436d] | 1697 | def _return_engine_type(self): |
---|
| 1698 | """ |
---|
[5062bbf] | 1699 | return the current type of engine |
---|
[707436d] | 1700 | """ |
---|
| 1701 | return self._fit_engine |
---|
| 1702 | |
---|
[c77d859] | 1703 | def _on_change_engine(self, engine='park'): |
---|
| 1704 | """ |
---|
[f32d144] | 1705 | Allow to select the type of engine to perform fit |
---|
[5062bbf] | 1706 | |
---|
| 1707 | :param engine: the key work of the engine |
---|
| 1708 | |
---|
[c77d859] | 1709 | """ |
---|
[77e23a2] | 1710 | ## saving fit engine name |
---|
[c77d859] | 1711 | self._fit_engine = engine |
---|
[707436d] | 1712 | ## change menu item state |
---|
[2dbffcc] | 1713 | if engine == "park": |
---|
[0b6ae5f] | 1714 | self.menu1.FindItemById(self.park_id).Check(True) |
---|
| 1715 | self.menu1.FindItemById(self.scipy_id).Check(False) |
---|
[707436d] | 1716 | else: |
---|
[0b6ae5f] | 1717 | self.menu1.FindItemById(self.park_id).Check(False) |
---|
| 1718 | self.menu1.FindItemById(self.scipy_id).Check(True) |
---|
[77e23a2] | 1719 | ## post a message to status bar |
---|
[a0da535] | 1720 | msg = "Engine set to: %s" % self._fit_engine |
---|
[f32d144] | 1721 | wx.PostEvent(self.parent, |
---|
[a0da535] | 1722 | StatusEvent(status=msg)) |
---|
[66ff250] | 1723 | ## send the current engine type to fitpanel |
---|
| 1724 | self.fit_panel._on_engine_change(name=self._fit_engine) |
---|
[dc613d6] | 1725 | |
---|
[c77d859] | 1726 | def _on_model_panel(self, evt): |
---|
| 1727 | """ |
---|
[5062bbf] | 1728 | react to model selection on any combo box or model menu.plot the model |
---|
| 1729 | |
---|
| 1730 | :param evt: wx.combobox event |
---|
| 1731 | |
---|
[c77d859] | 1732 | """ |
---|
| 1733 | model = evt.model |
---|
[f32d144] | 1734 | uid = evt.uid |
---|
[6bbeacd4] | 1735 | qmin = evt.qmin |
---|
| 1736 | qmax = evt.qmax |
---|
[2dbffcc] | 1737 | caption = evt.caption |
---|
| 1738 | enable_smearer = evt.enable_smearer |
---|
[9237df4] | 1739 | if model == None: |
---|
[c77d859] | 1740 | return |
---|
[2dbffcc] | 1741 | if uid not in self.page_finder.keys(): |
---|
| 1742 | return |
---|
[6bbeacd4] | 1743 | # save the name containing the data name with the appropriate model |
---|
[66ff250] | 1744 | self.page_finder[uid].set_model(model) |
---|
[2dbffcc] | 1745 | self.page_finder[uid].enable_smearing(enable_smearer) |
---|
[66ff250] | 1746 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax) |
---|
[2dbffcc] | 1747 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[33dd2e5] | 1748 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 1749 | self.sim_page.draw_page() |
---|
[fa02d95] | 1750 | if self.batch_page is not None and self.batch_on: |
---|
| 1751 | self.batch_page.draw_page() |
---|
[5062bbf] | 1752 | |
---|
[2dbffcc] | 1753 | def _update1D(self, x, output): |
---|
[bb18ef1] | 1754 | """ |
---|
[5062bbf] | 1755 | Update the output of plotting model 1D |
---|
[bb18ef1] | 1756 | """ |
---|
[58e0c83] | 1757 | msg = "Plot updating ... " |
---|
[f32d144] | 1758 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="update")) |
---|
[e575db9] | 1759 | |
---|
[ad3fa1e] | 1760 | def _complete1D(self, x, y, page_id, elapsed, index, model, |
---|
[f64a4b7] | 1761 | weight=None, fid=None, |
---|
[f32d144] | 1762 | toggle_mode_on=False, state=None, |
---|
| 1763 | data=None, update_chisqr=True, |
---|
[a5f6748] | 1764 | source='model', plot_result=True): |
---|
[c77d859] | 1765 | """ |
---|
[5062bbf] | 1766 | Complete plotting 1D data |
---|
[f32d144] | 1767 | """ |
---|
[c77d859] | 1768 | try: |
---|
[1ec979d] | 1769 | numpy.nan_to_num(y) |
---|
| 1770 | |
---|
[6bbeacd4] | 1771 | new_plot = Data1D(x=x, y=y) |
---|
[dce84c0] | 1772 | new_plot.is_data = False |
---|
[55d2f7e] | 1773 | new_plot.dy = numpy.zeros(len(y)) |
---|
[6bbeacd4] | 1774 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[f32d144] | 1775 | _yaxis, _yunit = data.get_yaxis() |
---|
| 1776 | _xaxis, _xunit = data.get_xaxis() |
---|
[2dbffcc] | 1777 | new_plot.title = data.name |
---|
[5e48acb] | 1778 | |
---|
[f32d144] | 1779 | new_plot.group_id = data.group_id |
---|
[5e48acb] | 1780 | if new_plot.group_id == None: |
---|
| 1781 | new_plot.group_id = data.group_id |
---|
[f32d144] | 1782 | new_plot.id = str(page_id) + "model-" + data.name |
---|
[a582d28] | 1783 | #if new_plot.id in self.color_dict: |
---|
[f32d144] | 1784 | # new_plot.custom_color = self.color_dict[new_plot.id] |
---|
[6bbeacd4] | 1785 | #find if this theory was already plotted and replace that plot given |
---|
| 1786 | #the same id |
---|
[2dbffcc] | 1787 | theory_data = self.page_finder[page_id].get_theory_data(fid=data.id) |
---|
[71e5c11] | 1788 | |
---|
| 1789 | if data.is_data: |
---|
| 1790 | data_name = str(data.name) |
---|
| 1791 | else: |
---|
| 1792 | data_name = str(model.__class__.__name__) |
---|
| 1793 | |
---|
[f32d144] | 1794 | new_plot.name = model.name + " [" + data_name +"]" |
---|
[6bbeacd4] | 1795 | new_plot.xaxis(_xaxis, _xunit) |
---|
| 1796 | new_plot.yaxis(_yaxis, _yunit) |
---|
[f32d144] | 1797 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
[2e08a9f] | 1798 | fid=data.id) |
---|
[2dbffcc] | 1799 | self.parent.update_theory(data_id=data.id, theory=new_plot, |
---|
[f32d144] | 1800 | state=state) |
---|
[66ff250] | 1801 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1802 | title = new_plot.title |
---|
[eb4f2fd] | 1803 | batch_on = self.fit_panel.get_page_by_id(page_id).batch_on |
---|
| 1804 | if not batch_on: |
---|
| 1805 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[2dbffcc] | 1806 | title=str(title))) |
---|
[a5f6748] | 1807 | elif plot_result: |
---|
[90a2358] | 1808 | top_data_id = self.fit_panel.get_page_by_id(page_id).data.id |
---|
| 1809 | if data.id == top_data_id: |
---|
| 1810 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[f32d144] | 1811 | title=str(title))) |
---|
[2dbffcc] | 1812 | caption = current_pg.window_caption |
---|
| 1813 | self.page_finder[page_id].set_fit_tab_caption(caption=caption) |
---|
[7643ba2] | 1814 | |
---|
[f32d144] | 1815 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
[2dbffcc] | 1816 | fid=data.id) |
---|
| 1817 | if toggle_mode_on: |
---|
[f32d144] | 1818 | wx.PostEvent(self.parent, |
---|
[2dbffcc] | 1819 | NewPlotEvent(group_id=str(page_id) + " Model2D", |
---|
[f32d144] | 1820 | action="Hide")) |
---|
[2296316] | 1821 | else: |
---|
[2dbffcc] | 1822 | if update_chisqr: |
---|
| 1823 | wx.PostEvent(current_pg, |
---|
[2e08a9f] | 1824 | Chi2UpdateEvent(output=self._cal_chisqr( |
---|
| 1825 | data=data, |
---|
[f64a4b7] | 1826 | fid=fid, |
---|
[62f851f] | 1827 | weight=weight, |
---|
[2dbffcc] | 1828 | page_id=page_id, |
---|
| 1829 | index=index))) |
---|
| 1830 | else: |
---|
[f64a4b7] | 1831 | self._plot_residuals(page_id=page_id, data=data, fid=fid, |
---|
[f32d144] | 1832 | index=index, weight=weight) |
---|
[cfbd06a] | 1833 | |
---|
[d522635] | 1834 | msg = "Computation completed!" |
---|
[f32d144] | 1835 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[c77d859] | 1836 | except: |
---|
[6bbeacd4] | 1837 | raise |
---|
[5062bbf] | 1838 | |
---|
[f32d144] | 1839 | def _update2D(self, output, time=None): |
---|
[c77d859] | 1840 | """ |
---|
[5062bbf] | 1841 | Update the output of plotting model |
---|
[c77d859] | 1842 | """ |
---|
[58e0c83] | 1843 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
[a0da535] | 1844 | #updating ... ", type="update")) |
---|
[a2cb1f9] | 1845 | #self.ready_fit() |
---|
[58e0c83] | 1846 | |
---|
[f32d144] | 1847 | def _complete2D(self, image, data, model, page_id, elapsed, index, qmin, |
---|
| 1848 | qmax, fid=None, weight=None, toggle_mode_on=False, state=None, |
---|
[a5f6748] | 1849 | update_chisqr=True, source='model', plot_result=True): |
---|
[c77d859] | 1850 | """ |
---|
[5062bbf] | 1851 | Complete get the result of modelthread and create model 2D |
---|
| 1852 | that can be plot. |
---|
[c77d859] | 1853 | """ |
---|
[1ec979d] | 1854 | numpy.nan_to_num(image) |
---|
[f32d144] | 1855 | new_plot = Data2D(image=image, err_image=data.err_data) |
---|
[fa65e99] | 1856 | new_plot.name = model.name |
---|
[818589a] | 1857 | new_plot.title = "Analytical model 2D " |
---|
[2ee5c61] | 1858 | new_plot.id = str(page_id) + "model-" + data.name |
---|
[f3242cf] | 1859 | new_plot.group_id = str(page_id) + " Model2D" |
---|
[2dbffcc] | 1860 | new_plot.detector = data.detector |
---|
| 1861 | new_plot.source = data.source |
---|
[f32d144] | 1862 | new_plot.is_data = False |
---|
[2dbffcc] | 1863 | new_plot.qx_data = data.qx_data |
---|
| 1864 | new_plot.qy_data = data.qy_data |
---|
| 1865 | new_plot.q_data = data.q_data |
---|
| 1866 | new_plot.mask = data.mask |
---|
| 1867 | ## plot boundaries |
---|
| 1868 | new_plot.ymin = data.ymin |
---|
| 1869 | new_plot.ymax = data.ymax |
---|
| 1870 | new_plot.xmin = data.xmin |
---|
| 1871 | new_plot.xmax = data.xmax |
---|
| 1872 | title = data.title |
---|
[71e5c11] | 1873 | |
---|
[dce84c0] | 1874 | new_plot.is_data = False |
---|
[71e5c11] | 1875 | if data.is_data: |
---|
| 1876 | data_name = str(data.name) |
---|
| 1877 | else: |
---|
| 1878 | data_name = str(model.__class__.__name__) |
---|
| 1879 | |
---|
| 1880 | if len(title) > 1: |
---|
| 1881 | new_plot.title = "Model2D for " + data_name |
---|
[53cf669] | 1882 | new_plot.name = model.name + " [" + \ |
---|
[71e5c11] | 1883 | data_name + "-2D]" |
---|
[fa65e99] | 1884 | theory_data = deepcopy(new_plot) |
---|
[5ef55d2] | 1885 | theory_data.name = "Unknown" |
---|
[6bbeacd4] | 1886 | |
---|
[f32d144] | 1887 | self.page_finder[page_id].set_theory_data(data=theory_data, |
---|
| 1888 | fid=data.id) |
---|
| 1889 | self.parent.update_theory(data_id=data.id, |
---|
[05319e2] | 1890 | theory=new_plot, |
---|
[f32d144] | 1891 | state=state) |
---|
[66ff250] | 1892 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1893 | title = new_plot.title |
---|
[a5f6748] | 1894 | if not source == 'fit' and plot_result: |
---|
[763bec8] | 1895 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[ae4ade7] | 1896 | title=title)) |
---|
[2dbffcc] | 1897 | if toggle_mode_on: |
---|
[f32d144] | 1898 | wx.PostEvent(self.parent, |
---|
[2dbffcc] | 1899 | NewPlotEvent(group_id=str(page_id) + " Model1D", |
---|
| 1900 | action="Hide")) |
---|
[2296316] | 1901 | else: |
---|
[2dbffcc] | 1902 | # Chisqr in fitpage |
---|
| 1903 | if update_chisqr: |
---|
| 1904 | wx.PostEvent(current_pg, |
---|
| 1905 | Chi2UpdateEvent(output=self._cal_chisqr(data=data, |
---|
[62f851f] | 1906 | weight=weight, |
---|
[f64a4b7] | 1907 | fid=fid, |
---|
[2dbffcc] | 1908 | page_id=page_id, |
---|
| 1909 | index=index))) |
---|
| 1910 | else: |
---|
[f64a4b7] | 1911 | self._plot_residuals(page_id=page_id, data=data, fid=fid, |
---|
[62f851f] | 1912 | index=index, weight=weight) |
---|
[d522635] | 1913 | msg = "Computation completed!" |
---|
[a0da535] | 1914 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[6bbeacd4] | 1915 | |
---|
[2dbffcc] | 1916 | def _draw_model2D(self, model, page_id, qmin, |
---|
| 1917 | qmax, |
---|
| 1918 | data=None, smearer=None, |
---|
[a0da535] | 1919 | description=None, enable2D=False, |
---|
[5ef55d2] | 1920 | state=None, |
---|
[f64a4b7] | 1921 | fid=None, |
---|
[62f851f] | 1922 | weight=None, |
---|
[fa65e99] | 1923 | toggle_mode_on=False, |
---|
[e3f6ef5] | 1924 | update_chisqr=True, source='model'): |
---|
[f93dfcb] | 1925 | """ |
---|
[5062bbf] | 1926 | draw model in 2D |
---|
| 1927 | |
---|
| 1928 | :param model: instance of the model to draw |
---|
| 1929 | :param description: the description of the model |
---|
| 1930 | :param enable2D: when True allows to draw model 2D |
---|
| 1931 | :param qmin: the minimum value to draw model 2D |
---|
| 1932 | :param qmax: the maximum value to draw model 2D |
---|
| 1933 | :param qstep: the number of division of Qx and Qy of the model to draw |
---|
[f93dfcb] | 1934 | |
---|
| 1935 | """ |
---|
[c77d859] | 1936 | if not enable2D: |
---|
[2dbffcc] | 1937 | return None |
---|
[c77d859] | 1938 | try: |
---|
| 1939 | from model_thread import Calc2D |
---|
| 1940 | ## If a thread is already started, stop it |
---|
[a0da535] | 1941 | if (self.calc_2D is not None) and self.calc_2D.isrunning(): |
---|
[c77d859] | 1942 | self.calc_2D.stop() |
---|
[f32d144] | 1943 | self.calc_2D = Calc2D(model=model, |
---|
[a0da535] | 1944 | data=data, |
---|
[66ff250] | 1945 | page_id=page_id, |
---|
[a0da535] | 1946 | smearer=smearer, |
---|
| 1947 | qmin=qmin, |
---|
| 1948 | qmax=qmax, |
---|
[62f851f] | 1949 | weight=weight, |
---|
[f64a4b7] | 1950 | fid=fid, |
---|
[fa65e99] | 1951 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 1952 | state=state, |
---|
[2296316] | 1953 | completefn=self._complete2D, |
---|
[e3f6ef5] | 1954 | update_chisqr=update_chisqr, source=source) |
---|
[c77d859] | 1955 | self.calc_2D.queue() |
---|
| 1956 | except: |
---|
[6bbeacd4] | 1957 | raise |
---|
[f72333f] | 1958 | |
---|
[f32d144] | 1959 | def _draw_model1D(self, model, page_id, data, |
---|
[2dbffcc] | 1960 | qmin, qmax, smearer=None, |
---|
[5ef55d2] | 1961 | state=None, |
---|
[62f851f] | 1962 | weight=None, |
---|
[f32d144] | 1963 | fid=None, |
---|
[e3f6ef5] | 1964 | toggle_mode_on=False, update_chisqr=True, source='model', |
---|
[2296316] | 1965 | enable1D=True): |
---|
[c77d859] | 1966 | """ |
---|
[5062bbf] | 1967 | Draw model 1D from loaded data1D |
---|
| 1968 | |
---|
| 1969 | :param data: loaded data |
---|
| 1970 | :param model: the model to plot |
---|
| 1971 | |
---|
[c77d859] | 1972 | """ |
---|
| 1973 | if not enable1D: |
---|
[f32d144] | 1974 | return |
---|
[c77d859] | 1975 | try: |
---|
| 1976 | from model_thread import Calc1D |
---|
| 1977 | ## If a thread is already started, stop it |
---|
[a0da535] | 1978 | if (self.calc_1D is not None) and self.calc_1D.isrunning(): |
---|
[c77d859] | 1979 | self.calc_1D.stop() |
---|
[2dbffcc] | 1980 | self.calc_1D = Calc1D(data=data, |
---|
[6bbeacd4] | 1981 | model=model, |
---|
[66ff250] | 1982 | page_id=page_id, |
---|
[a0da535] | 1983 | qmin=qmin, |
---|
| 1984 | qmax=qmax, |
---|
| 1985 | smearer=smearer, |
---|
[5ef55d2] | 1986 | state=state, |
---|
[62f851f] | 1987 | weight=weight, |
---|
[f64a4b7] | 1988 | fid=fid, |
---|
[fa65e99] | 1989 | toggle_mode_on=toggle_mode_on, |
---|
[2296316] | 1990 | completefn=self._complete1D, |
---|
| 1991 | #updatefn = self._update1D, |
---|
[e3f6ef5] | 1992 | update_chisqr=update_chisqr, |
---|
| 1993 | source=source) |
---|
[c77d859] | 1994 | self.calc_1D.queue() |
---|
| 1995 | except: |
---|
[a0da535] | 1996 | msg = " Error occurred when drawing %s Model 1D: " % model.name |
---|
| 1997 | msg += " %s" % sys.exc_value |
---|
| 1998 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[2dbffcc] | 1999 | |
---|
[f32d144] | 2000 | def _cal_chisqr(self, page_id, data, weight, fid=None, index=None): |
---|
[f72333f] | 2001 | """ |
---|
[5062bbf] | 2002 | Get handy Chisqr using the output from draw1D and 2D, |
---|
| 2003 | instead of calling expansive CalcChisqr in guithread |
---|
[f72333f] | 2004 | """ |
---|
[f32d144] | 2005 | data_copy = deepcopy(data) |
---|
[f72333f] | 2006 | # default chisqr |
---|
| 2007 | chisqr = None |
---|
[2dbffcc] | 2008 | #to compute chisq make sure data has valid data |
---|
[f72333f] | 2009 | # return None if data == None |
---|
[df9e0fc] | 2010 | if not check_data_validity(data_copy) or data_copy == None: |
---|
[2dbffcc] | 2011 | return chisqr |
---|
[df9e0fc] | 2012 | |
---|
[f72333f] | 2013 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 2014 | if data_copy.__class__.__name__ == "Data2D": |
---|
[f32d144] | 2015 | if index == None: |
---|
| 2016 | index = numpy.ones(len(data_copy.data), ntype=bool) |
---|
[62f851f] | 2017 | if weight != None: |
---|
| 2018 | data_copy.err_data = weight |
---|
[a0da535] | 2019 | # get rid of zero error points |
---|
[f32d144] | 2020 | index = index & (data_copy.err_data != 0) |
---|
| 2021 | index = index & (numpy.isfinite(data_copy.data)) |
---|
| 2022 | fn = data_copy.data[index] |
---|
[ca2bdae] | 2023 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 2024 | if theory_data == None: |
---|
[df9e0fc] | 2025 | return chisqr |
---|
[6bbeacd4] | 2026 | gn = theory_data.data[index] |
---|
[ca2bdae] | 2027 | en = data_copy.err_data[index] |
---|
[f72333f] | 2028 | else: |
---|
| 2029 | # 1 d theory from model_thread is only in the range of index |
---|
[2296316] | 2030 | if index == None: |
---|
[ca2bdae] | 2031 | index = numpy.ones(len(data_copy.y), ntype=bool) |
---|
[62f851f] | 2032 | if weight != None: |
---|
| 2033 | data_copy.dy = weight |
---|
[ca2bdae] | 2034 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 2035 | dy = numpy.ones(len(data_copy.y)) |
---|
[f151ddf] | 2036 | else: |
---|
[a0da535] | 2037 | ## Set consitently w/AbstractFitengine: |
---|
| 2038 | # But this should be corrected later. |
---|
[ca2bdae] | 2039 | dy = deepcopy(data_copy.dy) |
---|
[f32d144] | 2040 | dy[dy == 0] = 1 |
---|
| 2041 | fn = data_copy.y[index] |
---|
[eb4f2fd] | 2042 | |
---|
[ca2bdae] | 2043 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 2044 | if theory_data == None: |
---|
[df9e0fc] | 2045 | return chisqr |
---|
[6bbeacd4] | 2046 | gn = theory_data.y |
---|
[4d5fa8c] | 2047 | en = dy[index] |
---|
[df9e0fc] | 2048 | |
---|
[f72333f] | 2049 | # residual |
---|
[2296316] | 2050 | res = (fn - gn) / en |
---|
| 2051 | residuals = res[numpy.isfinite(res)] |
---|
[f72333f] | 2052 | # get chisqr only w/finite |
---|
[2296316] | 2053 | chisqr = numpy.average(residuals * residuals) |
---|
[eb4f2fd] | 2054 | |
---|
[f32d144] | 2055 | self._plot_residuals(page_id=page_id, data=data_copy, |
---|
[f64a4b7] | 2056 | fid=fid, |
---|
[62f851f] | 2057 | weight=weight, index=index) |
---|
[ca2bdae] | 2058 | |
---|
[f72333f] | 2059 | return chisqr |
---|
| 2060 | |
---|
[f64a4b7] | 2061 | def _plot_residuals(self, page_id, weight, fid=None, |
---|
[f32d144] | 2062 | data=None, index=None): |
---|
[2296316] | 2063 | """ |
---|
| 2064 | Plot the residuals |
---|
| 2065 | |
---|
| 2066 | :param data: data |
---|
[f32d144] | 2067 | :param index: index array (bool) |
---|
[2296316] | 2068 | : Note: this is different from the residuals in cal_chisqr() |
---|
| 2069 | """ |
---|
[ca2bdae] | 2070 | data_copy = deepcopy(data) |
---|
[2296316] | 2071 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 2072 | if data_copy.__class__.__name__ == "Data2D": |
---|
[2296316] | 2073 | # build residuals |
---|
| 2074 | residuals = Data2D() |
---|
| 2075 | #residuals.copy_from_datainfo(data) |
---|
| 2076 | # Not for trunk the line below, instead use the line above |
---|
[ca2bdae] | 2077 | data_copy.clone_without_data(len(data_copy.data), residuals) |
---|
[2296316] | 2078 | residuals.data = None |
---|
[f32d144] | 2079 | fn = data_copy.data |
---|
[ca2bdae] | 2080 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 2081 | gn = theory_data.data |
---|
[9fa4a4d] | 2082 | if weight == None: |
---|
| 2083 | en = data_copy.err_data |
---|
| 2084 | else: |
---|
| 2085 | en = weight |
---|
[f32d144] | 2086 | residuals.data = (fn - gn) / en |
---|
| 2087 | residuals.qx_data = data_copy.qx_data |
---|
| 2088 | residuals.qy_data = data_copy.qy_data |
---|
| 2089 | residuals.q_data = data_copy.q_data |
---|
| 2090 | residuals.err_data = numpy.ones(len(residuals.data)) |
---|
[2296316] | 2091 | residuals.xmin = min(residuals.qx_data) |
---|
| 2092 | residuals.xmax = max(residuals.qx_data) |
---|
| 2093 | residuals.ymin = min(residuals.qy_data) |
---|
| 2094 | residuals.ymax = max(residuals.qy_data) |
---|
[f32d144] | 2095 | residuals.q_data = data_copy.q_data |
---|
[ca2bdae] | 2096 | residuals.mask = data_copy.mask |
---|
[2296316] | 2097 | residuals.scale = 'linear' |
---|
| 2098 | # check the lengths |
---|
| 2099 | if len(residuals.data) != len(residuals.q_data): |
---|
| 2100 | return |
---|
| 2101 | else: |
---|
| 2102 | # 1 d theory from model_thread is only in the range of index |
---|
[ca2bdae] | 2103 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 2104 | dy = numpy.ones(len(data_copy.y)) |
---|
[2296316] | 2105 | else: |
---|
[62f851f] | 2106 | if weight == None: |
---|
[90a1440] | 2107 | dy = numpy.ones(len(data_copy.y)) |
---|
[f32d144] | 2108 | ## Set consitently w/AbstractFitengine: |
---|
[2296316] | 2109 | ## But this should be corrected later. |
---|
[90a1440] | 2110 | else: |
---|
[f32d144] | 2111 | dy = weight |
---|
| 2112 | dy[dy == 0] = 1 |
---|
| 2113 | fn = data_copy.y[index] |
---|
[ca2bdae] | 2114 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[2296316] | 2115 | gn = theory_data.y |
---|
| 2116 | en = dy[index] |
---|
| 2117 | # build residuals |
---|
| 2118 | residuals = Data1D() |
---|
| 2119 | residuals.y = (fn - gn) / en |
---|
[ca2bdae] | 2120 | residuals.x = data_copy.x[index] |
---|
[2296316] | 2121 | residuals.dy = numpy.ones(len(residuals.y)) |
---|
| 2122 | residuals.dx = None |
---|
| 2123 | residuals.dxl = None |
---|
| 2124 | residuals.dxw = None |
---|
| 2125 | residuals.ytransform = 'y' |
---|
| 2126 | # For latter scale changes |
---|
| 2127 | residuals.xaxis('\\rm{Q} ', 'A^{-1}') |
---|
| 2128 | residuals.yaxis('\\rm{Residuals} ', 'normalized') |
---|
| 2129 | new_plot = residuals |
---|
[f32d144] | 2130 | new_plot.name = "Residuals for " + str(theory_data.name.split()[0]) + "[" + str(data.name) +"]" |
---|
[2296316] | 2131 | ## allow to highlight data when plotted |
---|
| 2132 | new_plot.interactive = True |
---|
| 2133 | ## when 2 data have the same id override the 1 st plotted |
---|
[f32d144] | 2134 | new_plot.id = "res" + str(data_copy.id) |
---|
[2296316] | 2135 | ##group_id specify on which panel to plot this data |
---|
[5e48acb] | 2136 | group_id = self.page_finder[page_id].get_graph_id() |
---|
| 2137 | if group_id == None: |
---|
| 2138 | group_id = data.group_id |
---|
[f32d144] | 2139 | new_plot.group_id = "res" + str(group_id) |
---|
[2296316] | 2140 | #new_plot.is_data = True |
---|
| 2141 | ##post data to plot |
---|
[f32d144] | 2142 | title = new_plot.name |
---|
| 2143 | self.page_finder[page_id].set_residuals(residuals=new_plot, |
---|
| 2144 | fid=data.id) |
---|
[2de278d9] | 2145 | self.parent.update_theory(data_id=data.id, theory=new_plot) |
---|
[f64a4b7] | 2146 | batch_on = self.fit_panel.get_page_by_id(page_id).batch_on |
---|
[5cf1e5b] | 2147 | if not batch_on: |
---|
| 2148 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) |
---|