Skip to content

Fix transposed axes in make_image_interpolations - #984

Merged
astronomyk merged 2 commits into
mainfrom
fix/image-interpolation-axes
Sep 4, 2026
Merged

Fix transposed axes in make_image_interpolations#984
astronomyk merged 2 commits into
mainfrom
fix/image-interpolation-axes

Conversation

@astronomyk

@astronomyk astronomyk commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Yet again, just a bit of clean up of a latent bug that has not yet bitten.

TL;DR - aligns the RectBivariateSpline shape to the axis convention of .rectify

NAXIS1, NAXIS2 --> NAXIS2, NAXIS1 in spectral_trace_list_utils.py

From the below mentioned testing notebook:

from astropy.io import fits
from scipy.interpolate import RectBivariateSpline

# a non-square "detector window", e.g. a readout sub-window
xx, yy = np.meshgrid(np.arange(300), np.arange(200))
img = np.sin(xx / 20) * np.cos(yy / 15)          # shape (200, 300)
hdu = fits.ImageHDU(data=img)

# --- removed construction ---
try:
    RectBivariateSpline(np.arange(hdu.header["NAXIS1"]),
                        np.arange(hdu.header["NAXIS2"]),
                        hdu.data, kx=1, ky=1)
except ValueError as err:
    print("old construction raises:", err)

old construction raises: x dimension of z must have same number of elements as x


What

make_image_interpolations constructed its splines as RectBivariateSpline(arange(NAXIS1), arange(NAXIS2), hdu.data) — axes transposed relative to the data shape (NAXIS2, NAXIS1). For any non-square image this raises

ValueError: x dimension of z must have same number of elements as x

It has gone unnoticed because the detector images we currently rectify are square (2048x2048), and because the caller in SpectralTrace.rectify compensates by evaluating with swapped arguments, interps[ihdu](jarr, iarr).

The spline is now built with axes matching the data layout, so the (j, i) call convention used by rectify (and by the existing accuracy test) is correct by construction; the convention is documented in the docstring. Results on square images are bit-identical.

Testing

  • New regression test TestImageInterpolations::test_works_with_non_square_image (80x50 image), mirroring the existing accuracy test.
  • A notebook reproducing the old failure and verifying bit-identical results on square images is attached below.

RectBivariateSpline was constructed with x = arange(NAXIS1) and
y = arange(NAXIS2) against data of shape (NAXIS2, NAXIS1), which raises
ValueError for any non-square image. It went unnoticed because current
detector images are square and the caller in SpectralTrace.rectify
compensates by passing (j, i) swapped.

Build the spline with axes matching the data layout instead; the (j, i)
call convention used by rectify and existing tests is now correct by
construction and documented. Results on square images are unchanged.

Adds a non-square regression test.
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.50%. Comparing base (c47dc7a) to head (922a929).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #984   +/-   ##
=======================================
  Coverage   76.50%   76.50%           
=======================================
  Files          69       69           
  Lines        9062     9062           
=======================================
  Hits         6933     6933           
  Misses       2129     2129           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@teutoburg teutoburg moved this to 🏗 In progress in ScopeSim-development Aug 24, 2026
@teutoburg teutoburg changed the title Fix transposed axes in make_image_interpolations Fix transposed axes in make_image_interpolations Aug 24, 2026
@astronomyk
astronomyk marked this pull request as ready for review August 24, 2026 12:43
@astronomyk
astronomyk requested a review from oczoske August 24, 2026 12:44
@teutoburg teutoburg moved this from 🏗 In progress to 👀 Awaiting Review in ScopeSim-development Aug 24, 2026
@teutoburg teutoburg added bugfix PR resolving one or more bugs (use "Bug" type for issues, not this label) spectroscopy Related to spectroscopic modes labels Aug 24, 2026

@oczoske oczoske left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay. The same issue has been noticed and fixed by a human in #950 for PSF interpolation.

@astronomyk
astronomyk merged commit 597daa1 into main Sep 4, 2026
21 of 22 checks passed
@github-project-automation github-project-automation Bot moved this from 👀 Awaiting Review to ✅ Done in ScopeSim-development Sep 4, 2026
@astronomyk
astronomyk deleted the fix/image-interpolation-axes branch September 4, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix PR resolving one or more bugs (use "Bug" type for issues, not this label) spectroscopy Related to spectroscopic modes

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants