Skip to content

Commit

Permalink
made find_adc public and added tests
Browse files Browse the repository at this point in the history
Signed-off-by: “Aryan <“aryanroy5678@gmail.com”>
  • Loading branch information
“Aryan committed Aug 8, 2023
1 parent c1003d4 commit 5473559
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pywhy_graphs/algorithms/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"set_nodes_as_latent_confounders",
"is_valid_mec_graph",
"inducing_path",
"valid_mag",
"find_adc" "valid_mag",
]


Expand Down Expand Up @@ -607,7 +607,7 @@ def inducing_path(G, node_x: Node, node_y: Node, L: Set = None, S: Set = None):
return (path_exists, path)


def _find_adc(G):
def find_adc(G):

"""Finds an Almost Directed Cycles in a mixed edge graph.
Expand Down Expand Up @@ -652,6 +652,7 @@ def valid_mag(G: ADMG, L: set = None, S: set = None):
A boolean indicating whether the provided graph is a valid MAG or not.
"""

if L is None:
L = set()

Expand All @@ -670,7 +671,7 @@ def valid_mag(G: ADMG, L: set = None, S: set = None):
pass

# check if there are any almost directed cycles
if _find_adc(G): # if there is an ADC, it's not a valid MAG
if find_adc(G): # if there is an ADC, it's not a valid MAG
return False

# check if there are any inducing paths between non-adjacent nodes
Expand Down
27 changes: 27 additions & 0 deletions pywhy_graphs/algorithms/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,30 @@ def test_is_collider():
S = {"A"}

assert pywhy_graphs.inducing_path(admg, "Z", "Y", L, S)[0]


def test_find_adc():
# K -> H -> Z -> X -> Y -> J <- K
admg = ADMG()
admg.add_edge("Z", "X", admg.directed_edge_name)
admg.add_edge("X", "Y", admg.directed_edge_name)
admg.add_edge("Y", "J", admg.directed_edge_name)
admg.add_edge("H", "Z", admg.directed_edge_name)
admg.add_edge("K", "H", admg.directed_edge_name)
admg.add_edge("K", "J", admg.directed_edge_name)

assert not pywhy_graphs.find_adc(
admg
) # there is neither a directed nor an almost directed cycle

# K -> H -> Z -> X -> Y -> J <-> K
admg = ADMG()
admg.add_edge("Z", "X", admg.directed_edge_name)
admg.add_edge("X", "Y", admg.directed_edge_name)
admg.add_edge("Y", "J", admg.directed_edge_name)
admg.add_edge("H", "Z", admg.directed_edge_name)
admg.add_edge("K", "H", admg.directed_edge_name)
admg.add_edge("Y", "H", admg.directed_edge_name)
admg.add_edge("K", "J", admg.bidirected_edge_name)

assert pywhy_graphs.find_adc(admg)

0 comments on commit 5473559

Please sign in to comment.