.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/dowker/plot_relabel_reduce.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_dowker_plot_relabel_reduce.py: .. _dowker_relabel_reduce_gallery: 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. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python import oat_python as oat .. GENERATED FROM PYTHON SOURCE LINES 17-23 See also ----------------------------------------------- - The `HypernetX `_ library provides many useful tools for working with hypergraphs, including tools to remove duplicate nodes and edges. - :ref:`dowker_rbs_homology_gallery` shows how to relabel nodes/edges, and use proper labels in plotting. .. GENERATED FROM PYTHON SOURCE LINES 26-30 Relabel a hypergraph ----------------------------------------------- Here we assign integer labels to the nodes of a hypergraph whose nodes are labeled with strings. .. GENERATED FROM PYTHON SOURCE LINES 32-33 Define a hypergraph .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python E = { "A": ["x"], "B": ["y"], "C": ["x","y","z","zz"], "D": ["w","ww","x","y"], "DD": ["w","ww","x","y"] } .. GENERATED FROM PYTHON SOURCE LINES 36-37 Relabel with integers .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python relabeled_hg, label_translator = oat.hypergraph.relabel(E) .. GENERATED FROM PYTHON SOURCE LINES 40-41 The relabeled hypergraph: .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python relabeled_hg .. rst-class:: sphx-glr-script-out .. code-block:: none [[2], [3], [2, 3, 4, 5], [0, 1, 2, 3], [0, 1, 2, 3]] .. GENERATED FROM PYTHON SOURCE LINES 44-45 The label translator maps the new integer labels back to original labels and vice versa .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python for key, val in label_translator.items(): print(f"{key}: {val}") .. rst-class:: sphx-glr-script-out .. code-block:: none 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'] .. GENERATED FROM PYTHON SOURCE LINES 49-56 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 .. GENERATED FROM PYTHON SOURCE LINES 58-59 Define a hypergraph .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python E = { "A": ["x"], "B": ["y"], "C": ["x","y","z","zz"], "D": ["w","ww","x","y"], "DD": ["w","ww","x","y"] } .. GENERATED FROM PYTHON SOURCE LINES 62-63 Collapse out redundant information, returning a list of lists .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python reduced_hg, label_translator = oat.hypergraph.reduce_hypergraph_with_labels(E) .. GENERATED FROM PYTHON SOURCE LINES 66-67 The reduced hypergraph edges (duplicate nodes have been removed; the remaining nodes have been relabeled as integers) .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python reduced_hg .. rst-class:: sphx-glr-script-out .. code-block:: none [[0], [2], [0, 1, 2], [0, 2, 3]] .. GENERATED FROM PYTHON SOURCE LINES 70-74 The label translator maps - new integer labels back to **sets of original labels** - **single original labels** back to new integer labels .. GENERATED FROM PYTHON SOURCE LINES 74-75 .. code-block:: Python for key, val in label_translator.items(): print(f"{key}: {val}") .. rst-class:: sphx-glr-script-out .. code-block:: none 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']] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_dowker_plot_relabel_reduce.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_relabel_reduce.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_relabel_reduce.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_relabel_reduce.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_