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 test_that_contains_is_true_when_data_contains_entry_which_matches_to_a_single_keyword_and_false_otherwise( self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertTrue(data.contains(run=1234)) self.assertTrue(data.contains(filename="C:\\dir1\\file1.nxs")) self.assertFalse(data.contains(run=9999)) self.assertFalse(data.contains(filename="C:\\dir1\\file4.nxs"))
def test_counting_entries_with_keyword_argument_gives_correct_count(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertEqual(data.contains_n(run=1234), 2) self.assertEqual(data.contains_n(filename="C:\\dir1\\file1.nxs"), 1) self.assertEqual(data.contains_n(run=9999), 0)
def setUp(self): self.filepath = FileFinder.findRuns('EMU00019489.nxs')[0] self.load_result, self.run_number, self.filename, _ = load_workspace_from_filename( self.filepath) self.loaded_data = MuonLoadData() self.context = MuonDataContext(load_data=self.loaded_data) self.context.instrument = 'EMU' self.loaded_data.add_data(workspace=self.load_result, run=[self.run_number], filename=self.filename, instrument='EMU') self.context.current_runs = [[self.run_number]] self.context.update_current_data()
def test_that_remove_method_can_remove_several_entries_at_once(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1234, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertEqual(data.num_items(), 3) data.remove_data(run=1234) self.assert_empty(data)
def test_data_can_be_added_correctly_via_keyword_args(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") self.assertEqual(data.num_items(), 1) self.assertEqual(data.contains_n(run=1234), 1) self.assertEqual(data.contains_n(workspace=[1]), 1) self.assertEqual(data.contains_n(filename="C:\\dir1\\file1.nxs"), 1)
def __init__(self, loaded_data_store=MuonLoadData(), context=None): # Used with load thread self._filenames = [] self._loaded_data_store = loaded_data_store self._data_context = context.data_context self._current_run = None
def setup_context_for_ea_tests(parent_object): parent_object.loaded_data = MuonLoadData() parent_object.data_context = DataContext(load_data=parent_object.loaded_data) parent_object.gui_context = MuonGuiContext() parent_object.plot_panes_context = PlotPanesContext() parent_object.group_context = EAGroupContext(parent_object.data_context.check_group_contains_valid_detectors) parent_object.context = ElementalAnalysisContext(parent_object.data_context, parent_object.group_context, parent_object.gui_context, parent_object.plot_panes_context)
def setUp(self): self.muon_load_data = MuonLoadData() self.workspace = mock.MagicMock() self.workspace_last = mock.MagicMock() self.muon_load_data.add_data(run=1, workspace=mock.MagicMock(), filename='path to file') self.muon_load_data.add_data(run=2, workspace=self.workspace, filename='path to file') self.muon_load_data.add_data(run=3, workspace=mock.MagicMock(), filename='matching path') self.muon_load_data.add_data() self.muon_load_data.add_data(run=4, workspace=self.workspace_last, filename='path to file')
def test_that_contains_is_false_when_data_contains_entries_which_match_to_only_one_of_multiple_keywords( self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") # values from keywords correspond to different entries self.assertFalse( data.contains(run=1234, filename="C:\\dir1\\file2.nxs")) self.assertTrue(data.contains(run=1234, workspace=[3]))
def test_counting_entries_applies_AND_behaviour_to_keyword_arguments(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") data.add_data(run=1236, workspace=[4], filename="C:\\dir1\\file4.nxs") self.assertEqual(data.contains_n(run=1234, workspace=[1]), 1) self.assertEqual(data.contains_n(run=1234, workspace=[2]), 0) self.assertEqual( data.contains_n(run=1234, workspace=[2], filename="C:\\dir1\\file4.nxs"), 0)
def populate_loaded_data(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs", instrument='EMU') data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs", instrument='EMU') data.add_data(run=1236, workspace=[3], filename="C:\\dir1\\file3.nxs", instrument='EMU') return data
def __init__(self, base_directory="Muon Data", load_data=MuonLoadData()): """ Currently, only a single run is loaded into the Home/Grouping tab at once. This is held in the _current_data member. The load widget may load multiple runs at once, these are stored in the _loaded_data member. Groups and Pairs associated to the current run are stored in _grousp and _pairs as ordered dictionaries. """ self._loaded_data = load_data self._current_runs = [] self._main_field_direction = '' self._instrument = ConfigService.getInstrument().name() if ConfigService.getInstrument().name()\ in allowed_instruments else 'EMU' self.instrumentNotifier = MuonDataContext.InstrumentNotifier(self) self.message_notifier = MuonDataContext.MessageNotifier(self) self.base_directory = base_directory
def setUp(self): self.obj = QWidget() self.loaded_data = MuonLoadData() setup_context_for_tests(self) self.model = GroupingTabModel(context=self.context) self.grouping_table_view = GroupingTableView() self.grouping_table_widget = GroupingTablePresenter( self.grouping_table_view, self.model) self.pairing_table_view = PairingTableView() self.pairing_table_widget = PairingTablePresenter( self.pairing_table_view, self.model) self.diff_widget = DifferencePresenter(self.model) self.diff_widget.group_view.enter_diff_name = mock.Mock( side_effect=diff_name()) self.diff_widget.pair_view.enter_diff_name = mock.Mock( side_effect=diff_name()) self.grouping_table_view.warning_popup = mock.MagicMock() self.pairing_table_view.warning_popup = mock.MagicMock() self.add_three_groups() self.add_two_pairs() self.view = GroupingTabView(self.grouping_table_view, self.pairing_table_view, self.diff_widget.view) self.presenter = GroupingTabPresenter(self.view, self.model, self.grouping_table_widget, self.pairing_table_widget, self.diff_widget) self.presenter.create_update_thread = mock.MagicMock( return_value=mock.MagicMock()) self.presenter.pairing_table_widget.handle_add_pair_button_clicked = mock.MagicMock( ) self.view.display_warning_box = mock.MagicMock() self.grouping_table_view.warning_popup = mock.MagicMock() self.pairing_table_view.warning_popup = mock.MagicMock()
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)
def setUp(self): self.obj = QWidget() self.loaded_data = MuonLoadData() setup_context_for_ea_tests(self) self.model = EAGroupingTabModel(context=self.context) self.grouping_table_view = EAGroupingTableView() self.grouping_table_widget = EAGroupingTablePresenter(self.grouping_table_view, self.model) self.grouping_table_view.warning_popup = mock.MagicMock() self.view = EAGroupingTabView(self.grouping_table_view) self.presenter = EAGroupingTabPresenter(self.view, self.model, self.grouping_table_widget) self.presenter.create_update_thread = mock.MagicMock(return_value=mock.MagicMock()) self.view.display_warning_box = mock.MagicMock() self.grouping_table_view.warning_popup = mock.MagicMock()
class EAGroupContextTest(unittest.TestCase): def setUp(self): self.context = EAGroupContext() self.loadedData = MuonLoadData() def create_group_workspace_and_load(self): grpws = WorkspaceGroup() ws_detector1 = '9999; Detector 1' grpws.addWorkspace(CreateSampleWorkspace(OutputWorkspace=ws_detector1)) ws_detector2 = '9999; Detector 2' grpws.addWorkspace(CreateSampleWorkspace(OutputWorkspace=ws_detector2)) run = 9999 self.loadedData.add_data(run=[run], workspace=grpws) # ------------------------------------------------------------------------------------------------------------------ # TESTS # ------------------------------------------------------------------------------------------------------------------ def test_add_new_group(self): self.loadedData.clear() self.create_group_workspace_and_load() empty_group = [] new_group = self.context.add_new_group(empty_group, self.loadedData) self.assertEqual(len(new_group), 2) def test_remove_group(self): self.loadedData.clear() self.create_group_workspace_and_load() self.context.reset_group_to_default(self.loadedData) self.assertEqual(len(self.context.groups), 2) group_name1 = '9999; Detector 1' self.assertTrue(group_name1 in self.context.group_names) self.context.remove_group(group_name1) self.assertFalse(group_name1 in self.context.group_names) def test_reset_group_to_default(self): self.loadedData.clear() self.assertEqual(self.loadedData.num_items(), 0) self.create_group_workspace_and_load() self.context.reset_group_to_default(self.loadedData) self.assertEqual(len(self.context.groups), 2) def test_clear(self): self.loadedData.clear() self.create_group_workspace_and_load() self.context.reset_group_to_default(self.loadedData) self.assertEqual(len(self.context.groups), 2) self.context.clear() self.assertEqual(len(self.context.groups), 0) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_rebinned_workspace" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_peak_table" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_matches_group" ) def test_remove_rebinned_workspace_from_group(self, mock_remove_matches, mock_remove_peak, mock_remove_rebinned): mock_group = EAGroup("9999; Detector 1", "detector 1", "9999") self.context.add_group(mock_group) self.context.remove_workspace_from_group( '9999; Detector 1_EA_Rebinned_Fixed') mock_remove_rebinned.assert_called_once() mock_remove_peak.assert_not_called() mock_remove_matches.assert_not_called() @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_rebinned_workspace" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_peak_table" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_matches_group" ) def test_remove_peak_table_from_group(self, mock_remove_matches, mock_remove_peak, mock_remove_rebinned): mock_group = EAGroup("9999; Detector 1", "detector 1", "9999") self.context.add_group(mock_group) self.context.remove_workspace_from_group( '9999; Detector 1_EA_peak_table') mock_remove_rebinned.assert_not_called() mock_remove_peak.aassert_called_once() mock_remove_matches.assert_not_called() @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_rebinned_workspace" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_peak_table" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_matches_group" ) def test_remove_matches_table_from_group(self, mock_remove_matches, mock_remove_peak, mock_remove_rebinned): mock_group = EAGroup("9999; Detector 1", "detector 1", "9999") self.context.add_group(mock_group) self.context.remove_workspace_from_group('9999; Detector 1_EA_matches') mock_remove_rebinned.assert_not_called() mock_remove_peak.aassert_not_called() mock_remove_matches.assert_called_once() @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_rebinned_workspace" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_peak_table" ) @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.EAGroup.remove_matches_group" ) def test_remove_workspace_from_group_when_not_in_group( self, mock_remove_matches, mock_remove_peak, mock_remove_rebinned): mock_group = EAGroup("9999; Detector 1", "detector 1", "9999") self.context.add_group(mock_group) self.context.remove_workspace_from_group('mock_workspace') mock_remove_rebinned.assert_not_called() mock_remove_peak.aassert_not_called() mock_remove_matches.assert_not_called() @mock.patch( "mantidqtinterfaces.Muon.GUI.ElementalAnalysis2.ea_group.remove_ws_if_present" ) def test_error_raised_when_deleting_EAGroup(self, mock_remove_ws): self.loadedData.clear() self.create_group_workspace_and_load() self.context.reset_group_to_default(self.loadedData) self.assertEqual(len(self.context.groups), 2) group_name1 = '9999; Detector 1' self.assertTrue(group_name1 in self.context.group_names) mock_remove_ws.side_effect = ValueError("mock error") error_notifier_mock = mock.Mock() self.context[group_name1].error_notifier = error_notifier_mock self.context.remove_group(group_name1) self.assertFalse(group_name1 in self.context.group_names) mock_remove_ws.assert_called_once_with(group_name1) error_notifier_mock.notify_subscribers.assert_called_once_with( f"Unexpected error occurred when" f" deleting group {group_name1}: mock error")
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(self): setup_context_for_tests(self) self.model = BrowseFileWidgetModel(self.loaded_data, self.context) self.model._loaded_data_store = MuonLoadData()
def __init__(self, loaded_data_store=MuonLoadData(), context=None): # Temporary list of filenames used for load thread self._filenames = [] self._loaded_data_store = loaded_data_store self._data_context = context.data_context
def test_load_data_initialized_as_empty(self): data = MuonLoadData() self.assert_empty(data)
def __init__(self, loaded_data_store=MuonLoadData(), context=None): self._loaded_data_store = loaded_data_store self._data_context = context.data_context self._runs = [] self._current_run = None self._directory = ""
def test_that_adding_then_removing_single_item_leaves_data_empty(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") self.assertEqual(data.num_items(), 1) data.remove_data(run=1234) self.assert_empty(data) data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.remove_data(workspace=[1]) self.assert_empty(data) data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.remove_data(filename="C:\\dir1\\file1.nxs") self.assert_empty(data)
def __init__(self, load_data=MuonLoadData()): self.instrument = "rooth" self.current_runs = [] self._loaded_data = load_data self._run_info = [] self.previous_runs = []
def __init__(self, parent=None): super(ElementalAnalysisGui, self).__init__(parent) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setObjectName("ElementalAnalysis2") # setup error notifier and observer for context and group context self.error_notifier = GenericObservable() self.error_observer = GenericObserverWithArgPassing(self.warning_popup) self.error_notifier.add_subscriber(self.error_observer) self.loaded_data = MuonLoadData() self.data_context = DataContext(self.loaded_data) self.group_context = EAGroupContext( self.data_context.check_group_contains_valid_detectors, self.error_notifier) self.gui_context = MuonGuiContext() self.plot_panes_context = PlotPanesContext() self.context = ElementalAnalysisContext(self.data_context, self.group_context, self.gui_context, self.plot_panes_context, self.error_notifier) self.current_tab = '' self.plot_widget = EAPlotWidget(self.context, parent=self) self.dockable_plot_widget_window = PlottingDockWidget( parent=self, plotting_widget=self.plot_widget.view) self.dockable_plot_widget_window.setMinimumWidth(800) # Add dock widget to main Elemental 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) # disable and enable notifiers self.disable_notifier = GenericObservable() self.enable_notifier = GenericObservable() # disable and enable observers self.disable_observer = GenericObserver( self.disable_notifier.notify_subscribers) self.enable_observer = GenericObserver( self.enable_notifier.notify_subscribers) self.setup_dummy() self.setup_tabs() self.help_widget = HelpWidget("Elemental Analysis") central_widget = QtWidgets.QWidget() vertical_layout = QtWidgets.QVBoxLayout() vertical_layout.addWidget(self.load_widget.view) vertical_layout.addWidget(self.tabs) vertical_layout.addWidget(self.help_widget.view) central_widget.setLayout(vertical_layout) self.setCentralWidget(central_widget) self.setWindowTitle(self.context.name) # setup connections between notifiers and observers self.setup_enable_notifier() self.setup_disable_notifier() self.setup_load_observers() self.setup_gui_variable_observers() self.setup_group_calculation_enable_notifier() self.setup_group_calculation_disable_notifier() self.setup_grouping_changed_observers() self.setup_update_view_notifier() self.setMinimumHeight(800)
class MuonLoadDataTest(unittest.TestCase): def setUp(self): self.muon_load_data = MuonLoadData() self.workspace = mock.MagicMock() self.workspace_last = mock.MagicMock() self.muon_load_data.add_data(run=1, workspace=mock.MagicMock(), filename='path to file') self.muon_load_data.add_data(run=2, workspace=self.workspace, filename='path to file') self.muon_load_data.add_data(run=3, workspace=mock.MagicMock(), filename='matching path') self.muon_load_data.add_data() self.muon_load_data.add_data(run=4, workspace=self.workspace_last, filename='path to file') def assert_empty(self, load_data): self.assertEqual(load_data.get_parameter("run"), []) self.assertEqual(load_data.get_parameter("workspace"), []) self.assertEqual(load_data.get_parameter("filename"), []) self.assertEqual(load_data.num_items(), 0) def assert_unchanged(self, load_data): self.assertEqual(load_data.get_parameter("run"), [1234, 1235, 1236]) self.assertEqual(load_data.get_parameter("workspace"), [[1], [2], [3]]) self.assertEqual(load_data.get_parameter("filename"), [ "C:\\dir1\\file1.nxs", "C:\\dir1\\file2.nxs", "C:\\dir1\\file3.nxs" ]) self.assertEqual(load_data.num_items(), 3) def populate_loaded_data(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs", instrument='EMU') data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs", instrument='EMU') data.add_data(run=1236, workspace=[3], filename="C:\\dir1\\file3.nxs", instrument='EMU') return data # ------------------------------------------------------------------------------------------------------------------ # TESTS # ------------------------------------------------------------------------------------------------------------------ def test_load_data_initialized_as_empty(self): data = MuonLoadData() self.assert_empty(data) def test_data_can_be_added_correctly_via_keyword_args(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") self.assertEqual(data.num_items(), 1) self.assertEqual(data.contains_n(run=1234), 1) self.assertEqual(data.contains_n(workspace=[1]), 1) self.assertEqual(data.contains_n(filename="C:\\dir1\\file1.nxs"), 1) def test_that_clear_empties_the_data_of_all_entries(self): data = self.populate_loaded_data() self.assertEqual(data.num_items(), 3) data.clear() self.assert_empty(data) def test_that_adding_then_removing_single_item_leaves_data_empty(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") self.assertEqual(data.num_items(), 1) data.remove_data(run=1234) self.assert_empty(data) data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.remove_data(workspace=[1]) self.assert_empty(data) data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.remove_data(filename="C:\\dir1\\file1.nxs") self.assert_empty(data) def test_that_remove_method_can_remove_several_entries_at_once(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1234, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertEqual(data.num_items(), 3) data.remove_data(run=1234) self.assert_empty(data) def test_that_remove_method_applies_AND_to_multiple_keyword_arguments( self): data = self.populate_loaded_data() self.assertEqual(data.num_items(), 3) data.remove_data(run=1234, workspace=[2], filename="C:\\dir1\\file3.nxs") self.assert_unchanged(data) def test_that_contains_is_true_when_data_contains_entry_which_matches_to_a_single_keyword_and_false_otherwise( self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertTrue(data.contains(run=1234)) self.assertTrue(data.contains(filename="C:\\dir1\\file1.nxs")) self.assertFalse(data.contains(run=9999)) self.assertFalse(data.contains(filename="C:\\dir1\\file4.nxs")) def test_that_contains_is_false_when_data_contains_entries_which_match_to_only_one_of_multiple_keywords( self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") # values from keywords correspond to different entries self.assertFalse( data.contains(run=1234, filename="C:\\dir1\\file2.nxs")) self.assertTrue(data.contains(run=1234, workspace=[3])) def test_counting_entries_with_keyword_argument_gives_correct_count(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") self.assertEqual(data.contains_n(run=1234), 2) self.assertEqual(data.contains_n(filename="C:\\dir1\\file1.nxs"), 1) self.assertEqual(data.contains_n(run=9999), 0) def test_counting_entries_applies_AND_behaviour_to_keyword_arguments(self): data = MuonLoadData() data.add_data(run=1234, workspace=[1], filename="C:\\dir1\\file1.nxs") data.add_data(run=1235, workspace=[2], filename="C:\\dir1\\file2.nxs") data.add_data(run=1234, workspace=[3], filename="C:\\dir1\\file3.nxs") data.add_data(run=1236, workspace=[4], filename="C:\\dir1\\file4.nxs") self.assertEqual(data.contains_n(run=1234, workspace=[1]), 1) self.assertEqual(data.contains_n(run=1234, workspace=[2]), 0) self.assertEqual( data.contains_n(run=1234, workspace=[2], filename="C:\\dir1\\file4.nxs"), 0) def test_iterator_behaviour_of_data(self): data = self.populate_loaded_data() check = [{ "run": 1234, "workspace": [1], "filename": "C:\\dir1\\file1.nxs", 'instrument': 'EMU' }, { "run": 1235, "workspace": [2], "filename": "C:\\dir1\\file2.nxs", 'instrument': 'EMU' }, { "run": 1236, "workspace": [3], "filename": "C:\\dir1\\file3.nxs", 'instrument': 'EMU' }] for data_item in iter(data): self.assertEqual(data_item, check.pop(0)) def test_that_remove_current_data_removes_the_most_recently_added_data( self): data = self.populate_loaded_data() data.remove_current_data() check = [{ "run": 1234, "workspace": [1], "filename": "C:\\dir1\\file1.nxs", 'instrument': 'EMU' }, { "run": 1235, "workspace": [2], "filename": "C:\\dir1\\file2.nxs", 'instrument': 'EMU' }] for data_item in iter(data): self.assertEqual(data_item, check.pop(0)) def test_that_remove_last_added_data_removes_the_previous_data_item_before_the_most_recent( self): data = self.populate_loaded_data() data.remove_last_added_data() check = [{ "run": 1234, "workspace": [1], "filename": "C:\\dir1\\file1.nxs", 'instrument': 'EMU' }, { "run": 1236, "workspace": [3], "filename": "C:\\dir1\\file3.nxs", 'instrument': 'EMU' }] for data_item in iter(data): self.assertEqual(data_item, check.pop(0)) def test_that_can_add_data_to_struct(self): self.assertEqual(self.muon_load_data.num_items(), 5) self.assertEqual(self.muon_load_data.get_parameter('run')[2], 3) self.assertEqual( self.muon_load_data.get_parameter('workspace')[1], self.workspace) self.assertEqual( self.muon_load_data.get_parameter('filename')[0], 'path to file') def test_that_matches_returns_false_for_all_entries_with_only_one_match( self): match_list = self.muon_load_data._matches(run=1, workspace=self.workspace, filename='matching path') self.assertEqual(match_list, [False, False, False, False, False]) def test_that_matches_with_no_params_matches_all(self): match_list = self.muon_load_data._matches() self.assertEqual(match_list, [True, True, True, True, True]) def test_that_matches_with_unused_parameters_match_none(self): match_list = self.muon_load_data._matches(new_info='new info') self.assertEqual(match_list, [False, False, False, False, False]) def test_that_matches_correctly_with_only_one_parameter_given(self): match_list = self.muon_load_data._matches(filename='path to file') self.assertEqual(match_list, [True, True, False, False, True]) def test_that_get_data_returns_correct_dict(self): data_dict = self.muon_load_data.get_data(run=2) self.assertEqual( data_dict, { 'workspace': self.workspace, 'filename': 'path to file', 'run': 2, 'instrument': '' }) def test_that_get_latest_data_returns_correct_dict(self): data_dict = self.muon_load_data.get_latest_data() self.assertEqual( data_dict, { 'workspace': self.workspace_last, 'filename': 'path to file', 'run': 4, 'instrument': '' })
def setUp(self): self.context = EAGroupContext() self.loadedData = MuonLoadData()
class MuonDataContextTest(unittest.TestCase): def setUp(self): self.filepath = FileFinder.findRuns('EMU00019489.nxs')[0] self.load_result, self.run_number, self.filename, _ = load_workspace_from_filename( self.filepath) self.loaded_data = MuonLoadData() self.context = MuonDataContext(load_data=self.loaded_data) self.context.instrument = 'EMU' self.loaded_data.add_data(workspace=self.load_result, run=[self.run_number], filename=self.filename, instrument='EMU') self.context.current_runs = [[self.run_number]] self.context.update_current_data() def tearDown(self): AnalysisDataService.clear() def test_setting_current_data_with_a_different_field_sends_message_signal( self): self.context.current_data['MainFieldDirection'] = 'transverse' self.context.message_notifier.notify_subscribers = mock.MagicMock() self.context.update_current_data() self.context.message_notifier.notify_subscribers.assert_called_once_with( 'MainFieldDirection has changed between' ' data sets, click default to reset grouping if required') self.context.current_data['MainFieldDirection'] = 'longitudinal' def test_that_setting_current_runs_with_mixture_of_transverse_and_longitudanal_runs_raises_warning( self): loaded_data = copy.copy(self.context.current_data) self.context.message_notifier.notify_subscribers = mock.MagicMock() loaded_data['MainFieldDirection'] = 'transverse' self.loaded_data.add_data(workspace=loaded_data, run=[1], filename='filename', instrument='EMU') self.context.current_runs = [[19489], [1]] self.context.message_notifier.notify_subscribers.assert_called_once_with( 'MainFieldDirection changes within current run set:\ntransverse field runs 1\nlongitudinal field runs 19489\n' ) def test_is_data_loaded_returns_true_if_data_loaded(self): self.assertTrue(self.context.is_data_loaded()) def test_is_data_loaded_returns_false_if_no_data_loaded(self): self.context._loaded_data.clear() self.assertFalse(self.context.is_data_loaded()) def test_current_filenames_returns_path_to_current_file(self): self.assertEqual(self.context.current_filenames, [self.filepath]) def test_current_runs_set_correctly(self): self.assertEqual(self.context.current_runs, [[19489]]) def test_current_workspaces_returns_correctly(self): self.assertEqual(self.context.current_workspaces, [self.load_result]) def test_get_loaded_data_for_run_returns_correctly(self): self.assertEqual(self.context.get_loaded_data_for_run([19489]), self.load_result) def test_num_detectors_returns_correctly(self): self.assertEqual(self.context.num_detectors, 96) def test_num_periods_returns_correctly(self): self.assertEqual(self.context.num_periods([19489]), 1) def test_main_field_direction_returns_correctly(self): self.assertEqual(self.context.main_field_direction, 'Longitudinal') def test_dead_time_table_returns_correctly(self): self.assertEqual(self.context.dead_time_table, self.load_result['DeadTimeTable']) def test_return_sample_log_returns_correctly(self): self.assertEqual(self.context.get_sample_log('goodfrm').value, 31369.0) def test_that_is_multi_period_returns_false_for_single_period_data(self): is_multi_period = self.context.is_multi_period() self.assertTrue(not is_multi_period) def test_that_any_multi_period_data_will_mark_everything_as_multiperiod( self): multi_period_worspace_list = [ MuonWorkspaceWrapper(f'raw_data_{period_index + 1}') for period_index in range(4) ] load_result = {'OutputWorkspace': multi_period_worspace_list} self.loaded_data.add_data(workspace=load_result, run=[84447], filename='workspace_filename', instrument='EMU') self.context._current_runs = [[84447], [19489]] is_multi_period = self.context.is_multi_period() self.assertTrue(is_multi_period)
def __init__(self, loaded_data_store=MuonLoadData(), context=None): self._loaded_data_store = loaded_data_store self._data_context = context.data_context self._context = context
def setUp(self): self.muon_data = MuonLoadData() self.corrections_context = CorrectionsContext(self.muon_data)