Graph Module

PyBNesian 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

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 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 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.UndirectedGraph

Undirected graph.

static Complete(nodes: List[str]) pybnesian.UndirectedGraph

Creates a complete UndirectedGraph with the specified nodes.

Parameters

nodes – Nodes of the UndirectedGraph.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.UndirectedGraph) -> None

Creates a UndirectedGraph without nodes or edges.

  1. __init__(self: pybnesian.UndirectedGraph, nodes: List[str]) -> None

Creates an UndirectedGraph with the specified nodes and without edges.

Parameters

nodes – Nodes of the UndirectedGraph.

  1. __init__(self: pybnesian.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.

  1. __init__(self: pybnesian.UndirectedGraph, nodes: List[str], edges: List[Tuple[str, str]]) -> None

Creates an UndirectedGraph with the specified nodes and edges.

Parameters
add_edge(self: pybnesian.UndirectedGraph, n1: int or str, n2: int or str) None

Adds an edge between the nodes n1 and n2.

n1 and n2 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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.UndirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.UndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.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.UndirectedGraph, n1: int or str, n2: int or str) bool

Checks whether an edge between the nodes n1 and n2 exists.

n1 and n2 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.UndirectedGraph, n1: int or str, n2: int or str) bool

Checks whether there is an undirected path between nodes n1 and n2.

n1 and n2 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 and n2, False otherwise.

index(self: pybnesian.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.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.UndirectedGraph) Dict[str, int]

Gets all the indices in the graph.

Returns

A dictionary with the index of each node.

