Example #1
0
    def test_that_signal_is_not_sent_if_item_updated_to_same_value(self):
        context = MuonGuiContext()
        observer = Observer()
        observer.update = mock.MagicMock()
        context.add_subscriber(observer)
        context.update({'FirstGoodData': 12.0})

        context.update_and_send_signal(FirstGoodData=12.0)

        observer.update.assert_not_called()
    def test_that_signal_is_not_sent_if_item_updated_to_same_value(self):
        context = MuonGuiContext()
        observer = Observer()
        observer.update = mock.MagicMock()
        context.add_subscriber(observer)
        context.update({'FirstGoodData': 12.0})

        context.update_and_send_signal(FirstGoodData=12.0)

        observer.update.assert_not_called()
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')