|
5 | 5 |
|
6 | 6 |
|
7 | 7 | class TestSimpleEstimator(unittest.TestCase): |
8 | | - def test_prediction_success(self): |
| 8 | + def setUp(self): |
| 9 | + self.estimator = SimpleDistributionEstimator() |
| 10 | + self.confoundings = np.zeros((20, 5)) |
| 11 | + self.treatment_arms = np.hstack([np.zeros(10), np.ones(10)]) |
| 12 | + self.outcomes = np.arange(20) |
| 13 | + self.estimator.fit(self.confoundings, self.treatment_arms, self.outcomes) |
| 14 | + |
| 15 | + def test_fit(self): |
| 16 | + self.assertTrue(np.array_equal(self.estimator.confoundings, self.confoundings)) |
| 17 | + self.assertTrue( |
| 18 | + np.array_equal(self.estimator.treatment_arms, self.treatment_arms) |
| 19 | + ) |
| 20 | + self.assertTrue(np.array_equal(self.estimator.outcomes, self.outcomes)) |
| 21 | + |
| 22 | + def test_fit_invalid_shapes(self): |
9 | 23 | # Arrange |
10 | | - X = np.arange(20) |
11 | | - D = np.zeros(20) |
12 | | - D[:10] = 1 |
13 | | - Y = np.arange(20) |
14 | | - subject = SimpleDistributionEstimator() |
15 | | - subject.fit(X, D, Y) |
| 24 | + confoundings_invalid = np.array([[1, 2], [3, 4]]) |
| 25 | + treatment_arms_invalid = np.array([0, 1]) |
| 26 | + outcomes_invalid = np.array([0.5, 0.7]) |
| 27 | + |
| 28 | + # Assert |
| 29 | + with self.assertRaises(ValueError): |
| 30 | + self.estimator.fit(confoundings_invalid, self.treatment_arms, self.outcomes) |
| 31 | + |
| 32 | + with self.assertRaises(ValueError): |
| 33 | + self.estimator.fit(self.confoundings, treatment_arms_invalid, self.outcomes) |
| 34 | + |
| 35 | + with self.assertRaises(ValueError): |
| 36 | + self.estimator.fit(self.confoundings, self.treatment_arms, outcomes_invalid) |
| 37 | + |
| 38 | + def test_predict(self): |
| 39 | + # Arrange |
| 40 | + treatment_arms_test = np.array([0, 1]) |
| 41 | + locations_test = np.array([3, 6]) |
| 42 | + expected_output = np.array([0.4, 0]) |
16 | 43 |
|
17 | 44 | # Act |
18 | | - actual = subject.predict(D, Y) |
| 45 | + output = self.estimator.predict(treatment_arms_test, locations_test) |
19 | 46 |
|
20 | 47 | # Assert |
21 | | - expected = np.array( |
22 | | - [0.1 * i for i in range(1, 11)] + [0.1 * i for i in range(1, 11)] |
23 | | - ) |
24 | | - npt.assert_allclose(actual, expected) |
| 48 | + np.testing.assert_array_almost_equal(output, expected_output, decimal=2) |
25 | 49 |
|
26 | 50 | def test_prediction_fail_before_fit(self): |
27 | 51 | # Arrange |
28 | | - D = np.zeros(20) |
29 | | - D[:10] = 1 |
30 | | - Y = np.arange(20) |
| 52 | + treatment_arms_test = np.array([0, 1]) |
| 53 | + locations_test = np.array([3, 6]) |
31 | 54 | subject = SimpleDistributionEstimator() |
32 | 55 |
|
33 | 56 | # Act, Assert |
34 | 57 | with self.assertRaises(ValueError) as cm: |
35 | | - subject.predict(D, Y) |
| 58 | + subject.predict(treatment_arms_test, locations_test) |
36 | 59 | self.assertEqual( |
37 | 60 | str(cm.exception), |
38 | 61 | "This estimator has not been trained yet. Please call fit first", |
39 | 62 | ) |
40 | 63 |
|
41 | | - def test_fit_fail_invalid_input(self): |
| 64 | + def test_prediction_fail_invalid_arm(self): |
42 | 65 | # Arrange |
43 | | - X = np.arange(20) |
44 | | - D = np.zeros(10) |
45 | | - D[:10] = 1 |
46 | | - Y = np.arange(20) |
47 | | - subject = SimpleDistributionEstimator() |
| 66 | + treatment_arms_invalid = np.array([2]) |
| 67 | + locations_test = np.array([3, 6]) |
48 | 68 |
|
49 | 69 | # Act, Assert |
50 | 70 | with self.assertRaises(ValueError) as cm: |
51 | | - subject.fit(X, D, Y) |
| 71 | + self.estimator.predict(treatment_arms_invalid, locations_test) |
52 | 72 | self.assertEqual( |
53 | 73 | str(cm.exception), |
54 | | - "The shape of confounding and treatment_arm should be same", |
| 74 | + "This treatment_arms argument contains arms not included in the training data: {2}", |
55 | 75 | ) |
0 commit comments