Skip to content

Commit adbfb72

Browse files
committed
fix: exlude self-cycles in newer NetworkX versions
1 parent 75efc80 commit adbfb72

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

causal_testing/specification/causal_dag.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,14 @@ def check_iv_assumptions(self, treatment, outcome, instrument) -> bool:
169169
)
170170

171171
# (iii) Instrument and outcome do not share causes
172-
if any(
173-
(
174-
cause
175-
for cause in self.graph.nodes
176-
if len(list(nx.all_simple_paths(self.graph, source=cause, target=instrument))) > 0
177-
and len(list(nx.all_simple_paths(self.graph, source=cause, target=outcome))) > 0
178-
)
179-
):
180-
raise ValueError(f"Instrument {instrument} and outcome {outcome} share common causes")
181172

173+
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")
182180
return True
183181

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

0 commit comments

Comments
 (0)