Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: chenglu <[email protected]>
  • Loading branch information
louis-she committed Nov 25, 2024
1 parent 7be237b commit ac743bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions econml/dowhy.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,14 @@ def __getattr__(self, attr):
raise AttributeError(attr)
elif attr == "dowhy_":
if "dowhy_" not in dir(self):
raise RuntimeError("Please call DoWhyWrapper.fit first before any other operations")
raise AttributeError("Please call `DoWhyWrapper.fit` first before any other operations.")
else:
return self.dowhy_
elif attr in ['_cate_estimator', 'identified_estimand_', 'estimate_']:
return getattr(self, attr)
if attr in dir(self):
return getattr(self, attr)
else:
raise AttributeError(f"call `DoWhyWrapper.fit` first before any other operations.")
elif attr.startswith('dowhy__'):
return getattr(self.dowhy_, attr[len('dowhy__'):])
elif hasattr(self.estimate_._estimator_object, attr):
Expand Down
9 changes: 9 additions & 0 deletions econml/tests/test_dowhy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ def test_store_dataframe_name(self):
np.testing.assert_array_equal(est._effect_modifiers, X_name)
np.testing.assert_array_equal(est._treatment, [T_name])
np.testing.assert_array_equal(est._outcome, [Y_name])

def test_dowhy_without_fit(self):
with self.assertRaises(AttributeError) as context:
LinearDRLearner().dowhy.refute_estimate(method_name="random_common_cause", num_simulations=3)
self.assertTrue("Please call `DoWhyWrapper.fit` first before any other operations." in str(context.exception))

with self.assertRaises(AttributeError) as context:
LinearDRLearner().dowhy._estimator_object
self.assertTrue("call `DoWhyWrapper.fit` first before any other operations." in str(context.exception))

0 comments on commit ac743bf

Please sign in to comment.