.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/dowker/plot_rbs_homology.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_rbs_homology.py: .. _dowker_rbs_homology_gallery: Restricted Barycentric Subdivision (Vietoris-Rips) =============================================================== The Restricted Barycentric Subdivision Homology of a hypergraph is the homology of the clique complex of the simple graph :math:`G` such that - the vertices of :math:`G` are the hyperedges of the hypergraph - the edges of :math:`G` are the pairs :math:`\{e,h\}` such that :math:`e \subseteq h` or :math:`h \subseteq e` OAT doesn't offer a specialized function to compute this homology, but it does allow one to compute the *persistent homology* of any weighted graph. So, as a work around, we assign weight 0 to every edge of `G` and compute its persistent homology. This notebook shows how to implement this strategy. .. GENERATED FROM PYTHON SOURCE LINES 18-27 .. code-block:: Python import oat_python as oat import copy import plotly.graph_objects as go import numpy as np import networkx as nx import hypernetx as hnx import sklearn .. GENERATED FROM PYTHON SOURCE LINES 28-30 Define a hypergraph ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-33 The hypergraph has edges labeled "A", "B", "C", "D", with the following vertices: .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: Python E = { "A": ["x"], "B": ["y"], "C": ["x","y","z",], "D": ["x","y","w",], } .. GENERATED FROM PYTHON SOURCE LINES 37-39 Plot the hypergraph using HyperNetX ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python hnxgraph = hnx.Hypergraph(E) hnx.drawing.draw(hnxgraph) .. image-sg:: /auto_examples/dowker/images/sphx_glr_plot_rbs_homology_001.png :alt: plot rbs homology :srcset: /auto_examples/dowker/images/sphx_glr_plot_rbs_homology_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:150: DeprecationWarning: misc_props_col and misc_cell_props_col will be deprecated; all public references to these arguments will be removed in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/hypergraph.py:563: FutureWarning: This method or function will be deprecated in a future release. Public references to this method or function will be removed from the Hypergraph API in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:150: DeprecationWarning: misc_props_col and misc_cell_props_col will be deprecated; all public references to these arguments will be removed in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/hypergraph.py:563: FutureWarning: This method or function will be deprecated in a future release. Public references to this method or function will be removed from the Hypergraph API in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. .. GENERATED FROM PYTHON SOURCE LINES 45-47 Compute homology ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-69 .. code-block:: Python # convert to a list of edges G = list( E.values() ) # graph representing the edge containment poset (forgetting direction) containment = oat.hypergraph.edge_containment_graph_symmetrized( G ) # graph whose edges form the set complement of the containment graph anticontainment = nx.complement( containment ) # adjacency matrix of the anticontainment graph anti_adjacency = nx.adjacency_matrix( anticontainment ).todense() dissimilarity_matrix = oat.dissimilarity.sparse_matrix_for_dense( dissimilarity_matrix = anti_adjacency, max_dissimilarity = 0.5 ) # decomposition boundary matrix decomposition = oat.core.vietoris_rips.BoundaryMatrixDecomposition( dissimilarity_matrix = dissimilarity_matrix, max_homology_dimension = 1, ) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/oat-python/checkouts/stable/examples/dowker/plot_rbs_homology.py:57: FutureWarning: adjacency_matrix will return a scipy.sparse array instead of a matrix in Networkx 3.0. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/networkx/linalg/graphmatrix.py:173: DeprecationWarning: The scipy.sparse array containers will be used instead of matrices in Networkx 3.0. Use `to_scipy_sparse_array` instead. .. GENERATED FROM PYTHON SOURCE LINES 70-74 The persistent homology dataframe contains the persistent homology of the Vietoris-Rips complex. Because every edge in the underlying graph has weight 0, this is the same as the RBS homology of the hypergraph. The dataframe contains a cycle basis for the RBS homology, in which each cycle technically represets a homology class that is born at filtration parameter 0 and never dies. .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python persistent_homology = decomposition.persistent_homology_dataframe( return_cycle_representatives = True, return_bounding_chains = True, ) persistent_homology .. raw:: html
dimension interval_length birth_filtration birth_simplex death_filtration death_simplex cycle_representative num_cycle_simplices bounding_chain num_bounding_simplices
id
0 0 inf 0.0 (0,) inf None simplex filtration coefficient 0 (0,) ... 1 None None
1 1 inf 0.0 (1, 3) inf None simplex filtration coefficient 0 (0, 2) ... 4 None None


