From b40ef3ad7d461a9728522191f35ab3a08c608f0b Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Tue, 6 Aug 2013 22:45:52 +1000 Subject: [PATCH] Ensure array input to unique_rows is contiguous --- skimage/util/unique.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/skimage/util/unique.py b/skimage/util/unique.py index a09fadd9..6034d9a4 100644 --- a/skimage/util/unique.py +++ b/skimage/util/unique.py @@ -30,6 +30,11 @@ def unique_rows(ar): if ar.ndim != 2: raise ValueError("unique_rows() only makes sense for 2D arrays, " "got %dd" % ar.ndim) + # the view in the next line only works if the array is C-contiguous + ar = np.ascontiguousarray(ar) + # np.unique() finds identical items in a raveled array. To make it + # see each row as a single item, we create a view of each row as a + # byte string of length itemsize times number of columns in `ar` ar_row_view = ar.view('|S%d' % (ar.itemsize * ar.shape[1])) _, unique_row_indices = np.unique(ar_row_view, return_index=True) ar_out = ar[unique_row_indices]