|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from dte_adj import DistributionEstimatorBase |
| 5 | + |
| 6 | + |
| 7 | +def compute_cumulative_distribution( |
| 8 | + target_treatment_arms: np.ndarray, |
| 9 | + locations: np.ndarray, |
| 10 | + confoundings: np.ndarray, |
| 11 | + treatment_arms: np.ndarray, |
| 12 | + outcomes: np.array, |
| 13 | +) -> np.ndarray: |
| 14 | + """Mock implementation for testing purposes.""" |
| 15 | + return np.linspace( |
| 16 | + 0, 0.9, locations.shape[0] |
| 17 | + ) + target_treatment_arms * 0.1, np.zeros((outcomes.shape[0], locations.shape[0])) |
| 18 | + |
| 19 | + |
| 20 | +class MockDistributionEstimator(DistributionEstimatorBase): |
| 21 | + def __init__( |
| 22 | + self, mock_compute_cumulative_distribution=compute_cumulative_distribution |
| 23 | + ): |
| 24 | + super().__init__() |
| 25 | + self.compute_cumulative_distribution = MagicMock() |
| 26 | + self.compute_cumulative_distribution.side_effect = ( |
| 27 | + mock_compute_cumulative_distribution |
| 28 | + ) |
| 29 | + |
| 30 | + """Mock class to implement _compute_cumulative_distribution for testing.""" |
| 31 | + |
| 32 | + def _compute_cumulative_distribution( |
| 33 | + self, |
| 34 | + target_treatment_arms: np.ndarray, |
| 35 | + locations: np.ndarray, |
| 36 | + confoundings: np.ndarray, |
| 37 | + treatment_arms: np.ndarray, |
| 38 | + outcomes: np.array, |
| 39 | + ) -> np.ndarray: |
| 40 | + return self.compute_cumulative_distribution( |
| 41 | + target_treatment_arms, locations, confoundings, treatment_arms, outcomes |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def compute_confidence_intervals(*args, **kwargs): |
| 46 | + """Mock function for compute_confidence_intervals.""" |
| 47 | + size = len(kwargs["vec_loc"]) |
| 48 | + lower_bound = np.full(size, 0.1) |
| 49 | + upper_bound = np.full(size, 0.9) |
| 50 | + return lower_bound, upper_bound |
| 51 | + |
| 52 | + |
| 53 | +class TestDistributionEstimatorBase(unittest.TestCase): |
| 54 | + def setUp(self): |
| 55 | + self.estimator = MockDistributionEstimator() |
| 56 | + self.confoundings = np.zeros((20, 5)) |
| 57 | + self.treatment_arms = np.hstack([np.zeros(10), np.ones(10)]) |
| 58 | + self.outcomes = np.arange(20) |
| 59 | + self.estimator.fit(self.confoundings, self.treatment_arms, self.outcomes) |
| 60 | + |
| 61 | + def test_initialization(self): |
| 62 | + # Arrange |
| 63 | + base_estimator = MockDistributionEstimator() |
| 64 | + |
| 65 | + # Assert |
| 66 | + self.assertIsNone(base_estimator.confoundings) |
| 67 | + self.assertIsNone(base_estimator.treatment_arms) |
| 68 | + self.assertIsNone(base_estimator.outcomes) |
| 69 | + |
| 70 | + @patch( |
| 71 | + "dte_adj.compute_confidence_intervals", side_effect=compute_confidence_intervals |
| 72 | + ) |
| 73 | + def test_predict_dte(self, mock_compute_confidence_intervals): |
| 74 | + # Arrange |
| 75 | + target_treatment_arm = 1 |
| 76 | + control_treatment_arm = 0 |
| 77 | + locations = np.arange(20) |
| 78 | + |
| 79 | + # Act |
| 80 | + dte, lower_bound, upper_bound = self.estimator.predict_dte( |
| 81 | + target_treatment_arm, control_treatment_arm, locations |
| 82 | + ) |
| 83 | + |
| 84 | + # Assert |
| 85 | + np.testing.assert_array_almost_equal(dte, np.full(locations.shape, 0.1)) |
| 86 | + np.testing.assert_array_almost_equal(lower_bound, np.full(locations.shape, 0.1)) |
| 87 | + np.testing.assert_array_almost_equal(upper_bound, np.full(locations.shape, 0.9)) |
| 88 | + self.estimator.compute_cumulative_distribution.assert_called() |
| 89 | + |
| 90 | + @patch( |
| 91 | + "dte_adj.compute_confidence_intervals", side_effect=compute_confidence_intervals |
| 92 | + ) |
| 93 | + def test_predict_pte(self, mock_compute_confidence_intervals): |
| 94 | + # Arrange |
| 95 | + target_treatment_arm = 1 |
| 96 | + control_treatment_arm = 0 |
| 97 | + locations = np.arange(20) |
| 98 | + width = 0.1 |
| 99 | + |
| 100 | + # Act |
| 101 | + pte, lower_bound, upper_bound = self.estimator.predict_pte( |
| 102 | + target_treatment_arm, control_treatment_arm, width, locations |
| 103 | + ) |
| 104 | + |
| 105 | + # Assert |
| 106 | + np.testing.assert_array_almost_equal(pte, np.full(locations.shape, 0)) |
| 107 | + np.testing.assert_array_almost_equal(lower_bound, np.full(locations.shape, 0.1)) |
| 108 | + np.testing.assert_array_almost_equal(upper_bound, np.full(locations.shape, 0.9)) |
| 109 | + self.estimator.compute_cumulative_distribution.assert_called() |
| 110 | + |
| 111 | + def test_predict_qte(self): |
| 112 | + # Arrange |
| 113 | + target_treatment_arm = 1 |
| 114 | + control_treatment_arm = 0 |
| 115 | + quantiles = np.array([0.1 * i for i in range(1, 10)]) |
| 116 | + expected_qte = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) |
| 117 | + |
| 118 | + # Act |
| 119 | + qte, lower_bound, upper_bound = self.estimator.predict_qte( |
| 120 | + target_treatment_arm, control_treatment_arm, quantiles, n_bootstrap=5 |
| 121 | + ) |
| 122 | + |
| 123 | + # Assert |
| 124 | + np.testing.assert_array_almost_equal(qte, expected_qte) |
| 125 | + np.testing.assert_array_almost_equal(lower_bound.shape, quantiles.shape) |
| 126 | + np.testing.assert_array_almost_equal(lower_bound.shape, quantiles.shape) |
| 127 | + self.estimator.compute_cumulative_distribution.assert_called() |
| 128 | + |
| 129 | + def test_fit_success(self): |
| 130 | + # Assert |
| 131 | + self.assertTrue(np.array_equal(self.estimator.confoundings, self.confoundings)) |
| 132 | + self.assertTrue( |
| 133 | + np.array_equal(self.estimator.treatment_arms, self.treatment_arms) |
| 134 | + ) |
| 135 | + self.assertTrue(np.array_equal(self.estimator.outcomes, self.outcomes)) |
| 136 | + |
| 137 | + def test_fit_invalid_shapes(self): |
| 138 | + # Arrange |
| 139 | + confoundings_invalid = np.array([[1, 2], [3, 4]]) |
| 140 | + treatment_arms_invalid = np.array([0, 1]) |
| 141 | + outcomes_invalid = np.array([0.5, 0.7]) |
| 142 | + |
| 143 | + # Assert |
| 144 | + with self.assertRaises(ValueError): |
| 145 | + self.estimator.fit(confoundings_invalid, self.treatment_arms, self.outcomes) |
| 146 | + |
| 147 | + with self.assertRaises(ValueError): |
| 148 | + self.estimator.fit(self.confoundings, treatment_arms_invalid, self.outcomes) |
| 149 | + |
| 150 | + with self.assertRaises(ValueError): |
| 151 | + self.estimator.fit(self.confoundings, self.treatment_arms, outcomes_invalid) |
| 152 | + |
| 153 | + def test_predict_success(self): |
| 154 | + # Arrange |
| 155 | + treatment_arms_test = np.array([0, 1]) |
| 156 | + locations_test = np.array([3, 6]) |
| 157 | + expected_output = np.array([0.4, 0]) |
| 158 | + |
| 159 | + # Act |
| 160 | + output = self.estimator.predict(treatment_arms_test, locations_test) |
| 161 | + |
| 162 | + # Assert |
| 163 | + self.estimator.compute_cumulative_distribution.assert_called_once() |
| 164 | + |
| 165 | + def test_predict_fail_before_fit(self): |
| 166 | + # Arrange |
| 167 | + treatment_arms_test = np.array([0, 1]) |
| 168 | + locations_test = np.array([3, 6]) |
| 169 | + subject = MockDistributionEstimator() |
| 170 | + |
| 171 | + # Act, Assert |
| 172 | + with self.assertRaises(ValueError) as cm: |
| 173 | + subject.predict(treatment_arms_test, locations_test) |
| 174 | + self.assertEqual( |
| 175 | + str(cm.exception), |
| 176 | + "This estimator has not been trained yet. Please call fit first", |
| 177 | + ) |
| 178 | + |
| 179 | + def test_predict_fail_invalid_arm(self): |
| 180 | + # Arrange |
| 181 | + treatment_arms_invalid = np.array([2]) |
| 182 | + locations_test = np.array([3, 6]) |
| 183 | + |
| 184 | + # Act, Assert |
| 185 | + with self.assertRaises(ValueError) as cm: |
| 186 | + self.estimator.predict(treatment_arms_invalid, locations_test) |
| 187 | + self.assertEqual( |
| 188 | + str(cm.exception), |
| 189 | + "This treatment_arms argument contains arms not included in the training data: {2}", |
| 190 | + ) |
0 commit comments