13#include <boost/python/class.hpp>
14#include <boost/python/def.hpp>
15#include <boost/python/overloads.hpp>
16#include <boost/python/return_value_policy.hpp>
17#include <boost/python/scope.hpp>
21#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
22#define NO_IMPORT_ARRAY
23#include <numpy/arrayobject.h>
44bool isFloatArray(PyObject *
obj) {
45#if NPY_API_VERSION >= 0x00000007
60bool typesEqual(PyObject *first, PyObject *second) {
61#if NPY_API_VERSION >= 0x00000007
62 const auto *firstArray =
reinterpret_cast<const PyArrayObject *
>(first);
63 const auto *secondArray =
reinterpret_cast<const PyArrayObject *
>(second);
68 return PyArray_TYPE(firstArray) != PyArray_TYPE(secondArray);
72class UnknownDataType :
public std::invalid_argument {
75 : std::invalid_argument(
"Unknown datatype. Currently only arrays of "
76 "Python floats are supported ") {}
93 if (isFloatArray(data.ptr())) {
99 throw UnknownDataType();
110BOOST_PYTHON_FUNCTION_OVERLOADS(getStatisticsOverloads, getStatisticsNumpy, 1, 2)
121std::vector<double> getZscoreNumpy(
const NDArray &data) {
125 if (isFloatArray(data.ptr())) {
126 return getZscore(NDArrayToVector<double>(data)());
128 throw UnknownDataType();
137std::vector<double> getZscoreNumpyDeprecated(
const NDArray &data,
const bool sorted) {
139 PyErr_Warn(PyExc_DeprecationWarning,
"getZScore no longer requires the second sorted argument.");
140 return getZscoreNumpy(data);
147std::vector<double> getModifiedZscoreNumpy(
const NDArray &data,
const bool sorted =
false) {
152 if (isFloatArray(data.ptr())) {
155 throw UnknownDataType();
165BOOST_PYTHON_FUNCTION_OVERLOADS(getModifiedZscoreOverloads, getModifiedZscoreNumpy, 1, 2)
173using MomentsFunction = std::vector<double> (*)(
const std::vector<double> &,
const std::vector<double> &,
const int);
186std::vector<double> getMomentsNumpyImpl(MomentsFunction momentsFunc,
const NDArray &indep,
const NDArray &depend,
187 const int maxMoment) {
191 if (typesEqual(indep.ptr(), depend.ptr())) {
192 throw std::invalid_argument(
"Datatypes of input arrays must match.");
195 if (isFloatArray(indep.ptr()) && isFloatArray(indep.ptr())) {
196 return momentsFunc(NDArrayToVector<double>(indep)(), NDArrayToVector<double>(depend)(), maxMoment);
198 throw UnknownDataType();
206std::vector<double> getMomentsAboutOriginNumpy(
const NDArray &indep,
const NDArray &depend,
const int maxMoment = 3) {
216BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutOriginOverloads, getMomentsAboutOriginNumpy, 2, 3)
223std::vector<double> getMomentsAboutMeanNumpy(
const NDArray &indep,
NDArray &depend,
const int maxMoment = 3) {
233BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutMeanOverloads, getMomentsAboutMeanNumpy, 2, 3)
245 using ReturnNumpyArray = return_value_policy<Policies::VectorToNumpy>;
251 class_<Stats>(
"Stats", no_init)
252 .def(
"getStatistics", &getStatisticsNumpy,
253 getStatisticsOverloads((arg(
"data"), arg(
"sorted")),
"Determine the statistics for an array of data"))
254 .staticmethod(
"getStatistics")
256 .def(
"getZscore", &getZscoreNumpy, arg(
"data"),
"Determine the Z score for an array of data")
257 .def(
"getZscore", &getZscoreNumpyDeprecated, (arg(
"data"), arg(
"sorted")),
258 "Determine the Z score for an array of "
259 "data (deprecated + ignored sorted argument)")
260 .staticmethod(
"getZscore")
262 .def(
"getModifiedZscore", &getModifiedZscoreNumpy,
263 getModifiedZscoreOverloads((arg(
"data"), arg(
"sorted")),
264 "Determine the modified Z score for an array of data"))
265 .staticmethod(
"getModifiedZscore")
267 .def(
"getMomentsAboutOrigin", &getMomentsAboutOriginNumpy,
268 getMomentsAboutOriginOverloads(
269 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
270 "Calculate the first n-moments (inclusive) about the origin")[ReturnNumpyArray()])
271 .staticmethod(
"getMomentsAboutOrigin")
273 .def(
"getMomentsAboutMean", &getMomentsAboutMeanNumpy,
274 getMomentsAboutMeanOverloads(
275 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
276 "Calculate the first n-moments (inclusive) about the mean")[ReturnNumpyArray()])
277 .staticmethod(
"getMomentsAboutMean");
280 class_<Statistics>(
"Statistics")
283 .add_property(
"mean", &
Statistics::mean,
"Simple mean, sum(data)/nvalues, of the data set")
tagPyArrayObject PyArrayObject
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
double obj
the value of the quadratic function
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Thin object wrapper around a numpy array.
std::vector< double > getModifiedZscore(const std::vector< TYPE > &data)
Return the modified Z score values for a dataset.
Statistics getStatistics(const std::vector< TYPE > &data, const unsigned int flags=StatOptions::AllStats)
Return a statistics object for the given data set.
std::vector< double > getZscore(const std::vector< TYPE > &data)
Return the Z score values for a dataset.
std::vector< double > getMomentsAboutMean(const std::vector< TYPE > &x, const std::vector< TYPE > &y, const int maxMoment=3)
Return the first n-moments of the supplied data.
std::vector< double > getMomentsAboutOrigin(const std::vector< TYPE > &x, const std::vector< TYPE > &y, const int maxMoment=3)
Return the first n-moments of the supplied data.
Controls the computation of statisical data.
Simple struct to store statistics.
double median
Median value.
double minimum
Minimum value.
double maximum
Maximum value.
double standard_deviation
standard_deviation of the values
Converter taking an input numpy array and converting it to a std::vector.