Skip to content

Commit 2e3004a

Browse files
author
joeljonsson
committed
Added function to read in a numpy array as PixelData
1 parent 6a30b33 commit 2e3004a

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/wrapper/pythonBind.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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 ----------------------------
2022
template <typename T>
2123
class AprToImg {
24+
PixelData <T> originalImage;
25+
26+
APR <T> apr;
27+
2228
PixelData <T> reconstructedImage;
2329

2430
public:
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(),

test/pythonTest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
import pyApr
1111
import numpy as np
1212
apr=pyApr.AprShort()
13+
1314
apr.read(myPath + '/files/Apr/sphere_120/sphere_apr.h5')
15+
img=np.array(apr.reconstruct(), copy = False)
16+
print(img.shape)
17+
18+
img=np.arange(1000, dtype = np.uint16).reshape(10,10,10)
19+
20+
if(apr.readArr(img, False)):
21+
print('array successfully read into c++')
22+
else:
23+
print('Error: array could not be read into c++')
24+
25+
1426
img=np.array(apr, copy = False)
15-
assert img.shape == (120, 120, 120)
27+
print(img.shape)
28+
# plot a 2D slice of the image
29+

0 commit comments

Comments
 (0)