-
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: Implement one_hot as a wrapped gufunc.
- Loading branch information
1 parent
31b56eb
commit c93470b
Showing
8 changed files
with
161 additions
and
12 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,34 @@ | ||
|
||
from ufunc_config_types import UFuncExtMod, UFunc, UFuncSource | ||
|
||
|
||
ONE_HOT_DOCSTRING = """\ | ||
one_hot(k, /, ...) | ||
Fill the 1-d output with zeros except at k, where the output is 1. | ||
The `out` parameter of this ufunc must be given. The length | ||
of `out` determines n. | ||
""" | ||
|
||
one_hot_src = UFuncSource( | ||
funcname='one_hot_core_calc', | ||
typesignatures=[ | ||
'p->p', | ||
] | ||
) | ||
|
||
one_hot = UFunc( | ||
name='one_hot', | ||
header='one_hot_gufunc.h', | ||
docstring=ONE_HOT_DOCSTRING, | ||
signature='()->(n)', | ||
sources=[one_hot_src], | ||
) | ||
|
||
|
||
extmod = UFuncExtMod( | ||
module='_one_hot', | ||
docstring=("This extension module defines the gufunc 'one_hot'."), | ||
ufuncs=[one_hot], | ||
) |
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,43 @@ | ||
|
||
#ifndef ONE_HOT_GUFUNC_H | ||
#define ONE_HOT_GUFUNC_H | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include "Python.h" | ||
|
||
#include <cmath> | ||
#include <cstdio> | ||
|
||
#define NPY_NO_DEPRECATED_API NPY_API_VERSION | ||
#include "numpy/ndarraytypes.h" | ||
|
||
#include "../src/util/strided.hpp" | ||
|
||
|
||
// | ||
// `one_hot_core_calc` is the C++ core function | ||
// for the gufunc `one_hot` with signature '()->(n)' | ||
// | ||
template<typename T> | ||
static void | ||
one_hot_core_calc( | ||
npy_intp n, // core dimension n | ||
T *p_x, // pointer to x | ||
T *p_out, // pointer to out, a strided 1-d array | ||
npy_intp out_stride | ||
) | ||
{ | ||
if (out_stride == sizeof(T)) { | ||
memset(p_out, 0, n*sizeof(T)); | ||
} | ||
else { | ||
for (npy_intp i = 0; i < n; ++i) { | ||
set(p_out, out_stride, i, static_cast<npy_intp>(0)); | ||
} | ||
} | ||
if (0 <= *p_x && *p_x < n) { | ||
set(p_out, out_stride, *p_x, static_cast<npy_intp>(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 |
---|---|---|
|
@@ -35,6 +35,8 @@ | |
'M': 'NPY_DATETIME', | ||
'm': 'NPY_TIMEDELTA', | ||
'O': 'NPY_OBJECT', | ||
'p': 'NPY_INTP', | ||
'P': 'NPY_UINTP' | ||
} | ||
|
||
|
||
|
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,15 @@ | ||
import numpy as np | ||
from numpy.testing import assert_equal | ||
from ufunclab import one_hot | ||
|
||
|
||
def test_basic_scalar(): | ||
a = one_hot(3, 8) | ||
assert_equal(a, np.array([0, 0, 0, 1, 0, 0, 0, 0])) | ||
|
||
|
||
def test_basic_1d_k(): | ||
a = one_hot([3, 5, 6], 8) | ||
assert_equal(a, np.array([[0, 0, 0, 1, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 1, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 1, 0]])) |