Hypergraph Analysis and Visualization in PythonThe hatvis toolbox

Chapter 1
Introduction

This chapter explains why group interactions call for a representation of their own, shows on a four-vertex example how much a graph forgets, and maps the rest of the book onto the questions a hypergraph helps to answer. It then places hatvis among the existing hypergraph software, fixes the notation used in every chapter, describes the conventions shared by all functions of the package, and ends with a first session that builds, prints, and draws a hypergraph.

1.1 Problem and idea

Network science describes a system by its parts and by the pairs of parts that interact. The description is powerful because the pair is the simplest relation that still has structure: from pairs one obtains paths, distances, degrees, clusters, flows, and the whole apparatus of graph algorithms. Many relations, however, are not pairs. A machining step needs an operator, a machine, and a fixture at the same time; a scientific paper has a set of authors; a chemical reaction consumes a set of reactants; a customer requirement of a product is satisfied by a combination of engineering solutions; a news article mentions a set of themes; a shopping basket holds a set of items. The natural object in each case is a set of parts, and the system is a family of such sets. Recent reviews have argued in detail that these higher-order interactions change the structure and the dynamics of complex systems, not only their bookkeeping [11, 12, 13, 14].

A family of subsets of a vertex set is a hypergraph [15, 16, 17]. The usual way to analyze it with graph tools is to project it: either onto a graph on the vertices, linking two vertices whenever they appear together in some set (the clique expansion), or onto a bipartite graph whose two kinds of nodes are the vertices and the sets (the star expansion). Both projections are useful, and much of this book uses them, but the first one loses information that cannot be recovered. Figure 1.1 shows the simplest case. A team of three people {a,b,c} and three separate pairs {a,b},{b,c},{a,c} have the same clique expansion, a triangle; so do a team of four and the six pairs inside it, or a team of four and four teams of three. Whether the three people ever worked together, whether a requirement needs all three solutions or any two of them, whether a basket contained the three items at once or never more than two, is exactly the information the projection discards.

Figure 1.1

Figure 1.1. Two hypergraphs with the same clique expansion. A single group of three (left) and three separate pairs (middle) produce the same triangle when every hyperedge is replaced by the clique of its members (right). Every pairwise statistic computed on the triangle (degree, clustering coefficient, shortest paths, modularity) is therefore the same for the two systems, although in the first the three vertices always act together and in the second never. Section 2.2.4 gives a general characterization of when a projection loses information.

The idea of the book follows from this example. Instead of choosing one projection and forgetting the hypergraph, we keep the hypergraph as the primary object and derive from it, explicitly and with known losses, whatever representation a question needs: the incidence matrix for seriation and biclustering, the bipartite graph for drawing and for partitioning, the clique expansion for spectral methods, the s-line graphs for walks that require a minimum overlap, the adjacency tensor for centralities that respect the group, the transaction database for pattern mining. Each of these is a view of the same data, and the analysis is honest when the reader knows which view produced which number.

1.2 What the hypergraph view buys

The chapters of the book are organized by the question asked, not by the mathematical object. Figure 1.2 shows the flow: data become a hypergraph, the hypergraph is represented in several ways, the representations feed the views and the analyses, and the analyses feed decisions. Five advantages of the hypergraph view recur throughout and are summarized in chapter 13.

Lossless storage.

The incidence matrix B stores the groups exactly, with weights, multiplicities, and attributes. Every other representation is computed from it, and the losses of each are known (chapter 2).

Group-aware distances.

Two hyperedges are adjacent in the s-line graph only if they share at least s vertices. Walks and centralities parametrized by s distinguish a strongly connected cell from a chain of weak contacts, which no pairwise distance can do (chapter 6).

Group-aware objectives.

Hypergraph modularity, cut, and covering objectives count a group once, as a whole, rather than once per pair; a graph objective over-weights large groups quadratically (chapters 7 and 10).

Patterns as hyperedges.

A frequent itemset, a validated group, or a motif is a hyperedge of a derived hypergraph; mining, validation, and visualization therefore use the same machinery (chapters 8 and 9).

