-
Notifications
You must be signed in to change notification settings - Fork 36
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
Send correct blocks. #53
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import json | ||
|
||
import dask.array | ||
|
||
from . import __version__ | ||
from intake.source.base import DataSource, Schema | ||
|
||
|
@@ -24,7 +28,14 @@ def _get_schema(self): | |
'coords': tuple(self._ds.coords.keys()), | ||
} | ||
if getattr(self, 'on_server', False): | ||
metadata['internal'] = serialize_zarr_ds(self._ds) | ||
serialized = serialize_zarr_ds(self._ds) | ||
metadata['internal'] = serialized | ||
# The zarr serialization imposes a certain chunking, which will | ||
# be reflected in the xarray.Dataset object constructed on the | ||
# client side. We need to use that same chunking here on the | ||
# server side. Extract it from the serialized zarr metadata. | ||
self._chunks = {k.rsplit('/', 1)[0]: json.loads(v.decode())['chunks'] | ||
for k, v in serialized.items() if k.endswith('/.zarray')} | ||
metadata.update(self._ds.attrs) | ||
self._schema = Schema( | ||
datashape=None, | ||
|
@@ -52,17 +63,16 @@ def read_partition(self, i): | |
if not isinstance(i, (tuple, list)): | ||
raise TypeError('For Xarray sources, must specify partition as ' | ||
'tuple') | ||
if isinstance(i, list): | ||
i = tuple(i) | ||
if hasattr(self._ds, 'variables') or i[0] in self._ds.coords: | ||
arr = self._ds[i[0]].data | ||
i = i[1:] | ||
variable, *part = i | ||
part = tuple(part) | ||
if hasattr(self._ds, 'variables') or variable in self._ds.coords: | ||
arr = self._ds[variable].data | ||
else: | ||
arr = self._ds.data | ||
if isinstance(arr, np.ndarray): | ||
return arr | ||
# dask array | ||
return arr.blocks[i].compute() | ||
# Make a dask.array so that we can return the appropriate block. | ||
arr = dask.array.from_array(arr, chunks=self._chunks[variable]) | ||
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. Even if we have a dask array here, can we be sure that it has the same chunks as the one that the client will have? It looks like encoding a numpy-backed 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.
|
||
return arr.blocks[part].compute() | ||
|
||
def to_dask(self): | ||
"""Return xarray object where variables are dask arrays""" | ||
|
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.
OK so, to be sure, this was the problem - we sent the whole array. I suppose we could have figured out which chunk to send at this point? But maybe indeed better to use Dask's internal logic to do it for us.
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.
There might be a good semi-internal dask function to be used here, to extract a block from an array that is already in memory. I think we might be paying a significant tokenization cost by using
dask.array(...)
and should avoid that, but not sure.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.
That's a good point, actually. Dask will assign a token based on the content of the data, which would be slow for a big array, but you can specify the token so that it doesn't do this. I doubt there is a function for quite what is needed here.