Skip to content

Commit f6d992d

Browse files
mwcraigclaude
andcommitted
Accept astropy with or without the block_replicate float32 fix
astropy PR #20364 (in 7.2.3 and 8.0.2) fixed astropy/astropy#20360, so astropy.nddata.block_replicate(..., conserve_sum=True) now keeps float32 input as float32 on the devdeps job, tripping the dtype canary in test_block_replicate_float32_input_keeps_float32. Drop the canary: the test accepts either reference dtype and keeps comparing values after a cast to float32, so it passes on astropy with and without the fix. Word the documented difference in docs/array_api.rst and CHANGES.rst as applying to astropy before that fix. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 72d5c24 commit f6d992d

3 files changed

Lines changed: 24 additions & 26 deletions

File tree

‎CHANGES.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ New Features
2828
it to NumPy through ``astropy.nddata``. ``block_average`` and
2929
``block_replicate(..., conserve_sum=True)`` promote integer and boolean
3030
input to the library's default real floating dtype; the latter also
31-
preserves a floating input's dtype, where ``astropy.nddata`` currently
32-
upcasts to ``float64`` (astropy/astropy#20360). [#971]
31+
preserves a floating input's dtype, where ``astropy.nddata`` before the
32+
fix for astropy/astropy#20360 upcasts to ``float64``. [#971]
3333

3434
Other Changes and Additions
3535
^^^^^^^^^^^^^^^^^^^^^^^^^^^

‎ccdproc/tests/test_blocks.py‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -272,29 +272,26 @@ def test_complex_input_matches_astropy(function, reference_function):
272272
def test_block_replicate_float32_input_keeps_float32():
273273
"""
274274
Float32 input keeps its dtype through ``block_replicate`` with
275-
``conserve_sum=True``, where `astropy.nddata.block_replicate` promotes
276-
it to float64.
277-
278-
This is a documented difference: astropy divides by ``numpy.prod(block_size)``,
279-
an ``int64`` scalar that NEP 50 promotes against, while the native
280-
version divides by a Python int and so keeps the input's floating
281-
dtype -- deliberately, since FITS images are overwhelmingly float32 and
282-
doubling their size on upsampling is the wrong default for the GPU and
283-
lazy backends this module exists for. Values are compared after casting
284-
astropy's float64 reference down to float32, with a relaxed ``rtol``
285-
(rather than through ``_assert_matches``, which requires exact dtype
286-
equality), because the two float32 divisions round slightly
287-
differently: max relative difference ~5e-8 for ``block_size=3`` on a
288-
shape astropy has to trim. The assertion on the reference's dtype is a
289-
canary for astropy/astropy#20360, so that the documented difference is
290-
dropped once astropy stops upcasting.
275+
``conserve_sum=True``, on any astropy version.
276+
277+
`astropy.nddata.block_replicate` promoted such input to float64 before
278+
the fix for astropy/astropy#20360 (astropy PR #20364, in 7.2.3 and
279+
8.0.2): it divided by ``numpy.prod(block_size)``, an ``int64`` scalar
280+
that NEP 50 promotes against, while the native version divides by a
281+
Python int and so keeps the input's floating dtype -- deliberately,
282+
since FITS images are overwhelmingly float32 and doubling their size on
283+
upsampling is the wrong default for the GPU and lazy backends this
284+
module exists for. The test does not depend on which astropy it runs
285+
against: it accepts either reference dtype and compares values after
286+
casting the reference to float32, with a relaxed ``rtol`` rather than
287+
through ``_assert_matches``, which requires exact dtype equality,
288+
because a float64 division rounded to float32 can differ from a
289+
float32 division: max relative difference ~5e-8 for ``block_size=3``
290+
on a shape astropy has to trim.
291291
"""
292292
data32 = _2D.astype(np.float32)
293293
reference = nddata.block_replicate(data32, 3, True)
294-
# Canary for astropy/astropy#20360: when astropy stops upcasting, this
295-
# fails on the devdeps job, and the "second difference" in
296-
# docs/array_api.rst and CHANGES.rst should be dropped.
297-
assert reference.dtype == np.float64
294+
assert reference.dtype in (np.float32, np.float64)
298295
result = _blocks.block_replicate(_to_xp(data32), 3, True)
299296
assert result.dtype == _to_xp(data32).dtype
300297
expected = _to_xp(reference.astype(np.float32))

‎docs/array_api.rst‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ What limitations should I be aware of?
187187
in both cases anyway, so this differs only for a library whose default
188188
real dtype is not ``float64``. Second, on non-NumPy backends
189189
``block_replicate(..., conserve_sum=True)`` preserves a real floating
190-
input's dtype (``float32`` stays ``float32``), whereas ``astropy.nddata``
191-
upcasts ``float32`` and ``float16`` input to ``float64`` there
192-
(`astropy/astropy#20360 <https://github.com/astropy/astropy/issues/20360>`_;
193-
this difference goes away once that is fixed). Note also
190+
input's dtype (``float32`` stays ``float32``), which ``astropy.nddata``
191+
only does from the fix for
192+
`astropy/astropy#20360 <https://github.com/astropy/astropy/issues/20360>`_
193+
(astropy 7.2.3 and 8.0.2); earlier versions upcast ``float32`` and
194+
``float16`` input to ``float64`` there. Note also
194195
that the default ``block_reduce`` sum of integer or boolean input uses
195196
the array library's default integer width, which is ``int32`` on `jax`_
196197
unless 64-bit mode is enabled, so large blocks can overflow there.

0 commit comments

Comments
 (0)