Nonlinear group effects.

Contagion that requires a critical mass in a group, and consensus that depends on the agreement within a group, produce phenomena (hysteresis, bistability, accelerated or slowed convergence) that have no pairwise analogue (chapter 11).

Figure 1.2

Figure 1.2. The organization of the book. The hypergraph is the primary object; the representations of chapter 2 are derived from it with known losses, and every view and analysis states which representation it uses. Interchange with other packages goes through the Hypergraph Interchange Format and direct converters.

1.3 The software landscape

Hypergraph software has grown quickly in the last five years, and the packages differ more in their purpose than in their data structures. Table 1.1 lists the packages that hatvis interoperates with or builds upon.

The Hypergraph Analysis Toolbox (HAT) [18], available for MATLAB and Python, was the first general package to base hypergraph analysis on tensor algebra; it contains the hypergraph similarity measures of Surana, Chen, and Rajapakse [19] and methods for controllability and dynamics. HyperNetX [20], developed at the Pacific Northwest National Laboratory, implements the s-walk framework of Aksoy et al. [21] and draws hypergraphs as Euler-type diagrams in Matplotlib and in an interactive widget. XGI [22] represents higher-order interaction networks with a rich statistics interface, a collection of empirical data sets, and drawing functions for hypergraphs and simplicial complexes. Hypergraphx [23] gathers many recent methods of the network-physics community, among them higher-order motifs, statistically validated hyperedges, community models, and dynamical processes. EasyHypergraph [24], a module of the EasyGraph framework, aims at speed and memory efficiency for both analysis and hypergraph neural networks, and the DeepHypergraph library accompanies the HGNN+ family of neural models [25]. The Hypergraph Interchange Format (HIF) [26], written jointly by maintainers of several of these packages, defines a JSON schema for undirected and directed hypergraphs and simplicial complexes with attributes on vertices, hyperedges, and incidences; it is the natural way to move data between them.

Two tools of the authors’ group are the direct ancestors of hatvis. HAT-VIS [1] is a MATLAB tool for the similarity-based visualization of hypergraphs; its pipeline (weighted Jaccard similarity, transitive closure, multidimensional scaling, minimal-spanning-tree hyperedges, seriated incidence matrix) is the subject of chapter 5. HyperGodot [2] is an interactive viewer built on the Godot engine with force-directed and circular layouts, padded convex hulls, and comparative views of several system variants; its layout and drawing model is the subject of chapter 4.

Table 1.1. Hypergraph software related to hatvis. “Interop” lists how hatvis exchanges data with the package.

Package

Language

Main focus

Interop

HAT [18]

MATLAB, Python

tensor methods, similarity, controllability

HIF

HyperNetX [20]

Python

s-walks, Euler-type drawings, widget

converter, HIF

XGI [22]

Python

statistics, data sets, drawing

converter, HIF

Hypergraphx [23]

Python

motifs, validation, communities, dynamics

converter, HIF

EasyHypergraph [24]

Python

fast analysis and learning

incidence / edge list

HAT-VIS [1]

MATLAB

similarity-based visualization

reimplemented

HyperGodot [2]

Godot (GDScript)

interactive layouts and comparison

text format, reimplemented

hatvis does not try to replace these packages. Its aims are narrower and complementary: to put the visualization methods of HAT-VIS and HyperGodot, the manufacturing and quality-engineering applications of the group, and the most useful analysis methods of the literature into one consistent, documented, and tested Python API; to make every representation explicit; and to give each method a worked example and a chapter that explains when it should be used.

1.4 Notation

The notation of table 1.2 is used in every chapter, in the docstrings of the package, and in the online documentation. Index i,j always runs over vertices and e,f,g over hyperedges. A vertex is also called a node; the book says vertex, the code says node, as in NetworkX, HyperNetX, and XGI.

Table 1.2. Notation used throughout the book and in the docstrings of hatvis.

Symbol

Meaning

Code

H = (V,E,w)

hypergraph: vertices, hyperedges, weights

Hypergraph

n = |V |, m = |E|

