diff --git a/include/aare/PixelHistogram.hpp b/include/aare/PixelHistogram.hpp index 17f3408..c37cf34 100644 --- a/include/aare/PixelHistogram.hpp +++ b/include/aare/PixelHistogram.hpp @@ -28,6 +28,8 @@ class PixelHistogram { int rows_; int cols_; int n_threads_; + const AxisType xmin_; + const AxisType xmax_; std::vector partial_hists_; // Thread pool members @@ -35,7 +37,7 @@ class PixelHistogram { std::mutex work_mutex_; std::condition_variable work_cv_; std::condition_variable done_cv_; - const NDView* current_image_; + const NDView* current_image_; std::atomic completed_threads_; std::atomic stop_workers_; int work_generation_; @@ -48,7 +50,7 @@ class PixelHistogram { public: PixelHistogram(int rows, int cols, int n_bins, double xmin, double xmax, int n_threads = 1); ~PixelHistogram(); - void fill(const NDView &image); + void fill(const NDView &image); NDArray hdata() const; NDArray bin_centers() const; NDArray bin_edges() const; diff --git a/python/src/bind_PixelHistogram.hpp b/python/src/bind_PixelHistogram.hpp index 65b2451..cdbf6aa 100644 --- a/python/src/bind_PixelHistogram.hpp +++ b/python/src/bind_PixelHistogram.hpp @@ -30,7 +30,7 @@ void define_pixel_histogram_bindings(py::module &m) { .def("fill", [](PixelHistogram &self, - py::array_t image) { + py::array_t image) { auto view = make_view_2d(image); self.fill(view); }, @@ -38,9 +38,9 @@ void define_pixel_histogram_bindings(py::module &m) { Fill the histogram with image data. Args: - image: A 2D numpy array of pixel values (dtype: float64) + image: A 2D numpy array of pixel values (dtype: float32) )", - py::arg("image")) + py::arg("image").noconvert()) .def("hdata", [](const PixelHistogram &self) { diff --git a/src/PixelHistogram.cpp b/src/PixelHistogram.cpp index 7b5da28..8f54b8e 100644 --- a/src/PixelHistogram.cpp +++ b/src/PixelHistogram.cpp @@ -12,7 +12,7 @@ namespace aare { PixelHistogram::PixelHistogram(int rows, int cols, int n_bins, double xmin, double xmax, int n_threads): - rows_(rows), cols_(cols), n_threads_(n_threads), current_image_(nullptr), + rows_(rows), cols_(cols), n_threads_(n_threads), xmin_(xmin), xmax_(xmax), current_image_(nullptr), completed_threads_(0), stop_workers_(false), work_generation_(0) { if (rows_ < 1 || cols_ < 1 || n_bins < 1) { throw std::invalid_argument("PixelHistogram requires positive rows, cols and bins"); @@ -81,7 +81,7 @@ void PixelHistogram::worker_loop(int thread_id) { } // Get work assignment - const NDView& image = *current_image_; + const NDView& image = *current_image_; const int generation = work_generation_; const int first_row = row_start(thread_id); const int local_rows = row_count(thread_id); @@ -92,7 +92,11 @@ void PixelHistogram::worker_loop(int thread_id) { for (int local_row = 0; local_row < local_rows; ++local_row) { const auto row = static_cast(first_row + local_row); for (ssize_t col = 0; col < image.shape(1); ++col) { - partial_hists_[thread_id](image(row, col), col, local_row); + const auto val = image(row, col); + if (val < xmin_ || val >= xmax_) { + continue; // Skip out-of-range values + } + partial_hists_[thread_id](val, col, local_row); } } @@ -139,7 +143,7 @@ NDArray PixelHistogram::hdata() const { return data; } -void PixelHistogram::fill(const NDView &image) { +void PixelHistogram::fill(const NDView &image) { if (image.shape(0) != rows_ || image.shape(1) != cols_) { throw std::invalid_argument("PixelHistogram image shape does not match constructor shape"); } diff --git a/src/PixelHistogram.test.cpp b/src/PixelHistogram.test.cpp index 6daae60..c1acec9 100644 --- a/src/PixelHistogram.test.cpp +++ b/src/PixelHistogram.test.cpp @@ -13,7 +13,7 @@ using aare::NDView; TEST_CASE("Fill one pixel of a 5x10 histogram"){ PixelHistogram hist(5, 10, 20, 0.0, 10.0); - NDArray image({5, 10}, -1.0); //Need to fill with -1 to not generate counts + NDArray image({5, 10}, -1.0); //Need to fill with -1 to not generate counts image(2, 3) = 5.7; // This should go into bin 11 (since bins are [0-0.5), [0.5-1.0), ..., [9.5-10.0)) @@ -41,7 +41,7 @@ TEST_CASE("Fill one pixel of a 5x10 histogram"){ TEST_CASE("Fill pixels with uneven partial histogram row slices"){ PixelHistogram hist(5, 4, 10, 0.0, 10.0, 3); - NDArray image({5, 4}, -1.0); + NDArray image({5, 4}, -1.0); image(0, 0) = 0.2; image(1, 1) = 1.2;