Ejemplo n.º 1
0
    def setUp(self, mock_dual_view, mock_quick_edit, mock_base_view,
              mock_plot_canvas, mock_main_view, mock_muon_base_view,
              mock_muon_canvas):
        self.mock_dual_view = mock_dual_view
        self.mock_quick_edit = mock_quick_edit
        self.mock_main_view = mock_main_view
        self.mock_base_view = mock_base_view
        self.mock_plot_canvas = mock_plot_canvas
        self.mock_muon_base_view = mock_muon_base_view
        self.mock_muon_canvas = mock_muon_canvas

        self.canvas = {}
        self._num_count = 0
        self._letter_count = 0

        self.mock_dual_view.return_value = mock.MagicMock(
            autospec=DualPlotMaxentPaneView)
        self.mock_quick_edit.return_value = mock.MagicMock(
            autospec=DualQuickEditWidget)
        self.mock_main_view.return_value = mock.MagicMock(
            autospec=MainPlotWidgetView)
        self.mock_base_view.return_value = mock.MagicMock(
            autospec=BasePaneView)
        self.mock_plot_canvas.side_effect = self.canvas_mocks
        self.mock_muon_base_view.return_value = mock.MagicMock(
            autospec=BasePaneView)
        self.mock_muon_canvas.side_effect = self.canvas_mocks

        self.context = setup_context(freq=True)
        self.widget = FrequencyAnalysisPlotWidget(self.context)
Ejemplo n.º 2
0
    def __init__(self, parent=None, window_flags=None):
        super(FrequencyAnalysisGui, self).__init__(parent)
        if window_flags:
            self.setWindowFlags(window_flags)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

        try:
            check_facility()
        except AttributeError as error:
            self.warning_popup(error.args[0])
        # load the feature flags
        feature_dict = load_features()

        # initialise the data storing classes of the interface
        self.loaded_data = MuonLoadData()
        self.data_context = MuonDataContext('Frequency Domain Data',
                                            self.loaded_data)
        self.gui_context = MuonGuiContext()
        self.plot_panes_context = PlotPanesContext()
        self.group_pair_context = MuonGroupPairContext(
            self.data_context.check_group_contains_valid_detectors)
        self.corrections_context = CorrectionsContext(self.loaded_data)
        self.phase_context = PhaseTableContext()
        self.fitting_context = BasicFittingContext(
            allow_double_pulse_fitting=True)
        self.results_context = ResultsContext()
        self.model_fitting_context = ModelFittingContext(
            allow_double_pulse_fitting=False)

        self.frequency_context = FrequencyContext()

        self.context = FrequencyDomainAnalysisContext(
            muon_data_context=self.data_context,
            muon_gui_context=self.gui_context,
            muon_group_context=self.group_pair_context,
            corrections_context=self.corrections_context,
            muon_phase_context=self.phase_context,
            plot_panes_context=self.plot_panes_context,
            fitting_context=self.fitting_context,
            results_context=self.results_context,
            model_fitting_context=self.model_fitting_context,
            frequency_context=self.frequency_context)

        # create the dockable widget
        self.plot_widget = FrequencyAnalysisPlotWidget(self.context,
                                                       parent=self)

        self.dockable_plot_widget_window = PlottingDockWidget(
            parent=self, plotting_widget=self.plot_widget.view)
        self.dockable_plot_widget_window.setMinimumWidth(575)

        # Add dock widget to main Muon analysis window
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea,
                           self.dockable_plot_widget_window)
        # Need this line to stop the bug where the dock window snaps back to its original size after resizing.
        # 0 argument is arbitrary and has no effect on fit widget size
        # This is a qt bug reported at (https://bugreports.qt.io/browse/QTBUG-65592)
        if QT_VERSION >= LooseVersion("5.6"):
            self.resizeDocks({self.dockable_plot_widget_window}, {1},
                             QtCore.Qt.Horizontal)

        # construct all the widgets.
        self.load_widget = LoadWidget(self.loaded_data, self.context, self)
        self.grouping_tab_widget = GroupingTabWidget(self.context, parent)
        self.corrections_tab = CorrectionsTabWidget(self.context, self)
        self.home_tab = HomeTabWidget(self.context, self)
        self.phase_tab = PhaseTabWidget(self.context, self)
        self.transform = TransformWidget(self.context,
                                         FFTWidget,
                                         MaxEntWidget,
                                         parent=self)
        self.fitting_tab = FittingTabWidget(self.context, self)
        self.seq_fitting_tab = SeqFittingTabWidget(
            self.context, self.fitting_tab.fitting_tab_model, self)
        self.results_tab = ResultsTabWidget(self.context.fitting_context,
                                            self.context, self)

        self.add_model_analysis = AddModelAnalysis(self, feature_dict)
        self.add_raw_plots = AddRawPlots(self, feature_dict)
        self.add_fitting = AddFitting(self, feature_dict)

        setup_group_ws = AddGroupingWorkspaces(self, feature_dict)

        self.setup_tabs()
        self.plot_widget.insert_plot_panes()

        self.help_widget = HelpWidget(self.context.window_title)

        central_widget = QtWidgets.QWidget()
        vertical_layout = QtWidgets.QVBoxLayout()

        vertical_layout.addWidget(self.load_widget.load_widget_view)
        vertical_layout.addWidget(self.tabs)
        vertical_layout.addWidget(self.help_widget.view)
        central_widget.setLayout(vertical_layout)
        central_widget.setSizePolicy(
            QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum,
                                  QtWidgets.QSizePolicy.Maximum))
        self.disable_notifier = GenericObservable()
        self.disable_observer = GenericObserver(
            self.disable_notifier.notify_subscribers)
        self.enable_notifier = GenericObservable()
        self.enable_observer = GenericObserver(
            self.enable_notifier.notify_subscribers)
        self.setup_disable_notifier()
        self.setup_enable_notifier()

        self.setCentralWidget(central_widget)
        self.setWindowTitle(self.context.window_title)

        self.setup_load_observers()

        self.setup_gui_variable_observers()

        self.setup_grouping_changed_observers()

        self.setup_corrections_changed_observers()

        self.setup_instrument_changed_notifier()

        self.setup_group_calculation_enable_notifier()

        self.setup_group_calculation_disabler_notifier()

        self.setup_on_load_enabler()

        self.setup_on_load_disabler()

        self.setup_phase_quad_changed_notifier()

        self.setup_phase_table_changed_notifier()
        self.setup_fitting_notifier()

        self.setup_counts_calculation_finished_notifier()

        self.setup_asymmetry_pair_and_diff_calculations_finished_notifier()

        self.setup_transform()

        self.context.data_context.message_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.message_observer)

        self.add_model_analysis.add_observers_to_feature(self)
        self.add_model_analysis.set_feature_observables(self)

        self.add_raw_plots.add_observers_to_feature(self)
        setup_group_ws.add_observers_to_feature(self)
