dyng.triad_count#

dyng.triad_count: exact triad counts of a dynamic hypergraph (ESCHER / ESCHER+).

compute() counts the closed triads of a dyng.Hypergraph (three hyperedges that pairwise share a vertex: the triangles of the line graph) by one of four patterns:

  • "h_motif": the 30 h-motif classes of the triads’ Venn regions (bin k is ESCHER-GPU’s h-motif k + 1; bins 20-25, ESCHER-GPU’s open triads 21-26, are always 0);

  • "stathyper_type1": one bin, the sum of C(|e|, 3) over the live hyperedges;

  • "stathyper_type2": one bin, the sum of C(a, 2) * b * c over the adjacent pairs;

  • "stathyper_type3": one bin, the sum of x * y * z over the closed triads.

update() applies a batch of hyperedge deletions and insertions and changes every bin by the triads the batch destroys and creates (the subtract phase on the old hypergraph, the add phase on the new one), without recounting the rest; afterwards the counts equal compute() on the new hypergraph exactly. dyng.update() updates several results (one per pattern, say) with one batch.

The names mirror the C++ API (dyng::triad_count, <dyng/triad_count.hpp>). Supported hypergraphs: int32 vertex ids, unweighted or int32 weights (the weights are ignored). The h-motifs and types 2 and 3 read the line graph and the vertex-to-hyperedge rows: the hypergraph needs line_graph other than "none" and store_v2h=True (the defaults); type 1 needs neither. Cite with dyng.citation("triad_count").

Example

>>> import dyng
>>> hg = dyng.Hypergraph.from_lists([[0, 1, 2], [1, 2, 3], [2, 3, 4], [0, 2, 4]])
>>> tri = dyng.triad_count.compute(hg)
>>> tri.total, tri.num_bins
(4, 30)
>>> st = dyng.triad_count.update(hg, dyng.HyperedgeBatch(delete_hyperedges=[3]), tri)
>>> tri.total, int(st.delta.sum()), st.triads_removed
(1, -3, 3)

Classes#

Options

Options of compute() (C++ dyng::triad_count::options).

Pattern

What a result counts (C++ dyng::triad_count::pattern).

Result

Triad counts kept up to date by update() (C++ dyng::triad_count::result).

Stats

Counters of one update() (C++ dyng::triad_count::stats); all deterministic and

Functions#

compute(→ Result)

Count the closed triads of hypergraph by a pattern.

update(→ Stats)

Apply batch to hypergraph and update the counts result.

Module Contents#

class dyng.triad_count.Options#

Options of compute() (C++ dyng::triad_count::options).

kind is fixed at compute(); cuda_engine is a tunable that never changes what is counted (Result.set_options()).

kind#

The pattern: "h_motif" (30 bins), "stathyper_type1", "stathyper_type2" or "stathyper_type3" (one bin each), or a Pattern.

cuda_engine#

"automatic" or "fused" (the wedge engine); "operators" raises NotSupportedError on cuda (triad_count has no operators engine). Ignored by the host backends.

class dyng.triad_count.Pattern#

Bases: enum.StrEnum

What a result counts (C++ dyng::triad_count::pattern).

A member is also its name as a string (Pattern.h_motif == "h_motif"), so every function that takes a pattern takes either.

Initialize self. See help(type(self)) for accurate signature.

class dyng.triad_count.Result#

Triad counts kept up to date by update() (C++ dyng::triad_count::result).

counts[bin] is the number of triads in that bin (for the StatHyper types the one bin holds the pattern’s weighted sum); num_bins is 30 for "h_motif" and 1 otherwise.

clone(resources: dyng.resources.Resources | None = None) → Result#

A deep copy for resources.

count(bin: int) → int#

The count of one bin.

Raises:

InvalidArgumentError – bin is outside [0, num_bins).

set_options(options: Options | None = None, **kwargs: Any) → None#

Change the tunables (cuda_engine); kind must stay the same.

