Skip to content
Open
8 changes: 4 additions & 4 deletions dowhy/causal_estimators/distance_matching_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ def fit(self, data: pd.DataFrame, effect_modifier_names: Optional[List[str]] = N

# Check if the treatment is one-dimensional
if len(self._target_estimand.treatment_variable) > 1:
error_msg = str(self.__class__) + "cannot handle more than one treatment variable"
raise Exception(error_msg)
error_msg = "{} cannot handle more than one treatment variable".format(self.__class__.__name__)
raise ValueError(error_msg)
# Checking if the treatment is binary
if not data[self._target_estimand.treatment_variable[0]].isin([0, 1]).all():
error_msg = "Distance Matching method is applicable only for binary treatments."
self.logger.error(error_msg)
raise Exception(error_msg)
raise ValueError(error_msg)

self.logger.debug("Adjustment set variables used:" + ",".join(self._target_estimand.get_adjustment_set()))

Expand All @@ -160,7 +160,7 @@ def fit(self, data: pd.DataFrame, effect_modifier_names: Optional[List[str]] = N
self._observed_common_causes = None
error_msg = "No common causes/confounders present. Distance matching methods are not applicable"
self.logger.error(error_msg)
raise Exception(error_msg)
raise ValueError(error_msg)

self.symbolic_estimator = self.construct_symbolic_estimator(self._target_estimand)
self.logger.info(self.symbolic_estimator)
Expand Down
6 changes: 3 additions & 3 deletions dowhy/causal_estimators/doubly_robust_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ def fit(
# Validate target estimand
if len(self._target_estimand.treatment_variable) > 1:
error_msg = str(self.__class__) + " cannot handle more than one treatment variable"
raise Exception(error_msg)
raise ValueError(error_msg)
if len(self._target_estimand.outcome_variable) > 1:
error_msg = str(self.__class__) + " cannot handle more than one outcome variable"
raise Exception(error_msg)
raise ValueError(error_msg)
if self._target_estimand.identifier_method not in ["backdoor", "general_adjustment"]:
error_msg = str(self.__class__) + " only supports covariate adjustment identifiers"
raise Exception(error_msg)
raise ValueError(error_msg)
if effect_modifier_names and (len(effect_modifier_names) > 0):
# TODO: Add support for effect modifiers in the Doubly Robust Estimator
raise NotImplementedError("Effect Modifiers not supported yet for " + str(self.__class__))
Expand Down
8 changes: 4 additions & 4 deletions dowhy/causal_estimators/propensity_score_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ def fit(
self._observed_common_causes = None
error_msg = "No common causes/confounders present. Propensity score based methods are not applicable"
self.logger.error(error_msg)
raise Exception(error_msg)
raise ValueError(error_msg)

# Check if the treatment is one-dimensional
if len(self._target_estimand.treatment_variable) > 1:
error_msg = str(self.__class__) + "cannot handle more than one treatment variable"
raise Exception(error_msg)
error_msg = self.__class__.__name__ + " cannot handle more than one treatment variable"
raise ValueError(error_msg)
# Checking if the treatment is binary
treatment_values = data[self._target_estimand.treatment_variable[0]].astype(int).unique()
if any([v not in [0, 1] for v in treatment_values]):
error_msg = "Propensity score methods are applicable only for binary treatments"
self.logger.error(error_msg)
raise Exception(error_msg)
raise ValueError(error_msg)

if self.propensity_score_column not in data:
if self.propensity_score_model is None:
Expand Down
8 changes: 4 additions & 4 deletions dowhy/causal_estimators/two_stage_regression_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def __init__(
self.logger.info("INFO: Using Two Stage Regression Estimator")
# Check if the treatment is one-dimensional
if len(self._target_estimand.treatment_variable) > 1:
error_msg = str(self.__class__) + "cannot handle more than one treatment variable"
raise Exception(error_msg)
error_msg = f"{self.__class__.__name__} cannot handle more than one treatment variable"
raise ValueError(error_msg)
modified_target_estimand = copy.deepcopy(self._target_estimand)
modified_target_estimand.identifier_method = "backdoor"
modified_target_estimand.backdoor_variables = self._target_estimand.mediation_first_stage_confounders
Expand Down Expand Up @@ -172,8 +172,8 @@ def fit(
self._set_effect_modifiers(data, effect_modifier_names)

if len(self._target_estimand.treatment_variable) > 1:
error_msg = str(self.__class__) + "cannot handle more than one treatment variable"
raise Exception(error_msg)
error_msg = self.__class__.__name__ + " cannot handle more than one treatment variable"
raise ValueError(error_msg)

if self._target_estimand.identifier_method == "frontdoor":
self.logger.debug("Front-door variable used:" + ",".join(self._target_estimand.get_frontdoor_variables()))
Expand Down
16 changes: 16 additions & 0 deletions tests/causal_estimators/test_doubly_robust_estimator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from pytest import mark

from dowhy.causal_estimators.doubly_robust_estimator import DoublyRobustEstimator
Expand Down Expand Up @@ -99,3 +100,18 @@ def test_average_treatment_effect(
],
method_params={"num_simulations": 10, "num_null_simulations": 10},
)

def test_multiple_treatments_raises_value_error(self):
estimator_tester = SimpleEstimator(error_tolerance=0.5, Estimator=DoublyRobustEstimator)
with pytest.raises(ValueError, match="cannot handle more than one treatment variable"):
estimator_tester.average_treatment_effect_testsuite(
num_common_causes=[1],
num_instruments=[0],
num_effect_modifiers=[0],
num_treatments=[2],
treatment_is_binary=[True],
outcome_is_binary=[False],
confidence_intervals=[False],
test_significance=[False],
method_params={"num_simulations": 10, "num_null_simulations": 10},
)
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_frontdoor_estimator(self):
)
def test_frontdoor_num_variables_error(self, Estimator, num_treatments, num_frontdoor_variables):
estimator_tester = SimpleEstimator(error_tolerance=0, Estimator=Estimator, identifier_method="frontdoor")
with pytest.raises((ValueError, Exception)):
with pytest.raises(ValueError):
estimator_tester.average_treatment_effect_testsuite(
num_common_causes=[1, 1],
num_instruments=[0, 0],
Expand Down