Skip to content

Commit 4e939a1

Browse files
committed
Codecov
1 parent 7f5cf81 commit 4e939a1

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

causal_testing/json_front/json_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def generate_tests(self, effects: dict, mutates: dict, estimators: dict, f_flag:
117117
base_test_case = BaseTestCase(
118118
treatment_variable=next(self.scenario.variables[v] for v in test["mutations"]),
119119
outcome_variable=next(self.scenario.variables[v] for v in test["expectedEffect"]),
120-
effect=test["effect"],
120+
effect=test.get("effect", "direct"),
121121
)
122122
assert len(test["expectedEffect"]) == 1, "Can only have one expected effect."
123123
concrete_tests = [

causal_testing/specification/causal_dag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,8 @@ def identification(self, base_test_case: BaseTestCase):
521521
minimal_adjustment_set = min(minimal_adjustment_sets, key=len)
522522
return minimal_adjustment_set
523523

524+
def to_dot(self):
525+
return str(nx.nx_pydot.to_pydot(self.graph))
526+
524527
def __str__(self):
525528
return f"Nodes: {self.graph.nodes}\nEdges: {self.graph.edges}"

tests/json_front_tests/test_json_class.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,32 @@ def test_f_flag(self):
102102
with self.assertRaises(StatisticsError):
103103
self.json_class.generate_tests(effects, mutates, estimators, True)
104104

105+
def test_generate_coefficient_tests_from_json(self):
106+
example_test = {
107+
"tests": [
108+
{
109+
"name": "test1",
110+
"mutations": ["test_input"],
111+
"estimator": "LinearRegressionEstimator",
112+
"estimate_type": "coefficient",
113+
"effect_modifiers": [],
114+
"expectedEffect": {"test_output": "NoEffect"},
115+
"skip": False,
116+
}
117+
]
118+
}
119+
print(self.json_class.causal_specification.causal_dag.to_dot())
120+
self.json_class.test_plan = example_test
121+
effects = {"NoEffect": NoEffect()}
122+
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
123+
124+
self.json_class.generate_tests(effects, {}, estimators, False)
125+
126+
# Test that the final log message prints that failed tests are printed, which is expected behaviour for this scenario
127+
with open("temp_out.txt", "r") as reader:
128+
temp_out = reader.readlines()
129+
self.assertIn("failed", temp_out[-1])
130+
105131
def test_generate_tests_from_json(self):
106132
example_test = {
107133
"tests": [

tests/resources/data/dag.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
digraph G { test_input_no_dist; est_input -> B; B -> C; test_output -> test_input; test_output -> C}
1+
digraph G { test_input_no_dist; test_input -> B; B -> C; test_input -> test_output; test_output -> C}

tests/testing_tests/test_causal_test_case.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
import unittest
22
import os
33
from tests.test_helpers import create_temp_dir_if_non_existent, remove_temp_dir_if_existent
4-
from causal_testing.specification.causal_specification import CausalSpecification, Scenario
54
from causal_testing.specification.variable import Input, Output
6-
from causal_testing.specification.causal_dag import CausalDAG
75
from causal_testing.testing.causal_test_case import CausalTestCase
86
from causal_testing.testing.causal_test_outcome import ExactValue
97
from causal_testing.testing.base_test_case import BaseTestCase
108

119

12-
class TestCausalTestEngineObservational(unittest.TestCase):
13-
"""Test the CausalTestEngine workflow using observational data.
10+
class TestCausalTestCase(unittest.TestCase):
11+
"""Test the CausalTestCase class.
1412
15-
The causal test engine (CTE) is the main workflow for the causal testing framework. The CTE takes a causal test case
16-
and a causal specification and computes the causal effect of the intervention on the outcome of interest.
13+
The base test case is a data class which contains the minimum information
14+
necessary to perform identification. The CausalTestCase class represents
15+
a causal test case. We here test the basic getter methods.
1716
"""
1817

1918
def setUp(self) -> None:
20-
# 1. Create Causal DAG
21-
temp_dir_path = create_temp_dir_if_non_existent()
22-
dag_dot_path = os.path.join(temp_dir_path, "dag.dot")
23-
dag_dot = """digraph G { A -> C; D -> A; D -> C}"""
24-
f = open(dag_dot_path, "w")
25-
f.write(dag_dot)
26-
f.close()
27-
self.causal_dag = CausalDAG(dag_dot_path)
28-
2919
# 2. Create Scenario and Causal Specification
3020
A = Input("A", float)
3121
C = Output("C", float)
32-
D = Output("D", float)
33-
self.scenario = Scenario({A, C, D})
34-
self.causal_specification = CausalSpecification(scenario=self.scenario, causal_dag=self.causal_dag)
3522

3623
# 3. Create an intervention and causal test case
3724
self.expected_causal_effect = ExactValue(4)

tests/testing_tests/test_estimators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ def test_estimate_coefficient(self):
176176
)
177177
self.assertEqual(iv_estimator.estimate_coefficient(self.df), 2)
178178

179+
def test_estimate_unit_ate(self):
180+
"""
181+
Test we get the correct coefficient.
182+
"""
183+
iv_estimator = InstrumentalVariableEstimator(
184+
df=self.df,
185+
treatment="X",
186+
treatment_value=None,
187+
control_value=None,
188+
adjustment_set=set(),
189+
outcome="Y",
190+
instrument="Z",
191+
)
192+
unit_ate, [low, high] = iv_estimator.estimate_unit_ate()
193+
self.assertEqual(unit_ate, 2)
194+
179195

180196
class TestLinearRegressionEstimator(unittest.TestCase):
181197
"""Test the linear regression estimator against the programming exercises in Section 2 of Hernán and Robins [1].

0 commit comments

Comments
 (0)