6
6
import logging
7
7
8
8
from dataclasses import dataclass
9
- from enum import Enum
10
9
from pathlib import Path
11
10
12
11
import pandas as pd
@@ -106,12 +105,14 @@ def generate_tests(self, effects: dict, mutates: dict, estimators: dict, f_flag:
106
105
107
106
concrete_tests , dummy = abstract_test .generate_concrete_tests (5 , 0.05 )
108
107
failures = self ._execute_tests (concrete_tests , estimators , test , f_flag )
109
- msg = f"Executing test: { test ['name' ]} \n " + \
110
- f"abstract_test \n " + \
111
- f"{ abstract_test } \n " + \
112
- f"{ abstract_test .treatment_variable .name } ,{ abstract_test .treatment_variable .distribution } \n " + \
113
- f"Number of concrete tests for test case: { str (len (concrete_tests ))} \n " + \
114
- f"{ failures } /{ len (concrete_tests )} failed for { test ['name' ]} "
108
+ msg = (
109
+ f"Executing test: { test ['name' ]} \n "
110
+ + "abstract_test \n "
111
+ + f"{ abstract_test } \n "
112
+ + f"{ abstract_test .treatment_variable .name } ,{ abstract_test .treatment_variable .distribution } \n "
113
+ + f"Number of concrete tests for test case: { str (len (concrete_tests ))} \n "
114
+ + f"{ failures } /{ len (concrete_tests )} failed for { test ['name' ]} "
115
+ )
115
116
self ._append_to_file (msg , logging .INFO )
116
117
117
118
def _execute_tests (self , concrete_tests , estimators , test , f_flag ):
@@ -177,8 +178,9 @@ def _execute_test_case(self, causal_test_case: CausalTestCase, estimator: Estima
177
178
)
178
179
if not test_passes :
179
180
failed = True
180
- self ._append_to_file (f"FAILED- expected { causal_test_case .expected_causal_effect } , got { result_string } " ,
181
- logging .WARNING )
181
+ self ._append_to_file (
182
+ f"FAILED- expected { causal_test_case .expected_causal_effect } , got { result_string } " , logging .WARNING
183
+ )
182
184
return failed
183
185
184
186
def _setup_test (self , causal_test_case : CausalTestCase , estimator : Estimator ) -> tuple [CausalTestEngine , Estimator ]:
@@ -217,13 +219,24 @@ def add_modelling_assumptions(self, estimation_model: Estimator): # pylint: dis
217
219
return
218
220
219
221
def _append_to_file (self , line : str , log_level : int = None ):
220
- with open (self .output_path , "a" ) as f :
221
- f .write (line + "\n " )
222
+ """ Appends given line(s) to the current output file. If log_level is specified it also logs that message to the
223
+ logging level.
224
+ :param line: The line or lines of text to be appended to the file
225
+ :param log_level: An integer representing the logging level as specified by pythons inbuilt logging module. It
226
+ is possible to use the inbuilt logging level variables such as logging.INFO and logging.WARNING
227
+ """
228
+ with open (self .output_path , "a" , encoding = 'utf-8' ) as f :
229
+ f .write (line + "\n " , )
222
230
if log_level :
223
231
logger .log (level = log_level , msg = line )
224
232
225
233
@staticmethod
226
234
def check_file_exists (output_path : Path , overwrite : bool ):
235
+ """ Method that checks if the given path to an output file already exists. If overwrite is true the check is
236
+ passed.
237
+ :param output_path: File path for the output file of the JSON Frontend
238
+ :param overwrite: bool that if true, the current file can be overwritten
239
+ """
227
240
if not overwrite and output_path .is_file ():
228
241
raise FileExistsError (f"Chosen file output ({ output_path } ) already exists" )
229
242
@@ -243,8 +256,9 @@ def get_args(test_args=None) -> argparse.Namespace:
243
256
)
244
257
parser .add_argument (
245
258
"-w" ,
246
- help = "Specify to overwrite any existing output files. This can lead to the loss of existing outputs if not careful" ,
247
- action = "store_true"
259
+ help = "Specify to overwrite any existing output files. This can lead to the loss of existing outputs if not "
260
+ "careful" ,
261
+ action = "store_true" ,
248
262
)
249
263
parser .add_argument (
250
264
"--log_path" ,
0 commit comments