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

magnetic_scattrelease-4.2.2ticket-1009ticket-1249
Last change on this file since 5251ec6 was 5251ec6, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

improved support for py37 in sasgui

  • Property mode set to 100644
File size: 92.1 KB
Line 
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################################################################################
13from __future__ import print_function
14
15import re
16import sys
17import os
18import wx
19import logging
20import numpy as np
21import time
22from copy import deepcopy
23import traceback
24
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
33from sas.sascalc.dataloader.loader import Loader
34from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit
35from sas.sascalc.fit.pagestate import Reader, PageState, SimFitPageState
36from sas.sascalc.fit import models
37
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
50
51from sas.sasgui.perspectives.calculator.model_editor import TextDialog
52from sas.sasgui.perspectives.calculator.model_editor import EditorWindow
53from sas.sasgui.perspectives.calculator.pyconsole import PyConsole
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
64
65logger = logging.getLogger(__name__)
66
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    """
82    def __init__(self):
83        PluginBase.__init__(self, name="Fitting")
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
130        logger.info("Fitting plug-in started")
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:
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
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
209        self.id_result_panel = wx.NewId()
210        self.menu1.Append(self.id_result_panel, "Fit Results", "Show fit results panel")
211        wx.EVT_MENU(owner, self.id_result_panel, self.on_fit_results)
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()
236        self.menu1.AppendMenu(self.id_edit, "Plugin Model Operations",
237                              self.edit_model_menu)
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        """
245        event_id = event.GetId()
246        label = self.edit_menu.GetLabel(event_id)
247        filename = os.path.join(models.find_plugins_dir(), label)
248        frame = PyConsole(parent=self.parent, manager=self,
249                          panel=self.fit_panel,
250                          title='Advanced Plugin Model Editor',
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        """
259        event_id = event.GetId()
260        label = self.delete_menu.GetLabel(event_id)
261        toks = os.path.splitext(label)
262        path = os.path.join(models.find_plugins_dir(), toks[0])
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
267        try:
268            for ext in ['.py', '.pyc']:
269                p_path = path + ext
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
274                os.remove(p_path)
275            self.update_custom_combo()
276            if os.path.isfile(p_path):
277                msg = "Sorry! unable to delete the default "
278                msg += "plugin model... \n"
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')
284                #evt = StatusEvent(status=msg, type='stop', info='warning')
285                #wx.PostEvent(self.parent, evt)
286            else:
287                self.delete_menu.Delete(event_id)
288                for item in self.edit_menu.GetMenuItems():
289                    if item.GetLabel() == label:
290                        self.edit_menu.DeleteItem(item)
291                        msg = "The plugin model, %s, has been deleted." % label
292                        evt = StatusEvent(status=msg, type='stop', info='info')
293                        wx.PostEvent(self.parent, evt)
294                        break
295        except Exception:
296            traceback.print_exc()
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        """
304        event_id = event.GetId()
305        model_manager = models.ModelManager()
306        model_list = model_manager.composable_models()
307        plug_dir = models.find_plugins_dir()
308        textdial = TextDialog(None, self, wx.ID_ANY, 'Easy Sum/Multi(p1, p2) Editor',
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        """
318        if self.new_model_frame is not None:
319            self.new_model_frame.Show(False)
320            self.new_model_frame.Show(True)
321        else:
322            event_id = event.GetId()
323            dir_path = models.find_plugins_dir()
324            title = "New Plugin Model Function"
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
330    def load_plugin_models(self, event):
331        """
332        Update of models in plugin_models folder
333        """
334        event_id = event.GetId()
335        self.update_custom_combo()
336
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)
345            new_pmodel_list = self.fit_panel.reset_pmodel_list()
346            if not new_pmodel_list:
347                return
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
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
358            for uid, page in self.fit_panel.opened_pages.items():
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
388                # Reset structure factor combo box; even if the model list
389                # hasn't changed, the model may have.  Show the structure
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)
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
419        except Exception as exc:
420            logger.error("update_custom_combo: %s", exc)
421
422    def set_edit_menu(self, owner):
423        """
424        Set list of the edit model menu labels
425        """
426        wx_id = wx.NewId()
427        #new_model_menu = wx.Menu()
428        self.edit_model_menu.Append(wx_id, 'New Plugin Model',
429                                    'Add a new model function')
430        wx.EVT_MENU(owner, wx_id, self.make_new_model)
431
432        wx_id = wx.NewId()
433        self.edit_model_menu.Append(wx_id, 'Sum|Multi(p1, p2)',
434                                    'Sum of two model functions')
435        wx.EVT_MENU(owner, wx_id, self.make_sum_model)
436
437        e_id = wx.NewId()
438        self.edit_menu = wx.Menu()
439        self.edit_model_menu.AppendMenu(e_id,
440                                    'Advanced Plugin Editor', self.edit_menu)
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,
446                                        'Delete Plugin Models', self.delete_menu)
447        self.set_edit_menu_helper(owner, self.delete_custom_model)
448
449        wx_id = wx.NewId()
450        self.edit_model_menu.Append(wx_id, 'Load Plugin Models',
451          '(Re)Load all models present in user plugin_models folder')
452        wx.EVT_MENU(owner, wx_id, self.load_plugin_models)
453
454    def set_edit_menu_helper(self, owner=None, menu=None):
455        """
456        help for setting list of the edit model menu labels
457        """
458        if menu is None:
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:
477                    wx_id = wx.NewId()
478                    submenu.Append(wx_id, name)
479                    wx.EVT_MENU(owner, wx_id, menu)
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        """
498        event_id = event.GetId()
499        caption = "Const & Simul Fit"
500        page = self.sim_page
501        if event_id == self.id_batchfit:
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
513        if page is not None:
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)
587
588        # CRUFT: EVT_FITTER_CHANGED is not None for bumps 0.7.5.9 and above
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
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:
623            self.add_fit_page(data=data_list)
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)
643                        self.add_fit_page(data=[data])
644            except Exception as exc:
645                msg = "Fitting set_data: " + str(exc)
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)
656            except Exception as exc:
657                msg = "Fitting: cannot deal with the theory received"
658                evt = StatusEvent(status=msg, info="error")
659                logger.error("set_theory " + msg + "\n" + str(exc))
660                wx.PostEvent(self.parent, evt)
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        """
670        if isinstance(state, PageState):
671            state = state.clone()
672            self.temp_state.append(state)
673        elif isinstance(state, SimFitPageState):
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)
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
705            if state.formfactorcombobox is not None:
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,
711                             title=data.title))
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,
722                             title=data.title))
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(),
726                                caption=caption)
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        """
778        # If we are not dealing with a specific fit problem, then
779        # there is no point setting the weights.
780        if fid is None:
781            return
782        if uid in self.page_finder:
783            self.page_finder[uid].set_weight(flag=flag, is2d=is2d)
784
785    def set_fit_range(self, uid, qmin, qmax, fid=None):
786        """
787        Set the fitting range of a given page for all
788        its data by default. If fid is provide then set the range
789        only for the data with fid as id
790        :param uid: id corresponding to a fit page
791        :param fid: id corresponding to a fit problem (data, model)
792        :param qmin: minimum  value of the fit range
793        :param qmax: maximum  value of the fit range
794        """
795        if uid in self.page_finder:
796            self.page_finder[uid].set_range(qmin=qmin, qmax=qmax, fid=fid)
797
798    def schedule_for_fit(self, value=0, uid=None):
799        """
800        Set the fit problem field to 0 or 1 to schedule that problem to fit.
801        Schedule the specified fitproblem or get the fit problem related to
802        the current page and set value.
803        :param value: integer 0 or 1
804        :param uid: the id related to a page containing fitting information
805        """
806        if uid in self.page_finder:
807            self.page_finder[uid].schedule_tofit(value)
808
809    def get_page_finder(self):
810        """
811        return self.page_finder used also by simfitpage.py
812        """
813        return self.page_finder
814
815    def set_page_finder(self, modelname, names, values):
816        """
817        Used by simfitpage.py to reset a parameter given the string constrainst.
818
819        :param modelname: the name of the model for with the parameter
820                            has to reset
821        :param value: can be a string in this case.
822        :param names: the parameter name
823        """
824        sim_page_id = self.sim_page.uid
825        for uid, value in self.page_finder.items():
826            if uid != sim_page_id and uid != self.batch_page.uid:
827                model_list = value.get_model()
828                model = model_list[0]
829                if model.name == modelname:
830                    value.set_model_param(names, values)
831                    break
832
833    def split_string(self, item):
834        """
835        receive a word containing dot and split it. used to split parameterset
836        name into model name and parameter name example: ::
837
838            parameterset (item) = M1.A
839            Will return model_name = M1 , parameter name = A
840
841        """
842        if item.find(".") >= 0:
843            param_names = re.split(r"\.", item)
844            model_name = param_names[0]
845            ##Assume max len is 3; eg., M0.radius.width
846            if len(param_names) == 3:
847                param_name = param_names[1] + "." + param_names[2]
848            else:
849                param_name = param_names[1]
850            return model_name, param_name
851
852    def on_bumps_options(self, event=None):
853        """
854        Open the bumps options panel.
855        """
856        show_fit_config(self.parent, help=self.on_help)
857
858    def on_fitter_changed(self, event):
859        self._set_fitter_label(event.config)
860
861    def _set_fitter_label(self, config):
862        self.fit_panel.parent.SetTitle(self.fit_panel.window_name
863                                       + " - Active Fitting Optimizer: "
864                                       + config.selected_name)
865
866    def on_help(self, algorithm_id):
867        _TreeLocation = "user/sasgui/perspectives/fitting/optimizer.html"
868        _anchor = "#fit-"+algorithm_id
869        DocumentationWindow(self.parent, wx.ID_ANY, _TreeLocation, _anchor, "Optimizer Help")
870
871
872    def on_fit_results(self, event=None):
873        """
874        Make the Fit Results panel visible.
875        """
876        self.result_frame.Show()
877        self.result_frame.Raise()
878
879    def on_gpu_options(self, event=None):
880        """
881        Make the Fit Results panel visible.
882        """
883        dialog = GpuOptions(None, wx.ID_ANY, "")
884        dialog.Show()
885
886    def stop_fit(self, uid):
887        """
888        Stop the fit
889        """
890        if uid in self.fit_thread_list:
891            calc_fit = self.fit_thread_list[uid]
892            if calc_fit is not  None and calc_fit.isrunning():
893                calc_fit.stop()
894                msg = "Fit stop!"
895                wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop"))
896            del self.fit_thread_list[uid]
897        #set the fit button label of page when fit stop is trigger from
898        #simultaneous fit pane
899        sim_flag = self.sim_page is not None and uid == self.sim_page.uid
900        batch_flag = self.batch_page is not None and uid == self.batch_page.uid
901        if sim_flag or batch_flag:
902            for uid, value in self.page_finder.items():
903                if value.get_scheduled() == 1:
904                    if uid in self.fit_panel.opened_pages:
905                        panel = self.fit_panel.opened_pages[uid]
906                        panel._on_fit_complete()
907
908    def set_smearer(self, uid, smearer, fid, qmin=None, qmax=None, draw=True,
909                    enable_smearer=False):
910        """
911        Get a smear object and store it to a fit problem of fid as id. If proper
912        flag is enable , will plot the theory with smearing information.
913
914        :param smearer: smear object to allow smearing data of id fid
915        :param enable_smearer: Define whether or not all (data, model) contained
916            in the structure of id uid will be smeared before fitting.
917        :param qmin: the maximum value of the theory plotting range
918        :param qmax: the maximum value of the theory plotting range
919        :param draw: Determine if the theory needs to be plot
920        """
921        if uid not in self.page_finder:
922            return
923        self.page_finder[uid].enable_smearing(flag=enable_smearer)
924        self.page_finder[uid].set_smearer(smearer, fid=fid)
925        if draw:
926            ## draw model 1D with smeared data
927            data = self.page_finder[uid].get_fit_data(fid=fid)
928            if data is None:
929                msg = "set_mearer requires at least data.\n"
930                msg += "Got data = %s .\n" % str(data)
931                return
932                #raise ValueError(msg)
933            model = self.page_finder[uid].get_model(fid=fid)
934            if model is None:
935                return
936            enable1D = issubclass(data.__class__, Data1D)
937            enable2D = issubclass(data.__class__, Data2D)
938            ## if user has already selected a model to plot
939            ## redraw the model with data smeared
940            smear = self.page_finder[uid].get_smearer(fid=fid)
941
942            # compute weight for the current data
943            weight = self.page_finder[uid].get_weight(fid=fid)
944
945            self.draw_model(model=model, data=data, page_id=uid, smearer=smear,
946                            enable1D=enable1D, enable2D=enable2D,
947                            qmin=qmin, qmax=qmax, weight=weight)
948
949    def draw_model(self, model, page_id, data=None, smearer=None,
950                   enable1D=True, enable2D=False,
951                   state=None,
952                   fid=None,
953                   toggle_mode_on=False,
954                   qmin=None, qmax=None,
955                   update_chisqr=True, weight=None, source='model'):
956        """
957        Draw model.
958
959        :param model: the model to draw
960        :param name: the name of the model to draw
961        :param data: the data on which the model is based to be drawn
962        :param description: model's description
963        :param enable1D: if true enable drawing model 1D
964        :param enable2D: if true enable drawing model 2D
965        :param qmin:  Range's minimum value to draw model
966        :param qmax:  Range's maximum value to draw model
967        :param qstep: number of step to divide the x and y-axis
968        :param update_chisqr: update chisqr [bool]
969
970        """
971        #self.weight = weight
972        if issubclass(data.__class__, Data1D) or not enable2D:
973            ## draw model 1D with no loaded data
974            self._draw_model1D(model=model,
975                               data=data,
976                               page_id=page_id,
977                               enable1D=enable1D,
978                               smearer=smearer,
979                               qmin=qmin,
980                               qmax=qmax,
981                               fid=fid,
982                               weight=weight,
983                               toggle_mode_on=toggle_mode_on,
984                               state=state,
985                               update_chisqr=update_chisqr,
986                               source=source)
987        else:
988            ## draw model 2D with no initial data
989            self._draw_model2D(model=model,
990                               page_id=page_id,
991                               data=data,
992                               enable2D=enable2D,
993                               smearer=smearer,
994                               qmin=qmin,
995                               qmax=qmax,
996                               fid=fid,
997                               weight=weight,
998                               state=state,
999                               toggle_mode_on=toggle_mode_on,
1000                               update_chisqr=update_chisqr,
1001                               source=source)
1002
1003    def onFit(self, uid):
1004        """
1005        Get series of data, model, associates parameters and range and send then
1006        to  series of fitters. Fit data and model, display result to
1007        corresponding panels.
1008        :param uid: id related to the panel currently calling this fit function.
1009        """
1010        if uid is None:
1011            raise RuntimeError("no page to fit") # Should never happen
1012
1013        sim_page_uid = getattr(self.sim_page, 'uid', None)
1014        batch_page_uid = getattr(self.batch_page, 'uid', None)
1015
1016        if uid == sim_page_uid:
1017            fit_type = 'simultaneous'
1018        elif uid == batch_page_uid:
1019            fit_type = 'combined_batch'
1020        else:
1021            fit_type = 'single'
1022
1023        fitter_list = []
1024        sim_fitter = None
1025        if fit_type == 'simultaneous':
1026            # for simultaneous fitting only one fitter is needed
1027            sim_fitter = Fit()
1028            sim_fitter.fitter_id = self.sim_page.uid
1029            fitter_list.append(sim_fitter)
1030
1031        self.current_pg = None
1032        list_page_id = []
1033        fit_id = 0
1034        for page_id, page_info in self.page_finder.items():
1035            # For simulfit (uid give with None), do for-loop
1036            # if uid is specified (singlefit), do it only on the page.
1037            if page_id in (sim_page_uid, batch_page_uid): continue
1038            if fit_type == "single" and page_id != uid: continue
1039
1040            try:
1041                if page_info.get_scheduled() == 1:
1042                    page_info.nbr_residuals_computed = 0
1043                    page = self.fit_panel.get_page_by_id(page_id)
1044                    self.set_fit_weight(uid=page.uid,
1045                                        flag=page.get_weight_flag(),
1046                                        is2d=page._is_2D())
1047                    if not page.param_toFit:
1048                        msg = "No fitting parameters for %s" % page.window_caption
1049                        evt = StatusEvent(status=msg, info="error", type="stop")
1050                        wx.PostEvent(page.parent.parent, evt)
1051                        return False
1052                    if not page._update_paramv_on_fit():
1053                        msg = "Fitting range or parameter values are"
1054                        msg += " invalid in %s" % \
1055                                    page.window_caption
1056                        evt = StatusEvent(status=msg, info="error", type="stop")
1057                        wx.PostEvent(page.parent.parent, evt)
1058                        return False
1059
1060                    pars = [str(element[1]) for element in page.param_toFit]
1061                    for fitproblem in page_info.values():
1062                        if sim_fitter is None:
1063                            fitter = Fit()
1064                            fitter.fitter_id = page_id
1065                            fitter_list.append(fitter)
1066                        else:
1067                            fitter = sim_fitter
1068                        self._add_problem_to_fit(fitproblem=fitproblem,
1069                                                 pars=pars,
1070                                                 fitter=fitter,
1071                                                 fit_id=fit_id)
1072                        fit_id += 1
1073                    list_page_id.append(page_id)
1074                    page_info.clear_model_param()
1075            except KeyboardInterrupt:
1076                msg = "Fitting terminated"
1077                evt = StatusEvent(status=msg, info="info", type="stop")
1078                wx.PostEvent(self.parent, evt)
1079                return True
1080            except Exception as exc:
1081                raise
1082                msg = "Fitting error: %s" % exc
1083                evt = StatusEvent(status=msg, info="error", type="stop")
1084                wx.PostEvent(self.parent, evt)
1085                return False
1086        ## If a thread is already started, stop it
1087        #if self.calc_fitis not None and self.calc_fit.isrunning():
1088        #    self.calc_fit.stop()
1089        msg = "Fitting is in progress..."
1090        wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress"))
1091
1092        #Handler used to display fit message
1093        handler = ConsoleUpdate(parent=self.parent,
1094                                manager=self,
1095                                improvement_delta=0.1)
1096
1097        # batch fit
1098        batch_inputs = {}
1099        batch_outputs = {}
1100        if fit_type == "simultaneous":
1101            page = self.sim_page
1102        elif fit_type == "combined_batch":
1103            page = self.batch_page
1104        else:
1105            page = self.fit_panel.get_page_by_id(uid)
1106        if page.batch_on:
1107            calc_fit = FitThread(handler=handler,
1108                                 fn=fitter_list,
1109                                 pars=pars,
1110                                 batch_inputs=batch_inputs,
1111                                 batch_outputs=batch_outputs,
1112                                 page_id=list_page_id,
1113                                 completefn=self._batch_fit_complete,
1114                                 reset_flag=self.batch_reset_flag)
1115        else:
1116            ## Perform more than 1 fit at the time
1117            calc_fit = FitThread(handler=handler,
1118                                 fn=fitter_list,
1119                                 batch_inputs=batch_inputs,
1120                                 batch_outputs=batch_outputs,
1121                                 page_id=list_page_id,
1122                                 updatefn=handler.update_fit,
1123                                 completefn=self._fit_completed)
1124        #self.fit_thread_list[current_page_id] = calc_fit
1125        self.fit_thread_list[uid] = calc_fit
1126        calc_fit.queue()
1127        calc_fit.ready(2.5)
1128        msg = "Fitting is in progress..."
1129        wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress"))
1130
1131        return True
1132
1133    def remove_plot(self, uid, fid=None, theory=False):
1134        """
1135        remove model plot when a fit page is closed
1136        :param uid: the id related to the fitpage to close
1137        :param fid: the id of the fitproblem(data, model, range,etc)
1138        """
1139        if uid not in self.page_finder:
1140            return
1141        fitproblemList = self.page_finder[uid].get_fit_problem(fid)
1142        for fitproblem in fitproblemList:
1143            data = fitproblem.get_fit_data()
1144            model = fitproblem.get_model()
1145            plot_id = None
1146            if model is not None:
1147                plot_id = data.id + model.name
1148            if theory:
1149                plot_id = data.id + model.name
1150            group_id = data.group_id
1151            wx.PostEvent(self.parent, NewPlotEvent(id=plot_id,
1152                                                   group_id=group_id,
1153                                                   action='remove'))
1154
1155    def store_data(self, uid, data_list=None, caption=None):
1156        """
1157        Receive a list of data and store them ans well as a caption of
1158        the fit page where they come from.
1159        :param uid: if related to a fit page
1160        :param data_list: list of data to fit
1161        :param caption: caption of the window related to these data
1162        """
1163        if data_list is None:
1164            data_list = []
1165
1166        self.page_finder[uid].set_fit_data(data=data_list)
1167        if caption is not None:
1168            self.page_finder[uid].set_fit_tab_caption(caption=caption)
1169
1170    def on_add_new_page(self, event=None):
1171        """
1172        ask fit panel to create a new empty page
1173        """
1174        try:
1175            page = self.fit_panel.add_empty_page()
1176            # add data associated to the page created
1177            if page is not None:
1178                evt = StatusEvent(status="Page Created", info="info")
1179                wx.PostEvent(self.parent, evt)
1180            else:
1181                msg = "Page was already Created"
1182                evt = StatusEvent(status=msg, info="warning")
1183                wx.PostEvent(self.parent, evt)
1184        except Exception as exc:
1185            msg = "Creating Fit page: %s" % exc
1186            wx.PostEvent(self.parent, StatusEvent(status=msg, info="error"))
1187
1188    def add_fit_page(self, data):
1189        """
1190        given a data, ask to the fitting panel to create a new fitting page,
1191        get this page and store it into the page_finder of this plug-in
1192        :param data: is a list of data
1193        """
1194        page = self.fit_panel.set_data(data)
1195        # page could be None when loading state files
1196        if page is None:
1197            return page
1198        #append Data1D to the panel containing its theory
1199        #if theory already plotted
1200        if page.uid in self.page_finder:
1201            data = page.get_data()
1202            theory_data = self.page_finder[page.uid].get_theory_data(data.id)
1203            if issubclass(data.__class__, Data2D):
1204                data.group_id = wx.NewId()
1205                if theory_data is not None:
1206                    group_id = str(page.uid) + " Model1D"
1207                    wx.PostEvent(self.parent,
1208                                 NewPlotEvent(group_id=group_id,
1209                                              action="delete"))
1210                    self.parent.update_data(prev_data=theory_data,
1211                                            new_data=data)
1212            else:
1213                if theory_data is not None:
1214                    group_id = str(page.uid) + " Model2D"
1215                    data.group_id = theory_data.group_id
1216                    wx.PostEvent(self.parent,
1217                                 NewPlotEvent(group_id=group_id,
1218                                              action="delete"))
1219                    self.parent.update_data(prev_data=theory_data,
1220                                            new_data=data)
1221        self.store_data(uid=page.uid, data_list=page.get_data_list(),
1222                        caption=page.window_caption)
1223        if self.sim_page is not None and not self.batch_on:
1224            self.sim_page.draw_page()
1225        if self.batch_page is not None and self.batch_on:
1226            self.batch_page.draw_page()
1227
1228        return page
1229
1230    def _onEVT_SLICER_PANEL(self, event):
1231        """
1232        receive and event telling to update a panel with a name starting with
1233        event.panel_name. this method update slicer panel
1234        for a given interactor.
1235
1236        :param event: contains type of slicer , parameters for updating
1237            the panel and panel_name to find the slicer 's panel concerned.
1238        """
1239        event.panel_name
1240        for item in self.parent.panels:
1241            name = event.panel_name
1242            if self.parent.panels[item].window_caption.startswith(name):
1243                self.parent.panels[item].set_slicer(event.type, event.params)
1244
1245        #self.parent._mgr.Update()
1246
1247    def _closed_fitpage(self, event):
1248        """
1249        request fitpanel to close a given page when its unique data is removed
1250        from the plot. close fitpage only when the a loaded data is removed
1251        """
1252        if event is None or event.data is None:
1253            return
1254        if hasattr(event.data, "is_data"):
1255            if not event.data.is_data or \
1256                event.data.__class__.__name__ == "Data1D":
1257                self.fit_panel.close_page_with_data(event.data)
1258
1259    def _reset_schedule_problem(self, value=0, uid=None):
1260        """
1261        unschedule or schedule all fitproblem to be fit
1262        """
1263        # case that uid is not specified
1264        if uid is None:
1265            for page_id in self.page_finder:
1266                self.page_finder[page_id].schedule_tofit(value)
1267        # when uid is given
1268        else:
1269            if uid in self.page_finder:
1270                self.page_finder[uid].schedule_tofit(value)
1271
1272    def _add_problem_to_fit(self, fitproblem, pars, fitter, fit_id):
1273        """
1274        Create and set fitter with series of data and model
1275        """
1276        data = fitproblem.get_fit_data()
1277        model = fitproblem.get_model()
1278        smearer = fitproblem.get_smearer()
1279        qmin, qmax = fitproblem.get_range()
1280
1281        #Extra list of parameters and their constraints
1282        listOfConstraint = []
1283        param = fitproblem.get_model_param()
1284        if len(param) > 0:
1285            for item in param:
1286                ## check if constraint
1287                if item[0] is not None and item[1] is not None:
1288                    listOfConstraint.append((item[0], item[1]))
1289        new_model = model
1290        fitter.set_model(new_model, fit_id, pars, data=data,
1291                         constraints=listOfConstraint)
1292        fitter.set_data(data=data, id=fit_id, smearer=smearer, qmin=qmin,
1293                        qmax=qmax)
1294        fitter.select_problem_for_fit(id=fit_id, value=1)
1295
1296    def _onSelect(self, event):
1297        """
1298        when Select data to fit a new page is created .Its reference is
1299        added to self.page_finder
1300        """
1301        panel = self.plot_panel
1302        if panel is None:
1303            raise ValueError("Fitting:_onSelect: NonType panel")
1304        Plugin.on_perspective(self, event=event)
1305        self.select_data(panel)
1306
1307    def select_data(self, panel):
1308        """
1309        """
1310        for plottable in panel.graph.plottables:
1311            if plottable.__class__.__name__ in ["Data1D", "Theory1D"]:
1312                data_id = panel.graph.selected_plottable
1313                if plottable == panel.plots[data_id]:
1314                    data = plottable
1315                    self.add_fit_page(data=[data])
1316                    return
1317            else:
1318                data = plottable
1319                self.add_fit_page(data=[data])
1320
1321    def update_fit(self, result=None, msg=""):
1322        """
1323        """
1324        print("update_fit result", result)
1325
1326    def _batch_fit_complete(self, result, pars, page_id,
1327                            batch_outputs, batch_inputs, elapsed=None):
1328        """
1329        Display fit result in batch
1330        :param result: list of objects received from fitters
1331        :param pars: list of  fitted parameters names
1332        :param page_id: list of page ids which called fit function
1333        :param elapsed: time spent at the fitting level
1334        """
1335        uid = page_id[0]
1336        if uid in self.fit_thread_list:
1337            del self.fit_thread_list[uid]
1338
1339        wx.CallAfter(self._update_fit_button, page_id)
1340        t1 = time.time()
1341        str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1))
1342        msg = "Fit completed on %s \n" % str_time
1343        msg += "Duration time: %s s.\n" % str(elapsed)
1344        evt = StatusEvent(status=msg, info="info", type="stop")
1345        wx.PostEvent(self.parent, evt)
1346
1347        if batch_outputs is None:
1348            batch_outputs = {}
1349
1350        # format batch_outputs
1351        batch_outputs["Chi2"] = []
1352        #Don't like these loops
1353        # Need to create dictionary of all fitted parameters
1354        # since the number of parameters can differ between each fit result
1355        for list_res in result:
1356            for res in list_res:
1357                model, data = res.inputs[0]
1358                if model is not None and hasattr(model, "model"):
1359                    model = model.model
1360                #get all fittable parameters of the current model
1361                for param in  model.getParamList():
1362                    if param  not in batch_outputs:
1363                        batch_outputs[param] = []
1364                for param in model.getDispParamList():
1365                    if not model.is_fittable(param) and \
1366                        param in batch_outputs:
1367                        del batch_outputs[param]
1368                # Add fitted parameters and their error
1369                for param in res.param_list:
1370                    if param not in batch_outputs:
1371                        batch_outputs[param] = []
1372                    err_param = "error on %s" % str(param)
1373                    if err_param not in batch_inputs:
1374                        batch_inputs[err_param] = []
1375        msg = ""
1376        for list_res in result:
1377            for res in list_res:
1378                pid = res.fitter_id
1379                model, data = res.inputs[0]
1380                correct_result = False
1381                if model is not None and hasattr(model, "model"):
1382                    model = model.model
1383                if data is not None and hasattr(data, "sas_data"):
1384                    data = data.sas_data
1385
1386                is_data2d = issubclass(data.__class__, Data2D)
1387                # Check consistency of arrays
1388                if not is_data2d:
1389                    if len(res.theory) == len(res.index[res.index]) and \
1390                        len(res.index) == len(data.y):
1391                        correct_result = True
1392                else:
1393                    copy_data = deepcopy(data)
1394                    new_theory = copy_data.data
1395                    new_theory[res.index] = res.theory
1396                    new_theory[res.index == False] = np.nan
1397                    correct_result = True
1398                # Get all fittable parameters of the current model
1399                param_list = model.getParamList()
1400                for param in model.getDispParamList():
1401                    if '.' in param and param in param_list:
1402                        # Ensure polydispersity results are displayed
1403                        p1, p2 = param.split('.')
1404                        if not model.is_fittable(p1) and not (p2 == 'width' and param in res.param_list)\
1405                            and param in param_list:
1406                            param_list.remove(param)
1407                    elif not model.is_fittable(param) and \
1408                        param in param_list:
1409                        param_list.remove(param)
1410                if not correct_result or res.fitness is None or \
1411                    not np.isfinite(res.fitness) or \
1412                        np.any(res.pvec is None) or not \
1413                        np.all(np.isfinite(res.pvec)):
1414                    data_name = str(None)
1415                    if data is not None:
1416                        data_name = str(data.name)
1417                    model_name = str(None)
1418                    if model is not None:
1419                        model_name = str(model.name)
1420                    msg += "Data %s and Model %s did not fit.\n" % (data_name,
1421                                                                    model_name)
1422                    ERROR = np.NAN
1423                    cell = BatchCell()
1424                    cell.label = res.fitness
1425                    cell.value = res.fitness
1426                    batch_outputs["Chi2"].append(ERROR)
1427                    for param in param_list:
1428                        # Save value of  fixed parameters
1429                        if param not in res.param_list:
1430                            batch_outputs[str(param)].append(ERROR)
1431                        else:
1432                            # Save only fitted values
1433                            batch_outputs[param].append(ERROR)
1434                            batch_inputs["error on %s" % str(param)].append(ERROR)
1435                else:
1436                    # TODO: Why sometimes res.pvec comes with np.float64?
1437                    # probably from scipy lmfit
1438                    if res.pvec.__class__ == np.float64:
1439                        res.pvec = [res.pvec]
1440
1441                    cell = BatchCell()
1442                    cell.label = res.fitness
1443                    cell.value = res.fitness
1444                    batch_outputs["Chi2"].append(cell)
1445                    # add parameters to batch_results
1446                    for param in param_list:
1447                        # save value of  fixed parameters
1448                        if param not in res.param_list:
1449                            batch_outputs[str(param)].append(model.getParam(param))
1450                        else:
1451                            index = res.param_list.index(param)
1452                            #save only fitted values
1453                            batch_outputs[param].append(res.pvec[index])
1454                            if res.stderr is not None and \
1455                                len(res.stderr) == len(res.param_list):
1456                                item = res.stderr[index]
1457                                batch_inputs["error on %s" % param].append(item)
1458                            else:
1459                                batch_inputs["error on %s" % param].append('-')
1460                            model.setParam(param, res.pvec[index])
1461                #fill the batch result with emtpy value if not in the current
1462                #model
1463                EMPTY = "-"
1464                for key in batch_outputs.keys():
1465                    if key not in param_list and key not in ["Chi2", "Data"]:
1466                        batch_outputs[key].append(EMPTY)
1467
1468                self.page_finder[pid].set_batch_result(batch_inputs=batch_inputs,
1469                                                       batch_outputs=batch_outputs)
1470
1471                cpage = self.fit_panel.get_page_by_id(pid)
1472                cpage._on_fit_complete()
1473                self.page_finder[pid][data.id].set_result(res)
1474                fitproblem = self.page_finder[pid][data.id]
1475                qmin, qmax = fitproblem.get_range()
1476                plot_result = False
1477                if correct_result:
1478                    if not is_data2d:
1479                        self._complete1D(x=data.x[res.index], y=res.theory, page_id=pid,
1480                                         elapsed=None,
1481                                         index=res.index, model=model,
1482                                         weight=None, fid=data.id,
1483                                         toggle_mode_on=False, state=None,
1484                                         data=data, update_chisqr=False,
1485                                         source='fit', plot_result=plot_result)
1486                    else:
1487                        self._complete2D(image=new_theory, data=data,
1488                                         model=model,
1489                                         page_id=pid, elapsed=None,
1490                                         index=res.index,
1491                                         qmin=qmin,
1492                                         qmax=qmax, fid=data.id, weight=None,
1493                                         toggle_mode_on=False, state=None,
1494                                         update_chisqr=False,
1495                                         source='fit', plot_result=plot_result)
1496                self.on_set_batch_result(page_id=pid,
1497                                         fid=data.id,
1498                                         batch_outputs=batch_outputs,
1499                                         batch_inputs=batch_inputs)
1500
1501        evt = StatusEvent(status=msg, error="info", type="stop")
1502        wx.PostEvent(self.parent, evt)
1503        # Remove parameters that are not shown
1504        cpage = self.fit_panel.get_page_by_id(uid)
1505        tbatch_outputs = {}
1506        shownkeystr = cpage.get_copy_params()
1507        for key in batch_outputs.keys():
1508            if key in ["Chi2", "Data"] or shownkeystr.count(key) > 0:
1509                tbatch_outputs[key] = batch_outputs[key]
1510
1511        wx.CallAfter(self.parent.on_set_batch_result, tbatch_outputs,
1512                     batch_inputs, self.sub_menu)
1513
1514    def on_set_batch_result(self, page_id, fid, batch_outputs, batch_inputs):
1515        """
1516        """
1517        pid = page_id
1518        if fid not in self.page_finder[pid]:
1519            return
1520        fitproblem = self.page_finder[pid][fid]
1521        index = self.page_finder[pid].nbr_residuals_computed - 1
1522        residuals = fitproblem.get_residuals()
1523        theory_data = fitproblem.get_theory_data()
1524        data = fitproblem.get_fit_data()
1525        model = fitproblem.get_model()
1526        #fill batch result information
1527        if "Data" not in batch_outputs:
1528            batch_outputs["Data"] = []
1529        cell = BatchCell()
1530        cell.label = data.name
1531        cell.value = index
1532
1533        if theory_data is not None:
1534            #Suucessful fit
1535            theory_data.id = wx.NewId()
1536            theory_data.name = model.name + "[%s]" % str(data.name)
1537            if issubclass(theory_data.__class__, Data2D):
1538                group_id = wx.NewId()
1539                theory_data.group_id = group_id
1540                if group_id not in theory_data.list_group_id:
1541                    theory_data.list_group_id.append(group_id)
1542
1543            try:
1544                # associate residuals plot
1545                if issubclass(residuals.__class__, Data2D):
1546                    group_id = wx.NewId()
1547                    residuals.group_id = group_id
1548                    if group_id not in residuals.list_group_id:
1549                        residuals.list_group_id.append(group_id)
1550                batch_outputs["Chi2"][index].object = [residuals]
1551            except:
1552                pass
1553
1554        cell.object = [data, theory_data]
1555        batch_outputs["Data"].append(cell)
1556        for key, value in data.meta_data.items():
1557            if key not in batch_inputs:
1558                batch_inputs[key] = []
1559            #if key.lower().strip() != "loader":
1560            batch_inputs[key].append(value)
1561        param = "temperature"
1562        if hasattr(data.sample, param):
1563            if param not in  batch_inputs:
1564                batch_inputs[param] = []
1565            batch_inputs[param].append(data.sample.temperature)
1566
1567    def _fit_completed(self, result, page_id, batch_outputs,
1568                       batch_inputs=None, pars=None, elapsed=None):
1569        """
1570        Display result of the fit on related panel(s).
1571        :param result: list of object generated when fit ends
1572        :param pars: list of names of parameters fitted
1573        :param page_id: list of page ids which called fit function
1574        :param elapsed: time spent at the fitting level
1575        """
1576        t1 = time.time()
1577        str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1))
1578        msg = "Fit completed on %s \n" % str_time
1579        msg += "Duration time: %s s.\n" % str(elapsed)
1580        evt = StatusEvent(status=msg, info="info", type="stop")
1581        wx.PostEvent(self.parent, evt)
1582        wx.PostEvent(self.result_panel, PlotResultEvent(result=result))
1583        wx.CallAfter(self._update_fit_button, page_id)
1584        result = result[0]
1585        self.fit_thread_list = {}
1586        if page_id is None:
1587            page_id = []
1588        ## fit more than 1 model at the same time
1589        try:
1590            index = 0
1591            # Update potential simfit page(s)
1592            if self.sim_page is not None:
1593                self.sim_page._on_fit_complete()
1594            if self.batch_page:
1595                self.batch_page._on_fit_complete()
1596            # Update all fit pages
1597            for uid in page_id:
1598                res = result[index]
1599                fit_msg = res.mesg
1600                if res.fitness is None or \
1601                    not np.isfinite(res.fitness) or \
1602                        np.any(res.pvec is None) or \
1603                    not np.all(np.isfinite(res.pvec)):
1604                    fit_msg += "\nFitting did not converge!!!"
1605                    wx.CallAfter(self._update_fit_button, page_id)
1606                else:
1607                    #set the panel when fit result are float not list
1608                    if res.pvec.__class__ == np.float64:
1609                        pvec = [res.pvec]
1610                    else:
1611                        pvec = res.pvec
1612                    if res.stderr.__class__ == np.float64:
1613                        stderr = [res.stderr]
1614                    else:
1615                        stderr = res.stderr
1616                    cpage = self.fit_panel.get_page_by_id(uid)
1617                    # Make sure we got all results
1618                    #(CallAfter is important to MAC)
1619                    try:
1620                        #if res is not None:
1621                        wx.CallAfter(cpage.onsetValues, res.fitness,
1622                                     res.param_list,
1623                                     pvec, stderr)
1624                        index += 1
1625                        wx.CallAfter(cpage._on_fit_complete)
1626                    except KeyboardInterrupt:
1627                        fit_msg += "\nSingular point: Fitting stopped."
1628                    except Exception:
1629                        fit_msg += "\nSingular point: Fitting error occurred."
1630                if fit_msg:
1631                    evt = StatusEvent(status=fit_msg, info="warning", type="stop")
1632                    wx.PostEvent(self.parent, evt)
1633
1634        except Exception as exc:
1635            msg = "Fit completed but the following error occurred: %s" % exc
1636            #msg = "\n".join((traceback.format_exc(), msg))
1637            evt = StatusEvent(status=msg, info="warning", type="stop")
1638            wx.PostEvent(self.parent, evt)
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()
1662        if self.menu1 is None:
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)
1675        wx.PostEvent(self.parent, StatusEvent(status=msg))
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
1721        if model is None:
1722            return
1723        if uid not in self.page_finder:
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
1742    def create_theory_1D(self, x, y, page_id, model, data, state,
1743                         data_description, data_id, dy=None):
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)
1757        if dy is None:
1758            new_plot.is_data = False
1759            new_plot.dy = np.zeros(len(y))
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
1765        new_plot.interactive = True
1766        new_plot.dx = None
1767        new_plot.dxl = None
1768        new_plot.dxw = None
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
1773        if new_plot.group_id is None:
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,
1791                                  state=state)
1792        return new_plot
1793
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,
1798                    source='model', plot_result=True,
1799                    unsmeared_model=None, unsmeared_data=None,
1800                    unsmeared_error=None, sq_model=None, pq_model=None):
1801        """
1802            Complete plotting 1D data
1803            @param unsmeared_model: fit model, without smearing
1804            @param unsmeared_data: data, rescaled to unsmeared model
1805            @param unsmeared_error: data error, rescaled to unsmeared model
1806        """
1807        number_finite = np.count_nonzero(np.isfinite(y))
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)
1812        plots_to_update = [] # List of plottables that have changed since last calculation
1813        # Create the new theories
1814        if unsmeared_model is not None:
1815            unsmeared_model_plot = self.create_theory_1D(x, unsmeared_model,
1816                                  page_id, model, data, state,
1817                                  data_description=model.name + " unsmeared",
1818                                  data_id=str(page_id) + " " + data.name + " unsmeared")
1819            plots_to_update.append(unsmeared_model_plot)
1820
1821            if unsmeared_data is not None and unsmeared_error is not None:
1822                unsmeared_data_plot = self.create_theory_1D(x, unsmeared_data,
1823                                      page_id, model, data, state,
1824                                      data_description="Data unsmeared",
1825                                      data_id="Data  " + data.name + " unsmeared",
1826                                      dy=unsmeared_error)
1827                plots_to_update.append(unsmeared_data_plot)
1828        if sq_model is not None and pq_model is not None:
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,
1831                                  data_description=model.name + " S(q)",
1832                                  data_id=sq_id)
1833            plots_to_update.append(sq_plot)
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,
1836                                  data_description=model.name + " P(q)",
1837                                  data_id=pq_id)
1838            plots_to_update.append(pq_plot)
1839        # Update the P(Q), S(Q) and unsmeared theory plots if they exist
1840        wx.PostEvent(self.parent, NewPlotEvent(plots=plots_to_update,
1841                                              action='update'))
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,
1856                                                  fid=data.id)
1857        if toggle_mode_on:
1858            wx.PostEvent(self.parent,
1859                         NewPlotEvent(group_id=str(page_id) + " Model2D",
1860                                      action="Hide"))
1861        else:
1862            if update_chisqr:
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))
1869            else:
1870                self._plot_residuals(page_id=page_id, data=data, fid=fid,
1871                                     index=index, weight=weight)
1872
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."
1876            wx.PostEvent(self.parent, StatusEvent(status=msg, info="error"))
1877        else:
1878            msg = "Computation  completed!"
1879            if number_finite != y.size:
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.")
1882            wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop"))
1883
1884    def _calc_exception(self, etype, value, tb):
1885        """
1886        Handle exception from calculator by posting it as an error.
1887        """
1888        logger.error("".join(traceback.format_exception(etype, value, tb)))
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
1893    def _update2D(self, output, time=None):
1894        """
1895        Update the output of plotting model
1896        """
1897        msg = "Plot updating ... "
1898        wx.PostEvent(self.parent, StatusEvent(msg, type="update"))
1899
1900    def _complete2D(self, image, data, model, page_id, elapsed, index, qmin,
1901                    qmax, fid=None, weight=None, toggle_mode_on=False, state=None,
1902                    update_chisqr=True, source='model', plot_result=True):
1903        """
1904        Complete get the result of modelthread and create model 2D
1905        that can be plot.
1906        """
1907        number_finite = np.count_nonzero(np.isfinite(image))
1908        np.nan_to_num(image)
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,
1943                                  theory=new_plot,
1944                                  state=state)
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:
1948            wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title))
1949        if toggle_mode_on:
1950            wx.PostEvent(self.parent,
1951                         NewPlotEvent(group_id=str(page_id) + " Model1D",
1952                                      action="Hide"))
1953        else:
1954            # Chisqr in fitpage
1955            if update_chisqr:
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))
1962            else:
1963                self._plot_residuals(page_id=page_id, data=data, fid=fid,
1964                                     index=index, weight=weight)
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."
1969            wx.PostEvent(self.parent, StatusEvent(status=msg, info="error"))
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"))
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,
1985                      update_chisqr=True, source='model'):
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                ##
2008                ##    -PDB August 12, 2014
2009                while self.calc_2D.isrunning():
2010                    time.sleep(0.1)
2011            self.calc_2D = Calc2D(model=model,
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)
2025            self.calc_2D.queue()
2026        except:
2027            raise
2028
2029    def _draw_model1D(self, model, page_id, data,
2030                      qmin, qmax, smearer=None,
2031                      state=None, weight=None, fid=None,
2032                      toggle_mode_on=False, update_chisqr=True, source='model',
2033                      enable1D=True):
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()
2047                ## stop just raises the flag -- the thread is supposed to
2048                ## then kill itself but cannot.  Paul Kienzle came up with
2049                ## this fix to prevent threads from stepping on each other
2050                ## which was causing a simple custom plugin model to crash
2051                ##Sasview.
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
2059                ## May need rethinking
2060                ##
2061                ##    -PDB August 12, 2014
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,
2077                                  exception_handler=self._calc_exception,
2078                                  source=source)
2079            self.calc_1D.queue()
2080        except Exception as exc:
2081            msg = " Error occurred when drawing %s Model 1D: " % model.name
2082            msg += " %s" % exc
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
2097        # return None if data is None
2098        if not check_data_validity(data_copy) or data_copy is None:
2099            return chisqr
2100
2101        # Get data: data I, theory I, and data dI in order
2102        if data_copy.__class__.__name__ == "Data2D":
2103            if index is None:
2104                index = np.ones(len(data_copy.data), dtype=bool)
2105            if weight is not None:
2106                data_copy.err_data = weight
2107            # get rid of zero error points
2108            index = index & (data_copy.err_data != 0)
2109            index = index & (np.isfinite(data_copy.data))
2110            fn = data_copy.data[index]
2111            theory_data = self.page_finder[page_id].get_theory_data(fid=data_copy.id)
2112            if theory_data is None:
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
2118            if index is None:
2119                index = np.ones(len(data_copy.y), dtype=bool)
2120            if weight is not None:
2121                data_copy.dy = weight
2122            if data_copy.dy is None or data_copy.dy == []:
2123                dy = np.ones(len(data_copy.y))
2124            else:
2125                ## Set consistently w/AbstractFitengine:
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)
2132            if theory_data is None:
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:
2141            print("Unmatch lengths %s, %s, %s" % (len(fn), len(gn), len(en)))
2142            return
2143
2144        residuals = res[np.isfinite(res)]
2145        # get chisqr only w/finite
2146        chisqr = np.average(residuals * residuals)
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
2175            if weight is None:
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
2183            residuals.err_data = np.ones(len(residuals.data))
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
2196            if data_copy.dy is None or data_copy.dy == []:
2197                dy = np.ones(len(data_copy.y))
2198            else:
2199                if weight is None:
2200                    dy = np.ones(len(data_copy.y))
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]
2219            residuals.dy = np.ones(len(residuals.y))
2220            residuals.dx = None
2221            residuals.dxl = None
2222            residuals.dxw = None
2223            residuals.ytransform = 'y'
2224            # For latter scale changes
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()
2237        if group_id is None:
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.