perf(core): colocalization + contiguity micro-optimizations#94
Merged
Conversation
Five small, output-preserving speedups (measured end-to-end, single-threaded, verified against baseline): colocalization - _pearson_pair: derive the OLS slope as cov/var instead of a per-object scipy.linalg.lstsq + design-matrix build (~1.15-1.21x on get_correlation_pearson; slope matches lstsq to ~1e-6, Pearson r unchanged). Drops the now-unused lstsq import. - Costes (linear + bisection): extract fi[non_zero]/si[non_zero] once instead of re-slicing 4x/3x per object (~1.07-1.36x on get_correlation_costes; bit-identical). contiguity (labels are 1..N by contract, see cp_measure._sanitize) - get_sizeshape: nobjects = int(masks.max()) instead of (numpy.unique(masks) > 0).sum() (~1.0-1.19x, size-dependent; bit-identical). - get_feret: numpy.arange(1, masks.max()+1) instead of numpy.unique(ijv[:, 2]) (~1.03-1.18x; bit-identical). - labels_to_binmasks: numpy.arange(1, masks.max()+1) instead of numpy.unique(masks) (drops a full-image sort; bit-identical under the contract). All 95 non-numba tests pass; ruff + mypy clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Benchmark —
|
| size \ objects | 16 | 64 | 256 |
|---|---|---|---|
| 256 | 1.10× | 1.11× | 1.08× |
| 512 | 1.12× | 1.14× | 1.11× |
| 1024 | 1.13× | 1.13× | 1.13× |
pearson
| size \ objects | 16 | 64 | 256 |
|---|---|---|---|
| 256 | 1.66× | 1.56× | 1.30× |
| 512 | 1.72× | 1.59× | 1.53× |
| 1024 | 1.60× | 1.66× | 1.57× |
afermg
approved these changes
Jul 6, 2026
afermg
left a comment
Owner
There was a problem hiding this comment.
It makes sense, this PR leverages the assumptions of labels being contiguous to avoid unnecessary np.uniqu calls, as well as assigning intermediate variables to avoid indexing the same thing multiple times.
| coeffs = lstsq(numpy.array((fi, numpy.ones_like(fi))).transpose(), si)[0] | ||
| return float(corr), float(coeffs[0]) | ||
| # OLS slope of si on fi is cov/var — avoids a per-object lstsq + design-matrix build. | ||
| slope = numpy.cov(fi, si)[0, 1] / numpy.var(fi, ddof=1) |
Owner
There was a problem hiding this comment.
Crazy that this is faster than the scipy function
| non_zero = (fi > 0) | (si > 0) | ||
| xvar = numpy.var(fi[non_zero], axis=0, ddof=1) | ||
| yvar = numpy.var(si[non_zero], axis=0, ddof=1) | ||
| fz = fi[non_zero] |
Owner
There was a problem hiding this comment.
We are avoiding indexing twice, makes sense.
|
|
||
| labels = masks | ||
| nobjects = (numpy.unique(masks) > 0).sum() | ||
| nobjects = int(masks.max()) # contiguous 1..N (see cp_measure._sanitize) |
Owner
There was a problem hiding this comment.
Cool, the contiguous assumption pays off.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Five small, output-preserving speedups found by an end-to-end perf audit (measured single-threaded, each verified
allclosevs baseline).Colocalization
_pearson_pair: OLS slope ascov/varinstead of a per-objectscipy.linalg.lstsq+ design-matrix build — ~1.15–1.21× onget_correlation_pearson(slope matcheslstsqto ~1e-6, Pearson r unchanged). Drops the now-unusedlstsqimport.fi[non_zero]/si[non_zero]once instead of re-slicing 4×/3× per object — ~1.07–1.36× onget_correlation_costes, bit-identical.Contiguity (labels are 1..N by contract,
cp_measure._sanitize)get_sizeshape:nobjects = int(masks.max())instead of(numpy.unique(masks) > 0).sum()— ~1.0–1.19× (size-dependent), bit-identical.get_feret:numpy.arange(1, masks.max()+1)instead ofnumpy.unique(ijv[:, 2])— ~1.03–1.18×, bit-identical.labels_to_binmasks:arangeinstead ofnumpy.unique(masks)— drops a full-image sort, bit-identical under the contract.