MCPcopy Index your code
hub / github.com/pydata/xarray / test_chunk_encoding_with_dask

Method test_chunk_encoding_with_dask

xarray/tests/test_backends.py:2963–3041  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2961 reason="Very flaky on Windows CI. Can re-enable assuming it starts consistently passing.",
2962 )
2963 def test_chunk_encoding_with_dask(self) -> None:
2964 # These datasets DO have dask chunks. Need to check for various
2965 # interactions between dask and zarr chunks
2966 ds = xr.DataArray((np.arange(12)), dims="x", name="var1").to_dataset()
2967
2968 # - no encoding specified -
2969 # zarr automatically gets chunk information from dask chunks
2970 ds_chunk4 = ds.chunk({"x": 4})
2971 with self.roundtrip(ds_chunk4) as actual:
2972 assert (4,) == actual["var1"].encoding["chunks"]
2973
2974 # should fail if dask_chunks are irregular...
2975 ds_chunk_irreg = ds.chunk({"x": (5, 4, 3)})
2976 with pytest.raises(ValueError, match=r"uniform chunk sizes."):
2977 with self.roundtrip(ds_chunk_irreg) as actual:
2978 pass
2979
2980 # should fail if encoding["chunks"] clashes with dask_chunks
2981 badenc = ds.chunk({"x": 4})
2982 badenc.var1.encoding["chunks"] = (6,)
2983 with pytest.raises(ValueError, match=r"named 'var1' would overlap"):
2984 with self.roundtrip(badenc) as actual:
2985 pass
2986
2987 # unless...
2988 with self.roundtrip(badenc, save_kwargs={"safe_chunks": False}) as actual:
2989 # don't actually check equality because the data could be corrupted
2990 pass
2991
2992 # if dask chunks (4) are an integer multiple of zarr chunks (2) it should not fail...
2993 goodenc = ds.chunk({"x": 4})
2994 goodenc.var1.encoding["chunks"] = (2,)
2995 with self.roundtrip(goodenc) as actual:
2996 pass
2997
2998 # if initial dask chunks are aligned, size of last dask chunk doesn't matter
2999 goodenc = ds.chunk({"x": (3, 3, 6)})
3000 goodenc.var1.encoding["chunks"] = (3,)
3001 with self.roundtrip(goodenc) as actual:
3002 pass
3003
3004 goodenc = ds.chunk({"x": (3, 6, 3)})
3005 goodenc.var1.encoding["chunks"] = (3,)
3006 with self.roundtrip(goodenc) as actual:
3007 pass
3008
3009 # ... also if the last chunk is irregular
3010 ds_chunk_irreg = ds.chunk({"x": (5, 5, 2)})
3011 with self.roundtrip(ds_chunk_irreg) as actual:
3012 assert (5,) == actual["var1"].encoding["chunks"]
3013 # re-save Zarr arrays
3014 with self.roundtrip(ds_chunk_irreg) as original:
3015 with self.roundtrip(original) as actual:
3016 assert_identical(original, actual)
3017
3018 # but intermediate unaligned chunks are bad
3019 badenc = ds.chunk({"x": (3, 5, 3, 1)})
3020 badenc.var1.encoding["chunks"] = (3,)

Callers

nothing calls this directly

Calls 6

roundtripMethod · 0.95
assert_identicalFunction · 0.90
arangeMethod · 0.80
to_datasetMethod · 0.45
chunkMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected