Skip to main content

Rust implementation of Ripser for topological data analysis, optimized for CANNS library

Project description

CANNs-Ripser

CI PyPI version License

High-performance Rust implementation of Ripser for topological data analysis, optimized for the CANNS library.

🚀 Performance Highlights

CANNs-Ripser delivers significant performance improvements over the original ripser.py:

  • Mean speedup: 1.13x across 54 benchmarks
  • Peak speedup: Up to 1.82x on certain datasets
  • Memory efficiency: 1.01x memory ratio (stable usage)
  • Perfect accuracy: 100% match with ripser.py results

Performance by Category

Top Performing Scenarios

Dataset Type Configuration Speedup
Random N(0,I) d=2, n=500, maxdim=2 1.82x
Two moons n=400, noise=0.08, maxdim=2 1.77x
Random N(0,I) d=2, n=200, maxdim=2 1.72x
Random N(0,I) d=3, n=500, maxdim=2 1.72x
Random N(0,I) d=3, n=200, maxdim=2 1.66x

Overview

CANNs-Ripser is a high-performance Rust implementation of the Ripser algorithm for computing Vietoris-Rips persistence barcodes. It provides a Python interface that's fully compatible with the original ripser.py package, making it a drop-in replacement with significantly improved performance.

Features

🔥 Performance Optimizations (v0.4.0)

  • Algorithmic improvements: Row-by-row edge generation, binary search for sparse matrices
  • Memory optimization: Structure-of-Arrays layout, intelligent buffer reuse
  • Parallel processing: Multi-threading with Rayon (enabled by default)
  • Cache efficiency: K-major binomial coefficient layout, aggressive inlining
  • Zero-copy operations: Minimized allocations in hot paths

🔧 Core Features

  • Full Compatibility: Drop-in replacement for ripser.py with identical API
  • Multiple Metrics: Support for Euclidean, Manhattan, Cosine, and custom distance metrics
  • Sparse Matrices: Efficient handling of sparse distance matrices with neighbor intersection algorithms
  • Cocycle Computation: Optional computation of representative cocycles
  • Progress Tracking: Built-in progress bars and verbose output
  • CANNs Integration: Optimized for use with the CANNs Python Library for Continuous Attractor Neural Networks

Installation

From PyPI (Recommended)

pip install canns-ripser

From Source

git clone https://github.com/Routhleck/canns-ripser.git
cd canns-ripser
pip install maturin
maturin develop --release

Quick Start

Basic Usage

import numpy as np
from canns_ripser import ripser

# Generate sample data
data = np.random.rand(100, 3)

# Compute persistence diagrams
result = ripser(data, maxdim=2)
diagrams = result['dgms']

print(f"H0: {len(diagrams[0])} features")
print(f"H1: {len(diagrams[1])} features") 
print(f"H2: {len(diagrams[2])} features")

Advanced Options

# High-performance computation with progress tracking
result = ripser(
    data, 
    maxdim=2,
    thresh=1.0,                    # Distance threshold
    coeff=2,                       # Coefficient field Z/2Z  
    do_cocycles=True,              # Compute representative cycles
    verbose=True,                  # Detailed output
    progress_bar=True,             # Show progress
    progress_update_interval=1.0   # Update every second
)

# Access results
diagrams = result['dgms']          # Persistence diagrams
cocycles = result['cocycles']      # Representative cocycles  
num_edges = result['num_edges']    # Number of edges in complex

Sparse Matrix Support

from scipy import sparse

# Create sparse distance matrix
row = [0, 1, 2]
col = [1, 2, 0] 
data = [1.0, 1.5, 2.0]
sparse_dm = sparse.coo_matrix((data, (row, col)), shape=(3, 3))

# Compute with sparse matrix (automatically detected)
result = ripser(sparse_dm, distance_matrix=True, maxdim=1)

Performance Guide

When to Expect Best Performance

  • Medium to large datasets (n > 200 points)
  • Higher-dimensional homology (maxdim ≥ 2)
  • Moderately dense point clouds (not extremely sparse)
  • Random or structured data (vs. adversarial/pathological cases)

Optimization Tips

# Enable all performance features
result = ripser(
    data,
    maxdim=2,
    thresh=2.0,        # Set reasonable threshold to limit complex size
    coeff=2,           # Z/2Z is fastest (default)
    progress_bar=False # Disable for batch processing
)

Compatibility

CANNs-Ripser maintains 100% API compatibility with ripser.py:

# These work identically
import ripser              # Original
from canns_ripser import ripser as ripser_fast  # CANNS-Ripser

result1 = ripser.ripser(data, maxdim=2)
result2 = ripser_fast(data, maxdim=2)

# Results are numerically identical
assert np.allclose(result1['dgms'][0], result2['dgms'][0])

Technical Details

Algorithmic Optimizations

  • Dense edge enumeration: O(n²) row-by-row generation vs O(n³) vertex decoding
  • Sparse queries: O(log k) binary search vs O(k) linear scan
  • Cache-friendly data structures: SoA matrix layout, k-major binomial tables
  • Zero-apparent pairs: Skip redundant column reductions in higher dimensions

