Python bindings for sparse-ir-capi
This is a low-level binding for the sparse-ir-capi Rust library.
Requirements
- Python >= 3.10
- Rust toolchain (for building the Rust library)
- uv (for building and managing Python dependencies)
- numpy >= 1.26.4
- scipy
BLAS Support
This package automatically uses SciPy's BLAS backend for optimal performance. No additional BLAS installation is required - SciPy will provide the necessary BLAS functionality.
Build
Install Dependencies and Build
# Build the package (Rust library will be built automatically)
cd python
uv build
This will:
- Automatically build the Rust sparse-ir-capi library with Cargo (through the hatchling build hook in
hatch_build.py) - Copy the built library and header file to the Python package
- Create both source distribution (sdist) and wheel packages
Development Build
For development:
# Install in development mode (will auto-prepare if needed)
uv sync --locked
Note for CI/CD: The Rust library is built automatically during the Python package build. No separate build step is needed:
# In CI/CD scripts
cd python
uv build
See .github/workflows/ in the repository for the workflows that build and test this package.
BLAS Configuration
The package automatically uses SciPy's BLAS backend, which provides optimized BLAS operations without requiring separate BLAS installation. The build system is configured to use SciPy's BLAS functions directly.
Clean Build Artifacts
To remove build artifacts and files copied from the parent directory:
make clean
This will remove:
- Build directories and caches:
build/,dist/,*.egg-info,__pycache__/,.pytest_cache/,.venv/ - Files copied into the package:
pylibsparseir/*.so,pylibsparseir/*.dylib,pylibsparseir/*.dll,pylibsparseir/*.h
Build Process Overview
The build process works as follows:
-
Build hook:
pyproject.tomluses the hatchling backend, which runs the custom build hook inhatch_build.py. -
Rust Library Build: the hook runs
cargo build --release -p sparse-ir-capifrom the repository root:- Compiles the Rust library to a shared library (
.so,.dylib, or.dll) - Removes old shared libraries from
pylibsparseir/, then copies the new library and the headersparse-ir-capi/include/sparseir/sparseir.hthere
- Compiles the Rust library to a shared library (
-
Python Package Building:
uv buildoruv sync --locked:- Packages everything into distributable wheels and source distributions
-
Installation: The built package includes the compiled shared library and Python bindings
Conda Build
This package can also be built and distributed via conda-forge. The conda recipe is located in conda-recipe/ and supports multiple platforms and Python versions.
Building conda packages locally:
# Install conda-build
conda install conda-build
# Build the conda package
cd python
conda build conda-recipe
# Build for specific platforms
conda build conda-recipe --platform linux-64
conda build conda-recipe --platform osx-64
conda build conda-recipe --platform osx-arm64
Supported platforms:
- Linux x86_64
- macOS Intel (x86_64)
- macOS Apple Silicon (ARM64)
Supported Python versions:
- Python 3.11, 3.12, 3.13, 3.14 (the conda recipe; the PyPI package itself
supports Python 3.10 and newer, see
pyproject.toml)
Supported NumPy versions:
- NumPy 2.1, 2.2, 2.3
The conda build automatically:
- Uses SciPy's BLAS backend for optimal performance
- Cleans up old shared libraries before building
- Builds platform-specific packages with proper dependencies
Handle Ownership
Every function in pylibsparseir.core that creates a C object returns an
owning handle instead of a raw ctypes pointer: KernelHandle,
SVEResultHandle, BasisHandle, FuncsHandle, SamplingHandle, or
GemmBackendHandle (the default BLAS backend returned by
get_default_blas_backend()).
- A handle is released exactly once with the matching
spir_*_release: when it becomes unreachable, whenclose()is called (also on leaving awith handle:block), or at interpreter exit. Closing it again does nothing. - An open handle can be passed directly to any
_lib.spir_*function and is truthy. A released handle is falsy, and passing it to C raisesctypes.ArgumentErrorinstead of handing C a freed pointer. - Do not release an owned handle through
_lib.spir_*_release. Those entry points refuse owned handles withctypes.ArgumentError, because the owner would free them a second time. Handles created directly through_libremain the caller's to release, or can be handed over to an owner, e.g.FuncsHandle(_lib.spir_funcs_clone(funcs)). - The DLR and MiniPole entry points have no wrapper in
pylibsparseir.core; call them through_lib(spir_dlr_new_independent,spir_dlr_new,spir_minipole_from_dlr,spir_minipole_from_matsubara). A DLR is aspir_basisand can be handed toBasisHandle. Aspir_pole_reprhas no owning handle class, so release it yourself with_lib.spir_pole_repr_release. - C handles never depend on the handles they were created from
(
spir_basis_newcopies the kernel and the SVE result; funcs and samplings own their data), so handles can be released in any order.
Performance Notes
BLAS Support
This package automatically uses SciPy's optimized BLAS backend for improved linear algebra performance:
- Automatic BLAS: Uses SciPy's BLAS functions for optimal performance
- No additional setup: SciPy provides all necessary BLAS functionality
The build system automatically configures BLAS support through SciPy. You can verify BLAS support with debug output enabled:
export SPARSEIR_DEBUG=1
python -c "import pylibsparseir"
This will show:
[core.py] Created SciPy BLAS backend handle
[core.py] Registered SciPy BLAS dgemm @ 0x...
[core.py] Registered SciPy BLAS zgemm @ 0x...
Debug Output
The SPARSEIR_DEBUG environment variable enables debug output only when it is
set to 1, true, yes or on, in any letter case. Any other value,
including 0, false or an empty string, disables it, as does leaving it
unset. The same rule applies to both layers:
- pylibsparseir prints its messages to stdout. It checks the variable when it is imported.
- The Rust library prints
[SPARSEIR DEBUG],[SPARSEIR DEBUG ERROR]and[SPARSEIR WARN]lines to stderr. It checks the variable each time it would print one.
Troubleshooting
Build fails with missing Cargo:
# Make sure Rust toolchain is installed
# Install from https://rustup.rs/
# Then retry:
cd python
uv build
Clean rebuild:
# Remove all build artifacts
make clean
cd ../sparse-ir-capi
cargo clean
cd ../python
uv build
Metadata
Release files for pylibsparseir 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 66.9 MB
Release files / pylibsparseir-0.12.0-cp314-cp314-manylinux_2_28_x86_64.whl
| Download URL | pylibsparseir-0.12.0-cp314-cp314-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.14 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
6593374fe18629d1d402081038d2f2adae4bb918d4be591b4132146d63e6312c
|
|
BLAKE2b-256 checksum How to use checksums |
88576de24ee5edd0a03fe939def768337e5b711441d6928d529d1db7d2529c27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp314-cp314-macosx_15_0_arm64.whl
| Download URL | pylibsparseir-0.12.0-cp314-cp314-macosx_15_0_arm64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.14 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
8d2696bc516a4b29708f259c09061bfdfb85d5561f7304ed9bdc57d9c8e022d0
|
|
BLAKE2b-256 checksum How to use checksums |
78f784f789b3555e223b16413227ce532e210518204c73fe1fe68666c178acbc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp313-cp313-manylinux_2_28_x86_64.whl
| Download URL | pylibsparseir-0.12.0-cp313-cp313-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.13 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
4b5ea41fbce4f2e345e8f2613008020b05915dd76e4d7f5ac7f2fe8d3c500ba5
|
|
BLAKE2b-256 checksum How to use checksums |
2b5cd0202890f13d642187abf4b8a644c55d894a6e31a173b8940c81e1e5ad0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp313-cp313-macosx_15_0_arm64.whl
| Download URL | pylibsparseir-0.12.0-cp313-cp313-macosx_15_0_arm64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.13 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
60bfd5d8a14f59a805ddc234adf69912ddf62dde57b87157e9ebb98a8364b4d3
|
|
BLAKE2b-256 checksum How to use checksums |
9a13dcffc7e341b70a2b2dc05d63b7dab8fa9576c56d065a59602a9189a76ecc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp312-cp312-manylinux_2_28_x86_64.whl
| Download URL | pylibsparseir-0.12.0-cp312-cp312-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.12 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
671a84ddd00d08ebc3080709247b1421a133c2e1efa1c358bd7b6b1f42ba4444
|
|
BLAKE2b-256 checksum How to use checksums |
dfb5e7825d628f8a2750aa4c82dd12971f6c2a8fe7c4a354b7efc41f7dde7887
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp312-cp312-macosx_15_0_arm64.whl
| Download URL | pylibsparseir-0.12.0-cp312-cp312-macosx_15_0_arm64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.12 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f98bd46f717655691b2af191e057329610ddca1f03cbb61bc9afe70fbc673b1a
|
|
BLAKE2b-256 checksum How to use checksums |
c7b4533b2f08ca735fa25f6fa4978dc9cd7ba1e7b63802675f0a0ba7fa53b0b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | pylibsparseir-0.12.0-cp311-cp311-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
43506aeb95135c57789b6589156d989f11172c19faf62d6bffbafc2b0a3cf903
|
|
BLAKE2b-256 checksum How to use checksums |
3d2be70c50f00659ceefa74eb0ccd9269a0ab93bc854bb95e0ea281b8ceb84e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp311-cp311-macosx_15_0_arm64.whl
| Download URL | pylibsparseir-0.12.0-cp311-cp311-macosx_15_0_arm64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.11 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
e9f581c7ab2af0cc112acbdc38ec5e142489bda9935f4ce0c38786ccd76ac446
|
|
BLAKE2b-256 checksum How to use checksums |
6434b3337f1a41fa701d52b205e8f750a868a97366672fd3359cf8ef9c3363fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp310-cp310-manylinux_2_28_x86_64.whl
| Download URL | pylibsparseir-0.12.0-cp310-cp310-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
30e4a60a0b1582b5c7717f79d2119573074be7d20f46930f272de32b9cdada9d
|
|
BLAKE2b-256 checksum How to use checksums |
79be2103c7a534263480d6f073e34551fc793e6f80a15e5798fd8242d2d4fa4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pylibsparseir-0.12.0-cp310-cp310-macosx_15_0_arm64.whl
| Download URL | pylibsparseir-0.12.0-cp310-cp310-macosx_15_0_arm64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.10 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
8abfa9255d35356c33f83c6720f2a53bd89ea848a34d64e008f7723c3cbeee10
|
|
BLAKE2b-256 checksum How to use checksums |
2b5bd9b773bb7576b0cabb8b051fd0e027d05a0643db16fe3429ff6ac3e0094e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|