Skip to content

Commit 4de42e5

Browse files
committed
fix: from comments by @jmafoster1
1 parent adbfb72 commit 4de42e5

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

causal_testing/specification/causal_dag.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ def check_iv_assumptions(self, treatment, outcome, instrument) -> bool:
171171
# (iii) Instrument and outcome do not share causes
172172

173173
for cause in self.graph.nodes:
174-
if cause not in (instrument, outcome): # exclude self-cycles due to breaking changes in NetworkX > 3.2
175-
instrument_paths = list(nx.all_simple_paths(self.graph, source=cause, target=instrument))
176-
outcome_paths = list(nx.all_simple_paths(self.graph, source=cause, target=outcome))
177-
if len(instrument_paths) > 0 and len(outcome_paths) > 0:
178-
print(cause, instrument, instrument_paths, outcome, outcome_paths)
179-
raise ValueError(f"Instrument {instrument} and outcome {outcome} share common causes")
174+
# Exclude self-cycles due to breaking changes in NetworkX > 3.2
175+
outcome_paths = (
176+
list(nx.all_simple_paths(self.graph, source=cause, target=outcome)) if cause != outcome else []
177+
)
178+
instrument_paths = (
179+
list(nx.all_simple_paths(self.graph, source=cause, target=instrument)) if cause != instrument else []
180+
)
181+
if len(instrument_paths) > 0 and len(outcome_paths) > 0:
182+
raise ValueError(f"Instrument {instrument} and outcome {outcome} share common causes")
180183
return True
181184

182185
def add_edge(self, u_of_edge: Node, v_of_edge: Node, **attr):

0 commit comments

Comments
 (0)