NumCpp  2.7.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 
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:
61  template<typename dtype>
62  NdArray<double> nanpercentile(const NdArray<dtype>& inArray, double inPercentile,
63  Axis inAxis = Axis::NONE, const std::string& inInterpMethod = "linear")
64  {
65  STATIC_ASSERT_FLOAT(dtype);
66 
67  switch (inAxis)
68  {
69  case Axis::NONE:
70  {
71  std::vector<double> arrayCopy;
72  arrayCopy.reserve(inArray.size());
73  for (auto value : inArray)
74  {
75  if (!isnan(value))
76  {
77  arrayCopy.push_back(static_cast<double>(value));
78  }
79  }
80 
81  if (arrayCopy.empty())
82  {
83  NdArray<double> returnArray = { constants::nan };
84  return returnArray;
85  }
86 
87  return percentile(NdArray<double>(arrayCopy.data(),
88  static_cast<NdArray<dtype>::size_type>(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
size_type size() const noexcept
Definition: NdArrayCore.hpp:4296
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2772
value_type item() const
Definition: NdArrayCore.hpp:2931
uint32 size_type
Definition: NdArrayCore.hpp:87
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: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