property counts: numpy.ndarray#

The bins (a read-only uint64 NumPy copy of the current state, host memory on every backend; read it again after an update).

property graph_version: int#

The hypergraph version this result matches.

property kind: Pattern#

The pattern this result counts (fixed at compute()).

property num_bins: int#

30 for "h_motif", 1 for the StatHyper types.

property options: Options#

The options (a copy).

property space: str#

"host" (the counts are a host array on every backend).

property total: int#

The sum of the bins (CapacityError past 2^64 - 1).

class dyng.triad_count.Stats#

Counters of one update() (C++ dyng::triad_count::stats); all deterministic and equal on every backend.

affected#

Bins whose count changed.

iterations#

Always 0.

frontier_visits#

Hyperedges of the two phases (deleted_frontier + inserted_frontier).

fallback_used#

Always False (every backend updates incrementally).

converged#

Always True.

engine_used#

"operators" on the host backends, "fused" on cuda.

batch#

What applying the batch did to the hypergraph (batch.inserted_ids: the id of every inserted hyperedge, int64).

deleted_frontier#

The live hyperedges the batch deleted (the subtract phase).

inserted_frontier#

The hyperedges the batch inserted (the add phase).

triads_removed#

The triads the batch destroyed, summed over the bins (types 1-3, whose one bin is a sum of contributions: the contributions removed; the hyperedge weights are ignored).

triads_added#

The triads the batch created, summed over the bins (types 1-3: the contributions added).

delta#

The change of every bin, counts after minus counts before (a read-only int64 array of the result’s num_bins values).

dyng.triad_count.compute(hypergraph: dyng.hypergraph.Hypergraph, *, kind: PatternName | Pattern | None = None, options: Options | None = None, resources: dyng.resources.Resources | None = None, **kwargs: Any) → Result#

Count the closed triads of hypergraph by a pattern.

Parameters:
  • hypergraph – The hypergraph; it is not modified (line_graph="automatic" builds its line graph here, once).

  • kind – The pattern (Options.kind; default "h_motif").

  • options – Options; kind and the keywords (cuda_engine) override its fields.

  • resources – Default: the resources the hypergraph was built with.

  • **kwargs – Option fields.

Returns:

The counts, matching hypergraph.version.

Raises:
  • InvalidArgumentError – an unknown pattern or engine; a pattern that reads the line graph on a hypergraph built with line_graph="none" or store_v2h=False (the message names the property); "h_motif" or type 3 on a hypergraph of 2,147,483,647 vertices.

  • NotSupportedError – cuda_engine="operators" on cuda, or an unavailable backend.

  • CapacityError – a count exceeds 2^64 - 1.

  • OutOfMemoryError – host or device memory cannot be allocated (also a MemoryError).

dyng.triad_count.update(hypergraph: dyng.hypergraph.Hypergraph, batch: dyng.hypergraph.HyperedgeBatch, result: Result, *, resources: dyng.resources.Resources | None = None) → Stats#

Apply batch to hypergraph and update the counts result.

Postcondition: result equals compute(hypergraph, options=result.options) exactly.

Raises:
  • StaleResultError – result does not match the hypergraph’s current state, or a failed update left it unusable.

  • InvalidArgumentError – the batch is invalid for the hypergraph (nothing changed).

  • NotSupportedError – incidence edits (0.4; nothing changed).

  • CapacityError – a count exceeds 2^64 - 1, or a bin changes by 2^63 or more (after the commit: result is left unusable; recompute it).

  • OutOfMemoryError – host or device memory cannot be allocated (also a MemoryError), with one of three outcomes: before the commit, hypergraph and result are unchanged; inside the commit, hypergraph is unusable (as for dyng.Hypergraph.apply()) and result keeps the counts before the batch (recompute it once the hypergraph is built again); after the commit, hypergraph holds the new version and result is left unusable (recompute it).

  • InternalError – a bin would become negative (a library bug; result is left unusable).