NumCpp  2.1.0
A C++ implementation of the Python Numpy library
BoostInterface.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #ifdef INCLUDE_BOOST_PYTHON_INTERFACE
32 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/NdArray.hpp"
37 
38 #include "boost/python.hpp"
39 #include "boost/python/numpy.hpp"
40 
41 #include <map>
42 #include <string>
43 
44 namespace nc
45 {
46  namespace boostPythonInterface
47  {
48  //============================================================================
55  template<typename dtype>
56  inline NdArray<dtype> boost2Nc(const boost::python::numpy::ndarray& inArray)
57  {
58  BoostNdarrayHelper<dtype> helper(inArray);
59  if (helper.numDimensions() > 2)
60  {
61  THROW_RUNTIME_ERROR("Can only convert 1 and 2 dimensional arrays.");
62  }
63 
64  Shape arrayShape;
65  if (helper.numDimensions() == 1)
66  {
67  arrayShape.rows = 1;
68  arrayShape.cols = static_cast<uint32>(helper.shape().front());
69 
70  NdArray<dtype> returnArray(arrayShape);
71  for (uint32 i = 0; i < arrayShape.size(); ++i)
72  {
73  returnArray[i] = helper(i);
74  }
75 
76  return returnArray;
77  }
78 
79  arrayShape.rows = static_cast<uint32>(helper.shape().front());
80  arrayShape.cols = static_cast<uint32>(helper.shape()[1]);
81 
82  NdArray<dtype> returnArray(arrayShape);
83  for (uint32 row = 0; row < arrayShape.rows; ++row)
84  {
85  for (uint32 col = 0; col < arrayShape.cols; ++col)
86  {
87  returnArray(row, col) = helper(row, col);
88  }
89  }
90 
91  return returnArray;
92  }
93 
94  //============================================================================
101  template<typename dtype>
102  inline boost::python::numpy::ndarray nc2Boost(const NdArray<dtype>& inArray)
103  {
104  const Shape inShape = inArray.shape();
105  boost::python::tuple shape = boost::python::make_tuple(inShape.rows, inShape.cols);
106  BoostNdarrayHelper<dtype> newNdArrayHelper(shape);
107 
108  for (uint32 row = 0; row < inShape.rows; ++row)
109  {
110  for (uint32 col = 0; col < inShape.cols; ++col)
111  {
112  newNdArrayHelper(row, col) = inArray(row, col);
113  }
114  }
115  return newNdArrayHelper.getArray();
116  }
117 
118  //============================================================================
125  template<typename T>
126  inline std::vector<T> list2vector(const boost::python::list& inList)
127  {
128  return std::vector<T>(boost::python::stl_input_iterator<T>(inList), boost::python::stl_input_iterator<T>());
129  }
130 
131  //============================================================================
138  template <typename T>
139  inline boost::python::list vector2list(std::vector<T>& inVector)
140  {
141  boost::python::list outList;
142  for (auto& value : inVector)
143  {
144  outList.append(value);
145  }
146 
147  return outList;
148  }
149 
150  //============================================================================
157  template <class Key, class Value>
158  inline boost::python::dict map2dict(const std::map<Key, Value>& inMap)
159  {
160  boost::python::dict dictionary;
161  for (auto& keyValue : inMap)
162  {
163  dictionary[keyValue.first] = keyValue.second;
164  }
165  return dictionary;
166  }
167  } // namespace boostPythonInterface
168 } // namespace nc
169 #endif
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
Error.hpp
nc::shape
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:45
nc::boostPythonInterface::list2vector
std::vector< T > list2vector(const boost::python::list &inList)
Definition: BoostInterface.hpp:126
nc::boostPythonInterface::boost2Nc
NdArray< dtype > boost2Nc(const boost::python::numpy::ndarray &inArray)
Definition: BoostInterface.hpp:56
nc::NdArray< dtype >
nc::boostPythonInterface::BoostNdarrayHelper::shape
const std::vector< Py_intptr_t > & shape() noexcept
Definition: BoostNumpyNdarrayHelper.hpp:148
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::boostPythonInterface::BoostNdarrayHelper::getArray
const boost::python::numpy::ndarray & getArray() noexcept
Definition: BoostNumpyNdarrayHelper.hpp:118
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::boostPythonInterface::vector2list
boost::python::list vector2list(std::vector< T > &inVector)
Definition: BoostInterface.hpp:139
nc::boostPythonInterface::BoostNdarrayHelper
Helper class for ndarray.
Definition: BoostNumpyNdarrayHelper.hpp:53
BoostNumpyNdarrayHelper.hpp
nc::boostPythonInterface::map2dict
boost::python::dict map2dict(const std::map< Key, Value > &inMap)
Definition: BoostInterface.hpp:158
THROW_RUNTIME_ERROR
#define THROW_RUNTIME_ERROR(msg)
Definition: Error.hpp:38
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
nc::Shape::size
constexpr uint32 size() const noexcept
Definition: Core/Shape.hpp:103
nc::boostPythonInterface::BoostNdarrayHelper::numDimensions
uint8 numDimensions() noexcept
Definition: BoostNumpyNdarrayHelper.hpp:138
nc::boostPythonInterface::nc2Boost
boost::python::numpy::ndarray nc2Boost(const NdArray< dtype > &inArray)
Definition: BoostInterface.hpp:102