NumCpp  2.4.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
nanpercentile.hpp
Go to the documentation of this file.
1 #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), inPercentile, Axis::NONE, inInterpMethod);
89  }
90  case Axis::COL:
91  {
92  const Shape inShape = inArray.shape();
93 
94  NdArray<double> returnArray(1, inShape.rows);
95  for (uint32 row = 0; row < inShape.rows; ++row)
96  {
97  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&inArray.front(row), inShape.cols),
98  inPercentile, Axis::NONE, inInterpMethod);
99 
100  if (outValue.size() == 1)
101  {
102  returnArray[row] = outValue.item();
103  }
104  else
105  {
106  returnArray[row] = constants::nan;
107  }
108  }
109 
110  return returnArray;
111  }
112  case Axis::ROW:
113  {
114  NdArray<dtype> arrayTrans = inArray.transpose();
115  const Shape inShape = arrayTrans.shape();
116 
117  NdArray<double> returnArray(1, inShape.rows);
118  for (uint32 row = 0; row < inShape.rows; ++row)
119  {
120  NdArray<double> outValue = nanpercentile(NdArray<dtype>(&arrayTrans.front(row), inShape.cols, false),
121  inPercentile, Axis::NONE, inInterpMethod);
122 
123  if (outValue.size() == 1)
124  {
125  returnArray[row] = outValue.item();
126  }
127  else
128  {
129  returnArray[row] = constants::nan;
130  }
131  }
132 
133  return returnArray;
134  }
135  default:
136  {
137  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
138  return {}; // get rid of compiler warning
139  }
140  }
141 
142  return {}; // get rid of compiler warning
143  }
144 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
size_type size() const noexcept
Definition: NdArrayCore.hpp:4373
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4359
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4655
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2809
value_type item() const
Definition: NdArrayCore.hpp:2978
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