Note
Go to the end to download the full example code.
Reduce & Relabel
Homology calculations are only available in OAT for one type of hypergraph: a list of sorted-lists of integers. However we provide tools to translate back and forth between this and other data fromats, e.g. a dictionary of lists of strings.
Below are two examples.
import oat_python as oat
See also
The HypernetX library provides many useful tools for working with hypergraphs, including tools to remove duplicate nodes and edges.
Restricted Barycentric Subdivision (Vietoris-Rips) shows how to relabel nodes/edges, and use proper labels in plotting.
Relabel a hypergraph
Here we assign integer labels to the nodes of a hypergraph whose nodes are labeled with strings.
Define a hypergraph
E = { "A": ["x"], "B": ["y"], "C": ["x","y","z","zz"], "D": ["w","ww","x","y"], "DD": ["w","ww","x","y"] }
Relabel with integers
relabeled_hg, label_translator = oat.hypergraph.relabel(E)
The relabeled hypergraph:
relabeled_hg
[[2], [3], [2, 3, 4, 5], [0, 1, 2, 3], [0, 1, 2, 3]]
The label translator maps the new integer labels back to original labels and vice versa
for key, val in label_translator.items():
print(f"{key}: {val}")
new_node_for_old_node: {'w': 0, 'ww': 1, 'x': 2, 'y': 3, 'z': 4, 'zz': 5}
new_edge_for_old_edge: {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'DD': 4}
old_node_for_new_node: ['w', 'ww', 'x', 'y', 'z', 'zz']
old_edge_for_new_edge: ['A', 'B', 'C', 'D', 'DD']
Relabel and reduce a hypergraph
Here we perform two actions in one step:
reduce the hypergraph by removing duplicate hyperedges (meaning hyperedges with the same vertex set) and duplicate nodes (meaning nodes that belong to the same set of hyperedges)
reformat the reduced hypergraph as a list of sorted-lists of integers
Define a hypergraph
E = { "A": ["x"], "B": ["y"], "C": ["x","y","z","zz"], "D": ["w","ww","x","y"], "DD": ["w","ww","x","y"] }
Collapse out redundant information, returning a list of lists
reduced_hg, label_translator = oat.hypergraph.reduce_hypergraph_with_labels(E)
The reduced hypergraph edges (duplicate nodes have been removed; the remaining nodes have been relabeled as integers)
reduced_hg
[[0], [2], [0, 1, 2], [0, 2, 3]]
The label translator maps
new integer labels back to sets of original labels
single original labels back to new integer labels
for key, val in label_translator.items():
print(f"{key}: {val}")
new_node_for_old_node: {'x': 0, 'z': 1, 'y': 2, 'zz': 1, 'w': 3, 'ww': 3}
new_edge_for_old_edge: {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'DD': 3}
old_nodes_for_new_node: [['x'], ['z', 'zz'], ['y'], ['w', 'ww']]
old_edges_for_new_edge: [['A'], ['B'], ['C'], ['D', 'DD']]
Total running time of the script: (0 minutes 0.003 seconds)