Ejemplo n.º 1
0
 def add_two_pair_diffs(self):
     self.add_two_groups()
     self.add_two_pairs()
     diff0 = MuonDiff('pair_diff_0', 'pair_0', 'pair_1', group_or_pair='pair')
     diff1 = MuonDiff('pair_diff_1', 'pair_1', 'pair_0', group_or_pair='pair')
     self.presenter.pair_widget.add_diff(diff0)
     self.presenter.pair_widget.add_diff(diff1)
Ejemplo n.º 2
0
    def test_cannot_add_diff_with_duplicate_name(self):
        diff_1 = MuonDiff('diff', 'positive', 'negative')
        diff_2 = MuonDiff('diff', 'positive', 'negative')

        self.context.add_diff(diff_1)

        self.assertRaises(ValueError, self.context.add_diff, diff_2)
Ejemplo n.º 3
0
 def add_two_group_diffs(self):
     if not self.model.group_names:
         self.add_two_groups()
     diff0 = MuonDiff('group_diff_0', 'group_0', 'group_1')
     diff1 = MuonDiff('group_diff_1', 'group_1', 'group_0')
     self.presenter.group_widget.add_diff(diff0)
     self.presenter.group_widget.add_diff(diff1)
Ejemplo n.º 4
0
    def test_can_remove_diffs_as_expected(self):
        diff_1 = MuonDiff('diff_1', 'positive', 'negative')
        diff_2 = MuonDiff('diff_2', 'positive', 'negative')
        diff_3 = MuonDiff('diff_3', 'positive', 'negative')
        self.context.add_diff(diff_1)
        self.context.add_diff(diff_2)
        self.context.add_diff(diff_3)

        self.context.remove_diff('diff_2')

        self.assertEqual(self.context.diff_names, ['diff_1', 'diff_3'])
Ejemplo n.º 5
0
    def test_diff_names_returns_ordered_list_of_names(self):
        diff_1 = MuonDiff('diff_1', 'positive', 'negative')
        diff_2 = MuonDiff('diff_2', 'positive', 'negative')
        diff_3 = MuonDiff('diff_3', 'positive', 'negative')

        self.context.add_diff(diff_1)
        self.context.add_diff(diff_2)
        self.context.add_diff(diff_3)

        self.assertEqual(self.context.diff_names,
                         ['diff_1', 'diff_2', 'diff_3'])
Ejemplo n.º 6
0
 def add_two_pair_diffs(self):
     if not self.model.group_names:
         self.add_two_groups()
     if not self.model.pair_names:
         self.add_two_pairs()
     diff0 = MuonDiff('pair_diff_0',
                      'pair_0',
                      'pair_1',
                      group_or_pair='pair')
     diff1 = MuonDiff('pair_diff_1',
                      'pair_1',
                      'pair_0',
                      group_or_pair='pair')
     self.presenter.pair_widget.add_diff(diff0)
     self.presenter.pair_widget.add_diff(diff1)
 def handle_add_diff_button_clicked(self, group_1='', group_2=''):
     if len(self._model.get_names(self._group_or_pair)) == 0 or len(
             self._model.get_names(self._group_or_pair)) == 1:
         self._view.warning_popup(
             "At least two groups/pairs are required to create a diff")
     else:
         new_diff_name = self._view.enter_diff_name()
         if new_diff_name is None:
             return
         elif new_diff_name in self._model.group_and_pair_names:
             self._view.warning_popup(
                 "Groups and diffs must have unique names")
         elif self.validate_diff_name(new_diff_name):
             group1 = self._model.get_names(
                 self._group_or_pair)[0] if not group_1 else group_1
             group2 = self._model.get_names(
                 self._group_or_pair)[1] if not group_2 else group_2
             periods = self._model._context.group_pair_context[
                 group1].periods + self._model._context.group_pair_context[
                     group2].periods
             diff = MuonDiff(diff_name=str(new_diff_name),
                             positive=group1,
                             negative=group2,
                             group_or_pair=self._group_or_pair,
                             periods=periods)
             self.add_diff(diff)
             self.notify_data_changed()
Ejemplo n.º 8
0
    def test_non_pair_cannot_be_added(self):
        group = MuonGroup('group_1', [1, 3, 5, 7, 9])
        diff = MuonDiff('diff_1', 'positive', 'negative')

        # Value error as cannot assert isinstance in code since could add MuonPair or MuonBasePair
        self.assertRaises(ValueError, self.context.add_pair, group)
        self.assertRaises(ValueError, self.context.add_pair, diff)