Ejemplo n.º 3
0
class FrequencyAnalysisGui(QtWidgets.QMainWindow):
    """
    The Frequency Domain Analaysis 2.0 interface.
    """
    @staticmethod
    def warning_popup(message):
        message_box.warning(str(message))

    def __init__(self, parent=None, window_flags=None):
        super(FrequencyAnalysisGui, self).__init__(parent)
        if window_flags:
            self.setWindowFlags(window_flags)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

        try:
            check_facility()
        except AttributeError as error:
            self.warning_popup(error.args[0])
        # load the feature flags
        feature_dict = load_features()

        # initialise the data storing classes of the interface
        self.loaded_data = MuonLoadData()
        self.data_context = MuonDataContext('Frequency Domain Data',
                                            self.loaded_data)
        self.gui_context = MuonGuiContext()
        self.plot_panes_context = PlotPanesContext()
        self.group_pair_context = MuonGroupPairContext(
            self.data_context.check_group_contains_valid_detectors)
        self.corrections_context = CorrectionsContext(self.loaded_data)
        self.phase_context = PhaseTableContext()
        self.fitting_context = BasicFittingContext(
            allow_double_pulse_fitting=True)
        self.results_context = ResultsContext()
        self.model_fitting_context = ModelFittingContext(
            allow_double_pulse_fitting=False)

        self.frequency_context = FrequencyContext()

        self.context = FrequencyDomainAnalysisContext(
            muon_data_context=self.data_context,
            muon_gui_context=self.gui_context,
            muon_group_context=self.group_pair_context,
            corrections_context=self.corrections_context,
            muon_phase_context=self.phase_context,
            plot_panes_context=self.plot_panes_context,
            fitting_context=self.fitting_context,
            results_context=self.results_context,
            model_fitting_context=self.model_fitting_context,
            frequency_context=self.frequency_context)

        # create the dockable widget
        self.plot_widget = FrequencyAnalysisPlotWidget(self.context,
                                                       parent=self)

        self.dockable_plot_widget_window = PlottingDockWidget(
            parent=self, plotting_widget=self.plot_widget.view)
        self.dockable_plot_widget_window.setMinimumWidth(575)

        # Add dock widget to main Muon analysis window
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea,
                           self.dockable_plot_widget_window)
        # Need this line to stop the bug where the dock window snaps back to its original size after resizing.
        # 0 argument is arbitrary and has no effect on fit widget size
        # This is a qt bug reported at (https://bugreports.qt.io/browse/QTBUG-65592)
        if QT_VERSION >= LooseVersion("5.6"):
            self.resizeDocks({self.dockable_plot_widget_window}, {1},
                             QtCore.Qt.Horizontal)

        # construct all the widgets.
        self.load_widget = LoadWidget(self.loaded_data, self.context, self)
        self.grouping_tab_widget = GroupingTabWidget(self.context, parent)
        self.corrections_tab = CorrectionsTabWidget(self.context, self)
        self.home_tab = HomeTabWidget(self.context, self)
        self.phase_tab = PhaseTabWidget(self.context, self)
        self.transform = TransformWidget(self.context,
                                         FFTWidget,
                                         MaxEntWidget,
                                         parent=self)
        self.fitting_tab = FittingTabWidget(self.context, self)
        self.seq_fitting_tab = SeqFittingTabWidget(
            self.context, self.fitting_tab.fitting_tab_model, self)
        self.results_tab = ResultsTabWidget(self.context.fitting_context,
                                            self.context, self)

        self.add_model_analysis = AddModelAnalysis(self, feature_dict)
        self.add_raw_plots = AddRawPlots(self, feature_dict)
        self.add_fitting = AddFitting(self, feature_dict)

        setup_group_ws = AddGroupingWorkspaces(self, feature_dict)

        self.setup_tabs()
        self.plot_widget.insert_plot_panes()

        self.help_widget = HelpWidget(self.context.window_title)

        central_widget = QtWidgets.QWidget()
        vertical_layout = QtWidgets.QVBoxLayout()

        vertical_layout.addWidget(self.load_widget.load_widget_view)
        vertical_layout.addWidget(self.tabs)
        vertical_layout.addWidget(self.help_widget.view)
        central_widget.setLayout(vertical_layout)
        central_widget.setSizePolicy(
            QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum,
                                  QtWidgets.QSizePolicy.Maximum))
        self.disable_notifier = GenericObservable()
        self.disable_observer = GenericObserver(
            self.disable_notifier.notify_subscribers)
        self.enable_notifier = GenericObservable()
        self.enable_observer = GenericObserver(
            self.enable_notifier.notify_subscribers)
        self.setup_disable_notifier()
        self.setup_enable_notifier()

        self.setCentralWidget(central_widget)
        self.setWindowTitle(self.context.window_title)

        self.setup_load_observers()

        self.setup_gui_variable_observers()

        self.setup_grouping_changed_observers()

        self.setup_corrections_changed_observers()

        self.setup_instrument_changed_notifier()

        self.setup_group_calculation_enable_notifier()

        self.setup_group_calculation_disabler_notifier()

        self.setup_on_load_enabler()

        self.setup_on_load_disabler()

        self.setup_phase_quad_changed_notifier()

        self.setup_phase_table_changed_notifier()
        self.setup_fitting_notifier()

        self.setup_counts_calculation_finished_notifier()

        self.setup_asymmetry_pair_and_diff_calculations_finished_notifier()

        self.setup_transform()

        self.context.data_context.message_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.message_observer)

        self.add_model_analysis.add_observers_to_feature(self)
        self.add_model_analysis.set_feature_observables(self)

        self.add_raw_plots.add_observers_to_feature(self)
        setup_group_ws.add_observers_to_feature(self)

    def setup_transform(self):
        self.transform.set_up_calculation_observers(
            self.fitting_tab.fitting_tab_view.enable_tab_observer,
            self.fitting_tab.fitting_tab_view.disable_tab_observer)
        self.transform.new_data_observer(self.transform_finished_observer)
        self.transform._maxent._presenter.calculation_finished_notifier.add_subscriber(
            self.plot_widget.maxent_mode.new_data_observer)
        self.transform._maxent._presenter.new_reconstructed_data.add_subscriber(
            self.plot_widget.maxent_mode.reconstructed_data_observer)
        self.transform._maxent._presenter.method_changed.add_subscriber(
            self.plot_widget.maxent_mode.method_changed)
        self.transform._maxent._presenter.period_changed.add_subscriber(
            self.plot_widget.maxent_mode.period_changed)
        self.context.data_context.instrumentNotifier.add_subscriber(
            self.plot_widget.maxent_mode.instrument_observer)
        self.update_fits_observer = GenericObserver(self.handle_units_changed)
        self.plot_widget.update_freq_units_add_subscriber(
            self.update_fits_observer)

    def setup_tabs(self):
        """
        Set up the tabbing structure; the tabs work similarly to conventional
        web browsers.
        """
        self.tabs = DetachableTabWidget(self)
        self.tabs.addTabWithOrder(self.home_tab.home_tab_view, 'Home')
        self.tabs.addTabWithOrder(self.grouping_tab_widget.group_tab_view,
                                  'Grouping')
        self.tabs.addTabWithOrder(self.corrections_tab.corrections_tab_view,
                                  "Corrections")
        self.tabs.addTabWithOrder(self.phase_tab.phase_table_view,
                                  'Phase Table')
        self.tabs.addTabWithOrder(self.transform.widget, 'Transform')
        self.tabs.addTabWithOrder(self.fitting_tab.fitting_tab_view, 'Fitting')
        self.tabs.addTabWithOrder(self.seq_fitting_tab.seq_fitting_tab_view,
                                  'Sequential Fitting')
        self.tabs.addTabWithOrder(self.results_tab.results_tab_view, 'Results')
        self.add_model_analysis.add_to_tab(self)

        self.transform_finished_observer = GenericObserverWithArgPassing(
            self.handle_transform_performed)
        self.tabs.set_slot_for_tab_changed(self.handle_tab_changed)
        self.tabs.setElideMode(QtCore.Qt.ElideNone)
        self.tabs.setUsesScrollButtons(False)

    def handle_tab_changed(self):
        index = self.tabs.currentIndex()
        if TAB_ORDER[index] in [
                "Home", "Grouping", "Corrections", "Phase Table"
        ]:  # Plot all the selected data
            plot_mode = self.plot_widget.data_index
        elif TAB_ORDER[index] in [
                "Fitting", "Sequential Fitting", "Transform"
        ]:  # Plot the displayed workspace
            plot_mode = self.plot_widget.fit_index
        elif TAB_ORDER[index] in ["Model Fitting"]:
            plot_mode = self.plot_widget.model_fit_index
        else:
            return

        self.plot_widget.set_plot_view(plot_mode)

    def handle_transform_performed(self, new_data_workspace_name):
        self.update_fit_ws_list()
        self.fitting_tab.fitting_tab_presenter.set_selected_dataset(
            new_data_workspace_name)

    def handle_units_changed(self):
        old_name = self.fitting_tab.fitting_tab_presenter.current_dataset()
        if old_name == "":
            return
        self.update_fit_ws_list()
        new_name = self.frequency_context.switch_units_in_name(old_name)
        self.fitting_tab.fitting_tab_presenter.set_selected_dataset(new_name)
        x_lim = self.frequency_context.range()
        self.fitting_tab.fitting_tab_presenter.update_start_and_end_x_in_view_and_model(
            x_lim[0], x_lim[1])

    def update_fit_ws_list(self):
        self.fitting_tab.fitting_tab_presenter.handle_new_data_loaded()
        self.seq_fitting_tab.seq_fitting_tab_presenter.handle_selected_workspaces_changed(
        )

    def set_tab_warning(self, tab_name: str, message: str):
        """Sets a warning message as the tooltip of the provided tab."""
        self.tabs.set_tab_warning(TAB_ORDER.index(tab_name), message)

    def setup_disable_notifier(self):

        self.disable_notifier.add_subscriber(
            self.home_tab.home_tab_widget.disable_observer)

        self.disable_notifier.add_subscriber(
            self.load_widget.load_widget.disable_observer)

        self.disable_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.disable_tab_observer)

        self.disable_notifier.add_subscriber(
            self.corrections_tab.corrections_tab_view.disable_tab_observer)

        self.disable_notifier.add_subscriber(
            self.fitting_tab.fitting_tab_view.disable_tab_observer)

        self.disable_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.disable_tab_observer
        )

        self.disable_notifier.add_subscriber(
            self.phase_tab.phase_table_presenter.disable_tab_observer)

        self.disable_notifier.add_subscriber(
            self.results_tab.results_tab_presenter.disable_tab_observer)

        self.disable_notifier.add_subscriber(self.transform.disable_observer)

    def setup_enable_notifier(self):

        self.enable_notifier.add_subscriber(
            self.home_tab.home_tab_widget.enable_observer)

        self.enable_notifier.add_subscriber(
            self.load_widget.load_widget.enable_observer)

        self.enable_notifier.add_subscriber(
            self.corrections_tab.corrections_tab_view.enable_tab_observer)

        self.enable_notifier.add_subscriber(
            self.fitting_tab.fitting_tab_view.enable_tab_observer)

        self.enable_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.enable_tab_observer)

        self.enable_notifier.add_subscriber(
            self.phase_tab.phase_table_presenter.enable_tab_observer)

        self.enable_notifier.add_subscriber(
            self.results_tab.results_tab_presenter.enable_tab_observer)

        self.enable_notifier.add_subscriber(self.transform.enable_observer)

        self.enable_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.enable_tab_observer)

    def setup_load_observers(self):
        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.home_tab.home_tab_widget.loadObserver)

        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.loadObserver)

        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.corrections_tab.corrections_tab_presenter.load_observer)

        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.phase_tab.phase_table_presenter.run_change_observer)

        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.fitting_tab.fitting_tab_view.disable_tab_observer)

        self.load_widget.load_widget.loadNotifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.disable_tab_observer
        )

    def setup_gui_variable_observers(self):
        self.context.gui_context.gui_variables_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.gui_variables_observer
        )

        for observer in self.plot_widget.rebin_options_set_observers:
            self.context.gui_context.gui_variables_notifier.add_subscriber(
                observer)

        self.grouping_tab_widget.pairing_table_widget.selected_pair_changed_notifier.add_subscriber(
            self.plot_widget.data_mode.added_group_or_pair_observer)

        self.grouping_tab_widget.pairing_table_widget.selected_pair_changed_notifier.add_subscriber(
            self.transform.GroupPairObserver)

        self.grouping_tab_widget.grouping_table_widget.selected_group_changed_notifier.add_subscriber(
            self.transform.GroupPairObserver)

        self.grouping_tab_widget.grouping_table_widget.selected_group_changed_notifier.add_subscriber(
            self.plot_widget.data_mode.added_group_or_pair_observer)

        # differences
        self.grouping_tab_widget.diff_table.add_subscribers([
            self.transform.GroupPairObserver,
            self.plot_widget.data_mode.added_group_or_pair_observer
        ])

        self.fitting_tab.fitting_tab_presenter.fit_function_changed_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.
            fit_function_updated_observer)

        self.fitting_tab.fitting_tab_presenter.fit_parameter_changed_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.
            fit_parameter_updated_observer)

        self.seq_fitting_tab.seq_fitting_tab_presenter.fit_parameter_changed_notifier.add_subscriber(
            self.fitting_tab.fitting_tab_presenter.
            fit_parameter_updated_observer)

        self.seq_fitting_tab.seq_fitting_tab_presenter.sequential_fit_finished_notifier.add_subscriber(
            self.fitting_tab.fitting_tab_presenter.
            sequential_fit_finished_observer)

        self.fitting_tab.fitting_tab_presenter.selected_fit_results_changed.add_subscriber(
            self.plot_widget.fit_mode.plot_selected_fit_observer)

        self.seq_fitting_tab.seq_fitting_tab_presenter.selected_sequential_fit_notifier.add_subscriber(
            self.plot_widget.fit_mode.plot_selected_fit_observer)

        self.phase_tab.phase_table_presenter.selected_phasequad_changed_notifier.add_subscriber(
            self.plot_widget.data_mode.added_group_or_pair_observer)

        self.phase_tab.phase_table_presenter.selected_phasequad_changed_notifier.add_subscriber(
            self.transform.GroupPairObserver)

        self.phase_tab.phase_table_presenter.selected_phasequad_changed_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.
            selected_workspaces_observer)

    def setup_grouping_changed_observers(self):
        self.grouping_tab_widget.group_tab_presenter.groupingNotifier.add_subscriber(
            self.home_tab.home_tab_widget.groupingObserver)

        self.grouping_tab_widget.group_tab_presenter.groupingNotifier.add_subscriber(
            self.transform.GroupPairObserver)

        self.grouping_tab_widget.group_tab_presenter.groupingNotifier.add_subscriber(
            self.phase_tab.phase_table_presenter.group_change_observer)

    def setup_corrections_changed_observers(self):
        self.corrections_tab.corrections_tab_presenter.perform_corrections_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.gui_variables_observer
        )

    def setup_instrument_changed_notifier(self):
        self.context.data_context.instrumentNotifier.add_subscriber(
            self.home_tab.home_tab_widget.instrumentObserver)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.load_widget.load_widget.instrumentObserver)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.instrumentObserver)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.corrections_tab.corrections_tab_presenter.
            instrument_changed_observer)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.transform.instrumentObserver)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.phase_tab.phase_table_presenter.instrument_changed_observer)

        self.context.data_context.instrumentNotifier.add_subscriber(
            self.fitting_tab.fitting_tab_presenter.instrument_changed_observer)

        for observer in self.plot_widget.clear_plot_observers:
            self.context.data_context.instrumentNotifier.add_subscriber(
                observer)

    def setup_group_calculation_enable_notifier(self):
        self.grouping_tab_widget.group_tab_presenter.enable_editing_notifier.add_subscriber(
            self.enable_observer)

        self.corrections_tab.corrections_tab_presenter.enable_editing_notifier.add_subscriber(
            self.enable_observer)

        self.fitting_tab.fitting_tab_presenter.enable_editing_notifier.add_subscriber(
            self.enable_observer)

        self.phase_tab.phase_table_presenter.enable_editing_notifier.add_subscriber(
            self.enable_observer)

    def setup_group_calculation_disabler_notifier(self):
        self.grouping_tab_widget.group_tab_presenter.disable_editing_notifier.add_subscriber(
            self.disable_observer)

        self.corrections_tab.corrections_tab_presenter.disable_editing_notifier.add_subscriber(
            self.disable_observer)

        self.fitting_tab.fitting_tab_presenter.disable_editing_notifier.add_subscriber(
            self.disable_observer)

        self.phase_tab.phase_table_presenter.disable_editing_notifier.add_subscriber(
            self.disable_observer)

    def setup_on_load_enabler(self):
        self.load_widget.load_widget.load_run_widget.enable_notifier.add_subscriber(
            self.home_tab.home_tab_widget.enable_observer)

        self.load_widget.load_widget.load_run_widget.enable_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.enable_observer)

        self.load_widget.load_widget.load_run_widget.enable_notifier.add_subscriber(
            self.transform.enable_observer)

    def setup_on_load_disabler(self):
        self.load_widget.load_widget.load_run_widget.disable_notifier.add_subscriber(
            self.home_tab.home_tab_widget.disable_observer)

        self.load_widget.load_widget.load_run_widget.disable_notifier.add_subscriber(
            self.grouping_tab_widget.group_tab_presenter.disable_observer)

        self.load_widget.load_widget.load_run_widget.disable_notifier.add_subscriber(
            self.transform.disable_observer)

    def setup_counts_calculation_finished_notifier(self):
        self.grouping_tab_widget.group_tab_presenter.counts_calculation_finished_notifier.add_subscriber(
            self.corrections_tab.corrections_tab_presenter.
            pre_process_and_counts_calculated_observer)

    def setup_asymmetry_pair_and_diff_calculations_finished_notifier(self):
        for observer in self.plot_widget.data_changed_observers:
            self.corrections_tab.corrections_tab_presenter.asymmetry_pair_and_diff_calculations_finished_notifier.\
                add_subscriber(observer)
            self.phase_tab.phase_table_presenter.calculation_finished_notifier.add_subscriber(
                observer)

        self.corrections_tab.corrections_tab_presenter.asymmetry_pair_and_diff_calculations_finished_notifier.add_subscriber(
            self.transform.load_observer)

        self.corrections_tab.corrections_tab_presenter.asymmetry_pair_and_diff_calculations_finished_notifier.add_subscriber(
            self.seq_fitting_tab.seq_fitting_tab_presenter.
            selected_workspaces_observer)

    def setup_phase_quad_changed_notifier(self):
        self.phase_tab.phase_table_presenter.phasequad_calculation_complete_notifier.add_subscriber(
            self.transform.phase_quad_observer)

    def setup_phase_table_changed_notifier(self):
        self.phase_tab.phase_table_presenter.phase_table_calculation_complete_notifier.add_subscriber(
            self.transform._maxent._presenter.phase_table_observer)
        self.transform._maxent._presenter.new_phase_table.add_subscriber(
            self.phase_tab.phase_table_presenter.phase_table_observer)

    def setup_fitting_notifier(self):
        """Connect fitting and results tabs to inform of new fits"""
        self.fitting_context.new_fit_results_notifier.add_subscriber(
            self.results_tab.results_tab_presenter.new_fit_performed_observer)

        self.fitting_tab.fitting_tab_presenter.remove_plot_guess_notifier.add_subscriber(
            self.plot_widget.fit_mode.remove_plot_guess_observer)

        self.fitting_tab.fitting_tab_presenter.update_plot_guess_notifier.add_subscriber(
            self.plot_widget.fit_mode.update_plot_guess_observer)

    def closeEvent(self, event):
        self.tabs.closeEvent(event)
        self.context.ads_observer.unsubscribe()
        self.grouping_tab_widget.group_tab_presenter.closePeriodInfoWidget()
        super(FrequencyAnalysisGui, self).closeEvent(event)
