Skip to content

Cython speedup on custom distclassipy metrics - #38

Merged
sidchaini merged 12 commits into
mainfrom
cython
Nov 15, 2026
Merged

sidchaini merged 12 commits into
mainfrom
cython

Conversation

@sidchaini

Copy link
Copy Markdown
Owner

Written non-SciPy functions in Cython to enable SciPy-like speedup in compute speed & performance.

@sidchaini

Copy link
Copy Markdown
Owner Author

Classifier speed-up

Distance Metric Old Time (s) Cython Time (s) Speedup
meehl 0.07970 0.00184 43.32x
divergence 0.05467 0.00183 29.87x
penroseshape 0.05428 0.00189 28.72x
hellinger 0.04485 0.00166 27.02x
clark 0.04836 0.00188 25.72x
add_chisq 0.03995 0.00168 23.78x
prob_chisq 0.03728 0.00167 22.32x
squared_chisq 0.03705 0.00169 21.92x
vicis_symmetric_chisq 0.04066 0.00207 19.64x
wave_hedges 0.04291 0.00230 18.66x
matusita 0.02933 0.00162 18.10x
vicis_wave_hedges 0.03735 0.00214 17.45x
taneja 0.06286 0.00377 16.67x
squaredchord 0.02692 0.00167 16.12x
tanimoto 0.02795 0.00176 15.88x
google 0.02754 0.00175 15.74x
acc 0.03339 0.00215 15.53x
sorensen 0.02224 0.00157 14.17x
czekanowski 0.02222 0.00157 14.15x
kulczynski 0.02338 0.00180 12.99x
soergel 0.02251 0.00177 12.72x
motyka 0.02059 0.00176 11.70x
jensenshannon_divergence 0.08277 0.00719 11.51x
ruzicka 0.02117 0.00188 11.26x
topsoe 0.07750 0.00711 10.90x
jeffreys 0.05003 0.00477 10.49x
kumarjohnson 0.06274 0.00617 10.17x
gower 0.01366 0.00149 9.17x
jensen_difference 0.07820 0.00865 9.04x
lorentzian 0.03221 0.00408 7.89x
jaccard 0.01239 0.00184 6.73x
chebyshev_min 0.01342 0.00210 6.39x
marylandbridge 0.01041 0.00171 6.09x
squared_euclidean 0.00844 0.00156 5.41x
dice 0.00177 0.00164 1.08x

Anomaly detector speedup

Distance Metric Old Time (s) Cython Time (s) Speedup
DistanceAnomaly 0.40381 0.01542 26.19x

Brings in the Dependabot action bumps (checkout v7, setup-python v7,
codecov-action v7.1.1).

Conflict resolution: publish-to-pypi.yml was deleted on this branch but
modified on main. Keep it: PyPI trusted publishing is registered against
this workflow filename (v0.2.3 was published through it). It is rewritten
for compiled wheels in a follow-up commit.
Typed memoryviews declared as double[:, ::1] request a writable buffer,
so read-only float64 C-contiguous inputs (which np.ascontiguousarray
passes through uncopied) raised "ValueError: buffer source array is
read-only". SciPy's cdist accepts these, so this was a regression for
e.g. joblib memmaps under n_jobs > 1, np.load(mmap_mode="r"), pandas
copy-on-write arrays, and DistanceAnomaly(scale=False) fit on a single
class (1-row centroid frame).

Declare the input memoryviews const; the kernels only read from them.
It is regenerated from _cdistances.pyx on every build (Cython is a
build requirement) and .gitignore already excludes it; tracking it meant
every local build dirtied the working tree and the diff carried ~31k
generated lines.
0.3.0 is scoped to the Cython speedup only. Remove the experimental
dispersion_stat="aiqr" / "cdf" options, the cdf_grid_size parameter,
their tests, and DistanceAnomaly's all-NaN metric-column drop (needed
only under cdf). The _class_transform refactor is kept for std/iqr; it
reproduces the original scaling exactly (test_std_path_unchanged).

To restore the work for 0.4.0: git revert <this commit>.
Next minor after 0.2.3 (latest on PyPI): Cython kernels for the custom
metrics, distclassipy.cdist, DistanceAnomaly exported at top level, plus
the meehl/dice/mutation fixes described in the PR.
Fixes the failing "Build and test sdist" check on PR #38 and the release
pipeline, which had never run:

- Remove ci.yml. Its pytest matrix duplicated testing-and-coverage.yml,
  and its tag-triggered publish job would have broken PyPI trusted
  publishing (registered against publish-to-pypi.yml, no environment).
- testing-and-coverage.yml: add Python 3.14 and an sdist job that installs
  the sdist and runs the full suite from outside the checkout. The old
  smoke test ran `python -c "import distclassipy"` from the repo root, so
  it imported the uncompiled source tree instead of the installed wheel
  ("No module named 'distclassipy._cdistances'").
- publish-to-pypi.yml: same filename and release trigger, but build the
  sdist plus cibuildwheel wheels (cp310-cp314; Linux x86_64/aarch64,
  Windows AMD64, macOS x86_64/arm64) before publishing. Wheel tests use
  CIBW_TEST_SOURCES: running pytest on {project}/tests would put the repo
  root on sys.path (tests/__init__.py) and hit the same shadowing bug.
@sidchaini

Copy link
Copy Markdown
Owner Author

Cython kernels for all custom metrics (38-500x faster; DistanceAnomaly ~78x),
plus distclassipy.cdist and a top-level DistanceAnomaly export.

Small breaking change in behaviour:

  • meehl: fixed to the textbook x_{i+1} formula (was x_{i-1} with wraparound)/
    thiswill change meehl values and shifts default DistanceAnomaly scores ~1-4%.
  • metric="dice" now uses the custom (Cha 2007) formula, not SciPy's boolean dice.
  • jeffreys/taneja/topsoe/jensenshannon_divergence no longer modify the
    caller's input in place (and no longer crash under pandas 3).

@sidchaini
sidchaini merged commit 1ea6635 into main Nov 15, 2026
20 checks passed
@sidchaini
sidchaini deleted the cython branch November 15, 2026 21:15
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