Graph Module¶
The pybnesian.graph submodule includes different types of graphs. There are four types of graphs:
Undirected graphs.
Directed graphs.
Directed acyclic graphs (DAGs).
Partially directed graphs.
Depending on the type of edges: directed edges (arcs) or undirected edges (edges).
Each graph type has two variants:
Graphs. See Graphs.
Conditional graphs. See Conditional Graphs.
Graphs¶
All the nodes in the graph are represented by a name and are associated with a non-negative unique index.
The name can be obtained from the unique index using the method name()
, while the unique index can be obtained
from the index using the method index()
.
Removing a node invalidates the index of the removed node, while leaving the other nodes unaffected. When adding a node, the graph may reuse previously invalidated indices to avoid wasting too much memory.
If there are not removal of nodes in a graph, the unique indices are in the range [0-num_nodes()
). The removal of
nodes, can lead to some indices being greater or equal to num_nodes()
:
>>> from pybnesian.graph import UndirectedGraph
>>> g = UndirectedGraph(["a", "b", "c", "d"])
>>> g.index("a")
0
>>> g.index("b")
1
>>> g.index("c")
2
>>> g.index("d")
3
>>> g.remove_node("a")
>>> g.index("b")
1
>>> g.index("c")
2
>>> g.index("d")
3
>>> assert g.index("d") >= g.num_nodes()
Sometimes, this effect may be undesirable because we want to identify our nodes with a index
in a range [0-num_nodes()
). For this reason, there is a collapsed_index()
method and other related
methods index_from_collapsed()
, collapsed_from_index()
and collapsed_name()
.
Note that the collapsed index is not unique, because removing a node can change the collapsed index of at most
one other node.
>>> from pybnesian.graph import UndirectedGraph
>>> g = UndirectedGraph(["a", "b", "c", "d"])
>>> g.collapsed_index("a")
0
>>> g.collapsed_index("b")
1
>>> g.collapsed_index("c")
2
>>> g.collapsed_index("d")
3
>>> g.remove_node("a")
>>> g.collapsed_index("b")
1
>>> g.collapsed_index("c")
2
>>> g.collapsed_index("d")
0
>>> assert all([g.collapsed_index(n) < g.num_nodes() for n in g.nodes()])
- class pybnesian.graph.UndirectedGraph¶
Undirected graph.
- static Complete(nodes: List[str]) → pybnesian.graph.UndirectedGraph¶
Creates a complete
UndirectedGraph
with the specified nodes.- Parameters
nodes – Nodes of the
UndirectedGraph
.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.UndirectedGraph) -> None
Creates a
UndirectedGraph
without nodes or edges.__init__(self: pybnesian.graph.UndirectedGraph, nodes: List[str]) -> None
Creates an
UndirectedGraph
with the specified nodes and without edges.- Parameters
nodes – Nodes of the
UndirectedGraph
.
__init__(self: pybnesian.graph.UndirectedGraph, edges: List[Tuple[str, str]]) -> None
Creates an
UndirectedGraph
with the specified edges (the nodes are extracted from the edges).- Parameters
edges – Edges of the
UndirectedGraph
.
__init__(self: pybnesian.graph.UndirectedGraph, nodes: List[str], edges: List[Tuple[str, str]]) -> None
Creates an
UndirectedGraph
with the specified nodes and edges.- Parameters
nodes – Nodes of the
UndirectedGraph
.edges – Edges of the
UndirectedGraph
.
- add_edge(self: pybnesian.graph.UndirectedGraph, n1: int or str, n2: int or str) → None¶
Adds an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- add_node(self: pybnesian.graph.UndirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- collapsed_from_index(self: pybnesian.graph.UndirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.UndirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.UndirectedGraph) → Dict[str, int]¶
Gets the collapsed indices in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.UndirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.UndirectedGraph) -> pybnesian.graph.ConditionalUndirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.UndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalUndirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_node(self: pybnesian.graph.UndirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- edges(self: pybnesian.graph.UndirectedGraph) → List[Tuple[str, str]]¶
Gets the list of edges.
- Returns
A list of tuples (n1, n2) representing an edge between n1 and n2.
- has_edge(self: pybnesian.graph.UndirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether an edge between the nodes
n1
andn2
exists.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if the edge exists, False otherwise.
- has_path(self: pybnesian.graph.UndirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether there is an undirected path between nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if there is an undirected path between
n1
andn2
, False otherwise.
- index(self: pybnesian.graph.UndirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.UndirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.UndirectedGraph) → Dict[str, int]¶
Gets all the indices in the graph.
- Returns
A dictionary with the index of each node.
- is_valid(self: pybnesian.graph.UndirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- name(self: pybnesian.graph.UndirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- neighbors(self: pybnesian.graph.UndirectedGraph, node: int or str) → List[str]¶
Gets the neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Neighbor names.
- nodes(self: pybnesian.graph.UndirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_edges(self: pybnesian.graph.UndirectedGraph) → int¶
Gets the number of edges.
- Returns
Number of edges.
- num_neighbors(self: pybnesian.graph.UndirectedGraph, node: int or str) → int¶
Gets the number of neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Number of neighbors.
- num_nodes(self: pybnesian.graph.UndirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- remove_edge(self: pybnesian.graph.UndirectedGraph, n1: int or str, n2: int or str) → None¶
Removes an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- remove_node(self: pybnesian.graph.UndirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- save(self: pybnesian.graph.UndirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- unconditional_graph(self: pybnesian.graph.UndirectedGraph) → pybnesian.graph.UndirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.DirectedGraph¶
Directed graph that may contain cycles.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.DirectedGraph) -> None
Creates a
DirectedGraph
without nodes or arcs.__init__(self: pybnesian.graph.DirectedGraph, nodes: List[str]) -> None
Creates a
DirectedGraph
with the specified nodes and without arcs.- Parameters
nodes – Nodes of the
DirectedGraph
.
__init__(self: pybnesian.graph.DirectedGraph, arcs: List[Tuple[str, str]]) -> None
Creates a
DirectedGraph
with the specified arcs (the nodes are extracted from the arcs).- Parameters
arcs – Arcs of the
DirectedGraph
.
__init__(self: pybnesian.graph.DirectedGraph, nodes: List[str], arcs: List[Tuple[str, str]]) -> None
Creates a
DirectedGraph
with the specified nodes and arcs.- Parameters
nodes – Nodes of the
DirectedGraph
.arcs – Arcs of the
DirectedGraph
.
- add_arc(self: pybnesian.graph.DirectedGraph, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- add_node(self: pybnesian.graph.DirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- arcs(self: pybnesian.graph.DirectedGraph) → List[Tuple[str, str]]¶
Gets the list of arcs.
- Returns
A list of tuples (source, target) representing an arc source -> target.
- children(self: pybnesian.graph.DirectedGraph, node: int or str) → List[str]¶
Gets the children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Children node names.
- collapsed_from_index(self: pybnesian.graph.DirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.DirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.DirectedGraph) → Dict[str, int]¶
Gets the collapsed indices in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.DirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.DirectedGraph) -> pybnesian.graph.ConditionalDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.DirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_node(self: pybnesian.graph.DirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- flip_arc(self: pybnesian.graph.DirectedGraph, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- has_arc(self: pybnesian.graph.DirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
exists.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc exists, False otherwise.
- has_path(self: pybnesian.graph.DirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether there is a directed path between nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if there is an directed path between
n1
andn2
, False otherwise.
- index(self: pybnesian.graph.DirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.DirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.DirectedGraph) → Dict[str, int]¶
Gets all the indices in the graph.
- Returns
A dictionary with the index of each node.
- is_leaf(self: pybnesian.graph.DirectedGraph, node: int or str) → bool¶
Checks whether
node
is a leaf node. A root node do not have children nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is leaf, False otherwise.
- is_root(self: pybnesian.graph.DirectedGraph, node: int or str) → bool¶
Checks whether
node
is a root node. A root node do not have parent nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is root, False otherwise.
- is_valid(self: pybnesian.graph.DirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- leaves(self: pybnesian.graph.DirectedGraph) → Set[str]¶
Gets the leaf nodes of the graph. A leaf node do not have children nodes.
- Returns
The set of leaf nodes.
- name(self: pybnesian.graph.DirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- nodes(self: pybnesian.graph.DirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_arcs(self: pybnesian.graph.DirectedGraph) → int¶
Gets the number of arcs.
- Returns
Number of arcs.
- num_children(self: pybnesian.graph.DirectedGraph, node: int or str) → int¶
Gets the number of children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of children nodes.
- num_nodes(self: pybnesian.graph.DirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- num_parents(self: pybnesian.graph.DirectedGraph, node: int or str) → int¶
Gets the number of parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of parent nodes.
- parents(self: pybnesian.graph.DirectedGraph, node: int or str) → List[str]¶
Gets the parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Parent node names.
- remove_arc(self: pybnesian.graph.DirectedGraph, source: int or str, target: int or str) → None¶
Removes an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- remove_node(self: pybnesian.graph.DirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- roots(self: pybnesian.graph.DirectedGraph) → Set[str]¶
Gets the root nodes of the graph. A root node do not have parent nodes.
- Returns
The set of root nodes.
- save(self: pybnesian.graph.DirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- unconditional_graph(self: pybnesian.graph.DirectedGraph) → pybnesian.graph.DirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.Dag¶
Bases:
pybnesian.graph.DirectedGraph
Directed acyclic graph.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.Dag) -> None
Creates a
Dag
without nodes or arcs.__init__(self: pybnesian.graph.Dag, nodes: List[str]) -> None
Creates a
Dag
with the specified nodes and without arcs.- Parameters
nodes – Nodes of the
Dag
.
__init__(self: pybnesian.graph.Dag, arcs: List[Tuple[str, str]]) -> None
Creates a
Dag
with the specified arcs (the nodes are extracted from the arcs).- Parameters
arcs – Arcs of the
Dag
.
__init__(self: pybnesian.graph.Dag, nodes: List[str], arcs: List[Tuple[str, str]]) -> None
Creates a
Dag
with the specified nodes and arcs.
- add_arc(self: pybnesian.graph.Dag, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- can_add_arc(self: pybnesian.graph.Dag, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
can be added. That is, the arc is valid and do not generate a cycle.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc can be added, False otherwise.
- can_flip_arc(self: pybnesian.graph.Dag, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
can be flipped. That is, the flipped arc is valid and do not generate a cycle. If the arcsource
->target
do not exist, it will returnDag.can_add_arc()
.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc can be flipped, False otherwise.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.Dag) -> pybnesian.graph.ConditionalDag
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.Dag, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalDag
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- flip_arc(self: pybnesian.graph.Dag, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- save(self: pybnesian.graph.Dag, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- to_pdag(self: pybnesian.graph.Dag) → pybnesian.graph.PartiallyDirectedGraph¶
Gets the
PartiallyDirectedGraph
(PDAG) that represents the equivalence class of thisDag
.It implements the DAG-to-PDAG algorithm in [dag2pdag]. See also [dag2pdag_extra].
- Returns
A
PartiallyDirectedGraph
that represents the equivalence class of thisDag
.
- topological_sort(self: pybnesian.graph.Dag) → List[str]¶
Gets the topological sort of the DAG.
- Returns
Topological sort as a list of nodes.
- unconditional_graph(self: pybnesian.graph.Dag) → pybnesian.graph.Dag¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.PartiallyDirectedGraph¶
Partially directed graph. This graph can have edges and arcs.
- static CompleteUndirected(nodes: List[str]) → pybnesian.graph.PartiallyDirectedGraph¶
Creates a
PartiallyDirectedGraph
that is a complete undirected graph.- Parameters
nodes – Nodes of the
PartiallyDirectedGraph
.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.PartiallyDirectedGraph) -> None
Creates a
PartiallyDirectedGraph
without nodes, arcs and edges.__init__(self: pybnesian.graph.PartiallyDirectedGraph, nodes: List[str]) -> None
Creates a
PartiallyDirectedGraph
with the specified nodes and without arcs and edges.- Parameters
nodes – Nodes of the
PartiallyDirectedGraph
.
__init__(self: pybnesian.graph.PartiallyDirectedGraph, arcs: List[Tuple[str, str]], edges: List[Tuple[str, str]]) -> None
Creates a
PartiallyDirectedGraph
with the specified arcs and edges (the nodes are extracted from the arcs and edges).- Parameters
arcs – Arcs of the
PartiallyDirectedGraph
.edges – Edges of the
PartiallyDirectedGraph
.
__init__(self: pybnesian.graph.PartiallyDirectedGraph, nodes: List[str], arcs: List[Tuple[str, str]], edges: List[Tuple[str, str]]) -> None
Creates a
PartiallyDirectedGraph
with the specified nodes and arcs.- Parameters
nodes – Nodes of the
PartiallyDirectedGraph
.arcs – Arcs of the
PartiallyDirectedGraph
.edges – Edges of the
PartiallyDirectedGraph
.
- add_arc(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- add_edge(self: pybnesian.graph.PartiallyDirectedGraph, n1: int or str, n2: int or str) → None¶
Adds an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- add_node(self: pybnesian.graph.PartiallyDirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- arcs(self: pybnesian.graph.PartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the list of arcs.
- Returns
A list of tuples (source, target) representing an arc source -> target.
- children(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Children node names.
- collapsed_from_index(self: pybnesian.graph.PartiallyDirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.PartiallyDirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.PartiallyDirectedGraph) → Dict[str, int]¶
Gets the collapsed indices in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.PartiallyDirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.PartiallyDirectedGraph) -> pybnesian.graph.ConditionalPartiallyDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.PartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalPartiallyDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_node(self: pybnesian.graph.PartiallyDirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- direct(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Transformation to create the arc
source
->target
when possible.If there is an edge
source
–target
, it is transformed into an arcsource
->target
.If there is an arc
target
->source
, it is flipped into an arcsource
->target
.Else, the graph is left unaffected.
source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- edges(self: pybnesian.graph.PartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the list of edges.
- Returns
A list of tuples (n1, n2) representing an edge between n1 and n2.
- flip_arc(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- has_arc(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
exists.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc exists, False otherwise.
- has_connection(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether two nodes
source
andtarget
are connected.Two nodes
source
andtarget
are connected if there is an edgesource
–target
, or an arcsource
->target
or an arctarget
->source
.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if
source
andtarget
are connected, False otherwise.
- has_edge(self: pybnesian.graph.PartiallyDirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether an edge between the nodes
n1
andn2
exists.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if the edge exists, False otherwise.
- index(self: pybnesian.graph.PartiallyDirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.PartiallyDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.PartiallyDirectedGraph) → Dict[str, int]¶
Gets all the indices in the graph.
- Returns
A dictionary with the index of each node.
- is_leaf(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a leaf node. A root node do not have children nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is leaf, False otherwise.
- is_root(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a root node. A root node do not have parent nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is root, False otherwise.
- is_valid(self: pybnesian.graph.PartiallyDirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- leaves(self: pybnesian.graph.PartiallyDirectedGraph) → Set[str]¶
Gets the leaf nodes of the graph. A leaf node do not have children nodes.
- Returns
The set of leaf nodes.
- name(self: pybnesian.graph.PartiallyDirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- neighbors(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Neighbor names.
- nodes(self: pybnesian.graph.PartiallyDirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_arcs(self: pybnesian.graph.PartiallyDirectedGraph) → int¶
Gets the number of arcs.
- Returns
Number of arcs.
- num_children(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → int¶
Gets the number of children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of children nodes.
- num_edges(self: pybnesian.graph.PartiallyDirectedGraph) → int¶
Gets the number of edges.
- Returns
Number of edges.
- num_neighbors(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → int¶
Gets the number of neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Number of neighbors.
- num_nodes(self: pybnesian.graph.PartiallyDirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- num_parents(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → int¶
Gets the number of parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of parent nodes.
- parents(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Parent node names.
- remove_arc(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Removes an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- remove_edge(self: pybnesian.graph.PartiallyDirectedGraph, n1: int or str, n2: int or str) → None¶
Removes an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- remove_node(self: pybnesian.graph.PartiallyDirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- roots(self: pybnesian.graph.PartiallyDirectedGraph) → Set[str]¶
Gets the root nodes of the graph. A root node do not have parent nodes.
- Returns
The set of root nodes.
- save(self: pybnesian.graph.PartiallyDirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- to_approximate_dag(self: pybnesian.graph.PartiallyDirectedGraph) → pybnesian.graph.Dag¶
Gets a
Dag
approximate extension ofself
. This method can be useful whenPartiallyDirectedGraph.to_dag()
can not return a valid extension.The algorithm is based on generating a topological sort which tries to preserve a similar structure.
- Returns
A
Dag
approximate extension ofself
.
- to_dag(self: pybnesian.graph.PartiallyDirectedGraph) → pybnesian.graph.Dag¶
Gets a
Dag
which belongs to the equivalence class ofself
.It implements the algorithm in [pdag2dag].
- Returns
A
Dag
which belongs to the equivalence class ofself
.- Raises
ValueError – If
self
do not have a valid DAG extension.
- unconditional_graph(self: pybnesian.graph.PartiallyDirectedGraph) → pybnesian.graph.PartiallyDirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- undirect(self: pybnesian.graph.PartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Transformation to create the edge
source
–target
when possible.If there is not an arc
target
->source
, converts the arcsource
->target
into an edgesource
–target
. If there is not an arcsource
->target
, it adds the edgesource
–target
.Else, the graph is left unaffected
source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
Conditional Graphs¶
A conditional graph is the underlying graph in a conditional Bayesian networks ([PGM], Section 5.6). In a conditional Bayesian network, only the normal nodes can have a conditional probability density, while the interface nodes are always observed. A conditional graph splits all the nodes in two subsets: normal nodes and interface nodes. In a conditional graph, the interface nodes can not have parents.
In a conditional graph, normal nodes can be returned with nodes()
, the interface nodes with
interface_nodes()
and the joint set of nodes with joint_nodes()
. Also, there are many other functions
that have the prefix interface
and joint
to denote the interface and joint sets of nodes. Among them, there is
a collapsed index version for only interface nodes, interface_collapsed_index()
, and the joint set of nodes,
joint_collapsed_index()
. Note that the collapsed index for each set of nodes is independent.
- class pybnesian.graph.ConditionalUndirectedGraph¶
Conditional undirected graph.
- static Complete(nodes: List[str], interface_nodes: List[str]) → pybnesian.graph.ConditionalUndirectedGraph¶
Creates a complete
ConditionalUndirectedGraph
with the specified nodes. A complete conditional undirected graph connects every pair of nodes with an edge, except for pairs of interface nodes.- Parameters
nodes – Nodes of the
ConditionalUndirectedGraph
.interface_nodes – Interface nodes of the
ConditionalUndirectedGraph
.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.ConditionalUndirectedGraph) -> None
Creates a
ConditionalUndirectedGraph
without nodes or edges.__init__(self: pybnesian.graph.ConditionalUndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None
Creates a
ConditionalUndirectedGraph
with the specified nodes, interface_nodes and without edges.- Parameters
nodes – Nodes of the
ConditionalUndirectedGraph
.interface_nodes – Interface nodes of the
ConditionalUndirectedGraph
.
__init__(self: pybnesian.graph.ConditionalUndirectedGraph, nodes: List[str], interface_nodes: List[str], edges: List[Tuple[str, str]]) -> None
Creates a
ConditionalUndirectedGraph
with the specified nodes, interface_nodes and edges.- Parameters
nodes – Nodes of the
ConditionalUndirectedGraph
.interface_nodes – Interface nodes of the
ConditionalUndirectedGraph
.edges – Edges of the
ConditionalUndirectedGraph
.
- add_edge(self: pybnesian.graph.ConditionalUndirectedGraph, n1: int or str, n2: int or str) → None¶
Adds an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- add_interface_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Adds an interface node to the graph and returns its index.
- Parameters
node – Name of the new interface node.
- Returns
Index of the new interface node.
- add_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- collapsed_from_index(self: pybnesian.graph.ConditionalUndirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.ConditionalUndirectedGraph) → Dict[str, int]¶
Gets all the collapsed indices for the nodes in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.ConditionalUndirectedGraph) -> pybnesian.graph.ConditionalUndirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.ConditionalUndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalUndirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_interface_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → bool¶
Tests whether the interface node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the interface node, False otherwise.
- contains_joint_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → bool¶
Tests whether the node is in the joint set of nodes or not.
- Parameters
node – Name of the node.
- Returns
True if the node is in the joint set of nodes, False otherwise.
- contains_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- edges(self: pybnesian.graph.ConditionalUndirectedGraph) → List[Tuple[str, str]]¶
Gets the list of edges.
- Returns
A list of tuples (n1, n2) representing an edge between n1 and n2.
- has_edge(self: pybnesian.graph.ConditionalUndirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether an edge between the nodes
n1
andn2
exists.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if the edge exists, False otherwise.
- has_path(self: pybnesian.graph.ConditionalUndirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether there is an undirected path between nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if there is an undirected path between
n1
andn2
, False otherwise.
- index(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- index_from_interface_collapsed(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the interface collapsed index.
- Parameters
collapsed_index – Interface collapsed index of the node.
- Returns
Index of the node.
- index_from_joint_collapsed(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.ConditionalUndirectedGraph) → Dict[str, int]¶
Gets all the indices for the nodes in the graph.
- Returns
A dictionary with the index of each node.
- interface_collapsed_from_index(self: pybnesian.graph.ConditionalUndirectedGraph, index: int) → int¶
Gets the interface collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Interface collapsed index of the node.
- interface_collapsed_index(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Gets the interface collapsed index of an interface node from its name.
- Parameters
node – Name of the interface node.
- Returns
Interface collapsed index of the interface node.
- interface_collapsed_indices(self: pybnesian.graph.ConditionalUndirectedGraph) → Dict[str, int]¶
Gets all the interface collapsed indices for the interface nodes in the graph.
- Returns
A dictionary with the interface collapsed index of each interface node.
- interface_collapsed_name(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → str¶
Gets the name of an interface node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the interface node.
- Returns
Name of the interface node.
- interface_edges(self: pybnesian.graph.ConditionalUndirectedGraph) → List[Tuple[str, str]]¶
Gets the edges where one of the nodes is an interface node.
- Returns
edges as a list of tuples (inode, node), where
inode
is an interface node andnode
is a normal node.
- interface_nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → List[str]¶
Gets the interface nodes of the graph.
- Returns
Interface nodes of the graph.
- is_interface(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → bool¶
Checks whether the
node
is an interface node.- Parameters
node – A node name or index.
- Returns
True if
node
is interface node, False, otherwise.
- is_valid(self: pybnesian.graph.ConditionalUndirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- joint_collapsed_from_index(self: pybnesian.graph.ConditionalUndirectedGraph, index: int) → int¶
Gets the joint collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_index(self: pybnesian.graph.ConditionalUndirectedGraph, node: str) → int¶
Gets the joint collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_indices(self: pybnesian.graph.ConditionalUndirectedGraph) → Dict[str, int]¶
Gets all the joint collapsed indices for the joint set of nodes in the graph.
- Returns
A dictionary with the joint collapsed index of each joint node.
- joint_collapsed_name(self: pybnesian.graph.ConditionalUndirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Name of the node.
- joint_nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → List[str]¶
Gets the joint set of nodes of the graph.
- Returns
Joint set of nodes of the graph.
- name(self: pybnesian.graph.ConditionalUndirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- neighbors(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → List[str]¶
Gets the neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Neighbor names.
- nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_edges(self: pybnesian.graph.ConditionalUndirectedGraph) → int¶
Gets the number of edges.
- Returns
Number of edges.
- num_interface_nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → int¶
Gets the number of interface nodes.
- Returns
Number of interface nodes.
- num_joint_nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → int¶
Gets the number of joint nodes. That is,
num_nodes() + num_interface_nodes()
- Returns
Number of joint nodes.
- num_neighbors(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → int¶
Gets the number of neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Number of neighbors.
- num_nodes(self: pybnesian.graph.ConditionalUndirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- remove_edge(self: pybnesian.graph.ConditionalUndirectedGraph, n1: int or str, n2: int or str) → None¶
Removes an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- remove_interface_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → None¶
Removes an interface node.
- Parameters
node – A node name or index.
- remove_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- save(self: pybnesian.graph.ConditionalUndirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- set_interface(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → None¶
Converts a normal node into an interface node.
- Parameters
node – A node name or index.
- set_node(self: pybnesian.graph.ConditionalUndirectedGraph, node: int or str) → None¶
Converts an interface node into a normal node.
- Parameters
node – A node name or index.
- unconditional_graph(self: pybnesian.graph.ConditionalUndirectedGraph) → pybnesian.graph.UndirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.ConditionalDirectedGraph¶
Conditional directed graph.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.ConditionalDirectedGraph) -> None
Creates a
ConditionalDirectedGraph
without nodes or arcs.__init__(self: pybnesian.graph.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None
Creates a
ConditionalDirectedGraph
with the specified nodes, interface_nodes and without arcs.- Parameters
nodes – Nodes of the
ConditionalDirectedGraph
.interface_nodes – Interface nodes of the
ConditionalDirectedGraph
.
__init__(self: pybnesian.graph.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str], arcs: List[Tuple[str, str]]) -> None
Creates a
ConditionalDirectedGraph
with the specified nodes and arcs.- Parameters
nodes – Nodes of the
ConditionalDirectedGraph
.interface_nodes – Interface nodes of the
ConditionalDirectedGraph
.arcs – Arcs of the
ConditionalDirectedGraph
.
- add_arc(self: pybnesian.graph.ConditionalDirectedGraph, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- add_interface_node(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Adds an interface node to the graph and returns its index.
- Parameters
node – Name of the new interface node.
- Returns
Index of the new interface node.
- add_node(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- arcs(self: pybnesian.graph.ConditionalDirectedGraph) → List[Tuple[str, str]]¶
Gets the list of arcs.
- Returns
A list of tuples (source, target) representing an arc source -> target.
- children(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → List[str]¶
Gets the children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Children node names.
- collapsed_from_index(self: pybnesian.graph.ConditionalDirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.ConditionalDirectedGraph) → Dict[str, int]¶
Gets all the collapsed indices for the nodes in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.ConditionalDirectedGraph) -> pybnesian.graph.ConditionalDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_interface_node(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → bool¶
Tests whether the interface node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the interface node, False otherwise.
- contains_joint_node(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → bool¶
Tests whether the node is in the joint set of nodes or not.
- Parameters
node – Name of the node.
- Returns
True if the node is in the joint set of nodes, False otherwise.
- contains_node(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- flip_arc(self: pybnesian.graph.ConditionalDirectedGraph, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- has_arc(self: pybnesian.graph.ConditionalDirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
exists.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc exists, False otherwise.
- has_path(self: pybnesian.graph.ConditionalDirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether there is a directed path between nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if there is an directed path between
n1
andn2
, False otherwise.
- index(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- index_from_interface_collapsed(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the interface collapsed index.
- Parameters
collapsed_index – Interface collapsed index of the node.
- Returns
Index of the node.
- index_from_joint_collapsed(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.ConditionalDirectedGraph) → Dict[str, int]¶
Gets all the indices for the nodes in the graph.
- Returns
A dictionary with the index of each node.
- interface_arcs(self: pybnesian.graph.ConditionalDirectedGraph) → List[Tuple[str, str]]¶
Gets the arcs where the source node is an interface node.
- Returns
arcs with an interface node as source node.
- interface_collapsed_from_index(self: pybnesian.graph.ConditionalDirectedGraph, index: int) → int¶
Gets the interface collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Interface collapsed index of the node.
- interface_collapsed_index(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Gets the interface collapsed index of an interface node from its name.
- Parameters
node – Name of the interface node.
- Returns
Interface collapsed index of the interface node.
- interface_collapsed_indices(self: pybnesian.graph.ConditionalDirectedGraph) → Dict[str, int]¶
Gets all the interface collapsed indices for the interface nodes in the graph.
- Returns
A dictionary with the interface collapsed index of each interface node.
- interface_collapsed_name(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → str¶
Gets the name of an interface node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the interface node.
- Returns
Name of the interface node.
- interface_nodes(self: pybnesian.graph.ConditionalDirectedGraph) → List[str]¶
Gets the interface nodes of the graph.
- Returns
Interface nodes of the graph.
- is_interface(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → bool¶
Checks whether the
node
is an interface node.- Parameters
node – A node name or index.
- Returns
True if
node
is interface node, False, otherwise.
- is_leaf(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a leaf node. A root node do not have children nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is leaf, False otherwise.
- is_root(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a root node. A root node do not have parent nodes.This implementation do not take into account the interface arcs. That is, if a node only have interface nodes as parents, it is considered a root.
- Parameters
node – A node name or index.
- Returns
True if
node
is root, False otherwise.
- is_valid(self: pybnesian.graph.ConditionalDirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- joint_collapsed_from_index(self: pybnesian.graph.ConditionalDirectedGraph, index: int) → int¶
Gets the joint collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_index(self: pybnesian.graph.ConditionalDirectedGraph, node: str) → int¶
Gets the joint collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_indices(self: pybnesian.graph.ConditionalDirectedGraph) → Dict[str, int]¶
Gets all the joint collapsed indices for the joint set of nodes in the graph.
- Returns
A dictionary with the joint collapsed index of each joint node.
- joint_collapsed_name(self: pybnesian.graph.ConditionalDirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Name of the node.
- joint_nodes(self: pybnesian.graph.ConditionalDirectedGraph) → List[str]¶
Gets the joint set of nodes of the graph.
- Returns
Joint set of nodes of the graph.
- leaves(self: pybnesian.graph.ConditionalDirectedGraph) → Set[str]¶
Gets the leaf nodes of the graph. A leaf node do not have children nodes.
This implementation do not include the interface nodes in the result. Thus, this returns the same result as an unconditional graph without the interface nodes.
- Returns
The set of leaf nodes.
- name(self: pybnesian.graph.ConditionalDirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- nodes(self: pybnesian.graph.ConditionalDirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_arcs(self: pybnesian.graph.ConditionalDirectedGraph) → int¶
Gets the number of arcs.
- Returns
Number of arcs.
- num_children(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → int¶
Gets the number of children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of children nodes.
- num_interface_nodes(self: pybnesian.graph.ConditionalDirectedGraph) → int¶
Gets the number of interface nodes.
- Returns
Number of interface nodes.
- num_joint_nodes(self: pybnesian.graph.ConditionalDirectedGraph) → int¶
Gets the number of joint nodes. That is,
num_nodes() + num_interface_nodes()
- Returns
Number of joint nodes.
- num_nodes(self: pybnesian.graph.ConditionalDirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- num_parents(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → int¶
Gets the number of parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of parent nodes.
- parents(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → List[str]¶
Gets the parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Parent node names.
- remove_arc(self: pybnesian.graph.ConditionalDirectedGraph, source: int or str, target: int or str) → None¶
Removes an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- remove_interface_node(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → None¶
Removes an interface node.
- Parameters
node – A node name or index.
- remove_node(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- roots(self: pybnesian.graph.ConditionalDirectedGraph) → Set[str]¶
Gets the root nodes of the graph. A root node do not have parent nodes.
This implementation do not include the interface nodes in the result. Also, do not take into account the interface arcs. That is, if a node only have interface nodes as parents, it is considered a root. Thus, this returns the same result as an unconditional graph without the interface nodes.
- Returns
The set of root nodes.
- save(self: pybnesian.graph.ConditionalDirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- set_interface(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → None¶
Converts a normal node into an interface node.
- Parameters
node – A node name or index.
- set_node(self: pybnesian.graph.ConditionalDirectedGraph, node: int or str) → None¶
Converts an interface node into a normal node.
- Parameters
node – A node name or index.
- unconditional_graph(self: pybnesian.graph.ConditionalDirectedGraph) → pybnesian.graph.DirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.ConditionalDag¶
Bases:
pybnesian.graph.ConditionalDirectedGraph
Conditional directed acyclic graph.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.ConditionalDag) -> None
Creates a
ConditionalDag
without nodes or arcs.__init__(self: pybnesian.graph.ConditionalDag, nodes: List[str], interface_nodes: List[str]) -> None
Creates a
ConditionalDag
with the specified nodes, interface_nodes and without arcs.- Parameters
nodes – Nodes of the
ConditionalDag
.interface_nodes – Interface nodes of the
ConditionalDag
.
__init__(self: pybnesian.graph.ConditionalDag, nodes: List[str], interface_nodes: List[str], arcs: List[Tuple[str, str]]) -> None
Creates a
ConditionalDag
with the specified nodes, interface_nodes and arcs.- Parameters
nodes – Nodes of the
ConditionalDag
.interface_nodes – Interface nod”es of the
ConditionalDag
.arcs – Arcs of the
ConditionalDag
.
- add_arc(self: pybnesian.graph.ConditionalDag, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- can_add_arc(self: pybnesian.graph.ConditionalDag, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
can be added. That is, the arc is valid and do not generate a cycle or connects two interface nodes.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc can be added, False otherwise.
- can_flip_arc(self: pybnesian.graph.ConditionalDag, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
can be flipped. That is, the flipped arc is valid and do not generate a cycle. If the arcsource
->target
do not exist, it will returnConditionalDag.can_add_arc()
.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc can be flipped, False otherwise.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.ConditionalDag) -> pybnesian.graph.ConditionalDag
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.ConditionalDag, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalDag
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- flip_arc(self: pybnesian.graph.ConditionalDag, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- save(self: pybnesian.graph.ConditionalDag, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- to_pdag(self: pybnesian.graph.ConditionalDag) → pybnesian.graph.ConditionalPartiallyDirectedGraph¶
Gets the
ConditionalPartiallyDirectedGraph
(PDAG) that represents the equivalence class of thisConditionalDag
.It implements the DAG-to-PDAG algorithm in [dag2pdag]. See also [dag2pdag_extra].
- Returns
A
ConditionalPartiallyDirectedGraph
that represents the equivalence class of thisConditionalDag
.
- topological_sort(self: pybnesian.graph.ConditionalDag) → List[str]¶
Gets the topological sort of the conditional DAG. This topological sort does not include the interface nodes, since they are known to be always roots (they can be included at the very beginning of the topological sort).
- Returns
Topological sort as a list of nodes.
- unconditional_graph(self: pybnesian.graph.ConditionalDag) → pybnesian.graph.Dag¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- class pybnesian.graph.ConditionalPartiallyDirectedGraph¶
Conditional partially directed graph. This graph can have edges and arcs, except between pairs of interface nodes.
- static CompleteUndirected(nodes: List[str], interface_nodes: List[str]) → pybnesian.graph.ConditionalPartiallyDirectedGraph¶
Creates a
ConditionalPartiallyDirectedGraph
that is a complete undirected graph. A complete conditional undirected graph connects every pair of nodes with an edge, except for pairs of interface nodes.- Parameters
nodes – Nodes of the
ConditionalPartiallyDirectedGraph
.interface_nodes – Interface nodes of the
ConditionalPartiallyDirectedGraph
.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) -> None
Creates a
ConditionalPartiallyDirectedGraph
without nodes or arcs.__init__(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None
Creates a
ConditionalPartiallyDirectedGraph
with the specified nodes, interface_nodes and without edges.- Parameters
nodes – Nodes of the
ConditionalPartiallyDirectedGraph
.interface_nodes – Interface nodes of the
ConditionalPartiallyDirectedGraph
.
__init__(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str], arcs: List[Tuple[str, str]], edges: List[Tuple[str, str]]) -> None
Creates a
ConditionalPartiallyDirectedGraph
with the specified nodes and arcs.- Parameters
nodes – Nodes of the
ConditionalPartiallyDirectedGraph
.interface_nodes – Interface nodes of the
ConditionalPartiallyDirectedGraph
.arcs – Arcs of the
ConditionalPartiallyDirectedGraph
.edges – Edges of the
ConditionalPartiallyDirectedGraph
.
- add_arc(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Adds an arc between the nodes
source
andtarget
. If the arc already exists, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- add_edge(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) → None¶
Adds an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- add_interface_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Adds an interface node to the graph and returns its index.
- Parameters
node – Name of the new interface node.
- Returns
Index of the new interface node.
- add_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Adds a node to the graph and returns its index.
- Parameters
node – Name of the new node.
- Returns
Index of the new node.
- arcs(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the list of arcs.
- Returns
A list of tuples (source, target) representing an arc source -> target.
- children(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Children node names.
- collapsed_from_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, index: int) → int¶
Gets the collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Collapsed index of the node.
- collapsed_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Gets the collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Collapsed index of the node.
- collapsed_indices(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Dict[str, int]¶
Gets all the collapsed indices for the nodes in the graph.
- Returns
A dictionary with the collapsed index of each node.
- collapsed_name(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Name of the node.
- conditional_graph(*args, **kwargs)¶
Overloaded function.
conditional_graph(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) -> pybnesian.graph.ConditionalPartiallyDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the same nodes and without interface nodes.If
self
is conditional, it returns a copy ofself
.
- Returns
The conditional graph transformation of
self
.
conditional_graph(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.graph.ConditionalPartiallyDirectedGraph
Transforms the graph to a conditional graph.
If
self
is not conditional, it returns a conditional version of the graph with the given nodes and interface nodes.If
self
is conditional, it returns the same graph type with the given nodes and interface nodes.
- Parameters
nodes – The nodes for the new conditional graph.
interface_nodes – The interface nodes for the new conditional graph.
- Returns
The conditional graph transformation of
self
.
- contains_interface_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → bool¶
Tests whether the interface node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the interface node, False otherwise.
- contains_joint_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → bool¶
Tests whether the node is in the joint set of nodes or not.
- Parameters
node – Name of the node.
- Returns
True if the node is in the joint set of nodes, False otherwise.
- contains_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → bool¶
Tests whether the node is in the graph or not.
- Parameters
node – Name of the node.
- Returns
True if the graph contains the node, False otherwise.
- direct(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Transformation to create the arc
source
->target
when possible.If there is an edge
source
–target
, it is transformed into an arcsource
->target
.If there is an arc
target
->source
, it is flipped into an arcsource
->target
.Else, the graph is left unaffected.
source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- edges(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the list of edges.
- Returns
A list of tuples (n1, n2) representing an edge between n1 and n2.
- flip_arc(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Flips (reverses) an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- has_arc(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether an arc between the nodes
source
andtarget
exists.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if the arc exists, False otherwise.
- has_connection(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → bool¶
Checks whether two nodes
source
andtarget
are connected.Two nodes
source
andtarget
are connected if there is an edgesource
–target
, or an arcsource
->target
or an arctarget
->source
.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- Returns
True if
source
andtarget
are connected, False otherwise.
- has_edge(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) → bool¶
Checks whether an edge between the nodes
n1
andn2
exists.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- Returns
True if the edge exists, False otherwise.
- index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Gets the index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Index of the node.
- index_from_collapsed(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the node.
- Returns
Index of the node.
- index_from_interface_collapsed(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the interface collapsed index.
- Parameters
collapsed_index – Interface collapsed index of the node.
- Returns
Index of the node.
- index_from_joint_collapsed(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → int¶
Gets the index of a node from the joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Index of the node.
- indices(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Dict[str, int]¶
Gets all the indices for the nodes in the graph.
- Returns
A dictionary with the index of each node.
- interface_arcs(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the arcs where the source node is an interface node.
- Returns
arcs with an interface node as source node.
- interface_collapsed_from_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, index: int) → int¶
Gets the interface collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Interface collapsed index of the node.
- interface_collapsed_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Gets the interface collapsed index of an interface node from its name.
- Parameters
node – Name of the interface node.
- Returns
Interface collapsed index of the interface node.
- interface_collapsed_indices(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Dict[str, int]¶
Gets all the interface collapsed indices for the interface nodes in the graph.
- Returns
A dictionary with the interface collapsed index of each interface node.
- interface_collapsed_name(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → str¶
Gets the name of an interface node from its collapsed index.
- Parameters
collapsed_index – Collapsed index of the interface node.
- Returns
Name of the interface node.
- interface_edges(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[Tuple[str, str]]¶
Gets the edges where one of the nodes is an interface node.
- Returns
edges as a list of tuples (inode, node), where
inode
is an interface node andnode
is a normal node.
- interface_nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[str]¶
Gets the interface nodes of the graph.
- Returns
Interface nodes of the graph.
- is_interface(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → bool¶
Checks whether the
node
is an interface node.- Parameters
node – A node name or index.
- Returns
True if
node
is interface node, False, otherwise.
- is_leaf(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a leaf node. A root node do not have children nodes.- Parameters
node – A node name or index.
- Returns
True if
node
is leaf, False otherwise.
- is_root(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → bool¶
Checks whether
node
is a root node. A root node do not have parent nodes.This implementation do not take into account the interface arcs. That is, if a node only have interface nodes as parents, it is considered a root.
- Parameters
node – A node name or index.
- Returns
True if
node
is root, False otherwise.
- is_valid(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, index: int) → bool¶
Checks whether a index is a valid index (the node is not removed). All the valid indices are always returned by
indices()
.- Parameters
index – Index of the node.
- Returns
True if the index is valid, False otherwise.
- joint_collapsed_from_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, index: int) → int¶
Gets the joint collapsed index of a node from its index.
- Parameters
index – Index of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_index(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: str) → int¶
Gets the joint collapsed index of a node from its name.
- Parameters
node – Name of the node.
- Returns
Joint collapsed index of the node.
- joint_collapsed_indices(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Dict[str, int]¶
Gets all the joint collapsed indices for the joint set of nodes in the graph.
- Returns
A dictionary with the joint collapsed index of each joint node.
- joint_collapsed_name(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, collapsed_index: int) → str¶
Gets the name of a node from its joint collapsed index.
- Parameters
collapsed_index – Joint collapsed index of the node.
- Returns
Name of the node.
- joint_nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[str]¶
Gets the joint set of nodes of the graph.
- Returns
Joint set of nodes of the graph.
- leaves(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Set[str]¶
Gets the leaf nodes of the graph. A leaf node do not have children nodes.
This implementation do not include the interface nodes in the result. Thus, this returns the same result as an unconditional graph without the interface nodes.
- Returns
The set of leaf nodes.
- name(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, index: int) → str¶
Gets the name of a node from its index.
- Parameters
index – Index of the node.
- Returns
Name of the node.
- neighbors(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Neighbor names.
- nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → List[str]¶
Gets the nodes of the graph.
- Returns
Nodes of the graph.
- num_arcs(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → int¶
Gets the number of arcs.
- Returns
Number of arcs.
- num_children(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → int¶
Gets the number of children nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of children nodes.
- num_edges(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → int¶
Gets the number of edges.
- Returns
Number of edges.
- num_interface_nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → int¶
Gets the number of interface nodes.
- Returns
Number of interface nodes.
- num_joint_nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → int¶
Gets the number of joint nodes. That is,
num_nodes() + num_interface_nodes()
- Returns
Number of joint nodes.
- num_neighbors(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → int¶
Gets the number of neighbors (adjacent nodes by an edge) of a node.
- Parameters
node – A node name or index.
- Returns
Number of neighbors.
- num_nodes(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → int¶
Gets the number of nodes.
- Returns
Number of nodes.
- num_parents(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → int¶
Gets the number of parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Number of parent nodes.
- parents(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → List[str]¶
Gets the parent nodes of a node.
- Parameters
node – A node name or index.
- Returns
Parent node names.
- remove_arc(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Removes an arc between the nodes
source
andtarget
. If the arc do not exist, the graph is left unaffected.source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
- remove_edge(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) → None¶
Removes an edge between the nodes
n1
andn2
.n1
andn2
can be the name or the index, but the type of n1 and n2 must be the same.- Parameters
n1 – A node name or index.
n2 – A node name or index.
- remove_interface_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → None¶
Removes an interface node.
- Parameters
node – A node name or index.
- remove_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → None¶
Removes a node.
- Parameters
node – A node name or index.
- roots(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → Set[str]¶
Gets the root nodes of the graph. A root node do not have parent nodes.
This implementation do not include the interface nodes in the result. Also, do not take into account the interface arcs. That is, if a node only have interface nodes as parents, it is considered a root. Thus, this returns the same result as an unconditional graph without the interface nodes.
- Returns
The set of root nodes.
- save(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, filename: str) → None¶
Saves the graph in a pickle file with the given name.
- Parameters
filename – File name of the saved graph.
- set_interface(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → None¶
Converts a normal node into an interface node.
- Parameters
node – A node name or index.
- set_node(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, node: int or str) → None¶
Converts an interface node into a normal node.
- Parameters
node – A node name or index.
- to_approximate_dag(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → pybnesian.graph.ConditionalDag¶
Gets a
Dag
approximate extension ofself
. This method can be useful whenConditionalPartiallyDirectedGraph.to_dag()
can not return a valid extension.The algorithm is based on generating a topological sort which tries to preserve a similar structure.
- Returns
A
Dag
approximate extension ofself
.
- to_dag(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → pybnesian.graph.ConditionalDag¶
Gets a
Dag
which belongs to the equivalence class ofself
.It implements the algorithm in [pdag2dag].
- Returns
A
Dag
which belongs to the equivalence class ofself
.- Raises
ValueError – If
self
do not have a valid DAG extension.
- unconditional_graph(self: pybnesian.graph.ConditionalPartiallyDirectedGraph) → pybnesian.graph.PartiallyDirectedGraph¶
Transforms the graph to an unconditional graph.
If
self
is not conditional, it returns a copy ofself
.If
self
is conditional, the interface nodes are included as nodes in the returned graph.
- Returns
The unconditional graph transformation of
self
.
- undirect(self: pybnesian.graph.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) → None¶
Transformation to create the edge
source
–target
when possible.If there is not an arc
target
->source
, converts the arcsource
->target
into an edgesource
–target
. If there is not an arcsource
->target
, it adds the edgesource
–target
.Else, the graph is left unaffected
source
andtarget
can be the name or the index, but the type of source and target must be the same.- Parameters
source – A node name or index.
target – A node name or index.
Bibliography¶
- dag2pdag(1,2)
Chickering, M. (2002). Learning Equivalence Classes of Bayesian-Network Structures. Journal of Machine Learning Research, 2, 445-498.
- dag2pdag_extra(1,2)
Chickering, M. (1995). A Transformational Characterization of Equivalent Bayesian Network Structures. Proceedings of the Eleventh Conference on Uncertainty in Artificial Intelligence (UAI’95), Montreal.
- pdag2dag(1,2)
Dorit, D. and Tarsi, M. (1992). A simple algorithm to construct a consistent extension of a partially oriented graph (Report No: R-185).
- PGM
Koller, D. and Friedman, N. (2009). Probabilistic Graphical Models. MIT press.