Implementation Features

  • Memory allocator: mimalloc for efficient small allocations
  • Compilation: Link-time optimization, target-specific vectorization
  • Parallel execution: Rayon-based work-stealing parallelism
  • Error handling: Comprehensive validation with helpful error messages

Development

Building from Source

# Prerequisites
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
pip install maturin

# Build and install
git clone https://github.com/Routhleck/canns-ripser.git
cd canns-ripser
maturin develop --release --features parallel

# Run tests
python -m pytest tests/ -v

Running Benchmarks

cd benchmarks
python compare_ripser.py --n-points 100 --maxdim 2 --trials 5

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Citation

If you use CANNS-Ripser in your research, please cite:

@software{canns_ripser,
  title={CANNS-Ripser: High-Performance Rust Implementation of Ripser},
  author={He, Sichao},
  url={https://github.com/Routhleck/canns-ripser},
  year={2025}
}

Acknowledgments

  • Ulrich Bauer: Original Ripser algorithm and C++ implementation
  • Christopher Tralie & Nathaniel Saul: ripser.py Python implementation
  • Rust community: Amazing ecosystem of high-performance libraries

Related Projects

  • Ripser: Original C++ implementation
  • ripser.py: Python bindings for Ripser
  • CANNS: Continuous Attractor Neural Networks
  • scikit-tda: Topological Data Analysis in Python

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canns_ripser-0.4.4.tar.gz (246.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

canns_ripser-0.4.4-cp313-cp313-win_amd64.whl (257.2 kB view details)

Uploaded CPython 3.13Windows x86-64

canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_x86_64.whl (434.2 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_aarch64.whl (397.5 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ ARM64

canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (370.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (346.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

canns_ripser-0.4.4-cp313-cp313-macosx_11_0_arm64.whl (309.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

canns_ripser-0.4.4-cp313-cp313-macosx_10_13_x86_64.whl (344.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

canns_ripser-0.4.4-cp312-cp312-win_amd64.whl (257.5 kB view details)

Uploaded CPython 3.12Windows x86-64

canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_x86_64.whl (434.5 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_aarch64.whl (397.6 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (346.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

canns_ripser-0.4.4-cp312-cp312-macosx_11_0_arm64.whl (309.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

canns_ripser-0.4.4-cp312-cp312-macosx_10_13_x86_64.whl (344.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

canns_ripser-0.4.4-cp311-cp311-win_amd64.whl (258.8 kB view details)

Uploaded CPython 3.11Windows x86-64

canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_x86_64.whl (435.3 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_aarch64.whl (399.2 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ ARM64

canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (348.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

canns_ripser-0.4.4-cp311-cp311-macosx_11_0_arm64.whl (311.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

canns_ripser-0.4.4-cp311-cp311-macosx_10_12_x86_64.whl (346.2 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file canns_ripser-0.4.4.tar.gz.

File metadata

  • Download URL: canns_ripser-0.4.4.tar.gz
  • Upload date:
  • Size: 246.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for canns_ripser-0.4.4.tar.gz
Algorithm Hash digest
SHA256 747ea54bbef6799f5283e9014ac45610f799baf4f84bc800af798a35a532893a
MD5 3d919d6e7732b646fdff8812244dbd61
BLAKE2b-256 8627df0a5e879177acced320d4722d458ae51bdc4176116f33bc3d012eb95d0a

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1e0f7efece906055b71729a4265782bdf61a09436d893ef36f4006735bdb182f
MD5 f26845b605290afc697a7ddde8d59da6
BLAKE2b-256 3a1887c7fa76c55f9a516a556a9a4bad508c353961bc2bb933e8722e09a03cfc

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 df6fb0bbf6257843276afd0d228c1e7063ccc0f8d256e56b7be4462b4a4e354e
MD5 6cb2a2d6f3a10497ed2e05001750424b
BLAKE2b-256 7771220be081f9fd6e9f754c77f764b441516121978c02f21c3727e650ad83dc

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e2b5f5611a36a171f48dcb5d7bf7c4ffe1400cdc97f2e7a1bef5133397360dd7
MD5 17022033c55386fd5b1442e3f4112137
BLAKE2b-256 15c92b3b39127b2e12453abfac5f7d1776419f0bb4776b72fa8f234b6758a137

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa1b40213e44c4269678e8f6e95cf26271ddbbee62a0010e855b2a49778e302e
MD5 e51f5e8e4cbd48221088f63507598f4f
BLAKE2b-256 3cf9f6c92c34022aa7c862c403e48686b829a2298647aad4c828ff67ea3d2c07

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ae6ee240d29051c4fba926ada33161997b10cbaf60646efe124077e3e59b08c5
MD5 ec052c0a1afffd0a5afe005f67ed4f2e
BLAKE2b-256 3bb32487654002bd0b264bc7f24b5a192448e14cfc9fe9a7e3ab5c3dd25c14bf

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 932290d62c5dae0a2b7571c54b2c7385f1e5e2c5251387db9411a2f0a1b80054
MD5 0aa79611059cf8dc9be92abcf75007c8
BLAKE2b-256 b45c7e8ef2040301809a64e6344d675050d0017b20349c338bcd756c2acbb606

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 0f5cbd8aa36d57913fa5e47b764db116cfbb2a74415dd17cf8bfd88991ad8549
MD5 7d35d7d43973490236902c96556160f2
BLAKE2b-256 e5bd3fedeca67b2787ed94758e4cc50c60c749f77a975072357c4f271b86d4c1

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a908fdcee3f5e87d154bf87c3c14c504e9532fea07a375e2c3d8e1faa3a12dab
MD5 6b98518a508b358651b81a7ab43d6f46
BLAKE2b-256 c7fa116cb56631678b6aeb21ee027f5b66273a7e4d77a49986d8b1bb161a6ae9

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dbe341e775bfd3d46f70b58cb689c6456455d973dc0dec83a157ef71dcdbc8a2
MD5 1d72a09535c1edc2703516ed6d4aa9bc
BLAKE2b-256 4387b574cc29c7170138f1cc0e3e255cfb8120a5ff639d394095bb7a968a7f95

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c0ca471b67feddff6fea39c230c8d774728faf7ee527361a5f68d8d182603fe1
MD5 a4b2e76647ed45acb1f5789d7c0dddd0
BLAKE2b-256 13beb78809ca534790fb9f1d4acde4ddcb59b748818a586b2e4e72dddc4a5aac

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea2e2c5f82d3555642d2c63297b3a194d7fe37fc1ac551d6df540561dd44d450
MD5 7c76aec58ac1ef30e9ea637a50d6dce9
BLAKE2b-256 51463c5d61a9b09413df555f6d79b7d03aa64b414ff828d1e8cbf53edfe9901a

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7f1e780eb440b549ea7684b45337c4e01db118054f143b374d566912ba7fd3b8
MD5 e08c8745886cfdf0a506cb2e3ee1be81
BLAKE2b-256 0d122d8c17357875c0db809a327dd1de3c92c6d5615e6786ea3283be959f63c7

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ea964cff00f70a2d7bb282543e0bf69d9449f547e16ccd1ef2c79ede0c44900
MD5 a2d74abd661db1c299d78fe59caad387
BLAKE2b-256 e9c9e6bf6aacfd05dda69c8cedff2786057e274cab4fd426ebb746090150475a

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ce58ea1bf30b4a96de0f80c0dbf5f51db76114d94bc5682b6d9bb653a486d30c
MD5 26c0e162ae63a728ddc5a6e8322118cd
BLAKE2b-256 b5839b2bd7a5c0c076ccebd0619a10037c84d129b489b92bef4b9528c9ca2780

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 43e74e248365237d26e26c873303a81ae09f9ffc6c5e179cab977e12e2469233
MD5 21d4bdecae4387fbb1b59d3ba3588655
BLAKE2b-256 47744d11260170f3a469afb858fb973c2675fd3835fcaf390cc8629adbec14d8

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4b419b9ca9bc95a55716b7557fbd875433dbc5e3c421c8b380def9d646dfcffc
MD5 ee4b29abbbcc3e8a5422254f96c7786c
BLAKE2b-256 fa3b86b87898f6f443f7f4e0096bed41fbd9490605dfc4a37cd09cb3cc0db192

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2d175f0eb65f1f67d303047c8ef00b2e2502cd04ad8b8ab8a5078b5a54dd5b1f
MD5 4d4f2be4f980031a1dc8ae9b84c34ff1
BLAKE2b-256 4165024ac6a517bff8bcd2aa8a2c32311474fbfcdbab29fec8ad3fd557b9c974

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 278b680383cb0c890ccc98144c46a5c887c0830db001c5fd29037dd1e7e8d0e5
MD5 5d493ec3c0b8a75c740f7cae55e39624
BLAKE2b-256 5f7551bb3f8b2e710fed73e13b39ad63634ddb28f8c11c84ce2c22ad82eb5aee

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9971eaac0e93dfeeb479dd978731da875d8fbb59a8fb075ced417d10dd707eb8
MD5 96d66dc3654f6b7a61c0de51bbaafde9
BLAKE2b-256 7ea47ffeaa6e87bcc084020c98f2aafbc9a202476abece67eedfee164299600b

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aa7d73afa4b212dbafe65af9eced7eee04db0aafea20096f77aeb6ddc4e62725
MD5 459e7235ae37c76e07af7d40c9c03c7e
BLAKE2b-256 d7817316ac8d7b85137a2757c9c682d2a01094be4bb176a77da61fe9c3ecc2ca

See more details on using hashes here.

File details

Details for the file canns_ripser-0.4.4-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for canns_ripser-0.4.4-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 73fd0a9711874ab87b880fdbdfaff53c15fc92b7375e635d6d6050cba56c84b9
MD5 218808cdc05bb36ae3b4cf8ae2c1b163
BLAKE2b-256 ad54510effb2d790d8bc3a1a44b34cdcced58d6bc8175aec0964d1ddd148136b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page