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 setUp(self): AnalysisDataService.clear() 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.data_context = MuonDataContext(self.loaded_data) self.gui_context = MuonGuiContext() self.group_pair_context = MuonGroupPairContext() self.gui_context.update({'RebinType': 'None'}) self.context = MuonContext(muon_data_context=self.data_context, muon_gui_context=self.gui_context, muon_group_context=self.group_pair_context) self.data_context.instrument = 'EMU' self.loaded_data.add_data(workspace=self.load_result, run=[self.run_number], filename=self.filename, instrument='EMU') self.data_context.current_runs = [[self.run_number]] self.data_context.update_current_data() self.group_pair_context.reset_group_and_pairs_to_default( self.load_result['OutputWorkspace'][0]._workspace, 'EMU', '')
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) phase_table_context = PhaseTableContext() fitting_context = FittingContext() freq_context = FrequencyContext() 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, fitting_context=fitting_context, frequency_context=freq_context) else: return DataAnalysisContext(muon_data_context=data_context, muon_group_context=group_context, muon_gui_context=gui_context, muon_phase_context=phase_table_context, fitting_context=fitting_context)
def setUp(self): self.loaded_data = MuonLoadData() self.context = MuonDataContext(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 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 setup_context(): loaded_data = MuonLoadData() loaded_data.get_main_field_direction = mock.MagicMock(return_value='transverse') data_context = MuonDataContext(loaded_data) gui_context = MuonGuiContext() group_context = MuonGroupPairContext(data_context.check_group_contains_valid_detectors) phase_table_context = PhaseTableContext() return MuonContext(muon_data_context=data_context, muon_group_context=group_context, muon_gui_context=gui_context, muon_phase_context=phase_table_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 setup_context(): loaded_data = MuonLoadData() loaded_data.get_main_field_direction = mock.MagicMock( return_value='transverse') data_context = MuonDataContext(loaded_data) gui_context = MuonGuiContext() group_context = MuonGroupPairContext( data_context.check_group_contains_valid_detectors) phase_table_context = PhaseTableContext() return MuonContext(muon_data_context=data_context, muon_group_context=group_context, muon_gui_context=gui_context, muon_phase_context=phase_table_context)
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 setUp(self): self.loaded_data = MuonLoadData() self.context = MuonDataContext(self.loaded_data) self.context.instrument = 'EMU' filepath = FileFinder.findRuns('EMU00019489.nxs')[0] load_result, run, filename = load_workspace_from_filename(filepath) self.loaded_data.add_data(workspace=load_result, run=[run], filename=filename, instrument='EMU') self.context.current_runs = [[run]] self.context.update_current_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 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._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() ConfigService['default.instrument'] = 'MUSR' self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'MUSR' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.view = LoadWidgetView(parent=self.obj, load_file_view=self.load_file_view, load_run_view=self.load_run_view) self.presenter = LoadWidgetPresenter( self.view, LoadWidgetModel(self.data, self.context)) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.view.multiple_loading_check.setCheckState(1) self.presenter.handle_multiple_files_option_changed() self.runs = [15196, 15197] self.workspaces = [self.create_fake_workspace(1) for _ in self.runs] self.filenames = FileFinder.findRuns( 'MUSR00015196.nxs, MUSR00015197.nxs')
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonLoadData() self.view = LoadRunWidgetView(parent=self.obj) self.model = LoadRunWidgetModel(self.data) self.presenter = LoadRunWidgetPresenter(self.view, self.model) self.model.load_workspace_from_filename = mock.Mock( return_value=([1, 2, 3], "currentRun.nxs", 1234)) self.view.warning_popup = mock.Mock() self.view.disable_load_buttons = mock.Mock() self.view.enable_load_buttons = mock.Mock() self.presenter.set_current_instrument("EMU") fileUtils.get_current_run_filename = mock.Mock( return_value="EMU0001234.nxs") patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start() self.load_utils_patcher.exception_message_for_failed_files.return_value = ''
def setUp(self): self._qapp = mock_widget.mockQapp() self.view = BrowseFileWidgetView() self.view.on_browse_clicked = mock.Mock() self.view.set_file_edit = mock.Mock() self.view.reset_edit_to_cached_value = mock.Mock() self.view.show_file_browser_and_return_selection = mock.Mock( return_value=["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"]) self.data = MuonLoadData() self.context = MuonDataContext self.context.instrument = 'EMU' self.model = BrowseFileWidgetModel(self.data, self.context) self.model.exception_message_for_failed_files = mock.Mock() self.view.disable_load_buttons = mock.Mock() self.view.enable_load_buttons = mock.Mock() self.view.warning_popup = mock.Mock() self.presenter = BrowseFileWidgetPresenter(self.view, self.model) patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start()
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(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.popup_patcher = mock.patch('Muon.GUI.Common.thread_model.warning') self.addCleanup(self.popup_patcher.stop) self.popup_mock = self.popup_patcher.start() self.load_patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_patcher.stop) self.load_mock = self.load_patcher.start() self.load_run_patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_run_patcher.stop) self.load_run_mock = self.load_run_patcher.start() self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'EMU' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.model = LoadWidgetModel(self.data, self.context) self.view = LoadWidgetView(parent=self.obj, load_run_view=self.load_run_view, load_file_view=self.load_file_view) self.presenter = LoadWidgetPresenter(view=self.view, model=self.model) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.presenter.load_run_widget.set_current_instrument('EMU') self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.load_file_view.show_file_browser_and_return_selection = mock.Mock( return_value=["C:\\dir1\\EMU0001234.nxs"]) self.workspace_mock = self.create_fake_workspace(1) self.load_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs") self.load_run_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs") self.presenter.load_file_widget.on_browse_button_clicked() self.wait_for_thread(self.presenter.load_file_widget._load_thread) self.mock_loading_to_throw() file_utils.get_current_run_filename = mock.Mock( return_value="EMU0001234.nxs")
def setUp(self): self.data = MuonLoadData() self.model = BrowseFileWidgetModel(self.data) patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start()
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 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.phase_table_context = PhaseTableContext() parent_object.fitting_context = FittingContext() parent_object.context = MuonContext(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, fitting_context=parent_object.fitting_context)
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 test_that_contains_is_true_when_data_contains_entries_which_match_to_at_least_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.assertTrue(data.contains(run=1234, filename="C:\\dir1\\file2.nxs")) self.assertTrue(data.contains(run=1234, workspace=[3]))
def __init__(self, parent=None): super(MuonAnalysisGui, self).__init__(parent) 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.context = MuonDataContext(load_data=self.loaded_data) # construct all the widgets. self.load_widget = LoadWidget(self.loaded_data, self.context, self) self.grouping_tab_widget = GroupingTabWidget(self.context) self.home_tab = HomeTabWidget(self.context, self) self.setup_tabs() self.help_widget = HelpWidget() central_widget = QtGui.QWidget() vertical_layout = QtGui.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.setCentralWidget(central_widget) self.setWindowTitle("Muon Analysis version 2") self.home_tab.group_widget.pairAlphaNotifier.add_subscriber( self.grouping_tab_widget.group_tab_presenter.loadObserver) self.grouping_tab_widget.group_tab_presenter.groupingNotifier.add_subscriber( self.home_tab.home_tab_widget.groupingObserver) self.context.instrumentNotifier.add_subscriber( self.home_tab.home_tab_widget.instrumentObserver) self.context.instrumentNotifier.add_subscriber( self.load_widget.load_widget.instrumentObserver) self.context.instrumentNotifier.add_subscriber( self.grouping_tab_widget.group_tab_presenter.instrumentObserver) 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)
def __init__(self, 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._groups = OrderedDict() self._pairs = OrderedDict() self._loaded_data = load_data self._current_data = { "workspace": load_utils.empty_loaded_data() } # self.get_result(False)
def test_counting_entries_applies_OR_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]), 2) self.assertEqual(data.contains_n(run=1234, workspace=[2]), 3) self.assertEqual(data.contains_n(run=1234, workspace=[2], filename="C:\\dir1\\file4.nxs"), 4)
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 setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'EMU' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.presenter = LoadWidgetPresenter( LoadWidgetView(parent=self.obj, load_file_view=self.load_file_view, load_run_view=self.load_run_view), LoadWidgetModel(self.data, self.context)) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.filepath = FileFinder.findRuns('MUSR00022725.nxs')[0] self.load_patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_patcher.stop) self.load_mock = self.load_patcher.start() self.load_run_patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_run_patcher.stop) self.load_run_mock = self.load_run_patcher.start() self.mock_workspace = self.create_fake_workspace(1) self.mock_loading_from_browse(self.mock_workspace, "C:\dir1\dir2\dir3\EMU0001234.nxs", 1234) file_utils.get_current_run_filename = mock.Mock( return_value="C:\dir1\dir2\dir3\EMU0001234.nxs") self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.popup_patcher = mock.patch('Muon.GUI.Common.thread_model.warning') self.addCleanup(self.popup_patcher.stop) self.popup_mock = self.popup_patcher.start()
def test_model_is_cleared_correctly(self, load_utils_mock): files = [r'EMU00019489.nxs', r'EMU00019490.nxs', r'EMU00019491.nxs'] load_return_vals = [([1, 2, 3], filename, 19489 + i) for i, filename in enumerate(files)] model = LoadRunWidgetModel(MuonLoadData()) load_utils_mock.load_workspace_from_filename = mock.Mock() load_utils_mock.load_workspace_from_filename.side_effect = load_return_vals model.loadData(files) model.execute() self.assertEqual(len(model.loaded_runs), 3) model.clear_loaded_data() self.assertEqual(model.loaded_workspaces, []) self.assertEqual(model.loaded_filenames, []) self.assertEqual(model.loaded_runs, [])
def setUp(self): self._qapp = mock_widget.mockQapp() self.data = MuonLoadData() self.context = MuonDataContext self.context.instrument = 'EMU' self.view = BrowseFileWidgetView() self.model = BrowseFileWidgetModel(self.data, self.context) self.view.disable_load_buttons = mock.Mock() self.view.enable_load_buttons = mock.Mock() self.view.warning_popup = mock.Mock() self.presenter = BrowseFileWidgetPresenter(self.view, self.model) patcher = mock.patch('Muon.GUI.Common.load_file_widget.model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start()
def setUp(self): self._qapp = mock_widget.mockQapp() self.data = MuonLoadData() self.view = BrowseFileWidgetView() self.model = BrowseFileWidgetModel(self.data) self.view.disable_load_buttons = mock.Mock() self.view.enable_load_buttons = mock.Mock() self.view.warning_popup = mock.Mock() self.presenter = BrowseFileWidgetPresenter(self.view, self.model) self.presenter.enable_multiple_files(True) patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start()
def setUp(self): AnalysisDataService.clear() 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.data_context = MuonDataContext(self.loaded_data) self.gui_context = MuonGuiContext() self.group_pair_context = MuonGroupPairContext() self.gui_context.update({'RebinType': 'None'}) self.context = MuonContext(muon_data_context=self.data_context, muon_gui_context=self.gui_context, muon_group_context=self.group_pair_context) self.data_context.instrument = 'EMU' self.loaded_data.add_data(workspace=self.load_result, run=[self.run_number], filename=self.filename, instrument='EMU') self.data_context.current_runs = [[self.run_number]] self.data_context.update_current_data() self.group_pair_context.reset_group_and_pairs_to_default(self.load_result['OutputWorkspace'][0]._workspace, 'EMU', '')
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 populate_loaded_data(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=1236, workspace=[3], filename="C:\\dir1\\file3.nxs") 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_OR_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_empty(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_true_when_data_contains_entries_which_match_to_at_least_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.assertTrue(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_OR_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]), 2) self.assertEqual(data.contains_n(run=1234, workspace=[2]), 3) self.assertEqual(data.contains_n(run=1234, workspace=[2], filename="C:\\dir1\\file4.nxs"), 4) def test_iterator_behaviour_of_data(self): data = self.populate_loaded_data() check = [{"run": 1234, "workspace": [1], "filename": "C:\\dir1\\file1.nxs"}, {"run": 1235, "workspace": [2], "filename": "C:\\dir1\\file2.nxs"}, {"run": 1236, "workspace": [3], "filename": "C:\\dir1\\file3.nxs"}] 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"}, {"run": 1235, "workspace": [2], "filename": "C:\\dir1\\file2.nxs"}] 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"}, {"run": 1236, "workspace": [3], "filename": "C:\\dir1\\file3.nxs"}] 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_true_for_all_entries_with_one_match(self): match_list = self.muon_load_data._matches(run=1, workspace=self.workspace, filename='matching path') self.assertEqual(match_list, [True, True, True, False, False]) def test_that_matches_with_no_params_matches_none(self): match_list = self.muon_load_data._matches() self.assertEqual(match_list, [False, False, False, False, False]) 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}) 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})
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 populate_loaded_data(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=1236, workspace=[3], filename="C:\\dir1\\file3.nxs") return data
class MuonContextTest(unittest.TestCase): @classmethod def setUpClass(cls): pass def setUp(self): AnalysisDataService.clear() 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.data_context = MuonDataContext(self.loaded_data) self.gui_context = MuonGuiContext() self.group_pair_context = MuonGroupPairContext() self.gui_context.update({'RebinType': 'None'}) self.context = MuonContext(muon_data_context=self.data_context, muon_gui_context=self.gui_context, muon_group_context=self.group_pair_context) self.data_context.instrument = 'EMU' self.loaded_data.add_data(workspace=self.load_result, run=[self.run_number], filename=self.filename, instrument='EMU') self.data_context.current_runs = [[self.run_number]] self.data_context.update_current_data() self.group_pair_context.reset_group_and_pairs_to_default(self.load_result['OutputWorkspace'][0]._workspace, 'EMU', '') def test_reset_groups_and_pairs_to_default(self): self.assertEquals(self.group_pair_context.group_names, ['fwd', 'bwd']) self.assertEquals(self.group_pair_context.pair_names, ['long']) def test_calculate_group_calculates_group_for_given_run(self): counts_workspace, asymmetry_workspace = self.context.calculate_group('fwd', run=[19489]) self.assertEquals(type(counts_workspace), Workspace2D) self.assertEquals(type(counts_workspace), Workspace2D) def test_calculate_pair_calculates_pair_for_given_run(self): pair_asymmetry = self.context.calculate_pair('long', run=[19489]) self.assertEquals(type(pair_asymmetry), Workspace2D) def test_show_all_groups_calculates_and_shows_all_groups(self): self.context.show_all_groups() self.assertEquals(AnalysisDataService.getObjectNames(), ['EMU19489', 'EMU19489 Groups', 'EMU19489; Group; bwd; Asymmetry; #1', 'EMU19489; Group; bwd; Counts; #1', 'EMU19489; Group; fwd; Asymmetry; #1', 'EMU19489; Group; fwd; Counts; #1', 'Muon Data']) def test_that_show_all_calculates_and_shows_all_groups_with_rebin(self): self.gui_context['RebinType'] = 'Fixed' self.gui_context['RebinFixed'] = 2 self.context.show_all_groups() self.assertEquals(AnalysisDataService.getObjectNames(), ['EMU19489', 'EMU19489 Groups', 'EMU19489; Group; bwd; Asymmetry; #1', 'EMU19489; Group; bwd; Asymmetry; Rebin; #1', 'EMU19489; Group; bwd; Counts; #1', 'EMU19489; Group; bwd; Counts; Rebin; #1', 'EMU19489; Group; fwd; Asymmetry; #1', 'EMU19489; Group; fwd; Asymmetry; Rebin; #1', 'EMU19489; Group; fwd; Counts; #1', 'EMU19489; Group; fwd; Counts; Rebin; #1', 'Muon Data']) def test_show_all_pairs_calculates_and_shows_all_pairs(self): self.context.show_all_pairs() self.assertEquals(AnalysisDataService.getObjectNames(), ['EMU19489', 'EMU19489 Pairs', 'EMU19489; Pair Asym; long; #1', 'Muon Data']) def test_that_show_all_calculates_and_shows_all_pairs_with_rebin(self): self.gui_context['RebinType'] = 'Fixed' self.gui_context['RebinFixed'] = 2 self.context.show_all_pairs() self.assertEquals(AnalysisDataService.getObjectNames(), ['EMU19489', 'EMU19489 Pairs', 'EMU19489; Pair Asym; long; #1', 'EMU19489; Pair Asym; long; Rebin; #1', 'Muon Data']) def test_update_current_data_sets_current_run_in_data_context(self): self.context.update_current_data() self.assertEquals(self.data_context.current_data, self.load_result) def test_update_current_data_sets_groups_and_pairs(self): self.context.update_current_data() self.assertEquals(self.group_pair_context.pair_names, ['long']) self.assertEquals(self.group_pair_context.group_names, ['fwd', 'bwd']) def test_show_raw_data_puts_raw_data_into_the_ADS(self): self.context.show_raw_data() self.assertEquals(AnalysisDataService.getObjectNames(), ['EMU19489', 'EMU19489 Raw Data', 'EMU19489_raw_data', 'Muon Data']) def test_that_first_good_data_returns_correctly_when_from_file_chosen_option(self): self.gui_context.update({'FirstGoodDataFromFile': True}) first_good_data = self.context.first_good_data([19489]) self.assertEquals(first_good_data, 0.11) def test_first_good_data_returns_correctly_when_manually_specified_used(self): self.gui_context.update({'FirstGoodDataFromFile': False, 'FirstGoodData': 5}) first_good_data = self.context.first_good_data([19489]) self.assertEquals(first_good_data, 5) def test_that_last_good_data_returns_correctly_when_from_file_chosen_option(self): self.gui_context.update({'LastGoodDataFromFile': True}) last_good_data = self.context.last_good_data([19489]) self.assertEquals(last_good_data, 31.76) def test_last_good_data_returns_correctly_when_manually_specified_used(self): self.gui_context.update({'LastGoodDataFromFile': False, 'LastGoodData': 5}) last_good_data = self.context.last_good_data([19489]) self.assertEquals(last_good_data, 5) def test_that_dead_time_table_from_ADS_returns_table_name(self): self.gui_context.update({'DeadTimeSource': 'FromADS', 'DeadTimeTable': 'deadtime_table_name'}) deadtime_table = self.context.dead_time_table([19489]) self.assertEquals(deadtime_table, 'deadtime_table_name')