Skip to main content

Parallel compressed cover tree (PCCT) library targeting JAX-based Vecchia GP workloads.

Project description

Covertreex

High-performance cover tree library for k-NN queries, optimized for Vecchia-style Gaussian process pipelines.

PyPI version License

Features

  • Very fast, parallel implementation following Parallel Cover Trees and their Applications
  • Hybrid Python/Numba + Rust implementation
  • AVX2 SIMD optimized dot products in Rust backend
  • Residual correlation metric with RBF and Matérn 5/2 kernels
  • Hilbert curve ordering for cache-efficient tree construction

Installation

pip install covertreex

Quick Start

Basic Euclidean k-NN

import numpy as np
from covertreex import cover_tree

# Build tree and query
coords = np.random.randn(10000, 3).astype(np.float32)
tree = cover_tree(coords)
neighbors = tree.knn(k=10)

# With distances
neighbors, distances = tree.knn(k=10, return_distances=True)

Residual Correlation Metric (Vecchia GP)

For Gaussian process applications with Vecchia approximations:

import numpy as np
from covertreex import cover_tree
from covertreex.kernels import Matern52

coords = np.random.randn(10000, 3).astype(np.float32)

# Option 1: Provide a kernel (V-matrix built internally)
tree = cover_tree(coords, kernel=Matern52(lengthscale=1.0, variance=1.0))
neighbors = tree.knn(k=50)

# Option 2: Provide pre-computed V-matrix (from your GP)
# tree = cover_tree(coords, v_matrix=V, p_diag=p_diag)

# Predecessor constraint (for Vecchia): neighbor j must have j < query i
neighbors = tree.knn(k=50, predecessor_mode=True)

Engine Selection

from covertreex import cover_tree
from covertreex.kernels import Matern52

# cover_tree defaults to rust-hilbert (fastest)
tree = cover_tree(coords, kernel=Matern52(lengthscale=1.0), engine="rust-hilbert")
tree = cover_tree(coords, kernel=Matern52(lengthscale=1.0), engine="rust-natural")
tree = cover_tree(coords, kernel=Matern52(lengthscale=1.0), engine="python-numba")

Profile Presets

from covertreex import Runtime

# Load predefined configuration
runtime = Runtime.from_profile("residual-gold")

# With overrides
runtime = Runtime.from_profile("residual-gold", overrides=["seeds.global_seed=42"])

Available profiles: default, residual-gold, residual-fast, residual-audit, cpu-debug

Performance

Residual correlation k-NN benchmark (AMD Ryzen 9 9950X, N=32k, D=3, k=50):

Engine Build Time Query Throughput
python-numba 7.0s 40,000 q/s
rust-hilbert 0.9s 44,000 q/s

API Reference

cover_tree (recommended)

Factory function for building cover trees. Handles all configuration internally.

from covertreex import cover_tree
from covertreex.kernels import Matern52, RBF

# Euclidean distance (default)
tree = cover_tree(coords)

# Residual correlation with kernel
tree = cover_tree(coords, kernel=Matern52(lengthscale=1.0, variance=1.0))
tree = cover_tree(coords, kernel=RBF(lengthscale=2.0))

# Residual correlation with pre-computed V-matrix
tree = cover_tree(coords, v_matrix=V, p_diag=p_diag, kernel_diag=k_diag)

# Query
neighbors = tree.knn(k=10)
neighbors = tree.knn(k=50, predecessor_mode=True)  # Vecchia constraint
neighbors, distances = tree.knn(k=10, return_distances=True)

Kernel Classes

GP kernels for residual correlation metric:

from covertreex.kernels import Matern52, RBF

# Matérn 5/2 kernel (recommended for GP)
kernel = Matern52(lengthscale=1.0, variance=1.0)

# RBF (squared exponential) kernel
kernel = RBF(lengthscale=2.0, variance=1.0)

CoverTree (advanced)

Lower-level interface with explicit runtime configuration:

from covertreex import CoverTree, Runtime

runtime = Runtime(engine="rust-hilbert", metric="euclidean")
tree = CoverTree(runtime).fit(points)
neighbors = tree.knn(query_points, k=10)

Development

# Install in development mode
pip install -e ".[dev]"

# Build Rust backend
maturin develop --release

# Run tests
pytest

# Lint
ruff check covertreex

CLI (Testing)

A CLI is included for benchmarking and testing:

python -m cli.pcct --help
python -m cli.pcct query --engine rust-hilbert --tree-points 32768 --k 50

License

Apache 2.0 — See LICENSE for details.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

covertreex-0.4.0-cp313-cp313-manylinux_2_34_x86_64.whl (804.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

covertreex-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl (804.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file covertreex-0.4.0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for covertreex-0.4.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 13aefb7913484d57c16b1c8ef8a666b93c155dd2ec4b817bb079597667f14c41
MD5 92dc2cc9bfe54baeb3d8b01b217ee74e
BLAKE2b-256 d8f20aa6f1471c2cf99d7e3e48e3fb7481e2d2cef98db3f852b4b358b4226bbb

See more details on using hashes here.

File details

Details for the file covertreex-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for covertreex-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2bf1494973f6b4b38782d96fd1ff4dda7dbe4bbd9e47f9f87e522954c0d04af9
MD5 b856dcc6be6b41dddd5f48324b8a9ae1
BLAKE2b-256 5a6fb36c7e8e0cab749673228e5f1557eef42faa0631ee0c84f6a0dc0154754b

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