Graph
Graph data structures.
- class NxMixedGraph(directed: ~networkx.classes.digraph.DiGraph = <factory>, undirected: ~networkx.classes.graph.Graph = <factory>)[source]
A mixed graph based on a
networkx.Graph
and anetworkx.DiGraph
.Example usage:
graph = NxMixedGraph() graph.add_directed_edge('X', 'Y') graph.add_undirected_edge('X', 'Y')
- raise_on_counterfactual() None [source]
Raise an error if this is a counterfactual graph.
- Raises:
ValueError – if this graph is a counterfactual graph
- add_directed_edge(u: str | Variable, v: str | Variable, **attr) None [source]
Add a directed edge from u to v.
- add_undirected_edge(u: str | Variable, v: str | Variable, **attr) None [source]
Add an undirected edge between u and v.
- to_pgmpy_bayesian_network() pgmpy.models.BayesianNetwork [source]
Convert a mixed graph to an equivalent
pgmpy.BayesianNetwork
.
- to_pgmpy_causal_inference() pgmpy.inference.CausalInference.CausalInference [source]
Get a pgmpy causal inference object.
- classmethod from_admg(admg) NxMixedGraph [source]
Create from an ananke ADMG.
- to_latent_variable_dag(*, prefix: str | None = None, start: int = 0, tag: str | None = None) DiGraph [source]
Create a labeled DAG where bi-directed edges are assigned as nodes upstream of their two incident nodes.
- Parameters:
prefix – The prefix for latent variables. If none, defaults to
y0.graph.DEFAULT_PREFIX
.start – The starting number for latent variables (defaults to 0, could be changed to 1 if desired)
tag – The key for node data describing whether it is latent. If None, defaults to
y0.graph.DEFAULT_TAG
.
- Returns:
A latent variable DAG.
- classmethod from_latent_variable_dag(graph: DiGraph, tag: str | None = None) NxMixedGraph [source]
Load a labeled DAG.
- to_causaleffect() Any [source]
Get a causaleffect R object.
- Returns:
A causaleffect R object.
Warning
Appropriate R imports need to be done first for ‘causaleffect’ and ‘igraph’.
- joint() MultiGraph [source]
Return a joint graph.
- moralize()[source]
Moralize the graph.
- Returns:
A moralized ADMG in which all nodes \(U\) and \(v\) that are parents of some node \(N\) are connected with an undirected edge.
- draw(ax=None, title: str | None = None, prog: str | None = None, latex: bool = True) None [source]
Render the graph using matplotlib.
- Parameters:
ax – Axis to draw on (if none specified, makes a new one)
title – The optional title to show with the graph
prog – The pydot program to use, like dot, neato, osage, etc. If none is given, uses osage for small graphs and dot for larger ones.
latex – Parse string variables as y0 if possible to make pretty latex output
- classmethod from_causaleffect(graph) NxMixedGraph [source]
Construct an instance from a causaleffect R graph.
- classmethod from_edges(nodes: Iterable[Variable] | None = None, directed: Iterable[Tuple[Variable, Variable]] | None = None, undirected: Iterable[Tuple[Variable, Variable]] | None = None) NxMixedGraph [source]
Make a mixed graph from a pair of edge lists.
- classmethod from_str_edges(nodes: Iterable[str] | None = None, directed: Iterable[Tuple[str, str]] | None = None, undirected: Iterable[Tuple[str, str]] | None = None) NxMixedGraph [source]
Make a mixed graph from a pair of edge lists where nodes are strings.
- classmethod from_adj(nodes: Iterable[Variable] | None = None, directed: Mapping[Variable, Collection[Variable]] | None = None, undirected: Mapping[Variable, Collection[Variable]] | None = None) NxMixedGraph [source]
Make a mixed graph from a pair of adjacency lists.
- classmethod from_str_adj(nodes: Iterable[str] | None = None, directed: Mapping[str, Collection[str]] | None = None, undirected: Mapping[str, Collection[str]] | None = None) NxMixedGraph [source]
Make a mixed graph from a pair of adjacency lists of strings.
- classmethod from_causalfusion_path(file) NxMixedGraph [source]
Load a graph from a CausalFusion JSON file.
- classmethod from_causalfusion_json(data: Mapping[str, Any]) NxMixedGraph [source]
Load a graph from a CausalFusion JSON object.
- subgraph(vertices: Variable | Iterable[Variable]) NxMixedGraph [source]
Return a subgraph given a set of vertices.
- Parameters:
vertices – a subset of nodes
- Returns:
A NxMixedGraph subgraph
- remove_in_edges(vertices: Variable | Iterable[Variable]) NxMixedGraph [source]
Return a mutilated graph given a set of interventions.
- Parameters:
vertices – a subset of nodes from which to remove incoming edges
- Returns:
A NxMixedGraph subgraph
- get_intervened_ancestors(interventions, outcomes) Set[Variable] [source]
Get the ancestors of outcomes in a graph that has been intervened on.
- Parameters:
interventions – a set of interventions in the graph
outcomes – a set of outcomes in the graph
- Returns:
Set of nodes
- get_no_effect_on_outcomes(interventions, outcomes) Set[Variable] [source]
Find nodes in the graph which have no effect on the outcomes.
- Parameters:
interventions – a set of interventions in the graph
outcomes – a set of outcomes in the graph
- Returns:
Set of nodes
- remove_nodes_from(vertices: Variable | Iterable[Variable]) NxMixedGraph [source]
Return a subgraph that does not contain any of the specified vertices.
- Parameters:
vertices – a set of nodes to remove from graph
- Returns:
A NxMixedGraph subgraph
- remove_out_edges(vertices: Variable | Iterable[Variable]) NxMixedGraph [source]
Return a subgraph that does not have any outgoing edges from any of the given vertices.
- Parameters:
vertices – a set of nodes whose outgoing edges get removed from the graph
- Returns:
NxMixedGraph subgraph
- ancestors_inclusive(sources: Variable | Iterable[Variable]) set[Variable] [source]
Ancestors of a set include the set itself.
- descendants_inclusive(sources: Variable | Iterable[Variable]) set[Variable] [source]
Descendants of a set include the set itself.
- topological_sort() Iterable[Variable] [source]
Get a topological sort from the directed component of the mixed graph.
- get_c_components() list[frozenset[Variable]] [source]
Get the co-components (i.e., districts) in the undirected portion of the graph.
- is_connected() bool [source]
Return if there is only a single connected component in the undirected graph.
- intervene(variables: Set[Intervention]) NxMixedGraph [source]
Intervene on the given variables.
- Parameters:
variables – A set of interventions
- Returns:
A graph that has been intervened on the given variables, with edges into the intervened nodes removed
- get_markov_pillow(nodes: Collection[Variable]) Set[Variable] [source]
For each district, intervene on the domain of each parent not in the district.
- get_markov_blanket(nodes: Variable | Iterable[Variable]) Set[Variable] [source]
Get the Markov blanket for a set of nodes.
The Markov blanket in a directed graph is the union of the parents, children, and parents of children of a given node.
- Parameters:
nodes – A node or nodes to get the Markov blanket from
- Returns:
A set of variables comprising the Markov blanket
- pre(nodes: Variable | Iterable[Variable], topological_sort_order: Sequence[Variable] | None = None) list[Variable] [source]
Find all nodes prior to the given set of nodes under a topological sort order.
- Parameters:
nodes – iterable of nodes.
topological_sort_order – A valid topological sort order. If none given, calculates from the graph.
- Returns:
list corresponding to the order up until the given nodes. This does not include any of the nodes from the query.
- DEFULT_PREFIX = 'u_'
The default prefix for latent variables in a latent variable DAG represented. After the prefix, there will be a number assigned that’s incremented during construction.
- DEFAULT_TAG = 'hidden'
The default key in a latent variable DAG represented as a
networkx.DiGraph
for nodes that correspond to “latent” variables