From baa00289bb766b013bada4040cc76db5d8f8e0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Morales?= Date: Fri, 14 Jun 2024 17:15:47 -0600 Subject: [PATCH] stack allocate GroupedArray (#35) --- include/c_api.h | 649 +++++++------ include/grouped_array.h | 16 +- include/seasonal.h | 4 + pyproject.toml | 2 +- python/coreforecast/__init__.py | 2 +- python/coreforecast/grouped_array.py | 193 +++- src/c_api.cpp | 1316 +++++++++++++------------- tests/test_scalers.py | 6 +- 8 files changed, 1161 insertions(+), 1027 deletions(-) diff --git a/include/c_api.h b/include/c_api.h index 4dbd2da..6927d16 100644 --- a/include/c_api.h +++ b/include/c_api.h @@ -92,179 +92,199 @@ DLL_EXPORT int Float32_Period(const float *x, size_t n, int period); // GA // Manipulation -DLL_EXPORT int GroupedArrayFloat32_Create(const float *data, indptr_t n_data, - indptr_t *indptr, indptr_t n_indptr, - int num_threads, - GroupedArrayHandle *out); -DLL_EXPORT int GroupedArrayFloat32_Delete(GroupedArrayHandle handle); +DLL_EXPORT void GroupedArrayFloat32_IndexFromEnd(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int k, float *out); -DLL_EXPORT int GroupedArrayFloat32_IndexFromEnd(GroupedArrayHandle handle, - int k, float *out); -DLL_EXPORT void GroupedArrayFloat32_Head(GroupedArrayHandle handle, int k, - float *out); -DLL_EXPORT void GroupedArrayFloat32_Tail(GroupedArrayHandle handle, int k, - float *out); -DLL_EXPORT void GroupedArrayFloat32_Tails(GroupedArrayHandle handle, +// Lag +DLL_EXPORT void GroupedArrayFloat32_LagTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, float *out); +DLL_EXPORT void GroupedArrayFloat32_IndexFromEnd(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int k, float *out); +DLL_EXPORT void GroupedArrayFloat32_Head(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int k, float *out); +DLL_EXPORT void GroupedArrayFloat32_Tail(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int k, float *out); +DLL_EXPORT void GroupedArrayFloat32_Append( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + const float *other_data, const indptr_t *other_indptr, int other_n_indptr, + const indptr_t *out_indptr, float *out_data); +DLL_EXPORT void GroupedArrayFloat32_Tails(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const indptr_t *indptr_out, float *out); -DLL_EXPORT void GroupedArrayFloat32_Append(GroupedArrayHandle handle, - GroupedArrayHandle other_handle, - const indptr_t *out_indptr, - float *out_data); -// Lag -DLL_EXPORT int GroupedArrayFloat32_LagTransform(GroupedArrayHandle handle, - int lag, float *out); // Rolling -DLL_EXPORT int -GroupedArrayFloat32_RollingMeanTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_RollingStdTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_RollingMinTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_RollingMaxTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_RollingQuantileTransform(GroupedArrayHandle handle, int lag, - float p, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_RollingMeanUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - float *out); -DLL_EXPORT int GroupedArrayFloat32_RollingStdUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - float *out); -DLL_EXPORT int GroupedArrayFloat32_RollingMinUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - float *out); -DLL_EXPORT int GroupedArrayFloat32_RollingMaxUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_RollingQuantileUpdate(GroupedArrayHandle handle, int lag, - float p, int window_size, - int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingStdTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMinTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMaxTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingQuantileTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMeanUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingStdUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMinUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingMaxUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_RollingQuantileUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, int window_size, int min_samples, float *out); // Seasonal rolling -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingMeanTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingStdTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingMinTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingMaxTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingQuantileTransform( - GroupedArrayHandle handle, int lag, int season_length, float p, - int window_size, int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingMeanUpdate( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int -GroupedArrayFloat32_SeasonalRollingStdUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int -GroupedArrayFloat32_SeasonalRollingMinUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int -GroupedArrayFloat32_SeasonalRollingMaxUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, float *out); -DLL_EXPORT int GroupedArrayFloat32_SeasonalRollingQuantileUpdate( - GroupedArrayHandle handle, int lag, int season_length, float p, - int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingStdTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMinTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMaxTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingQuantileTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, float p, int window_size, int min_samples, + float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMeanUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingStdUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMinUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingMaxUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out); +DLL_EXPORT void GroupedArrayFloat32_SeasonalRollingQuantileUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, float p, int window_size, int min_samples, + float *out); // Expanding -DLL_EXPORT int -GroupedArrayFloat32_ExpandingMeanTransform(GroupedArrayHandle handle, int lag, - float *out, float *agg); -DLL_EXPORT int -GroupedArrayFloat32_ExpandingStdTransform(GroupedArrayHandle handle, int lag, - float *out, float *agg); -DLL_EXPORT int -GroupedArrayFloat32_ExpandingMinTransform(GroupedArrayHandle handle, int lag, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_ExpandingMaxTransform(GroupedArrayHandle handle, int lag, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_ExpandingQuantileTransform(GroupedArrayHandle handle, - int lag, float p, float *out); -DLL_EXPORT int -GroupedArrayFloat32_ExpandingQuantileUpdate(GroupedArrayHandle handle, int lag, - float p, float *out); +DLL_EXPORT void GroupedArrayFloat32_ExpandingMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float *out, float *agg); +DLL_EXPORT void GroupedArrayFloat32_ExpandingStdTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float *out, float *agg); +DLL_EXPORT void +GroupedArrayFloat32_ExpandingMinTransform(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, float *out); +DLL_EXPORT void +GroupedArrayFloat32_ExpandingMaxTransform(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, float *out); +DLL_EXPORT void GroupedArrayFloat32_ExpandingQuantileTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, float *out); +DLL_EXPORT void GroupedArrayFloat32_ExpandingQuantileUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, float *out); // Exponentially weighted -DLL_EXPORT int GroupedArrayFloat32_ExponentiallyWeightedMeanTransform( - GroupedArrayHandle handle, int lag, float alpha, float *out); +DLL_EXPORT void GroupedArrayFloat32_ExponentiallyWeightedMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float alpha, float *out); // Scalers -DLL_EXPORT int GroupedArrayFloat32_MinMaxScalerStats(GroupedArrayHandle handle, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_StandardScalerStats(GroupedArrayHandle handle, float *out); - -DLL_EXPORT int -GroupedArrayFloat32_RobustIqrScalerStats(GroupedArrayHandle handle, float *out); - -DLL_EXPORT int -GroupedArrayFloat32_RobustMadScalerStats(GroupedArrayHandle handle, float *out); - -DLL_EXPORT int GroupedArrayFloat32_ScalerTransform(GroupedArrayHandle handle, - const float *stats, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_ScalerInverseTransform(GroupedArrayHandle handle, - const float *stats, float *out); -DLL_EXPORT int -GroupedArrayFloat32_BoxCoxLambdaGuerrero(GroupedArrayHandle handle, int period, - float lower, float upper, float *out); - +DLL_EXPORT void GroupedArrayFloat32_MinMaxScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + float *out); +DLL_EXPORT void GroupedArrayFloat32_StandardScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + float *out); +DLL_EXPORT void GroupedArrayFloat32_RobustIqrScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + float *out); +DLL_EXPORT void GroupedArrayFloat32_RobustMadScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + float *out); DLL_EXPORT void -GroupedArrayFloat32_BoxCoxLambdaLogLik(GroupedArrayHandle handle, float lower, - float upper, float *out); -DLL_EXPORT int GroupedArrayFloat32_BoxCoxTransform(GroupedArrayHandle handle, - const float *lambdas, - float *out); -DLL_EXPORT int -GroupedArrayFloat32_BoxCoxInverseTransform(GroupedArrayHandle handle, - const float *lambdas, float *out); +GroupedArrayFloat32_ScalerTransform(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const float *stats, float *out); +DLL_EXPORT void GroupedArrayFloat32_ScalerInverseTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + const float *stats, float *out); +DLL_EXPORT void GroupedArrayFloat32_BoxCoxLambdaGuerrero( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int period, float lower, float upper, float *out); +DLL_EXPORT void GroupedArrayFloat32_BoxCoxLambdaLogLik( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + float lower, float upper, float *out); +DLL_EXPORT void +GroupedArrayFloat32_BoxCoxTransform(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const float *lambdas, float *out); +DLL_EXPORT void GroupedArrayFloat32_BoxCoxInverseTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + const float *lambdas, float *out); // Differences -DLL_EXPORT void GroupedArrayFloat32_NumDiffs(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat32_NumDiffs(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, float *out); -DLL_EXPORT void GroupedArrayFloat32_NumSeasDiffs(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat32_NumSeasDiffs(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int period, int max_d, float *out); -DLL_EXPORT void -GroupedArrayFloat32_NumSeasDiffsPeriods(GroupedArrayHandle handle, int max_d, - float *periods_and_out); -DLL_EXPORT void GroupedArrayFloat32_Period(GroupedArrayHandle handle, - size_t max_lag, float *out); -DLL_EXPORT void GroupedArrayFloat32_Difference(GroupedArrayHandle handle, int d, - float *out); -DLL_EXPORT void GroupedArrayFloat32_Differences(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat32_NumSeasDiffsPeriods( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int max_d, float *periods_and_out); +DLL_EXPORT void GroupedArrayFloat32_Period(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, size_t max_lag, + float *out); +DLL_EXPORT void GroupedArrayFloat32_Difference(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int d, float *out); +DLL_EXPORT void GroupedArrayFloat32_Differences(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *ds, float *out); DLL_EXPORT void GroupedArrayFloat32_InvertDifferences( - GroupedArrayHandle handle, GroupedArrayHandle tails_handle, + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + const float *other_data, const indptr_t *other_indptr, int other_n_indptr, const indptr_t *out_indptr, float *out_data); // Float64 Methods @@ -349,182 +369,197 @@ DLL_EXPORT int Float64_Period(const double *x, size_t n, int period); // GA // Manipulation -DLL_EXPORT int GroupedArrayFloat64_Create(const double *data, indptr_t n_data, - indptr_t *indptr, indptr_t n_indptr, - int num_threads, - GroupedArrayHandle *out); -DLL_EXPORT int GroupedArrayFloat64_Delete(GroupedArrayHandle handle); +DLL_EXPORT void GroupedArrayFloat64_IndexFromEnd(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int k, double *out); -DLL_EXPORT int GroupedArrayFloat64_IndexFromEnd(GroupedArrayHandle handle, - int k, double *out); -DLL_EXPORT void GroupedArrayFloat64_Head(GroupedArrayHandle handle, int k, - double *out); -DLL_EXPORT void GroupedArrayFloat64_Tail(GroupedArrayHandle handle, int k, - double *out); -DLL_EXPORT void GroupedArrayFloat64_Tails(GroupedArrayHandle handle, +// Lag +DLL_EXPORT void GroupedArrayFloat64_LagTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, double *out); +DLL_EXPORT void GroupedArrayFloat64_IndexFromEnd(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int k, double *out); +DLL_EXPORT void GroupedArrayFloat64_Head(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int k, double *out); +DLL_EXPORT void GroupedArrayFloat64_Tail(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int k, double *out); +DLL_EXPORT void GroupedArrayFloat64_Append( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + const double *other_data, const indptr_t *other_indptr, int other_n_indptr, + const indptr_t *out_indptr, double *out_data); +DLL_EXPORT void GroupedArrayFloat64_Tails(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const indptr_t *indptr_out, double *out); -DLL_EXPORT void GroupedArrayFloat64_Append(GroupedArrayHandle handle, - GroupedArrayHandle other_handle, - const indptr_t *out_indptr, - double *out_data); -// Lag -DLL_EXPORT int GroupedArrayFloat64_LagTransform(GroupedArrayHandle handle, - int lag, double *out); // Rolling -DLL_EXPORT int -GroupedArrayFloat64_RollingMeanTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_RollingStdTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_RollingMinTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_RollingMaxTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_RollingQuantileTransform(GroupedArrayHandle handle, int lag, - double p, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_RollingMeanUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - double *out); -DLL_EXPORT int GroupedArrayFloat64_RollingStdUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - double *out); -DLL_EXPORT int GroupedArrayFloat64_RollingMinUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - double *out); -DLL_EXPORT int GroupedArrayFloat64_RollingMaxUpdate(GroupedArrayHandle handle, - int lag, int window_size, - int min_samples, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_RollingQuantileUpdate(GroupedArrayHandle handle, int lag, - double p, int window_size, - int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingStdTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMinTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMaxTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingQuantileTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMeanUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingStdUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMinUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingMaxUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_RollingQuantileUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, int window_size, int min_samples, double *out); // Seasonal rolling -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingMeanTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingStdTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingMinTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingMaxTransform( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingQuantileTransform( - GroupedArrayHandle handle, int lag, int season_length, double p, - int window_size, int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingMeanUpdate( - GroupedArrayHandle handle, int lag, int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int -GroupedArrayFloat64_SeasonalRollingStdUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int -GroupedArrayFloat64_SeasonalRollingMinUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int -GroupedArrayFloat64_SeasonalRollingMaxUpdate(GroupedArrayHandle handle, int lag, - int season_length, int window_size, - int min_samples, double *out); -DLL_EXPORT int GroupedArrayFloat64_SeasonalRollingQuantileUpdate( - GroupedArrayHandle handle, int lag, int season_length, double p, - int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingStdTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMinTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMaxTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingQuantileTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, double p, int window_size, int min_samples, + double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMeanUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingStdUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMinUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingMaxUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out); +DLL_EXPORT void GroupedArrayFloat64_SeasonalRollingQuantileUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, double p, int window_size, int min_samples, + double *out); // Expanding -DLL_EXPORT int -GroupedArrayFloat64_ExpandingMeanTransform(GroupedArrayHandle handle, int lag, - double *out, double *agg); -DLL_EXPORT int -GroupedArrayFloat64_ExpandingStdTransform(GroupedArrayHandle handle, int lag, - double *out, double *agg); -DLL_EXPORT int -GroupedArrayFloat64_ExpandingMinTransform(GroupedArrayHandle handle, int lag, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_ExpandingMaxTransform(GroupedArrayHandle handle, int lag, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_ExpandingQuantileTransform(GroupedArrayHandle handle, - int lag, double p, double *out); -DLL_EXPORT int -GroupedArrayFloat64_ExpandingQuantileUpdate(GroupedArrayHandle handle, int lag, - double p, double *out); +DLL_EXPORT void GroupedArrayFloat64_ExpandingMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double *out, double *agg); +DLL_EXPORT void GroupedArrayFloat64_ExpandingStdTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double *out, double *agg); +DLL_EXPORT void GroupedArrayFloat64_ExpandingMinTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double *out); +DLL_EXPORT void GroupedArrayFloat64_ExpandingMaxTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double *out); +DLL_EXPORT void GroupedArrayFloat64_ExpandingQuantileTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, double *out); +DLL_EXPORT void GroupedArrayFloat64_ExpandingQuantileUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, double *out); // Exponentially weighted -DLL_EXPORT int GroupedArrayFloat64_ExponentiallyWeightedMeanTransform( - GroupedArrayHandle handle, int lag, double alpha, double *out); +DLL_EXPORT void GroupedArrayFloat64_ExponentiallyWeightedMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double alpha, double *out); // Scalers -DLL_EXPORT int GroupedArrayFloat64_MinMaxScalerStats(GroupedArrayHandle handle, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_StandardScalerStats(GroupedArrayHandle handle, double *out); - -DLL_EXPORT int -GroupedArrayFloat64_RobustIqrScalerStats(GroupedArrayHandle handle, - double *out); - -DLL_EXPORT int -GroupedArrayFloat64_RobustMadScalerStats(GroupedArrayHandle handle, - double *out); - -DLL_EXPORT int GroupedArrayFloat64_ScalerTransform(GroupedArrayHandle handle, - const double *stats, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_ScalerInverseTransform(GroupedArrayHandle handle, - const double *stats, double *out); -DLL_EXPORT int -GroupedArrayFloat64_BoxCoxLambdaGuerrero(GroupedArrayHandle handle, int period, - double lower, double upper, - double *out); - +DLL_EXPORT void GroupedArrayFloat64_MinMaxScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + double *out); +DLL_EXPORT void GroupedArrayFloat64_StandardScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + double *out); +DLL_EXPORT void GroupedArrayFloat64_RobustIqrScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + double *out); +DLL_EXPORT void GroupedArrayFloat64_RobustMadScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, + double *out); DLL_EXPORT void -GroupedArrayFloat64_BoxCoxLambdaLogLik(GroupedArrayHandle handle, double lower, - double upper, double *out); -DLL_EXPORT int GroupedArrayFloat64_BoxCoxTransform(GroupedArrayHandle handle, - const double *lambdas, - double *out); -DLL_EXPORT int -GroupedArrayFloat64_BoxCoxInverseTransform(GroupedArrayHandle handle, - const double *lambdas, double *out); +GroupedArrayFloat64_ScalerTransform(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const double *stats, double *out); +DLL_EXPORT void GroupedArrayFloat64_ScalerInverseTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + const double *stats, double *out); +DLL_EXPORT void GroupedArrayFloat64_BoxCoxLambdaGuerrero( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int period, double lower, double upper, double *out); +DLL_EXPORT void GroupedArrayFloat64_BoxCoxLambdaLogLik( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + double lower, double upper, double *out); +DLL_EXPORT void +GroupedArrayFloat64_BoxCoxTransform(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const double *lambdas, double *out); +DLL_EXPORT void GroupedArrayFloat64_BoxCoxInverseTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + const double *lambdas, double *out); // Differences -DLL_EXPORT void GroupedArrayFloat64_NumDiffs(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat64_NumDiffs(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, double *out); -DLL_EXPORT void GroupedArrayFloat64_NumSeasDiffs(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat64_NumSeasDiffs(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int period, int max_d, double *out); -DLL_EXPORT void -GroupedArrayFloat64_NumSeasDiffsPeriods(GroupedArrayHandle handle, int max_d, - double *periods_and_out); -DLL_EXPORT void GroupedArrayFloat64_Period(GroupedArrayHandle handle, - size_t max_lag, double *out); -DLL_EXPORT void GroupedArrayFloat64_Difference(GroupedArrayHandle handle, int d, - double *out); -DLL_EXPORT void GroupedArrayFloat64_Differences(GroupedArrayHandle handle, +DLL_EXPORT void GroupedArrayFloat64_NumSeasDiffsPeriods( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int max_d, double *periods_and_out); +DLL_EXPORT void GroupedArrayFloat64_Period(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, size_t max_lag, + double *out); +DLL_EXPORT void GroupedArrayFloat64_Difference(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int d, double *out); +DLL_EXPORT void GroupedArrayFloat64_Differences(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *ds, double *out); DLL_EXPORT void GroupedArrayFloat64_InvertDifferences( - GroupedArrayHandle handle, GroupedArrayHandle tails_handle, + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + const double *other_data, const indptr_t *other_indptr, int other_n_indptr, const indptr_t *out_indptr, double *out_data); } diff --git a/include/grouped_array.h b/include/grouped_array.h index f1ee965..5743ab6 100644 --- a/include/grouped_array.h +++ b/include/grouped_array.h @@ -4,7 +4,6 @@ #include #include -using GroupedArrayHandle = void *; using indptr_t = int32_t; template inline indptr_t FirstNotNaN(const T *data, indptr_t n) { @@ -35,15 +34,14 @@ template inline void SkipLags(T *out, int n, int lag) { template class GroupedArray { private: const T *data_; - indptr_t n_data_; const indptr_t *indptr_; int n_groups_; int num_threads_; public: - GroupedArray(const T *data, indptr_t n_data, const indptr_t *indptr, - int n_indptr, int num_threads) - : data_(data), n_data_(n_data), indptr_(indptr), n_groups_(n_indptr - 1), + GroupedArray(const T *data, const indptr_t *indptr, int n_indptr, + int num_threads) + : data_(data), indptr_(indptr), n_groups_(n_indptr - 1), num_threads_(num_threads) {} ~GroupedArray() {} template @@ -149,17 +147,17 @@ template class GroupedArray { } template - void Zip(Func f, const GroupedArray *other, const indptr_t *out_indptr, + void Zip(Func f, const GroupedArray &other, const indptr_t *out_indptr, T *out) const noexcept { #pragma omp parallel for schedule(static) num_threads(num_threads_) for (int i = 0; i < n_groups_; ++i) { indptr_t start = indptr_[i]; indptr_t end = indptr_[i + 1]; indptr_t n = end - start; - indptr_t other_start = other->indptr_[i]; - indptr_t other_end = other->indptr_[i + 1]; + indptr_t other_start = other.indptr_[i]; + indptr_t other_end = other.indptr_[i + 1]; indptr_t other_n = other_end - other_start; - f(data_ + start, n, other->data_ + other_start, other_n, + f(data_ + start, n, other.data_ + other_start, other_n, out + out_indptr[i]); } } diff --git a/include/seasonal.h b/include/seasonal.h index 1a518a2..98dbfe2 100644 --- a/include/seasonal.h +++ b/include/seasonal.h @@ -10,6 +10,10 @@ template void Difference(const T *data, int n, T *out, int d) { std::copy(data, data + n, out); return; } + if (n < d) { + std::fill(out, out + n, std::numeric_limits::quiet_NaN()); + return; + } std::fill(out, out + d, std::numeric_limits::quiet_NaN()); for (int i = d; i < n; ++i) { out[i] = data[i] - data[i - d]; diff --git a/pyproject.toml b/pyproject.toml index 3551bd8..1b7735f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "coreforecast" -version = "0.0.9" +version = "0.0.10" requires-python = ">=3.8" dependencies = [ "importlib_resources ; python_version < '3.10'", diff --git a/python/coreforecast/__init__.py b/python/coreforecast/__init__.py index 00ec2dc..9b36b86 100644 --- a/python/coreforecast/__init__.py +++ b/python/coreforecast/__init__.py @@ -1 +1 @@ -__version__ = "0.0.9" +__version__ = "0.0.10" diff --git a/python/coreforecast/grouped_array.py b/python/coreforecast/grouped_array.py index f694839..8f5200f 100644 --- a/python/coreforecast/grouped_array.py +++ b/python/coreforecast/grouped_array.py @@ -16,6 +16,12 @@ class GroupedArray: num_threads (int): Number of threads to use when computing transformations.""" def __init__(self, data: np.ndarray, indptr: np.ndarray, num_threads: int = 1): + if data.ndim != 1: + raise ValueError("data must be a 1d array") + if indptr.ndim != 1: + raise ValueError("indptr must be a 1d array") + if indptr[-1] != data.size: + raise ValueError("Last element of indptr must be equal to the size of data") self.data = np.ascontiguousarray(data, dtype=data.dtype) self.data = _ensure_float(self.data) if self.data.dtype == np.float32: @@ -24,18 +30,6 @@ def __init__(self, data: np.ndarray, indptr: np.ndarray, num_threads: int = 1): self.prefix = "GroupedArrayFloat64" self.indptr = indptr.astype(_indptr_dtype, copy=False) self.num_threads = num_threads - self._handle = ctypes.c_void_p() - _LIB[f"{self.prefix}_Create"]( - _data_as_void_ptr(self.data), - _indptr_t(self.data.size), - self.indptr.ctypes.data_as(ctypes.POINTER(_indptr_t)), - _indptr_t(self.indptr.size), - ctypes.c_int(num_threads), - ctypes.byref(self._handle), - ) - - def __del__(self): - _LIB[f"{self.prefix}_Delete"](self._handle) def __len__(self): return self.indptr.size - 1 @@ -61,8 +55,13 @@ def _append(self, other: "GroupedArray") -> "GroupedArray": new_indptr = self.indptr + other.indptr new_data = np.empty_like(self.data, shape=new_indptr[-1]) _LIB[f"{self.prefix}_Append"]( - self._handle, - other._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), + _data_as_void_ptr(other.data), + _data_as_void_ptr(other.indptr), + _indptr_t(other.indptr.size), _data_as_void_ptr(new_indptr), _data_as_void_ptr(new_data), ) @@ -76,7 +75,10 @@ def _pyfloat_to_c(self, x: float) -> Union[ctypes.c_float, ctypes.c_double]: def _scaler_fit(self, scaler_type: str) -> np.ndarray: stats = np.empty_like(self.data, shape=(len(self), 2)) _LIB[f"{self.prefix}_{scaler_type}ScalerStats"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(stats), ) return stats @@ -85,7 +87,10 @@ def _scaler_transform(self, stats: np.ndarray) -> np.ndarray: out = np.empty_like(self.data) stats = stats.astype(self.data.dtype, copy=False) _LIB[f"{self.prefix}_ScalerTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(stats), _data_as_void_ptr(out), ) @@ -95,7 +100,10 @@ def _scaler_inverse_transform(self, stats: np.ndarray) -> np.ndarray: out = np.empty_like(self.data) stats = stats.astype(self.data.dtype, copy=False) _LIB[f"{self.prefix}_ScalerInverseTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(stats), _data_as_void_ptr(out), ) @@ -104,7 +112,10 @@ def _scaler_inverse_transform(self, stats: np.ndarray) -> np.ndarray: def _index_from_end(self, k: int) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_IndexFromEnd"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(k), _data_as_void_ptr(out), ) @@ -113,7 +124,10 @@ def _index_from_end(self, k: int) -> np.ndarray: def _head(self, k: int) -> np.ndarray: out = np.empty_like(self.data, shape=k * len(self)) _LIB[f"{self.prefix}_Head"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(k), _data_as_void_ptr(out), ) @@ -122,7 +136,10 @@ def _head(self, k: int) -> np.ndarray: def _tail(self, k: int) -> np.ndarray: out = np.empty_like(self.data, shape=k * len(self)) _LIB[f"{self.prefix}_Tail"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(k), _data_as_void_ptr(out), ) @@ -131,7 +148,10 @@ def _tail(self, k: int) -> np.ndarray: def _tails(self, indptr_out: np.ndarray) -> np.ndarray: out = np.empty_like(self.data, shape=indptr_out[-1]) _LIB[f"{self.prefix}_Tails"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(indptr_out), _data_as_void_ptr(out), ) @@ -143,7 +163,10 @@ def _take(self, idxs: np.ndarray) -> np.ndarray: def _lag_transform(self, lag: int) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_LagTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), _data_as_void_ptr(out), ) @@ -154,7 +177,10 @@ def _rolling_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_Rolling{stat_name}Transform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(window_size), ctypes.c_int(min_samples), @@ -167,7 +193,10 @@ def _rolling_quantile_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_RollingQuantileTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), self._pyfloat_to_c(p), ctypes.c_int(window_size), @@ -181,7 +210,10 @@ def _rolling_update( ) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_Rolling{stat_name}Update"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(window_size), ctypes.c_int(min_samples), @@ -194,7 +226,10 @@ def _rolling_quantile_update( ) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_RollingQuantileUpdate"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), self._pyfloat_to_c(p), ctypes.c_int(window_size), @@ -213,7 +248,10 @@ def _seasonal_rolling_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_SeasonalRolling{stat_name}Transform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(season_length), ctypes.c_int(window_size), @@ -232,7 +270,10 @@ def _seasonal_rolling_update( ) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_SeasonalRolling{stat_name}Update"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(season_length), ctypes.c_int(window_size), @@ -251,7 +292,10 @@ def _seasonal_rolling_quantile_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_SeasonalRollingQuantileTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(season_length), self._pyfloat_to_c(p), @@ -271,7 +315,10 @@ def _seasonal_rolling_quantile_update( ) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_SeasonalRollingQuantileUpdate"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), ctypes.c_int(season_length), self._pyfloat_to_c(p), @@ -289,7 +336,10 @@ def _expanding_transform_with_aggs( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_Expanding{stat_name}Transform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), _data_as_void_ptr(out), _data_as_void_ptr(aggs), @@ -303,7 +353,10 @@ def _expanding_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_Expanding{stat_name}Transform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), _data_as_void_ptr(out), ) @@ -312,7 +365,10 @@ def _expanding_transform( def _expanding_quantile_transform(self, lag: int, p: float) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_ExpandingQuantileTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), self._pyfloat_to_c(p), _data_as_void_ptr(out), @@ -322,7 +378,10 @@ def _expanding_quantile_transform(self, lag: int, p: float) -> np.ndarray: def _expanding_quantile_update(self, lag: int, p: float) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_ExpandingQuantileUpdate"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), self._pyfloat_to_c(p), _data_as_void_ptr(out), @@ -337,7 +396,10 @@ def _exponentially_weighted_transform( ) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_ExponentiallyWeighted{stat_name}Transform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(lag), self._pyfloat_to_c(alpha), _data_as_void_ptr(out), @@ -351,7 +413,10 @@ def _boxcox_fit( if method == "guerrero": assert season_length is not None _LIB[f"{self.prefix}_BoxCoxLambdaGuerrero"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(season_length), _pyfloat_to_np_c(lower, self.data.dtype), _pyfloat_to_np_c(upper, self.data.dtype), @@ -359,7 +424,10 @@ def _boxcox_fit( ) else: _LIB[f"{self.prefix}_BoxCoxLambdaLogLik"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _pyfloat_to_np_c(lower, self.data.dtype), _pyfloat_to_np_c(upper, self.data.dtype), _data_as_void_ptr(out), @@ -370,7 +438,10 @@ def _boxcox_transform(self, stats: np.ndarray) -> np.ndarray: out = np.empty_like(self.data) stats = stats.astype(self.data.dtype, copy=False) _LIB[f"{self.prefix}_BoxCoxTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(stats), _data_as_void_ptr(out), ) @@ -380,7 +451,10 @@ def _boxcox_inverse_transform(self, stats: np.ndarray) -> np.ndarray: out = np.empty_like(self.data) stats = stats.astype(self.data.dtype, copy=False) _LIB[f"{self.prefix}_BoxCoxInverseTransform"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(stats), _data_as_void_ptr(out), ) @@ -389,7 +463,10 @@ def _boxcox_inverse_transform(self, stats: np.ndarray) -> np.ndarray: def _num_diffs(self, max_d: int = 1) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_NumDiffs"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(max_d), _data_as_void_ptr(out), ) @@ -398,7 +475,10 @@ def _num_diffs(self, max_d: int = 1) -> np.ndarray: def _num_seas_diffs(self, season_length: int, max_d: int = 1) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_NumSeasDiffs"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(season_length), ctypes.c_int(max_d), _data_as_void_ptr(out), @@ -409,7 +489,10 @@ def _num_seas_diffs_periods(self, max_d: int, periods: np.ndarray) -> np.ndarray periods_and_out = np.empty_like(self.data, shape=(len(self), 2)) periods_and_out[:, 0] = periods _LIB[f"{self.prefix}_NumSeasDiffsPeriods"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(max_d), _data_as_void_ptr(periods_and_out), ) @@ -418,7 +501,10 @@ def _num_seas_diffs_periods(self, max_d: int, periods: np.ndarray) -> np.ndarray def _periods(self, max_period: int) -> np.ndarray: out = np.empty_like(self.data, shape=len(self)) _LIB[f"{self.prefix}_Period"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_size_t(max_period), _data_as_void_ptr(out), ) @@ -427,7 +513,10 @@ def _periods(self, max_period: int) -> np.ndarray: def _diff(self, d: int) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_Difference"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), ctypes.c_int(d), _data_as_void_ptr(out), ) @@ -436,7 +525,10 @@ def _diff(self, d: int) -> np.ndarray: def _diffs(self, ds: np.ndarray) -> np.ndarray: out = np.empty_like(self.data) _LIB[f"{self.prefix}_Differences"]( - self._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), _data_as_void_ptr(ds), _data_as_void_ptr(out), ) @@ -444,17 +536,20 @@ def _diffs(self, ds: np.ndarray) -> np.ndarray: def _inv_diff(self, d: int, tails: np.ndarray) -> np.ndarray: ds = np.full(len(self), d, dtype=_indptr_dtype) - tails = tails.astype(self.data.dtype, copy=False) return self._inv_diffs(ds, tails) def _inv_diffs(self, ds: np.ndarray, tails: np.ndarray) -> np.ndarray: tails_indptr = _diffs_to_indptr(ds) tails = tails.astype(self.data.dtype, copy=False) - tails_ga = GroupedArray(tails, tails_indptr) out = np.empty_like(self.data) _LIB[f"{self.prefix}_InvertDifferences"]( - self._handle, - tails_ga._handle, + _data_as_void_ptr(self.data), + _data_as_void_ptr(self.indptr), + _indptr_t(self.indptr.size), + ctypes.c_int(self.num_threads), + _data_as_void_ptr(tails), + _data_as_void_ptr(tails_indptr), + _indptr_t(tails_indptr.size), _data_as_void_ptr(self.indptr), _data_as_void_ptr(out), ) diff --git a/src/c_api.cpp b/src/c_api.cpp index 78453fd..abcae06 100644 --- a/src/c_api.cpp +++ b/src/c_api.cpp @@ -148,381 +148,384 @@ int Float32_Period(const float *x, size_t n, int max_lag) { // GA // Lag -int GroupedArrayFloat32_LagTransform(GroupedArrayHandle handle, int lag, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(LagTransform, lag, out); - return 0; +void GroupedArrayFloat32_LagTransform(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int lag, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(LagTransform, lag, out); } // Manipulation -int GroupedArrayFloat32_Create(const float *data, indptr_t n_data, - indptr_t *indptr, indptr_t n_indptr, - int num_threads, GroupedArrayHandle *out) { - *out = new GroupedArray(data, n_data, indptr, n_indptr, num_threads); - return 0; -} -int GroupedArrayFloat32_Delete(GroupedArrayHandle handle) { - delete reinterpret_cast *>(handle); - return 0; -} -int GroupedArrayFloat32_IndexFromEnd(GroupedArrayHandle handle, int k, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(IndexFromEnd, 1, out, 0, k); - return 0; +void GroupedArrayFloat32_IndexFromEnd(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int k, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(IndexFromEnd, 1, out, 0, k); } -void GroupedArrayFloat32_Head(GroupedArrayHandle handle, int k, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(Head, k, out, 0, k); -} -void GroupedArrayFloat32_Tail(GroupedArrayHandle handle, int k, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(Tail, k, out, 0, k); -} -void GroupedArrayFloat32_Append(GroupedArrayHandle handle, - GroupedArrayHandle other_handle, - const indptr_t *out_indptr, float *out_data) { - auto ga = reinterpret_cast *>(handle); - auto other = reinterpret_cast *>(other_handle); - ga->Zip(Append, other, out_indptr, out_data); -} -void GroupedArrayFloat32_Tails(GroupedArrayHandle handle, +void GroupedArrayFloat32_Head(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int k, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(Head, k, out, 0, k); +} +void GroupedArrayFloat32_Tail(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int k, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(Tail, k, out, 0, k); +} +void GroupedArrayFloat32_Append(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const float *other_data, + const indptr_t *other_indptr, + int other_n_indptr, const indptr_t *out_indptr, + float *out_data) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + auto other = GroupedArray(other_data, other_indptr, other_n_indptr, + num_threads); + ga.Zip(Append, other, out_indptr, out_data); +} +void GroupedArrayFloat32_Tails(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *indptr_out, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->VariableReduce(Tail, indptr_out, out); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.VariableReduce(Tail, indptr_out, out); } // Lag // Rolling -int GroupedArrayFloat32_RollingMeanTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMeanTransform, lag, out, window_size, - min_samples); - return 0; -} -int GroupedArrayFloat32_RollingStdTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingStdTransform, lag, out, window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_RollingMinTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMinTransform, lag, out, window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_RollingMaxTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMaxTransform, lag, out, window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_RollingQuantileTransform(GroupedArrayHandle handle, - int lag, float p, - int window_size, - int min_samples, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingQuantileTransform, lag, out, window_size, - min_samples, p); - return 0; -} -int GroupedArrayFloat32_RollingMeanUpdate(GroupedArrayHandle handle, int lag, +void GroupedArrayFloat32_RollingMeanTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMeanTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat32_RollingStdTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingStdTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat32_RollingMinTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMinTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat32_RollingMaxTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMaxTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat32_RollingQuantileTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingQuantileTransform, lag, out, window_size, + min_samples, p); +} +void GroupedArrayFloat32_RollingMeanUpdate(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMeanUpdate(), 1, out, lag, window_size, min_samples); +} +void GroupedArrayFloat32_RollingStdUpdate(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, int window_size, int min_samples, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMeanUpdate(), 1, out, lag, window_size, min_samples); - return 0; + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingStdUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat32_RollingStdUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingStdUpdate(), 1, out, lag, window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_RollingMinUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMinUpdate(), 1, out, lag, window_size, min_samples); - return 0; +void GroupedArrayFloat32_RollingMinUpdate(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMinUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat32_RollingMaxUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMaxUpdate(), 1, out, lag, window_size, min_samples); - return 0; +void GroupedArrayFloat32_RollingMaxUpdate(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMaxUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat32_RollingQuantileUpdate(GroupedArrayHandle handle, - int lag, float p, int window_size, - int min_samples, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingQuantileUpdate(), 1, out, lag, window_size, - min_samples, p); - return 0; +void GroupedArrayFloat32_RollingQuantileUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float p, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingQuantileUpdate(), 1, out, lag, window_size, + min_samples, p); } // Seasonal rolling -int GroupedArrayFloat32_SeasonalRollingMeanTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMeanTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingStdTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingStdTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingMinTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMinTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingMaxTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMaxTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingQuantileTransform( - GroupedArrayHandle handle, int lag, int season_length, float p, - int window_size, int min_samples, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingQuantileTransform(), lag, out, - season_length, window_size, min_samples, p); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingMeanUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, float *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMeanUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingStdUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, float *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingStdUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingMinUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, float *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMinUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingMaxUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, float *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMaxUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat32_SeasonalRollingQuantileUpdate( - GroupedArrayHandle handle, int lag, int season_length, float p, - int window_size, int min_samples, float *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingQuantileUpdate(), 1, out, lag, season_length, - window_size, min_samples, p); - return 0; +void GroupedArrayFloat32_SeasonalRollingMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMeanTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingStdTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingStdTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingMinTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMinTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingMaxTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMaxTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingQuantileTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, float p, int window_size, int min_samples, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingQuantileTransform(), lag, out, + season_length, window_size, min_samples, p); +} +void GroupedArrayFloat32_SeasonalRollingMeanUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMeanUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingStdUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingStdUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingMinUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMinUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingMaxUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMaxUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat32_SeasonalRollingQuantileUpdate( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, float p, int window_size, int min_samples, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingQuantileUpdate(), 1, out, lag, season_length, + window_size, min_samples, p); } // Expanding -int GroupedArrayFloat32_ExpandingMeanTransform(GroupedArrayHandle handle, +void GroupedArrayFloat32_ExpandingMeanTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, float *out, + float *agg) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.TransformAndReduce(ExpandingMeanTransform, lag, out, 1, agg); +} +void GroupedArrayFloat32_ExpandingStdTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int lag, float *out, float *agg) { - auto ga = reinterpret_cast *>(handle); - ga->TransformAndReduce(ExpandingMeanTransform, lag, out, 1, agg); - return 0; -} -int GroupedArrayFloat32_ExpandingStdTransform(GroupedArrayHandle handle, - int lag, float *out, float *agg) { - auto ga = reinterpret_cast *>(handle); - ga->TransformAndReduce(ExpandingStdTransform, lag, out, 3, agg); - return 0; -} -int GroupedArrayFloat32_ExpandingMinTransform(GroupedArrayHandle handle, - int lag, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingMinTransform(), lag, out); - return 0; -} -int GroupedArrayFloat32_ExpandingMaxTransform(GroupedArrayHandle handle, - int lag, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingMaxTransform(), lag, out); - - return 0; -} -int GroupedArrayFloat32_ExpandingQuantileTransform(GroupedArrayHandle handle, - int lag, float p, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingQuantileTransform, lag, out, p); - return 0; -} -int GroupedArrayFloat32_ExpandingQuantileUpdate(GroupedArrayHandle handle, - int lag, float p, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(ExpandingQuantileUpdate, 1, out, lag, p); - return 0; + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.TransformAndReduce(ExpandingStdTransform, lag, out, 3, agg); +} +void GroupedArrayFloat32_ExpandingMinTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingMinTransform(), lag, out); +} +void GroupedArrayFloat32_ExpandingMaxTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingMaxTransform(), lag, out); +} +void GroupedArrayFloat32_ExpandingQuantileTransform(const float *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, int lag, + float p, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingQuantileTransform, lag, out, p); +} +void GroupedArrayFloat32_ExpandingQuantileUpdate(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, float p, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(ExpandingQuantileUpdate, 1, out, lag, p); } // Exponentially weighted -int GroupedArrayFloat32_ExponentiallyWeightedMeanTransform( - GroupedArrayHandle handle, int lag, float alpha, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExponentiallyWeightedMeanTransform, lag, out, alpha); - return 0; +void GroupedArrayFloat32_ExponentiallyWeightedMeanTransform( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, float alpha, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExponentiallyWeightedMeanTransform, lag, out, alpha); } // Scalers -int GroupedArrayFloat32_MinMaxScalerStats(GroupedArrayHandle handle, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(MinMaxScalerStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat32_StandardScalerStats(GroupedArrayHandle handle, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(StandardScalerStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat32_RobustIqrScalerStats(GroupedArrayHandle handle, +void GroupedArrayFloat32_MinMaxScalerStats(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(MinMaxScalerStats, 2, out, 0); +} +void GroupedArrayFloat32_StandardScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RobustScalerIqrStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat32_RobustMadScalerStats(GroupedArrayHandle handle, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RobustScalerMadStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat32_ScalerTransform(GroupedArrayHandle handle, - const float *stats, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(CommonScalerTransform, stats, out); - return 0; -} -int GroupedArrayFloat32_ScalerInverseTransform(GroupedArrayHandle handle, - const float *stats, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(CommonScalerInverseTransform, stats, out); - return 0; -} -int GroupedArrayFloat32_BoxCoxLambdaGuerrero(GroupedArrayHandle handle, - int period, float lower, - float upper, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(BoxCoxLambda_Guerrero, 2, out, 0, period, lower, upper); - return 0; -} -void GroupedArrayFloat32_BoxCoxLambdaLogLik(GroupedArrayHandle handle, + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(StandardScalerStats, 2, out, 0); +} +void GroupedArrayFloat32_RobustIqrScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RobustScalerIqrStats, 2, out, 0); +} +void GroupedArrayFloat32_RobustMadScalerStats(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RobustScalerMadStats, 2, out, 0); +} +void GroupedArrayFloat32_ScalerTransform(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const float *stats, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(CommonScalerTransform, stats, out); +} +void GroupedArrayFloat32_ScalerInverseTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + const float *stats, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(CommonScalerInverseTransform, stats, out); +} +void GroupedArrayFloat32_BoxCoxLambdaGuerrero(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int period, float lower, + float upper, float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(BoxCoxLambda_Guerrero, 2, out, 0, period, lower, upper); +} +void GroupedArrayFloat32_BoxCoxLambdaLogLik(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, float lower, float upper, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(BoxCoxLambda_LogLik, 2, out, 0, lower, upper); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(BoxCoxLambda_LogLik, 2, out, 0, lower, upper); } -int GroupedArrayFloat32_BoxCoxTransform(GroupedArrayHandle handle, - const float *lambdas, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(BoxCoxTransform, lambdas, out); - return 0; +void GroupedArrayFloat32_BoxCoxTransform(const float *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const float *lambdas, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(BoxCoxTransform, lambdas, out); } -int GroupedArrayFloat32_BoxCoxInverseTransform(GroupedArrayHandle handle, - const float *lambdas, - float *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(BoxCoxInverseTransform, lambdas, out); - return 0; +void GroupedArrayFloat32_BoxCoxInverseTransform(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + const float *lambdas, + float *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(BoxCoxInverseTransform, lambdas, out); } // Differences -void GroupedArrayFloat32_NumDiffs(GroupedArrayHandle handle, int max_d, +void GroupedArrayFloat32_NumDiffs(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumDiffs, 1, out, 0, max_d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumDiffs, 1, out, 0, max_d); } -void GroupedArrayFloat32_NumSeasDiffs(GroupedArrayHandle handle, int period, +void GroupedArrayFloat32_NumSeasDiffs(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int period, int max_d, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumSeasDiffs, 1, out, 0, period, max_d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumSeasDiffs, 1, out, 0, period, max_d); } -void GroupedArrayFloat32_NumSeasDiffsPeriods(GroupedArrayHandle handle, +void GroupedArrayFloat32_NumSeasDiffsPeriods(const float *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, float *periods_and_out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumSeasDiffsPeriods, 2, periods_and_out, 0, max_d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumSeasDiffsPeriods, 2, periods_and_out, 0, max_d); } -void GroupedArrayFloat32_Period(GroupedArrayHandle handle, size_t max_lag, +void GroupedArrayFloat32_Period(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, size_t max_lag, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(GreatestAutocovariance, 1, out, 0, max_lag); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(GreatestAutocovariance, 1, out, 0, max_lag); } -void GroupedArrayFloat32_Difference(GroupedArrayHandle handle, int d, +void GroupedArrayFloat32_Difference(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, int d, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(Difference, 0, out, d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(Difference, 0, out, d); } -void GroupedArrayFloat32_Differences(GroupedArrayHandle handle, +void GroupedArrayFloat32_Differences(const float *data, const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *ds, float *out) { - auto ga = reinterpret_cast *>(handle); - ga->VariableTransform(Differences, ds, out); -} -void GroupedArrayFloat32_InvertDifferences(GroupedArrayHandle handle, - GroupedArrayHandle tails_handle, - const indptr_t *out_indptr, - float *out_data) { - auto ga = reinterpret_cast *>(handle); - auto tails_ga = reinterpret_cast *>(tails_handle); - ga->Zip(InvertDifference, tails_ga, out_indptr, out_data); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.VariableTransform(Differences, ds, out); +} +void GroupedArrayFloat32_InvertDifferences( + const float *data, const indptr_t *indptr, int n_indptr, int num_threads, + const float *other_data, const indptr_t *other_indptr, int other_n_indptr, + const indptr_t *out_indptr, float *out_data) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + auto tails_ga = GroupedArray(other_data, other_indptr, other_n_indptr, + num_threads); + ga.Zip(InvertDifference, tails_ga, out_indptr, out_data); } // Float64 Methods @@ -674,388 +677,385 @@ int Float64_Period(const double *x, size_t n, int max_lag) { // GA // Lag -int GroupedArrayFloat64_LagTransform(GroupedArrayHandle handle, int lag, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(LagTransform, lag, out); - return 0; +void GroupedArrayFloat64_LagTransform(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(LagTransform, lag, out); } // Manipulation -int GroupedArrayFloat64_Create(const double *data, indptr_t n_data, - indptr_t *indptr, indptr_t n_indptr, - int num_threads, GroupedArrayHandle *out) { - *out = new GroupedArray(data, n_data, indptr, n_indptr, num_threads); - return 0; -} -int GroupedArrayFloat64_Delete(GroupedArrayHandle handle) { - delete reinterpret_cast *>(handle); - return 0; -} -int GroupedArrayFloat64_IndexFromEnd(GroupedArrayHandle handle, int k, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(IndexFromEnd, 1, out, 0, k); - return 0; +void GroupedArrayFloat64_IndexFromEnd(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int k, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(IndexFromEnd, 1, out, 0, k); } -void GroupedArrayFloat64_Head(GroupedArrayHandle handle, int k, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(Head, k, out, 0, k); -} -void GroupedArrayFloat64_Tail(GroupedArrayHandle handle, int k, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(Tail, k, out, 0, k); -} -void GroupedArrayFloat64_Append(GroupedArrayHandle handle, - GroupedArrayHandle other_handle, - const indptr_t *out_indptr, double *out_data) { - auto ga = reinterpret_cast *>(handle); - auto other = reinterpret_cast *>(other_handle); - ga->Zip(Append, other, out_indptr, out_data); -} -void GroupedArrayFloat64_Tails(GroupedArrayHandle handle, +void GroupedArrayFloat64_Head(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, int k, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(Head, k, out, 0, k); +} +void GroupedArrayFloat64_Tail(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, int k, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(Tail, k, out, 0, k); +} +void GroupedArrayFloat64_Append(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, + const double *other_data, + const indptr_t *other_indptr, + int other_n_indptr, const indptr_t *out_indptr, + double *out_data) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + auto other = GroupedArray(other_data, other_indptr, other_n_indptr, + num_threads); + ga.Zip(Append, other, out_indptr, out_data); +} +void GroupedArrayFloat64_Tails(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *indptr_out, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->VariableReduce(Tail, indptr_out, out); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.VariableReduce(Tail, indptr_out, out); } // Lag // Rolling -int GroupedArrayFloat64_RollingMeanTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMeanTransform, lag, out, window_size, - min_samples); - return 0; -} -int GroupedArrayFloat64_RollingStdTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingStdTransform, lag, out, window_size, - min_samples); - return 0; -} -int GroupedArrayFloat64_RollingMinTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMinTransform, lag, out, window_size, - min_samples); - return 0; -} -int GroupedArrayFloat64_RollingMaxTransform(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingMaxTransform, lag, out, window_size, - min_samples); - return 0; -} -int GroupedArrayFloat64_RollingQuantileTransform(GroupedArrayHandle handle, - int lag, double p, - int window_size, - int min_samples, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(RollingQuantileTransform, lag, out, window_size, - min_samples, p); - return 0; -} -int GroupedArrayFloat64_RollingMeanUpdate(GroupedArrayHandle handle, int lag, +void GroupedArrayFloat64_RollingMeanTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMeanTransform, lag, out, window_size, + min_samples); +} +void GroupedArrayFloat64_RollingStdTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingStdTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat64_RollingMinTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMinTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat64_RollingMaxTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, int window_size, + int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingMaxTransform, lag, out, window_size, min_samples); +} +void GroupedArrayFloat64_RollingQuantileTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(RollingQuantileTransform, lag, out, window_size, + min_samples, p); +} +void GroupedArrayFloat64_RollingMeanUpdate(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMeanUpdate(), 1, out, lag, window_size, min_samples); +} +void GroupedArrayFloat64_RollingStdUpdate(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, int window_size, int min_samples, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMeanUpdate(), 1, out, lag, window_size, - min_samples); - return 0; + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingStdUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat64_RollingStdUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingStdUpdate(), 1, out, lag, window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_RollingMinUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMinUpdate(), 1, out, lag, window_size, min_samples); - return 0; +void GroupedArrayFloat64_RollingMinUpdate(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMinUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat64_RollingMaxUpdate(GroupedArrayHandle handle, int lag, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingMaxUpdate(), 1, out, lag, window_size, min_samples); - return 0; +void GroupedArrayFloat64_RollingMaxUpdate(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int lag, + int window_size, int min_samples, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingMaxUpdate(), 1, out, lag, window_size, min_samples); } -int GroupedArrayFloat64_RollingQuantileUpdate(GroupedArrayHandle handle, - int lag, double p, - int window_size, int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RollingQuantileUpdate(), 1, out, lag, window_size, - min_samples, p); - return 0; +void GroupedArrayFloat64_RollingQuantileUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double p, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RollingQuantileUpdate(), 1, out, lag, window_size, + min_samples, p); } // Seasonal rolling -int GroupedArrayFloat64_SeasonalRollingMeanTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMeanTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingStdTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingStdTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingMinTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMinTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingMaxTransform(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingMaxTransform(), lag, out, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingQuantileTransform( - GroupedArrayHandle handle, int lag, int season_length, double p, - int window_size, int min_samples, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(SeasonalRollingQuantileTransform(), lag, out, - season_length, window_size, min_samples, p); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingMeanUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, - double *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMeanUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingStdUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, double *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingStdUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingMinUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, double *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMinUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingMaxUpdate(GroupedArrayHandle handle, - int lag, int season_length, - int window_size, - int min_samples, double *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingMaxUpdate(), 1, out, lag, season_length, - window_size, min_samples); - return 0; -} -int GroupedArrayFloat64_SeasonalRollingQuantileUpdate( - GroupedArrayHandle handle, int lag, int season_length, double p, - int window_size, int min_samples, double *out) { - - auto ga = reinterpret_cast *>(handle); - ga->Reduce(SeasonalRollingQuantileUpdate(), 1, out, lag, - season_length, window_size, min_samples, p); - return 0; +void GroupedArrayFloat64_SeasonalRollingMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMeanTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingStdTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingStdTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingMinTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMinTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingMaxTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingMaxTransform(), lag, out, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingQuantileTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, double p, int window_size, int min_samples, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(SeasonalRollingQuantileTransform(), lag, out, + season_length, window_size, min_samples, p); +} +void GroupedArrayFloat64_SeasonalRollingMeanUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMeanUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingStdUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingStdUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingMinUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMinUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingMaxUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, int window_size, int min_samples, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingMaxUpdate(), 1, out, lag, season_length, + window_size, min_samples); +} +void GroupedArrayFloat64_SeasonalRollingQuantileUpdate( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, int season_length, double p, int window_size, int min_samples, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(SeasonalRollingQuantileUpdate(), 1, out, lag, season_length, + window_size, min_samples, p); } // Expanding -int GroupedArrayFloat64_ExpandingMeanTransform(GroupedArrayHandle handle, +void GroupedArrayFloat64_ExpandingMeanTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, double *out, + double *agg) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.TransformAndReduce(ExpandingMeanTransform, lag, out, 1, agg); +} +void GroupedArrayFloat64_ExpandingStdTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int lag, double *out, double *agg) { - auto ga = reinterpret_cast *>(handle); - ga->TransformAndReduce(ExpandingMeanTransform, lag, out, 1, agg); - return 0; -} -int GroupedArrayFloat64_ExpandingStdTransform(GroupedArrayHandle handle, - int lag, double *out, - double *agg) { - auto ga = reinterpret_cast *>(handle); - ga->TransformAndReduce(ExpandingStdTransform, lag, out, 3, agg); - return 0; -} -int GroupedArrayFloat64_ExpandingMinTransform(GroupedArrayHandle handle, - int lag, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingMinTransform(), lag, out); - return 0; -} -int GroupedArrayFloat64_ExpandingMaxTransform(GroupedArrayHandle handle, - int lag, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingMaxTransform(), lag, out); - - return 0; -} -int GroupedArrayFloat64_ExpandingQuantileTransform(GroupedArrayHandle handle, - int lag, double p, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExpandingQuantileTransform, lag, out, p); - return 0; -} -int GroupedArrayFloat64_ExpandingQuantileUpdate(GroupedArrayHandle handle, - int lag, double p, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(ExpandingQuantileUpdate, 1, out, lag, p); - return 0; + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.TransformAndReduce(ExpandingStdTransform, lag, out, 3, agg); +} +void GroupedArrayFloat64_ExpandingMinTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingMinTransform(), lag, out); +} +void GroupedArrayFloat64_ExpandingMaxTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingMaxTransform(), lag, out); +} +void GroupedArrayFloat64_ExpandingQuantileTransform(const double *data, + const indptr_t *indptr, + int n_indptr, + int num_threads, int lag, + double p, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExpandingQuantileTransform, lag, out, p); +} +void GroupedArrayFloat64_ExpandingQuantileUpdate(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int lag, double p, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(ExpandingQuantileUpdate, 1, out, lag, p); } // Exponentially weighted -int GroupedArrayFloat64_ExponentiallyWeightedMeanTransform( - GroupedArrayHandle handle, int lag, double alpha, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(ExponentiallyWeightedMeanTransform, lag, out, alpha); - return 0; +void GroupedArrayFloat64_ExponentiallyWeightedMeanTransform( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + int lag, double alpha, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(ExponentiallyWeightedMeanTransform, lag, out, alpha); } // Scalers -int GroupedArrayFloat64_MinMaxScalerStats(GroupedArrayHandle handle, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(MinMaxScalerStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat64_StandardScalerStats(GroupedArrayHandle handle, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(StandardScalerStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat64_RobustIqrScalerStats(GroupedArrayHandle handle, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RobustScalerIqrStats, 2, out, 0); - return 0; -} -int GroupedArrayFloat64_RobustMadScalerStats(GroupedArrayHandle handle, +void GroupedArrayFloat64_MinMaxScalerStats(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(MinMaxScalerStats, 2, out, 0); +} +void GroupedArrayFloat64_StandardScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(RobustScalerMadStats, 2, out, 0); - return 0; + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(StandardScalerStats, 2, out, 0); } -int GroupedArrayFloat64_ScalerTransform(GroupedArrayHandle handle, - const double *stats, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(CommonScalerTransform, stats, out); - return 0; +void GroupedArrayFloat64_RobustIqrScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RobustScalerIqrStats, 2, out, 0); } -int GroupedArrayFloat64_ScalerInverseTransform(GroupedArrayHandle handle, - const double *stats, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(CommonScalerInverseTransform, stats, out); - return 0; +void GroupedArrayFloat64_RobustMadScalerStats(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(RobustScalerMadStats, 2, out, 0); } -int GroupedArrayFloat64_BoxCoxLambdaGuerrero(GroupedArrayHandle handle, - int period, double lower, - double upper, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(BoxCoxLambda_Guerrero, 2, out, 0, period, lower, upper); - return 0; +void GroupedArrayFloat64_ScalerTransform(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const double *stats, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(CommonScalerTransform, stats, out); } -void GroupedArrayFloat64_BoxCoxLambdaLogLik(GroupedArrayHandle handle, +void GroupedArrayFloat64_ScalerInverseTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + const double *stats, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(CommonScalerInverseTransform, stats, out); +} +void GroupedArrayFloat64_BoxCoxLambdaGuerrero(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + int period, double lower, + double upper, double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(BoxCoxLambda_Guerrero, 2, out, 0, period, lower, upper); +} +void GroupedArrayFloat64_BoxCoxLambdaLogLik(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, double lower, double upper, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(BoxCoxLambda_LogLik, 2, out, 0, lower, upper); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(BoxCoxLambda_LogLik, 2, out, 0, lower, upper); } -int GroupedArrayFloat64_BoxCoxTransform(GroupedArrayHandle handle, - const double *lambdas, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(BoxCoxTransform, lambdas, out); - return 0; +void GroupedArrayFloat64_BoxCoxTransform(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, const double *lambdas, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(BoxCoxTransform, lambdas, out); } -int GroupedArrayFloat64_BoxCoxInverseTransform(GroupedArrayHandle handle, - const double *lambdas, - double *out) { - auto ga = reinterpret_cast *>(handle); - ga->ScalerTransform(BoxCoxInverseTransform, lambdas, out); - return 0; +void GroupedArrayFloat64_BoxCoxInverseTransform(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, + const double *lambdas, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.ScalerTransform(BoxCoxInverseTransform, lambdas, out); } // Differences -void GroupedArrayFloat64_NumDiffs(GroupedArrayHandle handle, int max_d, +void GroupedArrayFloat64_NumDiffs(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumDiffs, 1, out, 0, max_d); -} -void GroupedArrayFloat64_NumSeasDiffs(GroupedArrayHandle handle, int period, - int max_d, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumSeasDiffs, 1, out, 0, period, max_d); -} -void GroupedArrayFloat64_NumSeasDiffsPeriods(GroupedArrayHandle handle, + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumDiffs, 1, out, 0, max_d); +} +void GroupedArrayFloat64_NumSeasDiffs(const double *data, + const indptr_t *indptr, int n_indptr, + int num_threads, int period, int max_d, + double *out) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumSeasDiffs, 1, out, 0, period, max_d); +} +void GroupedArrayFloat64_NumSeasDiffsPeriods(const double *data, + const indptr_t *indptr, + int n_indptr, int num_threads, int max_d, double *periods_and_out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(NumSeasDiffsPeriods, 2, periods_and_out, 0, max_d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(NumSeasDiffsPeriods, 2, periods_and_out, 0, max_d); } -void GroupedArrayFloat64_Period(GroupedArrayHandle handle, size_t max_lag, +void GroupedArrayFloat64_Period(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, size_t max_lag, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Reduce(GreatestAutocovariance, 1, out, 0, max_lag); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Reduce(GreatestAutocovariance, 1, out, 0, max_lag); } -void GroupedArrayFloat64_Difference(GroupedArrayHandle handle, int d, +void GroupedArrayFloat64_Difference(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, int d, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->Transform(Difference, 0, out, d); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.Transform(Difference, 0, out, d); } -void GroupedArrayFloat64_Differences(GroupedArrayHandle handle, +void GroupedArrayFloat64_Differences(const double *data, const indptr_t *indptr, + int n_indptr, int num_threads, const indptr_t *ds, double *out) { - auto ga = reinterpret_cast *>(handle); - ga->VariableTransform(Differences, ds, out); -} -void GroupedArrayFloat64_InvertDifferences(GroupedArrayHandle handle, - GroupedArrayHandle tails_handle, - const indptr_t *out_indptr, - double *out_data) { - auto ga = reinterpret_cast *>(handle); - auto tails_ga = reinterpret_cast *>(tails_handle); - ga->Zip(InvertDifference, tails_ga, out_indptr, out_data); + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + ga.VariableTransform(Differences, ds, out); +} +void GroupedArrayFloat64_InvertDifferences( + const double *data, const indptr_t *indptr, int n_indptr, int num_threads, + const double *other_data, const indptr_t *other_indptr, int other_n_indptr, + const indptr_t *out_indptr, double *out_data) { + auto ga = GroupedArray(data, indptr, n_indptr, num_threads); + auto tails_ga = GroupedArray(other_data, other_indptr, other_n_indptr, + num_threads); + ga.Zip(InvertDifference, tails_ga, out_indptr, out_data); } diff --git a/tests/test_scalers.py b/tests/test_scalers.py index 44e6bf9..01a927c 100644 --- a/tests/test_scalers.py +++ b/tests/test_scalers.py @@ -21,15 +21,17 @@ from coreforecast.seasonal import find_season_length +rng = np.random.default_rng(seed=0) + @pytest.fixture def indptr(): - lengths = np.random.randint(low=1_000, high=2_000, size=5_000) + lengths = rng.integers(low=1_000, high=2_000, size=5_000) return np.append(0, lengths.cumsum()).astype(np.int32) @pytest.fixture def data(indptr): - return np.random.randn(indptr[-1]) + return rng.normal(size=indptr[-1]) def std_scaler_stats(x):