Пример #1
0
 def test_basic_sum_by_group(self):
     """
     Test grouping and summing on a matrix which is small enough to verify
     the results by hand
     """
     # fmt: off
     group_definition = [(0, "even"), (1, "odd"), (2, "even"), (3, "odd")]
     rows = [
         [1, 2, 3, 4],
         [2, 3, 4, 5],
         [3, 4, 5, 6],
         [4, 5, 6, 7]
     ]
     expected_value = [
         [4, 6, 8, 10],
         [6, 8, 10, 12]
     ]
     # fmt: on
     matrix = numpy.array(rows)
     row_grouper = RowGrouper(group_definition)
     grouped_matrix = row_grouper.sum(matrix)
     value = to_list_of_lists(grouped_matrix)
     self.assertEqual(value, expected_value)
     self.assertEqual(row_grouper.ids, ["even", "odd"])
     self.assertEqual(row_grouper.offsets, {"even": 0, "odd": 1})
Пример #2
0
 def test_empty_group_produces_empty_matrix(self):
     """
     Test the empty group edge case
     """
     group_definition = []
     rows = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
     matrix = numpy.array(rows)
     row_grouper = RowGrouper(group_definition)
     grouped_matrix = row_grouper.sum(matrix)
     value = to_list_of_lists(grouped_matrix)
     self.assertEqual(value, [])
 def test_basic_sum_one_group(self):
     """
     Test summing for a single group on a matrix which is small enough to
     verify the results by hand
     """
     group_definition = [(0, "even"), (1, "odd"), (2, "even"), (3, "odd")]
     rows = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
     matrix = numpy.array(rows)
     row_grouper = RowGrouper(group_definition)
     group_sum = row_grouper.sum_one_group(matrix, "even")
     self.assertEqual(group_sum.tolist(), [4, 6, 8, 10])
 def test_sum_with_all_group_and_matrix_type_combinations(self):
     """
     Tests the `sum` method with every combination of group type and matrix
     type
     """
     test_cases = product(self.get_group_definitions(), self.get_matrices())
     for (group_name, group_definition), (matrix_name, matrix) in test_cases:
         # Use `subTest` when we upgrade to Python 3
         # with self.subTest(matrix=matrix_name, group=group_name):
         row_grouper = RowGrouper(group_definition)
         grouped_matrix = row_grouper.sum(matrix)
         values = to_list_of_lists(grouped_matrix)
         # Calculate the same dict the boring way using pure Python
         expected_values = self.sum_rows_by_group(group_definition, matrix)
         # We need to round floats to account for differences between
         # numpy and Python float rounding
         self.assertEqual(round_floats(values), round_floats(expected_values))
Пример #5
0
 def test_get_group_with_all_group_and_matrix_type_combinations(self):
     """
     Tests the `get_group` method with every combination of group type
     and matrix type
     """
     test_cases = product(self.get_group_definitions(), self.get_matrices())
     for (group_name, group_definition), (matrix_name, matrix) in test_cases:
         with self.subTest(matrix=matrix_name, group=group_name):
             row_grouper = RowGrouper(group_definition)
             for group_id in row_grouper.ids:
                 with self.subTest(group_id=group_id):
                     # Get the group members the boring way using pure
                     # Python
                     expected_value = self.get_group(
                         group_definition, matrix, group_id
                     )
                     value = row_grouper.get_group(matrix, group_id)
                     self.assertEqual(to_list_of_lists(value), expected_value)
 def test_sum_one_group_with_all_group_and_matrix_type_combinations(self):
     """
     Tests the `sum_one_group` method with every combination of group type
     and matrix type
     """
     test_cases = product(self.get_group_definitions(), self.get_matrices())
     for (group_name, group_definition), (matrix_name, matrix) in test_cases:
         # Use `subTest` when we upgrade to Python 3
         # with self.subTest(matrix=matrix_name, group=group_name):
         row_grouper = RowGrouper(group_definition)
         # Calculate sums for all groups the boring way using pure Python
         expected_values = self.sum_rows_by_group(group_definition, matrix)
         # Check the `sum_one_group` gives the expected answer for all groups
         for offset, group_id in enumerate(row_grouper.ids):
             expected_value = expected_values[offset]
             value = row_grouper.sum_one_group(matrix, group_id)
             # We need to round floats to account for differences between
             # numpy and Python float rounding
             self.assertEqual(
                 round_floats(value.tolist()), round_floats(expected_value)
             )
Пример #7
0
 def test_basic_get_group(self):
     """
     Test fetching members for a single group on a matrix which is small
     enough to verify the results by hand
     """
     # fmt: off
     group_definition = [(0, "even"), (1, "odd"), (2, "even"), (3, "odd")]
     rows = [
         [1, 2, 3, 4],
         [2, 3, 4, 5],
         [3, 4, 5, 6],
         [4, 5, 6, 7]
     ]
     expected_value = [
         [1, 2, 3, 4],
         [3, 4, 5, 6]
     ]
     # fmt: on
     matrix = numpy.array(rows)
     row_grouper = RowGrouper(group_definition)
     group = row_grouper.get_group(matrix, "even")
     self.assertEqual(to_list_of_lists(group), expected_value)
 def test_basic_sum_by_group(self):
     """
     Test grouping and summing on a matrix which is small enough to verify
     the results by hand
     """
     group_definition = [(0, 'even'), (1, 'odd'), (2, 'even'), (3, 'odd')]
     rows = [
         [1, 2, 3, 4],
         [2, 3, 4, 5],
         [3, 4, 5, 6],
         [4, 5, 6, 7],
     ]
     matrix = numpy.array(rows)
     row_grouper = RowGrouper(group_definition)
     grouped_matrix = row_grouper.sum(matrix)
     value = to_list_of_lists(grouped_matrix)
     expected_value = [
         [4, 6, 8, 10],
         [6, 8, 10, 12],
     ]
     self.assertEqual(value, expected_value)
     self.assertEqual(row_grouper.ids, ['even', 'odd'])
     self.assertEqual(row_grouper.offsets, {'even': 0, 'odd': 1})
Пример #9
0
    def test_sum_with_all_group_and_matrix_type_combinations(self):
        """
        Tests the `sum` method with every combination of group type and matrix
        type
        """
        test_cases = product(self.get_group_definitions(), self.get_matrices())
        for (group_name, group_definition), (matrix_name, matrix) in test_cases:
            with self.subTest(matrix=matrix_name, group=group_name):
                row_grouper = RowGrouper(group_definition)

                # Test summing all groups (the default if no groups specified)
                with self.subTest(group_ids=None):
                    grouped_matrix = row_grouper.sum(matrix)
                    values = to_list_of_lists(grouped_matrix)
                    # Calculate the same dict the boring way using pure Python
                    expected_values = self.sum_rows_by_group(group_definition, matrix)
                    # We need to round floats to account for differences
                    # between numpy and Python float rounding
                    self.assertEqual(
                        round_floats(values), round_floats(expected_values)
                    )

                # Test summing just specific groups by getting the last two
                # group ids in reverse order
                group_ids = row_grouper.ids[-1:-3:-1]
                with self.subTest(group_ids=group_ids):
                    grouped_matrix = row_grouper.sum(matrix, group_ids)
                    values = to_list_of_lists(grouped_matrix)
                    # Calculate the same dict the boring way using pure Python
                    expected_values = self.sum_rows_by_group(
                        group_definition, matrix, group_ids
                    )
                    # We need to round floats to account for differences
                    # between numpy and Python float rounding
                    self.assertEqual(
                        round_floats(values), round_floats(expected_values)
                    )