Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some optimizations for numpy aggregations #78

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions numpy_groupies/aggregate_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _sum(group_idx, a, size, fill_value, dtype=None):
dtype = minimum_dtype_scalar(fill_value, dtype, a)

if np.ndim(a) == 0:
ret = np.bincount(group_idx, minlength=size).astype(dtype)
ret = np.bincount(group_idx, minlength=size).astype(dtype, copy=False)
if a != 1:
ret *= a
else:
Expand All @@ -33,7 +33,9 @@ def _sum(group_idx, a, size, fill_value, dtype=None):
ret.real = np.bincount(group_idx, weights=a.real, minlength=size)
ret.imag = np.bincount(group_idx, weights=a.imag, minlength=size)
else:
ret = np.bincount(group_idx, weights=a, minlength=size).astype(dtype)
ret = np.bincount(group_idx, weights=a, minlength=size).astype(
dtype, copy=False
)

if fill_value != 0:
_fill_untouched(group_idx, ret, fill_value)
Expand Down Expand Up @@ -146,19 +148,19 @@ def _mean(group_idx, a, size, fill_value, dtype=np.dtype(np.float64)):
sums.real = np.bincount(group_idx, weights=a.real, minlength=size)
sums.imag = np.bincount(group_idx, weights=a.imag, minlength=size)
else:
sums = np.bincount(group_idx, weights=a, minlength=size).astype(dtype)
sums = np.bincount(group_idx, weights=a, minlength=size).astype(dtype, copy=False)

with np.errstate(divide="ignore", invalid="ignore"):
ret = sums.astype(dtype) / counts
ret = sums.astype(dtype, copy=False) / counts
if not np.isnan(fill_value):
ret[counts == 0] = fill_value
return ret


def _sum_of_squres(group_idx, a, size, fill_value, dtype=np.dtype(np.float64)):
ret = np.bincount(group_idx, weights=a * a, minlength=size)
counts = np.bincount(group_idx, minlength=size)
if fill_value != 0:
counts = np.bincount(group_idx, minlength=size)
ret[counts == 0] = fill_value
return ret

Expand All @@ -171,7 +173,7 @@ def _var(
counts = np.bincount(group_idx, minlength=size)
sums = np.bincount(group_idx, weights=a, minlength=size)
with np.errstate(divide="ignore", invalid="ignore"):
means = sums.astype(dtype) / counts
means = sums.astype(dtype, copy=False) / counts
counts = np.where(counts > ddof, counts - ddof, 0)
ret = (
np.bincount(group_idx, (a - means[group_idx]) ** 2, minlength=size) / counts
Expand Down Expand Up @@ -299,6 +301,7 @@ def _aggregate_base(
dtype=None,
axis=None,
_impl_dict=_impl_dict,
is_pandas=False,
**kwargs
):
iv = input_validation(group_idx, a, size=size, order=order, axis=axis, func=func)
Expand All @@ -324,7 +327,9 @@ def _aggregate_base(
kwargs["_nansqueeze"] = True
else:
good = ~np.isnan(a)
a = a[good]
if "len" not in func or is_pandas:
# a is not needed for len, nanlen!
a = a[good]
group_idx = group_idx[good]

dtype = check_dtype(dtype, func, a, flat_size)
Expand Down
1 change: 1 addition & 0 deletions numpy_groupies/aggregate_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def aggregate(
func=func,
axis=axis,
_impl_dict=_impl_dict,
is_pandas=True,
**kwargs
)

Expand Down