diff --git a/dowhy/api/causal_data_frame.py b/dowhy/api/causal_data_frame.py index 67f627f9f3..eeba19c2aa 100755 --- a/dowhy/api/causal_data_frame.py +++ b/dowhy/api/causal_data_frame.py @@ -118,7 +118,7 @@ def do( variable_types[key] = self.convert_to_custom_type(all_variables[key].name) elif len(self._obj.columns) < len(variable_types): - raise Exception("Number of variables in the DataFrame is lesser than the variable_types dict") + raise ValueError("Number of variables in the DataFrame is lesser than the variable_types dict") if not self._sampler: self._method = method @@ -160,7 +160,7 @@ def convert_to_custom_type(self, input_type): elif "category" in input_type: return "d" else: - raise Exception("{} format is not supported".format(input_type)) + raise TypeError("{} format is not supported".format(input_type)) def parse_x(self, x): if type(x) == str: @@ -169,4 +169,4 @@ def parse_x(self, x): return {xi: None for xi in x}, True if type(x) == dict: return x, False - raise Exception("x format not recognized: {}".format(type(x))) + raise TypeError("x format not recognized: {}".format(type(x))) diff --git a/dowhy/causal_identifier/id_identifier.py b/dowhy/causal_identifier/id_identifier.py index 5ef8a515dd..89d49ce5f7 100644 --- a/dowhy/causal_identifier/id_identifier.py +++ b/dowhy/causal_identifier/id_identifier.py @@ -47,7 +47,7 @@ def get_val(self, return_type: str): elif return_type == "sum": return self._sum else: - raise Exception("Provide correct return type.") + raise ValueError("Provide correct return type. Must be 'prod' or 'sum'.") def _print_estimator(self, prefix, estimator: Union[Dict, "IDExpression"] = None, start: bool = False): """ @@ -126,7 +126,7 @@ def identify_effect_id( try: tsort_node_names = OrderedSet(list(nx.topological_sort(graph))) # topological sorting of graph nodes except nx.NetworkXUnfeasible: - raise Exception("The graph must be a directed acyclic graph (DAG).") + raise ValueError("The graph must be a directed acyclic graph (DAG).") return __adjacency_matrix_identify_effect( adjacency_matrix, diff --git a/dowhy/causal_prediction/algorithms/base_algorithm.py b/dowhy/causal_prediction/algorithms/base_algorithm.py index 9a61ccaa53..c7e7cf545a 100644 --- a/dowhy/causal_prediction/algorithms/base_algorithm.py +++ b/dowhy/causal_prediction/algorithms/base_algorithm.py @@ -28,7 +28,7 @@ def __init__(self, model, optimizer, lr, weight_decay, betas, momentum): # Check if the optimizer is currently supported if self.optimizer not in ["Adam", "SGD"]: error_msg = self.optimizer + " is not implemented currently. Try Adam or SGD." - raise Exception(error_msg) + raise ValueError(error_msg) def training_step(self, train_batch, batch_idx): """ diff --git a/dowhy/do_samplers/kernel_density_sampler.py b/dowhy/do_samplers/kernel_density_sampler.py index d721270d9d..04edc0d5e1 100644 --- a/dowhy/do_samplers/kernel_density_sampler.py +++ b/dowhy/do_samplers/kernel_density_sampler.py @@ -40,7 +40,7 @@ def _fit_conditional(self): ) def _infer_variable_types(self): - raise Exception( + raise NotImplementedError( "Variable type inference not implemented. Specify variable_types={var_name: var_type}, " "where var_type is 'o', 'c', or 'd' for ordered, continuous, or discrete, respectively." ) diff --git a/dowhy/do_samplers/mcmc_sampler.py b/dowhy/do_samplers/mcmc_sampler.py index 1dfb77b4e6..cf97b79121 100644 --- a/dowhy/do_samplers/mcmc_sampler.py +++ b/dowhy/do_samplers/mcmc_sampler.py @@ -87,7 +87,7 @@ def build_bayesian_network(self, g, df): elif g.nodes()[node]["variable_type"] == "b": g.nodes()[node]["variable"] = pm.Bernoulli("{}".format(node), logit_p=mu, observed=df[node]) else: - raise Exception("Unrecognized variable type: {}".format(g.nodes()[node]["variable_type"])) + raise ValueError("Unrecognized variable type: {}".format(g.nodes()[node]["variable_type"])) return g def fit_causal_model(self, g, df, data_types, initialization_trace=None): @@ -99,7 +99,7 @@ def fit_causal_model(self, g, df, data_types, initialization_trace=None): g = self.build_bayesian_network(g, df) trace = pm.sample(1000, tune=1000) else: - raise Exception("Graph is not a DAG!") + raise ValueError("Graph is not a DAG!") return g, trace def sample_prior_causal_model(self, g, df, data_types, initialization_trace): @@ -111,7 +111,7 @@ def sample_prior_causal_model(self, g, df, data_types, initialization_trace): g = self.build_bayesian_network(g, df) trace = pm.sample_prior_predictive(1) else: - raise Exception("Graph is not a DAG!") + raise ValueError("Graph is not a DAG!") return g, trace def do_x_surgery(self, g, x): diff --git a/dowhy/gcm/equation_parser.py b/dowhy/gcm/equation_parser.py index ead89805a4..839d59531f 100644 --- a/dowhy/gcm/equation_parser.py +++ b/dowhy/gcm/equation_parser.py @@ -151,7 +151,7 @@ def _extract_noise_model_components(noise_eq: str) -> Tuple[str, dict]: parsed_args = _parse_args(args) return noise_model_name, parsed_args else: - raise Exception("Unable to recognise the format or function specified") + raise ValueError("Unable to recognise the format or function specified") def _extract_equation_components(equation: str) -> Tuple[str, str]: @@ -185,7 +185,7 @@ def _add_undefined_nodes_info(causal_nodes_info: dict, present_nodes: list) -> N def _check_node_redundancy(causal_nodes_info: dict, node_name: str) -> None: if node_name in causal_nodes_info: - raise Exception(f"The node {node_name} is specified twice which is not allowed.") + raise ValueError(f"The node {node_name} is specified twice which is not allowed.") def _sanitize_input_expression(expression: str, banned_characters: list) -> None: diff --git a/dowhy/utils/api.py b/dowhy/utils/api.py index 197be0a796..9da846a104 100644 --- a/dowhy/utils/api.py +++ b/dowhy/utils/api.py @@ -7,4 +7,4 @@ def parse_state(state): return [xi for xi in state.keys()] if not state: return [] - raise Exception("Input format for {} not recognized: {}".format(state, type(state))) + raise TypeError("Input format for {} not recognized: {}".format(state, type(state))) diff --git a/dowhy/utils/propensity_score.py b/dowhy/utils/propensity_score.py index e8f68ceb87..e355dd4c69 100644 --- a/dowhy/utils/propensity_score.py +++ b/dowhy/utils/propensity_score.py @@ -20,7 +20,7 @@ def propensity_of_treatment_score(data, covariates, treatment, model="logistic", def state_propensity_score(data, covariates, treatments, variable_types=None): if len(set(covariates).intersection(treatments)) != 0: - raise Exception("Can't control for causal states. Remove treatment from covariates.") + raise ValueError("Can't control for causal states. Remove treatment from covariates.") log_propensities = {} for i, treatment in enumerate(treatments): if variable_types[treatment] in ["b"]: @@ -36,7 +36,7 @@ def state_propensity_score(data, covariates, treatments, variable_types=None): continuous_treatment_model(data.copy(), covariates + treatments[i + 1 :], treatment, variable_types) ) else: - raise Exception( + raise ValueError( "Variable type {} for variable {} is not a recognized format type.".format( variable_types[treatment], treatment ) @@ -104,7 +104,7 @@ def get_type_string(variables, variable_types): elif variable_types[variable] in ["c"]: var_types.append("c") else: - raise Exception( + raise ValueError( "Variable type {} for variable {} not a recognized type.".format(variable_types[variable], variable) ) return "".join(var_types)