Skip to content

Commit

Permalink
fix: exlude self-cycles in newer NetworkX versions
Browse files Browse the repository at this point in the history
  • Loading branch information
f-allian committed Nov 28, 2024
1 parent 75efc80 commit adbfb72
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions causal_testing/specification/causal_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,14 @@ def check_iv_assumptions(self, treatment, outcome, instrument) -> bool:
)

# (iii) Instrument and outcome do not share causes
if any(
(
cause
for cause in self.graph.nodes
if len(list(nx.all_simple_paths(self.graph, source=cause, target=instrument))) > 0
and len(list(nx.all_simple_paths(self.graph, source=cause, target=outcome))) > 0
)
):
raise ValueError(f"Instrument {instrument} and outcome {outcome} share common causes")

for cause in self.graph.nodes:
if cause not in (instrument, outcome): # exclude self-cycles due to breaking changes in NetworkX > 3.2
instrument_paths = list(nx.all_simple_paths(self.graph, source=cause, target=instrument))
outcome_paths = list(nx.all_simple_paths(self.graph, source=cause, target=outcome))
if len(instrument_paths) > 0 and len(outcome_paths) > 0:
print(cause, instrument, instrument_paths, outcome, outcome_paths)
raise ValueError(f"Instrument {instrument} and outcome {outcome} share common causes")
return True

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

0 comments on commit adbfb72

Please sign in to comment.