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