HyperNetX

repository·master·Indexed 20 days ago

https://github.com/pnnl/hypernetx

A Python library for the creation, analysis, and visualization of complex network data modeled as hypergraphs. It generalizes traditional graph metrics to support higher-order relationships and includes specialized algorithm submodules for contagion, generative models, homology, modularity, Laplacians clustering, matching, and centrality measures. Version 2.4.3 utilizes a Pandas-based architecture supporting mutability, arithmetic operations, and metadata for cells, edges, and nodes.

Tokens
19.1K
Snippets
69
Records
104
Agent score
72%

What's inside hypernetx

  1. What is HyperNetX (HNX)?

    master

    HyperNetX (HNX) is an open-source Python library designed for the analysis and visualization of complex network data modeled as hypergraphs. Unlike standard graphs that represent pairwise interactions, HNX allows for hyperedges that can contain any number of nodes (1, 2, or more).

    Key capabilities include:

    • Exploratory Data Analysis: Uses algebraic topology, combinatorics, and generalized hypergraph/graph theoretical methods.
    • Metadata Support: Allows attaching numerical and categorical metadata to nodes (vertices), hyperedges, and node-hyperedge pairings (incidences).
    • Data Structures: Uses Pandas DataFrames as the underlying data structure, making nodes and hyperedges easily accessible.
    • Visualization: Includes a customizable Matplotlib-based module and HypernetX-Widget, a JavaScript add-on for interactive exploration within Jupyter Notebooks.
  2. Introduction to HyperNetX (HNX)

    master

    HyperNetX (HNX) is a Python library designed for working with hypergraphs. It provides tools to represent, manipulate, and analyze hypergraphs by generalizing traditional graph structures.

    Key capabilities include:

    • Mathematical Modeling: Handling hyperedges, incidence matrices, and hypergraph duality.
    • Data Management: Using specialized data structures like PropertyStore, IncidenceStore, and HypergraphView to manage nodes, edges, and incidences.
    • Flexible Construction: Building hypergraphs from various Python and data science formats such as lists of lists, dictionaries, and Pandas DataFrames.
    • Metadata Support: Associating properties and metadata with nodes, edges, and incidences.
  3. Overview of HyperNetX core components

    master

    HyperNetX is a Python package for creating, manipulating, and analyzing hypergraphs. The library is organized into four primary functional areas:

    • Hypergraphs: Core data structures and classes (such as Hypergraph, PropertyStore, IncidenceStore, and HypergraphView) used to represent hypergraph data.
    • Algorithms: Functions for hypergraph analysis, including connectivity, distance, centrality, and community detection.
    • Drawing: Tools for visualizing hypergraphs, including layout algorithms and rendering utilities.
    • Reports: Functionality for generating statistical summaries and reports of hypergraph data.
  4. Explore HyperNetX algorithm submodules

    master

    The algorithms package in HyperNetX provides various specialized tools for hypergraph analysis. The package is organized into several submodules, each focusing on a specific domain of hypergraph theory or application:

    • algorithms.contagion: Models for contagion and spreading processes on hypergraphs.
    • algorithms.generative_models: Algorithms for generating synthetic hypergraphs.
    • algorithms.homology_mod2: Tools for computing second-order homology.
    • algorithms.hypergraph_modularity: Measures and algorithms for hypergraph modularity.
    • algorithms.laplacians_clustering: Clustering methods based on hypergraph Laplacians.
    • algorithms.matching_algorithms: Algorithms for finding matchings in hypergraphs.
    • algorithms.s_centrality_measures: Measures for calculating centrality in hypergraphs.
  5. How Hypergraph constructors work with SetSystems

    master

    A hypergraph $H = (V, E)$ is composed of nodes ($V$) and hyperedges ($E$). HyperNetX (HNX) supports multi-edges by distinguishing edges via unique identifiers rather than just their node content.

    To create a hypergraph, you must provide a setsystem, which defines the many-to-many relationships between edges and nodes. HNX supports five types of setsystems:

    1. Iterable of iterables: A barebones approach where edge IDs are generated using Pandas default indexing. Elements must be hashable.
    2. Dictionary of iterables: Provides explicit edge IDs as keys and iterables of nodes as values.
    3. Dictionary of dictionaries: Allows assigning cell_properties (metadata for specific edge-node incidence pairs) directly within the setsystem.
    4. pandas.DataFrame: Most efficient for large datasets. The first two columns must represent incidence pairs. You can specify columns for cell weights and miscellaneous cell properties.
    5. numpy.ndarray: For homogeneous $n imes 2$ arrays. A DataFrame is generated internally, and incidence properties must be added after construction.
    # 1. Iterable of iterables
    list_of_lists = [['book','candle','cat'],['book','coffee cup'],['coffee cup','radio']]
    H = Hypergraph(list_of_lists)
    
    # 2. Dictionary of iterables
    sce_dict = {0: ('FN', 'TH'), 1: ('TH', 'JV')}
    H = hnx.Hypergraph(sce_dict)
    
    # 3. Dictionary of dictionaries (with cell properties)
    nested_dict = {0: {'FN':{'time':'early'}, 'TH':{'time':'late'}}}
    H = hnx.Hypergraph(nested_dict)
    
    # 4. pandas.DataFrame
    import pandas as pd
    d = {'col1': ['e1', 'e1', 'e2'], 'col2': [1, 2, 1], 'w': [0.5, 0.1, 0.52], 'col3':[{'name': 'related_to'}, {'name': 'related_to', 'startdate':'05.13.2020'}, {'name': 'owned_by'}]}
    df = pd.DataFrame(d)
    H = hnx.Hypergraph(df, edge_col='col1', node_col='col2', cell_weight_col='w', misc_cell_properties_col='col3')
    
    # 5. numpy.ndarray
    import numpy as np
    np_array = np.array([['A','a'],['A','b'],['B','a']])
    H = hnx.Hypergraph(np_array)
    H.incidences[('A','a')].color = 'red'
  6. Hypergraph concepts and features in HNX 2.3+

    master

    HyperNetX 2.3 introduced significant changes to the core library, moving to a Pandas-based architecture.

    Key Features:

    • Mutability: Ability to add and remove edges, nodes, and incidences.
    • Arithmetic: Support for sum, difference, union, and intersection of Hypergraphs.
    • Metadata: Constructors accept cell, edge, and node metadata.
    • New Components: IncidenceStore and PropertyStore manage structure and attributes; HypergraphView replaces EntitySet.

    Breaking Changes (v2.3+):

    • Not backwards compatible: Objects from earlier versions must be imported using their incidence dictionaries and/or property dataframes.
    • Python Requirement: Requires Python ">=3.10,<4.0.0".
    • Unified Structure: The distinction between static and dynamic hypergraphs has been removed; all use the same underlying Pandas-supported structure.
  7. Relationship between Hypergraphs and Bipartite Graphs

    master

    There is a one-to-one relationship between a hypergraph $H = \langle V, E \rangle$ and a corresponding bipartite graph $B = \langle V \sqcup E, I \rangle$.

    In this bipartite representation:

    • The vertices of $B$ consist of both the original vertices ($V$) and the hyperedges ($E$) from the hypergraph.
    • A connection (edge) exists in $B$ between a vertex $v$ and a hyperedge $e$ if and only if $v \in e$ in the original hypergraph $H$.

    Additionally, the Boolean incidence matrix $I$ of a hypergraph can be viewed as the characteristic matrix of a binary relation, creating a one-to-one mapping between hypergraphs, binary relations, and bipartite graphs.

  8. Understanding edge intersections and nesting in hypergraphs

    master

    Hypergraphs exhibit structural properties that differ from standard graphs due to varying edge sizes:

    • Edge Intersections: In a graph, incident edges intersect at exactly one vertex ($s(e,f)=1$). In a hypergraph, the intersection size $s(e,f) = |e \cap f|$ can be any integer from 1 up to the size of the smaller edge.
    • Nested Edges: Hypergraphs allow for nested or included edges, where one edge is a subset of another ($e \subseteq f$). This occurs when the intersection size equals the size of the smaller edge: $s(e,f) = \min(|e|,|f|)$.
  9. Understanding the difference between Graphs and Hypergraphs

    master

    In network science, a graph $G=\langle V,E\rangle$ consists of a set of vertices $V$ and a set of edges $E$, where each edge is a pair of vertices. A hypergraph $H=\langle V,E\rangle$ generalizes this by allowing a hyperedge $e \in E$ to be a subset of vertices of any arbitrary size $k$ (a $k$-edge).

    Key distinctions:

    • Uniformity: A hypergraph where every edge is a 2-edge (a pair) is called 2-uniform and is equivalent to a standard graph.
    • Incidence Matrix: For a graph, every column in the incidence matrix $I$ must have exactly two 1s. For a hypergraph, a column for a $k$-edge will have $k$ 1s.
    • 2-section (Underlying Graph): The 2-section of a hypergraph is a graph that represents only the pairwise connections present within the hyperedges. While a hypergraph uniquely determines its 2-section, the 2-section is usually insufficient to reconstruct the original hypergraph.