.. GENERATED FROM PYTHON SOURCE LINES 81-83 Inspect a cycle representative ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python cycle = persistent_homology["cycle_representative"][1] cycle .. raw:: html
simplex filtration coefficient
0 (0, 2) 0.0 1
1 (0, 3) 0.0 -1
2 (1, 2) 0.0 -1
3 (1, 3) 0.0 1


.. GENERATED FROM PYTHON SOURCE LINES 89-94 Relabel each vertex ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Recall that a vertex in the Vietoris-Rips complex corresponds to a hyperedge in the initial hypergraph. These hyperedges have labels "A", "B", "C", "D", so lets relabel the vertices in the cycle representative accordingly. .. GENERATED FROM PYTHON SOURCE LINES 97-98 Recall that each vertex in RBS homology represents an edge in the reduced hypergraph (which corresponds to a set of hyperedges in the initial hypergraph). Here we relabel each vertex with **one** of the edges that maps to it. .. GENERATED FROM PYTHON SOURCE LINES 100-108 .. code-block:: Python original_edge_labels= { p: k for p,k in enumerate(E.keys()) } cycle = copy.deepcopy(persistent_homology["cycle_representative"][1]) cycle["simplex"] = [ [original_edge_labels[x] for x in simplex] for simplex in cycle["simplex"] ] cycle .. raw:: html
simplex filtration coefficient
0 [A, C] 0.0 1
1 [A, D] 0.0 -1
2 [B, C] 0.0 -1
3 [B, D] 0.0 1


