Keep lazy_elementwise blocks as arrays for scalar-returning ops (#6965)#7126
Open
gaoflow wants to merge 1 commit into
Open
Keep lazy_elementwise blocks as arrays for scalar-returning ops (#6965)#7126gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
lazy_elementwise mapped the operation over the blocks of a lazy array, but some operations return a Python scalar for a 0-dimensional block -- for example cf_units.Unit.convert during convert_units. Dask cannot store such a block (it has no .size), so computing/saving a scalar lazy cube after convert_units raised AttributeError. Wrap each block result in np.asanyarray so it always remains an array. Fixes SciTools#6965.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Pull Request
Description
Closes #6965.
Computing (e.g. saving) a scalar lazy cube whose units had been converted failed:
The error only appears with
convert_unitsand only when the result is stored lazily (callingcube.datadirectly is fine).Cause
convert_unitsapplies the conversion lazily viairis._lazy_data.lazy_elementwise, which maps the operation over the blocks of the lazy array. The operation iscf_units.Unit.convert, and for a 0-dimensional block that call returns a Pythonfloatrather than a 0-d array. Dask's store step then doesblock.size, which afloatdoes not have.Fix
Wrap each block result in
np.asanyarrayinsidelazy_elementwise, so a block always remains an array regardless of what the operation returns. For a normal (n-d) block this is a no-op; for the scalar case it restores the expected 0-d array. Masks are preserved (asanyarray).Verification
Test_lazy_elementwise::test_scalar_returning_op_on_0d— an op that returns a Python scalar for a 0-d block now yields a 0-dndarray. Fails onmain, passes here.0.0 km); a 1-dconvert_unitsremains lazy with correct values.tests/unit/lazy_data/suite (51) and theconvert_unitscube tests pass.ruff check/ruff formatclean.