From 029416e503c8794f285ccdb008cbb77a94f4024c Mon Sep 17 00:00:00 2001 From: Richard Gildea Date: Tue, 11 Apr 2023 09:58:15 +0100 Subject: [PATCH 01/45] flumpy: add support for flex.miller_index (#618) - Add flumpy.to_numpy support for flex.miller_index - Add flumpy.miller_index_from_numpy Co-authored-by: Nicholas Devenish --- newsfragments/618.feature | 1 + src/dxtbx/boost_python/flumpy.cc | 61 ++++++++++++++++++++++++++++---- tests/test_flumpy.py | 39 ++++++++++++++++++-- 3 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 newsfragments/618.feature diff --git a/newsfragments/618.feature b/newsfragments/618.feature new file mode 100644 index 000000000..ebb9e858e --- /dev/null +++ b/newsfragments/618.feature @@ -0,0 +1 @@ +``flumpy``: Add support for conversion of ``flex.miller_index`` arrays to/from numpy. diff --git a/src/dxtbx/boost_python/flumpy.cc b/src/dxtbx/boost_python/flumpy.cc index a289f4acd..12e5ebf94 100644 --- a/src/dxtbx/boost_python/flumpy.cc +++ b/src/dxtbx/boost_python/flumpy.cc @@ -24,6 +24,7 @@ #include #include #include +#include using boost::optional; using std::cout; @@ -43,7 +44,8 @@ using grid = af::versa>; grid, grid, grid, grid, grid, \ grid, grid, grid>, grid>, \ grid>, grid>, \ - grid>, grid> + grid>, grid>, \ + grid> // Unwrapped, and possibly doesn't make sense: // void wrap_flex_std_string(); - differently sized per element // void wrap_flex_sym_mat3_double(); - nonlinear memory layout @@ -99,6 +101,31 @@ py::buffer_info get_buffer_specific(grid> flex) { strides); } +template +py::buffer_info get_buffer_specific(grid> flex) { + std::vector dim_sizes; + for (auto size : flex.accessor().all()) { + dim_sizes.push_back(size); + } + dim_sizes.push_back(3); + + std::vector strides; + for (int i = 0; i < dim_sizes.size(); ++i) { + auto stride = sizeof(T); + for (int j = i + 1; j < dim_sizes.size(); ++j) { + stride *= dim_sizes[j]; + } + strides.push_back(stride); + } + + return py::buffer_info(&flex.front(), + sizeof(T), + py::format_descriptor::format(), + dim_sizes.size(), + dim_sizes, + strides); +} + template py::buffer_info get_buffer_specific(grid> flex) { std::vector dim_sizes; @@ -486,6 +513,11 @@ py::object from_numpy(py::object array) { /// More structured arrays need to be explicitly requested template