Skip to main content

High-performance FITS I/O for PyTorch

Project description

torchfits

PyPI

CI Python 3.10+ License: GPL-2.0

torchfits is a focused FITS I/O library for PyTorch. It reads and writes FITS images, headers, HDUs, compressed images, and FITS tables through a multi-threaded C++ engine with vendored CFITSIO, returning tensor-native data without requiring users to build NumPy-to-torch glue code.

It is not a full replacement for Astropy, fitsio, or CFITSIO. The supported surface is documented explicitly in docs/parity.md, with source-backed tests for each claimed parity area. WCS, sky-coordinate models, HEALPix, sphere geometry, and sky-domain simulation workflows are out of scope for torchfits.

At a Glance

Task Traditional stack torchfits equivalent
Read image to GPU astropy/fitsio → numpy → torch → .to(device) torchfits.read("img.fits", device="cuda")
Write tensor to FITS tensor → numpy → astropy HDU → writeto torchfits.write("out.fits", tensor)
Filter large table load all rows → mask in Python where="MAG < 20" pushdown in C++
Read multi-extension files manual HDU dispatch with torchfits.open("mef.fits") as hdul: ...
Verify FITS checksums comparator-specific helpers torchfits.verify_checksums(path)

Features

FITS I/O — Multi-threaded C++ core with SIMD-optimized type conversion, memory-mapped image reads, intelligent chunking, and adaptive buffering. Reads and writes images, binary/ASCII tables, compressed images, and multi-extension FITS files with header round-trip coverage.

Table Engine — Arrow-native table API with predicate pushdown (where=), column projection, row slicing, streaming scan(), and in-place mutations (append, insert, update, delete rows and columns). Interop with Pandas, Polars, DuckDB, and PyArrow.

Compatibility Contract — Parity is tracked by tier: truthful public docs, fitsio core workflow parity, Astropy common workflow parity, selected CFITSIO backend behavior, and explicit non-goals. See docs/parity.md.

Install

pip install torchfits

Pre-built wheels are available for Linux and macOS (x86_64, arm64). No system CFITSIO needed—it's vendored and compiled automatically.

From source:

git clone https://github.com/astroai/torchfits.git
cd torchfits
pip install -e .

Requires Python 3.10+, a C++17 compiler, CMake 3.21+, and PyTorch 2.0+.

Quick Start

Read an image to GPU

import torchfits

data, header = torchfits.read("science.fits", device="cuda", return_header=True)
# data: torch.Tensor on CUDA, shape e.g. (4096, 4096), dtype torch.float32

Filter and stream a catalog

# Predicate pushdown — only matching rows leave C++
table = torchfits.table.read(
    "catalog.fits",
    columns=["RA", "DEC", "MAG_G"],
    where="MAG_G < 20.0 AND CLASS_STAR > 0.9",
)

# Stream 100M rows in constant memory
for batch in torchfits.table.scan("survey.fits", batch_size=50_000):
    process(batch)

Multi-HDU access

with torchfits.open("multi_ext.fits") as hdul:
    print(hdul)            # pretty-printed summary
    img = hdul[0].data     # image tensor
    tbl = hdul[1].data     # table dict
    tbl_filtered = hdul[1].filter("FLUX > 100 AND FLAG = 0")

Write back

torchfits.write("output.fits", data, header=header, overwrite=True)
torchfits.table.write("catalog_out.fits", table_dict, header=header)

Benchmarks

torchfits benchmark evidence is limited to FITS image I/O and FITS table I/O. Comparators are astropy.io.fits and fitsio; selected CFITSIO behavior is validated through the torchfits native backend and smoke tests.

Methodology, reproducible commands, results, and known deficits: docs/benchmarks.md

Documentation

API Reference Full public API with signatures and examples
Roadmap FITS I/O roadmap and parity tiers
Parity Matrix Supported, partial, unsupported, and out-of-scope features
Examples Runnable scripts for every major workflow
Installation Build from source, GPU setup, troubleshooting
Benchmarks Methodology, commands, and latest numbers
Changelog Version history and migration notes
Release Checklist Maintainer guide for cutting releases

