diff --git a/doc/source/api_changes.txt b/doc/source/api_changes.txt index 527c9fb0..14a66975 100644 --- a/doc/source/api_changes.txt +++ b/doc/source/api_changes.txt @@ -1,6 +1,9 @@ Version 0.10 ------------ - Removed ``skimage.io.video`` functionality due to broken gstreamer bindings +- The functions `skimage.util.view_as_blocks` and + `skimage.util.view_as_windows` now raise exceptions if the input data is not + contiguous. Version 0.9 ----------- diff --git a/skimage/util/shape.py b/skimage/util/shape.py index 95e0a42e..d61a647e 100644 --- a/skimage/util/shape.py +++ b/skimage/util/shape.py @@ -11,16 +11,16 @@ def view_as_blocks(arr_in, block_shape): Parameters ---------- - arr_in: ndarray - The n-dimensional input array. + arr_in : ndarray + Contiguous N-d input array. - block_shape: tuple + block_shape : tuple The shape of the block. Each dimension must divide evenly into the corresponding dimensions of `arr_in`. Returns ------- - arr_out: ndarray + arr_out : ndarray Block view of the input array. Examples @@ -70,8 +70,6 @@ def view_as_blocks(arr_in, block_shape): [[[76, 77], [82, 83]]]]) """ - - # -- basic checks on arguments if not isinstance(block_shape, tuple): raise TypeError('block needs to be a tuple') @@ -88,6 +86,9 @@ def view_as_blocks(arr_in, block_shape): raise ValueError("'block_shape' is not compatible with 'arr_in'") # -- restride the array to build the block view + if not arr_in.flags.contiguous: + raise ValueError("Cannot provide views on a non-contiguous input array") + arr_in = np.ascontiguousarray(arr_in) new_shape = tuple(arr_shape / block_shape) + tuple(block_shape) @@ -107,7 +108,7 @@ def view_as_windows(arr_in, window_shape, step=1): Parameters ---------- arr_in: ndarray - The n-dimensional input array. + Contiguous N-d input array. window_shape: tuple Defines the shape of the elementary n-dimensional orthotope (better know as hyperrectangle [1]_) of the rolling window view. @@ -228,6 +229,9 @@ def view_as_windows(arr_in, window_shape, step=1): raise ValueError("`window_shape` is too small") # -- build rolling window view + if not arr_in.flags.contiguous: + raise ValueError("Cannot provide views on a non-contiguous input array") + arr_in = np.ascontiguousarray(arr_in) new_shape = tuple((arr_shape - window_shape) // step + 1) + \ diff --git a/skimage/util/tests/test_shape.py b/skimage/util/tests/test_shape.py index b66ac2e2..79fb844b 100644 --- a/skimage/util/tests/test_shape.py +++ b/skimage/util/tests/test_shape.py @@ -1,6 +1,6 @@ import numpy as np from nose.tools import raises -from numpy.testing import assert_equal +from numpy.testing import assert_equal, assert_raises from skimage.util.shape import view_as_blocks, view_as_windows @@ -147,5 +147,13 @@ def test_view_as_windows_With_skip(): assert_equal(C.shape, (1, 1, 2, 2)) +def test_views_non_contiguous(): + A = np.arange(16).reshape((4, 4)) + A = A[::2, :] + + assert_raises(ValueError, view_as_blocks, A, (2, 2)) + assert_raises(ValueError, view_as_windows, A, (2, 2)) + + if __name__ == '__main__': np.testing.run_module_suite()