-
Notifications
You must be signed in to change notification settings - Fork 194
fix: keep chunk-cached DataArray.data lazy (#2910) #2912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
RanaPriyansh
wants to merge
1
commit into
Parcels-code:main
from
RanaPriyansh:fix/2910-lazy-chunk-cached-data
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that this test file is necessary. FWICT its pretty much just testing that I don't think its worth including in our test suite |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import dask | ||
| import dask.array as da | ||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from parcels._chunk_cached_array import ChunkCachedArray, wrap_dataset | ||
|
|
||
|
|
||
| def test_chunk_cached_data_stays_lazy_until_explicit_materialization(): | ||
| loaded = [] | ||
|
|
||
| @dask.delayed | ||
| def chunk(index): | ||
| loaded.append(index) | ||
| return np.arange(16 * index, 16 * index + 16).reshape(4, 4) | ||
|
|
||
| array = da.concatenate([da.from_delayed(chunk(i), shape=(4, 4), dtype=int) for i in range(2)]) | ||
| dataset = wrap_dataset(xr.Dataset({"value": (("x", "y"), array)}), max_cache_bytes=1024) | ||
|
|
||
| with dask.config.set(scheduler="synchronous"): | ||
| data = dataset.value.data | ||
| assert isinstance(data, da.Array) | ||
| assert loaded == [] | ||
|
|
||
| np.testing.assert_array_equal(dataset.value.values, np.arange(32).reshape(8, 4)) | ||
| assert sorted(loaded) == [0, 1] | ||
|
|
||
| loaded.clear() | ||
| np.testing.assert_array_equal(np.asarray(dataset.value), np.arange(32).reshape(8, 4)) | ||
| assert sorted(loaded) == [0, 1] | ||
|
|
||
|
|
||
| def test_chunk_cached_vectorized_selection_reuses_cached_chunk(): | ||
| loaded = [] | ||
|
|
||
| @dask.delayed | ||
| def chunk(index): | ||
| loaded.append(index) | ||
| return np.arange(16 * index, 16 * index + 16).reshape(4, 4) | ||
|
|
||
| array = da.concatenate([da.from_delayed(chunk(i), shape=(4, 4), dtype=int) for i in range(2)]) | ||
| dataset = wrap_dataset(xr.Dataset({"value": (("x", "y"), array)}), max_cache_bytes=1024) | ||
| assert isinstance(dataset.value.variable._data, ChunkCachedArray) | ||
| indices = {"x": xr.DataArray([0, 1], dims="points"), "y": xr.DataArray([1, 2], dims="points")} | ||
|
|
||
| with dask.config.set(scheduler="synchronous"): | ||
| np.testing.assert_array_equal(dataset.value.isel(indices).data, [1, 6]) | ||
| assert loaded == [0] | ||
| np.testing.assert_array_equal(dataset.value.isel(indices).data, [1, 6]) | ||
| assert loaded == [0] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though there is the problem that users writing interpolators should never really be using
da.datawhen using ChunkedArrays (since it falls back to Dask, which is less performant, or (before this PR) uses Numpy, which causes eager computation of results.Implementing
get_duck_arrayat all could result in users writing interpolators that have really bad performance.I'm thinking maybe the solution is just to do a
raise NotImplementedErrorhere. This would mean that users can't inspect the data using ada.data, but I think thats acceptable (users won't be inspecting this anyway).Thoughts @erikvansebille ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that
.databypasses the chunk cache. I tested raising fromget_duck_arrayat this head. It also makes.valuesandnp.asarray(data_array)raiseNotImplementedError; vectorized.isel(...).datastill works. The API decision therefore includes whether explicit whole-array materialization should remain supported.For test scope, the no-computation assertion fails on the original Parcels implementation because
get_duck_arraycomputes the chunks. The separate module can be reduced to a focused integration regression once the accessor contract is settled.