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})
 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_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)
                    )
 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))
 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})