-
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 the percentileofscore gufunc.
- Loading branch information
1 parent
e5f59de
commit 6731c9d
Showing
5 changed files
with
243 additions
and
24 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,41 @@ | ||
import numpy as np | ||
from ufunc_config_types import UFuncExtMod, UFunc, UFuncSource | ||
|
||
|
||
PERCENTILEOFSCORE_DOCSTRING = """\ | ||
percentileofscore(x, score, kind, /, ...) | ||
See `scipy.stats.percentileofscore`. | ||
""" | ||
|
||
input_types = [np.dtype('int8'), np.dtype('uint8'), | ||
np.dtype('int16'), np.dtype('uint16'), | ||
np.dtype('int32'), np.dtype('uint32'), | ||
np.dtype('int64'), np.dtype('uint64'), | ||
np.dtype('f'), np.dtype('d')] | ||
|
||
type_chars = [typ.char for typ in input_types] | ||
type_sigs = [f'{c}{c}p->d' for c in type_chars] | ||
|
||
percentileofscore_src = UFuncSource( | ||
funcname='percentileofscore_core_calc', | ||
typesignatures=type_sigs, | ||
) | ||
|
||
percentileofscore = UFunc( | ||
name='percentileofscore', | ||
header='percentileofscore_gufunc.h', | ||
docstring=PERCENTILEOFSCORE_DOCSTRING, | ||
signature='(n),(),()->()', | ||
sources=[percentileofscore_src], | ||
) | ||
|
||
|
||
extmod = UFuncExtMod( | ||
module='_percentileofscore', | ||
docstring=("This extension module defines the gufuncs 'percentileofscore'."), | ||
ufuncs=[percentileofscore], | ||
# The call `status = add_percentileofscore_constants(module);` will be | ||
# added to the end of the module init function. | ||
extra_module_funcs=['add_percentileofscore_kind_constants'] | ||
) |
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,110 @@ | ||
|
||
#ifndef PERCENTILEOFSCORE_GUFUNC_H | ||
#define PERCENTILEOFSOCRE_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" | ||
|
||
|
||
enum percentileofscore_kind: int { | ||
RANK = 0, | ||
WEAK, | ||
STRICT, | ||
MEAN, | ||
}; | ||
|
||
template<typename T> | ||
static inline void | ||
count(npy_intp n, | ||
const T *p_x, | ||
npy_intp stride, | ||
const T score, | ||
npy_intp *nless, | ||
npy_intp *nequal, | ||
npy_intp *ngreater) | ||
{ | ||
*nless = 0; | ||
*nequal = 0; | ||
*ngreater = 0; | ||
for (npy_intp k = 0; k < n; ++k) { | ||
T value = get(p_x, stride, k); | ||
if (value < score) { | ||
++*nless; | ||
} | ||
else if(value == score) { | ||
++*nequal; | ||
} | ||
else { | ||
++*ngreater; | ||
} | ||
} | ||
} | ||
|
||
// | ||
// `percentileofscore_core_calc`, the C++ core function | ||
// for the gufunc `percentileofscore` with signature '(n),(),()->()'. | ||
// | ||
template<typename T> | ||
static void | ||
percentileofscore_core_calc( | ||
npy_intp n, // core dimension n | ||
T *p_x, // pointer to first element of x, a strided 1-d array with n elements | ||
npy_intp x_stride, // stride (in bytes) for elements of x | ||
T *p_score, | ||
npy_intp *p_kind, | ||
double *p_out | ||
) | ||
{ | ||
npy_intp nless, nequal, ngreater, sum; | ||
|
||
count(n, p_x, x_stride, *p_score, &nless, &nequal, &ngreater); | ||
|
||
switch (static_cast<int>(*p_kind)) { | ||
case RANK: | ||
sum = 2*nless + nequal; | ||
if (nequal > 0) { | ||
++sum; | ||
} | ||
*p_out = sum*50.0/n; | ||
return; | ||
case WEAK: | ||
*p_out = (nless + nequal)*100.0/n; | ||
return; | ||
case STRICT: | ||
*p_out = nless*100.0/n; | ||
return; | ||
case MEAN: | ||
*p_out = (2*nless + nequal)*50.0/n; | ||
return; | ||
} | ||
} | ||
|
||
//-------------------------------------------------------- | ||
|
||
// | ||
// The name of this function is listed in the `extra_module_funcs` | ||
// attribute of the UFuncExtMod object that defines the gufuncs | ||
// `first` and `argfirst`. A call of this function will be added | ||
// to the end of the generated extension module. | ||
// | ||
int add_percentileofscore_kind_constants(PyObject *module) | ||
{ | ||
// Expose the numerical values RANK, WEAK, STRICT and MEAN as integers | ||
// in this module. | ||
const char *opnames[] = {"_RANK", "_WEAK", "_STRICT", "_MEAN"}; | ||
const int opcodes[] = {RANK, WEAK, STRICT, MEAN}; | ||
for (int k = 0; k < 4; ++k) { | ||
int status = PyModule_AddIntConstant(module, opnames[k], opcodes[k]); | ||
if (status == -1) { | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
#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
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