Skip to content

Commit 1da7ebb

Browse files
Test that constructive backdoor is not met
1 parent 09741f1 commit 1da7ebb

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

tests/json_front_tests/test_json_class.py

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_f_flag(self):
9999
effects = {"NoEffect": NoEffect()}
100100
mutates = {
101101
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
102-
> self.json_class.scenario.variables[x].z3
102+
> self.json_class.scenario.variables[x].z3
103103
}
104104
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
105105
with self.assertRaises(StatisticsError):
@@ -149,7 +149,7 @@ def test_run_json_tests_from_json(self):
149149
effects = {"NoEffect": NoEffect()}
150150
mutates = {
151151
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
152-
> self.json_class.scenario.variables[x].z3
152+
> self.json_class.scenario.variables[x].z3
153153
}
154154
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
155155

@@ -176,7 +176,7 @@ def test_generate_tests_from_json_no_dist(self):
176176
effects = {"NoEffect": NoEffect()}
177177
mutates = {
178178
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
179-
> self.json_class.scenario.variables[x].z3
179+
> self.json_class.scenario.variables[x].z3
180180
}
181181
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
182182

@@ -206,7 +206,7 @@ def test_formula_in_json_test(self):
206206
effects = {"Positive": Positive()}
207207
mutates = {
208208
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
209-
> self.json_class.scenario.variables[x].z3
209+
> self.json_class.scenario.variables[x].z3
210210
}
211211
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
212212

@@ -259,7 +259,7 @@ def test_concrete_generate_params(self):
259259
effects = {"NoEffect": NoEffect()}
260260
mutates = {
261261
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
262-
> self.json_class.scenario.variables[x].z3
262+
> self.json_class.scenario.variables[x].z3
263263
}
264264
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
265265

@@ -272,49 +272,91 @@ def test_concrete_generate_params(self):
272272
self.assertIn("failed", temp_out[-1])
273273

274274
def test_no_data_provided(self):
275+
json_class = JsonUtility("temp_out.txt", True)
276+
json_class.set_paths(self.json_path, self.dag_path)
277+
278+
with self.assertRaises(ValueError):
279+
json_class.setup(self.scenario)
280+
281+
def test_estimator_formula_type_check(self):
275282
example_test = {
276283
"tests": [
277284
{
278285
"name": "test1",
279286
"mutations": {"test_input": "Increase"},
280-
"estimator": "LinearRegressionEstimator",
287+
"estimator": "CausalForestEstimator",
281288
"estimate_type": "ate",
282289
"effect_modifiers": [],
283-
"expected_effect": {"test_output": "NoEffect"},
290+
"expected_effect": {"test_output": "Positive"},
284291
"skip": False,
292+
"formula": "test_output ~ test_input",
285293
}
286294
]
287295
}
288-
json_class = JsonUtility("temp_out.txt", True)
289-
json_class.set_paths(self.json_path, self.dag_path)
296+
self.json_class.test_plan = example_test
290297

291-
with self.assertRaises(ValueError):
292-
json_class.setup(self.scenario)
298+
with self.assertRaises(TypeError):
299+
self.json_class.run_json_tests(effects=effects, mutates=mutates, estimators=estimators, f_flag=False)
293300

294-
def test_estimator_formula_type_check(self):
301+
effects = {"Positive": Positive()}
302+
mutates = {
303+
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
304+
> self.json_class.scenario.variables[x].z3
305+
}
306+
estimators = {"CausalForestEstimator": CausalForestEstimator}
307+
308+
def test_constructive_back_door_not_met(self):
295309
example_test = {
296310
"tests": [
297311
{
298312
"name": "test1",
299-
"mutations": {"test_input": "Increase"},
300-
"estimator": "CausalForestEstimator",
313+
"mutations": {"X": "Increase"},
314+
"estimator": "LinearRegressionEstimator",
301315
"estimate_type": "ate",
302316
"effect_modifiers": [],
303-
"expected_effect": {"test_output": "Positive"},
317+
"expected_effect": {"Y": "NoEffect"},
304318
"skip": False,
305-
"formula": "test_output ~ test_input",
319+
"formula": "Y ~ X",
306320
}
307321
]
308322
}
309-
self.json_class.test_plan = example_test
310-
effects = {"Positive": Positive()}
323+
inputs = [
324+
Input("X", int),
325+
Input("Z", int)
326+
]
327+
outputs = [
328+
Output("Y", int)
329+
]
330+
variables = inputs + outputs
331+
modelling_scenario = Scenario(variables)
332+
modelling_scenario.setup_treatment_variables()
333+
json_utility = JsonUtility("temp_out.txt", True)
334+
test_data_dir_path = Path("tests/resources/data")
335+
dag_path = str(test_data_dir_path / "dag_not_descendent.dot")
336+
data_path = [str(test_data_dir_path / "not_descendent.csv")]
337+
338+
input_dict_list = [
339+
{"name": "X", "datatype": float},
340+
{"name": "Z", "datatype": float},
341+
]
342+
output_dict_list = [{"name": "Y", "datatype": float}]
343+
variables = CausalVariables(
344+
inputs=input_dict_list, outputs=output_dict_list, metas=None
345+
)
346+
347+
effects = {"NoEffect": NoEffect()}
311348
mutates = {
312-
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
313-
> self.json_class.scenario.variables[x].z3
349+
"Increase": lambda x: json_utility.scenario.treatment_variables[x].z3
350+
> json_utility.scenario.variables[x].z3
314351
}
315-
estimators = {"CausalForestEstimator": CausalForestEstimator}
316-
with self.assertRaises(TypeError):
317-
self.json_class.run_json_tests(effects=effects, mutates=mutates, estimators=estimators, f_flag=False)
352+
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
353+
354+
scenario = Scenario(variables=variables, constraints=None)
355+
json_utility.set_paths(self.json_path, dag_path, data_path)
356+
json_utility.setup(scenario)
357+
json_utility.test_plan = example_test
358+
with self.assertRaises(ValueError):
359+
json_utility.run_json_tests(effects=effects, mutates=mutates, estimators=estimators, f_flag=False)
318360

319361
def tearDown(self) -> None:
320362
remove_temp_dir_if_existent()

0 commit comments

Comments
 (0)