A comprehensive toolkit for sparse matrix management and test matrix generation
Project description
matrix-toolkit
A comprehensive Python toolkit for fetching, managing, and converting sparse matrices from the SuiteSparse Matrix Collection, plus programmatic generation of test matrices inspired by MATLAB's anymatrix.
Features
SuiteSparse Integration
- Smart Search & Filter: Search matrices by size, sparsity, symmetry, domain, and more
- Multi-Backend Support: Automatic conversion to SciPy, CuPy, JAX, PyTorch formats
- Flexible Storage: Save and load matrix collections with multiple formats (NPZ, HDF5, MAT)
- Dataset Management: Create reproducible matrix datasets with train/val/test splits
- Parallel Processing: Multi-threaded downloading and processing
- CLI Tools: Command-line interface for quick operations
Anymatrix Integration
- Test Matrix Generation: Programmatic generation of 40+ well-defined test matrices
- Property Verification: Automatic checking of mathematical properties
- Comprehensive Testing: Built-in test suite for all generated matrices
- Multiple Groups: Core, gallery, and custom matrix collections
- Unified Interface: Single API for both SuiteSparse and generated matrices
Installation
Basic Installation
pip install matrix-toolkit
With Optional Dependencies
# For CuPy support
pip install matrix-toolkit[cupy]
# For JAX support
pip install matrix-toolkit[jax]
# For PyTorch support
pip install matrix-toolkit[torch]
# Install all optional dependencies
pip install matrix-toolkit[all]
Development Installation
git clone https://github.com/inEXASCALE/matrix-toolkit.git
cd matrix-toolkit
pip install -e ".[dev]"
Quick Start
Basic Usage
from matrix_toolkit import MatrixFetcher
# Initialize fetcher
fetcher = MatrixFetcher()
# Search for matrices
matrices = fetcher.search(
rows=(1000, 50000),
sparsity=(0.8, 0.99),
symmetry='symmetric'
)
# Fetch a specific matrix
matrix = fetcher.get_matrix(
'HB/494_bus',
backend='scipy',
format='csr'
)
# Random sampling
sample = fetcher.fetch(
n=10,
mode='random',
filters={'domain': 'physics'}
)
Dataset Creation
# Create a standardized dataset
dataset = fetcher.create_dataset(
name='my_dataset',
filters={
'rows': (5000, 20000),
'sparsity': (0.9, 0.99),
},
size=100,
split={'train': 0.7, 'val': 0.15, 'test': 0.15}
)
# Save dataset
dataset.save('my_dataset')
# Load dataset
from matrix_toolkit.datasets import MatrixDataset
dataset = MatrixDataset.load('my_dataset')
Storage Management
# Save collection
fetcher.save_collection(
matrices,
path='/data/my_matrices',
format='npz',
compression=True
)
# Load collection
loaded = fetcher.load_collection('/data/my_matrices')
CLI Usage
# Search matrices
matrix-toolkit search --rows 1000:50000 --sparsity 0.9:0.99
# Fetch a matrix
matrix-toolkit fetch --name HB/494_bus --format csr --backend scipy
# List matrices by domain
matrix-toolkit list --domain physics
# Clear cache
matrix-toolkit cache --clear
# Create dataset
matrix-toolkit dataset create --config dataset.yaml
Anymatrix Test Matrices
from matrix_toolkit.anymatrix import AnyMatrix, MatrixProperties
# Initialize anymatrix
am = AnyMatrix()
# List available matrices
groups = am.groups() # ['core', 'gallery']
matrices = am.list('core') # List matrices in core group
# Generate a matrix
beta_matrix = am.generate('core/beta', 10)
# Check properties
assert MatrixProperties.is_symmetric(beta_matrix)
assert MatrixProperties.is_positive_definite(beta_matrix)
# Search for matrices with specific properties
symmetric_matrices = am.search(['symmetric', 'positive definite'])
Unified Interface
from matrix_toolkit import UnifiedMatrixCollection
mc = UnifiedMatrixCollection()
# Get from anymatrix
A = mc.get('anymatrix/core/beta', 10)
# Get from SuiteSparse
B = mc.get('suitesparse/HB/494_bus', backend='scipy')
# Search both collections
results = mc.search(properties=['symmetric'])
# Verify properties automatically
mc.verify_properties('anymatrix/core/beta', 10, verbose=True)
Available Anymatrix Collections
Core Group
beta- Symmetric positive definite matrixfourier- Discrete Fourier transform matrix (unitary)nilpot_triang- Nilpotent upper triangularnilpot_tridiag- Nilpotent tridiagonalvand- Vandermonde matrixcircul_binom- Circulant with binomial coefficientsstoch_cesaro- Stochastic Cesaro matrixtournament- Random tournament matrixperfect_shuffle- Perfect shuffle permutationcollatz- Collatz conjecture matrix- And more...
Gallery Group
lehmer- Lehmer matrix (symmetric positive definite)minij- MIN(i,j) matrixmoler- Moler matrixpei- Pei matrixclement- Clement tridiagonalkms- Kac-Murdock-Szego Toeplitz matrix
Running Tests
Test all anymatrix matrices
python examples/run_anymatrix_tests.py --verbose --report test_report.txt
Test specific groups
python examples/run_anymatrix_tests.py --groups core gallery
Python API
from matrix_toolkit.anymatrix.testing import run_all_tests
results = run_all_tests(verbose=True, generate_report=True)
Contributing
Contributions are welcome! Please read our Contributing Guide for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use this toolkit in your research, please cite:
@software{matrix_toolkit,
title={Matrix Toolkit: A Python Package for Sparse Matrix Management},
author={Xinye Chen},
year={2024},
url={https://github.com/chenxinye/matrix-toolkit}
}
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
matrix_toolkit-0.0.1.tar.gz
(64.0 kB
view details)
File details
Details for the file matrix_toolkit-0.0.1.tar.gz.
File metadata
- Download URL: matrix_toolkit-0.0.1.tar.gz
- Upload date:
- Size: 64.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c27dfe03b2ee08045f9a60b5c6563a7b3262e3c019be1928585d02077b4af91
|
|
| MD5 |
87a4db4148675a7a23c9946fe194f2e9
|
|
| BLAKE2b-256 |
532759181ff33441fd7fda4e4f0ada396eeb1389574662936d77a04be2d268eb
|