Ejemplo n.º 9
0
    def test_calculate_group_diff_calculates_diff_for_given_run(self):
        self._calculate_data_for(self.run_list, self.groups, self.rebins)

        diff = MuonDiff('diff', 'fwd', 'bwd')
        diff_asymmetry = self.context.calculate_diff(diff, self.run_list, False)

        self.assertEqual(diff_asymmetry, 'EMU19489; Diff; diff; Asymmetry; MA')
    def reset_group_and_pairs_to_default(self, workspace, instrument,
                                         main_field_direction, num_periods):
        default_groups, default_pairs, default_diffs, default_selected = get_default_grouping(
            workspace, instrument, main_field_direction)
        if num_periods == 1:
            self._groups = default_groups
            self._diffs = default_diffs
            self._pairs = default_pairs
            self._selected = default_selected
        else:
            periods = range(num_periods + 1)[1:]
            self._groups = []
            self._diffs = []
            self._pairs = []
            for period in periods:
                for group in default_groups:
                    self._groups.append(
                        MuonGroup(group.name + str(period), group.detectors,
                                  [period]))

            for period in periods:
                for pair in default_pairs:
                    self._pairs.append(
                        MuonPair(pair.name + str(period),
                                 pair.forward_group + str(period),
                                 pair.backward_group + str(period), pair.alpha,
                                 [period]))

            if default_diffs:
                for diff in default_diffs:
                    self._diffs.append(
                        MuonDiff(diff.name, diff.forward, diff.backward,
                                 diff.group_or_pair, diff.periods))
            else:
                for index in range(0, floor(len(periods) / 2.)):
                    for pair in default_pairs:
                        odd_period = index * 2 + 1
                        even_period = odd_period + 1
                        self._diffs.append(
                            MuonDiff("pair_diff" + str(index + 1),
                                     pair.name + str(odd_period),
                                     pair.name + str(even_period),
                                     group_or_pair="pair",
                                     periods=[odd_period, even_period]))

            self._selected = self.pair_names[0]
Ejemplo n.º 11
0
def load_grouping_from_XML(filename):
    """
    Load group/pair data from an XML file (which can be produced using the save_grouping_to_XML() function

    :param filename: Full filepath to an xml file.
    :return: (groups, pairs), lists of MuonGroup, MuonPair objects respectively.
    """
    tree = ET.parse(filename)
    root = tree.getroot()
    description = root.get('description')
    if not description:
        description = filename
    try:
        default = root.find('default').get('name')
    except (AttributeError, KeyError):
        default = ''

    group_names, group_ids, periods = _get_groups_from_XML(root)
    pair_names, pair_groups, pair_alphas, pair_periods = _get_pairs_from_XML(
        root)
    diff_names, diff_groups, diff_periods = _get_diffs_from_XML(root)
    groups, diffs, pairs = [], [], []

    for i, group_name in enumerate(group_names):
        period = periods[i] if periods and i < len(periods) else [1]
        groups += [
            MuonGroup(group_name=group_name,
                      detector_ids=group_ids[i],
                      periods=period)
        ]

    for i, pair_name in enumerate(pair_names):
        if pair_periods:
            pair_periods_converted = _convert_periods_to_int(pair_periods[i])
        else:
            pair_periods_converted = [1]

        pairs += [
            MuonPair(pair_name=pair_name,
                     forward_group_name=pair_groups[i][0],
                     backward_group_name=pair_groups[i][1],
                     alpha=pair_alphas[i],
                     periods=pair_periods_converted)
        ]

    for i, diff_name in enumerate(diff_names):
        if diff_periods:
            diff_periods_converted = _convert_periods_to_int(diff_periods[i])
        else:
            diff_periods_converted = [1]
        diffs += [
            MuonDiff(diff_name, diff_groups[i][0], diff_groups[i][1],
                     diff_groups[i][2], diff_periods_converted)
        ]

    return groups, pairs, diffs, description, default
