Skip to main content

Time series to network conversion and analysis

Project description

ts2net

PyPI version Documentation Status Tests License: MIT

Time series to networks. Clean API for visibility graphs, recurrence networks, and transition networks.

Install

Basic Installation

# Using uv (recommended)
uv pip install ts2net

# Or using pip
pip install ts2net

Development Installation

For development, use uv:

# Clone the repository
git clone https://github.com/kylejones200/ts2net.git
cd ts2net

# Install with uv (creates virtual environment and installs dependencies)
uv sync --group dev

# Build Rust extension
uv run maturin develop --release

Optional Dependencies

Install with optional features:

# Using uv
uv pip install "ts2net[speed]"      # Performance acceleration (Numba)
uv pip install "ts2net[dtw]"         # DTW distance (tslearn)
uv pip install "ts2net[cnn]"        # Temporal CNN embeddings (PyTorch)
uv pip install "ts2net[examples]"    # Example dependencies

# Or using pip
pip install ts2net[speed]
pip install ts2net[dtw]
pip install ts2net[cnn]
pip install ts2net[examples]

Verify Installation

import ts2net
print(ts2net.__version__)

from ts2net import HVG
import numpy as np
x = np.random.randn(100)
hvg = HVG()
hvg.build(x)
print(f"Installation successful: {hvg.n_nodes} nodes, {hvg.n_edges} edges")

Quick Start

import numpy as np
from ts2net import HVG

x = np.random.randn(1000)

hvg = HVG()
hvg.build(x)

print(hvg.n_nodes, hvg.n_edges)
print(hvg.degree_sequence())

Adjacency Matrix

A = hvg.adjacency_matrix()
print(A.shape)  # (1000, 1000)

NetworkX (Optional)

NetworkX is optional. Convert only if needed:

G = hvg.as_networkx()
import networkx as nx
print(nx.average_clustering(G))

Structural Decomposition and Residual Topology

For time series with predictable structure (seasonality, trends), decompose first, then analyze the residual:

from ts2net.bsts import features, BSTSSpec

# Decompose and analyze residual in one pass
spec = BSTSSpec(
    level=True,
    trend=False,
    seasonal_periods=[24, 168]  # Daily and weekly for hourly data
)

result = features(x, methods=['hvg', 'transition'], bsts=spec)

# Access three feature blocks
raw_stats = result.raw_stats              # Basic series statistics
structural_stats = result.structural_stats # Component variances, seasonal strength
residual_network_stats = result.residual_network_stats  # Network features from residual

Use cases:

  • Compare meters/wells without seasonal confounds
  • Flag series where structural model fails (high residual complexity)
  • Separate predictable structure from irregular dynamics

Installation:

uv pip install "ts2net[bsts]"  # Installs statsmodels
# Or: pip install ts2net[bsts]

See examples/bsts_features.py for complete examples.

Large Series

For large series, use output modes to control memory usage:

# Degrees only (most memory efficient)
hvg = HVG(output="degrees")
hvg.build(x)
degrees = hvg.degree_sequence()  # Fast, no edge storage

# Stats only (summary statistics without edges)
hvg = HVG(output="stats")
hvg.build(x)
stats = hvg.stats()  # n_nodes, n_edges, avg_degree, etc.

# Full edges (default, use for small-medium series)
hvg = HVG(output="edges")
hvg.build(x)
edges = hvg.edges  # Full edge list

Scale Guidelines

Series Length Method Recommended Settings Memory Risk
n < 10k All methods output="edges" Safe
10k < n < 100k HVG output="edges" or output="degrees" Safe with sparse
10k < n < 100k NVG limit=2000-5000, output="degrees" Use horizon limit
10k < n < 100k Recurrence rule='knn', k=10-30 Avoid exact all-pairs
n > 100k HVG output="degrees" or output="stats" Safe
n > 100k NVG limit=2000-5000, max_edges=1e6, output="degrees" Required limits
n > 100k Recurrence rule='knn', k=10-30, output="degrees" kNN only

