-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add PAG class - add error checks to CPDAG and PAG when adding edges and constructing graph - add algorithm to check validity of a CPDAG and / or PAG class - Update poetry to networkx version on PR branch `mixededge` by Adam Li
- Loading branch information
Showing
15 changed files
with
692 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,6 @@ graphs encountered in the literature. | |
.. autosummary:: | ||
:toctree: generated/ | ||
|
||
CPDAG | ||
ADMG | ||
CPDAG | ||
PAG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from ._version import __version__ # noqa: F401 | ||
from .classes import ADMG, CPDAG | ||
from .classes import ADMG, CPDAG, PAG | ||
from .algorithms import is_valid_mec_graph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .graph import * # noqa: F403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from itertools import combinations | ||
from typing import Union | ||
|
||
import networkx as nx | ||
|
||
import pywhy_graphs as pgraph | ||
|
||
|
||
def is_valid_mec_graph(G: Union[pgraph.PAG, pgraph.CPDAG], on_error: str = "raise") -> bool: | ||
"""Check G is a valid PAG. | ||
A valid CPDAG/PAG is one where each pair of nodes have | ||
at most one edge between them. | ||
Parameters | ||
---------- | ||
G : pgraph.PAG | pgraph.CPDAG | ||
The PAG or CPDAG. | ||
on_error : str | ||
Whether to raise an error if the graph is non-compliant. Default is 'raise'. | ||
Other options are 'ignore'. | ||
Returns | ||
------- | ||
bool | ||
Whether G is a valid PAG or CPDAG. | ||
""" | ||
for node1, node2 in combinations(G.nodes, 2): | ||
n_edges = 0 | ||
names = [] | ||
for name, graph in G.get_graphs().items(): | ||
if (node1, node2) in graph.edges or (node2, node1) in graph.edges: | ||
n_edges += 1 | ||
names.append(name) | ||
if n_edges > 1: | ||
if on_error == "raise": | ||
raise RuntimeError( | ||
f"There is more than one edge between ({node1}, {node2}) in the " | ||
f"edge types: {names}. Please fix the construction of the PAG." | ||
) | ||
return False | ||
|
||
# the directed edges should not form cycles | ||
if not nx.is_directed_acyclic_graph(G.sub_directed_graph()): | ||
if on_error == "raise": | ||
raise RuntimeError(f"{G} is not a DAG, which it should be.") | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .admg import ADMG | ||
from .cpdag import CPDAG | ||
from .pag import PAG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from enum import Enum, EnumMeta | ||
|
||
|
||
class MetaEnum(EnumMeta): | ||
def __contains__(cls, item): | ||
try: | ||
cls(item) | ||
except ValueError: | ||
return False | ||
return True | ||
|
||
|
||
class EdgeType(Enum, metaclass=MetaEnum): | ||
"""Enumeration of different causal edge endpoints. | ||
Categories | ||
---------- | ||
directed : str | ||
Signifies arrowhead ("->") edges. | ||
circle : str | ||
Signifies a circle ("*-o") endpoint. That is an uncertain edge, | ||
which is either circle with directed edge (``o->``), | ||
circle with undirected edge (``o-``), or | ||
circle with circle edge (``o-o``). | ||
undirected : str | ||
Signifies an undirected ("-") edge. That is an undirected edge (``-``), | ||
or circle with circle edge (``-o``). | ||
Notes | ||
----- | ||
The possible edges between two nodes thus are: | ||
->, <-, <->, o->, <-o, o-o | ||
In general, among all possible causal graphs, arrowheads depict | ||
non-descendant relationships. In DAGs, arrowheads depict direct | ||
causal relationships (i.e. parents/children). In ADMGs, arrowheads | ||
can come from directed edges, or bidirected edges | ||
""" | ||
|
||
ALL = "all" | ||
DIRECTED = "directed" | ||
BIDIRECTED = "bidirected" | ||
CIRCLE = "circle" | ||
UNDIRECTED = "undirected" | ||
|
||
|
||
class EndPoint(Enum, metaclass=MetaEnum): | ||
"""Enumeration of different causal edge endpoints. | ||
Categories | ||
---------- | ||
arrow : str | ||
Signifies arrowhead (">") endpoint. That is a normal | ||
directed edge (``->``), bidirected arrow (``<->``), | ||
or circle with directed edge (``o->``). | ||
circle : str | ||
Signifies a circle ("o") endpoint. That is an uncertain edge, | ||
which is either circle with directed edge (``o->``), | ||
circle with undirected edge (``o-``), or | ||
circle with circle edge (``o-o``). | ||
tail : str | ||
Signifies a tail ("-") endpoint. That is either | ||
a directed edge (``->``), or an undirected edge (``-``), or | ||
circle with circle edge (``-o``). | ||
Notes | ||
----- | ||
The possible edges between two nodes thus are: | ||
->, <-, <->, o->, <-o, o-o | ||
In general, among all possible causal graphs, arrowheads depict | ||
non-descendant relationships. In DAGs, arrowheads depict direct | ||
causal relationships (i.e. parents/children). In ADMGs, arrowheads | ||
can come from directed edges, or bidirected edges | ||
""" | ||
|
||
arrow = "arrow" | ||
circle = "circle" | ||
tail = "tail" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.