예제 #1
0
    def setUp(self):
        self.results_context = ResultsContext()
        self.result_table_names = ["Name1", "Name2"]

        workspace = WorkspaceFactory.create("Workspace2D",
                                            NVectors=3,
                                            YLength=5,
                                            XLength=5)
        add_ws_to_ads("Name1", workspace)
        add_ws_to_ads("Name2", workspace)
예제 #2
0
def create_test_model(input_workspaces,
                      function_name,
                      parameters,
                      output_workspace_names=None,
                      logs=None,
                      global_parameters=None):
    """
    Create a list of fits with time series logs on the workspaces
    :param input_workspaces: See create_test_fits
    :param function_name: See create_test_fits
    :param parameters: See create_test_fits
    :param logs: A list of (name, (values...), (name, (values...)))
    :param global_parameters: An optional list of tied parameters
    :return: A list of Fits with workspaces/logs attached
    """
    fits = create_test_fits(input_workspaces, function_name, parameters,
                            output_workspace_names, global_parameters)
    logs = logs if logs is not None else []
    for fit, workspace_name in zip(fits, input_workspaces):
        add_logs(workspace_name, logs)

    fitting_context = TFAsymmetryFittingContext()
    for fit in fits:
        fitting_context.add_fit(fit)
    return fitting_context, ResultsTabModel(fitting_context, ResultsContext())
예제 #3
0
def setup_context(freq=False):
    loaded_data = MuonLoadData()
    loaded_data.get_main_field_direction = mock.MagicMock(
        return_value='transverse')
    data_context = MuonDataContext(load_data=loaded_data)
    gui_context = MuonGuiContext()
    group_context = MuonGroupPairContext(
        data_context.check_group_contains_valid_detectors)
    corrections_context = CorrectionsContext(loaded_data)
    phase_table_context = PhaseTableContext()
    freq_context = FrequencyContext()
    plot_panes_context = PlotPanesContext()

    if freq:
        return FrequencyDomainAnalysisContext(
            muon_data_context=data_context,
            muon_group_context=group_context,
            muon_gui_context=gui_context,
            muon_phase_context=phase_table_context,
            corrections_context=corrections_context,
            fitting_context=BasicFittingContext(
                allow_double_pulse_fitting=True),
            frequency_context=freq_context,
            plot_panes_context=plot_panes_context)
    else:
        return DataAnalysisContext(muon_data_context=data_context,
                                   muon_group_context=group_context,
                                   muon_gui_context=gui_context,
                                   corrections_context=corrections_context,
                                   muon_phase_context=phase_table_context,
                                   fitting_context=TFAsymmetryFittingContext(
                                       allow_double_pulse_fitting=True),
                                   results_context=ResultsContext(),
                                   model_fitting_context=ModelFittingContext(),
                                   plot_panes_context=plot_panes_context)
예제 #4
0
def setup_context_for_tests(parent_object):
    parent_object.loaded_data = MuonLoadData()
    parent_object.loaded_data.get_main_field_direction = mock.MagicMock(
        return_value='transverse')
    parent_object.data_context = MuonDataContext(
        load_data=parent_object.loaded_data)
    parent_object.gui_context = MuonGuiContext()
    parent_object.group_context = MuonGroupPairContext(
        parent_object.data_context.check_group_contains_valid_detectors)
    parent_object.corrections_context = CorrectionsContext(
        parent_object.loaded_data)
    parent_object.phase_table_context = PhaseTableContext()
    parent_object.fitting_context = TFAsymmetryFittingContext(
        allow_double_pulse_fitting=True)
    parent_object.results_context = ResultsContext()
    parent_object.plot_panes_context = PlotPanesContext()
    parent_object.model_fitting_context = ModelFittingContext()
    parent_object.context = DataAnalysisContext(
        muon_data_context=parent_object.data_context,
        muon_group_context=parent_object.group_context,
        muon_gui_context=parent_object.gui_context,
        muon_phase_context=parent_object.phase_table_context,
        corrections_context=parent_object.corrections_context,
        fitting_context=parent_object.fitting_context,
        results_context=parent_object.results_context,
        model_fitting_context=parent_object.model_fitting_context,
        plot_panes_context=parent_object.plot_panes_context)
예제 #5
0
    def test_create_results_table_raises_error_if_number_params_different(
            self):
        parameters = OrderedDict([('Height', (100, 0.1)),
                                  ('Cost function value', (1.5, 0))])
        fits_func1 = create_test_fits(('ws1',), 'func1', parameters)

        parameters = OrderedDict([('Height', (100, 0.1)), ('A0', (1, 0.001)),
                                  ('Cost function value', (1.5, 0))])
        fits_func2 = create_test_fits(('ws2',), 'func2', parameters)

        fitting_context = TFAsymmetryFittingContext()
        fitting_context.fit_list = fits_func1 + fits_func2
        model = ResultsTabModel(fitting_context, ResultsContext())

        selected_results = [('ws1', 0), ('ws2', 1)]
        self.assertRaises(IndexError, model.create_results_table, [],
                          selected_results)
