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