-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limited implementation of pad (#461)
- Loading branch information
Showing
5 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from cubed.array_api.manipulation_functions import concat | ||
|
||
|
||
def pad(x, pad_width, mode=None, chunks=None): | ||
"""Pad an array.""" | ||
if len(pad_width) != x.ndim: | ||
raise ValueError("`pad_width` must have as many entries as array dimensions") | ||
axis = tuple( | ||
i | ||
for (i, (before, after)) in enumerate(pad_width) | ||
if (before != 0 or after != 0) | ||
) | ||
if len(axis) != 1: | ||
raise ValueError("only one axis can be padded") | ||
axis = axis[0] | ||
if pad_width[axis] != (1, 0): | ||
raise ValueError("only a pad width of (1, 0) is allowed") | ||
if mode != "symmetric": | ||
raise ValueError(f"Mode is not supported: {mode}") | ||
|
||
select = [] | ||
for i in range(x.ndim): | ||
if i == axis: | ||
select.append(slice(0, 1)) | ||
else: | ||
select.append(slice(None)) | ||
select = tuple(select) | ||
a = x[select] | ||
return concat([a, x], axis=axis, chunks=chunks or x.chunksize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
import pytest | ||
from numpy.testing import assert_array_equal | ||
|
||
import cubed | ||
import cubed.array_api as xp | ||
|
||
|
||
@pytest.fixture() | ||
def spec(tmp_path): | ||
return cubed.Spec(tmp_path, allowed_mem=100000) | ||
|
||
|
||
def test_pad(spec): | ||
an = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) | ||
|
||
a = xp.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]], chunks=(2, 2), spec=spec) | ||
b = cubed.pad(a, ((1, 0), (0, 0)), mode="symmetric") | ||
assert b.chunks == ((2, 2), (2, 1)) | ||
|
||
assert_array_equal(b.compute(), np.pad(an, ((1, 0), (0, 0)), mode="symmetric")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ Non-standardised functions | |
|
||
nanmean | ||
nansum | ||
pad | ||
|
||
Random number generation | ||
======================== | ||
|