hatvis.core.Hypergraph#

class hatvis.core.Hypergraph(edges=None, *, nodes=None, weights=None, edge_labels=None, node_data=None, edge_data=None, name=None)[source]#

Bases: object

Undirected (multi-, weighted, fuzzy) hypergraph.

Parameters:
edgesiterable or mapping, optional

Hyperedges. Accepted forms:

  • a list of iterables of vertex labels ([["a", "b"], ["b", "c", "d"]]), hyperedges are labelled e0, e1, ... unless edge_labels is given;

  • a mapping label -> iterable ({"E1": ["a", "b"]});

  • a mapping label -> {vertex: value} for real-valued (weighted or fuzzy) incidence values \(b_{ie}\).

nodessequence, optional

Vertex labels and their order. Vertices that occur in edges but not in nodes are appended in order of appearance; vertices listed here but not in any hyperedge become isolated vertices.

weightsarray_like or mapping, optional

Hyperedge weights \(w_e\) (default 1).

edge_labelssequence, optional

Labels of the hyperedges when edges is a list.

node_data, edge_dataDataFrame or dict, optional

Attribute tables indexed by the vertex / hyperedge labels.

namestr, optional

A name used in plots and reports.

Examples

>>> H = Hypergraph([["v1", "v2", "v3"], ["v1", "v2", "v4"], ["v3", "v5"]])
>>> H
Hypergraph(name=None, n_nodes=5, n_edges=3)
>>> H.edge_sizes().tolist()
[3, 3, 2]
>>> H.degrees().to_dict()
{'v1': 2, 'v2': 2, 'v3': 2, 'v4': 1, 'v5': 1}
add_edges(edges, weights=None, labels=None)[source]#

Return a new hypergraph with additional hyperedges.

alpha_cut(alpha, binary=True)[source]#

Crisp \(\alpha\)-cut of a fuzzy (real-valued) incidence matrix.

Keeps the incidences with \(b_{ie}\ge\alpha\) (Lee-Kwang and Lee 1995).

binary_incidence()[source]#

0/1 incidence pattern \([b_{ie}>0]\).

collapse(how='sum')[source]#

Merge repeated hyperedges (identical vertex sets).

The merged hyperedge keeps the first label; its weight is the sum (multiplicity for an unweighted hypergraph), max or mean of the weights. edge_data["multiplicity"] records the number of copies.

collapse_nodes()[source]#

Merge vertices with identical memberships (equivalence classes).

Returns the reduced hypergraph and a mapping representative -> list of merged vertices; node_data["multiplicity"] stores the class sizes.

corank()[source]#

Smallest hyperedge size.

degrees(weighted=False)[source]#

Vertex degrees.

weighted=False: number of hyperedges containing the vertex, \(d(v)=|\{e: v\in e\}|\); weighted=True: \(d_i=\sum_e w_e b_{ie}\).

dual()[source]#

Dual hypergraph \(H^*\): vertices and hyperedges swap roles.

The incidence matrix of \(H^*\) is \(\mathbf{B}^\mathsf{T}\), the attribute tables are swapped and \((H^*)^*=H\). The weights of the dual hyperedges are the "weight" column of node_data if present, otherwise 1; the old hyperedge weights are kept in node_data["weight"] of the dual.

>>> H = Hypergraph({"e1": ["o1", "o2"], "e2": ["o1", "a1"]})
>>> H.dual().edge_dict()
{'o1': ('e1', 'e2'), 'o2': ('e1',), 'a1': ('e2',)}
edge_dict()[source]#

Mapping edge label -> tuple of vertex labels.

edge_index(e)[source]#

Column index of hyperedge e.

edge_sets()[source]#

Hyperedges as frozensets of vertex labels (column order).

edge_sizes()[source]#

Hyperedge sizes \(|e|\).

property edges#

Hyperedge labels (column order of the incidence matrix).

filter_edges(min_size=0, max_size=None, min_weight=None, predicate=None)[source]#

Keep hyperedges by size, weight or an arbitrary predicate(label, members).

classmethod from_dataframe(df, node_col, edge_col, value_col=None, weights=None, name=None)[source]#

Build a hypergraph from an incidence list (one row per vertex–hyperedge pair).

>>> df = pd.DataFrame({"actor": ["O1", "R1", "O1"], "task": ["E1", "E1", "E2"]})
>>> Hypergraph.from_dataframe(df, "actor", "task").edge_dict()
{'E1': ('O1', 'R1'), 'E2': ('O1',)}
classmethod from_incidence(B, nodes=None, edges=None, weights=None, node_data=None, edge_data=None, name=None)[source]#

Build a hypergraph from an incidence matrix (vertices in rows).

B may be a NumPy array, a SciPy sparse matrix or a DataFrame (its index and columns are used as labels). Non-zero entries are the incidence values \(b_{ie}\) (weights or fuzzy memberships).

>>> H = Hypergraph.from_incidence([[1, 1, 0], [1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 0, 1]])
>>> H.edge_dict()
{'e0': ('v0', 'v1', 'v2'), 'e1': ('v0', 'v1', 'v3'), 'e2': ('v2', 'v3', 'v4')}
property incidence#

Incidence matrix \(\mathbf{B}\) (sparse, vertices x hyperedges).

incidence_matrix(weighted=True, dense=True)[source]#

Incidence matrix as dense array (default) or sparse array.

weighted=False returns the 0/1 pattern.

is_uniform()[source]#

True if all hyperedges have the same size (a k-uniform hypergraph).

property is_weighted#

True if the hyperedge weights or the incidence values are not all 1.

isolated_nodes()[source]#

Vertices that belong to no hyperedge.

members(e)[source]#

Vertices of hyperedge e (in vertex order).

memberships(v)[source]#

Hyperedges that contain vertex v.

node_dict()[source]#

Mapping vertex label -> tuple of hyperedge labels (the dual view).

node_index(v)[source]#

Row index of vertex v.

property nodes#

Vertex labels (row order of the incidence matrix).

rank()[source]#

Largest hyperedge size.

relabel(nodes=None, edges=None)[source]#

Rename vertices and/or hyperedges with mappings (missing keys keep their label).

remove_isolated()[source]#

Drop vertices without hyperedges.

restrict_edges(edges)[source]#

Partial hypergraph that keeps only edges (all vertices kept).

restrict_nodes(nodes)[source]#

Sub-hypergraph induced by nodes (hyperedges are cut to these vertices; empty ones kept).

set_edge_attr(name, values)[source]#

Set a hyperedge attribute in place and return self.

set_node_attr(name, values)[source]#

Set a vertex attribute in place (mapping, Series or sequence) and return self.

summary()[source]#

Basic statistics as a Series.

to_dataframe(weighted=True)[source]#

Incidence matrix as a labelled DataFrame (vertices x hyperedges).

toplexes()[source]#

Partial hypergraph of the maximal hyperedges (not contained in another one).

property weights#

Hyperedge weights \(w_e\).