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:
objectUndirected (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 labellede0, e1, ...unlessedge_labelsis 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
edgesbut not innodesare 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
edgesis a list.- node_data, edge_data
DataFrameordict,optional Attribute tables indexed by the vertex / hyperedge labels.
- name
str,optional A name used in plots and reports.
- edgesiterable or
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).
- 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),maxormeanof 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.
- 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 ofnode_dataif present, otherwise 1; the old hyperedge weights are kept innode_data["weight"]of the dual.>>> H = Hypergraph({"e1": ["o1", "o2"], "e2": ["o1", "a1"]}) >>> H.dual().edge_dict() {'o1': ('e1', 'e2'), 'o2': ('e1',), 'a1': ('e2',)}
- 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).
Bmay be a NumPy array, a SciPy sparse matrix or aDataFrame(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=Falsereturns the 0/1 pattern.
- property is_weighted#
True if the hyperedge weights or the incidence values are not all 1.
- property nodes#
Vertex labels (row order of the incidence matrix).
- relabel(nodes=None, edges=None)[source]#
Rename vertices and/or hyperedges with mappings (missing keys keep their label).
- restrict_nodes(nodes)[source]#
Sub-hypergraph induced by
nodes(hyperedges are cut to these vertices; empty ones kept).
- set_node_attr(name, values)[source]#
Set a vertex attribute in place (mapping, Series or sequence) and return
self.
- to_dataframe(weighted=True)[source]#
Incidence matrix as a labelled
DataFrame(vertices x hyperedges).
- property weights#
Hyperedge weights \(w_e\).