Contributing

git clone https://github.com/astroai/torchfits.git
cd torchfits
pixi install
pixi run test

The project uses pixi for environment management, ruff for linting, and pytest for testing.

License

GPL-2.0

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

torchfits-0.5.0b1.tar.gz (382.0 kB view details)

Uploaded Source

Built Distributions

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

torchfits-0.5.0b1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

torchfits-0.5.0b1-cp313-cp313-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

torchfits-0.5.0b1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

torchfits-0.5.0b1-cp312-cp312-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

torchfits-0.5.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

torchfits-0.5.0b1-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

torchfits-0.5.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

torchfits-0.5.0b1-cp310-cp310-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file torchfits-0.5.0b1.tar.gz.

File metadata

  • Download URL: torchfits-0.5.0b1.tar.gz
  • Upload date:
  • Size: 382.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchfits-0.5.0b1.tar.gz
Algorithm Hash digest
SHA256 c68d537c951cd34de0d45ebd55470cca84a6725d5799018c48d06462c7887dfa
MD5 b370b4e6f6b63ea88ac3f8ac36f24d43
BLAKE2b-256 fee19db06b1e1fca7dc80b3280db6f45a80baa9188ad0184c12d14e73ff1d6da

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1.tar.gz:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c9d2f2f65f5e2fd8d1704a71a6aaff0dda628c31bb0e64390245c4460cd790f
MD5 3f8a93733d42984a88bb143c81a675e9
BLAKE2b-256 e61a9c9042ec107379c4ad9356bf0d4e191ba7409110b37d87ba031570131e4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 39f60474ffb0f4a7819ac4e52b6ceb16d70d39a2083430807eb055b14195e64d
MD5 90403437ec2932a04c09d43ed422372e
BLAKE2b-256 18c1916056e194ad012853c0b93bab7442e295f9216a5eeca7d37e532346a41e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 278acd213c5421326296d77f44c42083654375957894494f1b0c92b5d6c11a94
MD5 383f8659c2b1a083038b3877ad420d5a
BLAKE2b-256 45de69db1f35309c5a95d99901aa960df6e21303b76d7aeed496b660bf5dd5cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f8cb80078bfca1d76454244f25a91a7b68462477f887a3c3844767aabb5e4c0f
MD5 0568201e696454f4992a3cc32e5fd39e
BLAKE2b-256 04ea467b6c547f96785b1913c62f072d1cb8aa94c8b231b74ad431da0f98615e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2d01949b3e86bd6ee9805648f2d994480a50625a3b4fee21c3f8acf442b6424d
MD5 25667dfcee5401f1da96c1944cde8cb6
BLAKE2b-256 189b517d3a5c31f0551daf4c8d82729e2de5cedc8f1d08e2569582f7c24d27a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f8b49d6c10e382936133ff67a4d1860cbf20b99b5145eed148bd733ed3ad6e3c
MD5 5549eb51071b3d972fc2ceccc5cdd3e3
BLAKE2b-256 f3ff36c3ff8ca11bbe281774098db4bd116094b6ef3e68b346f929fa91b9845b

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e3fa3b241214f7c3c40014ef654ac7404511e4b027c7287f64377b60002e68e9
MD5 ad315aa8fdf6c2bf6fdea89e474d5fd4
BLAKE2b-256 f2a6c80c05199f40a53caa4ee47e1a4dc3515f8940c92375474ff4651c12c56f

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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

File details

Details for the file torchfits-0.5.0b1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 49e7fe1aed5a25ac5fdb418acfe8a040625658ea85e35441d4215e62b06eefd1
MD5 3355418a4d051371a9c7e7afdc67ff25
BLAKE2b-256 97197261e17c0f3e4edc85511640f58a59cd41e3f88ef6f7fe80b03f336ff2d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.5.0b1-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on sfabbro/torchfits

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