numbers of vertices and hyperedges

H.n_nodes, H.n_edges

B = [bie] ∈ℝn×m

incidence matrix, bie > 0 iff vi ∈e

H.incidence

we, W = diag ⁡ (we)

hyperedge weights

H.weights

|e|

size (cardinality) of hyperedge e

H.edge_sizes()

di = ∑ ⁡ ewebie

(weighted) degree of vertex i

H.degrees()

Dv, De

diagonal degree and size matrices

H∗

dual hypergraph, incidence BT

H.dual()

Ls(H)

s-line graph: e ∼f⟺|e ∩f|≥s

s_line_graph(H, s)

A = BWBT −Dv

clique-expansion adjacency

clique_adjacency(H)

L

a hypergraph Laplacian

laplacian(H, kind)

S = [sij], Δ = 1 −S

vertex similarity and dissimilarity

vertex_similarity(H)

yi ∈ℝ2, Y

layout coordinates

dict label -> (x, y)

T = {T1,…,TN}

transaction database

hypergraph_to_transactions

supp ⁡ (X)

relative support of itemset X

frequent_itemsets

σ

partition (community labels)

pandas.Series of int

1.5 Conventions of the package

Every function of hatvis follows the same small set of rules, so that the results of one method can be passed to another without conversion.

The package depends on NumPy, SciPy, pandas, NetworkX, scikit-learn, and Matplotlib. The integer programs of chapter 10 use the HiGHS solver [28] through scipy.optimize.milp.

1.6 A first session

The smallest example of the HAT-VIS paper [1] has five vertices and three hyperedges of three vertices each. It is built from a dictionary that maps hyperedge labels to member lists; lists of member sets, incidence matrices, and long-format data frames are accepted as well (section 2.4).

H = hv.Hypergraph({"e1": ["v1", "v2", "v3"], "e2": ["v1", "v2", "v4"], "e3": ["v3", "v4", "v5"]}) 
print(H.to_dataframe().astype(int)) 
print(H.degrees().to_dict(), H.edge_sizes().to_dict()) 
D = H.dual() 
print(D.edge_dict())
   

The incidence matrix has a row per vertex and a column per hyperedge. The degrees are dv1 = ⋯ = dv4 = 2 and dv5 = 1; all three hyperedges have size 3. The dual hypergraph exchanges the two roles: its vertices are e1,e2,e3, and every original vertex becomes a hyperedge listing the hyperedges that contain it, so v1 becomes {e1,e2} and v5 the singleton {e3}. The two views of fig. 1.3 are complete descriptions of the same object: the matrix shows every incidence explicitly and scales to thousands of rows, while the drawing shows at a glance which groups overlap. Neither is a projection; both can be computed back into the other. Already this small example has a feature that the following chapters return to: v1 and v2 belong to exactly the same hyperedges, so no analysis based on incidences alone can tell them apart, and a similarity-based layout places them on top of each other (chapter 5).

Figure 1.3

Figure 1.3. A first hypergraph and its dual. Left: the incidence matrix of the five-vertex example of HAT-VIS. Middle: the hypergraph drawn with padded convex hulls on the force-directed layout of HyperGodot. Right: the dual hypergraph, whose vertices are the original hyperedges; v1 and v2 become two identical hyperedges {e1,e2} and v5 a singleton.

Open in ColabExample plot_01_quickstart.py in the gallery

1.7 Further reading

Berge’s monographs [15, 16] remain the standard reference for the combinatorics of hypergraphs, and Bretto [17] gives a modern introduction. The review of Battiston et al. [11] surveys the structure and dynamics of networks beyond pairwise interactions; Bick et al. [13] compare hypergraphs, simplicial complexes, and other higher-order models from a mathematical viewpoint, and Torres et al. [14] discuss when each representation of a complex system is appropriate. Lee et al. [29] survey the structural patterns observed in real-world hypergraphs and the tools and generators used to find them. For visualization, the surveys of Alsallakh et al. on set visualization [30] and of Fischer et al. on hypergraph visualization [31] are the best starting points.