Skip to content

Commit 663e9e4

Browse files
author
Alexandre de Siqueira
authored
Merge pull request #5081 from Harry-Kwon/remove-private-imports
removed usage of numpy's private functions from util.arraycrop
2 parents 956c53b + 76ea67b commit 663e9e4

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

skimage/util/arraycrop.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import numpy as np
7-
from numpy.lib.arraypad import _as_pairs
87

98
__all__ = ['crop']
109

@@ -20,8 +19,8 @@ def crop(ar, crop_width, copy=False, order='K'):
2019
Number of values to remove from the edges of each axis.
2120
``((before_1, after_1),`` ... ``(before_N, after_N))`` specifies
2221
unique crop widths at the start and end of each axis.
23-
``((before, after),)`` specifies a fixed start and end crop
24-
for every axis.
22+
``((before, after),) or (before, after)`` specifies
23+
a fixed start and end crop for every axis.
2524
``(n,)`` or ``n`` for integer ``n`` is a shortcut for
2625
before = after = ``n`` for all axes.
2726
copy : bool, optional
@@ -39,7 +38,30 @@ def crop(ar, crop_width, copy=False, order='K'):
3938
view of the input array.
4039
"""
4140
ar = np.array(ar, copy=False)
42-
crops = _as_pairs(crop_width, ar.ndim, as_index=True)
41+
42+
if isinstance(crop_width, int):
43+
crops = [[crop_width, crop_width]] * ar.ndim
44+
elif isinstance(crop_width[0], int):
45+
if len(crop_width) == 1:
46+
crops = [[crop_width[0], crop_width[0]]] * ar.ndim
47+
elif len(crop_width) == 2:
48+
crops = [crop_width] * ar.ndim
49+
else:
50+
raise ValueError(
51+
f"crop_width has an invalid length: {len(crop_width)}\n"
52+
"crop_width should be a sequence of N pairs, "
53+
"a single pair, or a single integer"
54+
)
55+
elif len(crop_width) == 1:
56+
crops = [crop_width[0]] * ar.ndim
57+
elif len(crop_width) == ar.ndim:
58+
crops = crop_width
59+
else:
60+
raise ValueError(
61+
f"crop_width has an invalid length: {len(crop_width)}\n"
62+
"crop_width should be a sequence of N pairs, "
63+
"a single pair, or a single integer"
64+
)
4365

4466
slices = tuple(slice(a, ar.shape[i] - b)
4567
for i, (a, b) in enumerate(crops))

skimage/util/tests/test_arraycrop.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def test_pair_crop():
2020
assert_equal(out.shape, (6, 2))
2121

2222

23+
def test_pair_tuple_crop():
24+
arr = np.arange(45).reshape(9, 5)
25+
out = crop(arr, ((1, 2),))
26+
assert_array_equal(out[0], [6, 7])
27+
assert_array_equal(out[-1], [31, 32])
28+
assert_equal(out.shape, (6, 2))
29+
30+
2331
def test_int_crop():
2432
arr = np.arange(45).reshape(9, 5)
2533
out = crop(arr, 1)
@@ -28,6 +36,14 @@ def test_int_crop():
2836
assert_equal(out.shape, (7, 3))
2937

3038

39+
def test_int_tuple_crop():
40+
arr = np.arange(45).reshape(9, 5)
41+
out = crop(arr, (1,))
42+
assert_array_equal(out[0], [6, 7, 8])
43+
assert_array_equal(out[-1], [36, 37, 38])
44+
assert_equal(out.shape, (7, 3))
45+
46+
3147
def test_copy_crop():
3248
arr = np.arange(45).reshape(9, 5)
3349
out0 = crop(arr, 1, copy=True)

0 commit comments

Comments
 (0)