Skip to main content

A package for optimized Convergent Cross Mapping using PyTorch.

Project description

FastCCM

Fast pairwise Convergent Cross Mapping in PyTorch.

FastCCM computes exact CCM scores equivalent to pyEDM>=2.3.2.

Features

  • Pairwise CCM and pairwise S-Map.
  • Separate source and target sets.
  • Vectorized E, tau, tp search and convergence testing.
  • Blocked execution, auto batching, and memmap output for large matrices.

Performance

Measured on CPU, Apple M4 Pro 64GB

CCM matrix timings (E=5, exclusion_window=5)

Condition CCM matrix / simplex (s) CCM matrix / S-MAP (s)
100x100, T=1000 0.075 0.530
200x200, T=1000 0.156 1.188
800x800, T=500 0.721 5.501
100x100, T=8000 3.581 14.194

Single time series timings (E=20, exclusion_window=10)

Condition Simplex projection (s) S-MAP projection (s)
T=2000 0.006 0.010
T=8000 0.045 0.088
T=32000 0.755 1.380
T=128000 12.945 23.922

Installation

Requirements: Python >= 3.9, pip.

CPU-only

pip install torch==2.10.0 --index-url https://download.pytorch.org/whl/cpu
pip install fastccm

CUDA 12.6

pip install torch==2.10.0 --index-url https://download.pytorch.org/whl/cu126
pip install fastccm

macOS (CPU / MPS)

pip install torch==2.10.0
pip install fastccm

Input format

FastCCM expects lists of 2D arrays.

  • X_emb: source embeddings with shape (T, E).
  • Y_emb: target embeddings with shape (T, E_y), or (T, 1) for scalar targets.
  • Different lengths are end-aligned automatically.

Examples

1. Generate data and find optimal E / tau

import numpy as np
from fastccm import PairwiseCCM, Functions, Visualizer, utils
from fastccm.data import get_truncated_rossler_lorenz_rand

system = get_truncated_rossler_lorenz_rand(
    tmax=200,
    n_steps=4000,
    C=2,
    seed=0,
)

funcs = Functions(device="cpu", memory_budget_gb=2.0, verbose=0)
viz = Visualizer()

searches = [
    funcs.find_optimal_embedding_params(
        system[:, i],
        sample_size=400,
        exclusion_window=500,
        E_range=np.arange(1, 20),
        tau_range=np.arange(1, 20),
        tp_range=np.arange(1, 100, 10),
        seed=i,
        subtract_global=False,   # Subtract global linear model fit. Set to True to select E and tau 
                                 # based on the Simplex Projection without autoregression
    )
    for i in range(system.shape[1])
]

opt_E = [res["optimal_E"] for res in searches]
opt_tau = [res["optimal_tau"] for res in searches]

print(opt_E)
print(opt_tau)

viz.visualize_optimal_e_tau(searches[3])

2. Build a pairwise CCM matrix with the selected embeddings

X_emb = utils.embed(system, E=opt_E, tau=opt_tau)
Y_emb = system.T[:, :, None]
ccm = PairwiseCCM(device="cpu", memory_budget_gb=2.0, verbose=0)

scores = ccm.score_matrix(
    X_emb=X_emb,
    Y_emb=Y_emb,
    library_size="auto",
    sample_size="auto",
    exclusion_window=20,
    method="simplex",
    seed=0,
)

ccm_matrix = scores[0]  # Y is scalar, so output shape is (1, n_Y, n_X)
print(ccm_matrix.shape)  # (6, 6)
print(ccm_matrix)

3. Run a convergence test

x_idx = 1
y_idx = 3

X_pair = [utils.embed(system[:, x_idx], E=opt_E[x_idx], tau=opt_tau[x_idx])[0]]
Y_pair = [utils.embed(system[:, y_idx], E=opt_E[y_idx], tau=opt_tau[y_idx])[0]]

conv = funcs.convergence_test(
    X_emb=X_pair,
    Y_emb=Y_pair,
    library_sizes=[100, 200, 400, 800, 1600],
    sample_size="auto",
    exclusion_window=20,
    method="simplex",
    trials=10,
    seed=0,
)

print(conv["library_sizes"])
print(conv["X_to_Y"].shape)  # (n_sizes, trials, E_y, n_Y, n_X)

viz.plot_convergence_test(conv)

4. Run larger jobs in blocks

import numpy as np

rng = np.random.default_rng(0)

X_emb = rng.uniform(0.0, 1.0, size=(50_000, 1000, 5)).astype(np.float32)
Y_emb = rng.uniform(0.0, 1.0, size=(50_000, 1000, 1)).astype(np.float32)

funcs = Functions(device="cpu", memory_budget_gb=2.0, verbose=1)

scores_mm = funcs.score_matrix_blocked(
    X_emb=X_emb,
    Y_emb=Y_emb,
    x_block=100,
    y_block=50_000,
    library_size="auto",
    sample_size="auto",
    exclusion_window=20,
    method="simplex",
    seed=0,
    out_path="ccm_scores.dat",
)

print(type(scores_mm))
print(scores_mm.shape)

Related files

  • notebooks/CCM results comparison.ipynb
  • scripts/benchmark_performance.py
  • scripts/benchmark_single_series_self_prediction.py

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

fastccm-0.3.1.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

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

fastccm-0.3.1-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file fastccm-0.3.1.tar.gz.

File metadata

  • Download URL: fastccm-0.3.1.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastccm-0.3.1.tar.gz
Algorithm Hash digest
SHA256 19420ec1909915f5360f47e6686b5f6f9548bcc25a160dad677463587485265c
MD5 704a694ac44f99f03a4576500adcae41
BLAKE2b-256 24aa6bb73089505446d380a291b8456e2af66c23320e66c014133f73f6def49a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastccm-0.3.1.tar.gz:

Publisher: python-publish.yml on z7743/FastCCM

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastccm-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fastccm-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastccm-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27020b758b569b8d7d959d397e73ef99418b8b402600d0e42f6a6be4f35b17e4
MD5 5f599a3827019a6520757f4897a8beea
BLAKE2b-256 b391dc7a23503a42eb1c709b4275f0edd75ee2f3bdee3c326e6b4f716708235d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastccm-0.3.1-py3-none-any.whl:

Publisher: python-publish.yml on z7743/FastCCM

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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