-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[WIP] Add map_blocks. #3258
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
+110
−0
Closed
[WIP] Add map_blocks. #3258
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,86 @@ | ||
try: | ||
import dask | ||
import dask.array | ||
from dask.highlevelgraph import HighLevelGraph | ||
|
||
except ImportError: | ||
pass | ||
|
||
from .dataarray import DataArray | ||
|
||
|
||
def get_chunk_slices(dataset): | ||
chunk_slices = {} | ||
for dim, chunks in dataset.chunks.items(): | ||
slices = [] | ||
start = 0 | ||
for chunk in chunks: | ||
stop = start + chunk | ||
slices.append(slice(start, stop)) | ||
start = stop | ||
chunk_slices[dim] = slices | ||
|
||
return chunk_slices | ||
|
||
|
||
def map_blocks(func, darray): | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
A version of dask's map_blocks for DataArrays. | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Parameters | ||
---------- | ||
func: callable | ||
User-provided function that should accept DataArrays corresponding to one chunk. | ||
darray: DataArray | ||
Chunks of this array will be provided to 'func'. The function must not change | ||
shape of the provided DataArray. | ||
|
||
Returns | ||
------- | ||
DataArray | ||
|
||
See Also | ||
-------- | ||
dask.array.map_blocks | ||
""" | ||
|
||
def _wrapper(darray): | ||
result = func(darray) | ||
if not isinstance(result, type(darray)): | ||
raise ValueError("Result is not the same type as input.") | ||
if result.shape != darray.shape: | ||
raise ValueError("Result does not have the same shape as input.") | ||
return result | ||
|
||
meta_array = DataArray(darray.data._meta, dims=darray.dims) | ||
result_meta = func(meta_array) | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
name = "%s-%s" % (darray.name or func.__name__, dask.base.tokenize(darray)) | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
slicers = get_chunk_slices(darray._to_temp_dataset()) | ||
dask_keys = list(dask.core.flatten(darray.__dask_keys__())) | ||
|
||
graph = { | ||
(name,) | ||
+ (*key[1:],): ( | ||
_wrapper, | ||
( | ||
DataArray, | ||
key, | ||
{ | ||
dim_name: darray[dim_name][slicers[dim_name][index]] | ||
for dim_name, index in zip(darray.dims, key[1:]) | ||
}, | ||
darray.dims, | ||
), | ||
) | ||
for key in dask_keys | ||
} | ||
|
||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
graph = HighLevelGraph.from_collections(name, graph, dependencies=[darray]) | ||
|
||
return DataArray( | ||
dask.array.Array(graph, name, chunks=darray.chunks, meta=result_meta), | ||
dims=darray.dims, | ||
coords=darray.coords, | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.