Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions intake_xarray/base.py
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

Expand All @@ -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,
Expand Down Expand Up @@ -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
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

# 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])
Copy link
Member Author

@danielballan danielballan Sep 17, 2019

Choose a reason for hiding this comment

The 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 xarray.Dataset with to_zarr(...) prompts zarr to automatically choose a chunking for us. If we serialized a dask -backed xarray.Dataset, will to_zarr(...) respect the existing chunking? If yes, then we have no problem here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_zarr uses the current chunking; rechunking is optional.

return arr.blocks[part].compute()

def to_dask(self):
"""Return xarray object where variables are dask arrays"""
Expand Down