Skip to content

Commit 82d4460

Browse files
Implement formula check in JSON frontend
1 parent 558d3e2 commit 82d4460

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

causal_testing/json_front/json_class.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def _execute_test_case(
270270
failed = False
271271

272272
estimation_model = self._setup_test(causal_test_case=causal_test_case, test=test)
273+
if "formula" in test:
274+
if not estimation_model.validate_formula(self.causal_specification.causal_dag):
275+
raise ValueError("Formula covariates do not satisfy the constructive back-door criterion.")
273276
causal_test_result = causal_test_case.execute_test(
274277
estimator=estimation_model, data_collector=self.data_collector
275278
)
@@ -331,6 +334,7 @@ def _setup_test(self, causal_test_case: CausalTestCase, test: Mapping) -> Estima
331334
estimator_kwargs["alpha"] = test["alpha"] if "alpha" in test else 0.05
332335

333336
estimation_model = test["estimator"](**estimator_kwargs)
337+
334338
return estimation_model
335339

336340
def _append_to_file(self, line: str, log_level: int = None):

causal_testing/testing/estimators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def __init__(
105105

106106
if formula is not None:
107107
self.formula = formula
108+
108109
else:
109110
terms = [treatment] + sorted(list(adjustment_set)) + sorted(list(effect_modifiers))
110111
self.formula = f"{outcome} ~ {'+'.join(terms)}"
@@ -131,7 +132,7 @@ def get_terms_from_formula(self):
131132
return outcome, self.treatment, covariates
132133

133134
def validate_formula(self, causal_dag: CausalDAG):
134-
outcome, treatment, covariates = causal_dag.get_terms_from_formula()
135+
outcome, treatment, covariates = self.get_terms_from_formula()
135136
proper_backdoor_graph = causal_dag.get_proper_backdoor_graph(treatments=[treatment], outcomes=[outcome])
136137
return CausalDAG.constructive_backdoor_criterion(proper_backdoor_graph=proper_backdoor_graph,
137138
treatments=[treatment], outcomes=[outcome],

0 commit comments

Comments
 (0)