avoid truncating for integer images when comparing to threshold
Build on RHEL9 / build (push) Successful in 2m58s
Build on RHEL8 / build (push) Successful in 3m42s
Run tests using data on local RHEL8 / build (push) Successful in 4m45s

This commit is contained in:
Erik Fröjdh committed 2026-11-04 17:45:52 +01:00
1 parent c085846862
commit bfd963e6ca
3 files changed
+95 -16

No files matched your search

+3 -2
View File
@@ -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
+31 -13
View File
@@ -11,6 +11,7 @@
#include <limits>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
@@ -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 <typename T> 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<std::is_floating_point_v<T>, T, double>;
private:
struct FixedThreshold {
T seed_value;
@@ -105,10 +115,12 @@ template <typename T> 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 <typename T> class VarClusterFinder {
std::optional<NDArray<T, 2>> noise_map_;
// Label image template: -1 on masked pixels, 0 elsewhere
std::optional<NDArray<int32_t, 2>> 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<int32_t, 2> labels_;
@@ -225,15 +237,21 @@ template <typename T> 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).
+61 -1
View File
@@ -13,7 +13,8 @@ using Hits = Finder::Hits;
namespace {
std::set<std::pair<int, int>> pixels_of(const Hits &hits, size_t i) {
template <class H>
std::set<std::pair<int, int>> pixels_of(const H &hits, size_t i) {
const auto &hit = hits.hits[i];
std::set<std::pair<int, int>> 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<int32_t, 2> img({4, 4}, 0);
img(1, 1) = 5;
img(2, 3) = 6;
NDArray<int32_t, 2> noise({4, 4}, 2);
VarClusterFinder<int32_t> 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<int32_t, 2> img({4, 4}, 0);
img(1, 1) = 20;
img(1, 2) = 3;
img(2, 1) = 4;
NDArray<int32_t, 2> noise({4, 4}, 2);
VarClusterFinder<int32_t> 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<std::pair<int, int>>{{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<uint16_t, 2> img({4, 4}, 0);
img(1, 1) = 40000;
VarClusterFinder<uint16_t> finder({4, 4}, 1);
NDArray<uint16_t, 2> noise({4, 4}, 20000);
finder.set_noise_map(noise.view());
finder.find_clusters(img.view());
CHECK(finder.total_clusters() == 0);
NDArray<uint16_t, 2> 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<double, 2> img({10, 10}, 100.0);
img(7, 2) = 300.0;