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

Function test_map_blocks_dask_args

xarray/tests/test_dask.py:1258–1316  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1256
1257
1258def test_map_blocks_dask_args():
1259 da1 = xr.DataArray(
1260 np.ones((10, 20)),
1261 dims=["x", "y"],
1262 coords={"x": np.arange(10), "y": np.arange(20)},
1263 ).chunk({"x": 5, "y": 4})
1264
1265 # check that block shapes are the same
1266 def sumda(da1, da2):
1267 assert da1.shape == da2.shape
1268 return da1 + da2
1269
1270 da2 = da1 + 1
1271 with raise_if_dask_computes():
1272 mapped = xr.map_blocks(sumda, da1, args=[da2])
1273 xr.testing.assert_equal(da1 + da2, mapped)
1274
1275 # one dimension in common
1276 da2 = (da1 + 1).isel(x=1, drop=True)
1277 with raise_if_dask_computes():
1278 mapped = xr.map_blocks(operator.add, da1, args=[da2])
1279 xr.testing.assert_equal(da1 + da2, mapped)
1280
1281 # test that everything works when dimension names are different
1282 da2 = (da1 + 1).isel(x=1, drop=True).rename({"y": "k"})
1283 with raise_if_dask_computes():
1284 mapped = xr.map_blocks(operator.add, da1, args=[da2])
1285 xr.testing.assert_equal(da1 + da2, mapped)
1286
1287 with pytest.raises(ValueError, match=r"Chunk sizes along dimension 'x'"):
1288 xr.map_blocks(operator.add, da1, args=[da1.chunk({"x": 1})])
1289
1290 with pytest.raises(ValueError, match=r"cannot align.*index.*are not equal"):
1291 xr.map_blocks(operator.add, da1, args=[da1.reindex(x=np.arange(20))])
1292
1293 # reduction
1294 da1 = da1.chunk({"x": -1})
1295 da2 = da1 + 1
1296 with raise_if_dask_computes():
1297 mapped = xr.map_blocks(lambda a, b: (a + b).sum("x"), da1, args=[da2])
1298 xr.testing.assert_equal((da1 + da2).sum("x"), mapped)
1299
1300 # reduction with template
1301 da1 = da1.chunk({"x": -1})
1302 da2 = da1 + 1
1303 with raise_if_dask_computes():
1304 mapped = xr.map_blocks(
1305 lambda a, b: (a + b).sum("x"), da1, args=[da2], template=da1.sum("x")
1306 )
1307 xr.testing.assert_equal((da1 + da2).sum("x"), mapped)
1308
1309 # bad template: not chunked
1310 with pytest.raises(ValueError, match="Provided template has no dask arrays"):
1311 xr.map_blocks(
1312 lambda a, b: (a + b).sum("x"),
1313 da1,
1314 args=[da2],
1315 template=da1.sum("x").compute(),

Callers

nothing calls this directly

Calls 9

raise_if_dask_computesFunction · 0.90
arangeMethod · 0.80
chunkMethod · 0.45
map_blocksMethod · 0.45
iselMethod · 0.45
renameMethod · 0.45
reindexMethod · 0.45
sumMethod · 0.45
computeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…