-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add bincount, like np.bincount but accepts an axis parameter.
- Loading branch information
1 parent
590561b
commit 57052ed
Showing
7 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#ifndef BINCOUNT_GUFUNC_H | ||
#define BINCOUNT_GUFUNC_H | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include "Python.h" | ||
|
||
#define NPY_NO_DEPRECATED_API NPY_API_VERSION | ||
#include "numpy/ndarraytypes.h" | ||
|
||
#include "../src/util/strided.hpp" | ||
|
||
|
||
// | ||
// `bincount_core_calc` is the C++ core function | ||
// for the gufunc `bincount` with signature '(n)->(m)' | ||
// | ||
template<typename T, typename U> | ||
static void | ||
bincount_core_calc( | ||
npy_intp n, // core dimension n | ||
npy_intp m, // core dimension m | ||
T *p_x, // pointer to x | ||
npy_intp x_stride, | ||
U *p_out, // pointer to out, a strided 1-d array | ||
npy_intp out_stride | ||
) | ||
{ | ||
for (npy_intp i = 0; i < m; ++i) { | ||
set(p_out, out_stride, i, static_cast<U>(0)); | ||
} | ||
for (npy_intp i = 0; i < n; ++i) { | ||
T k = get(p_x, x_stride, i); | ||
if (k >= 0 && k < m) { | ||
set(p_out, out_stride, k, get(p_out, out_stride, k) + 1); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
from ufunc_config_types import UFuncExtMod, UFunc, UFuncSource | ||
|
||
|
||
BINCOUNT_DOCSTRING = """\ | ||
bincount(x, /, ...) | ||
Count integer in `x`, a 1-d array of length n. | ||
The `out` array *must* be given, and it must be a 1-d. | ||
""" | ||
|
||
bincount_src = UFuncSource( | ||
funcname='bincount_core_calc', | ||
typesignatures=[ | ||
'b->P', | ||
'B->P', | ||
'h->P', | ||
'H->P', | ||
'i->P', | ||
'I->P', | ||
'l->P', | ||
'L->P', | ||
'q->P', | ||
'Q->P', | ||
'p->P', | ||
'P->P', | ||
] | ||
) | ||
|
||
bincount = UFunc( | ||
name='bincount', | ||
header='bincount_gufunc.h', | ||
docstring=BINCOUNT_DOCSTRING, | ||
signature='(n)->(m)', | ||
sources=[bincount_src], | ||
) | ||
|
||
extmod = UFuncExtMod( | ||
module='_bincount', | ||
docstring=("This extension module defines the gufunc 'bincount'."), | ||
ufuncs=[bincount], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
import numpy as np | ||
from numpy.testing import assert_equal | ||
from ufunclab import bincount | ||
|
||
|
||
def test_1d_default_m(): | ||
x = np.array([0, 0, 3, 4, 3, 4, 0, 3, 0]) | ||
y = bincount(x) | ||
assert_equal(y, [4, 0, 0, 3, 2]) | ||
|
||
|
||
def test_1d_given_m(): | ||
x = np.array([0, 0, 3, 4, 3, 4, 0, 3, 0]) | ||
y = bincount(x, 8) | ||
assert_equal(y, [4, 0, 0, 3, 2, 0, 0, 0]) | ||
|
||
|
||
@pytest.mark.parametrize('m', [None, 2, 5, 8]) | ||
def test_nd_default_m(m): | ||
x = np.array([[0, 4, 4, 3, 2, 1], | ||
[1, 1, 2, 2, 3, 3], | ||
[4, 4, 4, 4, 4, 2]]) | ||
ref = np.array([[1, 1, 1, 1, 2], | ||
[0, 2, 2, 2, 0], | ||
[0, 0, 1, 0, 5]]) | ||
refm = max(np.max(x) + 1, 0) | ||
y = bincount(x, m=m) | ||
if (m is None): | ||
expected = ref | ||
elif m <= refm: | ||
expected = ref[:, :m] | ||
else: | ||
expected = np.pad(ref, [(0, 0), (0, m - refm)]) | ||
assert_equal(y, expected) | ||
|
||
|
||
@pytest.mark.parametrize('m', [None, 2, 5, 8]) | ||
def test_axis(m): | ||
x = np.array([[0, 4, 4], | ||
[1, 1, 2], | ||
[4, 4, 4], | ||
[0, 0, 2]]) | ||
ref = np.array([[2, 1, 0], | ||
[1, 1, 0], | ||
[0, 0, 2], | ||
[0, 0, 0], | ||
[1, 2, 2]]) | ||
refm = max(np.max(x) + 1, 0) | ||
y = bincount(x, m=m, axis=0) | ||
if (m is None): | ||
expected = ref | ||
elif m <= refm: | ||
expected = ref[:m, :] | ||
else: | ||
expected = np.pad(ref, [(0, m - refm), (0, 0)]) | ||
assert_equal(y, expected) |