Esempio n. 1
0
    def test_sample_without_replacement(self) -> None:
        """Tests is sampling is done correctly."""
        # Prepare
        distribution = np.array([0.0, 1.0, 0.0], dtype=np.float32)
        empty_classes = [False, False, True]
        list_samples = [
            [
                np.zeros((3, 1, 1), dtype=np.float32),
                np.zeros((3, 1, 1), dtype=np.float32),
                np.zeros((3, 1, 1), dtype=np.float32),
                np.zeros((3, 1, 1), dtype=np.float32),
                np.zeros((3, 1, 1), dtype=np.float32),
            ],
            [
                7 * np.ones((3, 1, 1), dtype=np.float32),
                7 * np.ones((3, 1, 1), dtype=np.float32),
                7 * np.ones((3, 1, 1), dtype=np.float32),
                7 * np.ones((3, 1, 1), dtype=np.float32),
                7 * np.ones((3, 1, 1), dtype=np.float32),
            ],
            [],
        ]
        num_samples = 4
        expected_x = 7 * np.ones((4, 3, 1, 1), dtype=np.float32)
        expected_y = np.array(4 * [1], dtype=np.int64)

        # Execute
        this_partition, _ = sample_without_replacement(distribution,
                                                       list_samples,
                                                       num_samples,
                                                       empty_classes)

        # Assert
        assert_identity(this_partition, (expected_x, expected_y))
Esempio n. 2
0
 def test_sample_without_replacement_large_sample(self) -> None:
     """Tests is requesting too many samples will raise an exception."""
     # Prepare
     distribution = np.array([1.0 / 3, 1.0 / 3, 1.0 / 3], dtype=np.float32)
     list_samples = [
         [
             np.zeros((3, 32, 32), dtype=np.float32),
             np.zeros((3, 32, 32), dtype=np.float32),
         ],
         [
             np.ones((3, 32, 32), dtype=np.float32),
             np.ones((3, 32, 32), dtype=np.float32),
         ],
         [
             2 * np.ones((3, 32, 32), dtype=np.float32),
             2 * np.ones((3, 32, 32), dtype=np.float32),
         ],
     ]
     num_samples = 100000
     empty_classes = 3 * [False]
     # Execute
     with self.assertRaises(ValueError):
         sample_without_replacement(distribution, list_samples, num_samples,
                                    empty_classes)
Esempio n. 3
0
    def test_sample_without_replacement_updating_empty_list(self) -> None:
        """Tests is empty list is being updated."""
        # Prepare
        distribution = np.array([1.0 / 3, 1.0 / 3, 1.0 / 3], dtype=np.float32)
        empty_classes = [False, False, True]
        list_samples = [
            [
                np.zeros((3, 32, 32), dtype=np.float32),
                np.zeros((3, 32, 32), dtype=np.float32),
            ],
            [
                np.ones((3, 32, 32), dtype=np.float32),
                np.ones((3, 32, 32), dtype=np.float32),
            ],
            [],
        ]
        num_samples = 3
        # Execute
        _, list_empty = sample_without_replacement(distribution, list_samples,
                                                   num_samples, empty_classes)

        # Assert
        assert sum(list_empty) == 2