From f9b110c025bbceb6f52ad9294bc1a6cdf4209141 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 29 Dec 2021 17:16:02 -0700 Subject: [PATCH 1/3] Switch from ravel_multi_index to a faster vectorized alternative. Now that we have tests, we can be slightly confident that this works. --- numpy_groupies/utils_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_groupies/utils_numpy.py b/numpy_groupies/utils_numpy.py index 41ec2c9..2683db9 100644 --- a/numpy_groupies/utils_numpy.py +++ b/numpy_groupies/utils_numpy.py @@ -236,7 +236,7 @@ def offset_labels(group_idx, inshape, axis, order, size): def input_validation(group_idx, a, size=None, order='C', axis=None, - ravel_group_idx=True, check_bounds=True, method="ravel", func=None): + ravel_group_idx=True, check_bounds=True, method="offset", func=None): """ Do some fairly extensive checking of group_idx and a, trying to give the user as much help as possible with what is wrong. Also, convert ndim-indexing to 1d indexing. From 542ed4203256848d008d93a1d74bea16969475a9 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 29 Dec 2021 18:20:46 -0700 Subject: [PATCH 2/3] bugfix. --- numpy_groupies/utils_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_groupies/utils_numpy.py b/numpy_groupies/utils_numpy.py index 2683db9..ee8a851 100644 --- a/numpy_groupies/utils_numpy.py +++ b/numpy_groupies/utils_numpy.py @@ -223,7 +223,7 @@ def offset_labels(group_idx, inshape, axis, order, size): https://stackoverflow.com/questions/46256279/bin-elements-per-row-vectorized-2d-bincount-for-numpy """ if axis not in (-1, len(inshape) - 1): - newshape = (s for idx, s in enumerate(inshape) if idx != axis) + (inshape[axis],) + newshape = tuple(s for idx, s in enumerate(inshape) if idx != axis) + (inshape[axis],) else: newshape = inshape group_idx = np.broadcast_to(group_idx, newshape) From 0bd7249915d656069aa60cb3f5f5724d38cbf9f9 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 25 Apr 2022 21:32:24 -0600 Subject: [PATCH 3/3] bugfix --- numpy_groupies/utils_numpy.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/numpy_groupies/utils_numpy.py b/numpy_groupies/utils_numpy.py index ee8a851..545829a 100644 --- a/numpy_groupies/utils_numpy.py +++ b/numpy_groupies/utils_numpy.py @@ -222,17 +222,21 @@ def offset_labels(group_idx, inshape, axis, order, size): Copied from https://stackoverflow.com/questions/46256279/bin-elements-per-row-vectorized-2d-bincount-for-numpy """ + + newaxes = tuple(ax for ax in range(len(inshape)) if ax != axis) + group_idx = np.broadcast_to(np.expand_dims(group_idx, newaxes), inshape) + if axis not in (-1, len(inshape) - 1): + group_idx = np.moveaxis(group_idx, axis, -1) + newshape = group_idx.shape + + group_idx = (group_idx + + np.arange(np.prod(newshape[:-1]), dtype=int).reshape((*newshape[:-1], -1)) + * size + ) if axis not in (-1, len(inshape) - 1): - newshape = tuple(s for idx, s in enumerate(inshape) if idx != axis) + (inshape[axis],) + return np.moveaxis(group_idx, -1, axis) else: - newshape = inshape - group_idx = np.broadcast_to(group_idx, newshape) - group_idx: np.ndarray = ( - group_idx - + np.arange(np.prod(group_idx.shape[:-1]), dtype=int).reshape((*group_idx.shape[:-1], -1)) - * size - ) - return group_idx.reshape(inshape).ravel() + return group_idx def input_validation(group_idx, a, size=None, order='C', axis=None,