def setUp(self): self.fitting_context = BasicFittingContext() self.dataset_names = ["Name1", "Name2"] self.fit_function = FunctionFactory.createFunction("FlatBackground") self.single_fit_functions = [ self.fit_function.clone(), self.fit_function.clone() ]
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)
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 test_that_allow_double_pulse_fitting_is_set_to_false_by_default(self): self.assertFalse(self.fitting_context.allow_double_pulse_fitting) self.fitting_context = BasicFittingContext( allow_double_pulse_fitting=True) self.assertTrue(self.fitting_context.allow_double_pulse_fitting)
class BasicFittingContextTest(unittest.TestCase): @classmethod def setUpClass(cls): FrameworkManager.Instance() def setUp(self): self.fitting_context = BasicFittingContext() self.dataset_names = ["Name1", "Name2"] self.fit_function = FunctionFactory.createFunction("FlatBackground") self.single_fit_functions = [ self.fit_function.clone(), self.fit_function.clone() ] def test_that_allow_double_pulse_fitting_is_set_to_false_by_default(self): self.assertFalse(self.fitting_context.allow_double_pulse_fitting) self.fitting_context = BasicFittingContext( allow_double_pulse_fitting=True) self.assertTrue(self.fitting_context.allow_double_pulse_fitting) def test_that_the_context_has_been_instantiated_with_empty_fit_data(self): self.assertEqual(self.fitting_context.dataset_names, []) self.assertEqual(self.fitting_context.current_dataset_index, None) self.assertEqual(self.fitting_context.start_xs, []) self.assertEqual(self.fitting_context.end_xs, []) self.assertEqual(self.fitting_context.exclude_range, False) self.assertEqual(self.fitting_context.exclude_start_xs, []) self.assertEqual(self.fitting_context.exclude_end_xs, []) self.assertEqual(self.fitting_context.single_fit_functions, []) self.assertEqual(self.fitting_context.dataset_indices_for_undo, []) self.assertEqual(self.fitting_context.single_fit_functions_for_undo, []) self.assertEqual(self.fitting_context.fit_statuses_for_undo, []) self.assertEqual(self.fitting_context.chi_squared_for_undo, []) self.assertEqual(self.fitting_context.fit_statuses, []) self.assertEqual(self.fitting_context.chi_squared, []) self.assertEqual(self.fitting_context.plot_guess, False) self.assertEqual(self.fitting_context.plot_guess_type, 'x from fit range') self.assertEqual(self.fitting_context.plot_guess_points, None) self.assertEqual(self.fitting_context.plot_guess_start_x, None) self.assertEqual(self.fitting_context.plot_guess_end_x, None) self.assertEqual(self.fitting_context.guess_workspace_name, None) self.assertEqual(self.fitting_context.function_name, "") self.assertTrue(self.fitting_context.function_name_auto_update) self.assertEqual(self.fitting_context.minimizer, "") self.assertEqual(self.fitting_context.evaluation_type, "") self.assertTrue(self.fitting_context.fit_to_raw) def test_that_current_dataset_index_will_raise_if_the_index_is_greater_than_or_equal_to_the_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.current_dataset_index = 2 def test_that_current_dataset_index_does_not_throw_when_provided_None_and_there_are_no_datasets_loaded( self): self.fitting_context.current_dataset_index = None def test_that_number_of_datasets_returns_the_expected_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names self.assertEqual(self.fitting_context.number_of_datasets, 2) def test_that_start_xs_will_raise_if_the_number_of_xs_is_greater_than_or_equal_to_the_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.start_xs = [1.0, 2.0, 3.0] def test_that_end_xs_will_raise_if_the_number_of_xs_is_greater_than_or_equal_to_the_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.end_xs = [1.0, 2.0, 3.0] def test_that_exclude_start_xs_will_raise_if_the_number_of_xs_is_greater_than_or_equal_to_the_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.exclude_start_xs = [1.0, 2.0, 3.0] def test_that_exclude_end_xs_will_raise_if_the_number_of_xs_is_greater_than_or_equal_to_the_number_of_datasets( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.exclude_end_xs = [1.0, 2.0, 3.0] def test_that_single_fit_functions_will_raise_if_the_num_of_funcs_is_greater_than_or_equal_to_the_num_of_datasets( self): self.fitting_context.dataset_names = ["Name1"] with self.assertRaises(RuntimeError): self.fitting_context.single_fit_functions = self.single_fit_functions def test_that_fit_statuses_will_raise_if_the_number_of_datasets_is_smaller_than_the_provided_list_size( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.fit_statuses = [ "Success", "Success", "Success" ] def test_that_chi_squared_will_raise_if_the_number_of_datasets_is_smaller_than_the_provided_list_size( self): self.fitting_context.dataset_names = self.dataset_names with self.assertRaises(RuntimeError): self.fitting_context.chi_squared = [1.0, 2.0, 3.0] def test_that_guess_workspace_name_will_not_raise_if_the_workspace_does_not_exist( self): self.fitting_context.guess_workspace_name = "FakeName" self.assertEqual(self.fitting_context.guess_workspace_name, None) def test_that_guess_workspace_name_will_not_raise_if_the_name_provided_is_none( self): self.fitting_context.guess_workspace_name = None self.assertEqual(self.fitting_context.guess_workspace_name, None) def test_that_clear_will_clear_the_undo_data_and_active_fits_list(self): self.fitting_context.active_fit_history = [mock.Mock(), mock.Mock()] self.fitting_context.dataset_indices_for_undo = [0, 1] self.fitting_context.single_fit_functions_for_undo = [ mock.Mock(), mock.Mock() ] self.fitting_context.fit_statuses_for_undo = ["Success", "Fail"] self.fitting_context.chi_squared_for_undo = [2.2, 3.3] self.fitting_context.clear() self.assertEqual(self.fitting_context.active_fit_history, []) self.assertEqual(self.fitting_context.all_latest_fits(), []) self.assertEqual(self.fitting_context.dataset_indices_for_undo, []) self.assertEqual(self.fitting_context.single_fit_functions_for_undo, []) self.assertEqual(self.fitting_context.fit_statuses_for_undo, []) self.assertEqual(self.fitting_context.chi_squared_for_undo, []) def test_that_all_latest_fits_will_return_the_two_most_recent_unique_fits( self): output_ws = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) table_workspace = WorkspaceFactory.createTable() output_ws_wrap1 = StaticWorkspaceWrapper("Output1", output_ws) parameter_ws_wrap1 = StaticWorkspaceWrapper("Parameter1", table_workspace) covariance_ws_wrap1 = StaticWorkspaceWrapper("Covariance1", table_workspace) output_ws_wrap2 = StaticWorkspaceWrapper("Output2", output_ws) parameter_ws_wrap2 = StaticWorkspaceWrapper("Parameter2", table_workspace) covariance_ws_wrap2 = StaticWorkspaceWrapper("Covariance2", table_workspace) fit1 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) fit2 = FitInformation(["Input2"], "GausOsc", [output_ws_wrap2], parameter_ws_wrap2, covariance_ws_wrap2) fit3 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) self.fitting_context.active_fit_history = [fit1, fit2, fit3] self.assertEqual(self.fitting_context.active_fit_history[0], fit1) self.assertEqual(self.fitting_context.active_fit_history[1], fit2) self.assertEqual(self.fitting_context.active_fit_history[2], fit3) # fit3 == fit1 and fit3 is more recent, so 'all_latest_fits' will only return the most recent unique fits. self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[1], fit3) def test_remove_workspace_by_name_will_remove_a_fit_containing_a_specific_parameter_workspace( self): output_ws = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) table_workspace = WorkspaceFactory.createTable() output_ws_wrap1 = StaticWorkspaceWrapper("Output1", output_ws) parameter_ws_wrap1 = StaticWorkspaceWrapper("Parameter1", table_workspace) covariance_ws_wrap1 = StaticWorkspaceWrapper("Covariance1", table_workspace) output_ws_wrap2 = StaticWorkspaceWrapper("Output2", output_ws) parameter_ws_wrap2 = StaticWorkspaceWrapper("Parameter2", table_workspace) covariance_ws_wrap2 = StaticWorkspaceWrapper("Covariance2", table_workspace) fit1 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) fit2 = FitInformation(["Input2"], "GausOsc", [output_ws_wrap2], parameter_ws_wrap2, covariance_ws_wrap2) self.fitting_context.active_fit_history = [fit1, fit2] self.fitting_context.remove_workspace_by_name("Parameter1") self.assertEqual(self.fitting_context.active_fit_history[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2)