diff --git a/include/aare/VarClusterFinder.hpp b/include/aare/VarClusterFinder.hpp index dae45cb0..f259d037 100644 --- a/include/aare/VarClusterFinder.hpp +++ b/include/aare/VarClusterFinder.hpp @@ -49,7 +49,7 @@ template class VarClusterFinder { int check_neighbours(int i, int j); public: - VarClusterFinder(image_shape shape, T threshold) + VarClusterFinder(Shape<2> shape, T threshold) : shape_(shape), labeled_(shape, 0), peripheral_labeled_(shape, 0), binary_(shape), threshold_(threshold) { hits.reserve(2000); } diff --git a/python/aare/__init__.py b/python/aare/__init__.py index 8709bca3..c58d25ce 100644 --- a/python/aare/__init__.py +++ b/python/aare/__init__.py @@ -1,2 +1,3 @@ # Make the compiled classes that live in _aare available from aare. -from ._aare import File \ No newline at end of file +from ._aare import File +from ._aare import VarClusterFinder \ No newline at end of file diff --git a/python/src/module.cpp b/python/src/module.cpp index c6771b73..7d1d822b 100644 --- a/python/src/module.cpp +++ b/python/src/module.cpp @@ -1,5 +1,6 @@ #include "file.hpp" +#include "var_cluster.hpp" #include #include @@ -11,4 +12,5 @@ PYBIND11_MODULE(_aare, m) { define_file_io_bindings(m); + define_var_cluster_finder_bindings(m); } \ No newline at end of file diff --git a/python/src/np_helper.hpp b/python/src/np_helper.hpp index 3aa24cc7..e0c145bf 100644 --- a/python/src/np_helper.hpp +++ b/python/src/np_helper.hpp @@ -7,36 +7,37 @@ #include "aare/Frame.hpp" #include "aare/NDArray.hpp" +#include "aare/NDView.hpp" namespace py = pybind11; // Pass image data back to python as a numpy array -// template -// py::array return_image_data(pl::ImageData *image) { +template +py::array return_image_data(aare::NDArray *image) { -// py::capsule free_when_done(image, [](void *f) { -// pl::ImageData *foo = -// reinterpret_cast *>(f); -// delete foo; -// }); + py::capsule free_when_done(image, [](void *f) { + aare::NDArray *foo = + reinterpret_cast *>(f); + delete foo; + }); -// return py::array_t( -// image->shape(), // shape -// image->byte_strides(), // C-style contiguous strides for double -// image->data(), // the data pointer -// free_when_done); // numpy array references this parent -// } + return py::array_t( + image->shape(), // shape + image->byte_strides(), // C-style contiguous strides for double + image->data(), // the data pointer + free_when_done); // numpy array references this parent +} -// template py::array return_vector(std::vector *vec) { -// py::capsule free_when_done(vec, [](void *f) { -// std::vector *foo = reinterpret_cast *>(f); -// delete foo; -// }); -// return py::array_t({vec->size()}, // shape -// {sizeof(T)}, // C-style contiguous strides for double -// vec->data(), // the data pointer -// free_when_done); // numpy array references this parent -// } +template py::array return_vector(std::vector *vec) { + py::capsule free_when_done(vec, [](void *f) { + std::vector *foo = reinterpret_cast *>(f); + delete foo; + }); + return py::array_t({vec->size()}, // shape + {sizeof(T)}, // C-style contiguous strides for double + vec->data(), // the data pointer + free_when_done); // numpy array references this parent +} // template py::array do_read(Reader &r, size_t n_frames) { // py::array image; @@ -62,54 +63,54 @@ namespace py = pybind11; // return image; // } -py::array return_frame(pl::Frame *ptr) { - py::capsule free_when_done(ptr, [](void *f) { - pl::Frame *foo = reinterpret_cast(f); - delete foo; - }); +// py::array return_frame(pl::Frame *ptr) { +// py::capsule free_when_done(ptr, [](void *f) { +// pl::Frame *foo = reinterpret_cast(f); +// delete foo; +// }); - const uint8_t item_size = ptr->bytes_per_pixel(); - std::vector shape; - for (auto val : ptr->shape()) - if (val > 1) - shape.push_back(val); - - std::vector strides; - if (shape.size() == 1) - strides.push_back(item_size); - else if (shape.size() == 2) { - strides.push_back(item_size * shape[1]); - strides.push_back(item_size); - } - - if (item_size == 1) - return py::array_t( - shape, strides, - reinterpret_cast(ptr->data()), free_when_done); - else if (item_size == 2) - return py::array_t(shape, strides, - reinterpret_cast(ptr->data()), - free_when_done); - else if (item_size == 4) - return py::array_t(shape, strides, - reinterpret_cast(ptr->data()), - free_when_done); - return {}; -} +// const uint8_t item_size = ptr->bytes_per_pixel(); +// std::vector shape; +// for (auto val : ptr->shape()) +// if (val > 1) +// shape.push_back(val); + +// std::vector strides; +// if (shape.size() == 1) +// strides.push_back(item_size); +// else if (shape.size() == 2) { +// strides.push_back(item_size * shape[1]); +// strides.push_back(item_size); +// } + +// if (item_size == 1) +// return py::array_t( +// shape, strides, +// reinterpret_cast(ptr->data()), free_when_done); +// else if (item_size == 2) +// return py::array_t(shape, strides, +// reinterpret_cast(ptr->data()), +// free_when_done); +// else if (item_size == 4) +// return py::array_t(shape, strides, +// reinterpret_cast(ptr->data()), +// free_when_done); +// return {}; +// } // todo rewrite generic template auto get_shape_3d(py::array_t arr) { - return pl::Shape<3>{arr.shape(0), arr.shape(1), arr.shape(2)}; + return aare::Shape<3>{arr.shape(0), arr.shape(1), arr.shape(2)}; } -template auto make_span_3d(py::array_t arr) { - return pl::DataSpan(arr.mutable_data(), get_shape_3d(arr)); +template auto make_view_3d(py::array_t arr) { + return aare::NDView(arr.mutable_data(), get_shape_3d(arr)); } template auto get_shape_2d(py::array_t arr) { - return pl::Shape<2>{arr.shape(0), arr.shape(1)}; + return aare::Shape<2>{arr.shape(0), arr.shape(1)}; } -template auto make_span_2d(py::array_t arr) { - return pl::DataSpan(arr.mutable_data(), get_shape_2d(arr)); +template auto make_view_2d(py::array_t arr) { + return aare::NDView(arr.mutable_data(), get_shape_2d(arr)); } \ No newline at end of file diff --git a/python/src/var_cluster.hpp b/python/src/var_cluster.hpp new file mode 100644 index 00000000..cf62f02f --- /dev/null +++ b/python/src/var_cluster.hpp @@ -0,0 +1,43 @@ +#include "aare/VarClusterFinder.hpp" +#include "np_helper.hpp" +// #include "aare/defs.hpp" +// #include "aare/fClusterFileV2.hpp" + +#include +// #include +#include +// #include +#include +#include +// #include +// #include + + +namespace py = pybind11; +using namespace::aare; + + +void define_var_cluster_finder_bindings(py::module &m) { + py::class_>(m, "VarClusterFinder") + .def(py::init, double>()) + .def("labeled", + [](VarClusterFinder &self) { + auto ptr = new NDArray(self.labeled()); + return return_image_data(ptr); + }) + .def("find_clusters", + [](VarClusterFinder &self, + py::array_t + img) { + auto view = make_view_2d(img); + self.find_clusters(view); + }) + .def("steal_hits", + [](VarClusterFinder &self) { + auto ptr = new std::vector::Hit>( + self.steal_hits()); + return return_vector(ptr); + }) + .def("total_clusters", &VarClusterFinder::total_clusters); + +} \ No newline at end of file