Critical Warnings:

  • Dense adjacency matrices are disabled by default for n > 50k (prevents 63GB+ memory blowup)
  • NVG without limit can create millions of edges for smooth series
  • Recurrence exact all-pairs is O(n²) memory - use kNN for large n
  • NetworkX conversion refused for n > 200k (use force=True to override)

Memory Estimates:

  • Dense adjacency: ~8 * n² bytes (e.g., 90k nodes = 63 GB)
  • Sparse adjacency: ~16 * m bytes where m = edges (e.g., 100k edges = 1.6 MB)
  • Edge list: ~16 * m bytes (similar to sparse)
  • Degrees only: ~8 * n bytes (e.g., 90k nodes = 720 KB)

Methods

Visibility Graphs

HVG - Horizontal Visibility Graph

from ts2net import HVG

hvg = HVG(weighted=False, limit=None)
hvg.build(x)

NVG - Natural Visibility Graph

from ts2net import NVG

# For large series, use horizon limit and bounded work
nvg = NVG(weighted=False, limit=5000, max_edges=1_000_000, output="degrees")
nvg.build(x)

# Or with memory limit
nvg = NVG(limit=2000, max_memory_mb=100)  # Caps at ~100MB
nvg.build(x)

Recurrence Networks

Phase space recurrence:

from ts2net import RecurrenceNetwork

rn = RecurrenceNetwork(m=3, tau=1, rule='knn', k=5)
rn.build(x)

Parameters:

  • m: embedding dimension (None = auto via FNN)
  • tau: time delay
  • rule: 'knn', 'epsilon', 'radius'
  • k: neighbors for k-NN
  • epsilon: threshold for epsilon-recurrence

Transition Networks

Symbolic dynamics:

from ts2net import TransitionNetwork

tn = TransitionNetwork(symbolizer='ordinal', order=3)
tn.build(x)

Symbolizers:

  • 'ordinal': ordinal patterns
  • 'equal_width': equal-width bins
  • 'equal_freq': equal-frequency bins (quantiles)
  • 'kmeans': k-means clustering

Compare Methods

from ts2net import build_network

x = np.random.randn(1000)

for method in ['hvg', 'nvg', 'recurrence', 'transition']:
    if method == 'recurrence':
        g = build_network(x, method, m=3, rule='knn', k=5)
    elif method == 'transition':
        g = build_network(x, method, symbolizer='ordinal', order=3)
    else:
        g = build_network(x, method)
    
    print(f"{method}: {g.n_edges} edges")

Output:

hvg: 1979 edges
nvg: 2931 edges
recurrence: 3159 edges
transition: 18 edges

Multivariate

Multiple time series → network where nodes = time series:

from ts2net.multivariate import ts_dist, net_knn

X = np.random.randn(30, 1000)  # 30 series, 1000 points each

D = ts_dist(X, method='dtw', n_jobs=-1)
G = net_knn(D, k=5)

print(G.n_nodes, G.n_edges)

Distance methods: 'correlation', 'dtw', 'nmi', 'voi', 'es', 'vr'

Network builders: net_knn, net_enn, net_weighted

Performance

With Numba (recommended):

pip install numba

Speedups:

  • HVG: 100x faster
  • NVG: 180x faster
  • Recurrence: 10x faster

API

All methods follow the same pattern:

builder = Method(**params)
builder.build(x)

# Access results
builder.n_nodes
builder.n_edges
builder.edges                # list of tuples
builder.degree_sequence()    # numpy array
builder.adjacency_matrix()   # numpy array
builder.as_networkx()        # optional conversion

Troubleshooting

Common Issues

Memory errors with large time series:

  • Use output="degrees" or output="stats" instead of output="edges"
  • For NVG, always set limit parameter (e.g., limit=5000)
  • For recurrence networks, use rule='knn' with small k (10-30) instead of exact all-pairs

Slow performance:

  • Install Numba: uv pip install numba or pip install numba (100-180x speedup for visibility graphs)
  • Use output="degrees" if you don't need full edge lists
  • For multivariate, use n_jobs=-1 for parallel distance computation