is_valid(self: pybnesian.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.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.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.UndirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_edges(self: pybnesian.UndirectedGraph) int

Gets the number of edges.

Returns

Number of edges.

num_neighbors(self: pybnesian.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.UndirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

remove_edge(self: pybnesian.UndirectedGraph, n1: int or str, n2: int or str) None

Removes an edge between the nodes n1 and n2.

n1 and n2 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.UndirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

save(self: pybnesian.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.UndirectedGraph) pybnesian.UndirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.DirectedGraph

Directed graph that may contain cycles.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.DirectedGraph) -> None

Creates a DirectedGraph without nodes or arcs.

  1. __init__(self: pybnesian.DirectedGraph, nodes: List[str]) -> None

Creates a DirectedGraph with the specified nodes and without arcs.

Parameters

nodes – Nodes of the DirectedGraph.

  1. __init__(self: pybnesian.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.

  1. __init__(self: pybnesian.DirectedGraph, nodes: List[str], arcs: List[Tuple[str, str]]) -> None

Creates a DirectedGraph with the specified nodes and arcs.

Parameters
add_arc(self: pybnesian.DirectedGraph, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.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.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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.DirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.DirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.DirectedGraph, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.DirectedGraph, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target exists.

source and target 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.DirectedGraph, n1: int or str, n2: int or str) bool

Checks whether there is a directed path between nodes n1 and n2.

n1 and n2 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 and n2, False otherwise.

index(self: pybnesian.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.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.DirectedGraph) Dict[str, int]

Gets all the indices in the graph.

Returns

A dictionary with the index of each node.

is_leaf(self: pybnesian.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.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.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.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.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.DirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_arcs(self: pybnesian.DirectedGraph) int

Gets the number of arcs.

Returns

Number of arcs.

num_children(self: pybnesian.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.DirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

num_parents(self: pybnesian.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.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.DirectedGraph, source: int or str, target: int or str) None

Removes an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.DirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

roots(self: pybnesian.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.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.DirectedGraph) pybnesian.DirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.Dag

Bases: DirectedGraph

Directed acyclic graph.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.Dag) -> None

Creates a Dag without nodes or arcs.

  1. __init__(self: pybnesian.Dag, nodes: List[str]) -> None

Creates a Dag with the specified nodes and without arcs.

Parameters

nodes – Nodes of the Dag.

  1. __init__(self: pybnesian.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.

  1. __init__(self: pybnesian.Dag, nodes: List[str], arcs: List[Tuple[str, str]]) -> None

Creates a Dag with the specified nodes and arcs.

Parameters
  • nodes – Nodes of the Dag.

  • arcs – Arcs of the Dag.

add_arc(self: pybnesian.Dag, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.Dag, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target can be added. That is, the arc is valid and do not generate a cycle.

source and target 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.Dag, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target can be flipped. That is, the flipped arc is valid and do not generate a cycle. If the arc source -> target do not exist, it will return Dag.can_add_arc().

source and target 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.

  1. conditional_graph(self: pybnesian.Dag) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.Dag, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.Dag, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.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.Dag) pybnesian.PartiallyDirectedGraph

Gets the PartiallyDirectedGraph (PDAG) that represents the equivalence class of this Dag.

It implements the DAG-to-PDAG algorithm in [dag2pdag]. See also [dag2pdag_extra].

Returns

A PartiallyDirectedGraph that represents the equivalence class of this Dag.

topological_sort(self: pybnesian.Dag) List[str]

Gets the topological sort of the DAG.

Returns

Topological sort as a list of nodes.

unconditional_graph(self: pybnesian.Dag) pybnesian.Dag

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.PartiallyDirectedGraph

Partially directed graph. This graph can have edges and arcs.

static CompleteUndirected(nodes: List[str]) pybnesian.PartiallyDirectedGraph

Creates a PartiallyDirectedGraph that is a complete undirected graph.

Parameters

nodes – Nodes of the PartiallyDirectedGraph.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.PartiallyDirectedGraph) -> None

Creates a PartiallyDirectedGraph without nodes, arcs and edges.

  1. __init__(self: pybnesian.PartiallyDirectedGraph, nodes: List[str]) -> None

Creates a PartiallyDirectedGraph with the specified nodes and without arcs and edges.

Parameters

nodes – Nodes of the PartiallyDirectedGraph.

  1. __init__(self: pybnesian.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
  1. __init__(self: pybnesian.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
add_arc(self: pybnesian.PartiallyDirectedGraph, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.PartiallyDirectedGraph, n1: int or str, n2: int or str) None

Adds an edge between the nodes n1 and n2.

n1 and n2 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.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.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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.PartiallyDirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.PartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.PartiallyDirectedGraph, source: int or str, target: int or str) None

Transformation to create the arc source -> target when possible.

  • If there is an edge sourcetarget, it is transformed into an arc source -> target.

  • If there is an arc target -> source, it is flipped into an arc source -> target.

  • Else, the graph is left unaffected.

source and target 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.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.PartiallyDirectedGraph, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.PartiallyDirectedGraph, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target exists.

source and target 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.PartiallyDirectedGraph, source: int or str, target: int or str) bool

Checks whether two nodes source and target are connected.

Two nodes source and target are connected if there is an edge sourcetarget, or an arc source -> target or an arc target -> source.

source and target 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 and target are connected, False otherwise.

has_edge(self: pybnesian.PartiallyDirectedGraph, n1: int or str, n2: int or str) bool

Checks whether an edge between the nodes n1 and n2 exists.

n1 and n2 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.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.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.PartiallyDirectedGraph) Dict[str, int]

Gets all the indices in the graph.

Returns

A dictionary with the index of each node.

is_leaf(self: pybnesian.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.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.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.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.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.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.PartiallyDirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_arcs(self: pybnesian.PartiallyDirectedGraph) int

Gets the number of arcs.

Returns

Number of arcs.

num_children(self: pybnesian.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.PartiallyDirectedGraph) int

Gets the number of edges.

Returns

Number of edges.

num_neighbors(self: pybnesian.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.PartiallyDirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

num_parents(self: pybnesian.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.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.PartiallyDirectedGraph, source: int or str, target: int or str) None

Removes an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.PartiallyDirectedGraph, n1: int or str, n2: int or str) None

Removes an edge between the nodes n1 and n2.

n1 and n2 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.PartiallyDirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

roots(self: pybnesian.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.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.PartiallyDirectedGraph) pybnesian.Dag

Gets a Dag approximate extension of self. This method can be useful when PartiallyDirectedGraph.to_dag() cannot 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 of self.

to_dag(self: pybnesian.PartiallyDirectedGraph) pybnesian.Dag

Gets a Dag which belongs to the equivalence class of self.

It implements the algorithm in [pdag2dag].

Returns

A Dag which belongs to the equivalence class of self.

Raises

ValueError – If self do not have a valid DAG extension.

unconditional_graph(self: pybnesian.PartiallyDirectedGraph) pybnesian.PartiallyDirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • 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.PartiallyDirectedGraph, source: int or str, target: int or str) None

Transformation to create the edge sourcetarget when possible.

  • If there is not an arc target -> source, converts the arc source -> target into an edge sourcetarget. If there is not an arc source -> target, it adds the edge sourcetarget.

  • Else, the graph is left unaffected

source and target 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 cannot 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.ConditionalUndirectedGraph

Conditional undirected graph.

static Complete(nodes: List[str], interface_nodes: List[str]) pybnesian.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
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.ConditionalUndirectedGraph) -> None

Creates a ConditionalUndirectedGraph without nodes or edges.

  1. __init__(self: pybnesian.ConditionalUndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None

Creates a ConditionalUndirectedGraph with the specified nodes, interface_nodes and without edges.

Parameters
  1. __init__(self: pybnesian.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
add_edge(self: pybnesian.ConditionalUndirectedGraph, n1: int or str, n2: int or str) None

Adds an edge between the nodes n1 and n2.

n1 and n2 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.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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.ConditionalUndirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.ConditionalUndirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.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.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.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.ConditionalUndirectedGraph, n1: int or str, n2: int or str) bool

Checks whether an edge between the nodes n1 and n2 exists.

n1 and n2 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.ConditionalUndirectedGraph, n1: int or str, n2: int or str) bool

Checks whether there is an undirected path between nodes n1 and n2.

n1 and n2 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 and n2, False otherwise.

index(self: pybnesian.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.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.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.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.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.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.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.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.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.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 and node is a normal node.

interface_nodes(self: pybnesian.ConditionalUndirectedGraph) List[str]

Gets the interface nodes of the graph.

Returns

Interface nodes of the graph.

is_interface(self: pybnesian.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.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.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.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.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.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.ConditionalUndirectedGraph) List[str]

Gets the joint set of nodes of the graph.

Returns

Joint set of nodes of the graph.

name(self: pybnesian.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.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.ConditionalUndirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_edges(self: pybnesian.ConditionalUndirectedGraph) int

Gets the number of edges.

Returns

Number of edges.

num_interface_nodes(self: pybnesian.ConditionalUndirectedGraph) int

Gets the number of interface nodes.

Returns

Number of interface nodes.

num_joint_nodes(self: pybnesian.ConditionalUndirectedGraph) int

Gets the number of joint nodes. That is, num_nodes() + num_interface_nodes()

Returns

Number of joint nodes.

num_neighbors(self: pybnesian.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.ConditionalUndirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

remove_edge(self: pybnesian.ConditionalUndirectedGraph, n1: int or str, n2: int or str) None

Removes an edge between the nodes n1 and n2.

n1 and n2 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.ConditionalUndirectedGraph, node: int or str) None

Removes an interface node.

Parameters

node – A node name or index.

remove_node(self: pybnesian.ConditionalUndirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

save(self: pybnesian.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.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.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.ConditionalUndirectedGraph) pybnesian.UndirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.ConditionalDirectedGraph

Conditional directed graph.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.ConditionalDirectedGraph) -> None

Creates a ConditionalDirectedGraph without nodes or arcs.

  1. __init__(self: pybnesian.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None

Creates a ConditionalDirectedGraph with the specified nodes, interface_nodes and without arcs.

Parameters
  1. __init__(self: pybnesian.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str], arcs: List[Tuple[str, str]]) -> None

Creates a ConditionalDirectedGraph with the specified nodes and arcs.

Parameters
add_arc(self: pybnesian.ConditionalDirectedGraph, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.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.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.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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.ConditionalDirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.ConditionalDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.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.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.ConditionalDirectedGraph, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.ConditionalDirectedGraph, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target exists.

source and target 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.ConditionalDirectedGraph, n1: int or str, n2: int or str) bool

Checks whether there is a directed path between nodes n1 and n2.

n1 and n2 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 and n2, False otherwise.

index(self: pybnesian.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.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.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.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.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.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.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.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.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.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.ConditionalDirectedGraph) List[str]

Gets the interface nodes of the graph.

Returns

Interface nodes of the graph.

is_interface(self: pybnesian.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.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.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.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.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.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.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.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.ConditionalDirectedGraph) List[str]

Gets the joint set of nodes of the graph.

Returns

Joint set of nodes of the graph.

leaves(self: pybnesian.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.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.ConditionalDirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_arcs(self: pybnesian.ConditionalDirectedGraph) int

Gets the number of arcs.

Returns

Number of arcs.

num_children(self: pybnesian.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.ConditionalDirectedGraph) int

Gets the number of interface nodes.

Returns

Number of interface nodes.

num_joint_nodes(self: pybnesian.ConditionalDirectedGraph) int

Gets the number of joint nodes. That is, num_nodes() + num_interface_nodes()

Returns

Number of joint nodes.

num_nodes(self: pybnesian.ConditionalDirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

num_parents(self: pybnesian.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.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.ConditionalDirectedGraph, source: int or str, target: int or str) None

Removes an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.ConditionalDirectedGraph, node: int or str) None

Removes an interface node.

Parameters

node – A node name or index.

remove_node(self: pybnesian.ConditionalDirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

roots(self: pybnesian.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.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.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.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.ConditionalDirectedGraph) pybnesian.DirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.ConditionalDag

Bases: ConditionalDirectedGraph

Conditional directed acyclic graph.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.ConditionalDag) -> None

Creates a ConditionalDag without nodes or arcs.

  1. __init__(self: pybnesian.ConditionalDag, nodes: List[str], interface_nodes: List[str]) -> None

Creates a ConditionalDag with the specified nodes, interface_nodes and without arcs.

Parameters
  1. __init__(self: pybnesian.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
add_arc(self: pybnesian.ConditionalDag, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.ConditionalDag, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target can be added. That is, the arc is valid and do not generate a cycle or connects two interface nodes.

source and target 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.ConditionalDag, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target can be flipped. That is, the flipped arc is valid and do not generate a cycle. If the arc source -> target do not exist, it will return ConditionalDag.can_add_arc().

source and target 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.

  1. conditional_graph(self: pybnesian.ConditionalDag) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.ConditionalDag, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.ConditionalDag, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.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.ConditionalDag) pybnesian.ConditionalPartiallyDirectedGraph

Gets the ConditionalPartiallyDirectedGraph (PDAG) that represents the equivalence class of this ConditionalDag.

It implements the DAG-to-PDAG algorithm in [dag2pdag]. See also [dag2pdag_extra].

Returns

A ConditionalPartiallyDirectedGraph that represents the equivalence class of this ConditionalDag.

topological_sort(self: pybnesian.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.ConditionalDag) pybnesian.Dag

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • If self is conditional, the interface nodes are included as nodes in the returned graph.

Returns

The unconditional graph transformation of self.

class pybnesian.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.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
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.ConditionalPartiallyDirectedGraph) -> None

Creates a ConditionalPartiallyDirectedGraph without nodes or arcs.

  1. __init__(self: pybnesian.ConditionalPartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> None

Creates a ConditionalPartiallyDirectedGraph with the specified nodes, interface_nodes and without edges.

Parameters
  1. __init__(self: pybnesian.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
add_arc(self: pybnesian.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) None

Adds an arc between the nodes source and target. If the arc already exists, the graph is left unaffected.

source and target 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.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) None

Adds an edge between the nodes n1 and n2.

n1 and n2 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.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.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.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.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.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.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.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.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.

  1. conditional_graph(self: pybnesian.ConditionalPartiallyDirectedGraph) -> pybnesian.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 of self.

Returns

The conditional graph transformation of self.

  1. conditional_graph(self: pybnesian.ConditionalPartiallyDirectedGraph, nodes: List[str], interface_nodes: List[str]) -> pybnesian.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.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.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.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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) None

Transformation to create the arc source -> target when possible.

  • If there is an edge sourcetarget, it is transformed into an arc source -> target.

  • If there is an arc target -> source, it is flipped into an arc source -> target.

  • Else, the graph is left unaffected.

source and target 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.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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) None

Flips (reverses) an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) bool

Checks whether an arc between the nodes source and target exists.

source and target 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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) bool

Checks whether two nodes source and target are connected.

Two nodes source and target are connected if there is an edge sourcetarget, or an arc source -> target or an arc target -> source.

source and target 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 and target are connected, False otherwise.

has_edge(self: pybnesian.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) bool

Checks whether an edge between the nodes n1 and n2 exists.

n1 and n2 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.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.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.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.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.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.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.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.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.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.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.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 and node is a normal node.

interface_nodes(self: pybnesian.ConditionalPartiallyDirectedGraph) List[str]

Gets the interface nodes of the graph.

Returns

Interface nodes of the graph.

is_interface(self: pybnesian.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.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.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.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.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.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.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.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.ConditionalPartiallyDirectedGraph) List[str]

Gets the joint set of nodes of the graph.

Returns

Joint set of nodes of the graph.

leaves(self: pybnesian.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.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.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.ConditionalPartiallyDirectedGraph) List[str]

Gets the nodes of the graph.

Returns

Nodes of the graph.

num_arcs(self: pybnesian.ConditionalPartiallyDirectedGraph) int

Gets the number of arcs.

Returns

Number of arcs.

num_children(self: pybnesian.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.ConditionalPartiallyDirectedGraph) int

Gets the number of edges.

Returns

Number of edges.

num_interface_nodes(self: pybnesian.ConditionalPartiallyDirectedGraph) int

Gets the number of interface nodes.

Returns

Number of interface nodes.

num_joint_nodes(self: pybnesian.ConditionalPartiallyDirectedGraph) int

Gets the number of joint nodes. That is, num_nodes() + num_interface_nodes()

Returns

Number of joint nodes.

num_neighbors(self: pybnesian.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.ConditionalPartiallyDirectedGraph) int

Gets the number of nodes.

Returns

Number of nodes.

num_parents(self: pybnesian.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.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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) None

Removes an arc between the nodes source and target. If the arc do not exist, the graph is left unaffected.

source and target 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.ConditionalPartiallyDirectedGraph, n1: int or str, n2: int or str) None

