(self)
| 2335 | v.coarsen(windows, func, boundary, side) |
| 2336 | |
| 2337 | def test_coarsen_2d(self): |
| 2338 | # 2d-mean should be the same with the successive 1d-mean |
| 2339 | v = self.cls(["x", "y"], np.arange(6 * 12).reshape(6, 12)) |
| 2340 | actual = v.coarsen({"x": 3, "y": 4}, func="mean") |
| 2341 | expected = v.coarsen({"x": 3}, func="mean").coarsen({"y": 4}, func="mean") |
| 2342 | assert_equal(actual, expected) |
| 2343 | |
| 2344 | v = self.cls(["x", "y"], np.arange(7 * 12).reshape(7, 12)) |
| 2345 | actual = v.coarsen({"x": 3, "y": 4}, func="mean", boundary="trim") |
| 2346 | expected = v.coarsen({"x": 3}, func="mean", boundary="trim").coarsen( |
| 2347 | {"y": 4}, func="mean", boundary="trim" |
| 2348 | ) |
| 2349 | assert_equal(actual, expected) |
| 2350 | |
| 2351 | # if there is nan, the two should be different |
| 2352 | v = self.cls(["x", "y"], 1.0 * np.arange(6 * 12).reshape(6, 12)) |
| 2353 | v[2, 4] = np.nan |
| 2354 | v[3, 5] = np.nan |
| 2355 | actual = v.coarsen({"x": 3, "y": 4}, func="mean", boundary="trim") |
| 2356 | expected = ( |
| 2357 | v.coarsen({"x": 3}, func="sum", boundary="trim").coarsen( |
| 2358 | {"y": 4}, func="sum", boundary="trim" |
| 2359 | ) |
| 2360 | / 12 |
| 2361 | ) |
| 2362 | assert not actual.equals(expected) |
| 2363 | # adjusting the nan count |
| 2364 | expected[0, 1] *= 12 / 11 |
| 2365 | expected[1, 1] *= 12 / 11 |
| 2366 | assert_allclose(actual, expected) |
| 2367 | |
| 2368 | v = self.cls(("x", "y"), np.arange(4 * 4, dtype=np.float32).reshape(4, 4)) |
| 2369 | actual = v.coarsen(dict(x=2, y=2), func="count", boundary="exact") |
| 2370 | expected = self.cls(("x", "y"), 4 * np.ones((2, 2))) |
| 2371 | assert_equal(actual, expected) |
| 2372 | |
| 2373 | v[0, 0] = np.nan |
| 2374 | v[-1, -1] = np.nan |
| 2375 | expected[0, 0] = 3 |
| 2376 | expected[-1, -1] = 3 |
| 2377 | actual = v.coarsen(dict(x=2, y=2), func="count", boundary="exact") |
| 2378 | assert_equal(actual, expected) |
| 2379 | |
| 2380 | actual = v.coarsen(dict(x=2, y=2), func="sum", boundary="exact", skipna=False) |
| 2381 | expected = self.cls(("x", "y"), [[np.nan, 18], [42, np.nan]]) |
| 2382 | assert_equal(actual, expected) |
| 2383 | |
| 2384 | actual = v.coarsen(dict(x=2, y=2), func="sum", boundary="exact", skipna=True) |
| 2385 | expected = self.cls(("x", "y"), [[10, 18], [42, 35]]) |
| 2386 | assert_equal(actual, expected) |
| 2387 | |
| 2388 | # perhaps @pytest.mark.parametrize("operation", [f for f in duck_array_ops]) |
| 2389 | def test_coarsen_keep_attrs(self, operation="mean"): |
no test coverage detected