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();
109BOOST_PYTHON_FUNCTION_OVERLOADS(getStatisticsOverloads, getStatisticsNumpy, 1, 2)
120std::vector<double> getZscoreNumpy(
const NDArray &data) {
124 if (isFloatArray(data.ptr())) {
125 return getZscore(NDArrayToVector<double>(data)());
127 throw UnknownDataType();
136std::vector<double> getZscoreNumpyDeprecated(
const NDArray &data,
const bool sorted) {
138 PyErr_Warn(PyExc_DeprecationWarning,
"getZScore no longer requires the second sorted argument.");
139 return getZscoreNumpy(data);
146std::vector<double> getModifiedZscoreNumpy(
const NDArray &data,
const bool sorted =
false) {
151 if (isFloatArray(data.ptr())) {
154 throw UnknownDataType();
164BOOST_PYTHON_FUNCTION_OVERLOADS(getModifiedZscoreOverloads, getModifiedZscoreNumpy, 1, 2)
172using MomentsFunction = std::vector<double> (*)(
const std::vector<double> &,
const std::vector<double> &,
const int);
185std::vector<double> getMomentsNumpyImpl(MomentsFunction momentsFunc,
const NDArray &indep,
const NDArray &depend,
186 const int maxMoment) {
190 if (typesEqual(indep.ptr(), depend.ptr())) {
191 throw std::invalid_argument(
"Datatypes of input arrays must match.");
194 if (isFloatArray(indep.ptr()) && isFloatArray(indep.ptr())) {
195 return momentsFunc(NDArrayToVector<double>(indep)(), NDArrayToVector<double>(depend)(), maxMoment);
197 throw UnknownDataType();
205std::vector<double> getMomentsAboutOriginNumpy(
const NDArray &indep,
const NDArray &depend,
const int maxMoment = 3) {
215BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutOriginOverloads, getMomentsAboutOriginNumpy, 2, 3)
222std::vector<double> getMomentsAboutMeanNumpy(
const NDArray &indep,
NDArray &depend,
const int maxMoment = 3) {
232BOOST_PYTHON_FUNCTION_OVERLOADS(getMomentsAboutMeanOverloads, getMomentsAboutMeanNumpy, 2, 3)
244 using ReturnNumpyArray = return_value_policy<Policies::VectorToNumpy>;
250 class_<Stats>(
"Stats", no_init)
251 .def(
"getStatistics", &getStatisticsNumpy,
252 getStatisticsOverloads((arg(
"data"), arg(
"sorted")),
"Determine the statistics for an array of data"))
253 .staticmethod(
"getStatistics")
255 .def(
"getZscore", &getZscoreNumpy, arg(
"data"),
"Determine the Z score for an array of data")
256 .def(
"getZscore", &getZscoreNumpyDeprecated, (arg(
"data"), arg(
"sorted")),
257 "Determine the Z score for an array of "
258 "data (deprecated + ignored sorted argument)")
259 .staticmethod(
"getZscore")
261 .def(
"getModifiedZscore", &getModifiedZscoreNumpy,
262 getModifiedZscoreOverloads((arg(
"data"), arg(
"sorted")),
263 "Determine the modified Z score for an array of data"))
264 .staticmethod(
"getModifiedZscore")
266 .def(
"getMomentsAboutOrigin", &getMomentsAboutOriginNumpy,
267 getMomentsAboutOriginOverloads(
268 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
269 "Calculate the first n-moments (inclusive) about the origin")[ReturnNumpyArray()])
270 .staticmethod(
"getMomentsAboutOrigin")
272 .def(
"getMomentsAboutMean", &getMomentsAboutMeanNumpy,
273 getMomentsAboutMeanOverloads(
274 (arg(
"indep"), arg(
"depend"), arg(
"maxMoment")),
275 "Calculate the first n-moments (inclusive) about the mean")[ReturnNumpyArray()])
276 .staticmethod(
"getMomentsAboutMean");
279 class_<Statistics>(
"Statistics")
282 .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.