Python library for the creation and study of hypergraphs with analysis, visualization, and algorithm capabilities
npx @tessl/cli install tessl/pypi-hypernetx@2.4.0A comprehensive Python library for the creation and study of hypergraphs. HyperNetX provides classes and methods that generalize traditional graph metrics to hypergraphs, enabling researchers to study higher-order relationships and complex network structures.
pip install hypernetximport hypernetx as hnxCommon for working with hypergraphs:
from hypernetx import HypergraphSpecific functionality imports:
# Algorithms and analysis
from hypernetx import (
s_betweenness_centrality,
betti_numbers,
collective_contagion,
spec_clus,
greedy_matching
)
# Visualization
from hypernetx import draw, draw_incidence_upset
# Data import/export
from hypernetx import to_hif, from_hif
# Reports and statistics
from hypernetx import info, degree_dist
# Example datasets
from hypernetx import LesMisimport hypernetx as hnx
import pandas as pd
# Create hypergraph from incidence data
# Edge E1 contains nodes A, B, C
# Edge E2 contains nodes B, C, D
incidence_data = pd.DataFrame({
'edge': ['E1', 'E1', 'E1', 'E2', 'E2', 'E2'],
'node': ['A', 'B', 'C', 'B', 'C', 'D'],
'weight': [1, 1, 1, 1, 1, 1]
})
# Create hypergraph
H = hnx.Hypergraph(incidence_data)
# Basic hypergraph properties
print(f"Number of nodes: {H.order()}")
print(f"Number of edges: {len(H.edges)}")
print(f"Hypergraph shape: {H.shape}")
# Node and edge information
print(f"Nodes: {list(H.nodes)}")
print(f"Edges: {list(H.edges)}")
print(f"Incidence dict: {H.incidence_dict}")
# Basic analysis
print(f"Degree of node B: {H.degree('B')}")
print(f"Size of edge E1: {H.size('E1')}")HyperNetX is built on a flexible architecture that separates data storage, views, and properties:
This design enables efficient hypergraph manipulation while supporting rich metadata and properties on all objects.
Primary hypergraph data structure with construction, manipulation, and basic analysis methods. Includes property management, structural modifications, and conversion utilities.
class Hypergraph:
def __init__(self, setsystem=None, **kwargs): ...
def order(self) -> int: ...
def shape(self) -> tuple: ...
def degree(self, node_uid, s=1, max_size=None): ...
def size(self, edge, nodeset=None): ...
def incidence_matrix(self, index=False, weights=False): ...
def add_edge(self, edge_uid, inplace=True, **attr): ...
def remove_edges(self, edge_uids, name=None, inplace=True): ...Structural analysis including connectivity, distance measures, diameter calculations, and component analysis. Supports s-connected analysis for higher-order connectivity patterns.
def is_connected(self, s=1, edges=False) -> bool: ...
def s_connected_components(self, s=1, edges=True, return_singletons=False): ...
def diameter(self, s=1): ...
def distance(self, source, target, s=1): ...
def adjacency_matrix(self, s=1, index=False): ...Comprehensive collection of hypergraph algorithms including centrality measures, homology computations, contagion models, clustering methods, and matching algorithms.
def s_betweenness_centrality(H, s=1): ...
def betti_numbers(H, k=None): ...
def collective_contagion(H, **kwargs): ...
def spec_clus(H, k=2): ...
def greedy_matching(H): ...Visualization capabilities for hypergraph display including rubber band layouts, incidence upset plots, and bipartite representations.
def draw(H, **kwargs): ...
def draw_incidence_upset(H, **kwargs): ...
def draw_bipartite_using_euler(H, **kwargs): ...Data conversion utilities, HIF format support, statistical reporting functions, and example datasets for experimentation and learning.
def to_hif(hg, filename=None, **kwargs): ...
def from_hif(hif=None, filename=None): ...
def info(H): ...
def degree_dist(H): ...
class LesMis: ...Data Import/Export and Utilities
from typing import Union, Optional, Dict, List, Any
from pandas import DataFrame
from numpy import ndarray
from scipy.sparse import csr_matrix
# Core types
NodeID = Union[str, int]
EdgeID = Union[str, int]
IncidenceID = tuple[EdgeID, NodeID]
Weight = Union[int, float]
Properties = Dict[str, Any]
# Data input types
SetsystemInput = Union[DataFrame, Dict, List, ndarray, None]
PropertyInput = Union[DataFrame, Dict, None]
class HypergraphView:
def __init__(self, incidence_store, level: int, property_store=None): ...
@property
def items(self) -> set: ...
def is_empty(self) -> bool: ...
class IncidenceStore:
def __init__(self, data: DataFrame): ...
@property
def elements(self) -> Dict[EdgeID, List[NodeID]]: ...
@property
def memberships(self) -> Dict[NodeID, List[EdgeID]]: ...
class PropertyStore:
def __init__(self, data: Optional[DataFrame] = None, default_weight: Weight = 1): ...
def get_properties(self, uid: Union[NodeID, EdgeID, IncidenceID]) -> Dict[str, Any]: ...
def set_property(self, uid: Union[NodeID, EdgeID, IncidenceID], prop_name: str, prop_val: Any): ...
# Exception types
class HyperNetXException(Exception): ...
class HyperNetXError(Exception): ...
class HyperNetXNotImplementedError(NotImplementedError): ...