Removes an edge between the nodes n1 and n2.

n1 and n2 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.ConditionalPartiallyDirectedGraph, node: int or str) None

Removes an interface node.

Parameters

node – A node name or index.

remove_node(self: pybnesian.ConditionalPartiallyDirectedGraph, node: int or str) None

Removes a node.

Parameters

node – A node name or index.

roots(self: pybnesian.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.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.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.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.ConditionalPartiallyDirectedGraph) pybnesian.ConditionalDag

Gets a Dag approximate extension of self. This method can be useful when ConditionalPartiallyDirectedGraph.to_dag() cannot 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 of self.

to_dag(self: pybnesian.ConditionalPartiallyDirectedGraph) pybnesian.ConditionalDag

Gets a Dag which belongs to the equivalence class of self.

It implements the algorithm in [pdag2dag].

Returns

A Dag which belongs to the equivalence class of self.

Raises

ValueError – If self do not have a valid DAG extension.

unconditional_graph(self: pybnesian.ConditionalPartiallyDirectedGraph) pybnesian.PartiallyDirectedGraph

Transforms the graph to an unconditional graph.

  • If self is not conditional, it returns a copy of self.

  • 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.ConditionalPartiallyDirectedGraph, source: int or str, target: int or str) None

Transformation to create the edge sourcetarget when possible.

  • If there is not an arc target -> source, converts the arc source -> target into an edge sourcetarget. If there is not an arc source -> target, it adds the edge sourcetarget.

  • Else, the graph is left unaffected

source and target 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.