Skip to main content

A comprehensive Python package for Singular Spectrum Analysis with accelerated implementation

Project description

SSA-Tools

A comprehensive Python package for accelerated Singular Spectrum Analysis

PyPI version License: MIT

SSA-Tools provides both classic and accelerated implementations of Singular Spectrum Analysis (SSA), a powerful technique for time series decomposition, trend extraction, and noise reduction. The package includes complete visualization tools, weighted correlation analysis, and automated component grouping capabilities.

Features

  • Multiple SSA Implementations:

    • Classic SSA with both SVD and eigenvalue decomposition
    • Accelerated SSA with multi-threading and Numba JIT compilation
    • Performance optimizations for large time series analysis
  • Comprehensive Analysis Tools:

    • Component extraction and reconstruction
    • Weighted correlation analysis between components
    • Automated detection of related components
    • Variance analysis for components
  • Rich Visualization Suite:

    • Reconstructed components plotting
    • Variance explained charts
    • Weighted correlation heatmaps
    • Publication-ready visualization options
  • Easy-to-Use API:

    • Intuitive object-oriented interface
    • Sensible defaults for quick analysis
    • Advanced options for customization
    • Thorough parameter validation

Installation

Install SSA-Tools using pip:

pip install ssa-tools

Requirements

  • Python 3.7+
  • NumPy
  • Matplotlib
  • Joblib
  • Numba

Quick Start

import numpy as np
import matplotlib.pyplot as plt
from ssa_tools import SSA

# Generate sample data (sine wave + noise)
t = np.linspace(0, 10, 1000)
signal = np.sin(t) + 0.5 * np.sin(5 * t) + 0.2 * np.random.randn(len(t))

# Initialize SSA with parameters
ssa = SSA(
    data=signal,
    lag=100,  # Window length
    numComp=8,  # Number of components to extract
    method="accelerated",  # Use accelerated implementation
    decomposition="EVD"  # Eigenvalue decomposition
)

# Perform SSA decomposition
ssa.computeSSA()

# Plot reconstructed components
ssa.plotReconstructedComponents()

# Analyze variance explained by components
ssa.plotVarianceExplained()

# Compute weighted correlations between components
ssa.computeWCA()

# Plot correlation heatmap
ssa.plotWeightedCorrelations()

# Find related components
correlated_pairs = ssa.detectCorrelatedComponents(listPairs=True)

Detailed Usage

SSA Class

The main interface to the package is the SSA class, which has the following key parameters:

SSA(
    data,           # 1D time series as numpy array
    lag,            # Window length (2 <= lag < len(data)//2)
    numComp,        # Number of components (1 <= numComp <= lag)
    method="classic", # "classic" or "accelerated"
    decomposition="EVD", # "SVD" or "EVD"
    threshold=0.9   # Correlation threshold for grouping
)

Component Analysis

After performing SSA decomposition with computeSSA(), you can access:

  • ssa.RC: Reconstructed components matrix
  • ssa.eigenvalues: Eigenvalues for each component
  • ssa.eigenvectors: Eigenvectors from decomposition

Weighted Correlation

Compute and analyze correlations between components:

# Compute weighted correlations
ssa.computeWCA()

# Access correlation matrix
wcorr_matrix = ssa.WCorr

# Find related components
correlated_pairs = ssa.detectCorrelatedComponents()

Choosing Window Length

The window length (lag) parameter is crucial for SSA performance:

  • For periodic signals, lag should be larger than the period of interest
  • For trend extraction, larger values of lag capture longer-term trends
  • Typical values range from N/10 to N/2, where N is the signal length
  • For initial exploration, a value around N/4 often works well

Classic vs. Accelerated Implementation

Choose the right implementation for your needs:

  • Classic SSA:

    • Supports both SVD and EVD decomposition
    • Better for smaller datasets
    • More straightforward to understand and debug
  • Accelerated SSA:

    • Only supports EVD decomposition
    • Significantly faster for large datasets
    • Uses multi-threading and JIT compilation
    • Optimized for numerical efficiency

Advanced Examples

Signal Filtering

import numpy as np
from ssa_tools import SSA

# Generate noisy signal
t = np.linspace(0, 10, 1000)
clean = np.sin(t) + 0.5 * np.sin(5 * t)
noisy = clean + 0.5 * np.random.randn(len(t))

# SSA filtering
ssa = SSA(noisy, lag=100, numComp=2)
ssa.computeSSA()

# First two components represent filtered signal
filtered = np.sum(ssa.RC[:, :2], axis=1)

# Calculate filtering performance
mse = np.mean((filtered - clean)**2)
print(f"Mean Squared Error: {mse:.6f}")

Trend Extraction

import numpy as np
from ssa_tools import SSA

# Generate data with trend and seasonality
t = np.linspace(0, 10, 1000)
trend = 0.01 * t**2
seasonal = np.sin(2 * np.pi * t)
noise = 0.2 * np.random.randn(len(t))
signal = trend + seasonal + noise

# Extract trend with SSA
ssa = SSA(signal, lag=200, numComp=10)
ssa.computeSSA()

# Compute correlations
ssa.computeWCA()

# First component is usually the trend
extracted_trend = ssa.RC[:, 0]

More examples can be seen in examples/

API Reference

SSA Class

from ssa_tools import SSA

# Initialize
ssa = SSA(data, lag, numComp, method, decomposition, threshold)

# Core methods
ssa.computeSSA()  # Perform decomposition
ssa.computeWCA()  # Compute weighted correlations
ssa.detectCorrelatedComponents(listPairs=False)  # Find related components

# Visualization methods
ssa.plotReconstructedComponents()  # Plot components
ssa.plotVarianceExplained()  # Plot variance distribution
ssa.plotWeightedCorrelations()  # Plot correlation heatmap

Low-level functions

The package also provides direct access to the core SSA algorithms:

from ssa_tools import classic_ssa, accelerated_ssa

# Classic SSA implementation
RC, eigenvectors, eigenvalues = classic_ssa(signal, lag, numComp, decomposition)

# Accelerated SSA implementation
RC, eigenvectors, eigenvalues = accelerated_ssa(signal, lag, numComp)

Reference & Citation

If you use this package in a publication, please cite:

Publication: Accelerated Singular Spectrum Analysis and Machine Learning to investigate wood machining acoustics
Journal: Mechanical Systems and Signal Processing
DOI: https://doi.org/10.1016/j.ymssp.2024.111879

@article{SSA2024,
  title={Accelerated Singular Spectrum Analysis and Machine Learning to investigate wood machining acoustics},
  journal={Mechanical Systems and Signal Processing},
  year={2024},
  doi={10.1016/j.ymssp.2024.111879}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

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

ssa_tools-1.0.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

ssa_tools-1.0.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file ssa_tools-1.0.0.tar.gz.

File metadata

  • Download URL: ssa_tools-1.0.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for ssa_tools-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ffe0ffb5f0b63873140bb728195f17d17b7dc74e378ba09a2b4db3b996c483ed
MD5 4e4f9cfcb5cd974b9b2db21686fb026f
BLAKE2b-256 655dd6032c67b08195abfdb6052a2b5fe59241cdc03f11bc1df67eb9cde1847c

See more details on using hashes here.

File details

Details for the file ssa_tools-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ssa_tools-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for ssa_tools-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0108943cb950f0fff384c699e514c426f143b311756e2a83fd28ab5c74dd04b0
MD5 fc42755390fc72a237fe33b35cefaff3
BLAKE2b-256 35a6909893d5a06095f36c420102e4b63d6855b05bec856dc4c0ab2af6bef4be

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