예제 #6
0
    def test_that_when_new_fit_is_performed_function_name_is_set_to_lastest_fit_name(self):
        parameters = OrderedDict([('Height', (100, 0.1)),
                                  ('Cost function value', (1.5, 0))])
        fits_func1 = create_test_fits(('ws1',), 'func1', parameters)

        parameters = OrderedDict([('Height', (100, 0.1)), ('A0', (1, 0.001)),
                                  ('Cost function value', (1.5, 0))])
        fits_func2 = create_test_fits(('ws2',), 'func2', parameters)

        fitting_context = TFAsymmetryFittingContext()
        fits = fits_func1 + fits_func2
        for fit in fits:
            fitting_context.add_fit(fit)
        model = ResultsTabModel(fitting_context, ResultsContext())

        model.on_new_fit_performed()

        self.assertEqual(model.selected_fit_function(), 'func2')
예제 #7
0
    def test_create_results_table_with_mixed_global_non_global_raises_error(
            self):
        parameters = OrderedDict([('f0.Height', (100, 0.1)),
                                  ('f1.Height', (90, 0.001)),
                                  ('Cost function value', (1.5, 0))])
        fits_func1 = create_test_fits(('ws1',), 'func1', parameters)
        fits_globals = create_test_fits(('ws2',),
                                        'func1',
                                        parameters,
                                        global_parameters=['Height'])

        fitting_context = TFAsymmetryFittingContext()
        fitting_context.fit_list = fits_func1 + fits_globals
        model = ResultsTabModel(fitting_context, ResultsContext())

        selected_results = [('ws1', 0), ('ws2', 1)]
        self.assertRaises(IndexError, model.create_results_table, [],
                          selected_results)
예제 #8
0
class ResultsContextTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        FrameworkManager.Instance()

    def setUp(self):
        self.results_context = ResultsContext()
        self.result_table_names = ["Name1", "Name2"]

        workspace = WorkspaceFactory.create("Workspace2D",
                                            NVectors=3,
                                            YLength=5,
                                            XLength=5)
        add_ws_to_ads("Name1", workspace)
        add_ws_to_ads("Name2", workspace)

    def test_that_the_context_has_been_instantiated_with_empty_data(self):
        self.assertEqual(self.results_context.result_table_names, [])

    def test_that_the_result_table_names_can_be_set_in_the_context(self):
        self.results_context.result_table_names = self.result_table_names
        self.assertEqual(self.results_context.result_table_names,
                         self.result_table_names)

    def test_that_add_result_table_will_add_a_table_name(self):
        self.results_context.add_result_table(self.result_table_names[0])
        self.assertEqual(self.results_context.result_table_names,
                         [self.result_table_names[0]])

        self.results_context.add_result_table(self.result_table_names[1])
        self.assertEqual(self.results_context.result_table_names,
                         self.result_table_names)

    def test_that_add_result_table_will_not_add_a_duplicate_table_name(self):
        self.results_context.add_result_table(self.result_table_names[0])
        self.assertEqual(self.results_context.result_table_names,
                         [self.result_table_names[0]])

        self.results_context.add_result_table(self.result_table_names[0])
        self.assertEqual(self.results_context.result_table_names,
                         [self.result_table_names[0]])
예제 #9
0
    def test_create_results_table_with_logs_missing_from_some_workspaces_raises(self):
        workspace = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5)

        parameters = OrderedDict([('f0.Height', (100, 0.1))])
        logs = [('log1', (1., 2.)), ('log2', (3., 4.)), ('log3', (4., 5.)),
                ('log4', (5., 6.))]
        fits_logs1 = create_test_fits(('ws1',), 'func1', parameters,
                                      output_workspace_names=[StaticWorkspaceWrapper('test-ws1-ws', workspace)])
        add_logs(fits_logs1[0].input_workspaces[0], logs[:2])

        fits_logs2 = create_test_fits(('ws2',), 'func1', parameters,
                                      output_workspace_names=[StaticWorkspaceWrapper('test-ws2-ws', workspace)])
        add_logs(fits_logs2[0].input_workspaces[0], logs[2:])

        fitting_context = TFAsymmetryFittingContext()
        fitting_context.fit_list = fits_logs1 + fits_logs2
        model = ResultsTabModel(fitting_context, ResultsContext())

        selected_results = [('ws1', 0), ('ws2', 1)]
        selected_logs = ['log1', 'log3']
        self.assertRaises(IndexError, model.create_results_table,
                          selected_logs, selected_results)
예제 #10
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])

        # 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.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,
            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.setup_tabs()
        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)

        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)
예제 #11
0
 def test_model_returns_no_log_selection_if_no_fits_present(self):
     model = ResultsTabModel(TFAsymmetryFittingContext(), ResultsContext())
     self.assertEqual(0, len(model.log_selection({})))
예제 #12
0
    def test_updating_model_selected_fit_function(self):
        model = ResultsTabModel(TFAsymmetryFittingContext(), ResultsContext())
        new_selection = 'func2'
        model.set_selected_fit_function(new_selection)

        self.assertEqual(model.selected_fit_function(), new_selection)
예제 #13
0
    def test_default_model_has_no_selected_function_without_fits(self):
        model = ResultsTabModel(TFAsymmetryFittingContext(), ResultsContext())

        self.assertTrue(model.selected_fit_function() is None)
예제 #14
0
    def test_updating_model_results_table_name(self):
        table_name = 'table_name'
        model = ResultsTabModel(TFAsymmetryFittingContext(), ResultsContext())
        model.set_results_table_name(table_name)

        self.assertEqual(model.results_table_name(), table_name)
예제 #15
0
 def test_default_model_has_results_table_name(self):
     model = ResultsTabModel(TFAsymmetryFittingContext(), ResultsContext())
     self.assertEqual(model.results_table_name(), DEFAULT_TABLE_NAME)