Skip to content

Commit 41a8af1

Browse files
Convert all log messages to file
1 parent 7741448 commit 41a8af1

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

causal_testing/json_front/json_class.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ def generate_tests(self, effects: dict, mutates: dict, estimators: dict, f_flag:
105105
abstract_test = self._create_abstract_test_case(test, mutates, effects)
106106

107107
concrete_tests, dummy = abstract_test.generate_concrete_tests(5, 0.05)
108-
logger.info("Executing test: %s", test["name"])
109-
logger.info(abstract_test)
110-
logger.info([abstract_test.treatment_variable.name, abstract_test.treatment_variable.distribution])
111-
logger.info("Number of concrete tests for test case: %s", str(len(concrete_tests)))
112108
failures = self._execute_tests(concrete_tests, estimators, test, f_flag)
113-
logger.info("%s/%s failed for %s\n", failures, len(concrete_tests), test["name"])
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))}"
114+
115+
self.append_to_file(msg, logging.INFO)
116+
114117

115118
def _execute_tests(self, concrete_tests, estimators, test, f_flag):
116119
failures = 0
@@ -120,6 +123,7 @@ def _execute_tests(self, concrete_tests, estimators, test, f_flag):
120123
failures += 1
121124
return failures
122125

126+
123127
def _json_parse(self):
124128
"""Parse a JSON input file into inputs, outputs, metas and a test plan"""
125129
with open(self.input_paths.json_path, encoding="utf-8") as f:
@@ -129,6 +133,7 @@ def _json_parse(self):
129133
self.data.append(df)
130134
self.data = pd.concat(self.data)
131135

136+
132137
def _populate_metas(self):
133138
"""
134139
Populate data with meta-variable values and add distributions to Causal Testing Framework Variables
@@ -141,7 +146,8 @@ def _populate_metas(self):
141146
fitter.fit()
142147
(dist, params) = list(fitter.get_best(method="sumsquare_error").items())[0]
143148
var.distribution = getattr(scipy.stats, dist)(**params)
144-
logger.info(var.name + f" {dist}({params})")
149+
self.append_to_file(var.name + f" {dist}({params})", logging.INFO)
150+
145151

146152
def _execute_test_case(self, causal_test_case: CausalTestCase, estimator: Estimator, f_flag: bool) -> bool:
147153
"""Executes a singular test case, prints the results and returns the test case result
@@ -175,9 +181,11 @@ def _execute_test_case(self, causal_test_case: CausalTestCase, estimator: Estima
175181
)
176182
if not test_passes:
177183
failed = True
178-
self.append_to_file(f"FAILED- expected {causal_test_case.expected_causal_effect}, got {result_string}", logging.WARNING)
184+
self.append_to_file(f"FAILED- expected {causal_test_case.expected_causal_effect}, got {result_string}",
185+
logging.WARNING)
179186
return failed
180187

188+
181189
def _setup_test(self, causal_test_case: CausalTestCase, estimator: Estimator) -> tuple[CausalTestEngine, Estimator]:
182190
"""Create the necessary inputs for a single test case
183191
:param causal_test_case: The concrete test case to be executed
@@ -206,23 +214,28 @@ def _setup_test(self, causal_test_case: CausalTestCase, estimator: Estimator) ->
206214

207215
return causal_test_engine, estimation_model
208216

217+
209218
def add_modelling_assumptions(self, estimation_model: Estimator): # pylint: disable=unused-argument
210219
"""Optional abstract method where user functionality can be written to determine what assumptions are required
211220
for specific test cases
212221
:param estimation_model: estimator model instance for the current running test.
213222
"""
214223
return
224+
225+
215226
def append_to_file(self, line: str, log_level: int = None):
216227
with open(self.output_path, "a") as f:
217-
f.write('\n'.join(line))
228+
f.write(line+"\n")
218229
if log_level:
219230
logger.log(level=log_level, msg=line)
220231

232+
221233
@staticmethod
222234
def check_file_exists(output_path: Path, overwrite: bool):
223235
if not overwrite and output_path.is_file():
224236
raise FileExistsError(f"Chosen file output ({output_path}) already exists")
225237

238+
226239
@staticmethod
227240
def get_args(test_args=None) -> argparse.Namespace:
228241
"""Command-line arguments
@@ -298,4 +311,4 @@ def __init__(self, inputs: list[dict], outputs: list[dict], metas: list[dict]):
298311

299312
def __iter__(self):
300313
for var in self.inputs + self.outputs + self.metas:
301-
yield var
314+
yield var

0 commit comments

Comments
 (0)