You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
10 KiB

8 years ago
  1. /*
  2. tests/test_numpy_array.cpp -- test core array functionality
  3. Copyright (c) 2016 Ivan Smirnov <i.s.smirnov@gmail.com>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include <pybind11/numpy.h>
  9. #include <pybind11/stl.h>
  10. #include <cstdint>
  11. #include <vector>
  12. using arr = py::array;
  13. using arr_t = py::array_t<uint16_t, 0>;
  14. static_assert(std::is_same<arr_t::value_type, uint16_t>::value, "");
  15. template<typename... Ix> arr data(const arr& a, Ix... index) {
  16. return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...));
  17. }
  18. template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
  19. return arr(a.size() - a.index_at(index...), a.data(index...));
  20. }
  21. arr& mutate_data(arr& a) {
  22. auto ptr = (uint8_t *) a.mutable_data();
  23. for (size_t i = 0; i < a.nbytes(); i++)
  24. ptr[i] = (uint8_t) (ptr[i] * 2);
  25. return a;
  26. }
  27. arr_t& mutate_data_t(arr_t& a) {
  28. auto ptr = a.mutable_data();
  29. for (size_t i = 0; i < a.size(); i++)
  30. ptr[i]++;
  31. return a;
  32. }
  33. template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
  34. auto ptr = (uint8_t *) a.mutable_data(index...);
  35. for (size_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
  36. ptr[i] = (uint8_t) (ptr[i] * 2);
  37. return a;
  38. }
  39. template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) {
  40. auto ptr = a.mutable_data(index...);
  41. for (size_t i = 0; i < a.size() - a.index_at(index...); i++)
  42. ptr[i]++;
  43. return a;
  44. }
  45. template<typename... Ix> size_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
  46. template<typename... Ix> size_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
  47. template<typename... Ix> size_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
  48. template<typename... Ix> size_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
  49. template<typename... Ix> size_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
  50. template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; }
  51. #define def_index_fn(name, type) \
  52. sm.def(#name, [](type a) { return name(a); }); \
  53. sm.def(#name, [](type a, int i) { return name(a, i); }); \
  54. sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \
  55. sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); });
  56. template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) {
  57. if (r.ndim() != 2) throw std::domain_error("error: ndim != 2");
  58. py::list l;
  59. l.append(*r.data(0, 0));
  60. l.append(*r2.mutable_data(0, 0));
  61. l.append(r.data(0, 1) == r2.mutable_data(0, 1));
  62. l.append(r.ndim());
  63. l.append(r.itemsize());
  64. l.append(r.shape(0));
  65. l.append(r.shape(1));
  66. l.append(r.size());
  67. l.append(r.nbytes());
  68. return l.release();
  69. }
  70. test_initializer numpy_array([](py::module &m) {
  71. auto sm = m.def_submodule("array");
  72. sm.def("ndim", [](const arr& a) { return a.ndim(); });
  73. sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); });
  74. sm.def("shape", [](const arr& a, size_t dim) { return a.shape(dim); });
  75. sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); });
  76. sm.def("strides", [](const arr& a, size_t dim) { return a.strides(dim); });
  77. sm.def("writeable", [](const arr& a) { return a.writeable(); });
  78. sm.def("size", [](const arr& a) { return a.size(); });
  79. sm.def("itemsize", [](const arr& a) { return a.itemsize(); });
  80. sm.def("nbytes", [](const arr& a) { return a.nbytes(); });
  81. sm.def("owndata", [](const arr& a) { return a.owndata(); });
  82. def_index_fn(data, const arr&);
  83. def_index_fn(data_t, const arr_t&);
  84. def_index_fn(index_at, const arr&);
  85. def_index_fn(index_at_t, const arr_t&);
  86. def_index_fn(offset_at, const arr&);
  87. def_index_fn(offset_at_t, const arr_t&);
  88. def_index_fn(mutate_data, arr&);
  89. def_index_fn(mutate_data_t, arr_t&);
  90. def_index_fn(at_t, const arr_t&);
  91. def_index_fn(mutate_at_t, arr_t&);
  92. sm.def("make_f_array", [] {
  93. return py::array_t<float>({ 2, 2 }, { 4, 8 });
  94. });
  95. sm.def("make_c_array", [] {
  96. return py::array_t<float>({ 2, 2 }, { 8, 4 });
  97. });
  98. sm.def("wrap", [](py::array a) {
  99. return py::array(
  100. a.dtype(),
  101. std::vector<size_t>(a.shape(), a.shape() + a.ndim()),
  102. std::vector<size_t>(a.strides(), a.strides() + a.ndim()),
  103. a.data(),
  104. a
  105. );
  106. });
  107. struct ArrayClass {
  108. int data[2] = { 1, 2 };
  109. ArrayClass() { py::print("ArrayClass()"); }
  110. ~ArrayClass() { py::print("~ArrayClass()"); }
  111. };
  112. py::class_<ArrayClass>(sm, "ArrayClass")
  113. .def(py::init<>())
  114. .def("numpy_view", [](py::object &obj) {
  115. py::print("ArrayClass::numpy_view()");
  116. ArrayClass &a = obj.cast<ArrayClass&>();
  117. return py::array_t<int>({2}, {4}, a.data, obj);
  118. }
  119. );
  120. sm.def("function_taking_uint64", [](uint64_t) { });
  121. sm.def("isinstance_untyped", [](py::object yes, py::object no) {
  122. return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
  123. });
  124. sm.def("isinstance_typed", [](py::object o) {
  125. return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
  126. });
  127. sm.def("default_constructors", []() {
  128. return py::dict(
  129. "array"_a=py::array(),
  130. "array_t<int32>"_a=py::array_t<std::int32_t>(),
  131. "array_t<double>"_a=py::array_t<double>()
  132. );
  133. });
  134. sm.def("converting_constructors", [](py::object o) {
  135. return py::dict(
  136. "array"_a=py::array(o),
  137. "array_t<int32>"_a=py::array_t<std::int32_t>(o),
  138. "array_t<double>"_a=py::array_t<double>(o)
  139. );
  140. });
  141. // Overload resolution tests:
  142. sm.def("overloaded", [](py::array_t<double>) { return "double"; });
  143. sm.def("overloaded", [](py::array_t<float>) { return "float"; });
  144. sm.def("overloaded", [](py::array_t<int>) { return "int"; });
  145. sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; });
  146. sm.def("overloaded", [](py::array_t<long long>) { return "long long"; });
  147. sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; });
  148. sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; });
  149. sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; });
  150. sm.def("overloaded2", [](py::array_t<double>) { return "double"; });
  151. sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; });
  152. sm.def("overloaded2", [](py::array_t<float>) { return "float"; });
  153. // Only accept the exact types:
  154. sm.def("overloaded3", [](py::array_t<int>) { return "int"; }, py::arg().noconvert());
  155. sm.def("overloaded3", [](py::array_t<double>) { return "double"; }, py::arg().noconvert());
  156. // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but
  157. // rather that float gets converted via the safe (conversion to double) overload:
  158. sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; });
  159. sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; });
  160. // But we do allow conversion to int if forcecast is enabled (but only if no overload matches
  161. // without conversion)
  162. sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
  163. sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
  164. // Issue 685: ndarray shouldn't go to std::string overload
  165. sm.def("issue685", [](std::string) { return "string"; });
  166. sm.def("issue685", [](py::array) { return "array"; });
  167. sm.def("issue685", [](py::object) { return "other"; });
  168. sm.def("proxy_add2", [](py::array_t<double> a, double v) {
  169. auto r = a.mutable_unchecked<2>();
  170. for (size_t i = 0; i < r.shape(0); i++)
  171. for (size_t j = 0; j < r.shape(1); j++)
  172. r(i, j) += v;
  173. }, py::arg().noconvert(), py::arg());
  174. sm.def("proxy_init3", [](double start) {
  175. py::array_t<double, py::array::c_style> a({ 3, 3, 3 });
  176. auto r = a.mutable_unchecked<3>();
  177. for (size_t i = 0; i < r.shape(0); i++)
  178. for (size_t j = 0; j < r.shape(1); j++)
  179. for (size_t k = 0; k < r.shape(2); k++)
  180. r(i, j, k) = start++;
  181. return a;
  182. });
  183. sm.def("proxy_init3F", [](double start) {
  184. py::array_t<double, py::array::f_style> a({ 3, 3, 3 });
  185. auto r = a.mutable_unchecked<3>();
  186. for (size_t k = 0; k < r.shape(2); k++)
  187. for (size_t j = 0; j < r.shape(1); j++)
  188. for (size_t i = 0; i < r.shape(0); i++)
  189. r(i, j, k) = start++;
  190. return a;
  191. });
  192. sm.def("proxy_squared_L2_norm", [](py::array_t<double> a) {
  193. auto r = a.unchecked<1>();
  194. double sumsq = 0;
  195. for (size_t i = 0; i < r.shape(0); i++)
  196. sumsq += r[i] * r(i); // Either notation works for a 1D array
  197. return sumsq;
  198. });
  199. sm.def("proxy_auxiliaries2", [](py::array_t<double> a) {
  200. auto r = a.unchecked<2>();
  201. auto r2 = a.mutable_unchecked<2>();
  202. return auxiliaries(r, r2);
  203. });
  204. // Same as the above, but without a compile-time dimensions specification:
  205. sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) {
  206. auto r = a.mutable_unchecked();
  207. if (r.ndim() != 2) throw std::domain_error("error: ndim != 2");
  208. for (size_t i = 0; i < r.shape(0); i++)
  209. for (size_t j = 0; j < r.shape(1); j++)
  210. r(i, j) += v;
  211. }, py::arg().noconvert(), py::arg());
  212. sm.def("proxy_init3_dyn", [](double start) {
  213. py::array_t<double, py::array::c_style> a({ 3, 3, 3 });
  214. auto r = a.mutable_unchecked();
  215. if (r.ndim() != 3) throw std::domain_error("error: ndim != 3");
  216. for (size_t i = 0; i < r.shape(0); i++)
  217. for (size_t j = 0; j < r.shape(1); j++)
  218. for (size_t k = 0; k < r.shape(2); k++)
  219. r(i, j, k) = start++;
  220. return a;
  221. });
  222. sm.def("proxy_auxiliaries2_dyn", [](py::array_t<double> a) {
  223. return auxiliaries(a.unchecked(), a.mutable_unchecked());
  224. });
  225. sm.def("array_auxiliaries2", [](py::array_t<double> a) {
  226. return auxiliaries(a, a);
  227. });
  228. });