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 if (PyErr_Warn(PyExc_DeprecationWarning,
"getZScore no longer requires the second sorted argument.") < 0)
140 throw boost::python::error_already_set();
141 return getZscoreNumpy(data);
148std::vector<double> getModifiedZscoreNumpy(
const NDArray &data,
const bool sorted =
false) {
153 if (isFloatArray(data.ptr())) {
156 throw UnknownDataType();
166BOOST_PYTHON_FUNCTION_OVERLOADS(getModifiedZscoreOverloads, getModifiedZscoreNumpy, 1, 2)
174using MomentsFunction = std::vector<double> (*)(
const std::vector<double> &,
const std::vector<double> &,
const int);
187std::vector<double> getMomentsNumpyImpl(MomentsFunction momentsFunc,
const NDArray &indep,
const NDArray &depend,
188 const int maxMoment) {
192 if (typesEqual(indep.ptr(), depend.ptr())) {
193 throw std::invalid_argument(
"Datatypes of input arrays must match.");
196 if (isFloatArray(indep.ptr()) && isFloatArray(indep.ptr())) {
197 return momentsFunc(NDArrayToVector<double>(indep)(), NDArrayToVector<double>(depend)(), maxMoment);
199 throw UnknownDataType();
207std::vector<double> getMomentsAboutOriginNumpy(
const NDArray &indep,
const NDArray &depend,
const int maxMoment = 3) {
217BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutOriginOverloads, getMomentsAboutOriginNumpy, 2, 3)
224std::vector<double> getMomentsAboutMeanNumpy(
const NDArray &indep,
NDArray &depend,
const int maxMoment = 3) {
234BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutMeanOverloads, getMomentsAboutMeanNumpy, 2, 3)
246 using ReturnNumpyArray = return_value_policy<Policies::VectorToNumpy>;
252 class_<Stats>(
"Stats", no_init)
253 .def(
"getStatistics", &getStatisticsNumpy,
254 getStatisticsOverloads((arg(
"data"), arg(
"sorted")),
"Determine the statistics for an array of data"))
255 .staticmethod(
"getStatistics")
257 .def(
"getZscore", &getZscoreNumpy, arg(
"data"),
"Determine the Z score for an array of data")
258 .def(
"getZscore", &getZscoreNumpyDeprecated, (arg(
"data"), arg(
"sorted")),
259 "Determine the Z score for an array of "
260 "data (deprecated + ignored sorted argument)")
261 .staticmethod(
"getZscore")
263 .def(
"getModifiedZscore", &getModifiedZscoreNumpy,
264 getModifiedZscoreOverloads((arg(
"data"), arg(
"sorted")),
265 "Determine the modified Z score for an array of data"))
266 .staticmethod(
"getModifiedZscore")
268 .def(
"getMomentsAboutOrigin", &getMomentsAboutOriginNumpy,
269 getMomentsAboutOriginOverloads(
270 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
271 "Calculate the first n-moments (inclusive) about the origin")[ReturnNumpyArray()])
272 .staticmethod(
"getMomentsAboutOrigin")
274 .def(
"getMomentsAboutMean", &getMomentsAboutMeanNumpy,
275 getMomentsAboutMeanOverloads(
276 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
277 "Calculate the first n-moments (inclusive) about the mean")[ReturnNumpyArray()])
278 .staticmethod(
"getMomentsAboutMean");
281 class_<Statistics>(
"Statistics")
284 .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.