Skip to content

Fix block_replicate promoting float16/float32/complex64 inputs to float64 - #20364

Merged
larrybradley merged 2 commits into
astropy:mainfrom
CAOShurong:fix-block-replicate-dtype-20360
Sep 11, 2026
Merged

larrybradley merged 2 commits into
astropy:mainfrom
CAOShurong:fix-block-replicate-dtype-20360

Conversation

@CAOShurong

Copy link
Copy Markdown

Description

Fixes #20360.

Under NumPy NEP 50, dividing a float32 or float16 array by an int64 scalar (which np.prod(block_size) produces because block_size is an int ndarray) promotes the array to float64 (and complex64 promotes to complex128). Consequently, block_replicate(..., conserve_sum=True) unexpectedly promoted these dtypes to 64-bit precision, doubling the memory footprint for float32 astronomical imagery.

By converting np.prod(block_size) to a Python int (int(np.prod(block_size))), NumPy retains the floating point / complex precision of the input array under NEP 50 rules, while integer arrays continue to convert to float64 under Python true division as expected.

Checklist for package maintainer(s)

  • Unit tests added for np.float16, np.float32, np.float64, np.complex64, np.complex128, and np.int32 inputs.
  • Full astropy.nddata test suite passed (1324/1324 tests passed).
  • Towncrier changelog fragment added under docs/changes/nddata/20360.bugfix.rst.
  • Pre-commit formatting and ruff linting verified.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@pllim pllim added this to the v8.1.0 milestone Sep 11, 2026
@larrybradley
larrybradley merged commit 49c96ae into astropy:main Sep 11, 2026
38 of 39 checks passed
@pllim pllim added the Bug label Sep 11, 2026
@pllim pllim modified the milestones: v8.1.0, v7.2.3 Sep 11, 2026
@pllim

pllim commented Sep 11, 2026

Copy link
Copy Markdown
Member

@meeseeksdev backport to v7.2.x

@meeseeksdev backport to v8.0.x

@pllim pllim added backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x labels Sep 11, 2026
pllim pushed a commit that referenced this pull request Sep 11, 2026
pllim added a commit that referenced this pull request Sep 11, 2026
…364-on-v7.2.x

Backport PR #20364 on branch v7.2.x (Fix block_replicate promoting float16/float32/complex64 inputs to float64)
pllim pushed a commit that referenced this pull request Sep 11, 2026
pllim added a commit that referenced this pull request Sep 11, 2026
…364-on-v8.0.x

Backport PR #20364 on branch v8.0.x (Fix block_replicate promoting float16/float32/complex64 inputs to float64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x Bug nddata

Projects

None yet

Development

Successfully merging this pull request may close these issues.

block_replicate promotes float32/float16 input to float64 when conserve_sum=True

3 participants