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

Function test_groupby_math_nD_group

xarray/tests/test_groupby.py:1122–1158  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1120
1121
1122def test_groupby_math_nD_group() -> None:
1123 N = 40
1124 da = DataArray(
1125 np.random.random((N, N)),
1126 dims=("x", "y"),
1127 coords={
1128 "labels": (
1129 "x",
1130 np.repeat(["a", "b", "c", "d", "e", "f", "g", "h"], repeats=N // 8),
1131 ),
1132 },
1133 )
1134 da["labels2d"] = xr.broadcast(da.labels, da)[0]
1135
1136 g = da.groupby("labels2d")
1137 mean = g.mean()
1138 expected = da - mean.sel(labels2d=da.labels2d)
1139 expected["labels"] = expected.labels.broadcast_like(expected.labels2d)
1140 actual = g - mean
1141 assert_identical(expected, actual)
1142
1143 da["num"] = (
1144 "x",
1145 np.repeat([1, 2, 3, 4, 5, 6, 7, 8], repeats=N // 8),
1146 )
1147 da["num2d"] = xr.broadcast(da.num, da)[0]
1148 g = da.groupby_bins("num2d", bins=[0, 4, 6])
1149 mean = g.mean()
1150 idxr = np.digitize(da.num2d, bins=(0, 4, 6), right=True)[:30, :] - 1
1151 expanded_mean = mean.drop_vars("num2d_bins").isel(num2d_bins=(("x", "y"), idxr))
1152 expected = da.isel(x=slice(30)) - expanded_mean
1153 expected["labels"] = expected.labels.broadcast_like(expected.labels2d)
1154 expected["num"] = expected.num.broadcast_like(expected.num2d)
1155 # mean.num2d_bins.data is a pandas IntervalArray so needs to be put in `numpy` to allow indexing
1156 expected["num2d_bins"] = (("x", "y"), mean.num2d_bins.data.to_numpy()[idxr])
1157 actual = g - mean
1158 assert_identical(expected, actual)
1159
1160
1161def test_groupby_dataset_math_virtual() -> None:

Callers

nothing calls this directly

Calls 12

groupbyMethod · 0.95
groupby_binsMethod · 0.95
iselMethod · 0.95
DataArrayClass · 0.90
assert_identicalFunction · 0.90
repeatMethod · 0.80
meanMethod · 0.45
selMethod · 0.45
broadcast_likeMethod · 0.45
iselMethod · 0.45
drop_varsMethod · 0.45
to_numpyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…