MCPcopy
hub / github.com/pydata/xarray / test_zarr_fill_value_setting

Method test_zarr_fill_value_setting

xarray/tests/test_backends.py:3813–3870  ·  view source on GitHub ↗
(self, dtype)

Source from the content-addressed store, hash-verified

3811 @requires_dask
3812 @pytest.mark.parametrize("dtype", [int, float])
3813 def test_zarr_fill_value_setting(self, dtype):
3814 # When zarr_format=2, _FillValue sets fill_value
3815 # When zarr_format=3, fill_value is set independently
3816 # We test this by writing a dask array with compute=False,
3817 # on read we should receive chunks filled with `fill_value`
3818 fv = -1
3819 ds = xr.Dataset(
3820 {"foo": ("x", dask.array.from_array(np.array([0, 0, 0], dtype=dtype)))}
3821 )
3822 expected = xr.Dataset({"foo": ("x", [fv] * 3)})
3823
3824 zarr_format_2 = (
3825 has_zarr_v3 and zarr.config.get("default_zarr_format") == 2
3826 ) or not has_zarr_v3
3827 if zarr_format_2:
3828 attr = "_FillValue"
3829 expected.foo.attrs[attr] = fv
3830 else:
3831 attr = "fill_value"
3832 if dtype is float:
3833 # for floats, Xarray inserts a default `np.nan`
3834 expected.foo.attrs["_FillValue"] = np.nan
3835
3836 # turn off all decoding so we see what Zarr returns to us.
3837 # Since chunks, are not written, we should receive on `fill_value`
3838 open_kwargs = {
3839 "mask_and_scale": False,
3840 "consolidated": False,
3841 "use_zarr_fill_value_as_mask": False,
3842 }
3843 save_kwargs = dict(compute=False, consolidated=False)
3844 with self.roundtrip(
3845 ds,
3846 save_kwargs=ChainMap(save_kwargs, dict(encoding={"foo": {attr: fv}})),
3847 open_kwargs=open_kwargs,
3848 ) as actual:
3849 assert_identical(actual, expected)
3850
3851 ds.foo.encoding[attr] = fv
3852 with self.roundtrip(
3853 ds, save_kwargs=save_kwargs, open_kwargs=open_kwargs
3854 ) as actual:
3855 assert_identical(actual, expected)
3856
3857 if zarr_format_2:
3858 ds = ds.drop_encoding()
3859 with pytest.raises(ValueError, match="_FillValue"):
3860 with self.roundtrip(
3861 ds,
3862 save_kwargs=ChainMap(
3863 save_kwargs, dict(encoding={"foo": {"fill_value": fv}})
3864 ),
3865 open_kwargs=open_kwargs,
3866 ):
3867 pass
3868 # TODO: this doesn't fail because of the
3869 # ``raise_on_invalid=vn in check_encoding_set`` line in zarr.py
3870 # ds.foo.encoding["fill_value"] = fv

Callers

nothing calls this directly

Calls 5

roundtripMethod · 0.95
drop_encodingMethod · 0.95
assert_identicalFunction · 0.90
from_arrayMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected