Skip to content

Module: denoise

File: src/spatial_graph_algorithms/denoise/__init__.py Status: Implemented — scorers, filters, one-call denoising, and ground-truth evaluation.


Purpose

Remove likely-false edges from a SpatialGraph without knowing the ground truth. The input is a noisy graph; the output is a SpatialGraph with suspect edges removed.

This module is the complement of the false-edge injection in simulate — the goal is to recover an edge set closer to the true proximity graph.


API

from spatial_graph_algorithms.denoise import denoise

sg_clean = denoise(sg_noisy, method="jaccard", fraction_to_remove=0.05)

The same entry point works for the local walk-support scorer:

sg_clean = denoise(
    sg_noisy,
    method="local_walk_support",
    fraction_to_remove=0.05,
    sample_fraction=0.25,
    walk_len=8,
    walks_per_edge=500,
    seed=42,
)

For a two-step workflow, score first and pass the same method name to the filterer. Score polarity is resolved from SCORE_POLARITY; method descriptions live in SCORING_METHODS, so user code should not track score direction or method meaning manually.

from spatial_graph_algorithms.denoise import EdgeFilterer, score_edges
from spatial_graph_algorithms.metrics import evaluate_denoising

scores = score_edges(sg_noisy, method="jaccard")
sg_clean = EdgeFilterer().by_fraction(
    sg_noisy, scores, fraction=0.10, method="jaccard"
)
report = evaluate_denoising(sg_clean, scores=scores, method="jaccard")

Scoring Methods

Each method returns a dict[tuple[int, int], float] keyed by canonical edge tuples (min(u, v), max(u, v)). Polarity (high or low score = suspect) is registered in SCORE_POLARITY and resolved automatically by all filterers.

Method Polarity How it works Cost
betweenness high = suspect Edge betweenness centrality via NetworkX O(VE) — slow on large graphs
jaccard low = suspect Jaccard neighbourhood overlap; unipartite only O(E·k)
square_bipartite low = suspect 4-cycle count; bipartite only O(E·k²)
community low = suspect Louvain intra-community consistency (igraph, n_runs=5) O(E log V) per run
random_walk low = suspect Stochastic walk visitation frequency with bridge rejection O(n · walks · steps); allocates n×n matrix
degree high = suspect max(deg_u, deg_v) from CSR indptr; no graph construction O(n + E)
edge_support low = suspect Endpoint random-walk support against local neighbourhoods O(edges * walks_per_edge * walk_len)
reconstruction_length high = suspect Reconstruct coordinates, then rank edges by Euclidean length Depends on reconstruction method
walk_visit_expansion low = suspect Bridge-filtered walk visitation matrix followed by repeated matrix expansion O(n · walks · steps + n³ · expansions); allocates n×n matrix
local_walk_support low = suspect Endpoint random-walk support against local neighbourhoods; supports edge sampling O(sampled_edges * walks_per_edge * walk_len)

walk_visit_expansion is the older walk scorer: it launches walks from every node, discards walks that cannot show two edge-disjoint return paths, normalises the visitation matrix, then applies repeated matrix squaring to expand the signal.

edge_support estimates whether each edge is supported by random walks from its endpoints. It uses numba kernels for production-speed scoring. Scores below 1.0 indicate weaker-than-expected local support. The coherence module can sample those scores to estimate a practical prune fraction before denoising:

from spatial_graph_algorithms.denoise import denoise
from spatial_graph_algorithms.spatial_coherence import estimate_edge_removal

estimate = estimate_edge_removal(
    sg_noisy,
    sample_size=2_000,
    walk_len=8,
    walks_per_edge=2_000,
`local_walk_support` estimates whether each edge is supported by random walks from
its endpoints. It uses `numba` kernels for production-speed scoring. Lower
scores are more suspicious; scores below `1.0` indicate weaker-than-expected
local support. Use `sample_size` or `sample_fraction` for large graphs when you
want a fast estimate or a partial filter, or omit both arguments to score all
edges.

For diagnostics, call the result helper directly. This exposes per-edge scores,
the `scored_mask`, runtime, and threshold summaries:

```python
from spatial_graph_algorithms.denoise import denoise
from spatial_graph_algorithms.denoise.ES import local_walk_support_result

result = local_walk_support_result(
    sg_noisy.adjacency_matrix,
    sample_fraction=0.10,
    walk_len=10,
    walks_per_edge=500,
)
fraction = estimate["estimated_removal_fraction"]

sg_clean = denoise(
    sg_noisy,
    method="local_walk_support",
    fraction_to_remove=fraction,
    walk_len=10,
    walks_per_edge=500,
)

reconstruction_length can dispatch through every reconstruction method. It defaults to SVD, which automatically uses a biadjacency matrix for bipartite graphs:

from spatial_graph_algorithms.denoise import (
    compare_reconstruction_length_methods,
    denoise,
)

comparison = compare_reconstruction_length_methods(
    sg_noisy,
    methods=("svd", "mds", "landmark_mds", "strnd"),
    method_kwargs={"landmark_mds": {"n_landmarks": 64}},
)

sg_clean = denoise(
    sg_noisy,
    method="reconstruction_length",
    reconstruction_method="svd",
    fraction_to_remove=0.10,
)

The comparison reports runtime, CPD, KNN preservation, average precision, and precision among the top n_false longest edges when ground truth is available.

SVD is the provisional default. In an initial seeded 80-node comparison it had the best false-edge average precision on both KNN (0.722) and bipartite KNN (0.554) graphs, while taking about 0.1 seconds after UMAP warmup. STRND took roughly 13-21 seconds and MDS achieved slightly better bipartite CPD in that single run. These figures are indicative, not universal; use the comparison helper across representative graph sizes and seeds before changing the default.


Filtering Strategies

All strategies accept the same (sg, scores, *, method) signature and return a new SpatialGraph with edge_metadata["is_removed"] set.

Strategy Description
by_fraction(fraction=0.05) Remove the worst fraction of scored edges
by_threshold(threshold=t) Remove edges above/below an explicit score value
by_percentile(percentile=5.0) Remove edges outside a percentile band
by_optimal_f1(sg, scores) Oracle: sweep thresholds to maximise F1 vs ground truth

by_optimal_f1 is a ground-truth oracle and only works on simulated graphs with edge_metadata["is_false"]. Use it to establish the upper bound for a scorer.


Implementation Notes

  • SpatialGraph is never mutated — all filterers return a new object via sg.replace().
  • Removed edges are recorded in edge_metadata["is_removed"]; the column is created from scratch if edge_metadata was None.
  • Score polarity lives in SCORE_POLARITY; consumers must not pass a direction flag.
  • jaccard raises ValueError on bipartite graphs; square_bipartite raises on unipartite.
  • edge_support lives in ES.py; EdgeScorer.edge_support() scores the graph, while spatial_coherence.estimate_edge_removal() owns sampling for a graph-level removal estimate.
  • Canonical method descriptions live in SCORING_METHODS; compatibility aliases map random_walk to walk_visit_expansion and edge_support to local_walk_support.
  • local_walk_support lives in ES.py; EdgeScorer.local_walk_support() returns a simple score dict, while spatial_graph_algorithms.denoise.ES.local_walk_support_result() exposes diagnostics such as threshold_summary().

File Structure

denoise/
├── __init__.py          — SCORE_POLARITY, EdgeScorer, EdgeFilterer, score_edges(), denoise()
└── ES.py                — local walk-support random-walk scoring and diagnostics