Repository navigation
Read FITS regions directly into xFit forced photometry - #223
Conversation
|
Important Review skippedAuto reviews are limited based on label configuration. 🏷️ Required labels (at least one): 1
Please check the settings in the CodeRabbit UI or the
|
melo-gonzo
left a comment
There was a problem hiding this comment.
Approving. The stamp gather matches its docstring. I checked the covering rectangle, the floor(p + 0.5) centers and their offsets against the (x, y) convention both estimators use, input order and repeats, dtypes, and the mask bits, including the int16 sign bit. I reviewed by reading the code and ran the edge cases below with the Astropy reader.
Notes for a follow-up:
- One target near an edge rejects the whole batch, and the message does not say which target. A stamp that crosses an edge reaches the core check, "FITS section must lie within every image". A position below -0.5 raises "positions exceed the supported pixel index range" instead. Forced photometry at catalog positions often has a few targets within half a stamp of an edge. To filter them, callers have to repeat the rounding and the image-shape lookup. Checking positions against the header shape before the read, and naming the rows, would fix the message. Padding with
good=Falsewould instead let the aperture report the lost pixels as MASKED, and the PSF fit would use the pixels inside the image. - Memory follows how far apart the targets are, not how many stamps there are. That is host memory by default and device memory with
device=True. Two targets in opposite corners of a 4096 x 4096 exposure read the whole image. With float32 science and variance and an int32 mask, that is about 200 MB to return two 41 x 41 stamps. Withreader="astropy", device=True, the whole rectangle is also copied to the GPU before the gather. The guide could say this, and the Astropy path could gather on the host and copy only the stamps. - The shape, integer-mask and mask-width checks (
src/cuphoton/xfit/fits_input.py:399-408) run after the read decodes the pixels. All three use header facts, so they could run oninspect_fits_imageresults before decoding. test_forced_photometry_device_read_matches_host(tests/xfit/test_fits_input.py:153) skips on CPU jobs and is not in the GPU job's list (.github/workflows/ci.yml:350-366), so CI never runs it.- The guide says the default reader returns NumPy arrays and the example returns CuPy arrays (
docs/components/xfit.md:634-636).devicedecides that, notreader.reader="astropy", device=Truealso returns CuPy arrays. - This reader rounds half up.
forced_flux_atandwindow_stampsuserint, which rounds half to even, so x = 10.5 gets a stamp centered at 11 here and at 10 there. Each offset is consistent with its stamp, but the same target lands on different pixels in the two paths.
Reproducer (Astropy reader, a5656b9)
import numpy as np
from astropy.io import fits
from cuphoton.xfit import read_forced_photometry_stamps as read
image = np.arange(128, dtype=np.float32).reshape(8, 16)
variance = np.full(image.shape, 2.0)
mask = np.zeros(image.shape, dtype=np.int16)
planes = (image, variance, mask)
fits.HDUList([fits.PrimaryHDU(), *(fits.CompImageHDU(p, compression_type="GZIP_2", quantize_level=0) for p in planes)]).writeto("visit.fits", overwrite=True)
args = dict(science_hdu=1, variance_hdu=2, mask_hdu=3, bad_mask_bits=32768, stamp_shape=(3, 5))
print("ok", read("visit.fits", [[8.75, 4.25]], **args).offsets)
for pos in ([[8.75, 4.25], [1.0, 4.0]], [[8.75, 4.25], [14.6, 4.0]], [[8.75, 4.25], [-0.6, 4.0]]):
try:
read("visit.fits", pos, **args)
print(pos, "no error")
except ValueError as e:
print(pos, "ValueError:", e)
# ok [[-0.25 0.25]]
# [[8.75, 4.25], [1.0, 4.0]] ValueError: FITS section must lie within every image
# [[8.75, 4.25], [14.6, 4.0]] ValueError: FITS section must lie within every image
# [[8.75, 4.25], [-0.6, 4.0]] ValueError: positions exceed the supported pixel index range9dc3fad to
16e9409
Compare
a5656b9 to
c44be04
Compare
Gather science, variance and mask stamps from one exposure region so PSF and aperture measurements can share device-resident pixels and subpixel positions. Preserve the input mask bits and expose the reader provenance for callers that require GPU decoding. Signed-off-by: Trent Nelson <trentn@nvidia.com>
Check target coordinates before converting them to real values so a missing coordinate or imaginary component cannot become a valid measurement at a different position. Signed-off-by: Trent Nelson <trentn@nvidia.com>
Load the shared FITS layer when a caller starts a FITS operation so importing the xFit API stays light. Exercise both device readers on compressed data with a signed mask and verify co-registration for the full planes even when the requested section fits each HDU. Signed-off-by: Trent Nelson <trentn@nvidia.com>
Identify out-of-bounds target rows and match the xPois half-pixel rounding convention. Gather Astropy stamps before copying to CUDA and exercise both device readers in the native xDR coverage job. Signed-off-by: Trent Nelson <trentn@nvidia.com>
c44be04 to
68efd74
Compare
|
Thanks for checking the reader against the Astropy reproducer, Carmelo. I addressed the six points from your review in 68efd747.
|
Inspect nested rows and object arrays before conversion to real values. A missing coordinate or an imaginary component can otherwise select a plausible but incorrect target pixel. Signed-off-by: Trent Nelson <trentn@nvidia.com>
This PR builds on #221. Callers with FITS images and target pixel coordinates can feed the same science, variance, mask and subpixel offsets to the PSF and aperture estimators. Previously they had to build the stamps and admission mask themselves.
cuphoton.xfit.read_forced_photometry_stampsreads the smallest rectangle covering the requested stamps from each of three co-registered HDUs. It preserves input order, repeated positions and pixel dtypes. The caller chooses rejected FITS mask bits. Withreader="xdr", device=True, the gather and mask construction operate on CuPy arrays. The default Astropy path returns NumPy arrays. The xFit component documentation includes a worked example for both estimators.Validation
tests/xfit,tests/core/test_fits_io.pyandtests/test_public_api.pypassed 470 tests with 216 skips. Repository-wide Ruff, formatting, mypy and the lockfile check passed.