[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 | |
---|
[1610976] | 525 | def set_default_perspective(self): |
---|
| 526 | """ |
---|
[5062bbf] | 527 | Call back method that True to notify the parent that the current plug-in |
---|
[f32d144] | 528 | can be set as default perspective. |
---|
| 529 | when returning False, the plug-in is not candidate for an automatic |
---|
[5062bbf] | 530 | default perspective setting |
---|
[1610976] | 531 | """ |
---|
| 532 | return True |
---|
[a0da535] | 533 | |
---|
[17553ae] | 534 | def delete_data(self, data): |
---|
[8ee56a9] | 535 | """ |
---|
| 536 | delete the given data from panel |
---|
| 537 | """ |
---|
| 538 | self.fit_panel.delete_data(data) |
---|
| 539 | |
---|
[e88ebfd] | 540 | def set_data(self, data_list=None): |
---|
[a0da535] | 541 | """ |
---|
| 542 | receive a list of data to fit |
---|
| 543 | """ |
---|
[b2d9826] | 544 | if data_list is None: |
---|
| 545 | data_list = [] |
---|
[c26a64b] | 546 | selected_data_list = [] |
---|
[cc31608] | 547 | if self.batch_on: |
---|
| 548 | page = self.add_fit_page(data=data_list) |
---|
[c26a64b] | 549 | else: |
---|
[cc31608] | 550 | if len(data_list) > MAX_NBR_DATA: |
---|
| 551 | from fitting_widgets import DataDialog |
---|
| 552 | dlg = DataDialog(data_list=data_list, nb_data=MAX_NBR_DATA) |
---|
| 553 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 554 | selected_data_list = dlg.get_data() |
---|
| 555 | dlg.Destroy() |
---|
| 556 | |
---|
| 557 | else: |
---|
| 558 | selected_data_list = data_list |
---|
| 559 | try: |
---|
[5e48acb] | 560 | group_id = wx.NewId() |
---|
[cc31608] | 561 | for data in selected_data_list: |
---|
[5e48acb] | 562 | if data is not None: |
---|
[33b7954] | 563 | # 2D has no same group_id |
---|
| 564 | if data.__class__.__name__ == 'Data2D': |
---|
| 565 | group_id = wx.NewId() |
---|
[5e48acb] | 566 | data.group_id = group_id |
---|
[14774db] | 567 | if group_id not in data.list_group_id: |
---|
| 568 | data.list_group_id.append(group_id) |
---|
| 569 | page = self.add_fit_page(data=[data]) |
---|
[cc31608] | 570 | except: |
---|
| 571 | msg = "Fitting Set_data: " + str(sys.exc_value) |
---|
| 572 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[1b1bbf9] | 573 | |
---|
[f32d144] | 574 | def set_theory(self, theory_list=None): |
---|
[e88ebfd] | 575 | """ |
---|
| 576 | """ |
---|
| 577 | #set the model state for a given theory_state: |
---|
| 578 | for item in theory_list: |
---|
| 579 | try: |
---|
| 580 | _, theory_state = item |
---|
| 581 | self.fit_panel.set_model_state(theory_state) |
---|
| 582 | except: |
---|
| 583 | msg = "Fitting: cannot deal with the theory received" |
---|
| 584 | logging.error("set_theory " + msg + "\n" + str(sys.exc_value)) |
---|
[f32d144] | 585 | wx.PostEvent(self.parent, |
---|
| 586 | StatusEvent(status=msg, info="error")) |
---|
[a0da535] | 587 | |
---|
[b63dc6e] | 588 | def set_state(self, state=None, datainfo=None, format=None): |
---|
[f83b94a] | 589 | """ |
---|
[5062bbf] | 590 | Call-back method for the fit page state reader. |
---|
[8897d66] | 591 | This method is called when a .fitv/.svs file is loaded. |
---|
[5062bbf] | 592 | |
---|
[8897d66] | 593 | : param state: PageState object |
---|
| 594 | : param datainfo: data |
---|
[f83b94a] | 595 | """ |
---|
[90a7bbd] | 596 | #state = self.state_reader.get_state() |
---|
[b63dc6e] | 597 | if state != None: |
---|
[4bee68d] | 598 | state = state.clone() |
---|
[b63dc6e] | 599 | # store fitting state in temp_state |
---|
[f32d144] | 600 | self.temp_state.append(state) |
---|
[b63dc6e] | 601 | else: |
---|
| 602 | self.temp_state = [] |
---|
[ef16f59] | 603 | # index to start with for a new set_state |
---|
| 604 | self.state_index = 0 |
---|
[b63dc6e] | 605 | # state file format |
---|
| 606 | self.sfile_ext = format |
---|
[75fbd17] | 607 | |
---|
| 608 | self.on_set_state_helper(event=None) |
---|
[ef16f59] | 609 | |
---|
[f32d144] | 610 | def on_set_state_helper(self, event=None): |
---|
[4da35bc] | 611 | """ |
---|
[f32d144] | 612 | Set_state_helper. This actually sets state |
---|
[0fd2f27] | 613 | after plotting data from state file. |
---|
[ef16f59] | 614 | |
---|
[f32d144] | 615 | : event: FitStateUpdateEvent called |
---|
[0fd2f27] | 616 | by dataloader.plot_data from guiframe |
---|
[4da35bc] | 617 | """ |
---|
[7a07864] | 618 | if len(self.temp_state) == 0: |
---|
[f32d144] | 619 | if self.state_index == 0 and len(self.mypanels) <= 0 \ |
---|
| 620 | and self.sfile_ext == '.svs': |
---|
[b63dc6e] | 621 | self.temp_state = [] |
---|
| 622 | self.state_index = 0 |
---|
[4da35bc] | 623 | return |
---|
[3eb2811] | 624 | |
---|
[ef16f59] | 625 | try: |
---|
| 626 | # Load fitting state |
---|
| 627 | state = self.temp_state[self.state_index] |
---|
[3eb2811] | 628 | #panel state should have model selection to set_state |
---|
| 629 | if state.formfactorcombobox != None: |
---|
| 630 | #set state |
---|
[75fbd17] | 631 | data = self.parent.create_gui_data(state.data) |
---|
| 632 | data.group_id = state.data.group_id |
---|
[f32d144] | 633 | self.parent.add_data(data_list={data.id: data}) |
---|
[75fbd17] | 634 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 635 | title=data.title)) |
---|
[f95301b] | 636 | #need to be fix later make sure we are sendind guiframe.data |
---|
| 637 | #to panel |
---|
| 638 | state.data = data |
---|
[f32d144] | 639 | page = self.fit_panel.set_state(state) |
---|
[3eb2811] | 640 | else: |
---|
| 641 | #just set data because set_state won't work |
---|
[75fbd17] | 642 | data = self.parent.create_gui_data(state.data) |
---|
| 643 | data.group_id = state.data.group_id |
---|
[f32d144] | 644 | self.parent.add_data(data_list={data.id: data}) |
---|
[f95301b] | 645 | wx.PostEvent(self.parent, NewPlotEvent(plot=data, |
---|
| 646 | title=data.title)) |
---|
[cc31608] | 647 | page = self.add_fit_page([data]) |
---|
[2dbffcc] | 648 | caption = page.window_caption |
---|
[f32d144] | 649 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
[2dbffcc] | 650 | caption=caption) |
---|
[f32d144] | 651 | self.mypanels.append(page) |
---|
[3eb2811] | 652 | |
---|
[9b18735] | 653 | # get ready for the next set_state |
---|
[ef16f59] | 654 | self.state_index += 1 |
---|
[9b18735] | 655 | |
---|
[f32d144] | 656 | #reset state variables to default when all set_state is finished. |
---|
[ef16f59] | 657 | if len(self.temp_state) == self.state_index: |
---|
[9b18735] | 658 | |
---|
[ef16f59] | 659 | self.temp_state = [] |
---|
[b63dc6e] | 660 | #self.state_index = 0 |
---|
[ef16f59] | 661 | # Make sure the user sees the fitting panel after loading |
---|
[f32d144] | 662 | #self.parent.set_perspective(self.perspective) |
---|
[75fbd17] | 663 | self.on_perspective(event=None) |
---|
[ef16f59] | 664 | except: |
---|
[f32d144] | 665 | self.state_index = 0 |
---|
[8897d66] | 666 | self.temp_state = [] |
---|
[ef16f59] | 667 | raise |
---|
[1b14795] | 668 | |
---|
| 669 | def set_param2fit(self, uid, param2fit): |
---|
| 670 | """ |
---|
| 671 | Set the list of param names to fit for fitprobelm |
---|
| 672 | """ |
---|
| 673 | self.page_finder[uid].set_param2fit(param2fit) |
---|
[5e48acb] | 674 | |
---|
| 675 | def set_graph_id(self, uid, graph_id): |
---|
| 676 | """ |
---|
| 677 | Set graph_id for fitprobelm |
---|
| 678 | """ |
---|
| 679 | self.page_finder[uid].set_graph_id(graph_id) |
---|
| 680 | |
---|
| 681 | def get_graph_id(self, uid): |
---|
| 682 | """ |
---|
| 683 | Set graph_id for fitprobelm |
---|
| 684 | """ |
---|
[f32d144] | 685 | return self.page_finder[uid].get_graph_id() |
---|
[5e48acb] | 686 | |
---|
[f32d144] | 687 | def save_fit_state(self, filepath, fitstate): |
---|
[f83b94a] | 688 | """ |
---|
[5062bbf] | 689 | save fit page state into file |
---|
[f83b94a] | 690 | """ |
---|
| 691 | self.state_reader.write(filename=filepath, fitstate=fitstate) |
---|
[55bb249c] | 692 | |
---|
[f7ef313] | 693 | def set_fit_weight(self, uid, flag, is2d=False, fid=None): |
---|
[55bb249c] | 694 | """ |
---|
| 695 | Set the fit weights of a given page for all |
---|
[f32d144] | 696 | its data by default. If fid is provide then set the range |
---|
[55bb249c] | 697 | only for the data with fid as id |
---|
| 698 | :param uid: id corresponding to a fit page |
---|
| 699 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 700 | :param weight: current dy data |
---|
| 701 | """ |
---|
| 702 | if uid in self.page_finder.keys(): |
---|
[f7ef313] | 703 | self.page_finder[uid].set_weight(flag=flag, is2d=is2d) |
---|
[55bb249c] | 704 | |
---|
[2dbffcc] | 705 | def set_fit_range(self, uid, qmin, qmax, fid=None): |
---|
[ca7a626] | 706 | """ |
---|
[2dbffcc] | 707 | Set the fitting range of a given page for all |
---|
[f32d144] | 708 | its data by default. If fid is provide then set the range |
---|
[2dbffcc] | 709 | only for the data with fid as id |
---|
| 710 | :param uid: id corresponding to a fit page |
---|
| 711 | :param fid: id corresponding to a fit problem (data, model) |
---|
| 712 | :param qmin: minimum value of the fit range |
---|
| 713 | :param qmax: maximum value of the fit range |
---|
[ca7a626] | 714 | """ |
---|
[2dbffcc] | 715 | if uid in self.page_finder.keys(): |
---|
[f6933b8] | 716 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax, fid=fid) |
---|
| 717 | |
---|
[f32d144] | 718 | def schedule_for_fit(self, value=0, uid=None): |
---|
[948add7] | 719 | """ |
---|
[5062bbf] | 720 | Set the fit problem field to 0 or 1 to schedule that problem to fit. |
---|
[f32d144] | 721 | Schedule the specified fitproblem or get the fit problem related to |
---|
[5062bbf] | 722 | the current page and set value. |
---|
[f32d144] | 723 | :param value: integer 0 or 1 |
---|
[2dbffcc] | 724 | :param uid: the id related to a page contaning fitting information |
---|
| 725 | """ |
---|
[f32d144] | 726 | if uid in self.page_finder.keys(): |
---|
[66ff250] | 727 | self.page_finder[uid].schedule_tofit(value) |
---|
[ca7a626] | 728 | |
---|
[d89f09b] | 729 | def get_page_finder(self): |
---|
[5062bbf] | 730 | """ |
---|
| 731 | return self.page_finder used also by simfitpage.py |
---|
[f32d144] | 732 | """ |
---|
| 733 | return self.page_finder |
---|
[d89f09b] | 734 | |
---|
[f32d144] | 735 | def set_page_finder(self, modelname, names, values): |
---|
[d89f09b] | 736 | """ |
---|
[5062bbf] | 737 | Used by simfitpage.py to reset a parameter given the string constrainst. |
---|
| 738 | |
---|
[f32d144] | 739 | :param modelname: the name ot the model for with the parameter |
---|
[0fd2f27] | 740 | has to reset |
---|
[5062bbf] | 741 | :param value: can be a string in this case. |
---|
| 742 | :param names: the paramter name |
---|
[f32d144] | 743 | """ |
---|
[66ff250] | 744 | sim_page_id = self.sim_page.uid |
---|
| 745 | for uid, value in self.page_finder.iteritems(): |
---|
[fa02d95] | 746 | if uid != sim_page_id and uid != self.batch_page.uid: |
---|
[6bbeacd4] | 747 | list = value.get_model() |
---|
[f93dfcb] | 748 | model = list[0] |
---|
[6bbeacd4] | 749 | if model.name == modelname: |
---|
| 750 | value.set_model_param(names, values) |
---|
[d89f09b] | 751 | break |
---|
[2db1d66] | 752 | |
---|
[f32d144] | 753 | def split_string(self, item): |
---|
[d89f09b] | 754 | """ |
---|
[5062bbf] | 755 | receive a word containing dot and split it. used to split parameterset |
---|
| 756 | name into model name and parameter name example: :: |
---|
| 757 | |
---|
[3b19ac9] | 758 | paramaterset (item) = M1.A |
---|
[5062bbf] | 759 | Will return model_name = M1 , parameter name = A |
---|
| 760 | |
---|
[d89f09b] | 761 | """ |
---|
[6d8bad4] | 762 | if item.find(".") >= 0: |
---|
[7c8d3093] | 763 | param_names = re.split("\.", item) |
---|
[f32d144] | 764 | model_name = param_names[0] |
---|
[6d91073] | 765 | ##Assume max len is 3; eg., M0.radius.width |
---|
| 766 | if len(param_names) == 3: |
---|
[7c8d3093] | 767 | param_name = param_names[1] + "." + param_names[2] |
---|
[6d91073] | 768 | else: |
---|
[f32d144] | 769 | param_name = param_names[1] |
---|
[7c8d3093] | 770 | return model_name, param_name |
---|
[2296316] | 771 | |
---|
[6fe5100] | 772 | def on_bumps_options(self, event=None): |
---|
| 773 | from bumps.gui.fit_dialog import OpenFitOptions |
---|
| 774 | OpenFitOptions() |
---|
| 775 | |
---|
[66ff250] | 776 | def stop_fit(self, uid): |
---|
[ed2ea6a] | 777 | """ |
---|
[5062bbf] | 778 | Stop the fit engine |
---|
[ed2ea6a] | 779 | """ |
---|
[66ff250] | 780 | if uid in self.fit_thread_list.keys(): |
---|
| 781 | calc_fit = self.fit_thread_list[uid] |
---|
| 782 | if calc_fit is not None and calc_fit.isrunning(): |
---|
| 783 | calc_fit.stop() |
---|
| 784 | msg = "Fit stop!" |
---|
| 785 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[3ddc2c4] | 786 | del self.fit_thread_list[uid] |
---|
[66ff250] | 787 | #set the fit button label of page when fit stop is trigger from |
---|
| 788 | #simultaneous fit pane |
---|
[fa02d95] | 789 | sim_flag = self.sim_page is not None and uid == self.sim_page.uid |
---|
| 790 | batch_flag = self.batch_page is not None and uid == self.batch_page.uid |
---|
| 791 | if sim_flag or batch_flag: |
---|
[66ff250] | 792 | for uid, value in self.page_finder.iteritems(): |
---|
| 793 | if value.get_scheduled() == 1: |
---|
| 794 | if uid in self.fit_panel.opened_pages.keys(): |
---|
| 795 | panel = self.fit_panel.opened_pages[uid] |
---|
[fa02d95] | 796 | panel._on_fit_complete() |
---|
[6bbeacd4] | 797 | |
---|
[2dbffcc] | 798 | def set_smearer(self, uid, smearer, fid, qmin=None, qmax=None, draw=True, |
---|
| 799 | enable_smearer=False): |
---|
| 800 | """ |
---|
| 801 | Get a smear object and store it to a fit problem of fid as id. If proper |
---|
| 802 | flag is enable , will plot the theory with smearing information. |
---|
| 803 | |
---|
| 804 | :param smearer: smear object to allow smearing data of id fid |
---|
| 805 | :param enable_smearer: Define whether or not all (data, model) contained |
---|
| 806 | in the structure of id uid will be smeared before fitting. |
---|
| 807 | :param qmin: the maximum value of the theory plotting range |
---|
| 808 | :param qmax: the maximum value of the theory plotting range |
---|
| 809 | :param draw: Determine if the theory needs to be plot |
---|
[f32d144] | 810 | """ |
---|
[66ff250] | 811 | if uid not in self.page_finder.keys(): |
---|
[2dbffcc] | 812 | return |
---|
| 813 | self.page_finder[uid].enable_smearing(flag=enable_smearer) |
---|
| 814 | self.page_finder[uid].set_smearer(smearer, fid=fid) |
---|
[6bbeacd4] | 815 | if draw: |
---|
| 816 | ## draw model 1D with smeared data |
---|
[f32d144] | 817 | data = self.page_finder[uid].get_fit_data(fid=fid) |
---|
[2dbffcc] | 818 | if data is None: |
---|
| 819 | msg = "set_mearer requires at least data.\n" |
---|
| 820 | msg += "Got data = %s .\n" % str(data) |
---|
[269df2b] | 821 | return |
---|
| 822 | #raise ValueError, msg |
---|
[2dbffcc] | 823 | model = self.page_finder[uid].get_model(fid=fid) |
---|
[6bbeacd4] | 824 | if model is None: |
---|
| 825 | return |
---|
[2dbffcc] | 826 | enable1D = issubclass(data.__class__, Data1D) |
---|
| 827 | enable2D = issubclass(data.__class__, Data2D) |
---|
[6bbeacd4] | 828 | ## if user has already selected a model to plot |
---|
| 829 | ## redraw the model with data smeared |
---|
[2dbffcc] | 830 | smear = self.page_finder[uid].get_smearer(fid=fid) |
---|
[342dc442] | 831 | |
---|
| 832 | # compute weight for the current data |
---|
| 833 | weight = self.page_finder[uid].get_weight(fid=fid) |
---|
| 834 | |
---|
[66ff250] | 835 | self.draw_model(model=model, data=data, page_id=uid, smearer=smear, |
---|
[ba1f0b2] | 836 | enable1D=enable1D, enable2D=enable2D, |
---|
[342dc442] | 837 | qmin=qmin, qmax=qmax, weight=weight) |
---|
[f6aee42] | 838 | self._mac_sleep(0.2) |
---|
| 839 | |
---|
| 840 | def _mac_sleep(self, sec=0.2): |
---|
[2dbffcc] | 841 | """ |
---|
| 842 | Give sleep to MAC |
---|
| 843 | """ |
---|
[f6aee42] | 844 | if ON_MAC: |
---|
[f32d144] | 845 | time.sleep(sec) |
---|
[2dbffcc] | 846 | |
---|
[66ff250] | 847 | def draw_model(self, model, page_id, data=None, smearer=None, |
---|
[67e258c] | 848 | enable1D=True, enable2D=False, |
---|
[5ef55d2] | 849 | state=None, |
---|
[f64a4b7] | 850 | fid=None, |
---|
[fa65e99] | 851 | toggle_mode_on=False, |
---|
[f32d144] | 852 | qmin=None, qmax=None, |
---|
[e3f6ef5] | 853 | update_chisqr=True, weight=None, source='model'): |
---|
[ca7a626] | 854 | """ |
---|
[5062bbf] | 855 | Draw model. |
---|
| 856 | |
---|
| 857 | :param model: the model to draw |
---|
| 858 | :param name: the name of the model to draw |
---|
| 859 | :param data: the data on which the model is based to be drawn |
---|
| 860 | :param description: model's description |
---|
| 861 | :param enable1D: if true enable drawing model 1D |
---|
| 862 | :param enable2D: if true enable drawing model 2D |
---|
| 863 | :param qmin: Range's minimum value to draw model |
---|
| 864 | :param qmax: Range's maximum value to draw model |
---|
| 865 | :param qstep: number of step to divide the x and y-axis |
---|
[2296316] | 866 | :param update_chisqr: update chisqr [bool] |
---|
[ed2ea6a] | 867 | |
---|
[d89f09b] | 868 | """ |
---|
[62f851f] | 869 | #self.weight = weight |
---|
[f32d144] | 870 | if issubclass(data.__class__, Data1D) or not enable2D: |
---|
[f72333f] | 871 | ## draw model 1D with no loaded data |
---|
[f32d144] | 872 | self._draw_model1D(model=model, |
---|
[67e258c] | 873 | data=data, |
---|
[66ff250] | 874 | page_id=page_id, |
---|
[f32d144] | 875 | enable1D=enable1D, |
---|
[67e258c] | 876 | smearer=smearer, |
---|
| 877 | qmin=qmin, |
---|
[f32d144] | 878 | qmax=qmax, |
---|
[f64a4b7] | 879 | fid=fid, |
---|
[62f851f] | 880 | weight=weight, |
---|
[fa65e99] | 881 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 882 | state=state, |
---|
[e3f6ef5] | 883 | update_chisqr=update_chisqr, |
---|
| 884 | source=source) |
---|
[f32d144] | 885 | else: |
---|
[f72333f] | 886 | ## draw model 2D with no initial data |
---|
[ba1f0b2] | 887 | self._draw_model2D(model=model, |
---|
[66ff250] | 888 | page_id=page_id, |
---|
[67e258c] | 889 | data=data, |
---|
| 890 | enable2D=enable2D, |
---|
| 891 | smearer=smearer, |
---|
| 892 | qmin=qmin, |
---|
| 893 | qmax=qmax, |
---|
[f64a4b7] | 894 | fid=fid, |
---|
[d682c92] | 895 | weight=weight, |
---|
[5ef55d2] | 896 | state=state, |
---|
[fa65e99] | 897 | toggle_mode_on=toggle_mode_on, |
---|
[e3f6ef5] | 898 | update_chisqr=update_chisqr, |
---|
| 899 | source=source) |
---|
[2db1d66] | 900 | |
---|
[922497f] | 901 | def onFit(self, uid): |
---|
[ca7a626] | 902 | """ |
---|
[2dbffcc] | 903 | Get series of data, model, associates parameters and range and send then |
---|
[f32d144] | 904 | to series of fit engines. Fit data and model, display result to |
---|
| 905 | corresponding panels. |
---|
[2dbffcc] | 906 | :param uid: id related to the panel currently calling this fit function. |
---|
[ca7a626] | 907 | """ |
---|
[233c121] | 908 | if uid is None: raise RuntimeError("no page to fit") # Should never happen |
---|
| 909 | |
---|
| 910 | sim_page_uid = getattr(self.sim_page, 'uid', None) |
---|
| 911 | batch_page_uid = getattr(self.batch_page, 'uid', None) |
---|
| 912 | |
---|
| 913 | if uid == sim_page_uid: |
---|
| 914 | fit_type = 'simultaneous' |
---|
| 915 | elif uid == batch_page_uid: |
---|
| 916 | fit_type = 'combined_batch' |
---|
[ca7a626] | 917 | else: |
---|
[233c121] | 918 | fit_type = 'single' |
---|
| 919 | |
---|
[f32d144] | 920 | fitter_list = [] |
---|
| 921 | sim_fitter = None |
---|
[233c121] | 922 | if fit_type == 'simultaneous': |
---|
[f32d144] | 923 | #simulatanous fit only one engine need to be created |
---|
| 924 | sim_fitter = Fit(self._fit_engine) |
---|
[fa02d95] | 925 | sim_fitter.fitter_id = self.sim_page.uid |
---|
[f32d144] | 926 | fitter_list.append(sim_fitter) |
---|
[233c121] | 927 | |
---|
[67e258c] | 928 | self.current_pg = None |
---|
[66ff250] | 929 | list_page_id = [] |
---|
[922497f] | 930 | fit_id = 0 |
---|
[233c121] | 931 | for page_id, page_info in self.page_finder.iteritems(): |
---|
[dc613d6] | 932 | # For simulfit (uid give with None), do for-loop |
---|
| 933 | # if uid is specified (singlefit), do it only on the page. |
---|
[233c121] | 934 | if page_id in (sim_page_uid, batch_page_uid): continue |
---|
| 935 | if fit_type == "single" and page_id != uid: continue |
---|
| 936 | |
---|
[d89f09b] | 937 | try: |
---|
[233c121] | 938 | if page_info.get_scheduled() == 1: |
---|
| 939 | page_info.nbr_residuals_computed = 0 |
---|
[66ff250] | 940 | page = self.fit_panel.get_page_by_id(page_id) |
---|
[f32d144] | 941 | self.set_fit_weight(uid=page.uid, |
---|
[7ee79cb] | 942 | flag=page.get_weight_flag(), |
---|
[f32d144] | 943 | is2d=page._is_2D()) |
---|
[233c121] | 944 | if not page.param_toFit: |
---|
| 945 | msg = "No fitting parameters for %s"%page.window_caption |
---|
| 946 | wx.PostEvent(page.parent.parent, |
---|
| 947 | StatusEvent(status=msg, info="error", |
---|
| 948 | type="stop")) |
---|
| 949 | return False |
---|
| 950 | if not page._update_paramv_on_fit(): |
---|
[31469d50] | 951 | msg = "Fitting range or parameter values are" |
---|
[f32d144] | 952 | msg += " invalid in %s" % \ |
---|
[31469d50] | 953 | page.window_caption |
---|
[f32d144] | 954 | wx.PostEvent(page.parent.parent, |
---|
| 955 | StatusEvent(status=msg, info="error", |
---|
[31469d50] | 956 | type="stop")) |
---|
[233c121] | 957 | return False |
---|
| 958 | |
---|
| 959 | pars = [str(element[1]) for element in page.param_toFit] |
---|
| 960 | fitproblem_list = page_info.values() |
---|
[7bd6cfae] | 961 | for fitproblem in fitproblem_list: |
---|
[922497f] | 962 | if sim_fitter is None: |
---|
[f32d144] | 963 | fitter = Fit(self._fit_engine) |
---|
[fa02d95] | 964 | fitter.fitter_id = page_id |
---|
[f32d144] | 965 | fitter_list.append(fitter) |
---|
[922497f] | 966 | else: |
---|
| 967 | fitter = sim_fitter |
---|
[233c121] | 968 | self._add_problem_to_fit(fitproblem=fitproblem, |
---|
[f32d144] | 969 | pars=pars, |
---|
| 970 | fitter=fitter, |
---|
[233c121] | 971 | fit_id=fit_id) |
---|
[922497f] | 972 | fit_id += 1 |
---|
[66ff250] | 973 | list_page_id.append(page_id) |
---|
[233c121] | 974 | page_info.clear_model_param() |
---|
[986da97] | 975 | except KeyboardInterrupt: |
---|
| 976 | msg = "Fitting terminated" |
---|
| 977 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 978 | type="stop")) |
---|
[233c121] | 979 | return True |
---|
[d89f09b] | 980 | except: |
---|
[233c121] | 981 | msg = "Fitting error: %s" % str(sys.exc_value) |
---|
[ba1f0b2] | 982 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
| 983 | type="stop")) |
---|
[233c121] | 984 | return False |
---|
[e54d2c32] | 985 | ## If a thread is already started, stop it |
---|
| 986 | #if self.calc_fit!= None and self.calc_fit.isrunning(): |
---|
| 987 | # self.calc_fit.stop() |
---|
[a2cb1f9] | 988 | msg = "Fitting is in progress..." |
---|
[f32d144] | 989 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) |
---|
[a2cb1f9] | 990 | |
---|
[386ffe1] | 991 | #Handler used for fit engine displayed message |
---|
[66ff250] | 992 | handler = ConsoleUpdate(parent=self.parent, |
---|
| 993 | manager=self, |
---|
| 994 | improvement_delta=0.1) |
---|
[f6aee42] | 995 | self._mac_sleep(0.2) |
---|
[f32d144] | 996 | |
---|
[05fb6d12] | 997 | # batch fit |
---|
[233c121] | 998 | batch_inputs = {} |
---|
| 999 | batch_outputs = {} |
---|
[bf5e985] | 1000 | if fit_type == "simultaneous": |
---|
| 1001 | page = self.sim_page |
---|
| 1002 | elif fit_type == "combined_batch": |
---|
| 1003 | page = self.batch_page |
---|
| 1004 | else: |
---|
| 1005 | page = self.fit_panel.get_page_by_id(uid) |
---|
[233c121] | 1006 | if page.batch_on: |
---|
[f32d144] | 1007 | calc_fit = FitThread(handler=handler, |
---|
| 1008 | fn=fitter_list, |
---|
| 1009 | pars=pars, |
---|
| 1010 | batch_inputs=batch_inputs, |
---|
| 1011 | batch_outputs=batch_outputs, |
---|
| 1012 | page_id=list_page_id, |
---|
| 1013 | completefn=self._batch_fit_complete, |
---|
| 1014 | reset_flag=self.batch_reset_flag) |
---|
[e54d2c32] | 1015 | else: |
---|
| 1016 | ## Perform more than 1 fit at the time |
---|
[58e0c83] | 1017 | calc_fit = FitThread(handler=handler, |
---|
[cc31608] | 1018 | fn=fitter_list, |
---|
[33dd2e5] | 1019 | batch_inputs=batch_inputs, |
---|
| 1020 | batch_outputs=batch_outputs, |
---|
[66ff250] | 1021 | page_id=list_page_id, |
---|
[2296316] | 1022 | updatefn=handler.update_fit, |
---|
[386ffe1] | 1023 | completefn=self._fit_completed) |
---|
[233c121] | 1024 | #self.fit_thread_list[current_page_id] = calc_fit |
---|
| 1025 | self.fit_thread_list[uid] = calc_fit |
---|
[a2cb1f9] | 1026 | calc_fit.queue() |
---|
[233c121] | 1027 | calc_fit.ready(2.5) |
---|
[cc31608] | 1028 | msg = "Fitting is in progress..." |
---|
[f32d144] | 1029 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) |
---|
[cc31608] | 1030 | |
---|
[233c121] | 1031 | return True |
---|
| 1032 | |
---|
[2dbffcc] | 1033 | def remove_plot(self, uid, fid=None, theory=False): |
---|
[2f189dc] | 1034 | """ |
---|
[5062bbf] | 1035 | remove model plot when a fit page is closed |
---|
[2dbffcc] | 1036 | :param uid: the id related to the fitpage to close |
---|
| 1037 | :param fid: the id of the fitproblem(data, model, range,etc) |
---|
[2f189dc] | 1038 | """ |
---|
[2dbffcc] | 1039 | if uid not in self.page_finder.keys(): |
---|
| 1040 | return |
---|
| 1041 | fitproblemList = self.page_finder[uid].get_fit_problem(fid) |
---|
| 1042 | for fitproblem in fitproblemList: |
---|
| 1043 | data = fitproblem.get_fit_data() |
---|
| 1044 | model = fitproblem.get_model() |
---|
| 1045 | plot_id = None |
---|
| 1046 | if model is not None: |
---|
[6d8bad4] | 1047 | plot_id = data.id + model.name |
---|
[2dbffcc] | 1048 | if theory: |
---|
[e1bc51d] | 1049 | plot_id = data.id + model.name |
---|
[2dbffcc] | 1050 | group_id = data.group_id |
---|
| 1051 | wx.PostEvent(self.parent, NewPlotEvent(id=plot_id, |
---|
[f32d144] | 1052 | group_id=group_id, |
---|
| 1053 | action='remove')) |
---|
[edcbd467] | 1054 | |
---|
[2dbffcc] | 1055 | def store_data(self, uid, data_list=None, caption=None): |
---|
[31b0c47] | 1056 | """ |
---|
[2dbffcc] | 1057 | Recieve a list of data and store them ans well as a caption of |
---|
| 1058 | the fit page where they come from. |
---|
| 1059 | :param uid: if related to a fit page |
---|
| 1060 | :param data_list: list of data to fit |
---|
| 1061 | :param caption: caption of the window related to these data |
---|
[31b0c47] | 1062 | """ |
---|
[cc31608] | 1063 | if data_list is None: |
---|
| 1064 | data_list = [] |
---|
[f7ef313] | 1065 | |
---|
[2dbffcc] | 1066 | self.page_finder[uid].set_fit_data(data=data_list) |
---|
| 1067 | if caption is not None: |
---|
| 1068 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[f7ef313] | 1069 | |
---|
[6bbeacd4] | 1070 | def on_add_new_page(self, event=None): |
---|
[edcbd467] | 1071 | """ |
---|
[6bbeacd4] | 1072 | ask fit panel to create a new empty page |
---|
[edcbd467] | 1073 | """ |
---|
| 1074 | try: |
---|
[6bbeacd4] | 1075 | page = self.fit_panel.add_empty_page() |
---|
[6450d8c] | 1076 | # add data associated to the page created |
---|
[f32d144] | 1077 | if page != None: |
---|
[f304194] | 1078 | wx.PostEvent(self.parent, StatusEvent(status="Page Created", |
---|
[31b0c47] | 1079 | info="info")) |
---|
[edcbd467] | 1080 | else: |
---|
[f304194] | 1081 | msg = "Page was already Created" |
---|
[a0da535] | 1082 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1083 | info="warning")) |
---|
[edcbd467] | 1084 | except: |
---|
[2dbffcc] | 1085 | msg = "Creating Fit page: %s"%sys.exc_value |
---|
| 1086 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
[6bbeacd4] | 1087 | |
---|
| 1088 | def add_fit_page(self, data): |
---|
| 1089 | """ |
---|
| 1090 | given a data, ask to the fitting panel to create a new fitting page, |
---|
| 1091 | get this page and store it into the page_finder of this plug-in |
---|
[2dbffcc] | 1092 | :param data: is a list of data |
---|
[6bbeacd4] | 1093 | """ |
---|
| 1094 | page = self.fit_panel.set_data(data) |
---|
[7f76f89] | 1095 | # page could be None when loading state files |
---|
| 1096 | if page == None: |
---|
| 1097 | return page |
---|
[6bbeacd4] | 1098 | #append Data1D to the panel containing its theory |
---|
| 1099 | #if theory already plotted |
---|
[66ff250] | 1100 | if page.uid in self.page_finder: |
---|
[cc31608] | 1101 | data = page.get_data() |
---|
[2dbffcc] | 1102 | theory_data = self.page_finder[page.uid].get_theory_data(data.id) |
---|
[6bbeacd4] | 1103 | if issubclass(data.__class__, Data2D): |
---|
[e88ebfd] | 1104 | data.group_id = wx.NewId() |
---|
[a5701e6] | 1105 | if theory_data is not None: |
---|
[66ff250] | 1106 | group_id = str(page.uid) + " Model1D" |
---|
[f32d144] | 1107 | wx.PostEvent(self.parent, |
---|
[a5701e6] | 1108 | NewPlotEvent(group_id=group_id, |
---|
| 1109 | action="delete")) |
---|
[0fd2f27] | 1110 | self.parent.update_data(prev_data=theory_data, |
---|
[f32d144] | 1111 | new_data=data) |
---|
[6bbeacd4] | 1112 | else: |
---|
| 1113 | if theory_data is not None: |
---|
[66ff250] | 1114 | group_id = str(page.uid) + " Model2D" |
---|
[e88ebfd] | 1115 | data.group_id = theory_data.group_id |
---|
[a5701e6] | 1116 | wx.PostEvent(self.parent, |
---|
| 1117 | NewPlotEvent(group_id=group_id, |
---|
| 1118 | action="delete")) |
---|
[0fd2f27] | 1119 | self.parent.update_data(prev_data=theory_data, |
---|
[f32d144] | 1120 | new_data=data) |
---|
| 1121 | self.store_data(uid=page.uid, data_list=page.get_data_list(), |
---|
[2dbffcc] | 1122 | caption=page.window_caption) |
---|
[33dd2e5] | 1123 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 1124 | self.sim_page.draw_page() |
---|
[fa02d95] | 1125 | if self.batch_page is not None and self.batch_on: |
---|
| 1126 | self.batch_page.draw_page() |
---|
| 1127 | |
---|
[1b1bbf9] | 1128 | return page |
---|
[6bbeacd4] | 1129 | |
---|
[848a2ef] | 1130 | def _onEVT_SLICER_PANEL(self, event): |
---|
| 1131 | """ |
---|
[f32d144] | 1132 | receive and event telling to update a panel with a name starting with |
---|
| 1133 | event.panel_name. this method update slicer panel |
---|
[2e08a9f] | 1134 | for a given interactor. |
---|
[5062bbf] | 1135 | |
---|
[f32d144] | 1136 | :param event: contains type of slicer , paramaters for updating |
---|
[2e08a9f] | 1137 | the panel and panel_name to find the slicer 's panel concerned. |
---|
[848a2ef] | 1138 | """ |
---|
[ae84427] | 1139 | event.panel_name |
---|
[848a2ef] | 1140 | for item in self.parent.panels: |
---|
[0fd2f27] | 1141 | name = event.panel_name |
---|
| 1142 | if self.parent.panels[item].window_caption.startswith(name): |
---|
[848a2ef] | 1143 | self.parent.panels[item].set_slicer(event.type, event.params) |
---|
[df4c3ad] | 1144 | |
---|
[ae84427] | 1145 | #self.parent._mgr.Update() |
---|
[b324aa9] | 1146 | |
---|
[f32d144] | 1147 | def _closed_fitpage(self, event): |
---|
[848a2ef] | 1148 | """ |
---|
[f32d144] | 1149 | request fitpanel to close a given page when its unique data is removed |
---|
[5062bbf] | 1150 | from the plot. close fitpage only when the a loaded data is removed |
---|
[f32d144] | 1151 | """ |
---|
[784e2fa] | 1152 | if event is None or event.data is None: |
---|
| 1153 | return |
---|
[f32d144] | 1154 | if hasattr(event.data, "is_data"): |
---|
[a0da535] | 1155 | if not event.data.is_data or \ |
---|
[66ff250] | 1156 | event.data.__class__.__name__ == "Data1D": |
---|
[f32d144] | 1157 | self.fit_panel.close_page_with_data(event.data) |
---|
[2dbffcc] | 1158 | |
---|
[dc613d6] | 1159 | def _reset_schedule_problem(self, value=0, uid=None): |
---|
[6f023e8] | 1160 | """ |
---|
[5062bbf] | 1161 | unschedule or schedule all fitproblem to be fit |
---|
[6f023e8] | 1162 | """ |
---|
[dc613d6] | 1163 | # case that uid is not specified |
---|
| 1164 | if uid == None: |
---|
| 1165 | for page_id in self.page_finder.keys(): |
---|
| 1166 | self.page_finder[page_id].schedule_tofit(value) |
---|
| 1167 | # when uid is given |
---|
| 1168 | else: |
---|
[2dbffcc] | 1169 | if uid in self.page_finder.keys(): |
---|
| 1170 | self.page_finder[uid].schedule_tofit(value) |
---|
[dc613d6] | 1171 | |
---|
[233c121] | 1172 | def _add_problem_to_fit(self, fitproblem, pars, fitter, fit_id): |
---|
[2140e68] | 1173 | """ |
---|
[2dbffcc] | 1174 | Create and set fit engine with series of data and model |
---|
| 1175 | :param pars: list of fittable parameters |
---|
| 1176 | :param fitter_list: list of fit engine |
---|
[f32d144] | 1177 | :param value: structure storing data mapped to their model, range etc. |
---|
[2140e68] | 1178 | """ |
---|
[922497f] | 1179 | data = fitproblem.get_fit_data() |
---|
| 1180 | model = fitproblem.get_model() |
---|
| 1181 | smearer = fitproblem.get_smearer() |
---|
| 1182 | qmin, qmax = fitproblem.get_range() |
---|
[29a4024] | 1183 | |
---|
[922497f] | 1184 | #Extra list of parameters and their constraints |
---|
| 1185 | listOfConstraint = [] |
---|
| 1186 | param = fitproblem.get_model_param() |
---|
| 1187 | if len(param) > 0: |
---|
| 1188 | for item in param: |
---|
| 1189 | ## check if constraint |
---|
| 1190 | if item[0] != None and item[1] != None: |
---|
[f32d144] | 1191 | listOfConstraint.append((item[0], item[1])) |
---|
| 1192 | new_model = model |
---|
[41661a0] | 1193 | fitter.set_model(new_model, fit_id, pars, data=data, |
---|
| 1194 | constraints=listOfConstraint) |
---|
[f32d144] | 1195 | fitter.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin, |
---|
[922497f] | 1196 | qmax=qmax) |
---|
| 1197 | fitter.select_problem_for_fit(id=fit_id, value=1) |
---|
[7bd6cfae] | 1198 | |
---|
[f32d144] | 1199 | def _onSelect(self, event): |
---|
| 1200 | """ |
---|
| 1201 | when Select data to fit a new page is created .Its reference is |
---|
[5062bbf] | 1202 | added to self.page_finder |
---|
[d89f09b] | 1203 | """ |
---|
[940aca7] | 1204 | panel = self.plot_panel |
---|
| 1205 | if panel == None: |
---|
| 1206 | raise ValueError, "Fitting:_onSelect: NonType panel" |
---|
[67e258c] | 1207 | Plugin.on_perspective(self, event=event) |
---|
[940aca7] | 1208 | self.select_data(panel) |
---|
[5e48acb] | 1209 | |
---|
| 1210 | def select_data(self, panel): |
---|
| 1211 | """ |
---|
| 1212 | """ |
---|
[940aca7] | 1213 | for plottable in panel.graph.plottables: |
---|
[2fff4db] | 1214 | if plottable.__class__.__name__ in ["Data1D", "Theory1D"]: |
---|
[940aca7] | 1215 | data_id = panel.graph.selected_plottable |
---|
| 1216 | if plottable == panel.plots[data_id]: |
---|
[6bbeacd4] | 1217 | data = plottable |
---|
[cc31608] | 1218 | self.add_fit_page(data=[data]) |
---|
[edcbd467] | 1219 | return |
---|
[c77d859] | 1220 | else: |
---|
[6bbeacd4] | 1221 | data = plottable |
---|
[cc31608] | 1222 | self.add_fit_page(data=[data]) |
---|
[1c1436d] | 1223 | |
---|
[66ff250] | 1224 | def update_fit(self, result=None, msg=""): |
---|
| 1225 | """ |
---|
| 1226 | """ |
---|
| 1227 | print "update_fit result", result |
---|
| 1228 | |
---|
[f32d144] | 1229 | def _batch_fit_complete(self, result, pars, page_id, |
---|
[33dd2e5] | 1230 | batch_outputs, batch_inputs, elapsed=None): |
---|
[cc31608] | 1231 | """ |
---|
[f32d144] | 1232 | Display fit result in batch |
---|
[2dbffcc] | 1233 | :param result: list of objects received fromt fit engines |
---|
| 1234 | :param pars: list of fitted parameters names |
---|
| 1235 | :param page_id: list of page ids which called fit function |
---|
| 1236 | :param elapsed: time spent at the fitting level |
---|
[cc31608] | 1237 | """ |
---|
[92a5ac92] | 1238 | self._mac_sleep(0.2) |
---|
| 1239 | uid = page_id[0] |
---|
| 1240 | if uid in self.fit_thread_list.keys(): |
---|
[f32d144] | 1241 | del self.fit_thread_list[uid] |
---|
[92a5ac92] | 1242 | |
---|
[cc31608] | 1243 | self._update_fit_button(page_id) |
---|
[2bb37c3] | 1244 | t1 = time.time() |
---|
| 1245 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
| 1246 | msg = "Fit completed on %s \n" % str_time |
---|
| 1247 | msg += "Duration time: %s s.\n" % str(elapsed) |
---|
[cc31608] | 1248 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
[f32d144] | 1249 | type="stop")) |
---|
[fa02d95] | 1250 | |
---|
[33dd2e5] | 1251 | if batch_outputs is None: |
---|
| 1252 | batch_outputs = {} |
---|
[fa02d95] | 1253 | |
---|
| 1254 | # format batch_outputs |
---|
| 1255 | batch_outputs["Chi2"] = [] |
---|
| 1256 | #Don't like these loops |
---|
[f32d144] | 1257 | # Need to create dictionary of all fitted parameters |
---|
[fa02d95] | 1258 | # since the number of parameters can differ between each fit result |
---|
| 1259 | for list_res in result: |
---|
| 1260 | for res in list_res: |
---|
| 1261 | model, data = res.inputs[0] |
---|
| 1262 | if model is not None and hasattr(model, "model"): |
---|
| 1263 | model = model.model |
---|
| 1264 | #get all fittable parameters of the current model |
---|
| 1265 | for param in model.getParamList(): |
---|
| 1266 | if param not in batch_outputs.keys(): |
---|
| 1267 | batch_outputs[param] = [] |
---|
| 1268 | for param in model.getDispParamList(): |
---|
| 1269 | if not model.is_fittable(param) and \ |
---|
| 1270 | param in batch_outputs.keys(): |
---|
| 1271 | del batch_outputs[param] |
---|
| 1272 | # Add fitted parameters and their error |
---|
| 1273 | for param in res.param_list: |
---|
| 1274 | if param not in batch_outputs.keys(): |
---|
| 1275 | batch_outputs[param] = [] |
---|
| 1276 | err_param = "error on %s" % str(param) |
---|
| 1277 | if err_param not in batch_inputs.keys(): |
---|
| 1278 | batch_inputs[err_param] = [] |
---|
| 1279 | msg = "" |
---|
| 1280 | for list_res in result: |
---|
| 1281 | for res in list_res: |
---|
| 1282 | pid = res.fitter_id |
---|
| 1283 | model, data = res.inputs[0] |
---|
| 1284 | correct_result = False |
---|
| 1285 | if model is not None and hasattr(model, "model"): |
---|
| 1286 | model = model.model |
---|
[fd5ac0d] | 1287 | if data is not None and hasattr(data, "sas_data"): |
---|
| 1288 | data = data.sas_data |
---|
[fa02d95] | 1289 | |
---|
| 1290 | is_data2d = issubclass(data.__class__, Data2D) |
---|
| 1291 | #check consistency of arrays |
---|
| 1292 | if not is_data2d: |
---|
| 1293 | if len(res.theory) == len(res.index[res.index]) and \ |
---|
| 1294 | len(res.index) == len(data.y): |
---|
[46c4ccb] | 1295 | correct_result = True |
---|
[fa02d95] | 1296 | else: |
---|
| 1297 | copy_data = deepcopy(data) |
---|
| 1298 | new_theory = copy_data.data |
---|
| 1299 | new_theory[res.index] = res.theory |
---|
[f32d144] | 1300 | new_theory[res.index == False] = numpy.nan |
---|
[fa02d95] | 1301 | correct_result = True |
---|
| 1302 | #get all fittable parameters of the current model |
---|
| 1303 | param_list = model.getParamList() |
---|
| 1304 | for param in model.getDispParamList(): |
---|
| 1305 | if not model.is_fittable(param) and \ |
---|
| 1306 | param in param_list: |
---|
[f32d144] | 1307 | param_list.remove(param) |
---|
[fa02d95] | 1308 | if not correct_result or res.fitness is None or \ |
---|
| 1309 | not numpy.isfinite(res.fitness) or \ |
---|
| 1310 | numpy.any(res.pvec == None) or not \ |
---|
| 1311 | numpy.all(numpy.isfinite(res.pvec)): |
---|
| 1312 | data_name = str(None) |
---|
| 1313 | if data is not None: |
---|
| 1314 | data_name = str(data.name) |
---|
| 1315 | model_name = str(None) |
---|
| 1316 | if model is not None: |
---|
| 1317 | model_name = str(model.name) |
---|
[f32d144] | 1318 | msg += "Data %s and Model %s did not fit.\n" % (data_name, |
---|
[fa02d95] | 1319 | model_name) |
---|
| 1320 | ERROR = numpy.NAN |
---|
| 1321 | cell = BatchCell() |
---|
| 1322 | cell.label = res.fitness |
---|
| 1323 | cell.value = res.fitness |
---|
| 1324 | batch_outputs["Chi2"].append(ERROR) |
---|
| 1325 | for param in param_list: |
---|
| 1326 | # save value of fixed parameters |
---|
| 1327 | if param not in res.param_list: |
---|
| 1328 | batch_outputs[str(param)].append(ERROR) |
---|
| 1329 | else: |
---|
[4a52223] | 1330 | #save only fitted values |
---|
[fa02d95] | 1331 | batch_outputs[param].append(ERROR) |
---|
| 1332 | batch_inputs["error on %s" % str(param)].append(ERROR) |
---|
| 1333 | else: |
---|
[f32d144] | 1334 | # ToDo: Why sometimes res.pvec comes with numpy.float64? |
---|
[26e1001] | 1335 | # Need to fix it within ScipyEngine |
---|
[f32d144] | 1336 | if res.pvec.__class__ == numpy.float64: |
---|
[26e1001] | 1337 | res.pvec = [res.pvec] |
---|
| 1338 | |
---|
[fa02d95] | 1339 | cell = BatchCell() |
---|
| 1340 | cell.label = res.fitness |
---|
| 1341 | cell.value = res.fitness |
---|
| 1342 | batch_outputs["Chi2"].append(cell) |
---|
| 1343 | # add parameters to batch_results |
---|
| 1344 | for param in param_list: |
---|
| 1345 | # save value of fixed parameters |
---|
| 1346 | if param not in res.param_list: |
---|
| 1347 | batch_outputs[str(param)].append(model.getParam(param)) |
---|
| 1348 | else: |
---|
| 1349 | index = res.param_list.index(param) |
---|
[47d9be79] | 1350 | #save only fitted values |
---|
[fa02d95] | 1351 | batch_outputs[param].append(res.pvec[index]) |
---|
| 1352 | if res.stderr is not None and \ |
---|
| 1353 | len(res.stderr) == len(res.param_list): |
---|
[15f68ce] | 1354 | item = res.stderr[index] |
---|
[fa02d95] | 1355 | batch_inputs["error on %s" % param].append(item) |
---|
[356d2d3] | 1356 | else: |
---|
| 1357 | batch_inputs["error on %s" % param].append('-') |
---|
[fa02d95] | 1358 | model.setParam(param, res.pvec[index]) |
---|
[f32d144] | 1359 | #fill the batch result with emtpy value if not in the current |
---|
[fa02d95] | 1360 | #model |
---|
| 1361 | EMPTY = "-" |
---|
| 1362 | for key in batch_outputs.keys(): |
---|
| 1363 | if key not in param_list and key not in ["Chi2", "Data"]: |
---|
| 1364 | batch_outputs[key].append(EMPTY) |
---|
| 1365 | |
---|
| 1366 | self.page_finder[pid].set_batch_result(batch_inputs=batch_inputs, |
---|
[f32d144] | 1367 | batch_outputs=batch_outputs) |
---|
[fa02d95] | 1368 | |
---|
| 1369 | cpage = self.fit_panel.get_page_by_id(pid) |
---|
| 1370 | cpage._on_fit_complete() |
---|
| 1371 | self.page_finder[pid][data.id].set_result(res) |
---|
| 1372 | fitproblem = self.page_finder[pid][data.id] |
---|
| 1373 | qmin, qmax = fitproblem.get_range() |
---|
[a5f6748] | 1374 | plot_result = False |
---|
| 1375 | if correct_result: |
---|
[fa02d95] | 1376 | if not is_data2d: |
---|
[7619f50] | 1377 | self._complete1D(x=data.x[res.index], y=res.theory, page_id=pid, |
---|
[f32d144] | 1378 | elapsed=None, |
---|
| 1379 | index=res.index, model=model, |
---|
| 1380 | weight=None, fid=data.id, |
---|
| 1381 | toggle_mode_on=False, state=None, |
---|
| 1382 | data=data, update_chisqr=False, |
---|
| 1383 | source='fit', plot_result=plot_result) |
---|
[fa02d95] | 1384 | else: |
---|
| 1385 | self._complete2D(image=new_theory, data=data, |
---|
[f32d144] | 1386 | model=model, |
---|
| 1387 | page_id=pid, elapsed=None, |
---|
| 1388 | index=res.index, |
---|
| 1389 | qmin=qmin, |
---|
| 1390 | qmax=qmax, fid=data.id, weight=None, |
---|
| 1391 | toggle_mode_on=False, state=None, |
---|
| 1392 | update_chisqr=False, |
---|
| 1393 | source='fit', plot_result=plot_result) |
---|
| 1394 | self.on_set_batch_result(page_id=pid, |
---|
| 1395 | fid=data.id, |
---|
| 1396 | batch_outputs=batch_outputs, |
---|
[fa02d95] | 1397 | batch_inputs=batch_inputs) |
---|
[4a52223] | 1398 | |
---|
[986da97] | 1399 | wx.PostEvent(self.parent, StatusEvent(status=msg, error="info", |
---|
[f32d144] | 1400 | type="stop")) |
---|
[1ac202f] | 1401 | # Remove parameters that are not shown |
---|
| 1402 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
| 1403 | tbatch_outputs = {} |
---|
| 1404 | shownkeystr = cpage.get_copy_params() |
---|
| 1405 | for key in batch_outputs.keys(): |
---|
[f32d144] | 1406 | if key in ["Chi2", "Data"] or shownkeystr.count(key) > 0: |
---|
[1ac202f] | 1407 | tbatch_outputs[key] = batch_outputs[key] |
---|
| 1408 | |
---|
[f32d144] | 1409 | wx.CallAfter(self.parent.on_set_batch_result, tbatch_outputs, |
---|
| 1410 | batch_inputs, self.sub_menu) |
---|
[92a5ac92] | 1411 | |
---|
[f64a4b7] | 1412 | def on_set_batch_result(self, page_id, fid, batch_outputs, batch_inputs): |
---|
| 1413 | """ |
---|
| 1414 | """ |
---|
[f32d144] | 1415 | pid = page_id |
---|
[f64a4b7] | 1416 | if fid not in self.page_finder[pid]: |
---|
| 1417 | return |
---|
| 1418 | fitproblem = self.page_finder[pid][fid] |
---|
| 1419 | index = self.page_finder[pid].nbr_residuals_computed - 1 |
---|
[f32d144] | 1420 | residuals = fitproblem.get_residuals() |
---|
[f64a4b7] | 1421 | theory_data = fitproblem.get_theory_data() |
---|
| 1422 | data = fitproblem.get_fit_data() |
---|
| 1423 | model = fitproblem.get_model() |
---|
| 1424 | #fill batch result information |
---|
| 1425 | if "Data" not in batch_outputs.keys(): |
---|
| 1426 | batch_outputs["Data"] = [] |
---|
[79492222] | 1427 | from sas.guiframe.data_processor import BatchCell |
---|
[ec9a635] | 1428 | cell = BatchCell() |
---|
| 1429 | cell.label = data.name |
---|
| 1430 | cell.value = index |
---|
[6b3edc1] | 1431 | |
---|
[ec9a635] | 1432 | if theory_data != None: |
---|
[6b3edc1] | 1433 | #Suucessful fit |
---|
| 1434 | theory_data.id = wx.NewId() |
---|
[b5fe787] | 1435 | theory_data.name = model.name + "[%s]" % str(data.name) |
---|
[e80e704] | 1436 | if issubclass(theory_data.__class__, Data2D): |
---|
| 1437 | group_id = wx.NewId() |
---|
| 1438 | theory_data.group_id = group_id |
---|
| 1439 | if group_id not in theory_data.list_group_id: |
---|
| 1440 | theory_data.list_group_id.append(group_id) |
---|
| 1441 | |
---|
[ec9a635] | 1442 | try: |
---|
| 1443 | # associate residuals plot |
---|
[e80e704] | 1444 | if issubclass(residuals.__class__, Data2D): |
---|
| 1445 | group_id = wx.NewId() |
---|
| 1446 | residuals.group_id = group_id |
---|
| 1447 | if group_id not in residuals.list_group_id: |
---|
| 1448 | residuals.list_group_id.append(group_id) |
---|
[ec9a635] | 1449 | batch_outputs["Chi2"][index].object = [residuals] |
---|
| 1450 | except: |
---|
| 1451 | pass |
---|
| 1452 | |
---|
| 1453 | cell.object = [data, theory_data] |
---|
| 1454 | batch_outputs["Data"].append(cell) |
---|
| 1455 | for key, value in data.meta_data.iteritems(): |
---|
| 1456 | if key not in batch_inputs.keys(): |
---|
| 1457 | batch_inputs[key] = [] |
---|
[98fdccd] | 1458 | #if key.lower().strip() != "loader": |
---|
| 1459 | batch_inputs[key].append(value) |
---|
[ec9a635] | 1460 | param = "temperature" |
---|
| 1461 | if hasattr(data.sample, param): |
---|
| 1462 | if param not in batch_inputs.keys(): |
---|
[f32d144] | 1463 | batch_inputs[param] = [] |
---|
[ec9a635] | 1464 | batch_inputs[param].append(data.sample.temperature) |
---|
| 1465 | |
---|
[6a4002d] | 1466 | def _fit_completed(self, result, page_id, batch_outputs, |
---|
[f32d144] | 1467 | batch_inputs=None, pars=None, elapsed=None): |
---|
[c77d859] | 1468 | """ |
---|
[2dbffcc] | 1469 | Display result of the fit on related panel(s). |
---|
| 1470 | :param result: list of object generated when fit ends |
---|
| 1471 | :param pars: list of names of parameters fitted |
---|
| 1472 | :param page_id: list of page ids which called fit function |
---|
| 1473 | :param elapsed: time spent at the fitting level |
---|
[c77d859] | 1474 | """ |
---|
[2bb37c3] | 1475 | t1 = time.time() |
---|
| 1476 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
| 1477 | msg = "Fit completed on %s \n" % str_time |
---|
| 1478 | msg += "Duration time: %s s.\n" % str(elapsed) |
---|
| 1479 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 1480 | type="stop")) |
---|
[ed4aef2] | 1481 | wx.PostEvent(self.result_panel, PlotResultEvent(result=result)) |
---|
[2bb37c3] | 1482 | self._update_fit_button(page_id) |
---|
[922497f] | 1483 | result = result[0] |
---|
[dc613d6] | 1484 | self.fit_thread_list = {} |
---|
[66ff250] | 1485 | if page_id is None: |
---|
| 1486 | page_id = [] |
---|
[f32d144] | 1487 | ## fit more than 1 model at the same time |
---|
| 1488 | self._mac_sleep(0.2) |
---|
[ca7a626] | 1489 | try: |
---|
[6a4002d] | 1490 | index = 0 |
---|
| 1491 | for uid in page_id: |
---|
| 1492 | res = result[index] |
---|
[7643ba2] | 1493 | if res.fitness is None or \ |
---|
[6a4002d] | 1494 | not numpy.isfinite(res.fitness) or \ |
---|
| 1495 | numpy.any(res.pvec == None) or \ |
---|
| 1496 | not numpy.all(numpy.isfinite(res.pvec)): |
---|
| 1497 | msg = "Fitting did not converge!!!" |
---|
[f32d144] | 1498 | wx.PostEvent(self.parent, |
---|
| 1499 | StatusEvent(status=msg, |
---|
[6a4002d] | 1500 | info="warning", |
---|
| 1501 | type="stop")) |
---|
| 1502 | self._update_fit_button(page_id) |
---|
| 1503 | else: |
---|
[0438933] | 1504 | #set the panel when fit result are float not list |
---|
[f32d144] | 1505 | if res.pvec.__class__ == numpy.float64: |
---|
[0438933] | 1506 | pvec = [res.pvec] |
---|
| 1507 | else: |
---|
| 1508 | pvec = res.pvec |
---|
[f32d144] | 1509 | if res.stderr.__class__ == numpy.float64: |
---|
[0438933] | 1510 | stderr = [res.stderr] |
---|
| 1511 | else: |
---|
| 1512 | stderr = res.stderr |
---|
[7643ba2] | 1513 | cpage = self.fit_panel.get_page_by_id(uid) |
---|
[f32d144] | 1514 | # Make sure we got all results |
---|
[7643ba2] | 1515 | #(CallAfter is important to MAC) |
---|
[8b1d759] | 1516 | try: |
---|
| 1517 | #if res != None: |
---|
[f32d144] | 1518 | wx.CallAfter(cpage.onsetValues, res.fitness, |
---|
| 1519 | res.param_list, |
---|
[8b1d759] | 1520 | pvec, stderr) |
---|
| 1521 | index += 1 |
---|
| 1522 | wx.CallAfter(cpage._on_fit_complete) |
---|
[986da97] | 1523 | except KeyboardInterrupt: |
---|
| 1524 | msg = "Singular point: Fitting Stoped." |
---|
| 1525 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1526 | info="info", |
---|
| 1527 | type="stop")) |
---|
[8b1d759] | 1528 | except: |
---|
[2d0756a5] | 1529 | msg = "Singular point: Fitting Error occurred." |
---|
[f32d144] | 1530 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 1531 | info="error", |
---|
| 1532 | type="stop")) |
---|
[8b1d759] | 1533 | |
---|
[6a4002d] | 1534 | except: |
---|
[2d9c7266] | 1535 | msg = "Fit completed but Following" |
---|
| 1536 | msg += " warning occurred: %s" % sys.exc_value |
---|
| 1537 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="warning", |
---|
[2296316] | 1538 | type="stop")) |
---|
[6a4002d] | 1539 | |
---|
[12cd4ec] | 1540 | def _update_fit_button(self, page_id): |
---|
| 1541 | """ |
---|
| 1542 | Update Fit button when fit stopped |
---|
| 1543 | |
---|
| 1544 | : parameter page_id: fitpage where the button is |
---|
| 1545 | """ |
---|
[37290c4] | 1546 | if page_id.__class__.__name__ != 'list': |
---|
| 1547 | page_id = [page_id] |
---|
[f32d144] | 1548 | for uid in page_id: |
---|
[12cd4ec] | 1549 | page = self.fit_panel.get_page_by_id(uid) |
---|
| 1550 | page._on_fit_complete() |
---|
| 1551 | |
---|
[c77d859] | 1552 | def _on_show_panel(self, event): |
---|
[5062bbf] | 1553 | """ |
---|
| 1554 | """ |
---|
| 1555 | pass |
---|
[da6fd1a] | 1556 | |
---|
| 1557 | def on_reset_batch_flag(self, event): |
---|
| 1558 | """ |
---|
| 1559 | Set batch_reset_flag |
---|
| 1560 | """ |
---|
| 1561 | event.Skip() |
---|
[dafc36f] | 1562 | if self.menu1 == None: |
---|
| 1563 | return |
---|
[da6fd1a] | 1564 | menu_item = self.menu1.FindItemById(self.id_reset_flag) |
---|
| 1565 | flag = menu_item.IsChecked() |
---|
| 1566 | if not flag: |
---|
| 1567 | menu_item.Check(False) |
---|
| 1568 | self.batch_reset_flag = True |
---|
| 1569 | else: |
---|
| 1570 | menu_item.Check(True) |
---|
| 1571 | self.batch_reset_flag = False |
---|
[5062bbf] | 1572 | |
---|
[da6fd1a] | 1573 | ## post a message to status bar |
---|
| 1574 | msg = "Set Chain Fitting: %s" % str(not self.batch_reset_flag) |
---|
[f32d144] | 1575 | wx.PostEvent(self.parent, |
---|
[da6fd1a] | 1576 | StatusEvent(status=msg)) |
---|
| 1577 | |
---|
[386ffe1] | 1578 | |
---|
[c77d859] | 1579 | def _on_slicer_event(self, event): |
---|
| 1580 | """ |
---|
[f32d144] | 1581 | Receive a panel as event and send it to guiframe |
---|
[5062bbf] | 1582 | |
---|
| 1583 | :param event: event containing a panel |
---|
| 1584 | |
---|
[c77d859] | 1585 | """ |
---|
[5062bbf] | 1586 | if event.panel is not None: |
---|
[c77d859] | 1587 | new_panel = event.panel |
---|
[c3435b7f] | 1588 | self.slicer_panels.append(event.panel) |
---|
[c77d859] | 1589 | # Set group ID if available |
---|
[ae84427] | 1590 | event_id = self.parent.popup_panel(event.panel) |
---|
| 1591 | event.panel.uid = event_id |
---|
| 1592 | self.mypanels.append(event.panel) |
---|
[5062bbf] | 1593 | |
---|
[848a2ef] | 1594 | def _onclearslicer(self, event): |
---|
| 1595 | """ |
---|
[5062bbf] | 1596 | Clear the boxslicer when close the panel associate with this slicer |
---|
[848a2ef] | 1597 | """ |
---|
[ae84427] | 1598 | name = event.GetEventObject().frame.GetTitle() |
---|
[c3435b7f] | 1599 | for panel in self.slicer_panels: |
---|
[f32d144] | 1600 | if panel.window_caption == name: |
---|
[c3435b7f] | 1601 | |
---|
[df4c3ad] | 1602 | for item in self.parent.panels: |
---|
[2dbffcc] | 1603 | if hasattr(self.parent.panels[item], "uid"): |
---|
[f32d144] | 1604 | if self.parent.panels[item].uid == panel.base.uid: |
---|
[df4c3ad] | 1605 | self.parent.panels[item].onClearSlicer(event) |
---|
[ae84427] | 1606 | #self.parent._mgr.Update() |
---|
[f32d144] | 1607 | break |
---|
[c3435b7f] | 1608 | break |
---|
[5062bbf] | 1609 | |
---|
[c77d859] | 1610 | def _on_model_panel(self, evt): |
---|
| 1611 | """ |
---|
[5062bbf] | 1612 | react to model selection on any combo box or model menu.plot the model |
---|
| 1613 | |
---|
| 1614 | :param evt: wx.combobox event |
---|
| 1615 | |
---|
[c77d859] | 1616 | """ |
---|
| 1617 | model = evt.model |
---|
[f32d144] | 1618 | uid = evt.uid |
---|
[6bbeacd4] | 1619 | qmin = evt.qmin |
---|
| 1620 | qmax = evt.qmax |
---|
[2dbffcc] | 1621 | caption = evt.caption |
---|
| 1622 | enable_smearer = evt.enable_smearer |
---|
[9237df4] | 1623 | if model == None: |
---|
[c77d859] | 1624 | return |
---|
[2dbffcc] | 1625 | if uid not in self.page_finder.keys(): |
---|
| 1626 | return |
---|
[6bbeacd4] | 1627 | # save the name containing the data name with the appropriate model |
---|
[66ff250] | 1628 | self.page_finder[uid].set_model(model) |
---|
[2dbffcc] | 1629 | self.page_finder[uid].enable_smearing(enable_smearer) |
---|
[66ff250] | 1630 | self.page_finder[uid].set_range(qmin=qmin, qmax=qmax) |
---|
[2dbffcc] | 1631 | self.page_finder[uid].set_fit_tab_caption(caption=caption) |
---|
[33dd2e5] | 1632 | if self.sim_page is not None and not self.batch_on: |
---|
[6bbeacd4] | 1633 | self.sim_page.draw_page() |
---|
[fa02d95] | 1634 | if self.batch_page is not None and self.batch_on: |
---|
| 1635 | self.batch_page.draw_page() |
---|
[5062bbf] | 1636 | |
---|
[2dbffcc] | 1637 | def _update1D(self, x, output): |
---|
[bb18ef1] | 1638 | """ |
---|
[5062bbf] | 1639 | Update the output of plotting model 1D |
---|
[bb18ef1] | 1640 | """ |
---|
[58e0c83] | 1641 | msg = "Plot updating ... " |
---|
[f32d144] | 1642 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="update")) |
---|
[e575db9] | 1643 | |
---|
[ad3fa1e] | 1644 | def _complete1D(self, x, y, page_id, elapsed, index, model, |
---|
[f64a4b7] | 1645 | weight=None, fid=None, |
---|
[f32d144] | 1646 | toggle_mode_on=False, state=None, |
---|
| 1647 | data=None, update_chisqr=True, |
---|
[a5f6748] | 1648 | source='model', plot_result=True): |
---|
[c77d859] | 1649 | """ |
---|
[5062bbf] | 1650 | Complete plotting 1D data |
---|
[f32d144] | 1651 | """ |
---|
[c77d859] | 1652 | try: |
---|
[1ec979d] | 1653 | numpy.nan_to_num(y) |
---|
| 1654 | |
---|
[6bbeacd4] | 1655 | new_plot = Data1D(x=x, y=y) |
---|
[dce84c0] | 1656 | new_plot.is_data = False |
---|
[55d2f7e] | 1657 | new_plot.dy = numpy.zeros(len(y)) |
---|
[6bbeacd4] | 1658 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[f32d144] | 1659 | _yaxis, _yunit = data.get_yaxis() |
---|
| 1660 | _xaxis, _xunit = data.get_xaxis() |
---|
[2dbffcc] | 1661 | new_plot.title = data.name |
---|
[5e48acb] | 1662 | |
---|
[f32d144] | 1663 | new_plot.group_id = data.group_id |
---|
[5e48acb] | 1664 | if new_plot.group_id == None: |
---|
| 1665 | new_plot.group_id = data.group_id |
---|
[657e52c] | 1666 | new_plot.id = str(page_id) + " " + data.name |
---|
[a582d28] | 1667 | #if new_plot.id in self.color_dict: |
---|
[f32d144] | 1668 | # new_plot.custom_color = self.color_dict[new_plot.id] |
---|
[6bbeacd4] | 1669 | #find if this theory was already plotted and replace that plot given |
---|
| 1670 | #the same id |
---|
[2dbffcc] | 1671 | theory_data = self.page_finder[page_id].get_theory_data(fid=data.id) |
---|
[71e5c11] | 1672 | |
---|
| 1673 | if data.is_data: |
---|
| 1674 | data_name = str(data.name) |
---|
| 1675 | else: |
---|
| 1676 | data_name = str(model.__class__.__name__) |
---|
| 1677 | |
---|
[f32d144] | 1678 | new_plot.name = model.name + " [" + data_name +"]" |
---|
[6bbeacd4] | 1679 | new_plot.xaxis(_xaxis, _xunit) |
---|
| 1680 | new_plot.yaxis(_yaxis, _yunit) |
---|
[f32d144] | 1681 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
[2e08a9f] | 1682 | fid=data.id) |
---|
[2dbffcc] | 1683 | self.parent.update_theory(data_id=data.id, theory=new_plot, |
---|
[f32d144] | 1684 | state=state) |
---|
[66ff250] | 1685 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1686 | title = new_plot.title |
---|
[eb4f2fd] | 1687 | batch_on = self.fit_panel.get_page_by_id(page_id).batch_on |
---|
| 1688 | if not batch_on: |
---|
| 1689 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[2dbffcc] | 1690 | title=str(title))) |
---|
[a5f6748] | 1691 | elif plot_result: |
---|
[90a2358] | 1692 | top_data_id = self.fit_panel.get_page_by_id(page_id).data.id |
---|
| 1693 | if data.id == top_data_id: |
---|
| 1694 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[f32d144] | 1695 | title=str(title))) |
---|
[2dbffcc] | 1696 | caption = current_pg.window_caption |
---|
| 1697 | self.page_finder[page_id].set_fit_tab_caption(caption=caption) |
---|
[7643ba2] | 1698 | |
---|
[f32d144] | 1699 | self.page_finder[page_id].set_theory_data(data=new_plot, |
---|
[2dbffcc] | 1700 | fid=data.id) |
---|
| 1701 | if toggle_mode_on: |
---|
[f32d144] | 1702 | wx.PostEvent(self.parent, |
---|
[2dbffcc] | 1703 | NewPlotEvent(group_id=str(page_id) + " Model2D", |
---|
[f32d144] | 1704 | action="Hide")) |
---|
[2296316] | 1705 | else: |
---|
[2dbffcc] | 1706 | if update_chisqr: |
---|
| 1707 | wx.PostEvent(current_pg, |
---|
[2e08a9f] | 1708 | Chi2UpdateEvent(output=self._cal_chisqr( |
---|
| 1709 | data=data, |
---|
[f64a4b7] | 1710 | fid=fid, |
---|
[62f851f] | 1711 | weight=weight, |
---|
[2dbffcc] | 1712 | page_id=page_id, |
---|
| 1713 | index=index))) |
---|
| 1714 | else: |
---|
[f64a4b7] | 1715 | self._plot_residuals(page_id=page_id, data=data, fid=fid, |
---|
[f32d144] | 1716 | index=index, weight=weight) |
---|
[cfbd06a] | 1717 | |
---|
[d522635] | 1718 | msg = "Computation completed!" |
---|
[f32d144] | 1719 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[c77d859] | 1720 | except: |
---|
[6bbeacd4] | 1721 | raise |
---|
[5062bbf] | 1722 | |
---|
[f32d144] | 1723 | def _update2D(self, output, time=None): |
---|
[c77d859] | 1724 | """ |
---|
[5062bbf] | 1725 | Update the output of plotting model |
---|
[c77d859] | 1726 | """ |
---|
[58e0c83] | 1727 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
[a0da535] | 1728 | #updating ... ", type="update")) |
---|
[a2cb1f9] | 1729 | #self.ready_fit() |
---|
[58e0c83] | 1730 | |
---|
[f32d144] | 1731 | def _complete2D(self, image, data, model, page_id, elapsed, index, qmin, |
---|
| 1732 | qmax, fid=None, weight=None, toggle_mode_on=False, state=None, |
---|
[a5f6748] | 1733 | update_chisqr=True, source='model', plot_result=True): |
---|
[c77d859] | 1734 | """ |
---|
[5062bbf] | 1735 | Complete get the result of modelthread and create model 2D |
---|
| 1736 | that can be plot. |
---|
[c77d859] | 1737 | """ |
---|
[1ec979d] | 1738 | numpy.nan_to_num(image) |
---|
[f32d144] | 1739 | new_plot = Data2D(image=image, err_image=data.err_data) |
---|
[743f480] | 1740 | new_plot.name = model.name + '2d' |
---|
[818589a] | 1741 | new_plot.title = "Analytical model 2D " |
---|
[657e52c] | 1742 | new_plot.id = str(page_id) + " " + data.name |
---|
[f3242cf] | 1743 | new_plot.group_id = str(page_id) + " Model2D" |
---|
[2dbffcc] | 1744 | new_plot.detector = data.detector |
---|
| 1745 | new_plot.source = data.source |
---|
[f32d144] | 1746 | new_plot.is_data = False |
---|
[2dbffcc] | 1747 | new_plot.qx_data = data.qx_data |
---|
| 1748 | new_plot.qy_data = data.qy_data |
---|
| 1749 | new_plot.q_data = data.q_data |
---|
| 1750 | new_plot.mask = data.mask |
---|
| 1751 | ## plot boundaries |
---|
| 1752 | new_plot.ymin = data.ymin |
---|
| 1753 | new_plot.ymax = data.ymax |
---|
| 1754 | new_plot.xmin = data.xmin |
---|
| 1755 | new_plot.xmax = data.xmax |
---|
| 1756 | title = data.title |
---|
[71e5c11] | 1757 | |
---|
[dce84c0] | 1758 | new_plot.is_data = False |
---|
[71e5c11] | 1759 | if data.is_data: |
---|
| 1760 | data_name = str(data.name) |
---|
| 1761 | else: |
---|
[743f480] | 1762 | data_name = str(model.__class__.__name__) + '2d' |
---|
[71e5c11] | 1763 | |
---|
| 1764 | if len(title) > 1: |
---|
[940aca7] | 1765 | new_plot.title = "Model2D for %s "% model.name + data_name |
---|
[53cf669] | 1766 | new_plot.name = model.name + " [" + \ |
---|
[940aca7] | 1767 | data_name + "]" |
---|
[fa65e99] | 1768 | theory_data = deepcopy(new_plot) |
---|
[6bbeacd4] | 1769 | |
---|
[f32d144] | 1770 | self.page_finder[page_id].set_theory_data(data=theory_data, |
---|
| 1771 | fid=data.id) |
---|
| 1772 | self.parent.update_theory(data_id=data.id, |
---|
[05319e2] | 1773 | theory=new_plot, |
---|
[f32d144] | 1774 | state=state) |
---|
[66ff250] | 1775 | current_pg = self.fit_panel.get_page_by_id(page_id) |
---|
[ae4ade7] | 1776 | title = new_plot.title |
---|
[a5f6748] | 1777 | if not source == 'fit' and plot_result: |
---|
[763bec8] | 1778 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, |
---|
[ae4ade7] | 1779 | title=title)) |
---|
[2dbffcc] | 1780 | if toggle_mode_on: |
---|
[f32d144] | 1781 | wx.PostEvent(self.parent, |
---|
[2dbffcc] | 1782 | NewPlotEvent(group_id=str(page_id) + " Model1D", |
---|
| 1783 | action="Hide")) |
---|
[2296316] | 1784 | else: |
---|
[2dbffcc] | 1785 | # Chisqr in fitpage |
---|
| 1786 | if update_chisqr: |
---|
| 1787 | wx.PostEvent(current_pg, |
---|
| 1788 | Chi2UpdateEvent(output=self._cal_chisqr(data=data, |
---|
[62f851f] | 1789 | weight=weight, |
---|
[f64a4b7] | 1790 | fid=fid, |
---|
[2dbffcc] | 1791 | page_id=page_id, |
---|
| 1792 | index=index))) |
---|
| 1793 | else: |
---|
[f64a4b7] | 1794 | self._plot_residuals(page_id=page_id, data=data, fid=fid, |
---|
[62f851f] | 1795 | index=index, weight=weight) |
---|
[d522635] | 1796 | msg = "Computation completed!" |
---|
[a0da535] | 1797 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
[6bbeacd4] | 1798 | |
---|
[2dbffcc] | 1799 | def _draw_model2D(self, model, page_id, qmin, |
---|
| 1800 | qmax, |
---|
| 1801 | data=None, smearer=None, |
---|
[a0da535] | 1802 | description=None, enable2D=False, |
---|
[5ef55d2] | 1803 | state=None, |
---|
[f64a4b7] | 1804 | fid=None, |
---|
[62f851f] | 1805 | weight=None, |
---|
[fa65e99] | 1806 | toggle_mode_on=False, |
---|
[e3f6ef5] | 1807 | update_chisqr=True, source='model'): |
---|
[f93dfcb] | 1808 | """ |
---|
[5062bbf] | 1809 | draw model in 2D |
---|
| 1810 | |
---|
| 1811 | :param model: instance of the model to draw |
---|
| 1812 | :param description: the description of the model |
---|
| 1813 | :param enable2D: when True allows to draw model 2D |
---|
| 1814 | :param qmin: the minimum value to draw model 2D |
---|
| 1815 | :param qmax: the maximum value to draw model 2D |
---|
| 1816 | :param qstep: the number of division of Qx and Qy of the model to draw |
---|
[f93dfcb] | 1817 | |
---|
| 1818 | """ |
---|
[c77d859] | 1819 | if not enable2D: |
---|
[2dbffcc] | 1820 | return None |
---|
[c77d859] | 1821 | try: |
---|
| 1822 | from model_thread import Calc2D |
---|
| 1823 | ## If a thread is already started, stop it |
---|
[a0da535] | 1824 | if (self.calc_2D is not None) and self.calc_2D.isrunning(): |
---|
[c77d859] | 1825 | self.calc_2D.stop() |
---|
[9afebe1] | 1826 | ## stop just raises a flag to tell the thread to kill |
---|
| 1827 | ## itself -- see the fix in Calc1D implemented to fix |
---|
| 1828 | ## an actual problem. Seems the fix should also go here |
---|
| 1829 | ## and may be the cause of other noted instabilities |
---|
| 1830 | ## |
---|
| 1831 | ## -PDB August 12, 2014 |
---|
| 1832 | while self.calc_2D.isrunning(): |
---|
| 1833 | time.sleep(0.1) |
---|
[f32d144] | 1834 | self.calc_2D = Calc2D(model=model, |
---|
[a0da535] | 1835 | data=data, |
---|
[66ff250] | 1836 | page_id=page_id, |
---|
[a0da535] | 1837 | smearer=smearer, |
---|
| 1838 | qmin=qmin, |
---|
| 1839 | qmax=qmax, |
---|
[62f851f] | 1840 | weight=weight, |
---|
[f64a4b7] | 1841 | fid=fid, |
---|
[fa65e99] | 1842 | toggle_mode_on=toggle_mode_on, |
---|
[5ef55d2] | 1843 | state=state, |
---|
[2296316] | 1844 | completefn=self._complete2D, |
---|
[e3f6ef5] | 1845 | update_chisqr=update_chisqr, source=source) |
---|
[c77d859] | 1846 | self.calc_2D.queue() |
---|
| 1847 | except: |
---|
[6bbeacd4] | 1848 | raise |
---|
[f72333f] | 1849 | |
---|
[f32d144] | 1850 | def _draw_model1D(self, model, page_id, data, |
---|
[2dbffcc] | 1851 | qmin, qmax, smearer=None, |
---|
[5ef55d2] | 1852 | state=None, |
---|
[62f851f] | 1853 | weight=None, |
---|
[f32d144] | 1854 | fid=None, |
---|
[e3f6ef5] | 1855 | toggle_mode_on=False, update_chisqr=True, source='model', |
---|
[2296316] | 1856 | enable1D=True): |
---|
[c77d859] | 1857 | """ |
---|
[5062bbf] | 1858 | Draw model 1D from loaded data1D |
---|
| 1859 | |
---|
| 1860 | :param data: loaded data |
---|
| 1861 | :param model: the model to plot |
---|
| 1862 | |
---|
[c77d859] | 1863 | """ |
---|
| 1864 | if not enable1D: |
---|
[f32d144] | 1865 | return |
---|
[c77d859] | 1866 | try: |
---|
| 1867 | from model_thread import Calc1D |
---|
| 1868 | ## If a thread is already started, stop it |
---|
[a0da535] | 1869 | if (self.calc_1D is not None) and self.calc_1D.isrunning(): |
---|
[c77d859] | 1870 | self.calc_1D.stop() |
---|
[9afebe1] | 1871 | ## stop just raises the flag -- the thread is supposed to |
---|
| 1872 | ## then kill itself but cannot. Paul Kienzle came up with |
---|
| 1873 | ## this fix to prevent threads from stepping on each other |
---|
| 1874 | ## which was causing a simple custom model to crash Sasview. |
---|
| 1875 | ## We still don't know why the fit sometimes lauched a second |
---|
| 1876 | ## thread -- something which should also be investigated. |
---|
| 1877 | ## The thread approach was implemented in order to be able |
---|
| 1878 | ## to lauch a computation in a separate thread from the GUI so |
---|
| 1879 | ## that the GUI can still respond to user input including |
---|
| 1880 | ## a request to stop the computation. |
---|
| 1881 | ## It seems thus that the whole thread approach used here |
---|
| 1882 | ## May need rethinking |
---|
| 1883 | ## |
---|
| 1884 | ## -PDB August 12, 2014 |
---|
| 1885 | while self.calc_1D.isrunning(): |
---|
| 1886 | time.sleep(0.1) |
---|
[2dbffcc] | 1887 | self.calc_1D = Calc1D(data=data, |
---|
[6bbeacd4] | 1888 | model=model, |
---|
[66ff250] | 1889 | page_id=page_id, |
---|
[a0da535] | 1890 | qmin=qmin, |
---|
| 1891 | qmax=qmax, |
---|
| 1892 | smearer=smearer, |
---|
[5ef55d2] | 1893 | state=state, |
---|
[62f851f] | 1894 | weight=weight, |
---|
[f64a4b7] | 1895 | fid=fid, |
---|
[fa65e99] | 1896 | toggle_mode_on=toggle_mode_on, |
---|
[2296316] | 1897 | completefn=self._complete1D, |
---|
| 1898 | #updatefn = self._update1D, |
---|
[e3f6ef5] | 1899 | update_chisqr=update_chisqr, |
---|
| 1900 | source=source) |
---|
[c77d859] | 1901 | self.calc_1D.queue() |
---|
| 1902 | except: |
---|
[a0da535] | 1903 | msg = " Error occurred when drawing %s Model 1D: " % model.name |
---|
| 1904 | msg += " %s" % sys.exc_value |
---|
| 1905 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[2dbffcc] | 1906 | |
---|
[f32d144] | 1907 | def _cal_chisqr(self, page_id, data, weight, fid=None, index=None): |
---|
[f72333f] | 1908 | """ |
---|
[5062bbf] | 1909 | Get handy Chisqr using the output from draw1D and 2D, |
---|
| 1910 | instead of calling expansive CalcChisqr in guithread |
---|
[f72333f] | 1911 | """ |
---|
[bca2cb9] | 1912 | try: |
---|
| 1913 | data_copy = deepcopy(data) |
---|
| 1914 | except: |
---|
| 1915 | return |
---|
[f72333f] | 1916 | # default chisqr |
---|
| 1917 | chisqr = None |
---|
[2dbffcc] | 1918 | #to compute chisq make sure data has valid data |
---|
[f72333f] | 1919 | # return None if data == None |
---|
[df9e0fc] | 1920 | if not check_data_validity(data_copy) or data_copy == None: |
---|
[2dbffcc] | 1921 | return chisqr |
---|
[df9e0fc] | 1922 | |
---|
[f72333f] | 1923 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 1924 | if data_copy.__class__.__name__ == "Data2D": |
---|
[f32d144] | 1925 | if index == None: |
---|
[dbb3914] | 1926 | index = numpy.ones(len(data_copy.data), dtype=bool) |
---|
[62f851f] | 1927 | if weight != None: |
---|
| 1928 | data_copy.err_data = weight |
---|
[a0da535] | 1929 | # get rid of zero error points |
---|
[f32d144] | 1930 | index = index & (data_copy.err_data != 0) |
---|
| 1931 | index = index & (numpy.isfinite(data_copy.data)) |
---|
| 1932 | fn = data_copy.data[index] |
---|
[ca2bdae] | 1933 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 1934 | if theory_data == None: |
---|
[df9e0fc] | 1935 | return chisqr |
---|
[6bbeacd4] | 1936 | gn = theory_data.data[index] |
---|
[ca2bdae] | 1937 | en = data_copy.err_data[index] |
---|
[f72333f] | 1938 | else: |
---|
| 1939 | # 1 d theory from model_thread is only in the range of index |
---|
[2296316] | 1940 | if index == None: |
---|
[dbb3914] | 1941 | index = numpy.ones(len(data_copy.y), dtype=bool) |
---|
[62f851f] | 1942 | if weight != None: |
---|
| 1943 | data_copy.dy = weight |
---|
[ca2bdae] | 1944 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 1945 | dy = numpy.ones(len(data_copy.y)) |
---|
[f151ddf] | 1946 | else: |
---|
[a0da535] | 1947 | ## Set consitently w/AbstractFitengine: |
---|
| 1948 | # But this should be corrected later. |
---|
[ca2bdae] | 1949 | dy = deepcopy(data_copy.dy) |
---|
[f32d144] | 1950 | dy[dy == 0] = 1 |
---|
| 1951 | fn = data_copy.y[index] |
---|
[eb4f2fd] | 1952 | |
---|
[ca2bdae] | 1953 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 1954 | if theory_data == None: |
---|
[df9e0fc] | 1955 | return chisqr |
---|
[6bbeacd4] | 1956 | gn = theory_data.y |
---|
[4d5fa8c] | 1957 | en = dy[index] |
---|
[162c778] | 1958 | |
---|
[f72333f] | 1959 | # residual |
---|
[162c778] | 1960 | try: |
---|
| 1961 | res = (fn - gn) / en |
---|
| 1962 | except ValueError: |
---|
| 1963 | print "Unmatch lengths %s, %s, %s"% (len(fn), len(gn), len(en)) |
---|
| 1964 | return |
---|
| 1965 | |
---|
[2296316] | 1966 | residuals = res[numpy.isfinite(res)] |
---|
[f72333f] | 1967 | # get chisqr only w/finite |
---|
[2296316] | 1968 | chisqr = numpy.average(residuals * residuals) |
---|
[eb4f2fd] | 1969 | |
---|
[f32d144] | 1970 | self._plot_residuals(page_id=page_id, data=data_copy, |
---|
[f64a4b7] | 1971 | fid=fid, |
---|
[62f851f] | 1972 | weight=weight, index=index) |
---|
[ca2bdae] | 1973 | |
---|
[f72333f] | 1974 | return chisqr |
---|
| 1975 | |
---|
[f64a4b7] | 1976 | def _plot_residuals(self, page_id, weight, fid=None, |
---|
[f32d144] | 1977 | data=None, index=None): |
---|
[2296316] | 1978 | """ |
---|
| 1979 | Plot the residuals |
---|
| 1980 | |
---|
| 1981 | :param data: data |
---|
[f32d144] | 1982 | :param index: index array (bool) |
---|
[2296316] | 1983 | : Note: this is different from the residuals in cal_chisqr() |
---|
| 1984 | """ |
---|
[ca2bdae] | 1985 | data_copy = deepcopy(data) |
---|
[2296316] | 1986 | # Get data: data I, theory I, and data dI in order |
---|
[ca2bdae] | 1987 | if data_copy.__class__.__name__ == "Data2D": |
---|
[2296316] | 1988 | # build residuals |
---|
| 1989 | residuals = Data2D() |
---|
| 1990 | #residuals.copy_from_datainfo(data) |
---|
| 1991 | # Not for trunk the line below, instead use the line above |
---|
[ca2bdae] | 1992 | data_copy.clone_without_data(len(data_copy.data), residuals) |
---|
[2296316] | 1993 | residuals.data = None |
---|
[f32d144] | 1994 | fn = data_copy.data |
---|
[ca2bdae] | 1995 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[f32d144] | 1996 | gn = theory_data.data |
---|
[9fa4a4d] | 1997 | if weight == None: |
---|
| 1998 | en = data_copy.err_data |
---|
| 1999 | else: |
---|
| 2000 | en = weight |
---|
[f32d144] | 2001 | residuals.data = (fn - gn) / en |
---|
| 2002 | residuals.qx_data = data_copy.qx_data |
---|
| 2003 | residuals.qy_data = data_copy.qy_data |
---|
| 2004 | residuals.q_data = data_copy.q_data |
---|
| 2005 | residuals.err_data = numpy.ones(len(residuals.data)) |
---|
[2296316] | 2006 | residuals.xmin = min(residuals.qx_data) |
---|
| 2007 | residuals.xmax = max(residuals.qx_data) |
---|
| 2008 | residuals.ymin = min(residuals.qy_data) |
---|
| 2009 | residuals.ymax = max(residuals.qy_data) |
---|
[f32d144] | 2010 | residuals.q_data = data_copy.q_data |
---|
[ca2bdae] | 2011 | residuals.mask = data_copy.mask |
---|
[2296316] | 2012 | residuals.scale = 'linear' |
---|
| 2013 | # check the lengths |
---|
| 2014 | if len(residuals.data) != len(residuals.q_data): |
---|
| 2015 | return |
---|
| 2016 | else: |
---|
| 2017 | # 1 d theory from model_thread is only in the range of index |
---|
[ca2bdae] | 2018 | if data_copy.dy == None or data_copy.dy == []: |
---|
| 2019 | dy = numpy.ones(len(data_copy.y)) |
---|
[2296316] | 2020 | else: |
---|
[62f851f] | 2021 | if weight == None: |
---|
[90a1440] | 2022 | dy = numpy.ones(len(data_copy.y)) |
---|
[f32d144] | 2023 | ## Set consitently w/AbstractFitengine: |
---|
[2296316] | 2024 | ## But this should be corrected later. |
---|
[90a1440] | 2025 | else: |
---|
[f32d144] | 2026 | dy = weight |
---|
| 2027 | dy[dy == 0] = 1 |
---|
| 2028 | fn = data_copy.y[index] |
---|
[ca2bdae] | 2029 | theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id) |
---|
[2296316] | 2030 | gn = theory_data.y |
---|
| 2031 | en = dy[index] |
---|
| 2032 | # build residuals |
---|
| 2033 | residuals = Data1D() |
---|
[ffdfd23] | 2034 | try: |
---|
| 2035 | residuals.y = (fn - gn) / en |
---|
| 2036 | except: |
---|
| 2037 | msg = "ResidualPlot Error: different # of data points in theory" |
---|
| 2038 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
| 2039 | residuals.y = (fn - gn[index]) / en |
---|
[ca2bdae] | 2040 | residuals.x = data_copy.x[index] |
---|
[2296316] | 2041 | residuals.dy = numpy.ones(len(residuals.y)) |
---|
| 2042 | residuals.dx = None |
---|
| 2043 | residuals.dxl = None |
---|
| 2044 | residuals.dxw = None |
---|
| 2045 | residuals.ytransform = 'y' |
---|
| 2046 | # For latter scale changes |
---|
| 2047 | residuals.xaxis('\\rm{Q} ', 'A^{-1}') |
---|
| 2048 | residuals.yaxis('\\rm{Residuals} ', 'normalized') |
---|
[940aca7] | 2049 | theory_name = str(theory_data.name.split()[0]) |
---|
[2296316] | 2050 | new_plot = residuals |
---|
[940aca7] | 2051 | new_plot.name = "Residuals for " + str(theory_name) + "[" +\ |
---|
| 2052 | str(data.name) +"]" |
---|
[2296316] | 2053 | ## allow to highlight data when plotted |
---|
| 2054 | new_plot.interactive = True |
---|
| 2055 | ## when 2 data have the same id override the 1 st plotted |
---|
[940aca7] | 2056 | new_plot.id = "res" + str(data_copy.id) + str(theory_name) |
---|
[2296316] | 2057 | ##group_id specify on which panel to plot this data |
---|
[5e48acb] | 2058 | group_id = self.page_finder[page_id].get_graph_id() |
---|
| 2059 | if group_id == None: |
---|
| 2060 | group_id = data.group_id |
---|
[f32d144] | 2061 | new_plot.group_id = "res" + str(group_id) |
---|
[2296316] | 2062 | #new_plot.is_data = True |
---|
| 2063 | ##post data to plot |
---|
[f32d144] | 2064 | title = new_plot.name |
---|
| 2065 | self.page_finder[page_id].set_residuals(residuals=new_plot, |
---|
| 2066 | fid=data.id) |
---|
[2de278d9] | 2067 | self.parent.update_theory(data_id=data.id, theory=new_plot) |
---|
[f64a4b7] | 2068 | batch_on = self.fit_panel.get_page_by_id(page_id).batch_on |
---|
[5cf1e5b] | 2069 | if not batch_on: |
---|
| 2070 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) |
---|