Skip to content

63 community link prediction - #162

Open
eriknw wants to merge 1 commit into
python-graphblas:31-node-attrsfrom
eriknw:63-community-links
Open

eriknw wants to merge 1 commit into
python-graphblas:31-node-attrsfrom
eriknw:63-community-links

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add cn_soundarajan_hopcroft, ra_index_soundarajan_hopcroft and within_inter_cluster. They read each node's community, which the conversion now carries for them as it does for the attribute mixing names (#130); before, it refused, and they always fell back. The scores come from products masked to the pairs: the common neighbors over the adjacency, and those in the pair's community over the edges within communities, weighted by 1 / degree for the resource allocation index.

  • Values. cn's counts are NetworkX's exactly, and so are within_inter_cluster's ratios, to the last bit: the counts are exact, and NetworkX's one addition and one division are done in float64 here too. The resource allocation sums are added in another order than Python's sum, so a score may differ in its last bits (relative 4.5e-16 at most on the random graphs below, and 1.3e-15 with up to 369 common neighbors). NetworkX's int 0, for a pair of two communities or an empty sum, stays an int. A node whose community is NaN is in no community, not even its own, as NetworkX's == finds.
  • Order. The triples come in NetworkX's order, for a given ebunch and for every non-edge.
  • Errors, as NetworkX raises them and when. Directed graphs and multigraphs raise NetworkXNotImplemented on the call. delta <= 0 raises NetworkXAlgorithmError on the call, before ebunch is checked. A node of ebunch that is not in the graph raises NodeNotFound on the call, and an iterator ebunch is used up by that check, so nothing is scored. A node without the community raises NetworkXAlgorithmError ("No community information available for Node u", from a KeyError) only when the generator reaches the first pair that looks it up: NetworkX looks up u, then v, and the common neighbors only where u and v are of one community. The triples before that pair are yielded, and the node is named as the caller wrote it.
  • Left to NetworkX. A first failing pair with two or more common neighbors without the community: NetworkX names the first of them in the order of a set it builds from its adjacency, which a matrix does not keep. The pairs are checked on the call; with every non-edge, a node with a community and two or more neighbors without one is enough to decline. Community values other than ints, floats, complex numbers, strings, bytes, None, and tuples or frozensets of them: another type's == may differ from how a dict keys it, and NumPy's may raise (np.bool_(True) == 2**70 raises OverflowError inside NetworkX's generator). A delta other than a Python float or an int below 2**52 (NetworkX adds an int exactly). And, as for the mixing names, a community that is None, callable or unhashable, and a graph class with its own adjacency or nodes.
  • Output size. No bound is needed. The pairs of a given ebunch are scored in one masked product, so memory follows the caller's list, and every non-edge is scored a node at a time, as NetworkX hands them out. Reading every non-edge of a 3000-node graph (4.5e6 pairs) peaks at 117 MiB, and of a 6000-node graph (1.8e7 pairs) at 129 MiB (NetworkX: 59 and 66 MiB; the difference is GraphBLAS and the conversion).
  • Speed (Indicative, interleaved with NetworkX, best of 3, one process, conversion included; load 8.5 to 11.5). 3e6 random pairs on a Barabasi-Albert graph of 1e5 nodes and 5e5 edges, 20 communities: cn 5.4x (NetworkX 5.6 s), ra 3.3x (3.0 s), within 2.4x (3.2 s). Every non-edge of a 3000-node graph: cn 9.7x (5.5 s), ra 3.6x (1.7 s), within 2.7x (1.6 s). 1e6 pairs on a denser graph of 1e4 nodes and 5e5 edges: cn 10.6x (4.4 s); ra 3.5x and within 1.9x, where NetworkX takes under a second. On a cached conversion each is up to 1.2x faster again. Much of what is left is making the Python triples, which NetworkX's loop pays too.
  • Not covered: a community written in place (G.nodes[u]["community"] = c) empties no NetworkX cache, so with cache_converted_graphs on, a cached conversion keeps the old value, as for the mixing names.
  • Also changed: _predict takes a check run on the call, and node_attribute_rule the name of the parameter that names the attribute.
  • Tests. 330 random graphs of up to 27 nodes, with self-loops, isolated nodes, node keys out of order, community values of several kinds (ints, strings, equal numbers of several types, strings beside ints and None, tuples, NaN objects) and up to four nodes without one; each name with random pairs (self-pairs and edges among them) and with every non-edge. Against NetworkX: the triples, their order and score types, the scores (ints and ratios exactly, sums within 1e-12 relative), and where and how a missing community raises; a declined call reaches NetworkX's answer under the backend priority. Also each error and its moment, each decline, delta's values and types, the ebunch checks and containers, small graphs, views, a conversion cached for another name, and direct calls. 32 mutants of the kernel, the wrappers and the attribute rule each fail them, in NetworkX dev, 3.7 and 3.6.1.
  • NetworkX's tests (test_link_prediction.py) through the backend in dev, 3.7 and 3.6.1: the same 97 pass before and after, with 34 community calls served in each.

Stacked on #130.

cn_soundarajan_hopcroft, ra_index_soundarajan_hopcroft and
within_inter_cluster read each node's community, and the conversion
refused node attributes for them, so they always fell back. It now
carries the community for them as it does for the attribute mixing
names. The scores come from products masked to the pairs: the common
neighbors over the adjacency, and those in the pair's community over
the edges within communities, weighted by 1 / degree for the resource
allocation index.

The counts, and within_inter_cluster's ratios, are NetworkX's exactly;
the resource allocation sums are added in another order, so within
rounding. The triples come in NetworkX's order, and a node without the
community raises NetworkXAlgorithmError when the pair that looks it up
is reached, as there. Where two or more common neighbors of that pair
lack it, NetworkX names the first in its sets' order, which a matrix
does not keep, so such a call is left to it; so are community values
other than plain Python ones, whose `==` may not be a dict's.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.79592% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.94%. Comparing base (71111ea) to head (0c0a532).

Additional details and impacted files
@@                Coverage Diff                @@
##           31-node-attrs     #162      +/-   ##
=================================================
+ Coverage          93.73%   93.94%   +0.21%     
=================================================
  Files                196      197       +1     
  Lines              13062    13536     +474     
  Branches            1522     1581      +59     
=================================================
+ Hits               12244    12717     +473     
  Misses               560      560              
- Partials             258      259       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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