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",
)
# table: pyarrow.Table

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

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     # dict-like table accessor
    tbl_filtered = hdul[1].filter("FLUX > 100 AND FLAG = 0")

Write back

torchfits.write("output.fits", data, header=header, overwrite=True)
# table_dict is a dict of column names to 1D arrays/tensors
torchfits.table.write("catalog_out.fits", table_dict, header=header, overwrite=True)

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.0b3.tar.gz (403.6 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.0b3-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.0b3-cp313-cp313-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

torchfits-0.5.0b3-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.0b3-cp312-cp312-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

torchfits-0.5.0b3-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.0b3-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

torchfits-0.5.0b3-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.0b3-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.0b3.tar.gz.

File metadata

  • Download URL: torchfits-0.5.0b3.tar.gz
  • Upload date:
  • Size: 403.6 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.0b3.tar.gz
Algorithm Hash digest
SHA256 da09b3959c81a2209d6c2155ef029200b7cedf7a94b4e56b649a204844e96c76
MD5 22da3ffdb47f8e076e3325dcdbc69d08
BLAKE2b-256 6f9eda549efad73b8ad888ff20e63286d4a5aee3e24c76f0e16641ba52a2db00

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 50798d9af33b624a3262a62d4ca4a5541c2036d32c436aab99805e6a264e1f0f
MD5 03c7e392ad03c7b5e58552a29a698843
BLAKE2b-256 fc2dee3205a7b624c26de0e4e25794aa9aa42f123d3bb9b24518fa0f7bdec8db

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c3074b73b45aa25e6c135c4afa19236e0c441d2dc8dfb2c102f9e29de39dc2b
MD5 2009d3df5ef976dd27127921f037dbe8
BLAKE2b-256 d00ecf2e020d44063b45c57e3c34c8e88b579019dca053a0344c6d7fb5d75d25

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ec71977cc12ed6a1b131e02ebf68d42f54995b99425250243262f1f2a4463c2
MD5 41c7c434c622ac42b480874653691fa5
BLAKE2b-256 6f320c1c88b9e833cf43c3736d20285a1e30433eacf13d6bf186f5a4b9eeac5b

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2423ecc3a12e3d0c111afba5123b5fb07ca49a14bd37cbf5f5c4a9169b44287
MD5 5656e60312d556bbe32a5e7aa6bf1d35
BLAKE2b-256 95ce67f9f419b816a7496b8cc23f0549232d1ba60bd3cdd4df53ad480a9c0ece

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 569ceeb8c00952c7a9b91901fe8f6ae4f4ec9dc37d55f20f45d4e629e957b12d
MD5 848bdbdc5585f3515a57d27aca0cc006
BLAKE2b-256 ed6c451553d34a45332b31891d944d191a6363e0504a6bea98b375a2dfd84d08

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9db527ae61156f904b1c6b3e4b97d41a59457f5032622809b5b3a987fd22c7c7
MD5 35c7d151c6119a5ea6484f676432690a
BLAKE2b-256 e0ec0bbb22bfd8d128e8985d3cd7b6f43dc213f855543dda4e2956096b6e19c4

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8881cbe7ddbef8eddfa0fdbc663c9b1281a5e6f6a1033da747d42ebef8a1726
MD5 18a9638f44598302aacc17733674b99e
BLAKE2b-256 31cfe8f4820c38c6e0e4e29719a85140adb81783a68759fa9622c969e4c27bf3

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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.0b3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.5.0b3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6d7fc870ce3680e52fba56ad8fc0f0939e6f73988770f7286acd854f45ac0528
MD5 c5c1b6b134db40f9416888543438261a
BLAKE2b-256 5d4e7838f46796aa5f967f97a3b548c6c283002443fc6a1c8c8647e085209b29

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on astroai/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