From 3c05221dd443323e7948eadf8105c0b52e760f70 Mon Sep 17 00:00:00 2001 From: Claudio Freire Date: Wed, 31 Jan 2018 00:50:57 -0800 Subject: MAINT: Remove messy handling of output tuple in np.unique Largely taken from gh-9531 --- numpy/lib/arraysetops.py | 64 +++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index b1e74dc74..78d4536c0 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -110,6 +110,14 @@ def ediff1d(ary, to_end=None, to_begin=None): return result +def _unpack_tuple(x): + """ Unpacks one-element tuples for use as return values """ + if len(x) == 1: + return x[0] + else: + return x + + def unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None): """ @@ -211,7 +219,9 @@ def unique(ar, return_index=False, return_inverse=False, """ ar = np.asanyarray(ar) if axis is None: - return _unique1d(ar, return_index, return_inverse, return_counts) + ret = _unique1d(ar, return_index, return_inverse, return_counts) + return _unpack_tuple(ret) + if not (-ar.ndim <= axis < ar.ndim): raise ValueError('Invalid axis kwarg specified for unique') @@ -245,11 +255,9 @@ def unique(ar, return_index=False, return_inverse=False, output = _unique1d(consolidated, return_index, return_inverse, return_counts) - if not (return_index or return_inverse or return_counts): - return reshape_uniq(output) - else: - uniq = reshape_uniq(output[0]) - return (uniq,) + output[1:] + output = (reshape_uniq(output[0]),) + output[1:] + return _unpack_tuple(output) + def _unique1d(ar, return_index=False, return_inverse=False, return_counts=False): @@ -259,19 +267,15 @@ def _unique1d(ar, return_index=False, return_inverse=False, ar = np.asanyarray(ar).flatten() optional_indices = return_index or return_inverse - optional_returns = optional_indices or return_counts if ar.size == 0: - if not optional_returns: - ret = ar - else: - ret = (ar,) - if return_index: - ret += (np.empty(0, np.intp),) - if return_inverse: - ret += (np.empty(0, np.intp),) - if return_counts: - ret += (np.empty(0, np.intp),) + ret = (ar,) + if return_index: + ret += (np.empty(0, np.intp),) + if return_inverse: + ret += (np.empty(0, np.intp),) + if return_counts: + ret += (np.empty(0, np.intp),) return ret if optional_indices: @@ -282,22 +286,20 @@ def _unique1d(ar, return_index=False, return_inverse=False, aux = ar flag = np.concatenate(([True], aux[1:] != aux[:-1])) - if not optional_returns: - ret = aux[flag] - else: - ret = (aux[flag],) - if return_index: - ret += (perm[flag],) - if return_inverse: - iflag = np.cumsum(flag) - 1 - inv_idx = np.empty(ar.shape, dtype=np.intp) - inv_idx[perm] = iflag - ret += (inv_idx,) - if return_counts: - idx = np.concatenate(np.nonzero(flag) + ([ar.size],)) - ret += (np.diff(idx),) + ret = (aux[flag],) + if return_index: + ret += (perm[flag],) + if return_inverse: + iflag = np.cumsum(flag) - 1 + inv_idx = np.empty(ar.shape, dtype=np.intp) + inv_idx[perm] = iflag + ret += (inv_idx,) + if return_counts: + idx = np.concatenate(np.nonzero(flag) + ([ar.size],)) + ret += (np.diff(idx),) return ret + def intersect1d(ar1, ar2, assume_unique=False): """ Find the intersection of two arrays. -- cgit v1.2.1 From 2b417df83202df9ea67f1eec76985a3da20cb86c Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 31 Jan 2018 00:56:36 -0800 Subject: MAINT: Remove special-casing of empty arrays in unique_1d --- numpy/lib/arraysetops.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 78d4536c0..e1c1c8803 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -268,34 +268,26 @@ def _unique1d(ar, return_index=False, return_inverse=False, optional_indices = return_index or return_inverse - if ar.size == 0: - ret = (ar,) - if return_index: - ret += (np.empty(0, np.intp),) - if return_inverse: - ret += (np.empty(0, np.intp),) - if return_counts: - ret += (np.empty(0, np.intp),) - return ret - if optional_indices: perm = ar.argsort(kind='mergesort' if return_index else 'quicksort') aux = ar[perm] else: ar.sort() aux = ar - flag = np.concatenate(([True], aux[1:] != aux[:-1])) + mask = np.empty(aux.shape, dtype=np.bool_) + mask[:1] = True + mask[1:] = aux[1:] != aux[:-1] - ret = (aux[flag],) + ret = (aux[mask],) if return_index: - ret += (perm[flag],) + ret += (perm[mask],) if return_inverse: - iflag = np.cumsum(flag) - 1 + imask = np.cumsum(mask) - 1 inv_idx = np.empty(ar.shape, dtype=np.intp) - inv_idx[perm] = iflag + inv_idx[perm] = imask ret += (inv_idx,) if return_counts: - idx = np.concatenate(np.nonzero(flag) + ([ar.size],)) + idx = np.concatenate(np.nonzero(mask) + ([ar.size],)) ret += (np.diff(idx),) return ret -- cgit v1.2.1 From 41872c1085b707a6020b0058dc063d2981520bc6 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 31 Jan 2018 01:12:06 -0800 Subject: DOC: Use a bulleted list to show the outputs of `unique`, for clarity --- numpy/lib/arraysetops.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index e1c1c8803..1ac1f3f03 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -124,10 +124,11 @@ def unique(ar, return_index=False, return_inverse=False, Find the unique elements of an array. Returns the sorted unique elements of an array. There are three optional - outputs in addition to the unique elements: the indices of the input array - that give the unique values, the indices of the unique array that - reconstruct the input array, and the number of times each unique value - comes up in the input array. + outputs in addition to the unique elements: + + * the indices of the input array that give the unique values + * the indices of the unique array that reconstruct the input array + * the number of times each unique value comes up in the input array Parameters ---------- -- cgit v1.2.1 From f80e20cfcf0728e068933cd5de9575db8ac3f803 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 31 Jan 2018 01:12:53 -0800 Subject: MAINT: Make it clear that counts and inverse depend only on the mask --- numpy/lib/arraysetops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 1ac1f3f03..e6ff5bf38 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -284,11 +284,11 @@ def _unique1d(ar, return_index=False, return_inverse=False, ret += (perm[mask],) if return_inverse: imask = np.cumsum(mask) - 1 - inv_idx = np.empty(ar.shape, dtype=np.intp) + inv_idx = np.empty(mask.shape, dtype=np.intp) inv_idx[perm] = imask ret += (inv_idx,) if return_counts: - idx = np.concatenate(np.nonzero(mask) + ([ar.size],)) + idx = np.concatenate(np.nonzero(mask) + ([mask.size],)) ret += (np.diff(idx),) return ret -- cgit v1.2.1