NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanpercentile.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
39 #include "NumCpp/NdArray.hpp"
41 
42 #include <algorithm>
43 #include <cmath>
44 #include <string>
45 #include <vector>
46 
47 namespace nc
48 {
49  //============================================================================
50  // Method Description:
62  template<typename dtype>
63  NdArray<double> nanpercentile(const NdArray<dtype>& inArray, double inPercentile,
64  Axis inAxis = Axis::NONE, const std::string& inInterpMethod = "linear")
65  {
66  STATIC_ASSERT_FLOAT(dtype);
67 
68  switch (inAxis)
69  {
70  case Axis::NONE:
71  {
72  std::vector<double> arrayCopy;
73  arrayCopy.reserve(inArray.size());
74  for (auto value : inArray)
75  {
76  if (!isnan(value))
77  {
78  arrayCopy.push_back(static_cast<double>(value));
79  }
80  }
81 
82  if (arrayCopy.empty())
83  {
84  NdArray<double> returnArray = { constants::nan };
85  return returnArray;
86  }
87 
88  return percentile(NdArray<double>(arrayCopy.data(), arrayCopy.size(), false),
89  inPercentile, Axis::NONE, inInterpMethod);
90  }
91  case Axis::COL:
92  {
93  const Shape inShape = inArray.shape();
94 
95  NdArray<double> returnArray(1, inShape.rows);
96  for (uint32 row = 0; row < inShape.rows; ++row)
97  {
98  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&inArray.front(row), inShape.cols),
99  inPercentile, Axis::NONE, inInterpMethod);
100 
101  if (outValue.size() == 1)
102  {
103  returnArray[row] = outValue.item();
104  }
105  else
106  {
107  returnArray[row] = constants::nan;
108  }
109  }
110 
111  return returnArray;
112  }
113  case Axis::ROW:
114  {
115  NdArray<dtype> arrayTrans = inArray.transpose();
116  const Shape inShape = arrayTrans.shape();
117 
118  NdArray<double> returnArray(1, inShape.rows);
119  for (uint32 row = 0; row < inShape.rows; ++row)
120  {
121  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&arrayTrans.front(row), inShape.cols, false),
122  inPercentile, Axis::NONE, inInterpMethod);
123 
124  if (outValue.size() == 1)
125  {
126  returnArray[row] = outValue.item();
127  }
128  else
129  {
130  returnArray[row] = constants::nan;
131  }
132  }
133 
134  return returnArray;
135  }
136  default:
137  {
138  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
139  return {}; // get rid of compiler warning
140  }
141  }
142 
143  return {}; // get rid of compiler warning
144  }
145 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4497
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4841
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2933
value_type item() const
Definition: NdArrayCore.hpp:3102
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
const double nan
NaN.
Definition: Constants.hpp:44
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:51
NdArray< double > nanpercentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: nanpercentile.hpp:63
NdArray< double > percentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: percentile.hpp:66
std::uint32_t uint32
Definition: Types.hpp:40