Skip to content

Commit c387428

Browse files
fix testing bugs
1 parent 0576c4c commit c387428

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

causal_testing/json_front/json_class.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,48 +86,53 @@ def run_json_tests(self, effects: dict, estimators: dict, f_flag: bool = False,
8686
for test in self.test_plan["tests"]:
8787
if "skip" in test and test["skip"]:
8888
continue
89-
if "mutates" in test:
89+
if "mutations" in test:
9090
abstract_test = self._create_abstract_test_case(test, mutates, effects)
9191

9292
concrete_tests, dummy = abstract_test.generate_concrete_tests(5, 0.05)
9393
failures = self._execute_tests(concrete_tests, estimators, test, f_flag)
9494
msg = (
95-
f"Executing test: {test['name']}\n"
96-
+ "abstract_test\n"
97-
+ f"{abstract_test}\n"
98-
+ f"{abstract_test.treatment_variable.name},{abstract_test.treatment_variable.distribution}\n"
99-
+ f"Number of concrete tests for test case: {str(len(concrete_tests))}\n"
100-
+ f"{failures}/{len(concrete_tests)} failed for {test['name']}"
95+
f"Executing test: {test['name']}\n"
96+
+ "abstract_test\n"
97+
+ f"{abstract_test}\n"
98+
+ f"{abstract_test.treatment_variable.name},{abstract_test.treatment_variable.distribution}\n"
99+
+ f"Number of concrete tests for test case: {str(len(concrete_tests))}\n"
100+
+ f"{failures}/{len(concrete_tests)} failed for {test['name']}"
101101
)
102102
self._append_to_file(msg, logging.INFO)
103103
else:
104-
outcome_variable = next(iter(test['expectedEffect'])) # Take first key from dictionary of expected effect
105-
expected_effect = effects[test['expectedEffect'][outcome_variable]]
106-
base_test_case = BaseTestCase(treatment_variable=self.variables["inputs"][test["treatment_variable"]],
107-
outcome_variable=self.variables["outputs"][outcome_variable])
108-
109-
causal_test_case = CausalTestCase(base_test_case=base_test_case,
110-
expected_causal_effect=expected_effect,
111-
control_value=test["control_value"],
112-
treatment_value=test["treatment_value"],
113-
estimate_type=test["estimate_type"])
104+
outcome_variable = next(
105+
iter(test["expectedEffect"])
106+
) # Take first key from dictionary of expected effect
107+
base_test_case = BaseTestCase(
108+
treatment_variable=self.variables["inputs"][test["treatment_variable"]],
109+
outcome_variable=self.variables["outputs"][outcome_variable],
110+
)
114111

112+
causal_test_case = CausalTestCase(
113+
base_test_case=base_test_case,
114+
expected_causal_effect=effects[test["expectedEffect"][outcome_variable]],
115+
control_value=test["control_value"],
116+
treatment_value=test["treatment_value"],
117+
estimate_type=test["estimate_type"],
118+
)
115119

116-
if self._execute_test_case(causal_test_case=causal_test_case,
117-
estimator=estimators[test["estimator"]],
118-
f_flag=f_flag):
120+
if self._execute_test_case(
121+
causal_test_case=causal_test_case, estimator=estimators[test["estimator"]], f_flag=f_flag
122+
):
119123
result = "failed"
120124
else:
121125
result = "passed"
122126

123127
msg = (
124-
f"Executing concrete test: {test['name']} \n"
125-
+ f"treatment variable: {test['treatment_variable']} \n"
126-
+ f"outcome_variable = {outcome_variable} \n"
127-
+ f"control value = {test['control_value']}, treatment value = {test['treatment_value']} \n"
128-
+ f"result - {result} \n"
128+
f"Executing concrete test: {test['name']} \n"
129+
+ f"treatment variable: {test['treatment_variable']} \n"
130+
+ f"outcome_variable = {outcome_variable} \n"
131+
+ f"control value = {test['control_value']}, treatment value = {test['treatment_value']} \n"
132+
+ f"result - {result} \n"
129133
)
130134
self._append_to_file(msg, logging.INFO)
135+
131136
def _create_abstract_test_case(self, test, mutates, effects):
132137
assert len(test["mutations"]) == 1
133138
abstract_test = AbstractCausalTestCase(
@@ -262,12 +267,12 @@ def _append_to_file(self, line: str, log_level: int = None):
262267
logger.log(level=log_level, msg=line)
263268

264269
def _get_scenario_variables(self):
265-
for input in self.scenario.inputs():
266-
self.variables["inputs"][input.name] = input
267-
for output in self.scenario.outputs():
268-
self.variables["outputs"][output.name] = output
269-
for meta in self.scenario.metas():
270-
self.variables["metas"][meta.name] = meta
270+
for input_var in self.scenario.inputs():
271+
self.variables["inputs"][input_var.name] = input_var
272+
for output_var in self.scenario.outputs():
273+
self.variables["outputs"][output_var.name] = output_var
274+
for meta_var in self.scenario.metas():
275+
self.variables["metas"][meta_var.name] = meta_var
271276

272277
@staticmethod
273278
def check_file_exists(output_path: Path, overwrite: bool):
@@ -299,7 +304,7 @@ def get_args(test_args=None) -> argparse.Namespace:
299304
parser.add_argument(
300305
"-w",
301306
help="Specify to overwrite any existing output files. This can lead to the loss of existing outputs if not "
302-
"careful",
307+
"careful",
303308
action="store_true",
304309
)
305310
parser.add_argument(

tests/json_front_tests/test_json_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_f_flag(self):
9696
}
9797
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
9898
with self.assertRaises(StatisticsError):
99-
self.json_class.run_json_tests(effects, mutates, estimators, True)
99+
self.json_class.run_json_tests(effects, estimators, True, mutates)
100100

101101
def test_run_json_tests_from_json(self):
102102
example_test = {
@@ -120,7 +120,7 @@ def test_run_json_tests_from_json(self):
120120
}
121121
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
122122

123-
self.json_class.run_json_tests(effects, mutates, estimators, False)
123+
self.json_class.run_json_tests(effects, estimators, False, mutates)
124124

125125
# Test that the final log message prints that failed tests are printed, which is expected behaviour for this scenario
126126
with open("temp_out.txt", 'r') as reader:

0 commit comments

Comments
 (0)