diff --git a/RELEASE.md b/RELEASE.md index 975a8a34..53ac567d 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -49,8 +49,9 @@ gone. Hits are returned in scan order. The threshold factors are ``set_seed_factor`` and ``set_neighbour_factor`` in C++ and the ``seed_factor`` and ``neighbour_factor`` properties in Python; they take - floating point values and the seed factor is configurable instead of - being fixed to 5. + floating point values for every pixel type, where the factor and its + product with the noise used to be truncated to the pixel type, and the + seed factor is configurable instead of being fixed to 5. The noise map setters are ``set_noise_map`` and ``clear_noise_map`` in C++; in Python the noise map is the ``noise_map`` property, ``None`` to return to the fixed threshold, and ``labeled`` is a property instead of diff --git a/include/aare/VarClusterFinder.hpp b/include/aare/VarClusterFinder.hpp index 7d2356a2..22a3f495 100644 --- a/include/aare/VarClusterFinder.hpp +++ b/include/aare/VarClusterFinder.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -27,7 +28,8 @@ namespace aare { * to the constructor for seeds and, once set_neighbour_threshold() is used, * a separate one for neighbours. With a noise map they are per-pixel * multiples of the noise: the seed factor times the noise for seeds and the - * neighbour factor times the noise for neighbours, both 5 by default. + * neighbour factor times the noise for neighbours, both 5 by default. The + * factors are floating point for every pixel type, see Factor. * * The clusters are collected in a Hits structure: one Hit per cluster with * the pixel count, the summed energy and the position and value of the @@ -95,6 +97,14 @@ template class VarClusterFinder { } }; + /** + * @brief Type of the threshold factors and of the thresholds derived + * from the noise map: the pixel type for floating point images, double + * for integer images so that fractional factors are not truncated and + * the product with the noise cannot overflow the pixel type. + */ + using Factor = std::conditional_t, T, double>; + private: struct FixedThreshold { T seed_value; @@ -105,10 +115,12 @@ template class VarClusterFinder { struct NoiseThreshold { const T *noise; - T seed_factor; - T neighbour_factor; - T seed(ssize_t i) const { return seed_factor * noise[i]; } - T neighbour(ssize_t i) const { return neighbour_factor * noise[i]; } + Factor seed_factor; + Factor neighbour_factor; + Factor seed(ssize_t i) const { return seed_factor * noise[i]; } + Factor neighbour(ssize_t i) const { + return neighbour_factor * noise[i]; + } }; static constexpr size_t initial_hit_capacity = 2000; @@ -120,8 +132,8 @@ template class VarClusterFinder { std::optional> noise_map_; // Label image template: -1 on masked pixels, 0 elsewhere std::optional> masked_labels_; - T seed_factor_ = 5; - T neighbour_factor_ = 5; + Factor seed_factor_ = 5; + Factor neighbour_factor_ = 5; int connectivity_ = 8; bool erase_clusters_ = false; NDArray labels_; @@ -225,15 +237,21 @@ template class VarClusterFinder { return neighbour_threshold_.value_or(threshold_); } - /** @brief Seed threshold in units of the noise map (default 5). */ - void set_seed_factor(T factor) { seed_factor_ = factor; } + /** + * @brief Seed threshold in units of the noise map (default 5), floating + * point for every pixel type. + */ + void set_seed_factor(Factor factor) { seed_factor_ = factor; } /** @brief The seed threshold factor. */ - T seed_factor() const { return seed_factor_; } + Factor seed_factor() const { return seed_factor_; } - /** @brief Neighbour threshold in units of the noise map (default 5). */ - void set_neighbour_factor(T factor) { neighbour_factor_ = factor; } + /** + * @brief Neighbour threshold in units of the noise map (default 5), + * floating point for every pixel type. + */ + void set_neighbour_factor(Factor factor) { neighbour_factor_ = factor; } /** @brief The neighbour threshold factor. */ - T neighbour_factor() const { return neighbour_factor_; } + Factor neighbour_factor() const { return neighbour_factor_; } /** * @brief Grow clusters through 4 or 8 connected neighbours (default 8). diff --git a/src/VarClusterFinder.test.cpp b/src/VarClusterFinder.test.cpp index 06c9b75b..88365c7f 100644 --- a/src/VarClusterFinder.test.cpp +++ b/src/VarClusterFinder.test.cpp @@ -13,7 +13,8 @@ using Hits = Finder::Hits; namespace { -std::set> pixels_of(const Hits &hits, size_t i) { +template +std::set> pixels_of(const H &hits, size_t i) { const auto &hit = hits.hits[i]; std::set> pixels; for (int64_t k = hit.offset; k < hit.offset + hit.size; ++k) @@ -233,6 +234,65 @@ TEST_CASE("Noise map thresholds: seeds and neighbours use their own factors", } } +TEST_CASE("Noise factors are floating point for integer images", + "[varcluster]") { + SECTION("A fractional seed factor is not truncated") { + // Noise 2 and seed factor 2.5 give a threshold of 5: the pixel of + // value 5 is not above it, the pixel of value 6 is. A factor + // truncated to 2 would accept both. + NDArray img({4, 4}, 0); + img(1, 1) = 5; + img(2, 3) = 6; + NDArray noise({4, 4}, 2); + VarClusterFinder finder({4, 4}, 1); + finder.set_noise_map(noise.view()); + finder.set_seed_factor(2.5); + CHECK(finder.seed_factor() == 2.5); + finder.find_clusters(img.view()); + auto hits = finder.steal_hits(); + REQUIRE(hits.size() == 1); + CHECK(hits.hits[0].row == 2); + CHECK(hits.hits[0].col == 3); + CHECK(hits.hits[0].max == 6); + } + SECTION("A fractional neighbour factor is not truncated") { + // Noise 2 and neighbour factor 1.5 give a threshold of 3: the + // neighbour of value 4 joins the cluster, the one of value 3 does + // not. A factor truncated to 1 would take both. + NDArray img({4, 4}, 0); + img(1, 1) = 20; + img(1, 2) = 3; + img(2, 1) = 4; + NDArray noise({4, 4}, 2); + VarClusterFinder finder({4, 4}, 1); + finder.set_noise_map(noise.view()); + finder.set_neighbour_factor(1.5); + CHECK(finder.neighbour_factor() == 1.5); + finder.find_clusters(img.view()); + auto hits = finder.steal_hits(); + REQUIRE(hits.size() == 1); + CHECK(hits.hits[0].size == 2); + CHECK(pixels_of(hits, 0) == + std::set>{{1, 1}, {2, 1}}); + CHECK(hits.hits[0].energy == 24); + } + SECTION("The threshold does not wrap around in a narrow pixel type") { + // 5 times a noise of 20000 is 100000, which does not fit uint16_t. + // Wrapped to the pixel type it would be 34464, below the pixel. + NDArray img({4, 4}, 0); + img(1, 1) = 40000; + VarClusterFinder finder({4, 4}, 1); + NDArray noise({4, 4}, 20000); + finder.set_noise_map(noise.view()); + finder.find_clusters(img.view()); + CHECK(finder.total_clusters() == 0); + NDArray small_noise({4, 4}, 2000); // threshold 10000 + finder.set_noise_map(small_noise.view()); + finder.find_clusters(img.view()); + CHECK(finder.total_clusters() == 1); + } +} + TEST_CASE("Large clusters store every pixel", "[varcluster]") { NDArray img({10, 10}, 100.0); img(7, 2) = 300.0;