33//
44
55#include < pybind11/pybind11.h>
6+ #include < pybind11/stl.h>
7+ #include < pybind11/numpy.h>
68
79#include " ConfigAPR.h"
810#include " data_structures/APR/APR.hpp"
@@ -19,21 +21,60 @@ namespace py = pybind11;
1921// -------- Utility classes to be wrapped in python ----------------------------
2022template <typename T>
2123class AprToImg {
24+ PixelData <T> originalImage;
25+
26+ APR <T> apr;
27+
2228 PixelData <T> reconstructedImage;
2329
2430public:
2531 AprToImg () {}
2632 void read (const std::string &aAprFileName) {
27- APR <T> apr;
2833 apr.read_apr (aAprFileName);
29- ReconPatch r;
30- APRReconstruction ().interp_image_patch (apr, reconstructedImage, apr.particles_intensities , r);
34+ // ReconPatch r;
35+ // APRReconstruction().interp_image_patch(apr, reconstructedImage, apr.particles_intensities, r);
36+ }
37+
38+ T* pc_recon () {
39+ APRReconstruction ().interp_img (apr, reconstructedImage, apr.particles_intensities );
40+
41+ return reconstructedImage.mesh .get ();
42+ }
43+
44+ bool readArr (py::handle src, bool convert) {
45+
46+ /* Some sanity checks ... */
47+ if (!convert && !py::array_t <T>::check_ (src)) {
48+ std::cout << " failed type check" << std::endl;
49+ return false ;
50+ }
51+
52+ auto buf = py::array_t <T, py::array::c_style | py::array::forcecast>::ensure (src);
53+ if (!buf) {
54+ std::cout << " could not read buffer" << std::endl;
55+ return false ;
56+ }
57+
58+ auto dims = buf.ndim ();
59+ if (dims != 3 ) {
60+ std::cout << " failed dimension check" << std::endl;
61+ return false ;
62+ }
63+
64+ /* read in python array to originalImage */
65+ originalImage.init (buf.shape ()[0 ], buf.shape ()[1 ], buf.shape ()[2 ]);
66+
67+ for (int i=0 ; i<originalImage.mesh .size (); ++i) {
68+ originalImage.mesh [i] = buf.data ()[i] + 1 ;
69+ }
70+
71+ return true ;
3172 }
3273
33- T *data () {return reconstructedImage .mesh .get ();}
34- int height () const {return reconstructedImage .x_num ;}
35- int width () const {return reconstructedImage .y_num ;}
36- int depth () const {return reconstructedImage .z_num ;}
74+ T *data () {return originalImage .mesh .get ();}
75+ int height () const {return originalImage .x_num ;}
76+ int width () const {return originalImage .y_num ;}
77+ int depth () const {return originalImage .z_num ;}
3778};
3879
3980// -------- Templated wrapper -------------------------------------------------
@@ -44,9 +85,11 @@ void AddAprToImg(pybind11::module &m, const std::string &aTypeString) {
4485 py::class_<AprType>(m, typeStr.c_str (), py::buffer_protocol ())
4586 .def (py::init ())
4687 .def (" read" , &AprType::read, " Method to read HDF5 APR files" )
88+ .def (" reconstruct" , &AprType::pc_recon, " returns an image reconstructed from the APR" )
4789 .def (" width" , &AprType::width, " Returns number of columns (x)" )
4890 .def (" height" , &AprType::height, " Returns number of rows (y)" )
4991 .def (" depth" , &AprType::depth, " Returns depth (z)" )
92+ .def (" readArr" , &AprType::readArr, " reads in a python array" )
5093 .def_buffer ([](AprType &a) -> py::buffer_info{
5194 return py::buffer_info (
5295 a.data (),
0 commit comments