Skip to content

Speed up copies, edge removal and JSON saving; fix three bugs - #53

Merged
FraLotito merged 1 commit into
mainfrom
core-fixes
Nov 17, 2026
Merged

FraLotito merged 1 commit into
mainfrom
core-fixes

Conversation

@FraLotito

Copy link
Copy Markdown
Contributor

Performance, measured on the 172,000 interactions of the high-school contacts:

  • copy() copies the hypergraph's containers and shares their immutable contents, instead of calling copy.deepcopy: 1.43 s to 0.38 s.
  • remove_nodes(..., keep_edges=False) and remove_edges of Hypergraph and TemporalHypergraph remove all edges in one pass over each member's incident edges. Removing them one at a time was quadratic for nodes in many edges (keeping 50 of 327 nodes: about 3 s to 0.1 s). The result is the same as removing one at a time. Directed and multiplex hypergraphs keep their own removal.
  • save_json_hypergraph writes with json.dumps, which uses the C encoder: 5.3 s to 1.4 s, with byte-identical output.

Fixes:

  • annealed_adjacency_matrix assumed consecutive integer times, so it raised KeyError at the first gap. It also shaped the matrix by the largest node index and counted each pair twice in one triangle. It now returns the mean of the snapshots' adjacency matrices: symmetric, n x n, with a zero diagonal.
  • s_betweenness_nodes_averaged and s_closenness_nodes_averaged raised TypeError on numbered nodes and dropped named nodes containing an "E".
  • Loading a JSON file with tuple nodes raised TypeError, since JSON writes tuples as lists. Tuple nodes are now read back as tuples.

Behaviour changes:

  • remove_nodes and remove_edges check every node or edge before removing any. A missing one, or an edge listed twice, raises and leaves the hypergraph unchanged.
  • annealed_adjacency_matrix returns corrected values.

Tests cover copies, batch removal against removal one at a time, the annealed adjacency against the mean of snapshots with gaps in time, centralities on numbered and named nodes, and the JSON round trip of tuple nodes.

Performance, measured on the 172,000 interactions of the high-school
contacts:
- copy() copies the hypergraph's containers and shares their immutable
  contents, instead of calling copy.deepcopy: 1.43 s to 0.38 s.
- remove_nodes(..., keep_edges=False) and remove_edges of Hypergraph and
  TemporalHypergraph remove all edges in one pass over each member's
  incident edges. Removing them one at a time was quadratic for nodes in
  many edges (keeping 50 of 327 nodes: about 3 s to 0.1 s). The result is
  the same as removing one at a time. Directed and multiplex hypergraphs
  keep their own removal.
- save_json_hypergraph writes with json.dumps, which uses the C encoder:
  5.3 s to 1.4 s, with byte-identical output.

Fixes:
- annealed_adjacency_matrix assumed consecutive integer times, so it raised
  KeyError at the first gap. It also shaped the matrix by the largest node
  index and counted each pair twice in one triangle. It now returns the
  mean of the snapshots' adjacency matrices: symmetric, n x n, with a zero
  diagonal.
- s_betweenness_nodes_averaged and s_closenness_nodes_averaged raised
  TypeError on numbered nodes and dropped named nodes containing an "E".
- Loading a JSON file with tuple nodes raised TypeError, since JSON writes
  tuples as lists. Tuple nodes are now read back as tuples.

Behaviour changes:
- remove_nodes and remove_edges check every node or edge before removing
  any. A missing one, or an edge listed twice, raises and leaves the
  hypergraph unchanged.
- annealed_adjacency_matrix returns corrected values.

Tests cover copies, batch removal against removal one at a time, the
annealed adjacency against the mean of snapshots with gaps in time,
centralities on numbered and named nodes, and the JSON round trip of
tuple nodes.
@FraLotito
FraLotito merged commit 4020a12 into main Nov 17, 2026
1 check failed
@FraLotito
FraLotito deleted the core-fixes branch November 17, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant