1
- import os
1
+ """
2
+
3
+ Entrypoint script to run the causal testing framework on DAFNI
4
+
5
+ """
6
+
2
7
from pathlib import Path
3
8
import argparse
4
9
import json
@@ -14,7 +19,6 @@ class ValidationError(Exception):
14
19
"""
15
20
Custom class to capture validation errors in this script
16
21
"""
17
- pass
18
22
19
23
20
24
def get_args (test_args = None ) -> argparse .Namespace :
@@ -24,20 +28,22 @@ def get_args(test_args=None) -> argparse.Namespace:
24
28
:returns:
25
29
- argparse.Namespace - A Namsespace consisting of the arguments to this script
26
30
"""
27
- parser = argparse .ArgumentParser (description = "A script for running the causal testing famework on DAFNI." )
31
+ parser = argparse .ArgumentParser (description = "A script for running the CTF on DAFNI." )
28
32
29
33
parser .add_argument (
30
34
"--data_path" , required = True ,
31
35
help = "Path to the input runtime data (.csv)" , nargs = "+" )
32
36
33
37
parser .add_argument ('--tests_path' , required = True ,
34
- help = 'Path to the input configuration file containing the causal tests (.json)' )
38
+ help = 'Input configuration file path '
39
+ 'containing the causal tests (.json)' )
35
40
36
41
parser .add_argument ('--variables_path' , required = True ,
37
- help = 'Path to the input configuration file containing the predefined variables (.json)' )
42
+ help = 'Input configuration file path '
43
+ 'containing the predefined variables (.json)' )
38
44
39
45
parser .add_argument ("--dag_path" , required = True ,
40
- help = "Path to the input file containing a valid DAG (.dot). "
46
+ help = "Input configuration file path containing a valid DAG (.dot). "
41
47
"Note: this must be supplied if the --tests argument isn't provided." )
42
48
43
49
parser .add_argument ('--output_path' , required = False , help = 'Path to the output directory.' )
@@ -81,7 +87,7 @@ def get_args(test_args=None) -> argparse.Namespace:
81
87
return args
82
88
83
89
84
- def read_variables (variables_path : Path ) -> dict :
90
+ def read_variables (variables_path : Path ) -> FileNotFoundError | dict :
85
91
"""
86
92
Function to read the variables.json file specified by the user
87
93
:param variables_path: A Path object of the user-specified file path
@@ -90,44 +96,43 @@ def read_variables(variables_path: Path) -> dict:
90
96
"""
91
97
if not variables_path .exists () or variables_path .is_dir ():
92
98
93
- raise FileNotFoundError
94
-
95
99
print (f"JSON file not found at the specified location: { variables_path } " )
96
100
97
- else :
101
+ raise FileNotFoundError
98
102
99
- with variables_path .open ('r' ) as file :
103
+ with variables_path .open ('r' ) as file :
100
104
101
- inputs = json .load (file )
105
+ inputs = json .load (file )
102
106
103
- return inputs
107
+ return inputs
104
108
105
109
106
110
def validate_variables (data_dict : dict ) -> tuple :
107
111
"""
108
112
Function to validate the variables defined in the causal tests
109
113
:param data_dict: A dictionary consisting of the pre-defined variables for the causal tests
110
114
:returns:
111
- - tuple - Tuple consisting of the inputs, outputs and constraints to pass into the modelling scenario
115
+ - Tuple containing the inputs, outputs and constraints to pass into the modelling scenario
112
116
"""
113
117
if data_dict ["variables" ]:
114
118
115
119
variables = data_dict ["variables" ]
116
120
117
- inputs = [Input (variable ["name" ], eval (variable ["datatype" ])) for variable in variables if
121
+ inputs = [Input (variable ["name" ], eval (variable ["datatype" ]))
122
+ for variable in variables if
118
123
variable ["typestring" ] == "Input" ]
119
124
120
- outputs = [Output (variable ["name" ], eval (variable ["datatype" ])) for variable in variables if
125
+ outputs = [Output (variable ["name" ], eval (variable ["datatype" ]))
126
+ for variable in variables if
121
127
variable ["typestring" ] == "Output" ]
122
128
123
129
constraints = set ()
124
130
125
- for variable , _inputs in zip (variables , inputs ):
131
+ for variable , input_var in zip (variables , inputs ):
126
132
127
133
if "constraint" in variable :
128
134
129
- constraints .add (_inputs .z3 == variable ["constraint" ])
130
-
135
+ constraints .add (input_var .z3 == variable ["constraint" ])
131
136
else :
132
137
133
138
raise ValidationError ("Cannot find the variables defined by the causal tests." )
@@ -180,7 +185,8 @@ def main():
180
185
json_utility .setup (scenario = modelling_scenario , data = data_frame )
181
186
182
187
# Step 7: Run the causal tests
183
- test_outcomes = json_utility .run_json_tests (effects = expected_outcome_effects , mutates = {}, estimators = estimators ,
188
+ test_outcomes = json_utility .run_json_tests (effects = expected_outcome_effects ,
189
+ mutates = {}, estimators = estimators ,
184
190
f_flag = args .f )
185
191
186
192
# Step 8: Update, print and save the final outputs
@@ -196,7 +202,7 @@ def main():
196
202
test ["result" ].pop ("control_value" )
197
203
198
204
199
- with open (args .output_path , "w" ) as f :
205
+ with open (args .output_path , "w" , encoding = "utf-8" ) as f :
200
206
201
207
print (json .dumps (test_outcomes , indent = 2 ), file = f )
202
208
@@ -208,7 +214,8 @@ def main():
208
214
209
215
else :
210
216
211
- print (f"Execution successful. Output file saved at { Path (args .output_path ).parent .resolve ()} " )
217
+ print (f"Execution successful. "
218
+ f"Output file saved at { Path (args .output_path ).parent .resolve ()} " )
212
219
213
220
214
221
if __name__ == "__main__" :
0 commit comments