more tests on rolling views

This commit is contained in:
Nicolas Poilvert
2012-02-12 01:06:47 -05:00
parent b4a1bf18ba
commit 39a487834c
2 changed files with 81 additions and 4 deletions
+6 -3
View File
@@ -209,9 +209,12 @@ def rolling_view(arr, window_shape):
"""
# -- basic requirements on inputs
assert isinstance(arr, np.ndarray)
assert isinstance(window_shape, tuple)
assert len(window_shape) == arr.ndim
if not isinstance(arr, np.ndarray):
raise ValueError('the input should be an ndarray object')
if not isinstance(window_shape, tuple):
raise ValueError('the window shape should be a tuple')
if not (len(window_shape) == arr.ndim):
raise ValueError('array dimension and window length dont match')
# -- input array dimension
N = arr.ndim
+75 -1
View File
@@ -1,7 +1,7 @@
import numpy as np
from nose.tools import raises
from numpy.testing import assert_equal
from skimage.util.array_views import block_view
from skimage.util.array_views import block_view, rolling_view
@raises(ValueError)
@@ -65,3 +65,77 @@ def test_block_view_3D_array():
[58, 59]]],
[[[76, 77],
[82, 83]]]]))
@raises(ValueError)
def test_rolling_view_input_not_array():
A = [1, 2, 3, 4, 5]
rolling_view(A, (2,))
@raises(ValueError)
def test_rolling_view_window_not_tuple():
A = np.arange(10)
rolling_view(A, [2])
@raises(ValueError)
def test_rolling_view_wrong_window_dimension():
A = np.arange(10)
rolling_view(A, (2,2))
@raises(ValueError)
def test_rolling_view_negative_window_length():
A = np.arange(10)
rolling_view(A, (-1,))
@raises(ValueError)
def test_rolling_view_window_too_large():
A = np.arange(10)
rolling_view(A, (11,))
def test_rolling_view_1D():
A = np.arange(10)
window_shape = (3,)
B = rolling_view(A, window_shape)
assert_equal(B, np.array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8],
[7, 8, 9]]))
def test_rolling_view_2D():
A = np.arange(5*4).reshape(5, 4)
window_shape = (4, 3)
B = rolling_view(A, window_shape)
assert_equal(B.shape, (2, 2, 4, 3))
assert_equal(B, np.array([[[[ 0, 1, 2],
[ 4, 5, 6],
[ 8, 9, 10],
[12, 13, 14]],
[[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11],
[13, 14, 15]]],
[[[ 4, 5, 6],
[ 8, 9, 10],
[12, 13, 14],
[16, 17, 18]],
[[ 5, 6, 7],
[ 9, 10, 11],
[13, 14, 15],
[17, 18, 19]]]]))