Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dowhy/causal_identifier/identified_estimand.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ def __deepcopy__(self, memo):
instrumental_variables=copy.deepcopy(self.instrumental_variables),
frontdoor_variables=copy.deepcopy(self.frontdoor_variables),
mediator_variables=copy.deepcopy(self.mediator_variables),
mediation_first_stage_confounders=copy.deepcopy(self.mediation_first_stage_confounders),
mediation_second_stage_confounders=copy.deepcopy(self.mediation_second_stage_confounders),
default_backdoor_id=copy.deepcopy(self.default_backdoor_id),
default_adjustment_set_id=copy.deepcopy(self.default_adjustment_set_id),
identifier_method=copy.deepcopy(self.identifier_method),
no_directed_path=self.no_directed_path,
)

def __str__(self, only_target_estimand: bool = False, show_all_backdoor_sets: bool = False):
Expand Down
32 changes: 32 additions & 0 deletions tests/causal_identifiers/test_auto_identifier.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

from dowhy.causal_identifier import AutoIdentifier
from dowhy.causal_identifier.identify_effect import EstimandType
from dowhy.graph import build_graph_from_str
Expand All @@ -15,3 +17,33 @@ def test_auto_identify_identifies_no_directed_path(self):
assert identifier.identify_effect(
graph, action_nodes=["B", "T"], outcome_nodes=["Y"], observed_nodes=["T", "Y", "A", "B"]
).no_directed_path

def test_deepcopy_preserves_all_fields(self):
"""Regression test: __deepcopy__ must copy mediation confounders and no_directed_path."""
# Build NDE estimand with mediation confounders
graph = build_graph_from_str("digraph{T->M;T->Y;M->Y;W->T;W->Y;}")
identifier = AutoIdentifier(estimand_type=EstimandType.NONPARAMETRIC_NDE)
estimand = identifier.identify_effect(
graph,
action_nodes=["T"],
outcome_nodes=["Y"],
observed_nodes=["T", "M", "Y", "W"],
)
estimand_copy = copy.deepcopy(estimand)

assert estimand_copy.mediator_variables == estimand.mediator_variables
assert estimand_copy.mediation_first_stage_confounders == estimand.mediation_first_stage_confounders
assert estimand_copy.mediation_second_stage_confounders == estimand.mediation_second_stage_confounders
assert estimand_copy.no_directed_path == estimand.no_directed_path

def test_deepcopy_preserves_no_directed_path_flag(self):
"""Regression test: deepcopy of an estimand with no directed path preserves the flag."""
graph = build_graph_from_str("digraph{T->Y;A->Y;A->B;}")
identifier = AutoIdentifier(estimand_type=EstimandType.NONPARAMETRIC_ATE)
estimand = identifier.identify_effect(
graph, action_nodes=["T", "B"], outcome_nodes=["Y"], observed_nodes=["T", "Y", "A", "B"]
)
assert estimand.no_directed_path

estimand_copy = copy.deepcopy(estimand)
assert estimand_copy.no_directed_path