Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Commit 8516ff3

Browse files
authored
Merge pull request #14 from TomNicholas/all_properties
Define all Dataset properties on DataTree
2 parents ac1a68e + bc3e903 commit 8516ff3

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

datatree/datatree.py

+68-4
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ def _map_over_subtree(tree, *args, **kwargs):
9292
return _map_over_subtree
9393

9494

95+
_DATASET_PROPERTIES_TO_EXPOSE = ['dims', 'variables', 'encoding', 'sizes', 'attrs', 'nbytes', 'indexes', 'xindexes',
96+
'xindexes', 'coords', 'data_vars', 'chunks', 'real', 'imag']
97+
98+
9599
class DatasetPropertiesMixin:
96100
"""Expose properties of wrapped Dataset"""
97101

98-
# TODO a neater / more succinct way of doing this?
99-
# we wouldn't need it at all if we inherited directly from Dataset...
102+
# TODO a neater way of setting all of these?
103+
# We wouldn't need this at all if we inherited directly from Dataset...
100104

101105
@property
102106
def dims(self):
@@ -133,13 +137,73 @@ def attrs(self):
133137
else:
134138
raise AttributeError("property is not defined for a node with no data")
135139

140+
141+
@property
142+
def nbytes(self) -> int:
143+
return sum(node.ds.nbytes for node in self.subtree_nodes)
144+
145+
@property
146+
def indexes(self):
147+
if self.has_data:
148+
return self.ds.indexes
149+
else:
150+
raise AttributeError("property is not defined for a node with no data")
151+
152+
@property
153+
def xindexes(self):
154+
if self.has_data:
155+
return self.ds.xindexes
156+
else:
157+
raise AttributeError("property is not defined for a node with no data")
158+
159+
@property
160+
def coords(self):
161+
if self.has_data:
162+
return self.ds.coords
163+
else:
164+
raise AttributeError("property is not defined for a node with no data")
165+
166+
@property
167+
def data_vars(self):
168+
if self.has_data:
169+
return self.ds.data_vars
170+
else:
171+
raise AttributeError("property is not defined for a node with no data")
172+
173+
# TODO should this instead somehow give info about the chunking of every node?
174+
@property
175+
def chunks(self):
176+
if self.has_data:
177+
return self.ds.chunks
178+
else:
179+
raise AttributeError("property is not defined for a node with no data")
180+
181+
@property
182+
def real(self):
183+
if self.has_data:
184+
return self.ds.real
185+
else:
186+
raise AttributeError("property is not defined for a node with no data")
187+
188+
@property
189+
def imag(self):
190+
if self.has_data:
191+
return self.ds.imag
192+
else:
193+
raise AttributeError("property is not defined for a node with no data")
194+
136195
# TODO .loc
137196

138197
dims.__doc__ = Dataset.dims.__doc__
139198
variables.__doc__ = Dataset.variables.__doc__
140199
encoding.__doc__ = Dataset.encoding.__doc__
141200
sizes.__doc__ = Dataset.sizes.__doc__
142201
attrs.__doc__ = Dataset.attrs.__doc__
202+
indexes.__doc__ = Dataset.indexes.__doc__
203+
xindexes.__doc__ = Dataset.xindexes.__doc__
204+
coords.__doc__ = Dataset.coords.__doc__
205+
data_vars.__doc__ = Dataset.data_vars.__doc__
206+
chunks.__doc__ = Dataset.chunks.__doc__
143207

144208

145209
_MAPPED_DOCSTRING_ADDENDUM = textwrap.fill("This method was copied from xarray.Dataset, but has been altered to "
@@ -283,7 +347,7 @@ def __init__(
283347
self._add_all_dataset_api()
284348

285349
def _add_all_dataset_api(self):
286-
# Add methods like .mean(), but wrapped to map over subtrees
350+
# Add methods like .isel(), but wrapped to map over subtrees
287351
self._add_dataset_methods()
288352

289353
# TODO add dataset ops here
@@ -633,7 +697,7 @@ def merge_child_datasets(
633697
datasets = [self.get(path).ds for path in paths]
634698
return merge(datasets, compat=compat, join=join, fill_value=fill_value, combine_attrs=combine_attrs)
635699

636-
def as_dataarray(self) -> DataArray:
700+
def as_array(self) -> DataArray:
637701
return self.ds.as_dataarray()
638702

639703
@property

datatree/tests/test_dataset_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_properties(self):
8080
assert dt.sizes == dt.ds.sizes
8181
assert dt.variables == dt.ds.variables
8282

83+
8384
def test_no_data_no_properties(self):
8485
dt = DataNode('root', data=None)
8586
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)