16
16
17
17
18
18
class TestCausalTestSuite (unittest .TestCase ):
19
- """
20
-
21
- """
19
+ """Test the Test Suite object and it's implementation in the test engine using dummy data."""
22
20
23
21
def setUp (self ) -> None :
24
- self . test_suite = CausalTestSuite ()
22
+ # 1. Create dummy Scenario and BaseTestCase
25
23
A = Input ("A" , float )
26
24
self .A = A
27
25
C = Output ("C" , float )
28
26
self .C = C
29
27
D = Output ("D" , float )
30
28
self .D = D
31
29
self .base_test_case = BaseTestCase (A , C )
32
- self .expected_causal_effect = ExactValue (4 )
33
- test_list = [CausalTestCase (self .base_test_case ,
34
- self .expected_causal_effect ,
35
- 0 ,
36
- 1 , ),
37
- CausalTestCase (self .base_test_case ,
38
- self .expected_causal_effect ,
39
- 0 ,
40
- 2 )]
41
- self .estimators = [LinearRegressionEstimator ]
42
- self .test_suite .add_test_object (base_test_case = self .base_test_case ,
43
- causal_test_case_list = test_list ,
44
- estimators = self .estimators )
30
+ self .scenario = Scenario ({A , C , D })
45
31
32
+ # 2. Create DAG and dummy data and write to csvs
46
33
temp_dir_path = create_temp_dir_if_non_existent ()
47
34
dag_dot_path = os .path .join (temp_dir_path , "dag.dot" )
48
35
dag_dot = """digraph G { A -> C; D -> A; D -> C}"""
49
36
f = open (dag_dot_path , "w" )
50
37
f .write (dag_dot )
51
38
f .close ()
52
- self .causal_dag = CausalDAG (dag_dot_path )
53
- self .scenario = Scenario ({A , C , D })
54
39
55
40
np .random .seed (1 )
56
41
df = pd .DataFrame ({"D" : list (np .random .normal (60 , 10 , 1000 ))}) # D = exogenous
@@ -59,31 +44,49 @@ def setUp(self) -> None:
59
44
self .observational_data_csv_path = os .path .join (temp_dir_path , "observational_data.csv" )
60
45
df .to_csv (self .observational_data_csv_path , index = False )
61
46
47
+ self .causal_dag = CausalDAG (dag_dot_path )
48
+
49
+ # 3. Specify data structures required for test suite
50
+ self .expected_causal_effect = ExactValue (4 )
51
+ test_list = [
52
+ CausalTestCase (
53
+ self .base_test_case ,
54
+ self .expected_causal_effect ,
55
+ 0 ,
56
+ 1 ,
57
+ ),
58
+ CausalTestCase (self .base_test_case , self .expected_causal_effect , 0 , 2 ),
59
+ ]
60
+ self .estimators = [LinearRegressionEstimator ]
61
+
62
+ # 3. Create test_suite and add a test
63
+ self .test_suite = CausalTestSuite ()
64
+ self .test_suite .add_test_object (
65
+ base_test_case = self .base_test_case , causal_test_case_list = test_list , estimators = self .estimators
66
+ )
67
+
62
68
def test_adding_test_object (self ):
69
+ "test an object can be added to the test_suite using the add_test_object function"
63
70
test_suite = CausalTestSuite ()
64
- test_list = [CausalTestCase (self .base_test_case ,
65
- self .expected_causal_effect ,
66
- 0 ,
67
- 1 )]
71
+ test_list = [CausalTestCase (self .base_test_case , self .expected_causal_effect , 0 , 1 )]
68
72
estimators = [LinearRegressionEstimator ]
69
- test_suite .add_test_object (base_test_case = self . base_test_case ,
70
- causal_test_case_list = test_list ,
71
- estimators = estimators )
73
+ test_suite .add_test_object (
74
+ base_test_case = self . base_test_case , causal_test_case_list = test_list , estimators = estimators
75
+ )
72
76
manual_test_object = {
73
- self .base_test_case : {"tests" : test_list , "estimators" : estimators , "estimate_type" : "ate" }}
77
+ self .base_test_case : {"tests" : test_list , "estimators" : estimators , "estimate_type" : "ate" }
78
+ }
74
79
self .assertEqual (test_suite , manual_test_object )
75
80
76
81
def test_return_single_test_object (self ):
82
+ """Test that a single test case can be returned from the test_suite"""
77
83
base_test_case = BaseTestCase (self .A , self .D )
78
84
79
- test_list = [CausalTestCase (self .base_test_case ,
80
- self .expected_causal_effect ,
81
- 0 ,
82
- 1 )]
85
+ test_list = [CausalTestCase (self .base_test_case , self .expected_causal_effect , 0 , 1 )]
83
86
estimators = [LinearRegressionEstimator ]
84
- self .test_suite .add_test_object (base_test_case = base_test_case ,
85
- causal_test_case_list = test_list ,
86
- estimators = estimators )
87
+ self .test_suite .add_test_object (
88
+ base_test_case = base_test_case , causal_test_case_list = test_list , estimators = estimators
89
+ )
87
90
88
91
manual_test_case = {"tests" : test_list , "estimators" : estimators , "estimate_type" : "ate" }
89
92
@@ -97,27 +100,31 @@ def test_execute_test_suite_single_base_test_case(self):
97
100
98
101
causal_test_results = causal_test_engine .execute_test_suite (test_suite = self .test_suite )
99
102
causal_test_case_result = causal_test_results [self .base_test_case ]
100
- self .assertAlmostEqual (causal_test_case_result [' LinearRegressionEstimator' ][0 ].ate , 4 , delta = 1e-10 )
103
+ self .assertAlmostEqual (causal_test_case_result [" LinearRegressionEstimator" ][0 ].ate , 4 , delta = 1e-10 )
101
104
102
105
def test_execute_test_suite_multiple_estimators (self ):
106
+ """Check that executing a test suite with multiple estimators returns correct results for the dummy data
107
+ for each estimator
108
+ """
103
109
estimators = [LinearRegressionEstimator , CausalForestEstimator ]
104
110
test_suite_2_estimators = CausalTestSuite ()
105
- test_list = [CausalTestCase (self .base_test_case ,
106
- self .expected_causal_effect ,
107
- 0 ,
108
- 1 )]
109
- test_suite_2_estimators .add_test_object (base_test_case = self .base_test_case ,
110
- causal_test_case_list = test_list ,
111
- estimators = estimators )
111
+ test_list = [CausalTestCase (self .base_test_case , self .expected_causal_effect , 0 , 1 )]
112
+ test_suite_2_estimators .add_test_object (
113
+ base_test_case = self .base_test_case , causal_test_case_list = test_list , estimators = estimators
114
+ )
112
115
causal_test_engine = self .create_causal_test_engine ()
113
116
causal_test_results = causal_test_engine .execute_test_suite (test_suite = test_suite_2_estimators )
114
117
causal_test_case_result = causal_test_results [self .base_test_case ]
115
- linear_regression_result = causal_test_case_result [' LinearRegressionEstimator' ][0 ]
116
- causal_forrest_result = causal_test_case_result [' CausalForestEstimator' ][0 ]
118
+ linear_regression_result = causal_test_case_result [" LinearRegressionEstimator" ][0 ]
119
+ causal_forrest_result = causal_test_case_result [" CausalForestEstimator" ][0 ]
117
120
self .assertAlmostEqual (linear_regression_result .ate , 4 , delta = 1e-1 )
118
121
self .assertAlmostEqual (causal_forrest_result .ate , 4 , delta = 1e-1 )
119
122
120
123
def create_causal_test_engine (self ):
124
+ """
125
+ Creating test engine is relatively computationally complex, this function allows for it to
126
+ easily be made in only the tests that require it.
127
+ """
121
128
causal_specification = CausalSpecification (self .scenario , self .causal_dag )
122
129
123
130
data_collector = ObservationalDataCollector (self .scenario , self .observational_data_csv_path )
0 commit comments