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

Function test_cov

xarray/tests/test_computation.py:1609–1657  ·  view source on GitHub ↗
(
    n: int, dim: str | None, ddof: int, array_tuples: tuple[xr.DataArray, xr.DataArray]
)

Source from the content-addressed store, hash-verified

1607@pytest.mark.parametrize("n", [0, 1, 2])
1608@pytest.mark.parametrize("dim", [None, "time"])
1609def test_cov(
1610 n: int, dim: str | None, ddof: int, array_tuples: tuple[xr.DataArray, xr.DataArray]
1611) -> None:
1612 da_a, da_b = array_tuples[n]
1613
1614 if dim is not None:
1615
1616 def np_cov_ind(ts1, ts2, a, x):
1617 # Ensure the ts are aligned and missing values ignored
1618 ts1, ts2 = broadcast(ts1, ts2)
1619 valid_values = ts1.notnull() & ts2.notnull()
1620
1621 # While dropping isn't ideal here, numpy will return nan
1622 # if any segment contains a NaN.
1623 ts1 = ts1.where(valid_values)
1624 ts2 = ts2.where(valid_values)
1625
1626 return np.ma.cov(
1627 np.ma.masked_invalid(ts1.sel(a=a, x=x).data.flatten()),
1628 np.ma.masked_invalid(ts2.sel(a=a, x=x).data.flatten()),
1629 ddof=ddof,
1630 )[0, 1]
1631
1632 expected = np.zeros((3, 4))
1633 for a in [0, 1, 2]:
1634 for x in [0, 1, 2, 3]:
1635 expected[a, x] = np_cov_ind(da_a, da_b, a=a, x=x)
1636 actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
1637 assert_allclose(actual, expected)
1638
1639 else:
1640
1641 def np_cov(ts1, ts2):
1642 # Ensure the ts are aligned and missing values ignored
1643 ts1, ts2 = broadcast(ts1, ts2)
1644 valid_values = ts1.notnull() & ts2.notnull()
1645
1646 ts1 = ts1.where(valid_values)
1647 ts2 = ts2.where(valid_values)
1648
1649 return np.ma.cov(
1650 np.ma.masked_invalid(ts1.data.flatten()),
1651 np.ma.masked_invalid(ts2.data.flatten()),
1652 ddof=ddof,
1653 )[0, 1]
1654
1655 expected = np_cov(da_a, da_b)
1656 actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
1657 assert_allclose(actual, expected)
1658
1659
1660@pytest.mark.parametrize("n", [0, 1, 2])

Callers

nothing calls this directly

Calls 4

assert_allcloseFunction · 0.90
np_cov_indFunction · 0.85
np_covFunction · 0.85
covMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…