mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-14 11:18:06 +08:00
Merge pull request #847 from ahojnnes/template
Fix bugs in array padding and template matching
This commit is contained in:
@@ -39,9 +39,6 @@ Library:
|
||||
Extension: skimage.morphology._pnpoly
|
||||
Sources:
|
||||
skimage/morphology/_pnpoly.pyx
|
||||
Extension: skimage.feature._template
|
||||
Sources:
|
||||
skimage/feature/_template.pyx
|
||||
Extension: skimage.io._plugins._colormixer
|
||||
Sources:
|
||||
skimage/io/_plugins/_colormixer.pyx
|
||||
|
||||
@@ -41,4 +41,5 @@ cdef float integrate(float[:, ::1] sat, Py_ssize_t r0, Py_ssize_t c0,
|
||||
|
||||
if (c0 - 1 >= 0):
|
||||
S -= sat[r1, c0 - 1]
|
||||
|
||||
return S
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
#cython: cdivision=True
|
||||
#cython: boundscheck=False
|
||||
#cython: nonecheck=False
|
||||
#cython: wraparound=False
|
||||
|
||||
"""
|
||||
Template matching using normalized cross-correlation.
|
||||
|
||||
We use fast normalized cross-correlation algorithm (see [1]_ and [2]_) to
|
||||
compute match probability. This algorithm calculates the normalized
|
||||
cross-correlation of an image, `I`, with a template `T` according to the
|
||||
following equation::
|
||||
|
||||
sum{ I(x, y) [T(x, y) - <T>] }
|
||||
-------------------------------------------------------
|
||||
sqrt(sum{ [I(x, y) - <I>]^2 } sum{ [T(x, y) - <T>]^2 })
|
||||
|
||||
where `<T>` is the average of the template, and `<I>` is the average of the
|
||||
image *coincident with the template*, and sums are over the template and the
|
||||
image window coincident with the template. Note that the numerator is simply
|
||||
the cross-correlation of the image and the zero-mean template.
|
||||
|
||||
To speed up calculations, we use summed-area tables (a.k.a. integral images) to
|
||||
quickly calculate sums of image windows inside the loop. This step relies on
|
||||
the following relation (see Eq. 10 of [1])::
|
||||
|
||||
sum{ [I(x, y) - <I>]^2 } =
|
||||
sum{ I^2(x, y) } - [sum{ I(x, y) }]^2 / N_x N_y
|
||||
|
||||
(Without this relation, you would need to subtract each image-window mean from
|
||||
the image window *before* squaring.)
|
||||
|
||||
.. [1] Briechle and Hanebeck, "Template Matching using Fast Normalized
|
||||
Cross Correlation", Proceedings of the SPIE (2001).
|
||||
.. [2] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light and
|
||||
Magic.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
from scipy.signal import fftconvolve
|
||||
|
||||
cimport numpy as cnp
|
||||
from libc.math cimport sqrt, fabs
|
||||
from skimage._shared.transform cimport integrate
|
||||
|
||||
|
||||
from skimage.transform import integral
|
||||
|
||||
|
||||
def match_template(cnp.ndarray[float, ndim=2, mode="c"] image,
|
||||
cnp.ndarray[float, ndim=2, mode="c"] template):
|
||||
|
||||
cdef float[:, ::1] corr
|
||||
cdef float[:, ::1] image_sat
|
||||
cdef float[:, ::1] image_sqr_sat
|
||||
cdef float template_mean = np.mean(template)
|
||||
cdef float template_ssd
|
||||
cdef float inv_area
|
||||
cdef Py_ssize_t r, c, r_end, c_end
|
||||
cdef Py_ssize_t template_rows = template.shape[0]
|
||||
cdef Py_ssize_t template_cols = template.shape[1]
|
||||
cdef float den, window_sqr_sum, window_mean_sqr, window_sum
|
||||
|
||||
image_sat = integral.integral_image(image)
|
||||
image_sqr_sat = integral.integral_image(image**2)
|
||||
|
||||
template -= template_mean
|
||||
template_ssd = np.sum(template**2)
|
||||
# use inversed area for accuracy
|
||||
inv_area = 1.0 / (template.shape[0] * template.shape[1])
|
||||
|
||||
# when `dtype=float` is used, ascontiguousarray returns ``double``.
|
||||
corr = np.ascontiguousarray(fftconvolve(image,
|
||||
template[::-1, ::-1],
|
||||
mode="valid"),
|
||||
dtype=np.float32)
|
||||
|
||||
|
||||
# move window through convolution results, normalizing in the process
|
||||
for r in range(corr.shape[0]):
|
||||
for c in range(corr.shape[1]):
|
||||
# subtract 1 because `i_end` and `c_end` are used for indexing into
|
||||
# summed-area table, instead of slicing windows of the image.
|
||||
r_end = r + template_rows - 1
|
||||
c_end = c + template_cols - 1
|
||||
|
||||
window_sum = integrate(image_sat, r, c, r_end, c_end)
|
||||
window_mean_sqr = window_sum * window_sum * inv_area
|
||||
window_sqr_sum = integrate(image_sqr_sat, r, c, r_end, c_end)
|
||||
if window_sqr_sum <= window_mean_sqr:
|
||||
corr[r, c] = 0
|
||||
continue
|
||||
|
||||
den = sqrt((window_sqr_sum - window_mean_sqr) * template_ssd)
|
||||
corr[r, c] /= den
|
||||
|
||||
return np.asarray(corr)
|
||||
@@ -16,7 +16,6 @@ def configuration(parent_package='', top_path=None):
|
||||
cython(['censure_cy.pyx'], working_path=base_path)
|
||||
cython(['_brief_cy.pyx'], working_path=base_path)
|
||||
cython(['_texture.pyx'], working_path=base_path)
|
||||
cython(['_template.pyx'], working_path=base_path)
|
||||
|
||||
config.add_extension('corner_cy', sources=['corner_cy.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
@@ -26,8 +25,6 @@ def configuration(parent_package='', top_path=None):
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
config.add_extension('_texture', sources=['_texture.c'],
|
||||
include_dirs=[get_numpy_include_dirs(), '../_shared'])
|
||||
config.add_extension('_template', sources=['_template.c'],
|
||||
include_dirs=[get_numpy_include_dirs(), '../_shared'])
|
||||
|
||||
return config
|
||||
|
||||
|
||||
+146
-56
@@ -1,81 +1,171 @@
|
||||
"""template.py - Template matching
|
||||
"""
|
||||
import numpy as np
|
||||
from . import _template
|
||||
from scipy.signal import fftconvolve
|
||||
|
||||
from skimage.util import pad
|
||||
|
||||
|
||||
def match_template(image, template, pad_input=False):
|
||||
"""Match a template to a 2-D image using normalized correlation.
|
||||
def _window_sum_2d(image, window_shape):
|
||||
|
||||
The output is an array with values between -1.0 and 1.0, which correspond
|
||||
to the probability that the template is found at that position.
|
||||
window_sum = np.cumsum(image, axis=0)
|
||||
window_sum = (window_sum[window_shape[0]:-1]
|
||||
- window_sum[:-window_shape[0]-1])
|
||||
|
||||
window_sum = np.cumsum(window_sum, axis=1)
|
||||
window_sum = (window_sum[:, window_shape[1]:-1]
|
||||
- window_sum[:, :-window_shape[1]-1])
|
||||
|
||||
return window_sum
|
||||
|
||||
|
||||
def _window_sum_3d(image, window_shape):
|
||||
|
||||
window_sum = _window_sum_2d(image, window_shape)
|
||||
|
||||
window_sum = np.cumsum(window_sum, axis=2)
|
||||
window_sum = (window_sum[:, :, window_shape[2]:-1]
|
||||
- window_sum[:, :, :-window_shape[2]-1])
|
||||
|
||||
return window_sum
|
||||
|
||||
|
||||
def match_template(image, template, pad_input=False, mode='constant',
|
||||
constant_values=0):
|
||||
"""Match a template to a 2-D or 3-D image using normalized correlation.
|
||||
|
||||
The output is an array with values between -1.0 and 1.0. The value at a
|
||||
given position corresponds to the correlation coefficient between the image
|
||||
and the template.
|
||||
|
||||
For `pad_input=True` matches correspond to the center and otherwise to the
|
||||
top-left corner of the template. To find the best match you must search for
|
||||
peaks in the response (output) image.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
image : array_like
|
||||
2-D Image to process.
|
||||
template : array_like
|
||||
Template to locate.
|
||||
image : (M, N[, D]) array
|
||||
2-D or 3-D input image.
|
||||
template : (m, n[, d]) array
|
||||
Template to locate. It must be `(m <= M, n <= N[, d <= D])`.
|
||||
pad_input : bool
|
||||
If True, pad `image` with image mean so that output is the same size as
|
||||
the image, and output values correspond to the template center.
|
||||
Otherwise, the output is an array with shape `(M - m + 1, N - n + 1)`
|
||||
for an `(M, N)` image and an `(m, n)` template, and matches correspond
|
||||
to origin (top-left corner) of the template.
|
||||
If True, pad `image` so that output is the same size as the image, and
|
||||
output values correspond to the template center. Otherwise, the output
|
||||
is an array with shape `(M - m + 1, N - n + 1)` for an `(M, N)` image
|
||||
and an `(m, n)` template, and matches correspond to origin
|
||||
(top-left corner) of the template.
|
||||
mode : see `numpy.pad`, optional
|
||||
Padding mode.
|
||||
constant_values : see `numpy.pad`, optional
|
||||
Constant values used in conjunction with ``mode='constant'``.
|
||||
|
||||
Returns
|
||||
-------
|
||||
output : ndarray
|
||||
Correlation results between -1.0 and 1.0. For an `(M, N)` image and an
|
||||
`(m, n)` template, the `output` is `(M - m + 1, N - n + 1)` when
|
||||
`pad_input = False` and `(M, N)` when `pad_input = True`.
|
||||
output : array
|
||||
Response image with correlation coefficients.
|
||||
|
||||
References
|
||||
----------
|
||||
.. [1] Briechle and Hanebeck, "Template Matching using Fast Normalized
|
||||
Cross Correlation", Proceedings of the SPIE (2001).
|
||||
.. [2] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light
|
||||
and Magic.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> template = np.zeros((3, 3))
|
||||
>>> template[1, 1] = 1
|
||||
>>> print(template)
|
||||
[[ 0. 0. 0.]
|
||||
[ 0. 1. 0.]
|
||||
[ 0. 0. 0.]]
|
||||
>>> template
|
||||
array([[ 0., 0., 0.],
|
||||
[ 0., 1., 0.],
|
||||
[ 0., 0., 0.]])
|
||||
>>> image = np.zeros((6, 6))
|
||||
>>> image[1, 1] = 1
|
||||
>>> image[4, 4] = -1
|
||||
>>> print(image)
|
||||
[[ 0. 0. 0. 0. 0. 0.]
|
||||
[ 0. 1. 0. 0. 0. 0.]
|
||||
[ 0. 0. 0. 0. 0. 0.]
|
||||
[ 0. 0. 0. 0. 0. 0.]
|
||||
[ 0. 0. 0. 0. -1. 0.]
|
||||
[ 0. 0. 0. 0. 0. 0.]]
|
||||
>>> image
|
||||
array([[ 0., 0., 0., 0., 0., 0.],
|
||||
[ 0., 1., 0., 0., 0., 0.],
|
||||
[ 0., 0., 0., 0., 0., 0.],
|
||||
[ 0., 0., 0., 0., 0., 0.],
|
||||
[ 0., 0., 0., 0., -1., 0.],
|
||||
[ 0., 0., 0., 0., 0., 0.]])
|
||||
>>> result = match_template(image, template)
|
||||
>>> print(np.round(result, 3))
|
||||
[[ 1. -0.125 0. 0. ]
|
||||
[-0.125 -0.125 0. 0. ]
|
||||
[ 0. 0. 0.125 0.125]
|
||||
[ 0. 0. 0.125 -1. ]]
|
||||
>>> np.round(result, 3)
|
||||
array([[ 1. , -0.125, 0. , 0. ],
|
||||
[-0.125, -0.125, 0. , 0. ],
|
||||
[ 0. , 0. , 0.125, 0.125],
|
||||
[ 0. , 0. , 0.125, -1. ]], dtype=float32)
|
||||
>>> result = match_template(image, template, pad_input=True)
|
||||
>>> print(np.round(result, 3))
|
||||
[[-0.125 -0.125 -0.125 0. 0. 0. ]
|
||||
[-0.125 1. -0.125 0. 0. 0. ]
|
||||
[-0.125 -0.125 -0.125 0. 0. 0. ]
|
||||
[ 0. 0. 0. 0.125 0.125 0.125]
|
||||
[ 0. 0. 0. 0.125 -1. 0.125]
|
||||
[ 0. 0. 0. 0.125 0.125 0.125]]
|
||||
>>> np.round(result, 3)
|
||||
array([[-0.125, -0.125, -0.125, 0. , 0. , 0. ],
|
||||
[-0.125, 1. , -0.125, 0. , 0. , 0. ],
|
||||
[-0.125, -0.125, -0.125, 0. , 0. , 0. ],
|
||||
[ 0. , 0. , 0. , 0.125, 0.125, 0.125],
|
||||
[ 0. , 0. , 0. , 0.125, -1. , 0.125],
|
||||
[ 0. , 0. , 0. , 0.125, 0.125, 0.125]], dtype=float32)
|
||||
"""
|
||||
|
||||
if image.ndim not in (2, 3) or template.ndim not in (2, 3):
|
||||
raise ValueError("Only 2- and 3-D images supported.")
|
||||
if image.ndim < template.ndim:
|
||||
raise ValueError("Dimensionality of template must be less than or "
|
||||
"equal to the dimensionality of image.")
|
||||
if np.any(np.less(image.shape, template.shape)):
|
||||
raise ValueError("Image must be larger than template.")
|
||||
image = np.ascontiguousarray(image, dtype=np.float32)
|
||||
template = np.ascontiguousarray(template, dtype=np.float32)
|
||||
|
||||
if pad_input:
|
||||
pad_size = tuple(np.array(image.shape) + np.array(template.shape) - 1)
|
||||
pad_image = np.mean(image) * np.ones(pad_size, dtype=np.float32)
|
||||
h, w = image.shape
|
||||
i0, j0 = template.shape
|
||||
i0 /= 2
|
||||
j0 /= 2
|
||||
pad_image[i0:i0 + h, j0:j0 + w] = image
|
||||
image = pad_image
|
||||
result = _template.match_template(image, template)
|
||||
return result
|
||||
image_shape = image.shape
|
||||
|
||||
image = np.array(image, dtype=np.float32, copy=False)
|
||||
|
||||
pad_width = tuple((width, width) for width in template.shape)
|
||||
if mode == 'constant':
|
||||
image = pad(image, pad_width=pad_width, mode=mode,
|
||||
constant_values=constant_values)
|
||||
else:
|
||||
image = pad(image, pad_width=pad_width, mode=mode)
|
||||
|
||||
# Use special case for 2-D images for much better performance in
|
||||
# computation of integral images
|
||||
if image.ndim == 2:
|
||||
image_window_sum = _window_sum_2d(image, template.shape)
|
||||
image_window_sum2 = _window_sum_2d(image**2, template.shape)
|
||||
elif image.ndim == 3:
|
||||
image_window_sum = _window_sum_3d(image, template.shape)
|
||||
image_window_sum2 = _window_sum_3d(image**2, template.shape)
|
||||
|
||||
template_volume = np.prod(template.shape)
|
||||
template_ssd = np.sum((template - template.mean())**2)
|
||||
|
||||
if image.ndim == 2:
|
||||
xcorr = fftconvolve(image, template[::-1, ::-1],
|
||||
mode="valid")[1:-1, 1:-1]
|
||||
elif image.ndim == 3:
|
||||
xcorr = fftconvolve(image, template[::-1, ::-1, ::-1],
|
||||
mode="valid")[1:-1, 1:-1, 1:-1]
|
||||
|
||||
nom = xcorr - image_window_sum * (template.sum() / template_volume)
|
||||
|
||||
denom = image_window_sum2
|
||||
np.multiply(image_window_sum, image_window_sum, out=image_window_sum)
|
||||
np.divide(image_window_sum, template_volume, out=image_window_sum)
|
||||
denom -= image_window_sum
|
||||
denom *= template_ssd
|
||||
np.maximum(denom, 0, out=denom) # sqrt of negative number not allowed
|
||||
np.sqrt(denom, out=denom)
|
||||
|
||||
response = np.zeros_like(xcorr, dtype=np.float32)
|
||||
|
||||
# avoid zero-division
|
||||
mask = denom > np.finfo(np.float32).eps
|
||||
|
||||
response[mask] = nom[mask] / denom[mask]
|
||||
|
||||
slices = []
|
||||
for i in range(template.ndim):
|
||||
if pad_input:
|
||||
d0 = (template.shape[i] - 1) // 2
|
||||
d1 = d0 + image_shape[i]
|
||||
else:
|
||||
d0 = template.shape[i] - 1
|
||||
d1 = d0 + image_shape[i] - template.shape[i] + 1
|
||||
slices.append(slice(d0, d1))
|
||||
|
||||
return response[slices]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from numpy.testing import assert_array_almost_equal as assert_close
|
||||
from numpy.testing import assert_almost_equal, assert_equal, assert_raises
|
||||
|
||||
from skimage.morphology import diamond
|
||||
from skimage.feature import match_template, peak_local_max
|
||||
@@ -31,7 +31,7 @@ def test_template():
|
||||
positions = positions[np.argsort(positions[:, 0])]
|
||||
|
||||
for xy_target, xy in zip(target_positions, positions):
|
||||
yield assert_close, xy, xy_target
|
||||
yield assert_almost_equal, xy, xy_target
|
||||
|
||||
|
||||
def test_normalization():
|
||||
@@ -88,7 +88,7 @@ def test_no_nans():
|
||||
def test_switched_arguments():
|
||||
image = np.ones((5, 5))
|
||||
template = np.ones((3, 3))
|
||||
np.testing.assert_raises(ValueError, match_template, template, image)
|
||||
assert_raises(ValueError, match_template, template, image)
|
||||
|
||||
|
||||
def test_pad_input():
|
||||
@@ -108,14 +108,66 @@ def test_pad_input():
|
||||
image[mid, -9:-4] -= template # full min template centered at 12
|
||||
image[mid, -3:] += template[:, :3] # half max template centered at 18
|
||||
|
||||
result = match_template(image, template, pad_input=True)
|
||||
result = match_template(image, template, pad_input=True,
|
||||
constant_values=image.mean())
|
||||
|
||||
# get the max and min results.
|
||||
sorted_result = np.argsort(result.flat)
|
||||
i, j = np.unravel_index(sorted_result[:2], result.shape)
|
||||
assert_close(j, (12, 0))
|
||||
assert_equal(j, (12, 0))
|
||||
i, j = np.unravel_index(sorted_result[-2:], result.shape)
|
||||
assert_close(j, (18, 6))
|
||||
assert_equal(j, (18, 6))
|
||||
|
||||
|
||||
def test_3d():
|
||||
np.random.seed(1)
|
||||
template = np.random.rand(3, 3, 3)
|
||||
image = np.zeros((12, 12, 12))
|
||||
|
||||
image[3:6, 5:8, 4:7] = template
|
||||
|
||||
result = match_template(image, template)
|
||||
|
||||
assert_equal(result.shape, (10, 10, 10))
|
||||
assert_equal(np.unravel_index(result.argmax(), result.shape), (3, 5, 4))
|
||||
|
||||
|
||||
def test_3d_pad_input():
|
||||
np.random.seed(1)
|
||||
template = np.random.rand(3, 3, 3)
|
||||
image = np.zeros((12, 12, 12))
|
||||
|
||||
image[3:6, 5:8, 4:7] = template
|
||||
|
||||
result = match_template(image, template, pad_input=True)
|
||||
|
||||
assert_equal(result.shape, (12, 12, 12))
|
||||
assert_equal(np.unravel_index(result.argmax(), result.shape), (4, 6, 5))
|
||||
|
||||
|
||||
def test_padding_reflect():
|
||||
template = diamond(2)
|
||||
image = np.zeros((10, 10))
|
||||
image[2:7, :3] = template[:, -3:]
|
||||
|
||||
result = match_template(image, template, pad_input=True,
|
||||
mode='reflect')
|
||||
|
||||
assert_equal(np.unravel_index(result.argmax(), result.shape), (4, 0))
|
||||
|
||||
|
||||
def test_wrong_input():
|
||||
image = np.ones((5, 5, 1))
|
||||
template = np.ones((3, 3))
|
||||
assert_raises(ValueError, match_template, template, image)
|
||||
|
||||
image = np.ones((5, 5))
|
||||
template = np.ones((3, 3, 2))
|
||||
assert_raises(ValueError, match_template, template, image)
|
||||
|
||||
image = np.ones((5, 5, 3, 3))
|
||||
template = np.ones((3, 3, 2))
|
||||
assert_raises(ValueError, match_template, template, image)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,14 +3,7 @@ from .dtype import (img_as_float, img_as_int, img_as_uint, img_as_ubyte,
|
||||
from .shape import view_as_blocks, view_as_windows
|
||||
from .noise import random_noise
|
||||
|
||||
import numpy
|
||||
ver = numpy.__version__.split('.')
|
||||
chk = int(ver[0] + ver[1])
|
||||
if chk < 18: # Use internal version for numpy versions < 1.8.x
|
||||
from .arraypad import pad
|
||||
else:
|
||||
from numpy import pad
|
||||
del numpy, ver, chk
|
||||
from .arraypad import pad
|
||||
from ._regular_grid import regular_grid
|
||||
from .unique import unique_rows
|
||||
|
||||
|
||||
@@ -1027,7 +1027,11 @@ def _normalize_shape(narray, shape):
|
||||
"""
|
||||
normshp = None
|
||||
shapelen = len(np.shape(narray))
|
||||
if (isinstance(shape, int)) or shape is None:
|
||||
|
||||
if isinstance(shape, np.ndarray):
|
||||
shape = shape.tolist()
|
||||
|
||||
if isinstance(shape, (int, float)) or shape is None:
|
||||
normshp = ((shape, shape), ) * shapelen
|
||||
elif (isinstance(shape, (tuple, list))
|
||||
and isinstance(shape[0], (tuple, list))
|
||||
|
||||
+431
-331
@@ -13,209 +13,219 @@ class TestStatistic(TestCase):
|
||||
def test_check_mean_stat_length(self):
|
||||
a = np.arange(100).astype('f')
|
||||
a = pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), ))
|
||||
b = np.array([
|
||||
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
b = np.array(
|
||||
[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
0.5, 0.5, 0.5, 0.5, 0.5,
|
||||
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
|
||||
98., 98., 98., 98., 98., 98., 98., 98., 98., 98.,
|
||||
98., 98., 98., 98., 98., 98., 98., 98., 98., 98.])
|
||||
98., 98., 98., 98., 98., 98., 98., 98., 98., 98.,
|
||||
98., 98., 98., 98., 98., 98., 98., 98., 98., 98.
|
||||
])
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_maximum_1(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'maximum')
|
||||
b = np.array([
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99,
|
||||
b = np.array(
|
||||
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99])
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_maximum_2(self):
|
||||
a = np.arange(100) + 1
|
||||
a = pad(a, (25, 20), 'maximum')
|
||||
b = np.array([
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100,
|
||||
b = np.array(
|
||||
[100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100,
|
||||
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
|
||||
51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
|
||||
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
|
||||
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
|
||||
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
|
||||
91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
|
||||
51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
|
||||
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
|
||||
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
|
||||
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
|
||||
91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
|
||||
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100])
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_minimum_1(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'minimum')
|
||||
b = np.array([
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0,
|
||||
b = np.array(
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_minimum_2(self):
|
||||
a = np.arange(100) + 2
|
||||
a = pad(a, (25, 20), 'minimum')
|
||||
b = np.array([
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2,
|
||||
b = np.array(
|
||||
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2,
|
||||
|
||||
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
|
||||
12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
|
||||
22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
|
||||
42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
|
||||
62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
|
||||
72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
||||
82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
|
||||
92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
|
||||
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
|
||||
12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
|
||||
22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
|
||||
42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
|
||||
62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
|
||||
72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
||||
82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
|
||||
92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
|
||||
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_median(self):
|
||||
a = np.arange(100).astype('f')
|
||||
a = pad(a, (25, 20), 'median')
|
||||
b = np.array([
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
b = np.array(
|
||||
[49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5])
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_median_01(self):
|
||||
a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
|
||||
a = pad(a, 1, 'median')
|
||||
b = np.array([
|
||||
[4, 4, 5, 4, 4],
|
||||
b = np.array(
|
||||
[[4, 4, 5, 4, 4],
|
||||
|
||||
[3, 3, 1, 4, 3],
|
||||
[5, 4, 5, 9, 5],
|
||||
[8, 9, 8, 2, 8],
|
||||
[3, 3, 1, 4, 3],
|
||||
[5, 4, 5, 9, 5],
|
||||
[8, 9, 8, 2, 8],
|
||||
|
||||
[4, 4, 5, 4, 4]])
|
||||
[4, 4, 5, 4, 4]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_median_02(self):
|
||||
a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
|
||||
a = pad(a.T, 1, 'median').T
|
||||
b = np.array([
|
||||
[5, 4, 5, 4, 5],
|
||||
b = np.array(
|
||||
[[5, 4, 5, 4, 5],
|
||||
|
||||
[3, 3, 1, 4, 3],
|
||||
[5, 4, 5, 9, 5],
|
||||
[8, 9, 8, 2, 8],
|
||||
[3, 3, 1, 4, 3],
|
||||
[5, 4, 5, 9, 5],
|
||||
[8, 9, 8, 2, 8],
|
||||
|
||||
[5, 4, 5, 4, 5]])
|
||||
[5, 4, 5, 4, 5]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_mean_shape_one(self):
|
||||
a = [[4, 5, 6]]
|
||||
a = pad(a, (5, 7), 'mean', stat_length=2)
|
||||
b = np.array([
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
b = np.array(
|
||||
[[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]])
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
|
||||
[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_mean_2(self):
|
||||
a = np.arange(100).astype('f')
|
||||
a = pad(a, (25, 20), 'mean')
|
||||
b = np.array([
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
b = np.array(
|
||||
[49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
|
||||
10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
|
||||
30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
|
||||
40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
|
||||
50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
|
||||
60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
|
||||
70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
|
||||
80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
|
||||
90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
|
||||
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5])
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
|
||||
49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
|
||||
@@ -223,23 +233,73 @@ class TestConstant(TestCase):
|
||||
def test_check_constant(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'constant', constant_values=(10, 20))
|
||||
b = np.array([10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
10, 10, 10, 10, 10,
|
||||
b = np.array(
|
||||
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
10, 10, 10, 10, 10,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20])
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_constant_float(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'constant', constant_values=-1.1)
|
||||
b = np.array(
|
||||
[-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_constant_float(self):
|
||||
a = np.arange(100, dtype=float)
|
||||
a = pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2))
|
||||
b = np.array(
|
||||
[-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
-1.1, -1.1, -1.1, -1.1, -1.1,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
-1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2,
|
||||
-1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
|
||||
@@ -247,24 +307,25 @@ class TestLinearRamp(TestCase):
|
||||
def test_check_simple(self):
|
||||
a = np.arange(100).astype('f')
|
||||
a = pad(a, (25, 20), 'linear_ramp', end_values=(4, 5))
|
||||
b = np.array([
|
||||
4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
|
||||
2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
|
||||
0.80, 0.64, 0.48, 0.32, 0.16,
|
||||
b = np.array(
|
||||
[4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
|
||||
2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
|
||||
0.80, 0.64, 0.48, 0.32, 0.16,
|
||||
|
||||
0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
|
||||
10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
|
||||
20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
|
||||
30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
|
||||
40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
|
||||
50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
|
||||
60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
|
||||
70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
|
||||
80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
|
||||
90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
|
||||
0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
|
||||
10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
|
||||
20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
|
||||
30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
|
||||
40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
|
||||
50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
|
||||
60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
|
||||
70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
|
||||
80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
|
||||
90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
|
||||
|
||||
94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
|
||||
47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.])
|
||||
94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
|
||||
47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]
|
||||
)
|
||||
assert_array_almost_equal(a, b, decimal=5)
|
||||
|
||||
|
||||
@@ -272,67 +333,70 @@ class TestReflect(TestCase):
|
||||
def test_check_simple(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'reflect')
|
||||
b = np.array([
|
||||
25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
|
||||
15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
|
||||
5, 4, 3, 2, 1,
|
||||
b = np.array(
|
||||
[25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
|
||||
15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
|
||||
5, 4, 3, 2, 1,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
98, 97, 96, 95, 94, 93, 92, 91, 90, 89,
|
||||
88, 87, 86, 85, 84, 83, 82, 81, 80, 79])
|
||||
98, 97, 96, 95, 94, 93, 92, 91, 90, 89,
|
||||
88, 87, 86, 85, 84, 83, 82, 81, 80, 79]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_large_pad(self):
|
||||
a = [[4, 5, 6], [6, 7, 8]]
|
||||
a = pad(a, (5, 7), 'reflect')
|
||||
b = np.array([
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
b = np.array(
|
||||
[[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]])
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_shape(self):
|
||||
a = [[4, 5, 6]]
|
||||
a = pad(a, (5, 7), 'reflect')
|
||||
b = np.array([
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
b = np.array(
|
||||
[[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]])
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
|
||||
[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_01(self):
|
||||
@@ -355,83 +419,85 @@ class TestWrap(TestCase):
|
||||
def test_check_simple(self):
|
||||
a = np.arange(100)
|
||||
a = pad(a, (25, 20), 'wrap')
|
||||
b = np.array([
|
||||
75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
|
||||
85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
|
||||
95, 96, 97, 98, 99,
|
||||
b = np.array(
|
||||
[75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
|
||||
85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
|
||||
95, 96, 97, 98, 99,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
||||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
||||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
||||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
||||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
|
||||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
|
||||
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_large_pad(self):
|
||||
a = np.arange(12)
|
||||
a = np.reshape(a, (3, 4))
|
||||
a = pad(a, (10, 12), 'wrap')
|
||||
b = np.array([
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
b = np.array(
|
||||
[[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11]])
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11],
|
||||
[2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
|
||||
3, 0, 1, 2, 3, 0, 1, 2, 3],
|
||||
[6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
|
||||
7, 4, 5, 6, 7, 4, 5, 6, 7],
|
||||
[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
|
||||
11, 8, 9, 10, 11, 8, 9, 10, 11]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
def test_check_01(self):
|
||||
@@ -450,19 +516,21 @@ class TestStatLen(TestCase):
|
||||
a = np.arange(30)
|
||||
a = np.reshape(a, (6, 5))
|
||||
a = pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,))
|
||||
b = np.array([[ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
[ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
b = np.array(
|
||||
[[6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
[6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
|
||||
[ 1, 1, 1, 0, 1, 2, 3, 4, 3, 3],
|
||||
[ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
[11, 11, 11, 10, 11, 12, 13, 14, 13, 13],
|
||||
[16, 16, 16, 15, 16, 17, 18, 19, 18, 18],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[26, 26, 26, 25, 26, 27, 28, 29, 28, 28],
|
||||
[1, 1, 1, 0, 1, 2, 3, 4, 3, 3],
|
||||
[6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
|
||||
[11, 11, 11, 10, 11, 12, 13, 14, 13, 13],
|
||||
[16, 16, 16, 15, 16, 17, 18, 19, 18, 18],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[26, 26, 26, 25, 26, 27, 28, 29, 28, 28],
|
||||
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23]])
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
|
||||
[21, 21, 21, 20, 21, 22, 23, 24, 23, 23]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
|
||||
@@ -471,58 +539,90 @@ class TestEdge(TestCase):
|
||||
a = np.arange(12)
|
||||
a = np.reshape(a, (4, 3))
|
||||
a = pad(a, ((2, 3), (3, 2)), 'edge')
|
||||
b = np.array([
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
b = np.array(
|
||||
[[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[3, 3, 3, 3, 4, 5, 5, 5],
|
||||
[6, 6, 6, 6, 7, 8, 8, 8],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[3, 3, 3, 3, 4, 5, 5, 5],
|
||||
[6, 6, 6, 6, 7, 8, 8, 8],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11]])
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
|
||||
def test_check_too_many_pad_axes():
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3), (3, 2), (4, 5)),
|
||||
**kwargs)
|
||||
class TestZeroPadWidth(TestCase):
|
||||
def test_zero_pad_width(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
for pad_width in (0, (0, 0), ((0, 0), (0, 0))):
|
||||
assert_array_equal(arr, pad(arr, pad_width, mode='constant'))
|
||||
|
||||
|
||||
def test_check_negative_stat_length():
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(-3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3), (3, 2)),
|
||||
**kwargs)
|
||||
class TestNdarrayPadWidth(TestCase):
|
||||
def test_check_simple(self):
|
||||
a = np.arange(12)
|
||||
a = np.reshape(a, (4, 3))
|
||||
a = pad(a, np.array(((2, 3), (3, 2))), 'edge')
|
||||
b = np.array(
|
||||
[[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
|
||||
[0, 0, 0, 0, 1, 2, 2, 2],
|
||||
[3, 3, 3, 3, 4, 5, 5, 5],
|
||||
[6, 6, 6, 6, 7, 8, 8, 8],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11],
|
||||
[9, 9, 9, 9, 10, 11, 11, 11]]
|
||||
)
|
||||
assert_array_equal(a, b)
|
||||
|
||||
|
||||
def test_check_negative_pad_width():
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)),
|
||||
**kwargs)
|
||||
class ValueError1(TestCase):
|
||||
def test_check_simple(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3), (3, 2), (4, 5)),
|
||||
**kwargs)
|
||||
|
||||
def test_check_negative_stat_length(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(-3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3), (3, 2)),
|
||||
**kwargs)
|
||||
|
||||
def test_check_negative_pad_width(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)),
|
||||
**kwargs)
|
||||
|
||||
|
||||
def test_pad_one_axis_three_ways():
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3, 4), (3, 2)),
|
||||
**kwargs)
|
||||
class ValueError2(TestCase):
|
||||
def test_check_simple(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((2, 3, 4), (3, 2)),
|
||||
**kwargs)
|
||||
|
||||
|
||||
def test_zero_pad_width():
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
for pad_width in (0, (0, 0), ((0, 0), (0, 0))):
|
||||
assert np.all(arr == pad(arr, pad_width, mode='constant'))
|
||||
class ValueError3(TestCase):
|
||||
def test_check_simple(self):
|
||||
arr = np.arange(30)
|
||||
arr = np.reshape(arr, (6, 5))
|
||||
kwargs = dict(mode='mean', stat_length=(3, ))
|
||||
assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)),
|
||||
**kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user