Skip to content

Commit

Permalink
Pad with zeros by default
Browse files Browse the repository at this point in the history
  • Loading branch information
santisoler committed Nov 6, 2021
1 parent 35e1d33 commit 526255f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 4 additions & 4 deletions xrft/padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
<xarray.DataArray (y: 5, x: 7)>
array([[0, 0, 0, 0, 0, 0, 0],
Expand Down Expand Up @@ -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
<xarray.DataArray (y: 3, x: 8)>
array([[0, 1, 2, 3, 0, 0, 0, 0],
Expand Down Expand Up @@ -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
<xarray.DataArray (y: 5, x: 7)>
array([[0, 0, 0, 0, 0, 0, 0],
Expand Down
18 changes: 8 additions & 10 deletions xrft/tests/test_padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 526255f

Please sign in to comment.