Ejemplo n.º 4
0
class FrequencyAnalysisPlotWidgetTest(unittest.TestCase):
    def make_canvas_mock(self, key):
        self.canvas[key] = mock.MagicMock(autospec=PlottingCanvasWidget)
        return self.canvas[key]

    def canvas_mocks(self, parent, context, plot_model, figure_options=None):
        if plot_model.name == "Plot Data":
            return self.make_canvas_mock(DATA)
        elif plot_model.name == "Frequency Data":
            return self.make_canvas_mock(FIT)
        elif plot_model.name == "Raw Data":
            return self.make_canvas_mock(RAW)
        elif plot_model.name == "Model Data":
            return self.make_canvas_mock(MODEL)
        elif plot_model.name == "Maxent Dual plot":
            return self.make_canvas_mock(MAXENT)
        return self.make_canvas_mock("not found")

    # set up get methods for mock view
    @property
    def mock_view(self):
        return self.mock_main_view.return_value

    # since some functions are only called as part of the inherited class, we need to patch them there
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.PlottingCanvasWidget'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.BasePaneView'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.FrequencyDomainAnalysis.plot_widget.frequency_analysis_plot_widget.MainPlotWidgetView'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.FrequencyDomainAnalysis.plot_widget.frequency_analysis_plot_widget.PlottingCanvasWidget'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.FrequencyDomainAnalysis.plot_widget.frequency_analysis_plot_widget.BasePaneView'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.FrequencyDomainAnalysis.plot_widget.frequency_analysis_plot_widget.DualQuickEditWidget'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.FrequencyDomainAnalysis.plot_widget.frequency_analysis_plot_widget.DualPlotMaxentPaneView'
    )
    def setUp(self, mock_dual_view, mock_quick_edit, mock_base_view,
              mock_plot_canvas, mock_main_view, mock_muon_base_view,
              mock_muon_canvas):
        self.mock_dual_view = mock_dual_view
        self.mock_quick_edit = mock_quick_edit
        self.mock_main_view = mock_main_view
        self.mock_base_view = mock_base_view
        self.mock_plot_canvas = mock_plot_canvas
        self.mock_muon_base_view = mock_muon_base_view
        self.mock_muon_canvas = mock_muon_canvas

        self.canvas = {}
        self._num_count = 0
        self._letter_count = 0

        self.mock_dual_view.return_value = mock.MagicMock(
            autospec=DualPlotMaxentPaneView)
        self.mock_quick_edit.return_value = mock.MagicMock(
            autospec=DualQuickEditWidget)
        self.mock_main_view.return_value = mock.MagicMock(
            autospec=MainPlotWidgetView)
        self.mock_base_view.return_value = mock.MagicMock(
            autospec=BasePaneView)
        self.mock_plot_canvas.side_effect = self.canvas_mocks
        self.mock_muon_base_view.return_value = mock.MagicMock(
            autospec=BasePaneView)
        self.mock_muon_canvas.side_effect = self.canvas_mocks

        self.context = setup_context(freq=True)
        self.widget = FrequencyAnalysisPlotWidget(self.context)

    def test_setup(self):
        # want to check that by default only data, fit and maxent panes

        # check we have correct number of canvases, views etc.
        self.assertEqual(self.mock_plot_canvas.call_count, 2)
        self.assertEqual(self.mock_muon_canvas.call_count, 1)
        self.assertEqual(self.mock_muon_base_view.call_count, 1)
        self.assertEqual(self.mock_base_view.call_count, 1)
        self.assertEqual(self.mock_dual_view.call_count, 1)

        # check panes have been added, its just a list
        self.assertEqual(len(self.widget._panes), 3)

        # check the plot modes have been added
        self.assertEqual(list(self.widget.modes.keys()), [DATA, FIT, MAXENT])
        self.assertEqual(self.widget.modes[DATA].name, "Plot Data")
        # frequency is fit in this widget
        self.assertEqual(self.widget.modes[FIT].name, "Frequency Data")
        self.assertEqual(self.widget.modes[MAXENT].name, "Maxent Dual plot")

    def number_list(self):
        self._num_count += 1
        outputs = ["not used", "data 1", "fit 2", "maxent 3", "4"]
        return outputs[self._num_count]

    def letter_list(self):
        self._letter_count += 1
        outputs = ["not used", "data a", "fit b", "maxent c", "d"]
        return outputs[self._letter_count]

    def test_insert_plot_panes(self):
        type(self.mock_view).get_plot_mode = mock.PropertyMock(
            return_value="Plot Data")

        for key in self.widget.modes.keys():
            type(self.widget.modes[key]
                 ).workspace_replaced_in_ads_observer = mock.PropertyMock(
                     return_value=self.number_list())
            type(self.widget.modes[key]
                 ).workspace_deleted_from_ads_observer = mock.PropertyMock(
                     return_value=self.letter_list())

        self.context.update_plots_notifier.add_subscriber = mock.Mock()
        self.context.deleted_plots_notifier.add_subscriber = mock.Mock()
        self.widget.insert_plot_panes()
        self.context.update_plots_notifier.add_subscriber.assert_any_call(
            "data 1")
        self.context.update_plots_notifier.add_subscriber.assert_any_call(
            "fit 2")
        self.context.update_plots_notifier.add_subscriber.assert_any_call(
            "maxent 3")
        self.assertEqual(
            self.context.update_plots_notifier.add_subscriber.call_count, 3)

        self.context.deleted_plots_notifier.add_subscriber.assert_any_call(
            "data a")
        self.context.deleted_plots_notifier.add_subscriber.assert_any_call(
            "fit b")
        self.context.deleted_plots_notifier.add_subscriber.assert_any_call(
            "maxent c")
        self.assertEqual(
            self.context.deleted_plots_notifier.add_subscriber.call_count, 3)

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetView'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.PlottingCanvasWidget'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.BasePaneView'
    )
    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.RawPaneView'
    )
    def add_all_panes(self, mock_raw_view, mock_base_view, mock_plot_canvas,
                      mock_main_view):
        self.mock_main_view = mock_main_view
        self.mock_base_view = mock_base_view
        self.mock_raw_view = mock_raw_view
        self.mock_plot_canvas = mock_plot_canvas

        self.mock_main_view.return_value = mock.MagicMock(
            autospec=MainPlotWidgetView)
        self.mock_base_view.return_value = mock.MagicMock(
            autospec=BasePaneView)
        self.mock_raw_view.return_value = mock.MagicMock(autospec=RawPaneView)
        self.mock_plot_canvas.side_effect = self.canvas_mocks

        # can exclude the defaults (data and fit)
        self.widget.create_model_fit_pane()
        self.widget.create_raw_pane()
        # mock the update methods in new panes
        self.widget.modes[RAW].handle_data_updated = mock.Mock()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_leave_data(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Plot Data"
        type(mock_presenter.return_value).get_plot_mode = "Raw Data"

        self.widget.handle_plot_mode_changed_by_user()
        self.canvas[DATA].set_quick_edit_info.assert_not_called()
        self.canvas[FIT].set_quick_edit_info.assert_not_called()
        mock_presenter.return_value.hide.assert_called_once_with("Plot Data")

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_leave_fit(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Fit Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Raw Data")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.hide.assert_called_once_with("Fit Data")
        self.canvas[DATA].set_quick_edit_info.assert_not_called()
        self.canvas[FIT].set_quick_edit_info.assert_not_called()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_leave_raw(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Raw Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Fit Data")

        self.widget.handle_plot_mode_changed_by_user()
        # check it does not force any plots to update
        self.canvas[DATA].set_quick_edit_info.assert_not_called()
        self.canvas[FIT].set_quick_edit_info.assert_not_called()
        mock_presenter.return_value.hide.assert_called_once_with("Raw Data")

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_leave_model(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Model Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Fit Data")

        self.widget.handle_plot_mode_changed_by_user()
        # check it does not force any plots to update
        self.canvas[DATA].set_quick_edit_info.assert_not_called()
        self.canvas[FIT].set_quick_edit_info.assert_not_called()
        mock_presenter.return_value.hide.assert_called_once_with("Model Data")

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_leave_maxent(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Maxent Dual plot"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Fit Data")

        self.widget.handle_plot_mode_changed_by_user()
        # check it does not force any plots to update
        self.canvas[DATA].set_quick_edit_info.assert_not_called()
        self.canvas[FIT].set_quick_edit_info.assert_not_called()
        mock_presenter.return_value.hide.assert_called_once_with(
            "Maxent Dual plot")

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_to_data(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Model Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Plot Data")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.show.assert_called_once_with("Plot Data")
        self.widget.modes[RAW].handle_data_updated.assert_not_called()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_to_fit(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Model Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Fit Data")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.show.assert_called_once_with("Fit Data")
        self.widget.modes[RAW].handle_data_updated.assert_not_called()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_to_raw(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)

        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Model Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Raw Data")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.show.assert_called_once_with("Raw Data")
        self.widget.modes[RAW].handle_data_updated.assert_called_once_with()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_to_model(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)
        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Raw Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Model Data")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.show.assert_called_once_with("Model Data")
        self.widget.modes[RAW].handle_data_updated.assert_not_called()

    @mock.patch(
        'mantidqtinterfaces.Muon.GUI.MuonAnalysis.plot_widget.muon_analysis_plot_widget.MainPlotWidgetPresenter'
    )
    def test_plot_mode_changed_by_user_to_maxent(self, mock_presenter):
        mock_presenter.return_value = mock.MagicMock(
            autospec=MainPlotWidgetPresenter)
        self.add_all_panes()
        self.widget.insert_plot_panes()
        self.widget._current_plot_mode = "Raw Data"
        type(mock_presenter.return_value).get_plot_mode = mock.PropertyMock(
            return_value="Maxent Dual plot")

        self.widget.handle_plot_mode_changed_by_user()
        mock_presenter.return_value.show.assert_called_once_with(
            "Maxent Dual plot")
        self.widget.modes[RAW].handle_data_updated.assert_not_called()