.. GENERATED FROM PYTHON SOURCE LINES 109-112 Plot a cycle representative ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 114-134 .. code-block:: Python edges = persistent_homology["cycle_representative"][1]["simplex"] # the edges in the cycle points = oat.plot.vertex_embedding_for_simplices( simplices = edges.tolist(), dimension = 3, ) fig = oat.plot.fig_3d_for_simplices( edges, points=points, kwargs_points = dict( mode="markers+text", text= [ val for val in original_edge_labels.values() ], marker_size=8, textfont_size=18, ), ) fig.update_layout(template="plotly_dark") .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 135-137 Suspend and repeat --------------------- .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: Python F = { "A": ["x"], "B": ["y"], "C": ["x","y","z",], "D": ["x","y","w",], "E":["x","y","z","w","a"], "F":["x","y","z","w","b"] } hnxgraph = hnx.Hypergraph(F) hnx.drawing.draw(hnxgraph) .. image-sg:: /auto_examples/dowker/images/sphx_glr_plot_rbs_homology_002.png :alt: plot rbs homology :srcset: /auto_examples/dowker/images/sphx_glr_plot_rbs_homology_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:150: DeprecationWarning: misc_props_col and misc_cell_props_col will be deprecated; all public references to these arguments will be removed in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/hypergraph.py:563: FutureWarning: This method or function will be deprecated in a future release. Public references to this method or function will be removed from the Hypergraph API in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:150: DeprecationWarning: misc_props_col and misc_cell_props_col will be deprecated; all public references to these arguments will be removed in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/hypergraph.py:563: FutureWarning: This method or function will be deprecated in a future release. Public references to this method or function will be removed from the Hypergraph API in a future release. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/hypernetx/classes/entityset.py:618: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. .. GENERATED FROM PYTHON SOURCE LINES 144-145 convert to a list of edges .. GENERATED FROM PYTHON SOURCE LINES 145-163 .. code-block:: Python G = list( F.values() ) # graph representing the edge containment poset (forgetting direction) containment = oat.hypergraph.edge_containment_graph_symmetrized( G ) # graph whose edges form the set complement of the containment graph anticontainment = nx.complement( containment ) # adjacency matrix of the anticontainment graph anti_adjacency = nx.adjacency_matrix( anticontainment ).todense() dissimilarity_matrix = oat.dissimilarity.sparse_matrix_for_dense( dissimilarity_matrix = anti_adjacency, max_dissimilarity = 0.5 ) # decomposition boundary matrix decomposition = oat.core.vietoris_rips.BoundaryMatrixDecomposition( dissimilarity_matrix = dissimilarity_matrix, max_homology_dimension = 2, ) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/oat-python/checkouts/stable/examples/dowker/plot_rbs_homology.py:151: FutureWarning: adjacency_matrix will return a scipy.sparse array instead of a matrix in Networkx 3.0. /home/docs/checkouts/readthedocs.org/user_builds/oat-python/envs/stable/lib/python3.9/site-packages/networkx/linalg/graphmatrix.py:173: DeprecationWarning: The scipy.sparse array containers will be used instead of matrices in Networkx 3.0. Use `to_scipy_sparse_array` instead. .. GENERATED FROM PYTHON SOURCE LINES 164-165 The persistent homology dataframe: .. GENERATED FROM PYTHON SOURCE LINES 165-171 .. code-block:: Python persistent_homology = decomposition.persistent_homology_dataframe( return_cycle_representatives = True, return_bounding_chains = True, ) persistent_homology .. raw:: html
dimension interval_length birth_filtration birth_simplex death_filtration death_simplex cycle_representative num_cycle_simplices bounding_chain num_bounding_simplices
id
0 0 inf 0.0 (0,) inf None simplex filtration coefficient 0 (0,) ... 1 None None
1 2 inf 0.0 (1, 3, 5) inf None simplex filtration coefficient 0 (0, 2,... 8 None None


.. GENERATED FROM PYTHON SOURCE LINES 172-173 A cycle representative: .. GENERATED FROM PYTHON SOURCE LINES 173-176 .. code-block:: Python cycle = persistent_homology["cycle_representative"][1] cycle .. raw:: html
simplex filtration coefficient
0 (0, 2, 4) 0.0 -1
1 (0, 2, 5) 0.0 1
2 (0, 3, 4) 0.0 1
3 (0, 3, 5) 0.0 -1
4 (1, 2, 4) 0.0 1
5 (1, 2, 5) 0.0 -1
6 (1, 3, 4) 0.0 -1
7 (1, 3, 5) 0.0 1


.. GENERATED FROM PYTHON SOURCE LINES 177-178 A cycle with reverted vertex labels .. GENERATED FROM PYTHON SOURCE LINES 178-184 .. code-block:: Python original_edge_labels = { p: k for p,k in enumerate(F.keys()) } cycle = copy.deepcopy(persistent_homology["cycle_representative"][1]) cycle["simplex"] = [ [original_edge_labels[x] for x in simplex] for simplex in cycle["simplex"] ] cycle .. raw:: html
simplex filtration coefficient
0 [A, C, E] 0.0 -1
1 [A, C, F] 0.0 1
2 [A, D, E] 0.0 1
3 [A, D, F] 0.0 -1
4 [B, C, E] 0.0 1
5 [B, C, F] 0.0 -1
6 [B, D, E] 0.0 -1
7 [B, D, F] 0.0 1


.. GENERATED FROM PYTHON SOURCE LINES 185-186 Plot the cycle representative .. GENERATED FROM PYTHON SOURCE LINES 186-206 .. code-block:: Python triangles = persistent_homology["cycle_representative"][1]["simplex"] # the edges in the cycle points = oat.plot.vertex_embedding_for_simplices( simplices = triangles.tolist(), dimension = 3, ) fig = oat.plot.fig_3d_for_simplices( triangles, points=points, kwargs_points = dict( mode="markers+text", text= [ val for val in original_edge_labels.values() ], marker_size=8, textfont_size=18, ), ) fig.update_layout(template="plotly_dark") .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 207-208 Make triangles opaque and grey, for a different style. .. GENERATED FROM PYTHON SOURCE LINES 210-211 The trace for triangles is stored in fig.data[2] .. GENERATED FROM PYTHON SOURCE LINES 211-215 .. code-block:: Python fig.data[2].update( color = 'grey', opacity = 1.0) fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.304 seconds) .. _sphx_glr_download_auto_examples_dowker_plot_rbs_homology.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rbs_homology.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rbs_homology.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rbs_homology.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_