NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanpercentile.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <string>
33 #include <vector>
34 
38 #include "NumCpp/Core/Shape.hpp"
39 #include "NumCpp/Core/Types.hpp"
44 #include "NumCpp/NdArray.hpp"
46 
47 namespace nc
48 {
49  //============================================================================
50  // Method Description:
61  template<typename dtype>
63  double inPercentile,
64  Axis inAxis = Axis::NONE,
65  const std::string& inInterpMethod = "linear")
66  {
67  STATIC_ASSERT_FLOAT(dtype);
68 
69  switch (inAxis)
70  {
71  case Axis::NONE:
72  {
73  std::vector<double> arrayCopy;
74  arrayCopy.reserve(inArray.size());
75  for (auto value : inArray)
76  {
77  if (!isnan(value))
78  {
79  arrayCopy.push_back(static_cast<double>(value));
80  }
81  }
82 
83  if (arrayCopy.empty())
84  {
85  NdArray<double> returnArray = { constants::nan };
86  return returnArray;
87  }
88 
89  return percentile(NdArray<double>(arrayCopy.data(),
90  static_cast<typename NdArray<dtype>::size_type>(arrayCopy.size()),
91  false),
92  inPercentile,
93  Axis::NONE,
94  inInterpMethod);
95  }
96  case Axis::COL:
97  {
98  const Shape inShape = inArray.shape();
99 
100  NdArray<double> returnArray(1, inShape.rows);
101  for (uint32 row = 0; row < inShape.rows; ++row)
102  {
103  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&inArray.front(row), inShape.cols),
104  inPercentile,
105  Axis::NONE,
106  inInterpMethod);
107 
108  if (outValue.size() == 1)
109  {
110  returnArray[row] = outValue.item();
111  }
112  else
113  {
114  returnArray[row] = constants::nan;
115  }
116  }
117 
118  return returnArray;
119  }
120  case Axis::ROW:
121  {
122  NdArray<dtype> arrayTrans = inArray.transpose();
123  const Shape inShape = arrayTrans.shape();
124 
125  NdArray<double> returnArray(1, inShape.rows);
126  for (uint32 row = 0; row < inShape.rows; ++row)
127  {
128  NdArray<double> outValue =
129  nanpercentile(NdArray<dtype>(&arrayTrans.front(row), inShape.cols, false),
130  inPercentile,
131  Axis::NONE,
132  inInterpMethod);
133 
134  if (outValue.size() == 1)
135  {
136  returnArray[row] = outValue.item();
137  }
138  else
139  {
140  returnArray[row] = constants::nan;
141  }
142  }
143 
144  return returnArray;
145  }
146  default:
147  {
148  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
149  return {}; // get rid of compiler warning
150  }
151  }
152 
153  return {}; // get rid of compiler warning
154  }
155 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:45
size_type size() const noexcept
Definition: NdArrayCore.hpp:4289
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4650
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2764
value_type item() const
Definition: NdArrayCore.hpp:2921
uint32 size_type
Definition: NdArrayCore.hpp:88
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:47
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:49
NdArray< double > nanpercentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: nanpercentile.hpp:62
NdArray< double > percentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: percentile.hpp:65
std::uint32_t uint32
Definition: Types.hpp:40