Skip to content

Fix np.NaN → np.nan for numpy 2.x compatibility#350

Merged
sbailey merged 2 commits into
mainfrom
copilot/fix-np-nan-usage
Feb 25, 2026
Merged

Fix np.NaN → np.nan for numpy 2.x compatibility#350
sbailey merged 2 commits into
mainfrom
copilot/fix-np-nan-usage

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 25, 2026

numpy.NaN was removed in numpy 2.x; only lowercase numpy.nan is supported. This caused redrock.priors.tophat to crash at runtime (breaking qsoqn workflows), and redrock.plotspec to fail as well. Neither was caught by existing tests.

Changes

  • py/redrock/priors.py: Replace np.NaN with np.nan in tophat() (functional fix) and update matching comments
  • py/redrock/plotspec.py: Replace np.NaN with np.nan in plot()
  • py/redrock/test/test_priors.py (new): Add dedicated tests for all three prior functions (gaussian, lorentzien, tophat), including a case that would have caught this regression:
# Would raise AttributeError on numpy 2.x before this fix:
prior = Priors.tophat(z, z0=1.0, s0=0.4)  # called np.NaN internally
Original prompt

This section details on the original issue you should resolve

<issue_title>Rename np.NaN -> np.nan</issue_title>
<issue_description>numpy 2.x removed numpy.NaN and now only supports lowercase numpy.nan. This is used in redrock.plotspec and redrock.priors, but wasn't caught by unit tests because the existing unit tests don't seem to cover that code. In particular the tophat prior now crashes with numpy 2.x, which crashes qsoqn that uses it when rerunning redrock.

To do:

  • Add more complete tests of redrock.priors which would have caught this code. It appears that currently on the gaussian prior is tested as part of test_zscan.py bu thtere isn't a dedicated priors test.
  • Fix np.NaN -> np.nan usage in redrock.plotspec and redrock.priors
  • Double check for any other cases of np.NaN that should be changed to np.nan.
  • It's ok to not add unit tests for redrock.plotspec. That is not used by the pipeline, is in practice unused, and could be removed/refactored in the future anyway.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: sbailey <218471+sbailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix np.NaN usage for numpy 2.x compatibility Fix np.NaN → np.nan for numpy 2.x compatibility Feb 25, 2026
@sbailey sbailey marked this pull request as ready for review February 25, 2026 19:01
Copilot AI review requested due to automatic review settings February 25, 2026 19:01
@sbailey
Copy link
Copy Markdown
Collaborator

sbailey commented Feb 25, 2026

coveralls.io appears to be having a major outage causing that test to fail. Otherwise looks good. I'll wait for the copilot self-review to appear, then merge unless something comes up.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates Redrock to be compatible with NumPy 2.x by replacing deprecated/removed np.NaN usage with np.nan, and adds targeted unit tests to prevent regressions in the priors utilities.

Changes:

  • Replace np.NaN with np.nan in Priors.tophat() and update related docstring warning text.
  • Replace np.NaN with np.nan in PlotSpec.plot() for masking bad flux/model points.
  • Add a new dedicated test_priors.py covering gaussian, lorentzien, and tophat prior behaviors (including NumPy 2.x regression coverage).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
py/redrock/test/test_priors.py New unittest module adding coverage for the three prior functions, including tophat NaN behavior.
py/redrock/priors.py Switch tophat() fill value from removed np.NaN to np.nan and update warning text accordingly.
py/redrock/plotspec.py Replace np.NaN with np.nan when masking bad pixels prior to plotting/medfilt.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

z0, s0 = 1.0, 0.2
prior = Priors.tophat(self.z, z0, s0)
inside = np.abs(self.z - z0) < s0 / 2
# Values strictly inside the hat should be 0 or inf (boundary markers)
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The comment says values strictly inside the tophat can be "0 or inf (boundary markers)", but with inside = np.abs(self.z - z0) < s0 / 2 the boundary points (where the implementation may place np.inf) are excluded. Either adjust the comment to match the mask, or update the mask/assertions if you intended to include the boundary-marker behavior in this test.

Suggested change
# Values strictly inside the hat should be 0 or inf (boundary markers)
# Values inside the hat (as defined by the mask) should be finite or inf

Copilot uses AI. Check for mistakes.
Comment thread py/redrock/test/test_priors.py
@sbailey sbailey merged commit d72842d into main Feb 25, 2026
10 of 11 checks passed
@sbailey sbailey deleted the copilot/fix-np-nan-usage branch February 25, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rename np.NaN -> np.nan

3 participants