source: sasview/src/sas/sasgui/perspectives/fitting/fitting.py @ a5cffe5

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since a5cffe5 was a5cffe5, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

re-enable fitting using weights defined in the GUI. Refs #1205.

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