Ensure array input to unique_rows is contiguous

This commit is contained in:
Juan Nunez-Iglesias
2013-08-06 22:45:52 +10:00
parent 909d4cb5f6
commit b40ef3ad7d
+5
View File
@@ -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]