Import errors:

  • Ensure you're using Python 3.12+
  • If Rust extension fails to build, install Rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • For development, run maturin develop --release after installing Rust

NetworkX conversion refused:

  • For very large graphs (n > 200k), NetworkX conversion is disabled by default
  • Use force=True to override: hvg.as_networkx(force=True)
  • Consider using output="degrees" or output="stats" instead

Getting Help

Citation

Multivariate methods based on:

Ferreira, L.N. (2024). From time series to networks in R with the ts2net package. Applied Network Science, 9(1), 32. https://doi.org/10.1007/s41109-024-00642-2

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

License

MIT

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

ts2net-0.8.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distributions

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

ts2net-0.8.0-cp313-cp313-win_amd64.whl (554.6 kB view details)

Uploaded CPython 3.13Windows x86-64

ts2net-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (660.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

ts2net-0.8.0-cp313-cp313-macosx_11_0_arm64.whl (454.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

ts2net-0.8.0-cp312-cp312-win_amd64.whl (554.4 kB view details)

Uploaded CPython 3.12Windows x86-64

ts2net-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (660.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ts2net-0.8.0-cp312-cp312-macosx_11_0_arm64.whl (454.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file ts2net-0.8.0.tar.gz.

File metadata

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

File hashes

Hashes for ts2net-0.8.0.tar.gz
Algorithm Hash digest
SHA256 ee157d34839213cb25825528ea21c5ed7ab70c10a92a35e7f373249d6dd4da8e
MD5 4e17a88d81b4d8c983c7373d31f3f667
BLAKE2b-256 053b89d05c23c8f9dfc81db03a8c5c5b94372a1876b12b2f7a21abbc390d7ff7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0.tar.gz:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: ts2net-0.8.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 554.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ts2net-0.8.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 85d7fb39d47508e3b12ff9469f89fec778dd9026b1fa13d2ffc35835ddfb0079
MD5 767a567fc7ae4b86f02ad34c76d03b71
BLAKE2b-256 79789d222c50165d89b789b6c4b32367d84520c071aab10cc1e1e43a0cd0f9fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp313-cp313-win_amd64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ts2net-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b2be3216e47c6377c98036410a3434c670dd6d4c2a1ee6dfd0285f177e23bb1
MD5 3b5d8b91b8ad270654286a4de99b8fd0
BLAKE2b-256 03bb256cf4ae18ad0b2752dc6441b06320c725f7a7ab802054df3e046c5b6142

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ts2net-0.8.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37c012217283806657230d058a37478ec69ee34a48edf8500f1b82c49a087ba4
MD5 e99bf1fd573345e1f06459bf14a98072
BLAKE2b-256 21b6ca2b4f7b386735d3287c2d424c1093b9b5285f5bc39515a9c09e9ed2e9c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ts2net-0.8.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 554.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ts2net-0.8.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7e85ffa19757ba24c3a2e76a6a8372d8c9809de8254249add3774d19416ec4fc
MD5 6a2d7f8735a779af1f4d02ac8b053719
BLAKE2b-256 2a7b8ca201c825e9e9609d1717360464a587b247e9ecdf86e827007ba0131c04

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp312-cp312-win_amd64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ts2net-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31a72af458584fde8b6893134d64ea27caa8b1f360feb8b0b6d0d0af2cec81e4
MD5 603be2a96c7fc6b2b7c10ab0a43cc0bf
BLAKE2b-256 7dbbf82e028d7c0b1b4a5674d0e667470407ea002bbef448dc5495f14e8ea412

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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

File details

Details for the file ts2net-0.8.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ts2net-0.8.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fb68ba8de0e3af439abf38c764a60e38adec9125b95d2191c8f6f9774a78e3a3
MD5 601c5ace73756648e2db869e6a43a826
BLAKE2b-256 6836ef8cad65d016e8d62e37e33a4f0cf4a8981b3c475b589468bbd717f87719

See more details on using hashes here.

Provenance

The following attestation bundles were made for ts2net-0.8.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish-pypi.yml on kylejones200/ts2net

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