Skip to main content

GPU-Accelerated Phase-Amplitude Coupling calculation using PyTorch

Project description

gPAC: GPU-Accelerated Phase-Amplitude Coupling

PyPI version pytest codecov License: MIT Python 3.8+

gPAC is a PyTorch-based package for efficient computation of Phase-Amplitude Coupling (PAC) using Modulation Index (MI) with GPU acceleration. It provides:

  • 341.8x speedup over TensorPAC (tested on real benchmarks)
  • Smart memory management with auto/chunked/sequential strategies
  • Full differentiability for deep learning integration
  • Production-ready with comprehensive tests and examples
  • High correlation with TensorPAC (0.81 ± 0.04 across diverse PAC configurations)

🎯 Example Applications

Static PAC Analysis
Static PAC Analysis
Comodulogram visualization
Trainable PAC Classification
Trainable PAC Classification
Deep learning integration

🔬 PAC Values Comparison with TensorPAC

Comparison Pair 1
Phase: 4Hz, Amp: 40Hz
Correlation: 0.826
Comparison Pair 4
Phase: 12Hz, Amp: 100Hz
Correlation: 0.730
Correlation Summary
Overall Correlation
0.811 ± 0.042 (n=16)

Click images to view full size. Ground truth PAC locations marked with crosses.

📊 Performance Benchmarks

Parameter Scaling
Parameter Scaling Comparison
gPAC (blue) vs TensorPAC (red)
Performance Analysis
Performance Analysis
Speed & memory efficiency

Click images to view detailed performance metrics

🚀 Quick Start

# Installation
pip install gpu-pac

Quick Start

import torch
from torch.utils.data import DataLoader
from gpac import PAC
from gpac.dataset import SyntheticDataGenerator

# Generate synthetic PAC dataset
generator = SyntheticDataGenerator(fs=512, duration_sec=2.0)
dataset = generator.dataset(n_samples=100, balanced=True)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)

# Method 1: Specify frequency range and number of bands
pac_model = PAC(
    seq_len=dataset[0][0].shape[-1],
    fs=512,
    pha_range_hz=(2, 20),    # Phase: 2-20 Hz
    pha_n_bands=10,          # 10 linearly spaced bands
    amp_range_hz=(30, 100),  # Amplitude: 30-100 Hz  
    amp_n_bands=10,          # 10 linearly spaced bands
)

# Method 2: Direct band specification (alternative)
# pac_model = PAC(
#     seq_len=dataset[0][0].shape[-1],
#     fs=512,
#     pha_bands_hz=[[4, 8], [8, 12], [12, 20]],      # Theta, Alpha, Beta
#     amp_bands_hz=[[30, 50], [50, 80], [80, 120]],  # Low, Mid, High Gamma
# )

# Move to GPU if available
device = 'cuda' if torch.cuda.is_available() else 'cpu'
pac_model = pac_model.to(device)

# Process a batch
for signals, labels, metadata in dataloader:
    signals = signals.to(device)
    
    # Calculate PAC
    results = pac_model(signals)
    pac_values = results['pac']  # Shape: (batch, channels, pha_bands, amp_bands)
    
    print(f"Batch PAC shape: {pac_values.shape}")
    print(f"Max PAC value: {pac_values.max().item():.3f}")
    
    # Access frequency band definitions
    print(f"Phase bands: {pac_model.pha_bands_hz}")  # Tensor of shape (n_pha, 2) with [low, high] Hz
    print(f"Amplitude bands: {pac_model.amp_bands_hz}")  # Tensor of shape (n_amp, 2) with [low, high] Hz
    break  # Just show first batch

For more examples, see the examples directory.

🔧 Core Features

Flexible Frequency Band Configuration

  • Range-based: Specify frequency range and number of bands for automatic spacing
  • Direct specification: Define custom frequency bands for precise control
  • Standard bands: Compatible with theta, alpha, beta, gamma conventions
  • High resolution: Support for 50+ bands for detailed analysis
  • Band access: Direct access to frequency band definitions via pac.pha_bands_hz and pac.amp_bands_hz properties

GPU Optimization

  • Multi-GPU support: Automatic data parallelism across GPUs
  • FP16 mode: Half-precision computation for 2x memory efficiency
  • Torch compilation: JIT compilation for additional speedup
  • Batch processing: Efficient handling of multiple signals

Scientific Features

  • Permutation testing: Statistical validation with n_perm surrogates
  • Z-score normalization: Automatic statistical significance testing
  • Modulation Index: Standard MI calculation with 18 phase bins
  • Full differentiability: Gradient support for deep learning applications

🤝 Contributing

Contributions are welcome! Please see our contributing guidelines.

📖 Citation

If you use gPAC in your research, please cite:

@software{watanabe2025gpac,
  author = {Watanabe, Yusuke},
  title = {gPAC: GPU-Accelerated Phase-Amplitude Coupling},
  year = {2025},
  url = {https://github.com/ywatanabe1989/gPAC}
}

📄 License

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

🙏 Acknowledgments

  • TensorPAC team for the reference implementation
  • For fair comparison with TensorPAC, use identical frequency bands as demonstrated in ./benchmark/pac_values_comparison_with_tensorpac/generate_16_comparison_pairs.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

gpu_pac-0.3.0.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

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

gpu_pac-0.3.0-py3-none-any.whl (45.5 kB view details)

Uploaded Python 3

File details

Details for the file gpu_pac-0.3.0.tar.gz.

File metadata

  • Download URL: gpu_pac-0.3.0.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for gpu_pac-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1b33a446e1e815ffb7b2a803b9ea927ca802daacacdb7773bc1b0e7845dc2fd0
MD5 53aac7ac24a6dc8421313839c86b824a
BLAKE2b-256 d5e5a550860d3dda46d271758922536601c5e4eee4ca34c200cddc53badf7ee1

See more details on using hashes here.

File details

Details for the file gpu_pac-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: gpu_pac-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 45.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for gpu_pac-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d18593422a71f2fdad70d695cf74dc43bc70de4b6ff8f495714e8934533d08db
MD5 b86736a078491be020124a55ff5ca7ed
BLAKE2b-256 325b20380f15d213d77f054708a881d020452e91277594f8c094c1f1710ceaf0

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