NumCpp  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  else
79  {
80  arrayShape.rows = static_cast<uint32>(helper.shape().front());
81  arrayShape.cols = static_cast<uint32>(helper.shape()[1]);
82 
83  NdArray<dtype> returnArray(arrayShape);
84  for (uint32 row = 0; row < arrayShape.rows; ++row)
85  {
86  for (uint32 col = 0; col < arrayShape.cols; ++col)
87  {
88  returnArray(row, col) = helper(row, col);
89  }
90  }
91 
92  return returnArray;
93  }
94  }
95 
96  //============================================================================
103  template<typename dtype>
104  inline boost::python::numpy::ndarray nc2Boost(const NdArray<dtype>& inArray)
105  {
106  const Shape inShape = inArray.shape();
107  boost::python::tuple shape = boost::python::make_tuple(inShape.rows, inShape.cols);
108  BoostNdarrayHelper<dtype> newNdArrayHelper(shape);
109 
110  for (uint32 row = 0; row < inShape.rows; ++row)
111  {
112  for (uint32 col = 0; col < inShape.cols; ++col)
113  {
114  newNdArrayHelper(row, col) = inArray(row, col);
115  }
116  }
117  return newNdArrayHelper.getArray();
118  }
119 
120  //============================================================================
127  template<typename T>
128  inline std::vector<T> list2vector(const boost::python::list& inList)
129  {
130  return std::vector<T>(boost::python::stl_input_iterator<T>(inList), boost::python::stl_input_iterator<T>());
131  }
132 
133  //============================================================================
140  template <typename T>
141  inline boost::python::list vector2list(std::vector<T>& inVector)
142  {
143  boost::python::list outList;
144  for (auto& value : inVector)
145  {
146  outList.append(value);
147  }
148 
149  return outList;
150  }
151 
152  //============================================================================
159  template <class Key, class Value>
160  inline boost::python::dict map2dict(const std::map<Key, Value>& inMap)
161  {
162  boost::python::dict dictionary;
163  for (auto& keyValue : inMap)
164  {
165  dictionary[keyValue.first] = keyValue.second;
166  }
167  return dictionary;
168  }
169  }
170 }
171 #endif
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
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:128
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:141
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:160
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:104