-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add chunks='auto' support for cftime datasets #10527
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
Draft
charles-turner-1
wants to merge
5
commits into
pydata:main
Choose a base branch
from
charles-turner-1:autochunk-cftime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb1a967
All works, just need to satisfy mypy and whatnot now
charles-turner-1 852476d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c921c59
Merge branch 'main' into autochunk-cftime
charles-turner-1 1aba531
Fix moving import to be optional
charles-turner-1 9429c3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
import numpy as np | ||
|
||
from xarray.core.common import _contains_cftime_datetimes | ||
from xarray.core.indexing import ImplicitToExplicitIndexingAdapter | ||
from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint, T_ChunkedArray | ||
from xarray.namedarray.utils import is_duck_dask_array, module_available | ||
|
@@ -16,6 +17,7 @@ | |
_NormalizedChunks, | ||
duckarray, | ||
) | ||
from xarray.namedarray.parallelcompat import _Chunks | ||
|
||
try: | ||
from dask.array import Array as DaskArray | ||
|
@@ -264,3 +266,56 @@ def shuffle( | |
if chunks != "auto": | ||
raise NotImplementedError("Only chunks='auto' is supported at present.") | ||
return dask.array.shuffle(x, indexer, axis, chunks="auto") | ||
|
||
def rechunk( # type: ignore[override] | ||
self, | ||
data: T_ChunkedArray, | ||
chunks: _NormalizedChunks | tuple[int, ...] | _Chunks, | ||
**kwargs: Any, | ||
) -> Any: | ||
""" | ||
Changes the chunking pattern of the given array. | ||
|
||
Called when the .chunk method is called on an xarray object that is already chunked. | ||
|
||
Parameters | ||
---------- | ||
data : dask array | ||
Array to be rechunked. | ||
chunks : int, tuple, dict or str, optional | ||
The new block dimensions to create. -1 indicates the full size of the | ||
corresponding dimension. Default is "auto" which automatically | ||
determines chunk sizes. | ||
|
||
Returns | ||
------- | ||
chunked array | ||
|
||
See Also | ||
-------- | ||
dask.array.Array.rechunk | ||
cubed.Array.rechunk | ||
""" | ||
|
||
if _contains_cftime_datetimes(data): | ||
# Preprocess chunks if they're cftime | ||
cftime_nbytes_approx = 64 | ||
from dask import config as dask_config | ||
from dask.utils import parse_bytes | ||
|
||
target_chunksize = parse_bytes(dask_config.get("array.chunk-size")) | ||
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. very nice; everything but this line is dask-specific. Can we abstract all this logic to a helper function (say in |
||
|
||
# Calculate total elements per chunk | ||
elements_per_chunk = target_chunksize // cftime_nbytes_approx | ||
|
||
# Distribute elements across dimensions | ||
# Simple approach: try to make chunks roughly cubic | ||
ndim = data.ndim # type:ignore | ||
shape = data.shape # type:ignore | ||
if ndim > 0: | ||
chunk_size_per_dim = int(elements_per_chunk ** (1.0 / ndim)) | ||
chunks = tuple(min(chunk_size_per_dim, dim_size) for dim_size in shape) | ||
else: | ||
chunks = () | ||
|
||
return data.rechunk(chunks, **kwargs) |
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
Oops, something went wrong.
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.
first_n_items
is inxarray.core.formatting