Utils¶
Graph Tools¶
sort_edge(u: Any, v: Any) -> tuple[Any, Any]
¶
Sort u and v in an edge (u, v) such that the smaller element comes first.
If u and v are not comparable, the are sorted such the the object with the smaller object id comes first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Any
|
The first node. |
required |
v
|
Any
|
The second node. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, Any]
|
The sorted edge as a tuple. |
Source code in tralda/utils/graph_tools.py
build_adjacency_matrix(graph: nx.Graph | nx.DiGraph) -> tuple[np.ndarray, dict[Any, int]]
¶
Return an adjacency matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph | DiGraph
|
A graph. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
An adjacency matrix representing the provided graph. |
dict[Any, int]
|
A dict mapping each node to its row/column index. |
Source code in tralda/utils/graph_tools.py
graphs_equal(graph1: nx.Graph | nx.DiGraph, graph2: nx.Graph | nx.DiGraph) -> bool
¶
Returns whether two NetworkX graphs (directed or undirected) are equal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph1
|
Graph | DiGraph
|
The first graph. |
required |
graph2
|
Graph | DiGraph
|
The second graph. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the two graphs have the same nodes and edges, False otherwise. |
Source code in tralda/utils/graph_tools.py
is_subgraph(graph1: nx.Graph | nx.DiGraph, graph2: nx.Graph | nx.DiGraph) -> bool
¶
Returns whether graph1 is a subgraph of graph2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph1
|
Graph | DiGraph
|
The first graph. |
required |
graph2
|
Graph | DiGraph
|
The second graph. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if graph1 is a subgraph of graph2, False otherwise. |
Source code in tralda/utils/graph_tools.py
symmetric_diff(graph1: nx.Graph | nx.DiGraph, graph2: nx.Graph | nx.DiGraph) -> int
¶
Returns the number of edges in the symmetric difference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph1
|
Graph | DiGraph
|
The first graph. |
required |
graph2
|
Graph | DiGraph
|
The second graph. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of edges in the symmetric difference. |
Source code in tralda/utils/graph_tools.py
contingency_table(true_graph: nx.Graph | nx.DiGraph, graph: nx.Graph | nx.DiGraph, as_dict: bool = True) -> tuple[int, int, int, int] | dict[str, int]
¶
Contingency table for the edge sets of two graphs.
The two graphs must have the same vertex set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
true_graph
|
Graph | DiGraph
|
The 'true' graph. |
required |
graph
|
Graph | DiGraph
|
The graph whose edges are compared against the 'true' graph. |
required |
as_dict
|
bool
|
If True, the numbers of true positives, true negatives, false positives, and false negatives are returned as a dictionary. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int] | dict[str, int]
|
The true positives, true negatives, false positives, and false negatives. Optionally, as a dictionary with keys 'tp', 'tn', 'fp', and 'fn'. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the graph do not have the same set of vertices. |
Source code in tralda/utils/graph_tools.py
performance(true_graph: nx.Graph | nx.DiGraph, graph: nx.Graph | nx.DiGraph) -> tuple[int, int, int, int, int, int, float, float, float]
¶
Returns various metrics for the comparison of a graph against a reference graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
true_graph
|
Graph | DiGraph
|
The 'true' graph. |
required |
graph
|
Graph | DiGraph
|
The graph whose edges are compared against the 'true' graph. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int, int, int, float, float, float]
|
Order, size, tp, tn, fp, fn, accuracy, precision and recall of a (directed or undirected) graph w.r.t. 'true' graph. |
Source code in tralda/utils/graph_tools.py
false_edges(true_graph: nx.Graph | nx.DiGraph, graph: nx.Graph | nx.DiGraph) -> tuple[nx.Graph, nx.Graph] | tuple[nx.DiGraph, nx.DiGraph]
¶
Returns a graph containing false-negative and a graph containing false-positive edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
true_graph
|
Graph | DiGraph
|
The 'true' graph. |
required |
graph
|
Graph | DiGraph
|
The graph whose edges are compared against the 'true' graph. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Graph, Graph] | tuple[DiGraph, DiGraph]
|
A graph containing false-negative and a graph containing false-positive edges. |
Source code in tralda/utils/graph_tools.py
is_properly_colored(graph: nx.Graph | nx.DiGraph, color_attribute: str = 'color') -> bool
¶
Returns whether a (di)graph is properly colored.
A graph is properly color if, for any edge uv, the vertices u and v have different colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph | DiGraph
|
The input graph whose vertices should have some color attribute. |
required |
color_attribute
|
str
|
The vertex attribute that shall be used as color. |
'color'
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether or not the graph is properly colored. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If, in any edge, a vertex does not have the color attribute. |
Source code in tralda/utils/graph_tools.py
sort_by_colors(graph: nx.Graph | nx.DiGraph, color_attribute: str = 'color') -> dict[Any, list[Any]]
¶
Sort the vertices of a graph by color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph | DiGraph
|
The input graph whose vertices should have some color attribute. |
required |
color_attribute
|
str
|
The vertex attribute that shall be used as color. |
'color'
|
Returns:
| Type | Description |
|---|---|
dict[Any, list[Any]]
|
A dictionary with the colors as keys and lists of corresponding vertices as values. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If any vertex does not have the color attribute. |
Source code in tralda/utils/graph_tools.py
copy_node_attributes(from_graph: nx.Graph | nx.DiGraph, to_graph: nx.Graph | nx.DiGraph, attributes: str | tuple[str, ...] | list[str] = ('label', 'color')) -> None
¶
Copy node attributes from one graph to another.
By default, the 'label' and 'color' attributes are copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_graph
|
Graph | DiGraph
|
The source graph from which to copy node attributes. |
required |
to_graph
|
Graph | DiGraph
|
The target graph to which to copy node attributes. |
required |
attributes
|
str | tuple[str, ...] | list[str]
|
The attributes to copy. |
('label', 'color')
|
Source code in tralda/utils/graph_tools.py
random_graph(number_of_nodes: int, p: float = 0.5) -> nx.Graph
¶
Construct a random graph with the specified number of nodes.
The nodes are numbered continuously from 1 to number_of_nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_nodes
|
int
|
The number of nodes that the graph shall have. |
required |
p
|
float
|
Probability that an edge xy is inserted. |
0.5
|
Returns:
| Type | Description |
|---|---|
Graph
|
The generated random graph. |
Source code in tralda/utils/graph_tools.py
disturb_graph(graph: nx.Graph | nx.DiGraph, insertion_prob: float, deletion_prob: float, inplace: bool = False, preserve_properly_colored: bool = True, color_attribute: str = 'color') -> nx.Graph | nx.DiGraph
¶
Randomly insert and/or delete edges in a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph | DiGraph
|
The input graph. |
required |
insertion_prob
|
float
|
The probability with which a missing edge gets inserted. |
required |
deletion_prob
|
float
|
The probability with which a present edge gets deleted. |
required |
inplace
|
bool
|
If True, manipulate the graph inplace. Otherwise, a copy of the graph gets manipulated and returned. |
False
|
preserve_properly_colored
|
bool
|
Whether to preserve the property that the graph is properly colored. |
True
|
color_attribute
|
str
|
The vertex attribute that shall be used as color. |
'color'
|
Returns:
| Type | Description |
|---|---|
Graph | DiGraph
|
The disturbed graph. |
Source code in tralda/utils/graph_tools.py
independent_sets(graph: nx.Graph) -> list[list[Any]] | None
¶
Independent sets of a complete multipartite (i.e., a Fitch graph).
Returns a partition of the graph's vertex set that corresponds to its set of independent set if the graph is complete multipartite, and None otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The input graph. |
required |
Returns:
| Type | Description |
|---|---|
list[list[Any]] | None
|
A list of lists that represent the independent set. |
Source code in tralda/utils/graph_tools.py
complete_multipartite_graph_from_sets(partition: Sequence[Collection[Any]]) -> nx.Graph
¶
Construct the complete multipartite graphs from the independent sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partition
|
Sequence[Collection[Any]]
|
A partition of the vertices for the graph to construct. The partition represents the independent sets of the graph. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
A complete multipartite graph whose independent sets are the sets in the input partition. |
Source code in tralda/utils/graph_tools.py
Tree Tools¶
assert_leaf_sets_equal(trees: Collection[Tree]) -> set[Any] | None
¶
Checks whether trees have the same set of unique leaf labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trees
|
Collection[Tree]
|
Collection of Tree instances. |
required |
Returns:
| Type | Description |
|---|---|
set[Any] | None
|
The common set of leaf labels; or None if the trees do not share the same set of leaf labels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input collection is empty, contains empty trees, or the leaf labels are not unique in one tree. |
TypeError
|
If any of the input elements is not of type |