44"""
55
66import 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 ))
0 commit comments