GeezerVison is showing github.com — Add an example for sharing the memory buffer of the xarray between C+… · DerThorsten/xtensor-python@7f96473
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ test/CMakeCache.txt | 39 | 39 | test/Makefile | 40 | 40 | test/CMakeFiles/ | 41 | 41 | test/cmake_install.cmake | | 42 | +.pytest_cache/ | 42 | 43 |
| 43 | 44 | # Documentation build artefacts | 44 | 45 | docs/CMakeCache.txt | |
Collapse file Copy file name to clipboardExpand all lines: CMakeLists.txt +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,8 +12,8 @@ project(xtensor-python) | 12 | 12 | set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) | 13 | 13 | set(XTENSOR_PYTHON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) | 14 | 14 |
| 15 | | -# Versionning | 16 | | -# =========== | | 15 | +# Versioning | | 16 | +# ========== | 17 | 17 |
| 18 | 18 | set(XTENSOR_PYTHON_CONFIG_FILE | 19 | 19 | "${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp") | |
Collapse file Copy file name to clipboardExpand all lines: include/xtensor-python/xtensor_type_caster_base.hpp +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,7 +39,7 @@ namespace pybind11 | 39 | 39 | std::transform(src.strides().begin(), src.strides().end(), | 40 | 40 | python_strides.begin(), [](auto v) { | 41 | 41 | return sizeof(typename Type::value_type) * v; | 42 | | - }); | | 42 | + }); | 43 | 43 |
| 44 | 44 | std::vector<std::size_t> python_shape(src.shape().size()); | 45 | 45 | std::copy(src.shape().begin(), src.shape().end(), python_shape.begin()); | @@ -162,6 +162,8 @@ namespace pybind11 | 162 | 162 | { | 163 | 163 | return _("xt::xtensor"); | 164 | 164 | } | | 165 | +#else | | 166 | + static constexpr auto name = _("xt::xtensor"); | 165 | 167 | #endif | 166 | 168 |
| 167 | 169 | template <typename T> | |
Collapse file Copy file name to clipboardExpand all lines: test_python/main.cpp +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -122,6 +122,16 @@ struct B | 122 | 122 | int b; | 123 | 123 | }; | 124 | 124 |
| | 125 | +class C | | 126 | +{ | | 127 | +public: | | 128 | + using array_type = xt::xarray<double, xt::layout_type::row_major>; | | 129 | + C() : m_array{0, 0, 0, 0} {} | | 130 | + array_type & array() { return m_array; } | | 131 | +private: | | 132 | + array_type m_array; | | 133 | +}; | | 134 | + | 125 | 135 | xt::pyarray<A> dtype_to_python() | 126 | 136 | { | 127 | 137 | A a1{123, 321, 'a', {1, 2, 3}}; | @@ -217,4 +227,16 @@ PYBIND11_MODULE(xtensor_python_test, m) | 217 | 227 |
| 218 | 228 | m.def("col_major_array", col_major_array); | 219 | 229 | m.def("row_major_tensor", row_major_tensor); | | 230 | + | | 231 | + py::class_<C>(m, "C") | | 232 | + .def(py::init<>()) | | 233 | + .def_property_readonly( | | 234 | + "copy", | | 235 | + [](C & self) { return self.array(); } | | 236 | + ) | | 237 | + .def_property_readonly( | | 238 | + "ref", | | 239 | + [](C & self) -> C::array_type & { return self.array(); } | | 240 | + ) | | 241 | + ; | 220 | 242 | } |
Collapse file Copy file name to clipboardExpand all lines: test_python/test_pyarray.py +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,3 +146,18 @@ def test_col_row_major(self): | 146 | 146 | varF = np.arange(50, dtype=float).reshape(2, 5, 5, order='F') | 147 | 147 | xt.col_major_array(varF) | 148 | 148 | xt.col_major_array(varF[:, :, 0]) # still col major! | | 149 | + | | 150 | +class AttributeTest(TestCase): | | 151 | + | | 152 | + def setUp(self): | | 153 | + self.c = xt.C() | | 154 | + | | 155 | + def test_copy(self): | | 156 | + arr = self.c.copy | | 157 | + arr[0] = 1 | | 158 | + self.assertEqual([0.]*4, self.c.copy.tolist()) | | 159 | + | | 160 | + def test_reference(self): | | 161 | + arr = self.c.ref | | 162 | + arr[0] = 1 | | 163 | + self.assertEqual([1.] + [0.]*3, self.c.ref.tolist()) |
|