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

Method test_dot

xarray/tests/test_dataarray.py:4347–4391  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4345 full_like(da, fill_value=True, dtype={"x": bool})
4346
4347 def test_dot(self) -> None:
4348 x = np.linspace(-3, 3, 6)
4349 y = np.linspace(-3, 3, 5)
4350 z = range(4)
4351 da_vals = np.arange(6 * 5 * 4).reshape((6, 5, 4))
4352 da = DataArray(da_vals, coords=[x, y, z], dims=["x", "y", "z"])
4353
4354 dm_vals1 = range(4)
4355 dm1 = DataArray(dm_vals1, coords=[z], dims=["z"])
4356
4357 # nd dot 1d
4358 actual1 = da.dot(dm1)
4359 expected_vals1 = np.tensordot(da_vals, dm_vals1, (2, 0))
4360 expected1 = DataArray(expected_vals1, coords=[x, y], dims=["x", "y"])
4361 assert_equal(expected1, actual1)
4362
4363 # all shared dims
4364 actual2 = da.dot(da)
4365 expected_vals2 = np.tensordot(da_vals, da_vals, axes=([0, 1, 2], [0, 1, 2]))
4366 expected2 = DataArray(expected_vals2)
4367 assert_equal(expected2, actual2)
4368
4369 # multiple shared dims
4370 dm_vals3 = np.arange(20 * 5 * 4).reshape((20, 5, 4))
4371 j = np.linspace(-3, 3, 20)
4372 dm3 = DataArray(dm_vals3, coords=[j, y, z], dims=["j", "y", "z"])
4373 actual3 = da.dot(dm3)
4374 expected_vals3 = np.tensordot(da_vals, dm_vals3, axes=([1, 2], [1, 2]))
4375 expected3 = DataArray(expected_vals3, coords=[x, j], dims=["x", "j"])
4376 assert_equal(expected3, actual3)
4377
4378 # Ellipsis: all dims are shared
4379 actual4 = da.dot(da, dim=...)
4380 expected4 = da.dot(da)
4381 assert_equal(expected4, actual4)
4382
4383 # Ellipsis: not all dims are shared
4384 actual5 = da.dot(dm3, dim=...)
4385 expected5 = da.dot(dm3, dim=("j", "x", "y", "z"))
4386 assert_equal(expected5, actual5)
4387
4388 with pytest.raises(NotImplementedError):
4389 da.dot(dm3.to_dataset(name="dm"))
4390 with pytest.raises(TypeError):
4391 da.dot(dm3.values) # type: ignore[type-var]
4392
4393 def test_dot_align_coords(self) -> None:
4394 # GH 3694

Callers

nothing calls this directly

Calls 6

dotMethod · 0.95
to_datasetMethod · 0.95
DataArrayClass · 0.90
assert_equalFunction · 0.90
linspaceMethod · 0.80
arangeMethod · 0.80

Tested by

no test coverage detected