Exemple #1
0
class MuonDataContextTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super(MuonDataContextTest, cls).setUpClass()
        cls.filepath = FileFinder.findRuns('EMU00019489.nxs')[0]
        cls.load_result, cls.run_number, cls.filename, _ = load_workspace_from_filename(
            cls.filepath)

    def setUp(self):
        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)
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 populate_ADS(self):
        self.context.calculate_all_groups()
        self.context.show_all_groups()
        self.context.calculate_all_pairs()
        self.context.show_all_pairs()
        workspace = CreateWorkspace([0], [0], StoreInADS=False)
        self.context.phase_context.add_phase_quad(
            MuonWorkspaceWrapper(workspace,
                                 'EMU19489; PhaseQuad; PhaseTable EMU19489'))

    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')

    def test_get_workspace_names_returns_all_stored_workspaces_if_all_selected(
            self):
        self.populate_ADS()
        workspace_list = self.context.get_names_of_workspaces_to_fit(
            '19489', 'fwd, bwd, long', True)

        self.assertEqual(workspace_list, [
            'EMU19489; Group; fwd; Asymmetry; #1',
            'EMU19489; Group; bwd; Asymmetry; #1',
            'EMU19489; Pair Asym; long; #1',
            'EMU19489; PhaseQuad; PhaseTable EMU19489'
        ])

    def test_get_workspace_names_returns_nothing_if_no_parameters_passed(self):
        self.populate_ADS()
        workspace_list = self.context.get_names_of_workspaces_to_fit()

        self.assertEqual(workspace_list, [])

    def test_get_workspaces_names_copes_with_bad_groups(self):
        self.populate_ADS()
        workspace_list = self.context.get_names_of_workspaces_to_fit(
            '19489', 'fwd, bwd, long, random, wrong', True)

        self.assertEqual(workspace_list, [
            'EMU19489; Group; fwd; Asymmetry; #1',
            'EMU19489; Group; bwd; Asymmetry; #1',
            'EMU19489; Pair Asym; long; #1',
            'EMU19489; PhaseQuad; PhaseTable EMU19489'
        ])

    def test_get_workspaces_names_copes_with_non_existent_runs(self):
        self.populate_ADS()

        workspace_list = self.context.get_names_of_workspaces_to_fit(
            '19489, 22222', 'fwd, bwd, long', True)

        self.assertEqual(workspace_list, [
            'EMU19489; Group; fwd; Asymmetry; #1',
            'EMU19489; Group; bwd; Asymmetry; #1',
            'EMU19489; Pair Asym; long; #1',
            'EMU19489; PhaseQuad; PhaseTable EMU19489'
        ])

    def test_that_run_ranged_correctly_parsed(self):
        self.populate_ADS()

        workspace_list = self.context.get_names_of_workspaces_to_fit(
            '19489-95', 'fwd, bwd, long', True)

        self.assertEqual(workspace_list, [
            'EMU19489; Group; fwd; Asymmetry; #1',
            'EMU19489; Group; bwd; Asymmetry; #1',
            'EMU19489; Pair Asym; long; #1',
            'EMU19489; PhaseQuad; PhaseTable EMU19489'
        ])
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')