From 12af2ef7ed0a9b17ed5200353667fe624ae81bf3 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Mon, 7 Apr 2014 18:15:49 +0200 Subject: [PATCH] Take a softer approach and raise a warning when a non-contiguous input is provided --- doc/source/api_changes.txt | 3 --- skimage/util/shape.py | 20 ++++++++++++-------- skimage/util/tests/test_shape.py | 7 ++++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/doc/source/api_changes.txt b/doc/source/api_changes.txt index 14a66975..527c9fb0 100644 --- a/doc/source/api_changes.txt +++ b/doc/source/api_changes.txt @@ -1,9 +1,6 @@ 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 d61a647e..d47150af 100644 --- a/skimage/util/shape.py +++ b/skimage/util/shape.py @@ -2,6 +2,7 @@ __all__ = ['view_as_blocks', 'view_as_windows'] import numpy as np from numpy.lib.stride_tricks import as_strided +from warnings import warn def view_as_blocks(arr_in, block_shape): @@ -12,8 +13,7 @@ def view_as_blocks(arr_in, block_shape): Parameters ---------- arr_in : ndarray - Contiguous N-d input array. - + N-d input array. block_shape : tuple The shape of the block. Each dimension must divide evenly into the corresponding dimensions of `arr_in`. @@ -21,7 +21,8 @@ def view_as_blocks(arr_in, block_shape): Returns ------- arr_out : ndarray - Block view of the input array. + Block view of the input array. If `arr_in` is non-contiguous, a copy + is made. Examples -------- @@ -86,8 +87,10 @@ 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") + warn(RuntimeWarning("Cannot provide views on a non-contiguous input " + "array without copying.")) arr_in = np.ascontiguousarray(arr_in) @@ -107,9 +110,9 @@ def view_as_windows(arr_in, window_shape, step=1): Parameters ---------- - arr_in: ndarray + arr_in : ndarray Contiguous N-d input array. - window_shape: tuple + window_shape : tuple Defines the shape of the elementary n-dimensional orthotope (better know as hyperrectangle [1]_) of the rolling window view. step : int, optional @@ -119,7 +122,7 @@ def view_as_windows(arr_in, window_shape, step=1): Returns ------- - arr_out: ndarray + arr_out : ndarray (rolling) window view of the input array. Notes @@ -230,7 +233,8 @@ def view_as_windows(arr_in, window_shape, step=1): # -- build rolling window view if not arr_in.flags.contiguous: - raise ValueError("Cannot provide views on a non-contiguous input array") + warn(RuntimeWarning("Cannot provide views on a non-contiguous input " + "array without copying.")) arr_in = np.ascontiguousarray(arr_in) diff --git a/skimage/util/tests/test_shape.py b/skimage/util/tests/test_shape.py index 79fb844b..7a5640a3 100644 --- a/skimage/util/tests/test_shape.py +++ b/skimage/util/tests/test_shape.py @@ -1,6 +1,7 @@ import numpy as np from nose.tools import raises -from numpy.testing import assert_equal, assert_raises +from numpy.testing import assert_equal, assert_warns + from skimage.util.shape import view_as_blocks, view_as_windows @@ -151,8 +152,8 @@ 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)) + assert_warns(RuntimeWarning, view_as_blocks, A, (2, 2)) + assert_warns(RuntimeWarning, view_as_windows, A, (2, 2)) if __name__ == '__main__':