Ejemplo n.º 12
0
    def test_that_calculate_diffs_for_will_calculate_the_expected_diff_asymmetry_workspaces(
            self):
        diff = MuonDiff('group_diff', 'fwd', 'bwd')
        self.context.group_pair_context.add_diff(diff)

        self.context.calculate_all_counts()
        self.model.calculate_asymmetry_workspaces_for(
            [f"{self.run_number}", f"{self.run_number}"], ["fwd", "bwd"])

        self.model.calculate_diffs_for([f"{self.run_number}"], ["bwd"])

        self._assert_workspaces_exist(
            ["EMU19489; Diff; group_diff; Asymmetry; MA"])
 def update_model_from_view(self, table=None):
     if not table:
         table = self._view.get_table_contents()
     self._model.clear_diffs(self._group_or_pair)
     for entry in table:
         periods = self._model._context.group_pair_context[
             entry[2]].periods + self._model._context.group_pair_context[
                 entry[3]].periods
         diff = MuonDiff(diff_name=str(entry[0]),
                         positive=str(entry[2]),
                         negative=str(entry[3]),
                         group_or_pair=self._group_or_pair,
                         periods=periods)
         self._model.add_diff(diff)
Ejemplo n.º 14
0
    def test_groups_pairs_diffs_should_all_have_unique_names(self):
        group_1 = MuonGroup('group', [1, 3, 5, 7, 9])
        group_2 = MuonGroup('pair', [1, 3, 5, 7, 9])
        group_3 = MuonGroup('diff', [1, 3, 5, 7, 9])
        pair_1 = MuonPair('pair')
        pair_2 = MuonPair('group')
        pair_3 = MuonPair('diff')
        diff_1 = MuonDiff('diff', 'positive', 'negative')
        diff_2 = MuonDiff('group', 'positive', 'negative')
        diff_3 = MuonDiff('pair', 'positive', 'negative')

        # Add correct group, pair and diff
        self.context.add_group(group_1)
        self.context.add_pair(pair_1)
        self.context.add_diff(diff_1)

        # Now check cannot duplicate names
        self.assertRaises(ValueError, self.context.add_group, group_2)
        self.assertRaises(ValueError, self.context.add_group, group_3)
        self.assertRaises(ValueError, self.context.add_pair, pair_2)
        self.assertRaises(ValueError, self.context.add_pair, pair_3)
        self.assertRaises(ValueError, self.context.add_diff, diff_2)
        self.assertRaises(ValueError, self.context.add_diff, diff_3)
Ejemplo n.º 15
0
 def test_that_cannot_initialize_MuonDiff_without_negative(self):
     with self.assertRaises(TypeError):
         MuonDiff(diff_name="diff", positive="positive")
Ejemplo n.º 16
0
    def test_is_group_diff_by_default(self):
        diff = MuonDiff("diff1", "positive", "negative")

        self.assertEqual("group", diff.group_or_pair)
Ejemplo n.º 17
0
    def test_can_create_pair_diff(self):
        diff = MuonDiff("diff1", "positive", "negative", "pair")

        self.assertEqual("pair", diff.group_or_pair)
Ejemplo n.º 18
0
    def test_calculate_group_diff_returns_nothing_if_relevant_groups_do_not_exist(self):
        self.context.calculate_all_counts()
        diff = MuonDiff('diff', 'fwd', 'bwd')
        diff_asymmetry = self.context.calculate_diff(diff, [19489], True)

        self.assertEqual(diff_asymmetry, None)
Ejemplo n.º 19
0
 def add_group_diff(self):
     diff = MuonDiff('group_diff', 'fwd', 'bwd')
     self.group_pair_context.add_diff(diff)
     return diff
Ejemplo n.º 20
0
    def test_diff_can_be_added(self):
        diff = MuonDiff('diff_1', 'positive', 'negative')

        self.context.add_diff(diff)

        self.assertEqual(self.context['diff_1'], diff)
Ejemplo n.º 21
0
 def add_pair_diff(self):
     long2 = MuonPair('long2', 'bwd', 'fwd')
     self.group_pair_context.add_pair(long2)
     diff = MuonDiff('pair_diff', 'long', 'long2', 'pair')
     self.group_pair_context.add_diff(diff)
     return diff
Ejemplo n.º 22
0
    def test_non_group_cannot_be_added(self):
        pair = MuonPair('pair_1')
        diff = MuonDiff('diff_1', 'positive', 'negative')

        self.assertRaises(AssertionError, self.context.add_group, pair)
        self.assertRaises(AssertionError, self.context.add_group, diff)
Ejemplo n.º 23
0
    def test_that_can_get_positive_and_negative(self):
        diff = MuonDiff("diff1", "positive", "negative")

        self.assertEqual("positive", diff.positive)
        self.assertEqual("negative", diff.negative)