From 526255ff0ff20280dd0723ddf5e649a05afbe51a Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Sat, 6 Nov 2021 12:29:43 -0300 Subject: [PATCH] Pad with zeros by default --- xrft/padding.py | 8 ++++---- xrft/tests/test_padding.py | 18 ++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/xrft/padding.py b/xrft/padding.py index cd2df763..c4b4937b 100644 --- a/xrft/padding.py +++ b/xrft/padding.py @@ -12,7 +12,7 @@ def pad( pad_width=None, mode="constant", stat_length=None, - constant_values=None, + constant_values=0, end_values=None, reflect_type=None, **pad_width_kwargs, @@ -107,7 +107,7 @@ def pad( ... coords={"x": [0, 1, 2], "y": [-5, -4, -3]}, ... dims=("y", "x"), ... ) - >>> da_padded = pad(da, x=2, y=1, constant_values=0) + >>> da_padded = pad(da, x=2, y=1) >>> da_padded array([[0, 0, 0, 0, 0, 0, 0], @@ -135,7 +135,7 @@ def pad( Asymmetric padding - >>> da_padded = pad(da, x=(1, 4), constant_values=0) + >>> da_padded = pad(da, x=(1, 4)) >>> da_padded array([[0, 1, 2, 3, 0, 0, 0, 0], @@ -317,7 +317,7 @@ def unpad(da, pad_width=None, **pad_width_kwargs): ... coords={"x": [0, 1, 2], "y": [-5, -4, -3]}, ... dims=("y", "x"), ... ) - >>> da_padded = pad(da, x=2, y=1, constant_values=0) + >>> da_padded = pad(da, x=2, y=1) >>> da_padded array([[0, 0, 0, 0, 0, 0, 0], diff --git a/xrft/tests/test_padding.py b/xrft/tests/test_padding.py index 334fc5fa..3a279fae 100644 --- a/xrft/tests/test_padding.py +++ b/xrft/tests/test_padding.py @@ -69,13 +69,12 @@ def test_pad_with_kwargs(sample_da_2d): """ Test pad function by passing pad_width as kwargs """ - # Check if the array is padded with nans by default padded_da = pad(sample_da_2d, x=2, y=1) assert padded_da.shape == (19, 15) - npt.assert_allclose(padded_da.values[:1, :], np.nan) - npt.assert_allclose(padded_da.values[-1:, :], np.nan) - npt.assert_allclose(padded_da.values[:, :2], np.nan) - npt.assert_allclose(padded_da.values[:, -2:], np.nan) + npt.assert_allclose(padded_da.values[:1, :], 0) + npt.assert_allclose(padded_da.values[-1:, :], 0) + npt.assert_allclose(padded_da.values[:, :2], 0) + npt.assert_allclose(padded_da.values[:, -2:], 0) npt.assert_allclose(padded_da.values[1:-1, 2:-2], sample_da_2d) npt.assert_allclose(padded_da.x, np.linspace(-2, 12, 15)) npt.assert_allclose(padded_da.y, np.linspace(-4.5, 4.5, 19)) @@ -85,14 +84,13 @@ def test_pad_with_pad_width(sample_da_2d): """ Test pad function by passing pad_width as argument """ - # Check if the array is padded with nans by default pad_width = {"x": (2, 3), "y": (1, 3)} padded_da = pad(sample_da_2d, pad_width) assert padded_da.shape == (21, 16) - npt.assert_allclose(padded_da.values[:1, :], np.nan) - npt.assert_allclose(padded_da.values[-3:, :], np.nan) - npt.assert_allclose(padded_da.values[:, :2], np.nan) - npt.assert_allclose(padded_da.values[:, -3:], np.nan) + npt.assert_allclose(padded_da.values[:1, :], 0) + npt.assert_allclose(padded_da.values[-3:, :], 0) + npt.assert_allclose(padded_da.values[:, :2], 0) + npt.assert_allclose(padded_da.values[:, -3:], 0) npt.assert_allclose(padded_da.values[1:-3, 2:-3], sample_da_2d) npt.assert_allclose(padded_da.x, np.linspace(-2, 13, 16)) npt.assert_allclose(padded_da.y, np.linspace(-4.5, 5.5, 21))