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