Skip to content

Commit 07b7022

Browse files
author
Joseph Hamman
committed
check chunks
1 parent e26acda commit 07b7022

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

tests/test_core.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ def test_dataarray_validate_array_type():
8181
schema.validate(da)
8282

8383

84+
def test_dataarray_validate_chunks():
85+
pytest.importorskip('dask')
86+
87+
da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': 2})
88+
schema = DataArraySchema(chunks={'x': 2})
89+
schema.validate(da)
90+
91+
schema = DataArraySchema(chunks={'x': (2, 2)})
92+
schema.validate(da)
93+
94+
schema = DataArraySchema(chunks={'x': [2, 2]})
95+
schema.validate(da)
96+
97+
schema = DataArraySchema(chunks={'x': 3})
98+
with pytest.raises(SchemaError, match=r'.*(3).*'):
99+
schema.validate(da)
100+
101+
schema = DataArraySchema(chunks={'x': (2, 1)})
102+
with pytest.raises(SchemaError, match=r'.*(2, 1).*'):
103+
schema.validate(da)
104+
105+
84106
def test_dataset_empty_constructor():
85107
ds_schema = DatasetSchema()
86108
assert hasattr(ds_schema, 'validate')

xarray_schema/core.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
103103
raise NotImplementedError('coords schema not implemented yet')
104104

105105
if self.chunks:
106-
raise NotImplementedError('chunk schema not implemented yet')
106+
dim_chunks = dict(zip(da.dims, da.chunks))
107+
for key, ec in self.chunks.items():
108+
if isinstance(ec, int):
109+
for ac in dim_chunks[key][:-1]:
110+
if ac != ec:
111+
raise SchemaError(f'{key} chunks did not match: {ac} != {ec}')
112+
113+
else: # assumes ec is an iterable
114+
ac = dim_chunks[key]
115+
if tuple(ac) != tuple(ec):
116+
raise SchemaError(f'{key} chunks did not match: {ac} != {ec}')
107117

108118
if self.attrs:
109119
raise NotImplementedError('attrs schema not implemented yet')

0 commit comments

Comments
 (0)