Skip to main content

FITS I/O for PyTorch with native tensor reads, datasets, and transforms

Project description

torchfits

PyPI

CI Python 3.10+ License: MIT

torchfits is FITS I/O for PyTorch: a multi-threaded C++ engine with vendored CFITSIO that reads and writes images, headers, HDUs, compressed images, and tables as tensors — without NumPy-to-torch glue. Optional torchfits.data datasets and torchfits.transforms provide header-aware preprocessing for ML training loops.

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, sky-domain simulation, and photometric physics are out of scope. Transforms cover FITS scale/null/dtype and tensor preprocessing only.

At a Glance

Task Traditional stack torchfits equivalent
Read image to GPU astropy/fitsio → numpy → torch → .to(device) torchfits.read_tensor("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: ...
PyTorch training loop hand-rolled Dataset + cache tuning FitsImageDataset + make_loader(..., num_workers=4)
Normalize for model input ad-hoc scaling in the training script Compose([BackgroundSubtract(), ZScaleNormalize()])
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.

ML Data Layertorchfits.data ships FitsImageDataset, FitsImageIterableDataset, FitsTableDataset, FitsTableIterableDataset, FitsCutoutDataset, and make_loader with automatic handle-cache warm-up.

Transforms — 25+ FITSTransform classes for image stretches, header-aware scaling (FITSHeaderScale, FITSScaleColumns, TNullToNan), spectral/hyperspectral preprocessing, and continuum estimators. Most ship .inverse() for decoding model outputs back to physical units. See docs/api.md and examples/example_transforms.py.

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.

What's New in 0.7.0

0.7.0 completes the ML data layer and removes legacy dataset aliases:

  • FitsTableIterableDataset — stream large catalogs via table.scan with multi-worker batch sharding.
  • FitsCutoutDataset — patch training from a cutout index table.
  • Legacy removalFITSDataset / IterableFITSDataset removed from the root namespace; see migration_datasets.md.
  • Docs site — Zensical + GitHub Pages at astroai.github.io/torchfits.

0.6.0 shipped the core ML surface (torchfits.data image/table datasets, torchfits.transforms, C++ predicate pushdown, thread-safe caches). See docs/changelog.md.

Full history: docs/changelog.md. Roadmap for 1.0: docs/roadmap.md.

Transforms

from torchfits.transforms import ArcsinhStretch, BackgroundSubtract, Compose, ZScaleNormalize

pipeline = Compose([BackgroundSubtract(), ArcsinhStretch(a=0.1), ZScaleNormalize()])
normalized = pipeline(image)              # forward → model input
restored = pipeline.inverse(normalized)   # inverse → physical flux

Representative classes (full catalog in docs/api.md):

Category Examples Inverse
Image stretches ArcsinhStretch, LogStretch, ZScaleNormalize, RobustNormalize
Header / table FITSHeaderScale, FITSHeaderNormalize, FITSScaleColumns, TNullToNan
Spectral ContinuumNormalize, ContinuumRemoval, DopplerShift, SpectralBinning ✓ (except BandMath)
Continuum estimators AsymmetricLeastSquares, AlphaShapeContinuum, WaveletDecompose, SavitzkyGolayFilter
Outlier / time SigmaClip, AsymmetricSigmaClip, PhaseFold ✗ (lossy or many-to-one)

Runnable demos: examples/example_transforms.py (image pipeline), examples/example_hyperspectral.py (spectral cube).

Performance

Median wall-clock from the lab exhaustive benchmark suite (run exhaustive_cuda_0.7.0_20260711_055635, CANFAR staging GPU + CPU rows); see docs/benchmarks.md for methodology, deficit transparency, and reproducible commands.

Case torchfits astropy fitsio Speedup vs astropy
Large float32 image read (16 MB, CPU) 3.93 ms 7.60 ms 6.09 ms 1.9×
Compressed Rice image (CPU) 8.99 ms 28.14 ms 9.36 ms 3.1×
50× repeated 100×100 cutouts (CPU) 4.63 ms 76.04 ms 4.76 ms 16×
Table read (100k rows, 8 cols) 86.9 μs 6.32 ms 59.41 ms 73×
Varlen table read (100k rows, 3 cols) 90.8 μs 3.49 ms 220 ms 38×

ML DataLoader (local diagnostic, not in lab CSV): 30×512² float32, CPU, 2 epochs — torchfits 1.12× vs fitsio on Rice-compressed files; uncompressed within ~4%. make_loader(..., optimize_cache=True) warms handle caches automatically when the dataset exposes a files attribute.

GPU integer reads: Default read(..., device="cuda") applies BSCALE/BZERO on device and returns float32 for generic scaled pixels — good for ML. For native integer dtypes (int8, uint16) matching fitsio, use read_tensor(..., raw_scale=True) or rely on the automatic signed-byte / unsigned-integer fast paths (see benchmarks doc). Tables remain CPU-resident in all backends; GPU rows measure host decode + H2D copy, not disk→GPU bypass.

Install

pip install torchfits

Pre-built wheels are available for Linux x86_64 and macOS arm64. No system CFITSIO is needed—it is vendored and compiled automatically. Other architectures install from source when a compatible compiler and PyTorch are available.

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

tensor = torchfits.read_tensor("science.fits", hdu=0, device="cuda")

PyTorch DataLoader

from torchfits.data import FitsImageDataset, make_loader

ds = FitsImageDataset("observations/*.fits", label_key="CLASS")
loader = make_loader(ds, batch_size=32, num_workers=4)

for images, labels in loader:
    ...  # images: [B, 1, H, W] when add_channel_dim=True (default)

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

Published site: astroai.github.io/torchfits

Documentation site Browse all docs on GitHub Pages
API Reference Full public API with signatures and examples
Migration from Astropy Side-by-side workflow translation
Migration from fitsio Side-by-side workflow translation
Dataset migration Removed FITSDatasettorchfits.data
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

MIT

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.9.1.tar.gz (543.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.9.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (996.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

torchfits-0.9.1-cp313-cp313-macosx_11_0_arm64.whl (889.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

torchfits-0.9.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (996.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

torchfits-0.9.1-cp312-cp312-macosx_11_0_arm64.whl (889.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

torchfits-0.9.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (997.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

torchfits-0.9.1-cp311-cp311-macosx_11_0_arm64.whl (890.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

torchfits-0.9.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (998.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

torchfits-0.9.1-cp310-cp310-macosx_11_0_arm64.whl (891.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file torchfits-0.9.1.tar.gz.

File metadata

  • Download URL: torchfits-0.9.1.tar.gz
  • Upload date:
  • Size: 543.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.9.1.tar.gz
Algorithm Hash digest
SHA256 d9912d5cabc556703cbe8191982b24c987acb9656c07bd19c6b203e8927671aa
MD5 f65ca10d9f40024a6783cec3582c9d2b
BLAKE2b-256 23e4771b4de9c7f0f592846889c3d9553e06f1aa2ba44ba8824a6bc6d6d70c6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1.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.9.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eb49dea8a0f83ff73010720e29c91d126ef276dcbd3882f40f512697346ef328
MD5 01120f19dfc009aeaa2f0af368694671
BLAKE2b-256 23169b40dd8b5ed2d8c32b8ff874148f340f221cf9048c9f0021b1b871fc5695

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_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.9.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4e7987679b831540f8aba5867b7b854e6163e8ac4926170d236d59be453f46dc
MD5 3d9bd21ee353a4bdc058c06799ea97e9
BLAKE2b-256 66b26d496de186e5ae2d92e13ebbbd34b4211a41017b91c9aa4e3b67e1999d3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-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.9.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c9e3f6c6f0c15e9abd919bfb9e06bb1a6b558a1c2c14ba3f3096de915af30923
MD5 cb28566da94487fb271e38fc5814de39
BLAKE2b-256 ab3287aaac44f7957ec33f08d584588ca20ead7f53ac50805cde77da249ed94d

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_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.9.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2063982ef64f009d4d7dbb1d4e9094c6c7bb18df80ee27e5fc847ab92722c78b
MD5 d23aed82f5e243b3a66f3b26b36bf601
BLAKE2b-256 9f17997bc672088d4e77a24904723851a81f14795af0f9be13b6ce5a66a058ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-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.9.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 300d418ea85524cf03d884060bd2a5014a683a2268a43fa34f1b3758b4225b32
MD5 b1ff372072f16f2011d851349df3aeea
BLAKE2b-256 d16cc6bcde43c62f4d929237e284f635a4b7fc37430da5794d7bbd471623e911

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_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.9.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f7fa339afc694c560c61833198525e9623375a3782233b0adc48c8c827544232
MD5 6f440f8de0709ca0fdd13c8e1883aa72
BLAKE2b-256 da5d7f1b2fae1e62c39ec2ddd880d0f7e4e04f1c49c36d10c71b5e3065ced947

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-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.9.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0a79bdfcdb0ff8a5e8a3ee378722998133f1ded8e65ba4110bb4e7e2f8c7c2e3
MD5 cd3cf5c4cfe752a8c709b59069713890
BLAKE2b-256 54f2fe68fe42e113401f825bc1490ac2bf8f4d3bd412a05a35c7a4f6e8455265

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_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.9.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchfits-0.9.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79a289ae33bea0efcf86118a818ac7d364aa251e932d771b768ae3e31b8bab41
MD5 2b2d0ea600ea73d487ba11a77a2938e8
BLAKE2b-256 5adfcbc4fe9cb8d136ea2dbc5bb469ef374b2ba67c75e424b9d886522be81da5

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfits-0.9.1-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