| 96 | ) |
| 97 | @pytest.mark.parametrize("compute", [True, False]) |
| 98 | def test_xarray_blockwise_fusion_store(compute): |
| 99 | pytest.importorskip("zarr") |
| 100 | |
| 101 | def custom_scheduler_get(dsk, keys, expected, **kwargs): |
| 102 | dsk = dsk.__dask_graph__() |
| 103 | assert ( |
| 104 | len(dsk) == expected |
| 105 | ), f"False number of tasks got {len(dsk)} but expected {expected}" |
| 106 | return [42 for _ in keys] |
| 107 | |
| 108 | # First test that this mocking stuff works as expected |
| 109 | with pytest.raises(AssertionError, match="False number of tasks"): |
| 110 | scheduler = partial(custom_scheduler_get, expected=42) |
| 111 | dask.compute(da.ones(10), scheduler=scheduler) |
| 112 | |
| 113 | coord = da.arange(8, chunks=-1) |
| 114 | data = da.random.random((8, 8), chunks=-1) + 1 |
| 115 | x = xr.DataArray(data, coords={"x": coord, "y": coord}, dims=["x", "y"]) |
| 116 | |
| 117 | y = ((x + 1) * 2) / 2 - 1 |
| 118 | |
| 119 | # Everything fused into one compute task |
| 120 | # one finalize Alias |
| 121 | expected = 2 |
| 122 | scheduler = partial(custom_scheduler_get, expected=expected) |
| 123 | dask.compute(y, scheduler=scheduler) |
| 124 | |
| 125 | with tmpdir() as dirname: |
| 126 | if compute: |
| 127 | with dask.config.set(scheduler=scheduler): |
| 128 | y.to_zarr(dirname, compute=True) |
| 129 | else: |
| 130 | # There's a delayed finalize store smashed on top which won't be fused by |
| 131 | # default |
| 132 | expected += 1 |
| 133 | scheduler = partial(custom_scheduler_get, expected=expected) |
| 134 | stored = y.to_zarr(dirname, compute=False) |
| 135 | dask.compute(stored, scheduler=scheduler) |
| 136 | |
| 137 | |
| 138 | @pytest.mark.parametrize("wrap_xarray", [False, True]) |