Skip to content

Commit 16301d7

Browse files
committed
Removed adequacy warning catching and made alpha an MR parameter
1 parent 6325ced commit 16301d7

3 files changed

Lines changed: 19 additions & 48 deletions

File tree

causal_testing/testing/causal_test_adequacy.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from itertools import combinations
88

99
import pandas as pd
10-
from lifelines.exceptions import ConvergenceError
11-
from numpy.linalg import LinAlgError
1210

1311
from causal_testing.specification.causal_dag import CausalDAG
1412
from causal_testing.testing.causal_test_case import CausalTestCase
@@ -104,21 +102,9 @@ def measure_adequacy(self):
104102
estimator.df = estimator.df[estimator.df[self.group_by].isin(ids)]
105103
else:
106104
estimator.df = estimator.df.sample(len(estimator.df), replace=True, random_state=i)
107-
try:
108-
result = self.test_case.execute_test(estimator)
109-
outcomes.append(self.test_case.expected_causal_effect.apply(result))
110-
results.append(result.effect_estimate.to_df())
111-
except LinAlgError:
112-
logger.warning("Adequacy LinAlgError")
113-
continue
114-
except ConvergenceError:
115-
logger.warning("Adequacy ConvergenceError")
116-
continue
117-
except ValueError as e:
118-
logger.warning(f"Adequacy ValueError: {e}")
119-
continue
120-
# outcomes = [self.test_case.expected_causal_effect.apply(c) for c in results]
121-
# results = pd.concat([c.effect_estimate.to_df() for c in results])
105+
result = self.test_case.execute_test(estimator)
106+
outcomes.append(self.test_case.expected_causal_effect.apply(result))
107+
results.append(result.effect_estimate.to_df())
122108
results = pd.concat(results)
123109
results["var"] = results.index
124110
results["passed"] = outcomes

causal_testing/testing/metamorphic_relation.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,45 @@ def __eq__(self, other):
3333
same_adjustment_set = set(self.adjustment_vars) == set(other.adjustment_vars)
3434
return same_type and same_treatment and same_outcome and same_effect and same_adjustment_set
3535

36-
37-
class ShouldCause(MetamorphicRelation):
38-
"""Class representing a should cause metamorphic relation."""
39-
4036
def to_json_stub(
4137
self,
4238
skip: bool = False,
4339
estimate_type: str = "coefficient",
4440
effect_type: str = "direct",
4541
estimator: str = "LinearRegressionEstimator",
42+
alpha: float = 0.05,
4643
) -> dict:
4744
"""
4845
Convert to a JSON frontend stub string for user customisation.
4946
:param skip: Whether to skip the test (default False).
5047
:param effect_type: The type of causal effect to consider (total or direct)
5148
:param estimate_type: The estimate type to use when evaluating tests
5249
:param estimator: The name of the estimator class to use when evaluating the test
50+
:param alpha: The significance level to use when calculating the confidence intervals
5351
"""
5452
return {
5553
"name": str(self),
5654
"estimator": estimator,
5755
"estimate_type": estimate_type,
5856
"effect": effect_type,
5957
"treatment_variable": self.base_test_case.treatment_variable,
60-
"expected_effect": {self.base_test_case.outcome_variable: "SomeEffect"},
6158
"formula": (
6259
f"{self.base_test_case.outcome_variable} ~ "
6360
f"{' + '.join([self.base_test_case.treatment_variable] + self.adjustment_vars)}"
6461
),
62+
"alpha": alpha,
6563
"skip": skip,
6664
}
6765

66+
67+
class ShouldCause(MetamorphicRelation):
68+
"""Class representing a should cause metamorphic relation."""
69+
70+
def to_json_stub(self, **kwargs):
71+
return super().to_json_stub(**kwargs) | {
72+
"expected_effect": {self.base_test_case.outcome_variable: "SomeEffect"},
73+
}
74+
6875
def __str__(self):
6976
formatted_str = f"{self.base_test_case.treatment_variable} --> {self.base_test_case.outcome_variable}"
7077
if self.adjustment_vars:
@@ -75,33 +82,9 @@ def __str__(self):
7582
class ShouldNotCause(MetamorphicRelation):
7683
"""Class representing a should cause metamorphic relation."""
7784

78-
def to_json_stub(
79-
self,
80-
skip: bool = False,
81-
estimate_type: str = "coefficient",
82-
effect_type: str = "direct",
83-
estimator: str = "LinearRegressionEstimator",
84-
) -> dict:
85-
"""
86-
Convert to a JSON frontend stub string for user customisation.
87-
:param skip: Whether to skip the test (default False).
88-
:param effect_type: The type of causal effect to consider (total or direct)
89-
:param estimate_type: The estimate type to use when evaluating tests
90-
:param estimator: The name of the estimator class to use when evaluating the test
91-
"""
92-
return {
93-
"name": str(self),
94-
"estimator": estimator,
95-
"estimate_type": estimate_type,
96-
"effect": effect_type,
97-
"treatment_variable": self.base_test_case.treatment_variable,
85+
def to_json_stub(self, **kwargs):
86+
return super().to_json_stub(**kwargs) | {
9887
"expected_effect": {self.base_test_case.outcome_variable: "NoEffect"},
99-
"formula": (
100-
f"{self.base_test_case.outcome_variable} ~ "
101-
f"{' + '.join([self.base_test_case.treatment_variable] + self.adjustment_vars)}"
102-
),
103-
"alpha": 0.05,
104-
"skip": skip,
10588
}
10689

10790
def __str__(self):

tests/testing_tests/test_metamorphic_relations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def test_should_cause_json_stub(self):
104104
"formula": "Z ~ X1",
105105
"treatment_variable": "X1",
106106
"name": "X1 --> Z",
107+
"alpha": 0.05,
107108
"skip": False,
108109
},
109110
)
@@ -130,6 +131,7 @@ def test_should_cause_logistic_json_stub(self):
130131
"formula": "Z ~ X1",
131132
"treatment_variable": "X1",
132133
"name": "X1 --> Z",
134+
"alpha": 0.05,
133135
"skip": False,
134136
},
135137
)

0 commit comments

Comments
 (0)