-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
WIP: Performance improvements for zarr backend #1800
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
a0bea98
move backend append logic to the prepare_variable methods
afdb254
deprecate variables/dimensions/attrs properties on AbstractWritableDa…
cc02150
warnings instead of errors for backend properties
86240cd
use attrs.update when setting zarr attributes
9c89ef2
more performance improvements to attributes in zarr backend
2568d21
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
d459c66
fix typo
c59ca57
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
2dd186a
Merge branch 'fix/zarr_set_attrs' of github.com:jhamman/xarray into f…
8f71b31
new set_dimensions method for writable data stores
07b9c21
Merge branch 'fix/zarr_set_attrs' of github.com:jhamman/xarray into f…
67fcd92
more fixes for zarr
b38e1a6
more tests for zarr and remove append logic for zarr
47ba8b6
more tests for zarr and remove append logic for zarr
9152b12
Merge branch 'fix/zarr_set_attrs' of github.com:jhamman/xarray into f…
26b6bcb
a few more tweaks to zarr attrs
b7681ae
Add encode methods to writable data stores, fixes for Zarr tests
e084e9e
fix for InMemoryDataStore
a6aeb36
fix for unlimited dimensions Scipy Datastores
264b13f
another patch for scipy
9c03bfc
whatsnew
c92020a
ordereddict
18434f9
address some of rabernats comments, in particular, this commit remove…
9f89c7c
stop skipping zero-dim zarr tests
3590d28
update minimum zarr version for tests
69cacee
Merge branch 'master' into fix/zarr_set_attrs
8d744e0
Merge branch 'master' into fix/zarr_set_attrs
a8dabdf
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
7858db7
Merge branch 'fix/zarr_set_attrs' of github.com:jhamman/xarray into f…
48bf7ef
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
53260c9
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
7ed6bf8
cleanup and docs for zarr performance branch
3872da2
fix two failing tests when using zarr master
e6b7068
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
c31decf
flake8
189d262
back to zarr 2.2
07b92e2
Merge branch 'master' of github.com:pydata/xarray into fix/zarr_set_a…
96996ef
remove extra store method
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import time | ||
import traceback | ||
import contextlib | ||
from collections import Mapping | ||
from collections import Mapping, OrderedDict | ||
import warnings | ||
|
||
from ..conventions import cf_encoder | ||
|
@@ -96,6 +96,9 @@ def __getitem__(self, key): | |
def __len__(self): | ||
return len(self.variables) | ||
|
||
def get_dimensions(self): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
def get_attrs(self): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
|
@@ -195,6 +198,37 @@ def __init__(self, writer=None): | |
writer = ArrayWriter() | ||
self.writer = writer | ||
|
||
def encode(self, variables, attributes): | ||
""" | ||
Encode the variables and attributes in this store | ||
|
||
Parameters | ||
---------- | ||
variables : dict-like | ||
Dictionary of key/value (variable name / xr.Variable) pairs | ||
attributes : dict-like | ||
Dictionary of key/value (attribute name / attribute) pairs | ||
|
||
Returns | ||
------- | ||
variables : dict-like | ||
attributes : dict-like | ||
|
||
""" | ||
variables = OrderedDict([(k, self.encode_variable(v)) | ||
for k, v in variables.items()]) | ||
attributes = OrderedDict([(k, self.encode_attribute(v)) | ||
for k, v in attributes.items()]) | ||
return variables, attributes | ||
|
||
def encode_variable(self, v): | ||
"""encode one variable""" | ||
return v | ||
|
||
def encode_attribute(self, a): | ||
"""encode one attribute""" | ||
return a | ||
|
||
def set_dimension(self, d, l): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
|
@@ -208,24 +242,74 @@ def sync(self): | |
self.writer.sync() | ||
|
||
def store_dataset(self, dataset): | ||
# in stores variables are all variables AND coordinates | ||
# in xarray.Dataset variables are variables NOT coordinates, | ||
# so here we pass the whole dataset in instead of doing | ||
# dataset.variables | ||
""" | ||
in stores, variables are all variables AND coordinates | ||
in xarray.Dataset variables are variables NOT coordinates, | ||
so here we pass the whole dataset in instead of doing | ||
dataset.variables | ||
""" | ||
self.store(dataset, dataset.attrs) | ||
|
||
def store(self, variables, attributes, check_encoding_set=frozenset(), | ||
unlimited_dims=None): | ||
""" | ||
Top level method for putting data on this store, this method: | ||
- encodes variables/attributes | ||
- sets dimensions | ||
- sets variables | ||
|
||
Parameters | ||
---------- | ||
variables : dict-like | ||
Dictionary of key/value (variable name / xr.Variable) pairs | ||
attributes : dict-like | ||
Dictionary of key/value (attribute name / attribute) pairs | ||
check_encoding_set : list-like | ||
List of variables that should be checked for invalid encoding | ||
values | ||
unlimited_dims : list-like | ||
List of dimension names that should be treated as unlimited | ||
dimensions. | ||
""" | ||
|
||
variables, attributes = self.encode(variables, attributes) | ||
|
||
self.set_attributes(attributes) | ||
self.set_dimensions(variables, unlimited_dims=unlimited_dims) | ||
self.set_variables(variables, check_encoding_set, | ||
unlimited_dims=unlimited_dims) | ||
|
||
def set_attributes(self, attributes): | ||
""" | ||
This provides a centralized method to set the dataset attributes on the | ||
data store. | ||
|
||
Parameters | ||
---------- | ||
attributes : dict-like | ||
Dictionary of key/value (attribute name / attribute) pairs | ||
""" | ||
for k, v in iteritems(attributes): | ||
self.set_attribute(k, v) | ||
|
||
def set_variables(self, variables, check_encoding_set, | ||
unlimited_dims=None): | ||
""" | ||
This provides a centralized method to set the variables on the data | ||
store. | ||
|
||
Parameters | ||
---------- | ||
variables : dict-like | ||
Dictionary of key/value (variable name / xr.Variable) pairs | ||
check_encoding_set : list-like | ||
List of variables that should be checked for invalid encoding | ||
values | ||
unlimited_dims : list-like | ||
List of dimension names that should be treated as unlimited | ||
dimensions. | ||
""" | ||
|
||
for vn, v in iteritems(variables): | ||
name = _encode_variable_name(vn) | ||
check = vn in check_encoding_set | ||
|
@@ -234,24 +318,51 @@ def set_variables(self, variables, check_encoding_set, | |
|
||
self.writer.add(source, target) | ||
|
||
def set_necessary_dimensions(self, variable, unlimited_dims=None): | ||
def set_dimensions(self, variables, unlimited_dims=None): | ||
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. Can you add a docstring or comment explaining what this method does? This would help new developers (including myself) come onboard with backend development. |
||
""" | ||
This provides a centralized method to set the dimensions on the data | ||
store. | ||
|
||
Parameters | ||
---------- | ||
variables : dict-like | ||
Dictionary of key/value (variable name / xr.Variable) pairs | ||
unlimited_dims : list-like | ||
List of dimension names that should be treated as unlimited | ||
dimensions. | ||
""" | ||
if unlimited_dims is None: | ||
unlimited_dims = set() | ||
dims = self.get_dimensions() | ||
for d, l in zip(variable.dims, variable.shape): | ||
if d not in dims: | ||
is_unlimited = d in unlimited_dims | ||
self.set_dimension(d, l, is_unlimited) | ||
|
||
existing_dims = self.get_dimensions() | ||
|
||
dims = OrderedDict() | ||
for v in unlimited_dims: # put unlimited_dims first | ||
dims[v] = None | ||
for v in variables.values(): | ||
dims.update(dict(zip(v.dims, v.shape))) | ||
|
||
for dim, length in dims.items(): | ||
if dim in existing_dims and length != existing_dims[dim]: | ||
raise ValueError( | ||
"Unable to update size for existing dimension" | ||
"%r (%d != %d)" % (dim, length, existing_dims[dim])) | ||
elif dim not in existing_dims: | ||
is_unlimited = dim in unlimited_dims | ||
self.set_dimension(dim, length, is_unlimited) | ||
|
||
|
||
class WritableCFDataStore(AbstractWritableDataStore): | ||
|
||
def store(self, variables, attributes, *args, **kwargs): | ||
def encode(self, variables, attributes): | ||
# All NetCDF files get CF encoded by default, without this attempting | ||
# to write times, for example, would fail. | ||
cf_variables, cf_attrs = cf_encoder(variables, attributes) | ||
AbstractWritableDataStore.store(self, cf_variables, cf_attrs, | ||
*args, **kwargs) | ||
variables, attributes = cf_encoder(variables, attributes) | ||
variables = OrderedDict([(k, self.encode_variable(v)) | ||
for k, v in variables.items()]) | ||
attributes = OrderedDict([(k, self.encode_attribute(v)) | ||
for k, v in attributes.items()]) | ||
return variables, attributes | ||
|
||
|
||
class DataStorePickleMixin(object): | ||
|
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
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
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.
This is definitely cleaner than what we had before, but I am reluctant to give the idea that this this is a new supported method for third-party DataStore classes. Maybe we can call this
_encode
for now, or add a warning about implementing it?Eventually, I would like to remove all implementations from the DataStore base classes, and leave them as purely abstract. This will make it clearer to new backend implementers what they actually should/can implement.
So instead of implementing an
encode()
method, data store classes could have a list of defaultencoders
(see xarray.coding) used when reading/writing data. But xarray.coding isn't quite ready for this yet...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.
Clearly some refactoring will be needed to this once the overall backend refactoring moves forward. For now, however, this seems like a reasonable compromise.
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.
@shoyer - I'm a bit confused here. As you'll see in the Zarr backend, the
encode_variable
method is applying a list of encoders. Where in theWritableDataStore
were you envisioning the application of the encoders?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.
@shoyer - What would you say to merging this in its current state and leaving the encoders refactor to a separate PR? I'm happy to make more changes here but a) I'm not sure how to address your last comment, and b) I've already drifted a fair ways off track with this PR.
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.
I'm fine with that. We may want to change it more in the future but this is a clear improvement for now.