From 46c6bc27d1abe0f02e570280a1bed48992a80aeb Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 18:48:52 -0400 Subject: [PATCH 01/98] BUG: Preserve fractional angles in AcosImageFilter Return float32 for integer input so fractional radians and NaN can be stored. Retain floating input types and array shapes. Document the type change and downstream effects. Add independent numeric checks and coverage for disk-backed arrays. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/AcosImageFilter.md | 39 ++- .../Filters/AcosImageFilter.cpp | 6 +- .../test/AcosImageFilterTest.cpp | 322 +++++++++++++++++- .../ImageProcessingConstants.hpp | 7 + 4 files changed, 368 insertions(+), 6 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/AcosImageFilter.md b/src/Plugins/NXImageProcessing/docs/AcosImageFilter.md index a6042bbe1c..d9e626a27a 100644 --- a/src/Plugins/NXImageProcessing/docs/AcosImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/AcosImageFilter.md @@ -1,19 +1,52 @@ # Acos Image Filter -Computes the arccosine of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `acos(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Acos Image Filter. +Computes the **principal arccosine**, `acos(x)`, for each value in the input image. The input is a dimensionless cosine value. The output is an angle in **radians**, from 0 to pi for valid input. It is not an angle in degrees or a normalized angle. + +Values in the closed interval `[-1, 1]` have a real angle. Values outside this interval, including positive and negative infinity, produce NaN (not a number). Incoming NaN also produces NaN. The filter does not clamp, replace or reject these values. + +### Output Type and Precision + +| Input type | Output type | Output bytes per value | +|---|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float32 | 4 | +| float32 | float32 | 4 | +| float64 | float64 | 8 | + +Integer inputs need floating output because their valid values `-1`, `0` and `1` produce pi, pi/2 and 0 radians. Integer output would lose the fractional part of these angles and could not store NaN. With unsigned integer input, only `0` and `1` are in the valid domain. + +The calculation uses double precision, then stores the result in the output type. For valid integer inputs, float32 introduces at most about `8.74e-8` radians of representation error. The filter preserves the input tuple shape and component shape. It processes each component separately. + +For integer input, output storage is four times the size of an 8-bit input, twice the size of a 16-bit input, equal to a 32-bit input, and half the size of a 64-bit input. Floating input keeps its existing storage width. + +### Migration and Downstream Use + +Existing pipelines with integer input now receive float32 angles. Previously, integer output discarded fractional angles: for example, `acos(0)` became 1 rather than pi/2 radians. Check downstream filters that require integer input and thresholds that relied on that truncation. For example, `acos(-1)` is pi and exceeds a threshold of 3.1 radians; the old integer result of 3 did not. This is a constructed example. + +Before converting the result to an integer type later in a pipeline, handle NaN and check the destination range. An unchecked conversion can reintroduce the unsafe behavior this floating output avoids. For valid integer inputs, direct zero/nonzero status is unchanged. + +## Algorithm + +The filter evaluates each value independently. Resident arrays use direct memory views. Out-of-core arrays use serial bulk reads and writes through working batches; parallel calculation uses owned buffers. Batch sizes follow the pointwise engine's memory target and store layout. A small image can fit in one batch. Output uses the input storage format and preserves its geometry. % Auto generated parameter table will be inserted here ## Example Pipelines +The following scalar values illustrate the result. These are examples, not named pipeline files. + +| Input cosine | Output angle (radians) | +|---|---| +| -1 | pi, approximately 3.14159265 | +| 0 | pi/2, approximately 1.57079633 | +| 1 | 0 | +| 2 | NaN | + ## License & Copyright Please see the description file distributed with this plugin. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp index 689cdf1475..98b12653e7 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp @@ -78,7 +78,8 @@ IFilter::PreflightResult AcosImageFilter::preflightImpl(const DataStructure& dat auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> AcosImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +90,8 @@ Result<> AcosImageFilter::executeImpl(DataStructure& dataStructure, const Argume const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const AcosOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp index 1d7be17401..67debe4b38 100644 --- a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp @@ -3,8 +3,324 @@ #include "NXImageProcessing/Filters/AcosImageFilter.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Filter/IFilter.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; +namespace +{ +DataPath runMappedIntegerAcos(DataStructure& dataStructure, const DataPath& input) +{ + const AcosImageFilter filter; + Arguments args; + args.insertOrAssign(AcosImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(AcosImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(AcosImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // A mapping regression must fail before invalid integer values can reach a NaN-to-integer cast. + REQUIRE(actionPtr->type() == DataType::float32); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + return input.replaceName("Output"); +} + +// Test-only transfer failures use the engine's existing forced-buffer option. These stores are resident; +// the separate SimplnxOoc test supplies the actual disk witness. +template +class AcosTransferStore : public DataStore +{ +public: + explicit AcosTransferStore(T initialValue) + : DataStore(ShapeType{1, 1, 4}, ShapeType{1}, initialValue) + { + } + ~AcosTransferStore() noexcept override = default; + AcosTransferStore(const AcosTransferStore&) = delete; + AcosTransferStore(AcosTransferStore&&) = delete; + AcosTransferStore& operator=(const AcosTransferStore&) = delete; + AcosTransferStore& operator=(AcosTransferStore&&) = delete; + + Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override + { + ++m_ReadCount; + if(m_FailRead) + { + return MakeErrorResult(-187401, "Acos test input transfer failed."); + } + return DataStore::copyIntoBuffer(start, buffer); + } + Result<> copyFromBuffer(usize start, nonstd::span buffer) override + { + ++m_WriteCount; + if(m_FailWrite) + { + return MakeErrorResult(-187402, "Acos test output transfer failed."); + } + return DataStore::copyFromBuffer(start, buffer); + } + + void setFailRead(bool failRead) noexcept + { + m_FailRead = failRead; + } + void setFailWrite(bool failWrite) noexcept + { + m_FailWrite = failWrite; + } + usize readCount() const noexcept + { + return m_ReadCount; + } + usize writeCount() const noexcept + { + return m_WriteCount; + } + +private: + bool m_FailRead = false; + bool m_FailWrite = false; + mutable usize m_ReadCount = 0; + usize m_WriteCount = 0; +}; +} // namespace + +// An angle at zero is pi/2 radians, independently of the input's integer width or signedness. +// Keeping the input at zero makes the unfixed integer-output implementation safe to execute. +TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer zero produces float32 radians", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {0}); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(AcosImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(AcosImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(AcosImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + + const AcosImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::float32); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 1}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(anglesPtr->getDataStoreRef().getValue(0) == Approx(1.5707963267948966).epsilon(0).margin(1.0e-7)); + + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: floating input retains type and propagates domain NaN", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage( + dataStructure, {-1, 0, 1, 2, -2, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 8}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = anglesPtr->getDataStoreRef(); + constexpr float64 angleTolerance = std::is_same_v ? 1.0e-7 : 1.0e-15; + REQUIRE(store.getValue(0) == Approx(3.14159265358979323846).epsilon(0).margin(angleTolerance)); + REQUIRE(store.getValue(1) == Approx(1.57079632679489661923).epsilon(0).margin(angleTolerance)); + REQUIRE(store.getValue(2) == 0); + for(usize index = 3; index < 8; ++index) + { + REQUIRE(std::isnan(store.getValue(index))); + } + + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + std::vector values = {0, 1, 2, std::numeric_limits::max()}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = runMappedIntegerAcos(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, values.size()}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(output)); + const auto& store = anglesPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(store.getValue(1) == 0.0f); + REQUIRE(std::isnan(store.getValue(2))); + REQUIRE(std::isnan(store.getValue(3))); + if constexpr(std::is_signed_v) + { + REQUIRE(store.getValue(4) == Approx(3.14159265358979323846).epsilon(0).margin(1.0e-7)); + REQUIRE(std::isnan(store.getValue(5))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + const TestType inside = std::nextafter(TestType{1}, TestType{0}); + const TestType outside = std::nextafter(TestType{1}, std::numeric_limits::infinity()); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {inside, -inside, outside, -outside}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 4}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = anglesPtr->getDataStoreRef(); + // The two interior values must give finite, non-endpoint angles; exterior values must not be clamped. + REQUIRE(store.getValue(0) > 0); + REQUIRE(store.getValue(0) < 1.0e-3); + REQUIRE(store.getValue(1) > 3.14); + REQUIRE(store.getValue(1) < 3.14159265358979323846); + REQUIRE(std::isnan(store.getValue(2))); + REQUIRE(std::isnan(store.getValue(3))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::AcosImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AcosImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(input)); + auto store = DataStoreUtilities::CreateDataStore(dataStructure, input, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(store != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(store); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + for(usize index = 0; index < 16; ++index) + { + store->setValue(index, static_cast(static_cast(index % 4) - 1)); + } + const DataPath output = runMappedIntegerAcos(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(output)); + const auto& result = anglesPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(result.getValue(index) == Approx(3.14159265358979323846).epsilon(0).margin(1.0e-7)); + REQUIRE(result.getValue(index + 1) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(result.getValue(index + 2) == 0.0f); + REQUIRE(std::isnan(result.getValue(index + 3))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::AcosImageFilter: mapped buffered engine preserves cancellation and I/O failures", "[ImageProcessing][Issue1874][AcosImageFilter]") +{ + UnitTest::LoadPlugins(); + using OutputType = ImageProcessing::IntegerToFloat32Type; + AcosTransferStore input(0); + AcosTransferStore output(-7.0f); + std::atomic_bool cancel = false; + ImageProcessing::detail::PointwiseExecutionOptions options; + options.forceBuffered = true; + const auto operation = [](int16 value) -> OutputType { return static_cast(std::acos(static_cast(value))); }; + SECTION("Precancel performs no transfers") + { + cancel = true; + const auto result = ImageProcessing::detail::ApplyPointwiseImpl(input, output, operation, cancel, IFilter::MessageHandler{}, options); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(input.readCount() == 0); + REQUIRE(output.writeCount() == 0); + } + SECTION("Read failure returns unchanged and performs no write") + { + input.setFailRead(true); + const auto result = ImageProcessing::detail::ApplyPointwiseImpl(input, output, operation, cancel, IFilter::MessageHandler{}, options); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -187401); + REQUIRE(input.readCount() == 1); + REQUIRE(output.writeCount() == 0); + } + SECTION("Write failure returns unchanged") + { + output.setFailWrite(true); + const auto result = ImageProcessing::detail::ApplyPointwiseImpl(input, output, operation, cancel, IFilter::MessageHandler{}, options); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -187402); + REQUIRE(input.readCount() == 1); + REQUIRE(output.writeCount() == 1); + } + for(usize index = 0; index < 4; ++index) + { + REQUIRE(output.getValue(index) == -7.0f); + } +} + // PART A domain edge (documents ITK-faithful behavior; does NOT "fix" it): acos is undefined for |x|>1. The op is // static_cast(std::acos(static_cast(x))) -- exactly ITK's equally-unguarded Acos functor -- so acos(2) // is IEEE NaN. This pins that observed output. @@ -17,8 +333,12 @@ TEST_CASE("NXImageProcessing::AcosImageFilter: acos of |x|>1 is NaN (domain edge const DataPath input = ip_test::BuildScalarValuesImage(ds, {2.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isnan(out.getValue(0))); + + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp index 710cd0f02b..da5aabbd52 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp @@ -6,6 +6,7 @@ #include "simplnx/Utilities/FilterUtilities.hpp" #include +#include namespace nx::core::ImageProcessing { @@ -40,6 +41,12 @@ inline constexpr int32 k_RelabelComponentTooManyObjects = -8590; template using SameAsInput = T; +/** + * @brief Maps integer input to float32 and retains floating input types. + */ +template +using IntegerToFloat32Type = std::conditional_t, float32, T>; + template using AlwaysFloat64 = float64; From 4c26545db35371dd033c9844baa102f47cf2e5f7 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 19:09:56 -0400 Subject: [PATCH 02/98] BUG: Preserve fractional angles in AsinImageFilter Return float32 for integer input so fractional radians and NaN can be stored. Retain floating input types and array shapes. Document the precision, storage and downstream effects. Add independent principal-angle checks and tests for multi-component arrays. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/AsinImageFilter.md | 41 +++- .../Filters/AsinImageFilter.cpp | 6 +- .../test/AsinImageFilterTest.cpp | 201 +++++++++++++++++- 3 files changed, 241 insertions(+), 7 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/AsinImageFilter.md b/src/Plugins/NXImageProcessing/docs/AsinImageFilter.md index cc94ce7f36..94b4a5fa67 100644 --- a/src/Plugins/NXImageProcessing/docs/AsinImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/AsinImageFilter.md @@ -1,19 +1,54 @@ # Asin Image Filter -Computes the arcsine of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `asin(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Asin Image Filter. +Computes the **principal arcsine**, `asin(x)`, for each value in the input image. The input is a dimensionless sine value. The output is an angle in **radians**, from -pi/2 to pi/2 for valid input. + +Values in the closed interval `[-1, 1]` have a real angle. Values outside this interval, including positive and negative infinity, produce NaN (not a number). Incoming NaN also produces NaN. The filter does not clamp, replace or reject these values. + +### Output Type and Precision + +| Input type | Output type | Output bytes per value | +|---|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float32 | 4 | +| float32 | float32 | 4 | +| float64 | float64 | 8 | + +Integer inputs need floating output because their valid values `-1`, `0` and `1` produce -pi/2, 0 and pi/2 radians. Integer output would lose the fractional part of these angles and could not store NaN. With unsigned integer input, only `0` and `1` are in the valid domain. + +The calculation uses double precision, then stores the result in the output type. For valid integer inputs, float32 introduces at most about `4.37e-8` radians of representation error. The filter preserves the input tuple shape and component shape. It processes each component separately. + +For integer input, output storage is four times the size of an 8-bit input, twice the size of a 16-bit input, equal to a 32-bit input, and half the size of a 64-bit input. Floating input keeps its existing storage width. + +### Migration and Downstream Use + +Existing pipelines with integer input now receive float32 angles. Previously, integer output discarded fractional angles: for example, `asin(1)` became 1 rather than pi/2 radians. Check downstream filters that require integer input and thresholds that relied on that truncation. For valid integer inputs, direct zero/nonzero status is unchanged. + +For example, an Asin result passed to [Acos Image Filter](AcosImageFilter.md) can change from the old quantized `1` to pi/2. Acos then produces NaN because pi/2 is outside its input domain `[-1, 1]`. This is a constructed composition example; it does not represent a reproduced user pipeline. Check the units and domain before using a radian angle as a dimensionless trigonometric input. + +Before converting the result to an integer type later in a pipeline, handle NaN and check the destination range. An unchecked conversion can reintroduce unsafe behavior. + +## Algorithm + +The filter evaluates each value independently. Resident arrays use direct memory views. Out-of-core arrays use serial bulk reads and writes through working batches; parallel calculation uses owned buffers. Batch sizes follow the pointwise engine's memory target and store layout. A small image can fit in one batch. Output uses the input storage format and preserves its geometry. % Auto generated parameter table will be inserted here ## Example Pipelines +The following scalar values illustrate the result. These are examples, not named pipeline files. + +| Input sine | Output angle (radians) | +|---|---| +| -1 | -pi/2, approximately -1.57079633 | +| 0 | 0 | +| 1 | pi/2, approximately 1.57079633 | +| 2 | NaN | + ## License & Copyright Please see the description file distributed with this plugin. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp index 612b3a741d..52643e6c1e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp @@ -78,7 +78,8 @@ IFilter::PreflightResult AsinImageFilter::preflightImpl(const DataStructure& dat auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> AsinImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +90,8 @@ Result<> AsinImageFilter::executeImpl(DataStructure& dataStructure, const Argume const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const AsinOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp index 498d46ddaf..d82922ed5d 100644 --- a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp @@ -3,8 +3,203 @@ #include "NXImageProcessing/Filters/AsinImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; +namespace +{ +DataPath runMappedIntegerAsin(DataStructure& dataStructure, const DataPath& inputPath, const ShapeType& tupleShape, const ShapeType& componentShape = {1}) +{ + const AsinImageFilter filter; + Arguments args; + args.insertOrAssign(AsinImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(AsinImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(AsinImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop an unfixed implementation before an invalid integer value can reach a NaN-to-integer conversion. + REQUIRE(actionPtr->type() == DataType::float32); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + return inputPath.replaceName("Output"); +} +} // namespace + +// The principal angle at integer one is pi/2 radians for every integer width and signedness. +TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer one produces float32 radians", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {1}); + const DataPath outputPath = runMappedIntegerAsin(dataStructure, inputPath, {1, 1, 1}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(anglesPtr->getDataStoreRef().getValue(0) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + std::vector inputValues = {0, 1, 2, std::numeric_limits::max()}; + if constexpr(std::is_signed_v) + { + inputValues.push_back(-1); + inputValues.push_back(std::numeric_limits::lowest()); + } + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, inputValues); + const DataPath outputPath = runMappedIntegerAsin(dataStructure, inputPath, {1, 1, inputValues.size()}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, inputValues.size()}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(outputPath)); + const auto& angleStore = anglesPtr->getDataStoreRef(); + REQUIRE(angleStore.getValue(0) == 0.0f); + REQUIRE(angleStore.getValue(1) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(std::isnan(angleStore.getValue(2))); + REQUIRE(std::isnan(angleStore.getValue(3))); + if constexpr(std::is_signed_v) + { + REQUIRE(angleStore.getValue(4) == Approx(-1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(std::isnan(angleStore.getValue(5))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: floating input retains type and principal angles", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {-1, -0.5, -TestType{0}, TestType{0}, 0.5, 1, 2, -2, std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), -std::numeric_limits::infinity()}); + const DataPath outputPath = ip_test::RunPointwiseFilter(dataStructure, inputPath); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 11}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& angleStore = anglesPtr->getDataStoreRef(); + constexpr float64 angleTolerance = std::is_same_v ? 1.0e-7 : 1.0e-15; + REQUIRE(angleStore.getValue(0) == Approx(-1.57079632679489661923).epsilon(0).margin(angleTolerance)); + REQUIRE(angleStore.getValue(1) == Approx(-0.52359877559829887308).epsilon(0).margin(angleTolerance)); + REQUIRE(angleStore.getValue(2) == 0); + REQUIRE(std::signbit(angleStore.getValue(2))); + REQUIRE(angleStore.getValue(3) == 0); + REQUIRE_FALSE(std::signbit(angleStore.getValue(3))); + REQUIRE(angleStore.getValue(4) == Approx(0.52359877559829887308).epsilon(0).margin(angleTolerance)); + REQUIRE(angleStore.getValue(5) == Approx(1.57079632679489661923).epsilon(0).margin(angleTolerance)); + for(usize index = 6; index < 11; ++index) + { + REQUIRE(std::isnan(angleStore.getValue(index))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + const TestType insideValue = std::nextafter(TestType{1}, TestType{0}); + const TestType outsideValue = std::nextafter(TestType{1}, std::numeric_limits::infinity()); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {insideValue, -insideValue, outsideValue, -outsideValue}); + const DataPath outputPath = ip_test::RunPointwiseFilter(dataStructure, inputPath); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 4}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& angleStore = anglesPtr->getDataStoreRef(); + REQUIRE(angleStore.getValue(0) > 1.57); + REQUIRE(angleStore.getValue(0) < 1.57079632679489661923); + REQUIRE(angleStore.getValue(1) < -1.57); + REQUIRE(angleStore.getValue(1) > -1.57079632679489661923); + REQUIRE(std::isnan(angleStore.getValue(2))); + REQUIRE(std::isnan(angleStore.getValue(3))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::AsinImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AsinImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(inputPath)); + auto inputStore = DataStoreUtilities::CreateDataStore(dataStructure, inputPath, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(inputStore != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(inputStore); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + constexpr std::array inputValues = {-1, 0, 1, 2}; + for(usize index = 0; index < 16; ++index) + { + inputStore->setValue(index, inputValues.at(index % inputValues.size())); + } + const DataPath outputPath = runMappedIntegerAsin(dataStructure, inputPath, {2, 2, 2}, {2}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float32Array* anglesPtr = nullptr; + REQUIRE_NOTHROW(anglesPtr = &dataStructure.getDataRefAs(outputPath)); + const auto& angleStore = anglesPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(angleStore.getValue(index) == Approx(-1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(angleStore.getValue(index + 1) == 0.0f); + REQUIRE(angleStore.getValue(index + 2) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); + REQUIRE(std::isnan(angleStore.getValue(index + 3))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + // PART A domain edge (documents ITK-faithful behavior): asin is undefined for |x|>1. The op is // static_cast(std::asin(static_cast(x))) -- ITK's equally-unguarded Asin functor -- so asin(2) is NaN. TEST_CASE("NXImageProcessing::AsinImageFilter: asin of |x|>1 is NaN (domain edge)", "[NXImageProcessing][AsinImageFilter]") @@ -16,8 +211,10 @@ TEST_CASE("NXImageProcessing::AsinImageFilter: asin of |x|>1 is NaN (domain edge const DataPath input = ip_test::BuildScalarValuesImage(ds, {2.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); - REQUIRE(std::isnan(out.getValue(0))); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + REQUIRE(std::isnan(outputArrayPtr->getDataStoreRef().getValue(0))); + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- From 64ed19b4d80bde1e2a8159ce593e80a8378ab639 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 19:49:25 -0400 Subject: [PATCH 03/98] BUG: Preserve Exp range and fractional results for integer input Use float64 output for integer input and preserve existing floating types. Handle float32 overflow before narrowing and document storage, precision, underflow and downstream mask changes. Add independent boundary and shape checks. Paired Release builds and 16 in-core / 20 disk-configuration cases pass. Configured clang-tidy remains nonpassing on existing/shared diagnostics; source review and clang-format 19.1.1 checks pass. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/CMakeLists.txt | 1 + .../NXImageProcessing/docs/ExpImageFilter.md | 34 +- .../Filters/ExpImageFilter.cpp | 11 +- .../utils/MathOutputUtilities.hpp | 36 +++ .../test/ExpImageFilterTest.cpp | 301 +++++++++++++++++- 5 files changed, 372 insertions(+), 11 deletions(-) create mode 100644 src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp diff --git a/src/Plugins/NXImageProcessing/CMakeLists.txt b/src/Plugins/NXImageProcessing/CMakeLists.txt index 90c6f8bb2d..b32628e482 100644 --- a/src/Plugins/NXImageProcessing/CMakeLists.txt +++ b/src/Plugins/NXImageProcessing/CMakeLists.txt @@ -144,6 +144,7 @@ set(PLUGIN_EXTRA_SOURCES "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/GaussianImageFilterExecutors.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/ComponentImageFilterExecutors.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/WatershedImageFilterExecutors.cpp" + "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/MathOutputUtilities.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/FijiMontageUtilities.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/FijiMontageUtilities.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/MetaImageUtilities.hpp" diff --git a/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md b/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md index 98e431cd2e..591004c781 100644 --- a/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md @@ -1,14 +1,42 @@ # Exp Image Filter -Computes the exponential of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `exp(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Exp Image Filter. +Computes the natural exponential, `exp(x) = e^x`, for each input value. The input and output are dimensionless. Positive inputs describe growth. Negative inputs give positive decay values below 1. Each component is processed separately. The output keeps the input tuple and component shapes. + +### Output type and precision + +| Input type | Output type | Output bytes per value | +|---|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float64 | 8 | +| float32 | float32 | 4 | +| float64 | float64 | 8 | + +Integer input uses float64 because an exponential can have fractional decay or large growth. For example, integer input -1 gives about 0.3678794412. Integer input 100 gives the finite float64 value about 2.6881171418 × 10^43. Float32 input 100 keeps float32 output and gives positive infinity. Output promotion depends on the input type, not its value. + +Float32 has about 7 significant decimal digits; float64 has about 16. Floating results are rounded to the output type. Float64 covers the full int8 and uint8 exponential range, but no finite output type covers all possible input values. Integer input 255 gives about 5.5602316477 × 10^110, which exceeds the float32 range. + +### Numeric limits + +- NaN (not a number) produces NaN. +- Positive infinity produces positive infinity. Negative infinity produces positive zero. +- Positive and negative zero both produce 1. +- Growth beyond the finite output range produces positive infinity. Finite double results above the float32 range are handled explicitly before conversion. +- Very small decay results can be **subnormal** values, which are positive values below the smallest normal floating value. Further underflow produces zero. Float32 input -128 produces zero; float64 or integer input -128 gives about 2.5722093726 × 10^-56. + +### Storage and downstream use + +For integer inputs, the 8-byte output uses 8, 4, 2, or 1 times the bytes of an 8-, 16-, 32-, or 64-bit input value. One billion uint8 input values require 1 GB, and their output values require 8 GB, before other storage. Floating input keeps its existing output width. + +Earlier integer output truncated positive decay values below 1 to zero. These values now remain positive until floating underflow occurs. Review thresholds and zero/nonzero masks that depended on that integer truncation. Downstream filters that accept only integers can reject the float64 output. An unchecked conversion back to integer can again lose decay values or exceed the integer range. + +## Algorithm + +The filter applies the same exponential operation to resident and disk-backed arrays. It uses the existing pointwise traversal and bounded transfer buffers for disk-backed storage. It does not create a full-array resident temporary for the calculation. Output storage still requires the bytes shown above. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp index d114f44c08..49f5101eac 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct ExpOperation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::exp(static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::exp(static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult ExpImageFilter::preflightImpl(const DataStructure& data auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> ExpImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> ExpImageFilter::executeImpl(DataStructure& dataStructure, const Argumen const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const ExpOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp new file mode 100644 index 0000000000..5c397b253c --- /dev/null +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "simplnx/Common/Types.hpp" + +#include +#include + +namespace nx::core::ImageProcessing +{ +/** @brief Preserve floating input types and store integer-input math results as float64. */ +template +using IntegerToFloat64Type = std::conditional_t, float64, T>; + +/** + * @brief Convert a floating math result, using signed infinity outside the float32 finite range. + * NaN, infinities, signed zero, and ordinary subnormal or zero underflow retain floating semantics. + */ +template +OutputType ConvertMathResult(float64 value) noexcept +{ + static_assert(std::is_same_v || std::is_same_v); + if constexpr(std::is_same_v) + { + constexpr float64 finiteLimit = std::numeric_limits::max(); + if(value > finiteLimit) + { + return std::numeric_limits::infinity(); + } + if(value < -finiteLimit) + { + return -std::numeric_limits::infinity(); + } + } + return static_cast(value); +} +} // namespace nx::core::ImageProcessing diff --git a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp index a55a003394..29611f6459 100644 --- a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp @@ -2,12 +2,300 @@ #include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/ExpImageFilter.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): the op is static_cast(std::exp(static_cast(x))). -// exp overflows double above ~709.78, so exp(1000) is +inf and casting +inf to float32 stays +inf. ITK's Exp -// functor is the same expression, so this pins the observed +inf overflow. +TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: integer input produces float64 growth and decay", "[NXImageProcessing][Issue1874][ExpImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + std::vector values = {0, 1, 100}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + values.push_back(std::numeric_limits::max()); + + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(ExpImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + + const ExpImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop the baseline before exp(100) can undergo an out-of-range conversion to an integer. + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, values.size()}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, values.size()}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float64Array* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 1.0); + REQUIRE(store.getValue(1) == Approx(2.71828182845904523536).epsilon(1.0e-15)); + REQUIRE(store.getValue(2) == Approx(2.68811714181613544841e43).epsilon(1.0e-15)); + if constexpr(std::is_signed_v) + { + REQUIRE(store.getValue(3) == Approx(0.36787944117144232160).epsilon(1.0e-15)); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(4) == Approx(2.57220937264241482684e-56).epsilon(1.0e-15)); + } + else + { + REQUIRE(store.getValue(4) == 0.0); + } + } + const float64 maximumResult = store.getValue(values.size() - 1); + if constexpr(std::is_same_v) + { + REQUIRE(maximumResult == Approx(1.43020799583481044656e55).epsilon(1.0e-15)); + } + else if constexpr(std::is_same_v) + { + REQUIRE(maximumResult == Approx(5.56023164772767512139e110).epsilon(1.0e-15)); + } + else + { + REQUIRE(std::isinf(maximumResult)); + REQUIRE(maximumResult > 0.0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values retain growth decay and range behavior", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), + -std::numeric_limits::infinity(), + TestType{0}, + -TestType{0}, + 100, + -128, + 1000, + -1000, + -100, + -103, + -104, + 10, + 88, + 89, + 709, + 710, + -744, + -745, + -746}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 20}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + REQUIRE(std::isnan(store.getValue(0))); + REQUIRE(store.getValue(1) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(2) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(2))); + REQUIRE(store.getValue(3) == 1); + REQUIRE(store.getValue(4) == 1); + REQUIRE(store.getValue(7) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(8) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(8))); + REQUIRE(store.getValue(16) == std::numeric_limits::infinity()); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(5) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(6) == 0); + REQUIRE(store.getValue(9) == 3.7835058536770060915e-44f); + REQUIRE(store.getValue(10) == std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(11) == 0); + REQUIRE(store.getValue(12) == Approx(22026.46579480671651696).epsilon(1.0e-7)); + REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-7)); + REQUIRE(store.getValue(14) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(15) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(17) == 0); + REQUIRE(store.getValue(18) == 0); + REQUIRE(store.getValue(19) == 0); + } + else + { + REQUIRE(store.getValue(5) == Approx(2.68811714181613544841e43).epsilon(1.0e-15)); + REQUIRE(store.getValue(6) == Approx(2.57220937264241482684e-56).epsilon(1.0e-15)); + REQUIRE(store.getValue(9) == Approx(3.72007597602083596296e-44).epsilon(1.0e-15)); + REQUIRE(store.getValue(10) == Approx(1.85211676951797542963e-45).epsilon(1.0e-15)); + REQUIRE(store.getValue(11) == Approx(6.81355682154529853676e-46).epsilon(1.0e-15)); + REQUIRE(store.getValue(12) == Approx(22026.46579480671651696).epsilon(1.0e-15)); + REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-15)); + REQUIRE(store.getValue(14) == Approx(4.48961281917434524628e38).epsilon(1.0e-15)); + REQUIRE(store.getValue(15) == Approx(8.21840746155497218924e307).epsilon(1.0e-15)); + REQUIRE(store.getValue(17) == 2 * std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(18) == std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(19) == 0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::ExpImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(input)); + auto inputStore = DataStoreUtilities::CreateDataStore(dataStructure, input, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(inputStore != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(inputStore); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + for(usize index = 0; index < 16; ++index) + { + inputStore->setValue(index, static_cast(static_cast(index % 4) - 1)); + } + + Arguments args; + args.insertOrAssign(ExpImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const ExpImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == ShapeType{2, 2, 2}); + REQUIRE(actionPtr->componentDims() == ShapeType{2}); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath output = input.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float64Array* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(store.getValue(index) == Approx(0.36787944117144232160).epsilon(1.0e-15)); + REQUIRE(store.getValue(index + 1) == 1.0); + REQUIRE(store.getValue(index + 2) == Approx(2.71828182845904523536).epsilon(1.0e-15)); + REQUIRE(store.getValue(index + 3) == Approx(7.38905609893065022723).epsilon(1.0e-15)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::ExpImageFilter: floating result conversion handles finite narrowing boundaries", "[NXImageProcessing][Issue1874][ExpImageFilter]") +{ + UnitTest::LoadPlugins(); + constexpr float64 finiteLimit = std::numeric_limits::max(); + const float64 outside = std::nextafter(finiteLimit, std::numeric_limits::infinity()); + const float64 inside = std::nextafter(finiteLimit, float64{0}); + REQUIRE(ImageProcessing::ConvertMathResult(inside) == std::numeric_limits::max()); + REQUIRE(ImageProcessing::ConvertMathResult(finiteLimit) == std::numeric_limits::max()); + REQUIRE(ImageProcessing::ConvertMathResult(outside) == std::numeric_limits::infinity()); + REQUIRE(ImageProcessing::ConvertMathResult(-inside) == -std::numeric_limits::max()); + REQUIRE(ImageProcessing::ConvertMathResult(-finiteLimit) == -std::numeric_limits::max()); + REQUIRE(ImageProcessing::ConvertMathResult(-outside) == -std::numeric_limits::infinity()); + REQUIRE(std::isnan(ImageProcessing::ConvertMathResult(std::numeric_limits::quiet_NaN()))); + REQUIRE(ImageProcessing::ConvertMathResult(std::numeric_limits::infinity()) == std::numeric_limits::infinity()); + REQUIRE(ImageProcessing::ConvertMathResult(-std::numeric_limits::infinity()) == -std::numeric_limits::infinity()); + REQUIRE(ImageProcessing::ConvertMathResult(0.0) == 0.0f); + REQUIRE_FALSE(std::signbit(ImageProcessing::ConvertMathResult(0.0))); + REQUIRE(std::signbit(ImageProcessing::ConvertMathResult(-0.0))); + REQUIRE(ImageProcessing::ConvertMathResult(std::numeric_limits::denorm_min()) == std::numeric_limits::denorm_min()); + REQUIRE(ImageProcessing::ConvertMathResult(outside) == outside); + REQUIRE(ImageProcessing::ConvertMathResult(std::numeric_limits::denorm_min()) == std::numeric_limits::denorm_min()); + REQUIRE(std::signbit(ImageProcessing::ConvertMathResult(-0.0))); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {-1, 0, 1}); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(ExpImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + + const ExpImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 3}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 3}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + constexpr float64 relativeTolerance = std::is_same_v ? 1.0e-7 : 1.0e-15; + REQUIRE(store.getValue(0) == Approx(0.36787944117144232160).epsilon(relativeTolerance)); + REQUIRE(store.getValue(1) == 1); + REQUIRE(store.getValue(2) == Approx(2.71828182845904523536).epsilon(relativeTolerance)); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +// exp(1000) exceeds the float64 finite range and produces positive infinity. +// ConvertMathResult retains infinity and explicitly handles finite results above the float32 range. TEST_CASE("NXImageProcessing::ExpImageFilter: exp of a large input overflows to +inf (domain edge)", "[NXImageProcessing][ExpImageFilter]") { UnitTest::LoadPlugins(); @@ -17,9 +305,12 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: exp of a large input overflows to const DataPath input = ip_test::BuildScalarValuesImage(ds, {1000.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isinf(out.getValue(0))); REQUIRE(out.getValue(0) > 0.0f); // +inf + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- @@ -44,7 +335,7 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: ITK real-image golden (defaults)", args.insertOrAssign(ExpImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(ExpImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(ExpImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - ExpImageFilter filter; + const ExpImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From 96e3af5e570ebb551bd52dbfeb4b7e10ab070e42 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 19:52:45 -0400 Subject: [PATCH 04/98] BUG: Preserve negative exponential range for integer input Use float64 output for integer input and retain existing floating types. Convert before negation and handle float32 overflow explicitly. Document precision, storage, underflow and downstream mask changes. Paired Release builds and 15 in-core / 20 disk-configuration tests pass. Source review and clang-format 19.1.1 pass; configured static checks retain existing diagnostics. Signed-off-by: Jessica Marquis --- .../docs/ExpNegativeImageFilter.md | 38 ++- .../Filters/ExpNegativeImageFilter.cpp | 11 +- .../test/ExpNegativeImageFilterTest.cpp | 278 +++++++++++++++++- 3 files changed, 316 insertions(+), 11 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md b/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md index 6cca66b243..5104edb360 100644 --- a/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md @@ -1,14 +1,46 @@ # Exp Negative Image Filter -Computes the negative exponential of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `exp(-x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Exp Negative Image Filter. +Computes the negative exponential, `exp(-x) = e^(-x)`, for each input value. The input and output are dimensionless. Positive inputs give positive decay values below 1. Negative inputs produce growth. Each component is processed separately. The output keeps the input tuple and component shapes. + +### Output type and precision + +| Input type | Output type | Output bytes per value | +|---|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float64 | 8 | +| float32 | float32 | 4 | +| float64 | float64 | 8 | + +Integer input uses float64 to preserve quantitative decay and a wider growth range. Input 1 gives about 0.3678794412. Input 255 gives about 1.7984862203 × 10^-111, which is positive in float64 but below the float32 range. Input -128 gives about 3.8877084060 × 10^55, which also requires the float64 range. Output promotion depends on the input type, not its value. + +Float32 has about 7 significant decimal digits; float64 has about 16. Results are rounded to the output type. Float64 represents every int8 and uint8 negative-exponential result as a finite positive value, but the larger integer types can still produce underflow or overflow. Converting a 64-bit integer argument to float64 can round that argument before the calculation. Floating input keeps its existing type: float32 input -128 produces positive infinity, while float64 or integer input -128 has the finite result above. + +### Numeric limits + +- NaN (not a number) produces NaN. +- Positive infinity produces positive zero. Negative infinity produces positive infinity. +- Positive and negative zero both produce 1. +- Growth beyond the finite output range produces positive infinity. Finite float64 results above the float32 range are handled explicitly before conversion. +- Very small decay results can be **subnormal** values, which are positive values below the smallest normal floating value. Further underflow produces positive zero. Float32 input 128 produces zero; float64 or integer input 128 gives about 2.5722093726 × 10^-56. + +The argument is converted to float64 before its sign changes. A signed integer minimum is therefore not negated in its integer type. + +### Storage and downstream use + +For integer inputs, the 8-byte output uses 8, 4, 2, or 1 times the bytes of an 8-, 16-, 32-, or 64-bit input value. One billion uint8 input values require 1 GB, and their output values require 8 GB, before other storage. Floating input keeps its existing output width. + +Earlier integer output truncated decay values to zero: integer input 1 or 255 gave zero. These results now remain positive until floating underflow occurs. Every uint8 input from 0 through 255 has a positive float64 output. A later output-not-equal-to-zero mask can therefore select every cell. Review masks and thresholds that depended on the earlier integer truncation. + +Downstream filters that accept only integers can reject the float64 output. Adjust the pipeline to accept floating data and reconsider its thresholds. If an integer conversion is necessary, choose an explicit range and rounding policy. An unchecked conversion can lose decay values or exceed the integer range. + +## Algorithm + +The filter applies the same negative-exponential operation to resident and disk-backed arrays. It uses the existing pointwise traversal and bounded transfer buffers for disk-backed storage. It does not create a full-array resident temporary for the calculation. Output storage still requires the bytes shown above. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp index 91cc9dd728..12b51de636 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct ExpNegativeOperation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::exp(-static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::exp(-static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult ExpNegativeImageFilter::preflightImpl(const DataStructu auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> ExpNegativeImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> ExpNegativeImageFilter::executeImpl(DataStructure& dataStructure, const const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const ExpNegativeOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index 51aa0fde78..137fc22b2f 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -3,11 +3,276 @@ #include "NXImageProcessing/Filters/ExpNegativeImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): the op is static_cast(std::exp(-static_cast(x))). -// For a large NEGATIVE input, -x is large positive, so exp(-x) overflows double (>~709.78) to +inf, and casting -// +inf to float32 stays +inf. ITK's ExpNegative functor is the same expression, so this pins the +inf overflow. +TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input produces float64 decay", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + std::vector values = {0, 1, std::numeric_limits::max()}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpNegativeImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + + const ExpNegativeImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop before an extreme negative input could overflow an integer output. + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, values.size()}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, values.size()}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float64Array* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 1.0); + REQUIRE(store.getValue(1) == Approx(0.36787944117144232160).epsilon(1.0e-15)); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(2) > 0.0); + REQUIRE(store.getValue(2) == Approx(6.99198999664591705080e-56).epsilon(1.0e-15)); + } + else if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(2) > 0.0); + REQUIRE(store.getValue(2) == Approx(1.79848622027946356313e-111).epsilon(1.0e-15)); + } + else + { + REQUIRE(store.getValue(2) == 0.0); + REQUIRE_FALSE(std::signbit(store.getValue(2))); + } + if constexpr(std::is_signed_v) + { + REQUIRE(store.getValue(3) == Approx(2.71828182845904523536).epsilon(1.0e-15)); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(4) == Approx(3.88770840599459509222e55).epsilon(1.0e-15)); + } + else + { + REQUIRE(store.getValue(4) == std::numeric_limits::infinity()); + } + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE values retain sign reversed range behavior", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), + -std::numeric_limits::infinity(), + TestType{0}, + -TestType{0}, + -100, + 128, + -1000, + 1000, + 100, + 103, + 104, + -10, + -88, + -89, + -709, + -710, + 744, + 745, + 746}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 20}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + REQUIRE(std::isnan(store.getValue(0))); + REQUIRE(store.getValue(1) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(1))); + REQUIRE(store.getValue(2) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(3) == 1); + REQUIRE(store.getValue(4) == 1); + REQUIRE(store.getValue(7) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(8) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(8))); + REQUIRE(store.getValue(16) == std::numeric_limits::infinity()); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(5) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(6) == 0); + REQUIRE(store.getValue(9) == 3.7835058536770060915e-44f); + REQUIRE(store.getValue(10) == std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(11) == 0); + REQUIRE(store.getValue(12) == Approx(22026.46579480671651696).epsilon(1.0e-7)); + REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-7)); + REQUIRE(store.getValue(14) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(15) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(17) == 0); + REQUIRE(store.getValue(18) == 0); + REQUIRE(store.getValue(19) == 0); + } + else + { + REQUIRE(store.getValue(5) == Approx(2.68811714181613544841e43).epsilon(1.0e-15)); + REQUIRE(store.getValue(6) > 0); + REQUIRE(store.getValue(6) == Approx(2.57220937264241482684e-56).epsilon(1.0e-15)); + REQUIRE(store.getValue(9) > 0); + REQUIRE(store.getValue(9) == Approx(3.72007597602083596296e-44).epsilon(1.0e-15)); + REQUIRE(store.getValue(10) > 0); + REQUIRE(store.getValue(10) == Approx(1.85211676951797542963e-45).epsilon(1.0e-15)); + REQUIRE(store.getValue(11) > 0); + REQUIRE(store.getValue(11) == Approx(6.81355682154529853676e-46).epsilon(1.0e-15)); + REQUIRE(store.getValue(12) == Approx(22026.46579480671651696).epsilon(1.0e-15)); + REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-15)); + REQUIRE(store.getValue(14) == Approx(4.48961281917434524628e38).epsilon(1.0e-15)); + REQUIRE(store.getValue(15) == Approx(8.21840746155497218924e307).epsilon(1.0e-15)); + REQUIRE(store.getValue(17) == 2 * std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(18) == std::numeric_limits::denorm_min()); + REQUIRE(store.getValue(19) == 0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: mapped multi component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(input)); + auto inputStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, input, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(inputStorePtr != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(inputStorePtr); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + for(usize index = 0; index < 16; ++index) + { + inputStorePtr->setValue(index, static_cast(static_cast(index % 4) - 1)); + } + Arguments args; + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpNegativeImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const ExpNegativeImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == ShapeType{2, 2, 2}); + REQUIRE(actionPtr->componentDims() == ShapeType{2}); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath output = input.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float64Array* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(store.getValue(index) == Approx(2.71828182845904523536).epsilon(1.0e-15)); + REQUIRE(store.getValue(index + 1) == 1.0); + REQUIRE(store.getValue(index + 2) == Approx(0.36787944117144232160).epsilon(1.0e-15)); + REQUIRE(store.getValue(index + 3) == Approx(0.13533528323661269189).epsilon(1.0e-15)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {-1, 0, 1}); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ExpNegativeImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(ExpNegativeImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + + const ExpNegativeImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 3}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 3}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* exponentialPtr = nullptr; + REQUIRE_NOTHROW(exponentialPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = exponentialPtr->getDataStoreRef(); + constexpr float64 relativeTolerance = std::is_same_v ? 1.0e-7 : 1.0e-15; + REQUIRE(store.getValue(0) == Approx(2.71828182845904523536).epsilon(relativeTolerance)); + REQUIRE(store.getValue(1) == 1); + REQUIRE(store.getValue(2) == Approx(0.36787944117144232160).epsilon(relativeTolerance)); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +// exp(-(-1000)) exceeds the float64 finite range and produces positive infinity. +// The shared math conversion also handles finite results above the float32 range explicitly. TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: exp(-x) of a large negative input overflows to +inf (domain edge)", "[NXImageProcessing][ExpNegativeImageFilter]") { UnitTest::LoadPlugins(); @@ -17,9 +282,12 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: exp(-x) of a large negativ const DataPath input = ip_test::BuildScalarValuesImage(ds, {-1000.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isinf(out.getValue(0))); REQUIRE(out.getValue(0) > 0.0f); // exp(-(-1000)) = exp(1000) = +inf + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- @@ -44,7 +312,7 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: ITK real-image golden (def args.insertOrAssign(ExpNegativeImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(ExpNegativeImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(ExpNegativeImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - ExpNegativeImageFilter filter; + const ExpNegativeImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From 9383ec902107d00e56ac40f3be608900fe488de3 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 19:57:57 -0400 Subject: [PATCH 05/98] BUG: Preserve natural logarithm results for integer input Produce float64 for integer inputs while preserving floating input types. Keep IEEE domain outcomes and document precision, storage and downstream changes. Add independent logarithm identities, native limits, floating boundaries and shape tests. Paired Release builds and 15 in-core / 17 disk-configuration cases pass. Source review and clang-format pass; configured static checks retain documented existing and oracle diagnostics. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/LogImageFilter.md | 47 +++- .../Filters/LogImageFilter.cpp | 11 +- .../test/LogImageFilterTest.cpp | 223 +++++++++++++++++- 3 files changed, 270 insertions(+), 11 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/LogImageFilter.md b/src/Plugins/NXImageProcessing/docs/LogImageFilter.md index 51b7be9d5e..d080ea626a 100644 --- a/src/Plugins/NXImageProcessing/docs/LogImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/LogImageFilter.md @@ -1,14 +1,55 @@ # Log Image Filter -Computes the natural logarithm of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `log(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Log Image Filter. +Computes the natural logarithm, `ln(x)`, of each image value. The base is the mathematical constant *e*, approximately 2.71828. The filter applies the same operation to each component and preserves the array's tuple and component shapes. + +Use positive input values to obtain finite results. The logarithm is dimensionless. If the source has physical units, first divide by a suitable reference with the same units. This transform does not supply a physical calibration. + +### Output Type and Precision + +Preflight and execution use the following output types: + +| Input type | Output type | +|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float64 | +| float32 | float32 | +| float64 | float64 | + +The calculation uses float64 precision. Integer input produces float64 output so that fractional logarithms are retained. For example, `ln(1) = 0` and `ln(2)` is approximately 0.6931471805599453. Float32 input retains float32 output and its usual rounding. + +Converting an int64 or uint64 input to float64 can round the input before the logarithm is calculated. Nearby large integers can therefore produce the same result. Float64 output avoids an additional rounding step to float32; it does not make every 64-bit integer exactly representable. + +Each output value from integer input occupies 8 bytes. Compared with the former same-type output, this uses 8, 4, 2, or 1 times the storage for 8-, 16-, 32-, or 64-bit input, respectively. These factors describe array values and exclude storage metadata and compression. + +### Zero, Negative, and Nonfinite Values + +The filter retains ordinary floating-point behavior: + +| Input value | Output value | +|---|---| +| Positive finite value | Finite natural logarithm | +| Positive or negative zero | Negative infinity | +| Negative finite value | NaN (not a number) | +| Negative infinity | NaN | +| Positive infinity | Positive infinity | +| NaN | NaN | + +Zero and negative input are distinct cases. The filter does not clamp them to a positive value or replace the result with zero. Positive subnormal values, which are smaller than the smallest normal floating-point value, still have finite logarithms. + +### Existing Pipelines + +Integer input now produces float64 output. Earlier same-type integer output discarded fractional results; `ln(2)` became zero. Update downstream filters that require integer arrays. Recheck thresholds and zero/nonzero masks that depended on the old quantization. + +Handle NaN and infinity explicitly before range calculations or conversion to integer. An unchecked conversion back to integer can reintroduce unsafe behavior. + +## Algorithm + +This is an ITK-free implementation of the legacy ITK Log Image Filter. It uses the existing pointwise traversal for resident and disk-backed arrays. Resident storage retains the direct route when available. The buffered route reads and writes bounded batches through the store interface. It does not create a full-array resident copy for this transform. Cancellation and storage failures follow the shared pointwise execution path. ![](Images/LogImageFilter.png) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp index 217cacf6d0..1106904987 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct LogOperation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::log(static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::log(static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult LogImageFilter::preflightImpl(const DataStructure& data auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> LogImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> LogImageFilter::executeImpl(DataStructure& dataStructure, const Argumen const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const LogOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp index 37ea0c0a48..03b5330f2f 100644 --- a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp @@ -3,11 +3,221 @@ #include "NXImageProcessing/Filters/LogImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): the op is static_cast(std::log(static_cast(x))) -// -- ITK's equally-unguarded Log functor. log(x<0) is NaN and log(0) is -inf (IEEE pole). This test pins both. (The -// golden test only tolerates the -inf tail, it does not assert it explicitly.) +namespace +{ +DataPath runMappedIntegerLog(DataStructure& dataStructure, const DataPath& input, const ShapeType& tupleShape, const ShapeType& componentShape) +{ + const LogImageFilter filter; + Arguments args; + args.insertOrAssign(LogImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(LogImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(LogImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop before zero or negative integer input can produce an unsafe nonfinite-to-integer cast. + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + return input.replaceName("Output"); +} +} // namespace + +TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce float64 natural logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + constexpr int32 highestPower = std::numeric_limits::digits - 1; + constexpr TestType highestPowerOfTwo = static_cast(TestType{1} << highestPower); + std::vector values = {1, 2, 0, std::numeric_limits::max(), highestPowerOfTwo}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + const ShapeType tupleShape = {1, 1, values.size()}; + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = runMappedIntegerLog(dataStructure, input, tupleShape, {1}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float64Array* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0.0); + REQUIRE(store.getValue(1) == Approx(0.69314718055994530942).epsilon(0).margin(1.0e-15)); + REQUIRE(std::isinf(store.getValue(2))); + REQUIRE(std::signbit(store.getValue(2))); + // 2^(digits-1) <= maximum < 2^digits. Rounding can make the upper logarithm equal for 64-bit types. + constexpr float64 logTwo = 0.69314718055994530942; + constexpr float64 lowerBound = highestPower * logTwo; + constexpr float64 upperBound = std::numeric_limits::digits * logTwo; + REQUIRE(std::isfinite(store.getValue(3))); + REQUIRE(store.getValue(3) >= lowerBound); + REQUIRE(store.getValue(3) <= upperBound); + REQUIRE(store.getValue(4) == Approx(lowerBound).epsilon(0).margin(1.0e-14)); + if constexpr(std::is_signed_v) + { + REQUIRE(std::isnan(store.getValue(5))); + REQUIRE(std::isnan(store.getValue(6))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = + ip_test::BuildScalarValuesImage(dataStructure, {1, 2, TestType{0}, -TestType{0}, -1, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), + -std::numeric_limits::infinity(), std::numeric_limits::max(), std::numeric_limits::lowest()}); + const DataPath output = input.replaceName("Output"); + const LogImageFilter filter; + Arguments args; + args.insertOrAssign(LogImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(LogImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(LogImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 10}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 10}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + constexpr float64 logarithmTolerance = std::is_same_v ? 3.0e-8 : 1.0e-15; + REQUIRE(store.getValue(0) == TestType{0}); + REQUIRE(store.getValue(1) == Approx(0.69314718055994530942).epsilon(0).margin(logarithmTolerance)); + for(usize index = 2; index < 4; ++index) + { + REQUIRE(std::isinf(store.getValue(index))); + REQUIRE(std::signbit(store.getValue(index))); + } + REQUIRE(std::isnan(store.getValue(4))); + REQUIRE(std::isnan(store.getValue(5))); + REQUIRE(std::isinf(store.getValue(6))); + REQUIRE_FALSE(std::signbit(store.getValue(6))); + REQUIRE(std::isnan(store.getValue(7))); + REQUIRE(std::isfinite(store.getValue(8))); + REQUIRE(store.getValue(8) > TestType{0}); + REQUIRE(std::isnan(store.getValue(9))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundaries retain finite logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::min(), std::numeric_limits::denorm_min(), + std::nextafter(TestType{1}, TestType{0}), std::nextafter(TestType{1}, std::numeric_limits::infinity())}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 4}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + // IEEE binary types have minimum normal 2^(min_exponent-1) and minimum subnormal 2^(min_exponent-digits). + constexpr float64 logTwo = 0.69314718055994530942; + constexpr float64 normalLogarithm = (std::numeric_limits::min_exponent - 1) * logTwo; + constexpr float64 subnormalLogarithm = (std::numeric_limits::min_exponent - std::numeric_limits::digits) * logTwo; + constexpr float64 logarithmTolerance = std::is_same_v ? 1.0e-5 : 1.0e-12; + REQUIRE(store.getValue(0) == Approx(normalLogarithm).epsilon(0).margin(logarithmTolerance)); + REQUIRE(store.getValue(1) == Approx(subnormalLogarithm).epsilon(0).margin(logarithmTolerance)); + REQUIRE(std::isfinite(store.getValue(2))); + REQUIRE(store.getValue(2) < TestType{0}); + REQUIRE(std::isfinite(store.getValue(3))); + REQUIRE(store.getValue(3) > TestType{0}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::LogImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][LogImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 1, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(input)); + auto storePtr = DataStoreUtilities::CreateDataStore(dataStructure, input, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(storePtr != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(storePtr); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + constexpr std::array values = {1, 2, 0, -1}; + for(usize index = 0; index < 16; ++index) + { + storePtr->setValue(index, values.at(index % values.size())); + } + const DataPath output = runMappedIntegerLog(dataStructure, input, {2, 2, 2}, {2}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float64Array* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs(output)); + const auto& result = logarithmsPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(result.getValue(index) == 0.0); + REQUIRE(result.getValue(index + 1) == Approx(0.69314718055994530942).epsilon(0).margin(1.0e-15)); + REQUIRE(std::isinf(result.getValue(index + 2))); + REQUIRE(std::signbit(result.getValue(index + 2))); + REQUIRE(std::isnan(result.getValue(index + 3))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +// Floating domain behavior: log(x<0) is NaN and log(0) is -infinity. TEST_CASE("NXImageProcessing::LogImageFilter: log of negative is NaN and log(0) is -inf (domain edge)", "[NXImageProcessing][LogImageFilter]") { UnitTest::LoadPlugins(); @@ -17,10 +227,13 @@ TEST_CASE("NXImageProcessing::LogImageFilter: log of negative is NaN and log(0) const DataPath input = ip_test::BuildScalarValuesImage(ds, {-2.0f, 0.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isnan(out.getValue(0))); // log(-2) -> NaN REQUIRE(std::isinf(out.getValue(1))); // log(0) -> -inf REQUIRE(out.getValue(1) < 0.0f); + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- @@ -46,7 +259,7 @@ TEST_CASE("NXImageProcessing::LogImageFilter: ITK real-image golden (defaults)", args.insertOrAssign(LogImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(LogImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(LogImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - LogImageFilter filter; + const LogImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From 00b26e6e02948801a8cd71c75b9663e38ec055dd Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:16:35 -0400 Subject: [PATCH 06/98] DOC: Clarify Exp pointwise buffer limits Document the existing 64 MiB scratch target and minimum complete storage slab. A slab can exceed the target or contain the full array. The filter adds no separate full-volume calculation buffer. Numeric code and tests are unchanged; source review verified the statement against the engine. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/docs/ExpImageFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md b/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md index 591004c781..f742eb997a 100644 --- a/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ExpImageFilter.md @@ -36,7 +36,7 @@ Earlier integer output truncated positive decay values below 1 to zero. These va ## Algorithm -The filter applies the same exponential operation to resident and disk-backed arrays. It uses the existing pointwise traversal and bounded transfer buffers for disk-backed storage. It does not create a full-array resident temporary for the calculation. Output storage still requires the bytes shown above. +The filter applies the same exponential operation to resident and disk-backed arrays. Buffered processing targets 64 MiB of transfer scratch by default, but each batch must contain at least one complete storage slab formed by the trailing tuple dimensions, such as an XY slice for Z/Y/X storage. A slab can exceed that target or cover the entire array. Bulk input and output transfers are serial. The calculation runs in parallel on owned buffers. Exp adds no separate full-volume scratch array beyond those transfer buffers. Output storage still requires the bytes shown above. % Auto generated parameter table will be inserted here From ce19dd434c3c2a82b6c242fb70563ae48daf63f8 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:16:35 -0400 Subject: [PATCH 07/98] DOC: Clarify ExpNegative pointwise buffer limits Document the existing 64 MiB scratch target and minimum complete storage slab. A slab can exceed the target or contain the full array. The filter adds no separate full-volume calculation buffer. Numeric code and tests are unchanged; source review verified the statement against the engine. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md b/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md index 5104edb360..9884b575dd 100644 --- a/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ExpNegativeImageFilter.md @@ -40,7 +40,7 @@ Downstream filters that accept only integers can reject the float64 output. Adju ## Algorithm -The filter applies the same negative-exponential operation to resident and disk-backed arrays. It uses the existing pointwise traversal and bounded transfer buffers for disk-backed storage. It does not create a full-array resident temporary for the calculation. Output storage still requires the bytes shown above. +The filter applies the same negative-exponential operation to resident and disk-backed arrays. The shared streaming path targets 64 MiB of transfer scratch space, with a minimum of one complete slab across the trailing tuple dimensions, such as an XY slice for Z/Y/X tuple order. A slab can exceed that target or cover the whole array. Bulk reads and writes are serial; the calculation can run in parallel on owned buffers. Exp Negative adds no separate full-volume scratch array. Output storage still requires the bytes shown above. % Auto generated parameter table will be inserted here From 98d296a3dc0896b401f87fbcf2254d203b98cdde Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:16:36 -0400 Subject: [PATCH 08/98] DOC: Clarify Log pointwise buffer limits Document the existing 64 MiB scratch target and minimum complete storage slab. A slab can exceed the target or contain the full array. The filter adds no separate full-volume calculation buffer. Numeric code and tests are unchanged; source review verified the statement against the engine. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/docs/LogImageFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/docs/LogImageFilter.md b/src/Plugins/NXImageProcessing/docs/LogImageFilter.md index d080ea626a..d38f913210 100644 --- a/src/Plugins/NXImageProcessing/docs/LogImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/LogImageFilter.md @@ -49,7 +49,7 @@ Handle NaN and infinity explicitly before range calculations or conversion to in ## Algorithm -This is an ITK-free implementation of the legacy ITK Log Image Filter. It uses the existing pointwise traversal for resident and disk-backed arrays. Resident storage retains the direct route when available. The buffered route reads and writes bounded batches through the store interface. It does not create a full-array resident copy for this transform. Cancellation and storage failures follow the shared pointwise execution path. +This is an ITK-free implementation of the legacy ITK Log Image Filter. It uses the existing pointwise traversal for resident and disk-backed arrays. Resident storage retains the direct route when available. The buffered route has a default transfer scratch target of 64 MiB and processes at least one complete slab across the trailing tuple dimensions, such as an XY slice for a Z/Y/X array. A slab can exceed the target or cover the whole array. Bulk reads and writes run serially; parallel calculation uses owned buffers. Log adds no separate full-volume scratch allocation. Cancellation and storage failures follow the shared pointwise execution path. ![](Images/LogImageFilter.png) From f3919529dcba61210df255e96d8032814604b54a Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:17:20 -0400 Subject: [PATCH 09/98] BUG: Preserve decimal logarithm results for integer input Use float64 for integer input and retain floating input types and IEEE domain behavior. Document precision, storage, downstream effects and the inherited transfer-buffer slab limit. Independent boundary and shape tests pass in paired Release builds: 15 in-core and 17 OOC cases. Source review and clang-format pass; configured static checks retain documented existing/shared diagnostics. Signed-off-by: Jessica Marquis --- .../docs/Log10ImageFilter.md | 47 ++- .../Filters/Log10ImageFilter.cpp | 11 +- .../test/Log10ImageFilterTest.cpp | 279 +++++++++++++++++- 3 files changed, 327 insertions(+), 10 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/Log10ImageFilter.md b/src/Plugins/NXImageProcessing/docs/Log10ImageFilter.md index 220b0307ab..feb24c2e22 100644 --- a/src/Plugins/NXImageProcessing/docs/Log10ImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/Log10ImageFilter.md @@ -1,14 +1,55 @@ # Log10 Image Filter -Computes the base-10 logarithm of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `log10(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Log10 Image Filter. +Computes the base-10 logarithm, `log10(x)`, of each image value. The filter applies the same operation to each component and preserves the array's tuple and component shapes. For example, `log10(1) = 0`, `log10(10) = 1`, and `log10(100) = 2`. + +Use positive input values to obtain finite results. The logarithm is dimensionless. If the source has physical units, first divide by a suitable reference with the same units. This transform does not supply a physical calibration. + +### Output Type and Precision + +Preflight and execution use the following output types: + +| Input type | Output type | +|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float64 | +| float32 | float32 | +| float64 | float64 | + +The calculation uses float64 precision. Integer input produces float64 output so that fractional logarithms are retained. For example, `log10(2)` is approximately 0.3010299956639812. Float32 input retains float32 output and its usual rounding. + +Float32 has enough range for these logarithms. Float64 output for integer input retains the existing double-precision calculation; this is a precision and storage choice. Converting int64 or uint64 input to float64 can round the input before the logarithm is calculated. Nearby large integers can therefore produce the same result. Float64 output does not make every 64-bit integer exactly representable. + +Each output value from integer input occupies 8 bytes. Compared with the former same-type output, this uses 8, 4, 2, or 1 times the storage for 8-, 16-, 32-, or 64-bit input, respectively. These factors describe array values and exclude storage metadata and compression. + +### Zero, Negative, and Nonfinite Values + +The filter retains ordinary floating-point behavior: + +| Input value | Output value | +|---|---| +| Positive finite value | Finite base-10 logarithm | +| Positive or negative zero | Negative infinity | +| Negative finite value | NaN (not a number) | +| Negative infinity | NaN | +| Positive infinity | Positive infinity | +| NaN | NaN | + +Zero and negative input are distinct cases. The filter does not clamp them to a positive value or replace the result with zero. Positive subnormal values, which are smaller than the smallest normal floating-point value, still have finite logarithms. + +### Existing Pipelines + +Integer input now produces float64 output. Earlier same-type integer output discarded fractional results; `log10(2)` became zero. Update downstream filters that require integer arrays. Recheck thresholds and zero/nonzero masks that depended on the old quantization. + +Handle NaN and infinity explicitly before range calculations or conversion to integer. An unchecked conversion back to integer can reintroduce unsafe behavior. + +## Algorithm + +This is an ITK-free implementation of the legacy ITK Log10 Image Filter. It uses the existing pointwise traversal for resident and disk-backed arrays. Resident storage retains the direct route when available. The buffered route uses serial bulk reads and writes with a default transfer scratch target of 64 MiB. Each working batch contains at least one complete storage slab across the trailing dimensions, such as an XY slice in a Z/Y/X image. A slab can exceed the target or cover the entire array. Parallel math uses owned buffers. Log10 adds no separate scratch array for the full volume. Cancellation and storage failures follow the shared pointwise execution path. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp index dc0e166927..7016f29d27 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct Log10Operation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::log10(static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::log10(static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult Log10ImageFilter::preflightImpl(const DataStructure& da auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> Log10ImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> Log10ImageFilter::executeImpl(DataStructure& dataStructure, const Argum const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const Log10Operation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp index a0f527179c..33fcec23bf 100644 --- a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp @@ -3,10 +3,278 @@ #include "NXImageProcessing/Filters/Log10ImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/AttributeMatrix.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): the op is static_cast(std::log10(static_cast(x))) -// -- ITK's equally-unguarded Log10 functor. log10(x<0) is NaN and log10(0) is -inf (IEEE pole). This test pins both. +namespace +{ +// Independent decimal constants for log10(2^digits - 1), rounded to float64. +template +constexpr float64 maximumIntegerLog10() +{ + constexpr auto digits = std::numeric_limits::digits; + if constexpr(digits == 7) + { + return 2.1038037209559568; + } + else if constexpr(digits == 8) + { + return 2.406540180433955; + } + else if constexpr(digits == 15) + { + return 4.515436681141699; + } + else if constexpr(digits == 16) + { + return 4.81647330376525; + } + else if constexpr(digits == 31) + { + return 9.331929865381183; + } + else if constexpr(digits == 32) + { + return 9.632959861146281; + } + else if constexpr(digits == 63) + { + return 18.964889726830815; + } + else + { + static_assert(digits == 64); + return 19.265919722494797; + } +} +} // namespace + +TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: integer values produce float64 logarithms and domain results", "[NXImageProcessing][Issue1874][Log10ImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + std::vector values = {1, 10, 100, 2, 0, std::numeric_limits::max()}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(Log10ImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(Log10ImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(Log10ImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const Log10ImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop the baseline before zero or a negative input can produce a nonfinite-to-integer cast. + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, values.size()}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, values.size()}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float64Array* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0.0); + REQUIRE(store.getValue(1) == 1.0); + REQUIRE(store.getValue(2) == 2.0); + REQUIRE(store.getValue(3) == Approx(0.30102999566398119521).epsilon(0).margin(1.0e-15)); + REQUIRE(std::isinf(store.getValue(4))); + REQUIRE(std::signbit(store.getValue(4))); + REQUIRE(store.getValue(5) == Approx(maximumIntegerLog10()).epsilon(0).margin(1.0e-14)); + if constexpr(std::is_signed_v) + { + REQUIRE(std::isnan(store.getValue(6))); + REQUIRE(std::isnan(store.getValue(7))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: floating input retains type and IEEE domain behavior", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {1, 10, 100, 2, TestType{0.0}, -TestType{0.0}, -1, std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), -std::numeric_limits::infinity()}); + const DataPath output = input.replaceName("Output"); + Arguments args; + args.insertOrAssign(Log10ImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(Log10ImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(Log10ImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const Log10ImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 10}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 10}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0); + REQUIRE(store.getValue(1) == 1); + REQUIRE(store.getValue(2) == 2); + constexpr float64 absoluteTolerance = std::is_same_v ? 3.0e-8 : 1.0e-15; + REQUIRE(store.getValue(3) == Approx(0.30102999566398119521).epsilon(0).margin(absoluteTolerance)); + for(const usize index : {usize{4}, usize{5}}) + { + REQUIRE(std::isinf(store.getValue(index))); + REQUIRE(std::signbit(store.getValue(index))); + } + REQUIRE(std::isnan(store.getValue(6))); + REQUIRE(std::isnan(store.getValue(7))); + REQUIRE(std::isinf(store.getValue(8))); + REQUIRE_FALSE(std::signbit(store.getValue(8))); + REQUIRE(std::isnan(store.getValue(9))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating boundaries have finite ordered logarithms", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = + ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::denorm_min(), std::numeric_limits::min(), std::numeric_limits::max(), + std::nextafter(TestType{1}, TestType{0}), TestType{1}, std::nextafter(TestType{1}, TestType{2})}); + const DataPath output = ip_test::RunPointwiseFilter(dataStructure, input); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 6}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = logarithmsPtr->getDataStoreRef(); + // The two lower limits are exact powers of two: log10(2^n) = n * log10(2). + constexpr float64 log10Two = 0.30102999566398119521; + constexpr float64 expectedSubnormal = (std::numeric_limits::min_exponent - std::numeric_limits::digits) * log10Two; + constexpr float64 expectedNormal = (std::numeric_limits::min_exponent - 1) * log10Two; + constexpr float64 expectedMaximum = std::is_same_v ? 38.531839419103626 : 308.25471555991675; + constexpr float64 limitTolerance = std::is_same_v ? 5.0e-6 : 5.0e-13; + REQUIRE(std::isfinite(store.getValue(0))); + REQUIRE(store.getValue(0) == Approx(expectedSubnormal).epsilon(0).margin(limitTolerance)); + REQUIRE(std::isfinite(store.getValue(1))); + REQUIRE(store.getValue(1) == Approx(expectedNormal).epsilon(0).margin(limitTolerance)); + REQUIRE(std::isfinite(store.getValue(2))); + REQUIRE(store.getValue(2) == Approx(expectedMaximum).epsilon(0).margin(limitTolerance)); + constexpr float64 expectedBelowOne = std::is_same_v ? -2.588596909327576e-8 : -4.8216373327664354e-17; + constexpr float64 expectedAboveOne = std::is_same_v ? 5.177193355766362e-8 : 9.64327466553287e-17; + constexpr float64 adjacentTolerance = std::is_same_v ? 5.0e-15 : 1.0e-30; + REQUIRE(store.getValue(3) == Approx(expectedBelowOne).epsilon(0).margin(adjacentTolerance)); + REQUIRE(store.getValue(3) < 0); + REQUIRE(store.getValue(4) == 0); + REQUIRE(store.getValue(5) == Approx(expectedAboveOne).epsilon(0).margin(adjacentTolerance)); + REQUIRE(store.getValue(5) > 0); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::Log10ImageFilter: integer output preserves tuple and component shapes", "[NXImageProcessing][Issue1874][Log10ImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + auto* imageGeomPtr = ImageGeom::Create(dataStructure, "Image Geometry"); + REQUIRE(imageGeomPtr != nullptr); + imageGeomPtr->setDimensions({2, 1, 1}); + const ShapeType tupleShape{1, 1, 2}; + const ShapeType componentShape{2}; + auto* cellDataPtr = AttributeMatrix::Create(dataStructure, "CellData", tupleShape, imageGeomPtr->getId()); + REQUIRE(cellDataPtr != nullptr); + imageGeomPtr->setCellData(*cellDataPtr); + const DataPath input({"Image Geometry", "CellData", "Input"}); + auto storePtr = DataStoreUtilities::CreateDataStore(dataStructure, input, tupleShape, componentShape); + REQUIRE(storePtr != nullptr); + auto* inputArrayPtr = DataArray::Create(dataStructure, "Input", storePtr, cellDataPtr->getId()); + REQUIRE(inputArrayPtr != nullptr); + const std::vector values{1, 10, 100, 2}; + for(usize index = 0; index < values.size(); ++index) + { + inputArrayPtr->getDataStoreRef().setValue(index, values.at(index)); + } + Arguments args; + args.insertOrAssign(Log10ImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(Log10ImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(Log10ImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const Log10ImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath output = input.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == componentShape); + const Float64Array* logarithmsPtr = nullptr; + REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs(output)); + const auto& outputStore = logarithmsPtr->getDataStoreRef(); + REQUIRE(outputStore.getValue(0) == 0.0); + REQUIRE(outputStore.getValue(1) == 1.0); + REQUIRE(outputStore.getValue(2) == 2.0); + REQUIRE(outputStore.getValue(3) == Approx(0.30102999566398119521).epsilon(0).margin(1.0e-15)); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +// Negative input produces NaN; zero produces negative infinity. Floating output represents both results. TEST_CASE("NXImageProcessing::Log10ImageFilter: log10 of negative is NaN and log10(0) is -inf (domain edge)", "[NXImageProcessing][Log10ImageFilter]") { UnitTest::LoadPlugins(); @@ -16,10 +284,13 @@ TEST_CASE("NXImageProcessing::Log10ImageFilter: log10 of negative is NaN and log const DataPath input = ip_test::BuildScalarValuesImage(ds, {-2.0f, 0.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isnan(out.getValue(0))); // log10(-2) -> NaN REQUIRE(std::isinf(out.getValue(1))); // log10(0) -> -inf REQUIRE(out.getValue(1) < 0.0f); + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- @@ -45,7 +316,7 @@ TEST_CASE("NXImageProcessing::Log10ImageFilter: ITK real-image golden (defaults) args.insertOrAssign(Log10ImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(Log10ImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(Log10ImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - Log10ImageFilter filter; + const Log10ImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From a4a4612afebd51bf82222c6996dbbb6b536256c5 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:17:20 -0400 Subject: [PATCH 10/98] BUG: Preserve fractional square roots for integer input Use float64 for integer input and retain floating input types and IEEE domain behavior. Document precision, storage, downstream effects and the inherited transfer-buffer slab limit. Independent boundary and shape tests pass in paired Release builds: 15 in-core and 17 OOC cases. Source review and clang-format pass; configured static checks retain documented existing/shared diagnostics. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/SqrtImageFilter.md | 48 ++- .../Filters/SqrtImageFilter.cpp | 11 +- .../test/SqrtImageFilterTest.cpp | 274 +++++++++++++++++- 3 files changed, 323 insertions(+), 10 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/SqrtImageFilter.md b/src/Plugins/NXImageProcessing/docs/SqrtImageFilter.md index be2b53055c..dd2873a5a1 100644 --- a/src/Plugins/NXImageProcessing/docs/SqrtImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/SqrtImageFilter.md @@ -1,14 +1,56 @@ # Sqrt Image Filter -Computes the square root of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `sqrt(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Sqrt Image Filter. +Computes the square root, `sqrt(x)`, of each image value. The filter applies the same operation to each component and preserves the array's tuple and component shapes. + +Use finite nonnegative values to obtain finite results. The output has the square root of the input units. For example, input in squared length units produces output in length units. Dimensionless input produces dimensionless output. The transform alone does not supply a physical calibration. + +### Output Type and Precision + +Preflight and execution use the following output types: + +| Input type | Output type | +|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float64 | +| float32 | float32 | +| float64 | float64 | + +The calculation uses float64 precision. Integer input produces float64 output because a square root usually has a fractional part. For example, `sqrt(0) = 0`, `sqrt(4) = 2`, and `sqrt(2)` is approximately 1.4142135623730951. This is a mathematical square root, rather than an integer-root operation that discards the fractional part. Float32 input retains float32 output and its usual rounding. + +Converting int64 or uint64 input to float64 can round the input before the square root is calculated. Nearby large integers can therefore produce the same result. Float64 output avoids an additional rounding step to float32; it does not make every 64-bit integer transform exact. For example, the float64 result for `sqrt(INT64_MAX)` is approximately 3037000499.97605. Its representation error is about 0.000000208, compared with about 52 for float32. + +Each output value from integer input occupies 8 bytes. Compared with the former same-type output, this uses 8, 4, 2, or 1 times the storage for 8-, 16-, 32-, or 64-bit input, respectively. These factors describe array values and exclude storage metadata and compression. + +### Zero, Negative, and Nonfinite Values + +The filter retains ordinary floating-point behavior: + +| Input value | Output value | +|---|---| +| Positive finite value | Positive finite square root | +| Positive zero | Positive zero | +| Negative floating zero | Negative zero | +| Negative finite value | NaN (not a number) | +| Negative infinity | NaN | +| Positive infinity | Positive infinity | +| NaN | NaN | + +The filter does not clamp negative values to zero. Positive subnormal values, which are smaller than the smallest normal floating-point value, have positive square roots. Negative signed integer input produces a representable NaN in the float64 output. + +### Existing Pipelines + +Integer input now produces float64 output. Earlier same-type integer output discarded fractional results; `sqrt(2)` became 1. Update downstream filters that require integer arrays. Recheck thresholds that depended on integer quantization. For valid nonnegative input, the direct zero/nonzero status is preserved: zero stays zero, and positive input stays positive. + +Handle NaN and infinity explicitly before range calculations or conversion to integer. An unchecked conversion back to integer can reintroduce unsafe behavior. + +## Algorithm + +This is an ITK-free implementation of the legacy ITK Sqrt Image Filter. It uses the existing pointwise traversal for resident and disk-backed arrays. Resident storage retains the direct route when available. The buffered route uses a default 64 MiB transfer-scratch target, with a minimum of one complete storage slab across the trailing tuple dimensions. For Z/Y/X tuple order, this can be an XY slice. A slab can exceed the target or cover the whole array. Bulk store reads and writes are serial; parallel calculations use owned buffers. Sqrt adds no separate full-volume scratch array. Cancellation and storage failures follow the shared pointwise execution path. ![](Images/SqrtImageFilter.png) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp index 0a67c57b85..a2dddaacb2 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct SqrtOperation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::sqrt(static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::sqrt(static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult SqrtImageFilter::preflightImpl(const DataStructure& dat auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> SqrtImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> SqrtImageFilter::executeImpl(DataStructure& dataStructure, const Argume const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const SqrtOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp index b4459110fb..69f599d96c 100644 --- a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp @@ -3,10 +3,273 @@ #include "NXImageProcessing/Filters/SqrtImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): sqrt is undefined for x<0. The op is -// static_cast(std::sqrt(static_cast(x))) -- ITK's equally-unguarded Sqrt functor -- so sqrt(-4) is NaN. +namespace +{ +DataPath runMappedIntegerSqrt(DataStructure& dataStructure, const DataPath& input, const ShapeType& tupleShape, const ShapeType& componentShape) +{ + const SqrtImageFilter filter; + Arguments args; + args.insertOrAssign(SqrtImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SqrtImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(SqrtImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop before negative integer input can produce an unsafe NaN-to-integer cast. + REQUIRE(actionPtr->type() == DataType::float64); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + return input.replaceName("Output"); +} +} // namespace + +TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: integer values produce float64 square roots", "[NXImageProcessing][Issue1874][SqrtImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + std::vector values = {0, 2, 4, std::numeric_limits::max()}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + values.push_back(std::numeric_limits::lowest()); + } + const ShapeType tupleShape = {1, 1, values.size()}; + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath output = runMappedIntegerSqrt(dataStructure, input, tupleShape, {1}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float64Array* rootsPtr = nullptr; + REQUIRE_NOTHROW(rootsPtr = &dataStructure.getDataRefAs(output)); + const auto& store = rootsPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0.0); + REQUIRE(store.getValue(1) == Approx(1.4142135623730950488).epsilon(0).margin(3.0e-16)); + REQUIRE(store.getValue(2) == 2.0); + REQUIRE(std::isfinite(store.getValue(3))); + REQUIRE(store.getValue(3) > 2.0); + REQUIRE(store.getValue(3) <= 4294967296.0); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(11.269427669584644).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(15.968719422671311).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(181.0165738268184132).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(255.998046867549362).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(46340.9500010519849).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(3) == Approx(65535.9999923706055).epsilon(1.0e-15)); + } + if constexpr(std::is_same_v) + { + // Independent high-precision root of 9223372036854775807, with one float64 ULP of tolerance. + REQUIRE(store.getValue(3) == Approx(3037000499.9760496923).epsilon(0).margin(5.0e-7)); + } + if constexpr(std::is_same_v) + { + // sqrt(18446744073709551615) rounds to 2^32 in float64. + REQUIRE(store.getValue(3) == 4294967296.0); + } + if constexpr(std::is_signed_v) + { + REQUIRE(std::isnan(store.getValue(4))); + REQUIRE(std::isnan(store.getValue(5))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage( + dataStructure, {TestType{0}, -TestType{0}, 2, 4, -1, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity(), + std::numeric_limits::max(), std::numeric_limits::lowest(), std::numeric_limits::min(), -std::numeric_limits::min()}); + const DataPath output = input.replaceName("Output"); + const SqrtImageFilter filter; + Arguments args; + args.insertOrAssign(SqrtImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SqrtImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(SqrtImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 12}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 12}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* rootsPtr = nullptr; + REQUIRE_NOTHROW(rootsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = rootsPtr->getDataStoreRef(); + constexpr float64 rootTolerance = std::is_same_v ? 1.0e-7 : 3.0e-16; + REQUIRE(store.getValue(0) == TestType{0}); + REQUIRE_FALSE(std::signbit(store.getValue(0))); + REQUIRE(store.getValue(1) == TestType{0}); + REQUIRE(std::signbit(store.getValue(1))); + REQUIRE(store.getValue(2) == Approx(1.4142135623730950488).epsilon(0).margin(rootTolerance)); + REQUIRE(store.getValue(3) == TestType{2}); + REQUIRE(std::isnan(store.getValue(4))); + REQUIRE(std::isnan(store.getValue(5))); + REQUIRE(std::isinf(store.getValue(6))); + REQUIRE_FALSE(std::signbit(store.getValue(6))); + REQUIRE(std::isnan(store.getValue(7))); + REQUIRE(std::isfinite(store.getValue(8))); + REQUIRE(store.getValue(8) > TestType{2}); + REQUIRE(std::isnan(store.getValue(9))); + REQUIRE(std::isfinite(store.getValue(10))); + REQUIRE(store.getValue(10) > TestType{0}); + REQUIRE(store.getValue(10) < TestType{1}); + REQUIRE(std::isnan(store.getValue(11))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating boundary roots match independent binary values", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + constexpr TestType smallestNormal = std::numeric_limits::min(); + constexpr TestType infinity = std::numeric_limits::infinity(); + DataStructure dataStructure; + const DataPath input = ip_test::BuildScalarValuesImage( + dataStructure, {std::numeric_limits::denorm_min(), -std::numeric_limits::denorm_min(), smallestNormal, std::nextafter(smallestNormal, TestType{0}), + std::nextafter(smallestNormal, infinity), TestType{1}, std::nextafter(TestType{1}, TestType{0}), std::nextafter(TestType{1}, infinity), std::numeric_limits::max()}); + const DataPath output = input.replaceName("Output"); + const SqrtImageFilter filter; + Arguments args; + args.insertOrAssign(SqrtImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SqrtImageFilter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(SqrtImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 9}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 9}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* rootsPtr = nullptr; + REQUIRE_NOTHROW(rootsPtr = &dataStructure.getDataRefAs>(output)); + const auto& store = rootsPtr->getDataStoreRef(); + + // Exact binary literals follow square roots of powers of two and nearest-value rounding. + // The float32 smallest subnormal is 2^-149; its root is sqrt(2) * 2^-75. + constexpr TestType subnormalRoot = static_cast(std::is_same_v ? 0x1.6a09e6p-75 : 0x1p-537); + constexpr TestType normalRoot = static_cast(std::is_same_v ? 0x1p-63 : 0x1p-511); + constexpr TestType belowNormalRoot = static_cast(std::is_same_v ? 0x1.fffffep-64 : 0x1.fffffffffffffp-512); + constexpr TestType belowOneRoot = static_cast(std::is_same_v ? 0x1.fffffep-1 : 0x1.fffffffffffffp-1); + constexpr TestType maximumRoot = static_cast(std::is_same_v ? 0x1.fffffep63 : 0x1.fffffffffffffp511); + REQUIRE(store.getValue(0) == subnormalRoot); + REQUIRE(std::isnan(store.getValue(1))); + REQUIRE(store.getValue(2) == normalRoot); + REQUIRE(store.getValue(3) == belowNormalRoot); + REQUIRE(store.getValue(4) == normalRoot); + REQUIRE(store.getValue(5) == TestType{1}); + REQUIRE(store.getValue(6) == belowOneRoot); + REQUIRE(store.getValue(7) == TestType{1}); + REQUIRE(store.getValue(8) == maximumRoot); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::SqrtImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SqrtImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(input)); + auto storePtr = DataStoreUtilities::CreateDataStore(dataStructure, input, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(storePtr != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(storePtr); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + constexpr std::array values = {0, 2, 4, -1}; + for(usize index = 0; index < 16; ++index) + { + storePtr->setValue(index, values.at(index % values.size())); + } + const DataPath output = runMappedIntegerSqrt(dataStructure, input, {2, 2, 2}, {2}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(output)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{2, 2, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{2}); + const Float64Array* rootsPtr = nullptr; + REQUIRE_NOTHROW(rootsPtr = &dataStructure.getDataRefAs(output)); + const auto& result = rootsPtr->getDataStoreRef(); + for(usize index = 0; index < 16; index += 4) + { + REQUIRE(result.getValue(index) == 0.0); + REQUIRE(result.getValue(index + 1) == Approx(1.4142135623730950488).epsilon(0).margin(3.0e-16)); + REQUIRE(result.getValue(index + 2) == 2.0); + REQUIRE(std::isnan(result.getValue(index + 3))); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +// Negative floating input produces a representable NaN. TEST_CASE("NXImageProcessing::SqrtImageFilter: sqrt of a negative is NaN (domain edge)", "[NXImageProcessing][SqrtImageFilter]") { UnitTest::LoadPlugins(); @@ -16,8 +279,11 @@ TEST_CASE("NXImageProcessing::SqrtImageFilter: sqrt of a negative is NaN (domain const DataPath input = ip_test::BuildScalarValuesImage(ds, {-4.0f}); const DataPath output = ip_test::RunPointwiseFilter(ds, input); - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); + const Float32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &ds.getDataRefAs(output)); + const auto& out = outputArrayPtr->getDataStoreRef(); REQUIRE(std::isnan(out.getValue(0))); + UnitTest::CheckArraysInheritTupleDims(ds); } // ----------------------------------------------------------------------------- @@ -42,7 +308,7 @@ TEST_CASE("NXImageProcessing::SqrtImageFilter: ITK real-image golden (defaults)" args.insertOrAssign(SqrtImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(SqrtImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(SqrtImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - SqrtImageFilter filter; + const SqrtImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From 8708667f1ef6e630e57b473f4913147f6c5de8b5 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:19:17 -0400 Subject: [PATCH 11/98] BUG: Preserve signed fractional tangent results Map integer input to float32 and retain existing floating types. Keep IEEE edge behavior and the existing double-argument precision limit. Document the accuracy/storage tradeoff, downstream effects and buffer slab minimum. Paired Release builds and 12 in-core / 14 OOC cases pass. Source review and clang-format 19.1.1 pass. Configured static checks remain nonpassing with documented existing/shared or intentional signed-widening diagnostics. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/TanImageFilter.md | 36 ++- .../Filters/TanImageFilter.cpp | 11 +- .../test/TanImageFilterTest.cpp | 229 ++++++++++++++++-- 3 files changed, 253 insertions(+), 23 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/TanImageFilter.md b/src/Plugins/NXImageProcessing/docs/TanImageFilter.md index a9c981012a..7a5b3e995d 100644 --- a/src/Plugins/NXImageProcessing/docs/TanImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/TanImageFilter.md @@ -1,14 +1,44 @@ # Tan Image Filter -Computes the tangent of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `tan(x)` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Tan Image Filter. +Computes the **tangent**, `tan(x)`, for each input value. The input is an angle in **radians**. The output is a signed, dimensionless response. Each component is processed separately. The output keeps the input tuple and component shapes. + +### Output Type and Precision + +| Input type | Output type | Output bytes per value | +|---|---|---| +| int8, uint8, int16, uint16, int32, uint32, int64, uint64 | float32 | 4 | +| float32 | float32 | 4 | +| float64 | float64 | 8 | + +Integer input needs floating output because an integer angle can produce a signed, fractional tangent. For example, `tan(1)` is about 1.55740772 and `tan(2)` is about -2.18503986. An unsigned input does not imply a positive tangent. Integer output would discard fractions and cannot safely store all negative responses. + +The input is converted to double precision before the tangent calculation. The result is then rounded to the output type. Float32 has about 7 significant decimal digits; float64 has about 16. The selected float32 output for integer input uses half the bytes of float64. A representation study across all int8 and uint8 arguments found a worst relative rounding error of about `5.63e-8`. This measures output rounding only. It is not a bound on the tangent calculation or wider integer inputs. + +Integer arguments above `2^53` in magnitude can lose whole integer units when converted to double. For example, `9007199254740993` becomes `9007199254740992` before evaluation. This can change the tangent's phase and result. Float64 output would not recover that lost input precision. + +### Numeric Limits + +- Positive and negative zero keep their signs. +- NaN (not a number), positive infinity, and negative infinity produce NaN. +- Finite angles near a **pole**, where the mathematical tangent is undefined, are evaluated without a cutoff or clamp. Poles occur at `pi/2 + k*pi` radians for integer `k`. +- The nearest float32 and float64 values to `pi/2` lie on opposite sides of that pole. Their results are about `-2.2877332e7` and `1.6331239353e16`, respectively. Both are finite. Small changes in a stored angle near a pole can change the result's sign and magnitude. +- If a finite double result exceeds the float32 finite range, it is stored as infinity with the result's sign. NaN, infinity, and signed zero keep their floating behavior during output conversion. + +### Storage and Downstream Use + +For integer input, the 4-byte output uses 4, 2, 1, or 0.5 times the bytes of an 8-, 16-, 32-, or 64-bit input value. Floating input keeps its existing output width. + +Existing pipelines with integer input now receive float32 responses. Earlier integer output discarded fractions: for example, `tan(3)` became zero in signed integer output instead of about -0.14254654. Review thresholds and zero/nonzero masks that depended on this truncation. Filters that accept only integer arrays can reject the new output type. Near-pole responses can dominate extrema and normalization. Before converting output back to integers, handle nonfinite values and check the destination range; an unchecked conversion can reintroduce unsafe behavior. + +## Algorithm + +The filter evaluates each value independently through the existing pointwise traversal. Resident arrays use direct memory views. Disk-backed arrays use serial bulk reads and writes through working batches. Transfer buffers target a combined 64 MiB, but each batch holds at least one complete trailing-dimensional slab, such as an XY slice for a Z/Y/X tuple shape. A slab can exceed this target or contain the whole array. Parallel calculation uses owned buffers. The filter adds no separate Tan-specific full-array calculation buffer. Output uses the input storage format and preserves its geometry. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp index cc62cda25b..44023cf257 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp @@ -2,6 +2,9 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Types.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -22,7 +25,7 @@ struct TanOperation template auto makeMapOp() const { - return [](T value) -> U { return static_cast(std::tan(static_cast(value))); }; + return [](T value) -> U { return ImageProcessing::ConvertMathResult(std::tan(static_cast(value))); }; } }; } // namespace @@ -78,7 +81,8 @@ IFilter::PreflightResult TanImageFilter::preflightImpl(const DataStructure& data auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> TanImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -89,7 +93,8 @@ Result<> TanImageFilter::executeImpl(DataStructure& dataStructure, const Argumen const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); const TanOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, + messageHandler); } namespace diff --git a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp index 25846e8b28..10f2a1864c 100644 --- a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp @@ -3,30 +3,225 @@ #include "NXImageProcessing/Filters/TanImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AttributeMatrix.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): tan has a pole at pi/2. Because pi/2 is irrational the -// nearest float32 to it is NOT exactly the pole, so std::tan(static_cast(x)) is a very large FINITE value -// (never inf) -- and ITK's Tan functor is the same expression, so this pins that observed large-but-finite output -// (asserted both by magnitude and by exact equality to the recomputed op). -TEST_CASE("NXImageProcessing::TanImageFilter: tan near pi/2 is large-magnitude finite (domain edge)", "[NXImageProcessing][TanImageFilter]") +TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: integer radians produce signed fractional float32 responses", "[NXImageProcessing][Issue1874][TanImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); - constexpr float64 k_HalfPi = 1.5707963267948966; // double nearest pi/2 - const float32 inValue = static_cast(k_HalfPi); - const float32 expected = static_cast(std::tan(static_cast(inValue))); + std::vector values = {0, 1, 3}; + if constexpr(std::is_signed_v) + { + values.push_back(-1); + } + const usize twoRadianIndex = values.size(); + values.push_back(2); + const usize firstExtremum = values.size(); + values.push_back(std::numeric_limits::lowest()); + values.push_back(std::numeric_limits::max()); + if constexpr(sizeof(TestType) == sizeof(int64)) + { + constexpr TestType exactDoubleInteger = TestType{9007199254740992ULL}; + values.push_back(exactDoubleInteger); + values.push_back(exactDoubleInteger + 1); + } + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, values); + const ShapeType tupleShape = {1, 1, values.size()}; + Arguments args; + args.insertOrAssign(TanImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(TanImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(TanImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const TanImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // Stop the baseline before a negative tangent could be converted to an unsigned integer. + REQUIRE(actionPtr->type() == DataType::float32); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); - DataStructure ds; - const DataPath input = ip_test::BuildScalarValuesImage(ds, {inValue}); - const DataPath output = ip_test::RunPointwiseFilter(ds, input); - - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); - const float32 value = out.getValue(0); - REQUIRE(std::isfinite(value)); // NOT inf: float32 cannot represent the exact pole - REQUIRE(std::abs(value) > 1.0e6f); // but is a large magnitude - REQUIRE(value == expected); // exact: identical std::tan-in-double formula as the filter op + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const Float32Array* responsesPtr = nullptr; + REQUIRE_NOTHROW(responsesPtr = &dataStructure.getDataRefAs(outputPath)); + const auto& store = responsesPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0.0f); + REQUIRE(store.getValue(1) == Approx(1.5574077246549022305).epsilon(0).margin(1.0e-7)); + REQUIRE(store.getValue(2) == Approx(-0.1425465430742778053).epsilon(0).margin(1.0e-8)); + if constexpr(std::is_signed_v) + { + REQUIRE(store.getValue(3) == Approx(-1.5574077246549022305).epsilon(0).margin(1.0e-7)); + } + REQUIRE(store.getValue(twoRadianIndex) == Approx(-2.18503986326151899164).epsilon(0).margin(2.0e-7)); + // Native integer endpoints are accepted, without claiming exact phase after conversion to double. + REQUIRE(std::isfinite(store.getValue(firstExtremum))); + REQUIRE(std::isfinite(store.getValue(firstExtremum + 1))); + if constexpr(sizeof(TestType) == sizeof(int64)) + { + // The approved double argument conversion maps 2^53 and 2^53+1 to the same argument. + REQUIRE(std::isfinite(store.getValue(values.size() - 2))); + REQUIRE(store.getValue(values.size() - 2) == store.getValue(values.size() - 1)); + } + + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: floating responses retain type signed zero and finite near-pole values", "[NXImageProcessing][Issue1874][TanImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {0, -TestType{0}, 1, -1, 3, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), + -std::numeric_limits::infinity(), static_cast(1.5707963267948966), + std::numeric_limits::lowest(), std::numeric_limits::max()}); + Arguments args; + args.insertOrAssign(TanImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(TanImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(TanImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const TanImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 11}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 11}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* responsesPtr = nullptr; + REQUIRE_NOTHROW(responsesPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& store = responsesPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(0))); + REQUIRE(store.getValue(1) == 0); + REQUIRE(std::signbit(store.getValue(1))); + constexpr float64 responseTolerance = std::is_same_v ? 1.0e-7 : 1.0e-15; + REQUIRE(store.getValue(2) == Approx(1.5574077246549022305).epsilon(0).margin(responseTolerance)); + REQUIRE(store.getValue(3) == Approx(-1.5574077246549022305).epsilon(0).margin(responseTolerance)); + REQUIRE(store.getValue(4) == Approx(-0.1425465430742778053).epsilon(0).margin(responseTolerance)); + for(usize index = 5; index < 8; ++index) + { + REQUIRE(std::isnan(store.getValue(index))); + } + // Independent high-precision sine/cosine references at the exact stored half-pi approximations. + // The two stored arguments are on opposite sides of the pole and must not be excluded. + REQUIRE(std::isfinite(store.getValue(8))); + if constexpr(std::is_same_v) + { + REQUIRE(store.getValue(8) == Approx(-22877332.428856459874).epsilon(1.0e-7)); + } + else + { + REQUIRE(store.getValue(8) == Approx(16331239353195369.756).epsilon(1.0e-15)); + } + REQUIRE(std::isfinite(store.getValue(9))); + REQUIRE(std::isfinite(store.getValue(10))); + + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::TanImageFilter: integer vector tuples preserve shape and component order", "[NXImageProcessing][Issue1874][TanImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + + DataStructure dataStructure; + const ShapeType tupleShape = {1, 2, 2}; + const ShapeType componentShape = {2}; + auto* imageGeomPtr = ImageGeom::Create(dataStructure, "Image Geometry"); + REQUIRE(imageGeomPtr != nullptr); + imageGeomPtr->setDimensions({2, 2, 1}); + auto* cellDataPtr = AttributeMatrix::Create(dataStructure, "CellData", tupleShape, imageGeomPtr->getId()); + REQUIRE(cellDataPtr != nullptr); + imageGeomPtr->setCellData(*cellDataPtr); + const DataPath inputPath({"Image Geometry", "CellData", "Input"}); + auto inputStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, inputPath, tupleShape, componentShape); + REQUIRE(inputStorePtr != nullptr); + auto* inputArrayPtr = DataArray::Create(dataStructure, "Input", inputStorePtr, cellDataPtr->getId()); + REQUIRE(inputArrayPtr != nullptr); + constexpr std::array values = {0, 1, 3, -1, 1, 0, -1, 3}; + for(usize index = 0; index < values.size(); ++index) + { + inputArrayPtr->getDataStoreRef().setValue(index, values.at(index)); + } + + Arguments args; + args.insertOrAssign(TanImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(TanImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(TanImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const TanImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::float32); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == componentShape); + const Float32Array* responsesPtr = nullptr; + REQUIRE_NOTHROW(responsesPtr = &dataStructure.getDataRefAs(outputPath)); + constexpr std::array expected = {0, 1.5574077246549022305, -0.1425465430742778053, -1.5574077246549022305, 1.5574077246549022305, 0, -1.5574077246549022305, -0.1425465430742778053}; + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(responsesPtr->getDataStoreRef().getValue(index) == Approx(expected.at(index)).epsilon(0).margin(1.0e-7)); + } + + UnitTest::CheckArraysInheritTupleDims(dataStructure); } // ----------------------------------------------------------------------------- From 47e785c898ff30457d097e5060c06b2f4391bceb Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 20:19:18 -0400 Subject: [PATCH 12/98] BUG: Widen integer squares and reject 64-bit overflow Use exact native integer products with the approved widening map. Check signed and unsigned 64-bit bounds before multiplication through a native extrema scan. Retain floating types with checked overflow conversion. Document type mapping, limits, scan cost and migration. Paired Release builds and 12 in-core / 17 OOC cases pass. Source review and clang-format 19.1.1 pass. Configured static checks remain nonpassing with documented existing/shared or intentional signed-widening diagnostics. Signed-off-by: Jessica Marquis --- .../docs/SquareImageFilter.md | 50 +++- .../Filters/SquareImageFilter.cpp | 103 ++++++- .../test/SquareImageFilterTest.cpp | 273 +++++++++++++++++- 3 files changed, 405 insertions(+), 21 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/SquareImageFilter.md b/src/Plugins/NXImageProcessing/docs/SquareImageFilter.md index 394dd030a2..7dd91f2cc5 100644 --- a/src/Plugins/NXImageProcessing/docs/SquareImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/SquareImageFilter.md @@ -1,16 +1,58 @@ # Square Image Filter -Computes the square of each pixel. - ## Group (Subgroup) NXImageProcessing (Pointwise) ## Description -Computes `x * x` for every pixel. ITK-free, out-of-core-capable reimplementation of the legacy ITK Square Image Filter. +Computes `x * x` for each value in the selected image cell array. The output keeps the input tuple and component shapes. Each component is squared separately. The output has the square of the input units; for example, an input in metres gives an output in square metres. Dimensionless input remains dimensionless. + +### Output types and exact integer results + +The filter widens 8-, 16- and 32-bit integers by one native width. It multiplies integers in the output integer type, without an intermediate floating-point conversion. + +| Input type | Output type | +|---|---| +| int8 | int16 | +| uint8 | uint16 | +| int16 | int32 | +| uint16 | uint32 | +| int32 | int64 | +| uint32 | uint64 | +| int64 | int64 | +| uint64 | uint64 | +| float32 | float32 | +| float64 | float64 | + +For example, *16* gives the exact integer result *256*, and *4097* gives *16785409*. The largest uint32 input, *4294967295*, gives the exact uint64 result *18446744065119617025*. + +The 64-bit integer output types have these input limits: + +- int64: *-3037000499* through *3037000499*, inclusive. +- uint64: *0* through *4294967295*, inclusive. + +An input outside these limits produces an error before integer multiplication. The error identifies the input array, the offending value and the allowed bound. The filter checks the signed limits directly, including the int64 minimum value. It does not saturate the output or change an overflowing integer result to a floating-point type. + +### Floating-point results + +Floating-point inputs keep their type. Squaring either signed zero gives positive zero. NaN gives NaN, and either infinity gives positive infinity. A result above the output finite range gives positive infinity. Small results can become subnormal values or positive zero through underflow. Floating-point results have the precision of their output type; they are not exact integer substitutes. + +### Input and downstream guidance + +Select an image geometry and a numeric cell array that belongs to that geometry. The array must have one tuple per image cell. An image reader or an earlier image-processing filter can supply this array. + +The widened 8-, 16- and 32-bit integer output uses twice the input array's storage. Check any later filter or export that requires a specific integer width. Exact integer comparisons can also differ from older results that lost low bits through floating-point arithmetic. + +For larger 64-bit integer inputs, explicitly convert the input array to float64 before this filter if approximate squares are acceptable. The conversion and floating-point multiplication can lose integer units. Choose that conversion deliberately rather than narrowing the exact integer output to fit a downstream width. + +## Algorithm + +The filter uses the existing pointwise traversal for both in-memory and disk-backed arrays. In-memory arrays use the resident route when available. Other stores use serial bulk reads and writes, with arithmetic on owned resident buffers. The generic traversal targets a 64 MiB scratch budget but requires at least one storage slab; a slab can exceed that target or cover a thin array. Square adds no separate temporary buffer beyond the existing traversal and scan buffers. + +Only int64 and uint64 inputs require an extra native-integer minimum/maximum scan before multiplication. This adds one complete input read for those types. The scan uses the existing resident or buffered reduction and checks cancellation and read errors. Other types use one pointwise pass. Cancellation and I/O failures follow the filter framework's existing result behavior. A later I/O failure does not guarantee that the output is unchanged. -![](Images/SquareImageFilter.png) +![Square image example](Images/SquareImageFilter.png) % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp index 1235c7a4e7..9e852d9b1c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp @@ -2,6 +2,15 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "ImageProcessing/utils/MathOutputUtilities.hpp" + +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -9,22 +18,90 @@ #include "simplnx/Parameters/StringParameter.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/StreamingStatistics.hpp" #include "simplnx/Utilities/SIMPLConversion.hpp" -#include +#include + +#include +#include +#include using namespace nx::core; namespace { +template +using SquareOutputType = std::conditional_t< + std::is_same_v, int16, + std::conditional_t< + std::is_same_v, uint16, + std::conditional_t, int32, + std::conditional_t, uint32, std::conditional_t, int64, std::conditional_t, uint64, T>>>>>>; + +template +Result<> validateSquareRange(const AbstractDataStore& inputStore, const DataPath& inputPath, const std::atomic_bool& shouldCancel) +{ + static_assert(std::is_same_v || std::is_same_v); + if(shouldCancel) + { + return {}; + } + auto extremaResult = ImageProcessing::ComputeArrayMinMax(inputStore, shouldCancel); + if(extremaResult.invalid()) + { + return ConvertResult(std::move(extremaResult)); + } + if(shouldCancel || extremaResult.value().count == 0) + { + return ConvertResult(std::move(extremaResult)); + } + const auto& extrema = extremaResult.value(); + if constexpr(std::is_same_v) + { + // Compare native signed values directly; taking abs(INT64_MIN) would overflow. + constexpr int64 limit = 3037000499LL; + if(extrema.min < -limit) + { + auto errorResult = MakeErrorResult(-1874091, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at least -3037000499. Convert the input to " + "float64 first if approximate larger squares are acceptable.", + extrema.min, inputPath.toString())); + return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); + } + if(extrema.max > limit) + { + auto errorResult = MakeErrorResult(-1874092, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at most " + "3037000499. Convert the input to float64 first if approximate larger squares are acceptable.", + extrema.max, inputPath.toString())); + return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); + } + } + else if(extrema.max > 4294967295ULL) + { + auto errorResult = MakeErrorResult(-1874093, fmt::format("Square Image Filter cannot square value {} in input array '{}': uint64 inputs must be at most " + "4294967295. Convert the input to float64 first if approximate larger squares are acceptable.", + extrema.max, inputPath.toString())); + return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); + } + return ConvertResult(std::move(extremaResult)); +} + struct SquareOperation { template auto makeMapOp() const { return [](T value) -> U { - const double d = static_cast(value); - return static_cast(d * d); + if constexpr(std::is_integral_v) + { + const U widenedValue = static_cast(value); + return static_cast(widenedValue * widenedValue); + } + else + { + const float64 floatingValue = static_cast(value); + return ImageProcessing::ConvertMathResult(floatingValue * floatingValue); + } }; } }; @@ -81,7 +158,8 @@ IFilter::PreflightResult SquareImageFilter::preflightImpl(const DataStructure& d auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); return {std::move(resultOutputActions)}; } Result<> SquareImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, @@ -91,8 +169,23 @@ Result<> SquareImageFilter::executeImpl(DataStructure& dataStructure, const Argu auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); + Result<> validationResult; + const DataType inputType = dataStructure.getDataRefAs(selectedInputArray).getDataType(); + if(inputType == DataType::int64) + { + validationResult = validateSquareRange(dataStructure.getDataRefAs(selectedInputArray).getDataStoreRef(), selectedInputArray, shouldCancel); + } + else if(inputType == DataType::uint64) + { + validationResult = validateSquareRange(dataStructure.getDataRefAs(selectedInputArray).getDataStoreRef(), selectedInputArray, shouldCancel); + } + if(validationResult.invalid() || shouldCancel) + { + return validationResult; + } const SquareOperation operation{}; - return ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + auto executeResult = ImageProcessing::ExecuteImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); + return MergeResults(std::move(validationResult), std::move(executeResult)); } namespace diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 6437c9829f..7e889f3d8b 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -3,23 +3,272 @@ #include "NXImageProcessing/Filters/SquareImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + using namespace nx::core; -// PART A domain edge (documents ITK-faithful behavior): the op is { double d = x; return static_cast(d*d); }. -// For x = 1e20 (representable in float32), d*d = 1e40 which exceeds FLT_MAX (~3.4e38), so the narrowing cast to -// float32 yields +inf. ITK's Sqr functor squares the same way, so this pins the observed overflow to +inf. -TEST_CASE("NXImageProcessing::SquareImageFilter: square near sqrt(FLT_MAX) overflows to +inf (domain edge)", "[NXImageProcessing][SquareImageFilter]") +namespace +{ +DataPath runSquareWithType(DataStructure& dataStructure, const DataPath& inputPath, DataType expectedType, const ShapeType& tupleShape, const ShapeType& componentShape) +{ + Arguments args; + args.insertOrAssign(SquareImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SquareImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const SquareImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + // A hard preflight guard stops the old same-width output before any narrowing overflow or typed lookup. + REQUIRE(actionPtr->type() == expectedType); + REQUIRE(actionPtr->dims() == tupleShape); + REQUIRE(actionPtr->componentDims() == componentShape); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == expectedType); + REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(outputArrayPtr->getComponentShape() == componentShape); + return outputPath; +} + +template +void requireSquareValues(const std::vector& values, const std::vector& expected) +{ + REQUIRE(values.size() == expected.size()); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, values); + const DataPath outputPath = runSquareWithType(dataStructure, inputPath, GetDataType(), ShapeType{1, 1, values.size()}, ShapeType{1}); + const DataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& store = outputArrayPtr->getDataStoreRef(); + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(store.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +template +void requireSquareOverflow(InputType value, int32 expectedCode) +{ + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {0, value, 1}); + Arguments args; + args.insertOrAssign(SquareImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SquareImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const SquareImageFilter filter; + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().size() == 1); + REQUIRE(executeResult.result.errors().front().code == expectedCode); + REQUIRE(executeResult.result.errors().front().message.find(inputPath.toString()) != std::string::npos); + REQUIRE(executeResult.result.errors().front().message.find(std::to_string(value)) != std::string::npos); + REQUIRE(executeResult.result.errors().front().message.find("float64") != std::string::npos); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +} // namespace + +TEST_CASE("NXImageProcessing::SquareImageFilter: smaller integer types widen for exact squares", "[NXImageProcessing][Issue1874][SquareImageFilter]") { UnitTest::LoadPlugins(); - const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + SECTION("int8 to int16") + { + requireSquareValues({0, 1, -1, 16, -128, 127}, {0, 1, 1, 256, 16384, 16129}); + } + SECTION("uint8 to uint16") + { + requireSquareValues({0, 1, 16, 225, 255}, {0, 1, 256, 50625, 65025}); + } + SECTION("int16 to int32") + { + requireSquareValues({0, -16, 16, -32768, 32767}, {0, 256, 256, 1073741824, 1073676289}); + } + SECTION("uint16 to uint32") + { + requireSquareValues({0, 16, 4097, 65535}, {0, 256, 16785409, 4294836225U}); + } + SECTION("int32 to int64") + { + requireSquareValues({0, -16, 16, std::numeric_limits::min(), std::numeric_limits::max()}, {0, 256, 256, 4611686018427387904LL, 4611686014132420609LL}); + } + SECTION("uint32 to uint64") + { + requireSquareValues({0, 16, 4097, 4294967295U}, {0, 256, 16785409, 18446744065119617025ULL}); + } +} - DataStructure ds; - const DataPath input = ip_test::BuildScalarValuesImage(ds, {1.0e20f}); - const DataPath output = ip_test::RunPointwiseFilter(ds, input); +TEST_CASE("NXImageProcessing::SquareImageFilter: retained 64-bit integers preserve low bits at safe bounds", "[NXImageProcessing][Issue1874][SquareImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + SECTION("int64 signed bounds") + { + // These exact products and the baseline double products fit int64. No overflowing input reaches the baseline. + requireSquareValues({0, -1, 1, -3037000498LL, 3037000498LL, -3037000499LL, 3037000499LL}, + {0, 1, 1, 9223372024852248004LL, 9223372024852248004LL, 9223372030926249001LL, 9223372030926249001LL}); + } + SECTION("uint64 bound") + { + requireSquareValues({0, 1, 4294967294ULL, 4294967295ULL}, {0, 1, 18446744056529682436ULL, 18446744065119617025ULL}); + } +} - const auto& out = ds.getDataRefAs>(output).getDataStoreRef(); - REQUIRE(std::isinf(out.getValue(0))); - REQUIRE(out.getValue(0) > 0.0f); // (1e20)^2 = 1e40 > FLT_MAX -> +inf +TEST_CASE("NXImageProcessing::SquareImageFilter: 64-bit overflow inputs return actionable errors", "[NXImageProcessing][Issue1874][SquareImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + SECTION("Signed minimum") + { + requireSquareOverflow(std::numeric_limits::min(), -1874091); + } + SECTION("Signed negative adjacent overflow") + { + requireSquareOverflow(-3037000500LL, -1874091); + } + SECTION("Signed positive adjacent overflow") + { + requireSquareOverflow(3037000500LL, -1874092); + } + SECTION("Signed maximum") + { + requireSquareOverflow(std::numeric_limits::max(), -1874092); + } + SECTION("Unsigned adjacent overflow") + { + requireSquareOverflow(4294967296ULL, -1874093); + } + SECTION("Unsigned maximum") + { + requireSquareOverflow(std::numeric_limits::max(), -1874093); + } +} + +TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: empty 64-bit inputs retain empty output", "[NXImageProcessing][Issue1874][SquareImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + requireSquareValues({}, {}); +} + +TEST_CASE("NXImageProcessing::SquareImageFilter: cancellation preserves the filter cancellation result", "[NXImageProcessing][Issue1874][SquareImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::min()}); + Arguments args; + args.insertOrAssign(SquareImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(SquareImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + const SquareImageFilter filter; + const std::atomic_bool shouldCancel = true; + const auto executeResult = filter.execute(dataStructure, args, nullptr, {}, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().size() == 1); + REQUIRE(executeResult.result.errors().front().code == -1); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::SquareImageFilter: widened multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SquareImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildRampImage(dataStructure, 2, 0, 0); + Int16Array* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(inputPath)); + const auto inputStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, inputPath, ShapeType{2, 2, 2}, ShapeType{2}); + REQUIRE(inputStorePtr != nullptr); + const auto replaceResult = inputArrayPtr->setDataStore(inputStorePtr); + SIMPLNX_RESULT_REQUIRE_VALID(replaceResult); + constexpr std::array values = {-32768, -16, 16, 32767}; + constexpr std::array expected = {1073741824, 256, 256, 1073676289}; + for(usize index = 0; index < 16; ++index) + { + inputStorePtr->setValue(index, values.at(index % values.size())); + } + const DataPath outputPath = runSquareWithType(dataStructure, inputPath, DataType::int32, ShapeType{2, 2, 2}, ShapeType{2}); + const Int32Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + const auto& store = outputArrayPtr->getDataStoreRef(); + for(usize index = 0; index < 16; ++index) + { + REQUIRE(store.getValue(index) == expected.at(index % expected.size())); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating inputs retain type and safe IEEE values", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + const TestType infinity = std::numeric_limits::infinity(); + DataStructure dataStructure; + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {0, -TestType{0}, -2, 2, infinity, -infinity, std::numeric_limits::quiet_NaN()}); + const DataPath outputPath = runSquareWithType(dataStructure, inputPath, GetDataType(), ShapeType{1, 1, 7}, ShapeType{1}); + const DataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& store = outputArrayPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(0))); + REQUIRE(store.getValue(1) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(1))); + REQUIRE(store.getValue(2) == 4); + REQUIRE(store.getValue(3) == 4); + REQUIRE(store.getValue(4) == infinity); + REQUIRE(store.getValue(5) == infinity); + REQUIRE(std::isnan(store.getValue(6))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating overflow is positive infinity and underflow is positive zero", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const TestType maximum = std::numeric_limits::max(); + const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {maximum, -maximum, std::numeric_limits::min(), std::numeric_limits::denorm_min()}); + const DataPath outputPath = runSquareWithType(dataStructure, inputPath, GetDataType(), ShapeType{1, 1, 4}, ShapeType{1}); + const DataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs>(outputPath)); + const auto& store = outputArrayPtr->getDataStoreRef(); + REQUIRE(store.getValue(0) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(1) == std::numeric_limits::infinity()); + REQUIRE(store.getValue(2) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(2))); + REQUIRE(store.getValue(3) == 0); + REQUIRE_FALSE(std::signbit(store.getValue(3))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); } // ----------------------------------------------------------------------------- @@ -44,7 +293,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: ITK real-image golden (defaults args.insertOrAssign(SquareImageFilter::k_InputImageGeomPath_Key, std::make_any(geom)); args.insertOrAssign(SquareImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - SquareImageFilter filter; + const SquareImageFilter filter; auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); auto executeResult = filter.execute(ds, args); From b9fe586d5d574c6afe0ee8d9f99576b3d1ec357b Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 21:32:22 -0400 Subject: [PATCH 13/98] BUG: Preserve native HConvex markers and check signed contrast Reject fractional Height for integer images and lower integer markers with exact saturating arithmetic. Check signed contrast before subtraction. Keep output types and floating behavior. Shared exact-height capability is opt-in; existing callers retain their defaults. Document migration and limits. Paired full Release builds pass, with 55 in-core / 63 OOC HConvex cases and 40 / 46 shared-caller controls. Source review and clang-format pass. Configured static checks remain nonpassing on documented existing, framework and owned-buffer/numeric diagnostics. Signed-off-by: Jessica Marquis --- .../docs/HConvexImageFilter.md | 21 +- .../Filters/HConvexImageFilter.cpp | 20 +- .../Filters/HConvexImageFilter.hpp | 7 +- .../utils/ImageFilterExecutorDetail.hpp | 165 +++++- .../utils/ImageFilterExecutors.hpp | 19 +- .../ReconstructionImageFilterExecutors.cpp | 26 +- .../test/HConvexImageFilterTest.cpp | 472 +++++++++++++++++- .../MorphologicalReconstructionEngine.hpp | 106 ++++ 8 files changed, 806 insertions(+), 30 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md b/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md index d7785caa5d..71d2249d4d 100644 --- a/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md @@ -12,13 +12,22 @@ Computes the *h-convex* transform of a grayscale image: **HConvex(f, h) = f − This isolates bright "domes" and measures their prominence: peaks taller than `Height` all map to `Height`, while shallower peaks map to their true contrast. It is a common pre-step for peak detection and marker extraction. -Internally the filter reconstructs the input by dilation of the `input − Height` marker (the H Maxima result) and then subtracts that result from the input. Because the reconstruction result never exceeds the input, the difference is always non-negative and stays within the input type's range. +Internally the filter reconstructs the `input − Height` marker by dilation under the original input, then subtracts that reconstruction from the input. The reconstruction cannot exceed an integer input, so the integer contrast is nonnegative. A nonnegative contrast can still exceed a signed output type's maximum. The filter returns an execution error in that case. -The input array must be single-component (scalar); the output element type matches the input element type. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK H Convex Image Filter, and matches it exactly for integer and float types. +The input array must have one component per tuple (a **scalar image**). Output type, tuple dimensions and component dimensions match the input. Output storage does not increase automatically. Resident and disk-backed arrays use the same numeric rules. ### Height -The height (contrast) cap. Regional maxima taller than this value are reported at this value; shallower maxima are reported at their true height. +**Height** is a contrast offset in the input's intensity units. It must be finite and nonnegative. + +- Integer images require a whole-number Height. A fractional value, such as *2.5*, fails preflight. Choose an intentional whole value, or explicitly convert the image to a floating type before using a fraction. +- Floating images allow fractional Height and keep their existing floating arithmetic and precision. +- With integer images, the marker is the exact native value minus Height, saturated at the input type's minimum. Native 64-bit image values do not pass through floating point. Height *0* preserves the marker and reconstruction exactly; the HConvex output is therefore zero everywhere. +- Height is stored as a float64 parameter. Above *2^53*, float64 cannot distinguish every consecutive integer Height. The filter uses the whole value represented by that parameter. A Height of *2^64* or more lowers every integer marker value to the type minimum. + +If the actual signed integer contrast does not fit the unchanged output type, execution fails with the input array and first failing tuple in the message. For example, int8 input `[-128, 127, -128]` with Height *255* would require contrast `[0, 255, 0]`; *255* cannot fit in int8, so the filter returns an error. Input `[-128, -1, -128]` at the same Height has representable contrast `[0, 127, 0]`. + +For an existing integer pipeline that used fractional Height, choose a whole Height or convert its input explicitly. Exact marker arithmetic can also change results that previously lost native 64-bit units. Review downstream peak selection, watershed markers and segmentation when migrating such a pipeline. ### Fully Connected @@ -31,7 +40,11 @@ Geodesic morphology, the h-maxima transform, and the h-convex transform are desc ## Algorithm -The filter lowers the input by **Height**, reconstructs that marker under the input by dilation, and subtracts the reconstruction from the input. Resident arrays use the Vincent hybrid reconstruction, whose raster, anti-raster, and fold-to-convergence sweeps run as dependency-exact, block-wavefront parallel plane sweeps when the reconstruction uses face connectivity (**Fully Connected** off); with full connectivity it falls back to the exact serial sweeps, since a same-row diagonal read can land in the horizontally adjacent block, which no rectangular block schedule can order. Disk-backed reconstruction iterates a forward raster sweep followed by a reverse anti-raster sweep over consecutive Z-plane slabs until stable, skipping any slab whose planes are already proven unchanged since that direction's last visit. Within a plane, the raster/anti-raster sweep is tiled into independent blocks that run in parallel; under face connectivity every voxel still sees exactly the values the un-blocked sweep would produce, and under full connectivity the iterated forward/reverse passes still converge to the identical final result. Each work and mask slab targets 16 MiB, plus one boundary plane. At least one plane is processed, so a single plane wider than the target can make the buffers exceed 16 MiB. Update order and output are unchanged, preserving exact storage-path parity. +The filter lowers the input by **Height**, reconstructs that marker under the input by dilation, and subtracts the reconstruction from the input. Resident arrays keep the Vincent reconstruction route and direct parallel subtraction. Signed subtraction checks each contrast before performing arithmetic that could overflow. + +Disk-backed input supplies its marker from owned reconstruction buffers, so it does not require a separate resident marker image. Reconstruction keeps its existing working-memory reservation and sweep routes. A granted working set can hold a complete image when it fits. Otherwise, 2-D data uses rows or tiles, and 3-D data uses slabs with a boundary plane. A 3-D streaming plan needs enough space for two working planes and one boundary plane; it returns an error if the grant cannot hold that minimum. This is a working-buffer budget, not a limit on total process memory. + +For disk-backed or other nonresident stores, the signed contrast phase transfers data serially through two owned buffers of at most 32 MiB each. It performs parallel arithmetic only on those buffers and propagates transfer errors and warnings. Resident signed contrast uses direct parallel spans without these transfer buffers. The buffer limits exclude the data stores, their cache and the reconstruction working set. If execution fails, some output values or completed chunks may already have been written; an error does not promise an unchanged output array. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp index baceee0217..3b3d359ebb 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp @@ -2,7 +2,12 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/BoolParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" @@ -16,6 +21,7 @@ #include "simplnx/Utilities/SIMPLConversion.hpp" #include +#include #include @@ -27,6 +33,7 @@ namespace // its Height guards: -8553 (non-finite) and -8556 (negative). constexpr int32 k_NonFiniteHeight = -8553; constexpr int32 k_NegativeHeight = -8556; +constexpr int32 k_FractionalIntegerHeight = -1874101; } // namespace namespace nx::core @@ -57,7 +64,7 @@ Parameters HConvexImageFilter::parameters() const params.insertSeparator(Parameters::Separator{"Input Parameter(s)"}); params.insert(std::make_unique(k_Height_Key, "Height", "The height (contrast) cap. Each regional maximum is transformed to its own height capped at this value (its dome height); the rest of the image " - "becomes 0. Must be finite.", + "becomes 0. Must be finite and nonnegative. Integer input requires a whole-number Height; floating input allows fractions.", 2.0)); params.insert(std::make_unique(k_FullyConnected_Key, "Fully Connected", "Whether the connected components are defined strictly by face connectivity (Off) or by face+edge+vertex connectivity (On). Default is Off. For " @@ -105,6 +112,14 @@ IFilter::PreflightResult HConvexImageFilter::preflightImpl(const DataStructure& { return {MakeErrorResult(k_NegativeHeight, fmt::format("H Convex requires a non-negative Height, but the supplied value was {}.", height))}; } + const auto* inputArrayPtr = dataStructure.getDataAs(selectedInputArray); + if(inputArrayPtr != nullptr && ImageProcessing::GetIntegerScalarTypes().contains(inputArrayPtr->getDataType()) && std::trunc(height) != height) + { + return {.outputActions = MakeErrorResult( + k_FractionalIntegerHeight, fmt::format("H Convex requires a whole-number Height for integer input array '{}', but received {}. Choose an intentional whole Height or explicitly " + "convert the input to floating point before using a fractional Height.", + selectedInputArray.toString(), height))}; + } // requireScalar=true: the grayscale reconstruction engine is defined on scalar images (matching the legacy ITK // operator); the shared preflight also validates type membership and the geometry/tuple match. @@ -126,7 +141,8 @@ Result<> HConvexImageFilter::executeImpl(DataStructure& dataStructure, const Arg // then subtract that h-maxima result from the input (the h-dome). FullyConnected selects the reconstruction // connectivity. return ImageProcessing::ExecuteMorphologicalReconstructionImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Dilation, fullyConnected, - ImageProcessing::detail::ReconMarker::MinusHeight, height, shouldCancel, messageHandler, /*differenceFromInput=*/true); + ImageProcessing::detail::ReconMarker::MinusHeight, height, shouldCancel, messageHandler, /*differenceFromInput=*/true, + /*exactIntegerHeight=*/true); } namespace diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp index 3854356407..d337c7126a 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp @@ -15,9 +15,10 @@ namespace nx::core * HConvex(f, h) = f - HMaxima(f, h). A regional maximum of contrast c maps to min(c, h); the background maps to 0. * It is implemented by reconstruction-by-dilation of the (input - Height) marker under the input mask (the HMaxima * result) followed by an elementwise subtraction of that result from the input. Output type == input type; the - * input must be single-component (scalar). This matches the legacy ITK H Convex Image Filter exactly for integer - * and float types, and is out-of-core capable: the reconstruction result never exceeds the input, so the - * difference never underflows. + * input must be single-component (scalar). Height is finite and nonnegative; integer images require a whole Height. + * Integer marker arithmetic preserves native units and saturates at the type minimum. Floating images allow fractional + * Height. A nonnegative contrast that exceeds a signed output type's maximum returns an execution error. + * Resident and disk-backed arrays use the same numeric contract. */ class NXIMAGEPROCESSING_EXPORT HConvexImageFilter : public IFilter { diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index 905a3cea5d..e1db9a23d5 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -3,9 +3,11 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" #include "simplnx/Common/Range.hpp" +#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp" #include "simplnx/Utilities/ImageProcessing/StreamingStatistics.hpp" #include "simplnx/Utilities/ParallelDataAlgorithm.hpp" @@ -608,8 +610,8 @@ Result<> CropSubtractFromStore(const AbstractDataStore& padded, const Abstrac * stays bounded (a fixed chunk for the height markers, one z-plane for Fillhole), so an out-of-core input * yields an out-of-core-bounded marker. @p dims is (X,Y,Z); @p height feeds MinusHeight/PlusHeight only. * - * MinusHeight/PlusHeight offset every voxel by +/-height through @ref SaturateCastFromDouble, so an offset that - * leaves the type range saturates (ITK-faithful clamp) instead of wrapping. + * MinusHeight/PlusHeight use @ref SaturateCastFromDouble by default. The exactIntegerHeight opt-in preserves native + * integer units with saturating addition or subtraction. Floating values retain the default arithmetic. * * The Fillhole/GrindPeak marker border is the boundary in ITK's EFFECTIVE dimensionality: the legacy filter * processes a (W,H,1) volume as a 2D ITK image (ITKArrayHelper selects Dimension 2 when Z==1), so X and Y are always @@ -619,10 +621,20 @@ Result<> CropSubtractFromStore(const AbstractDataStore& padded, const Abstrac * rule, including "needle" geometries where a non-collapsed axis has extent 1 (every voxel is then a border voxel * and the image is left unchanged). * - * @pre @p markerStore has the same tuple/element layout as @p inStore (X fastest). @tparam T scalar type. + * @tparam T Scalar marker type. + * @param inStore Original mask values. + * @param markerStore Receives marker values; has the input's tuple and element layout. + * @param dims Image dimensions in X, Y, Z order. + * @param kind Selects a height or border marker. + * @param height Finite offset in intensity units; nonnegative and whole for the integer opt-in. + * @param shouldCancel Requests cancellation between transfers. + * @param exactIntegerHeight Opt-in to exact, saturating integer offsets for a validated whole Height. + * @return The first transfer or dimension error, or a valid result. + * @pre @p markerStore has the same tuple/element layout as @p inStore (X fastest). */ template -Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, AbstractDataStore& markerStore, const SizeVec3& dims, ReconMarker kind, float64 height, const std::atomic_bool& shouldCancel) +Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, AbstractDataStore& markerStore, const SizeVec3& dims, ReconMarker kind, float64 height, const std::atomic_bool& shouldCancel, + bool exactIntegerHeight = false) { if(kind == ReconMarker::MinusHeight || kind == ReconMarker::PlusHeight) { @@ -643,9 +655,19 @@ Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, Abstract { return r; } - for(usize i = 0; i < count; ++i) + if(exactIntegerHeight && std::is_integral_v) { - buffer[i] = SaturateCastFromDouble(static_cast(buffer[i]) + signedHeight); + if constexpr(std::is_integral_v) + { + ApplyExactIntegerReconstructionHeight(nonstd::span(buffer.get(), count), height, kind == ReconMarker::MinusHeight); + } + } + else + { + for(usize i = 0; i < count; ++i) + { + buffer[i] = SaturateCastFromDouble(static_cast(buffer[i]) + signedHeight); + } } if(Result<> r = markerStore.copyFromBuffer(start, nonstd::span(buffer.get(), count)); r.invalid()) { @@ -865,5 +887,136 @@ inline usize SelectRelabelStagingValues(usize numValues, uint64 grantedBytes, us return stagingSlabs == 0 ? fallbackValues : stagingSlabs * slabPlan->slabValues; } +/** + * @brief Subtracts HConvex reconstruction values in one owned resident range without signed overflow. + * @tparam T Signed integer scalar type. + * @param inputValuesPtr Original image values. + * @param reconstructionValuesPtr Reconstruction values, each no greater than the corresponding input. + * @param outputValuesPtr Contrast output; may alias the reconstruction buffer. + * @param count Number of values in the range. + * @param start Global tuple offset for diagnostics. + * @param inputPath Original array path for diagnostics. + * @return The first contrast overflow, or a valid result. + */ +template +Result<> SubtractHConvexContrastRange(const T* inputValuesPtr, const T* reconstructionValuesPtr, T* outputValuesPtr, usize count, usize start, const DataPath& inputPath) +{ + static_assert(std::is_integral_v && std::is_signed_v); + std::atomic firstInvalid{count}; + ApplyElementwiseResidentRange(inputValuesPtr, reconstructionValuesPtr, outputValuesPtr, count, [&firstInvalid](const T* inputPtr, const T* reconstructionPtr, T* outputPtr, usize index) { + const T inputValue = inputPtr[index]; + const T reconstructionValue = reconstructionPtr[index]; + // max + a negative value is representable. Check before subtracting, including for int64 minima. + if(reconstructionValue < 0 && inputValue > std::numeric_limits::max() + reconstructionValue) + { + PublishSmallerIndex(firstInvalid, index); + return; + } + outputPtr[index] = static_cast(inputValue - reconstructionValue); + }); + const usize invalid = firstInvalid.load(std::memory_order_relaxed); + if(invalid != count) + { + return MakeErrorResult(-1874102, fmt::format("H Convex contrast at tuple {} of input array '{}' exceeds the {} output maximum {}. Reduce Height or explicitly convert the input to a wider or " + "floating-point type before this filter.", + start + invalid, inputPath.toString(), DataTypeToString(GetDataType()), std::numeric_limits::max())); + } + return {}; +} + +/** + * @brief Computes signed HConvex contrast through bounded chunks with serial store transfers. + * @tparam T Signed integer scalar type. + * @param input Original image store. + * @param reconstruction Reconstruction store. + * @param output Contrast output; may alias reconstruction. + * @param inputPath Original array path for diagnostics. + * @param shouldCancel Requests cancellation between chunks. + * @param chunkValues Maximum number of values in each owned buffer. + * @return The first transfer or contrast error, with warnings from completed transfers. + */ +template +Result<> SubtractHConvexContrastChunked(const AbstractDataStore& input, const AbstractDataStore& reconstruction, AbstractDataStore& output, const DataPath& inputPath, + const std::atomic_bool& shouldCancel, usize chunkValues = k_ElementwiseChunkValues) +{ + static_assert(std::is_integral_v && std::is_signed_v); + const usize size = input.getSize(); + chunkValues = std::max(1, chunkValues); + auto inputBuffer = std::make_unique_for_overwrite(std::min(chunkValues, size)); + auto reconstructionBuffer = std::make_unique_for_overwrite(std::min(chunkValues, size)); + Result<> accumulatedResult; + for(usize start = 0; start < size; start += chunkValues) + { + if(shouldCancel) + { + return accumulatedResult; + } + const usize count = std::min(chunkValues, size - start); + accumulatedResult = MergeResults(std::move(accumulatedResult), input.copyIntoBuffer(start, nonstd::span(inputBuffer.get(), count))); + if(accumulatedResult.invalid()) + { + return accumulatedResult; + } + accumulatedResult = MergeResults(std::move(accumulatedResult), reconstruction.copyIntoBuffer(start, nonstd::span(reconstructionBuffer.get(), count))); + if(accumulatedResult.invalid()) + { + return accumulatedResult; + } + accumulatedResult = MergeResults(std::move(accumulatedResult), SubtractHConvexContrastRange(inputBuffer.get(), reconstructionBuffer.get(), inputBuffer.get(), count, start, inputPath)); + if(accumulatedResult.invalid()) + { + return accumulatedResult; + } + accumulatedResult = MergeResults(std::move(accumulatedResult), output.copyFromBuffer(start, nonstd::span(inputBuffer.get(), count))); + if(accumulatedResult.invalid()) + { + return accumulatedResult; + } + } + return accumulatedResult; +} + +/** + * @brief Computes HConvex contrast, preserving the resident fast path and bounded serial store transfers. + * @tparam T Numeric scalar type. + * @param input Original image store. + * @param reconstruction Reconstruction store. + * @param output Contrast output; may alias reconstruction. + * @param inputPath Original array path for diagnostics. + * @param shouldCancel Requests cancellation before resident work or between chunks. + * @return The first transfer or signed contrast error, with warnings from completed transfers. + * + * Unsigned and floating arrays retain the existing subtraction. Signed arrays check before each subtraction. + * Completed chunks may already contain contrast when a later chunk fails; this operation does not promise atomic output. + */ +template +Result<> SubtractHConvexContrast(const AbstractDataStore& input, const AbstractDataStore& reconstruction, AbstractDataStore& output, const DataPath& inputPath, + const std::atomic_bool& shouldCancel) +{ + if constexpr(!std::is_integral_v || !std::is_signed_v) + { + return SubtractStores(input, reconstruction, output, shouldCancel); + } + else + { + if(shouldCancel) + { + return {}; + } + const auto* residentInputPtr = dynamic_cast*>(&input); + const auto* residentReconstructionPtr = dynamic_cast*>(&reconstruction); + auto* residentOutputPtr = dynamic_cast*>(&output); + if(residentInputPtr != nullptr && residentReconstructionPtr != nullptr && residentOutputPtr != nullptr) + { + const auto inputValues = residentInputPtr->createSpan(); + const auto reconstructionValues = residentReconstructionPtr->createSpan(); + auto outputValues = residentOutputPtr->createSpan(); + return SubtractHConvexContrastRange(inputValues.data(), reconstructionValues.data(), outputValues.data(), inputValues.size(), 0, inputPath); + } + + return SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel); + } +} + } // namespace detail } // namespace nx::core::ImageProcessing diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp index a1d1b70df2..084bf1df94 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp @@ -199,11 +199,26 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteBinaryMorphologyCompositeImageFilter(Da * reconstruction result is subtracted from the input in place after reconstruction (output = input - * reconstruction). Output type == input type (SameAsInput). The caller's preflight must have enforced a * scalar input (@ref PreflightImageFilter requireScalar=true) and, for the height markers, a finite - * @p height. + * @p height. The opt-in @p exactIntegerHeight also requires a nonnegative whole Height for integer images; + * it preserves native integer units and saturates markers at the type bounds. Other callers default to their existing arithmetic. + * @param dataStructure Owns the selected arrays and geometry. + * @param imageGeomPath Image geometry used for dimensions. + * @param inputArrayPath Scalar input image and reconstruction mask. + * @param outputArrayPath Receives reconstruction or contrast in the input type. + * @param op Selects reconstruction by dilation or erosion. + * @param fullyConnected Enables face, edge and vertex neighbors instead of faces only. + * @param markerKind Selects a height or border marker. + * @param height Validated finite height in intensity units. + * @param shouldCancel Requests cancellation during reconstruction and between transfers. + * @param messageHandler Receives progress messages. + * @param differenceFromInput Selects HConvex contrast with checked signed subtraction. + * @param exactIntegerHeight Selects exact native integer markers for a validated whole, nonnegative Height. + * @return A reconstruction, contrast or transfer error, or a valid result. */ NXIMAGEPROCESSING_EXPORT Result<> ExecuteMorphologicalReconstructionImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, ReconstructOp op, bool fullyConnected, detail::ReconMarker markerKind, float64 height, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, bool differenceFromInput = false); + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, bool differenceFromInput = false, + bool exactIntegerHeight = false); /** * @brief Execute opening-by-reconstruction (@p firstOp == Erode) or closing-by-reconstruction (@p firstOp == diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp index 919cac003c..5c9d25b4e6 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp @@ -23,7 +23,7 @@ namespace detail namespace { /** - * @brief Dispatched execute body for the three grayscale reconstruction filters. Derives the marker (@ref + * @brief Dispatched execute body for the grayscale reconstruction filters. Derives the marker (@ref * BuildReconstructionMarker) into an out-of-core-inheriting scratch store, then reconstructs it under the * original input (the mask) into the output. The engine's DispatchAlgorithm keys the in-core/out-of-core * path on the real input + output arrays (the scratch marker inherits the input's storage mode), so an @@ -42,6 +42,7 @@ struct MorphologicalReconstructionExecuteFn const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; bool differenceFromInput; // HConvex: after reconstruction, output = input - reconstruction (the h-dome) + bool ExactIntegerHeight = false; template Result<> operator()() const @@ -54,6 +55,7 @@ struct MorphologicalReconstructionExecuteFn std::shared_ptr> markerStore; const AbstractDataStore* marker = &inStore; ReconstructionMarkerOptions markerOptions; + markerOptions.ExactIntegerHeight = ExactIntegerHeight; const bool isHeightMarker = markerKind == ReconMarker::MinusHeight || markerKind == ReconMarker::PlusHeight; const bool isBorderMarker = markerKind == ReconMarker::FillholeMaxBorder || markerKind == ReconMarker::GrindPeakMinBorder; const bool useDerivedMarker = isBorderMarker || (inStore.getStoreType() == IDataStore::StoreType::OutOfCore && isHeightMarker); @@ -74,25 +76,25 @@ struct MorphologicalReconstructionExecuteFn // Marker scratch inherits the input's storage format (out-of-core input -> out-of-core scratch), keeping the // whole filter memory-bounded through the streamed sweep path. markerStore = DataStoreUtilities::CreateDataStoreWithFormat(inArray.getDataFormat(), inArray.getTupleShape(), std::vector{1}, IDataAction::Mode::Execute); - if(Result<> r = BuildReconstructionMarker(inStore, *markerStore, dims, markerKind, height, shouldCancel); r.invalid()) + if(auto markerResult = BuildReconstructionMarker(inStore, *markerStore, dims, markerKind, height, shouldCancel, ExactIntegerHeight); markerResult.invalid()) { - return r; + return markerResult; } marker = markerStore.get(); } // Mask == the ORIGINAL input; the engine reconstructs the marker under it into outStore. OOC detection uses // the real input (mask) + output arrays (the scratch marker has no IDataArray). const bool useTemporaryWork = markerKind == ReconMarker::FillholeMaxBorder || markerKind == ReconMarker::GrindPeakMinBorder; - if(Result<> r = ApplyMorphologicalReconstruction(*marker, inStore, outStore, dims, op, fullyConnected, inArray, outArray, shouldCancel, messageHandler, useTemporaryWork, markerOptions); - r.invalid()) + auto reconstructionResult = + ApplyMorphologicalReconstruction(*marker, inStore, outStore, dims, op, fullyConnected, inArray, outArray, shouldCancel, messageHandler, useTemporaryWork, markerOptions); + if(reconstructionResult.invalid()) { - return r; + return reconstructionResult; } if(differenceFromInput) { - // HConvex: output = input - reconstruction (the h-maxima result), i.e. the h-dome. SubtractStores is - // alias-safe (out == b), and hmax <= input everywhere so the difference never underflows. - return SubtractStores(inStore, outStore, outStore, shouldCancel); + // HConvex owns exact integer markers and checked signed contrast. Other callers retain their existing defaults. + return MergeResults(std::move(reconstructionResult), SubtractHConvexContrast(inStore, outStore, outStore, inputArrayPath, shouldCancel)); } return {}; } @@ -272,15 +274,15 @@ struct BinaryReconstructionOpeningExecuteFn // ----------------------------------------------------------------------------- Result<> ExecuteMorphologicalReconstructionImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, ReconstructOp op, bool fullyConnected, detail::ReconMarker markerKind, float64 height, const std::atomic_bool& shouldCancel, - const IFilter::MessageHandler& messageHandler, bool differenceFromInput) + const IFilter::MessageHandler& messageHandler, bool differenceFromInput, bool exactIntegerHeight) { using TypeSetPolicy = AllNumeric; const auto& inputArray = dataStructure.getDataRefAs(inputArrayPath); const DataType inputType = inputArray.getDataType(); const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); const SizeVec3 dims = imageGeom.getDimensions(); - detail::MorphologicalReconstructionExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, op, fullyConnected, markerKind, height, - shouldCancel, messageHandler, differenceFromInput}; + detail::MorphologicalReconstructionExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, op, fullyConnected, markerKind, height, shouldCancel, + messageHandler, differenceFromInput, exactIntegerHeight}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 727bcec418..2fd992b123 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -1,12 +1,37 @@ #include "ReconstructionFilterTestUtils.hpp" #include "NXImageProcessing/Filters/HConvexImageFilter.hpp" - +#include "NXImageProcessing/utils/ImageFilterExecutorDetail.hpp" + +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp" + +#include +#include +#include #include +#include +#include #include #include +#include +#include #include using namespace nx::core; @@ -17,8 +42,453 @@ namespace // Filter-local error codes for the Height guards (kept in sync with HConvexImageFilter.cpp). constexpr int32 k_NonFiniteHeight = -8553; constexpr int32 k_NegativeHeight = -8556; + +Arguments makeHConvexArguments(const DataPath& inputPath, float64 height, bool fullyConnected = false) +{ + Arguments args; + args.insertOrAssign(HConvexImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(HConvexImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(HConvexImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(height)); + args.insertOrAssign(HConvexImageFilter::k_FullyConnected_Key, std::make_any(fullyConnected)); + return args; +} + +class HConvexTransferStore : public DataStore +{ +public: + explicit HConvexTransferStore(int16 initialValue) + : DataStore(ShapeType{1, 1, 3}, ShapeType{1}, initialValue) + { + } + ~HConvexTransferStore() noexcept override = default; + HConvexTransferStore(const HConvexTransferStore&) = delete; + HConvexTransferStore(HConvexTransferStore&&) = delete; + HConvexTransferStore& operator=(const HConvexTransferStore&) = delete; + HConvexTransferStore& operator=(HConvexTransferStore&&) = delete; + + Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override + { + if(m_FailRead) + { + return MakeErrorResult(-18741001, "HConvex test input transfer failed."); + } + auto result = DataStore::copyIntoBuffer(start, buffer); + result.warnings().push_back(Warning{.code = 18741001, .message = "HConvex test input transfer warning."}); + return result; + } + + Result<> copyFromBuffer(usize start, nonstd::span buffer) override + { + if(m_FailWrite) + { + return MakeErrorResult(-18741002, "HConvex test output transfer failed."); + } + auto result = DataStore::copyFromBuffer(start, buffer); + result.warnings().push_back(Warning{.code = 18741002, .message = "HConvex test output transfer warning."}); + return result; + } + + void setFailRead(bool failRead) + { + m_FailRead = failRead; + } + + void setFailWrite(bool failWrite) + { + m_FailWrite = failWrite; + } + +private: + bool m_FailRead = false; + bool m_FailWrite = false; +}; } // namespace +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: integer fractional Height fails preflight", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + const float64 height = GENERATE(0.5, 2.5); + CAPTURE(height); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 2, 0}); + const HConvexImageFilter filter; + + // Integer images require a whole Height. Stop at preflight: the baseline must never execute this request. + const auto preflightResult = filter.preflight(dataStructure, makeHConvexArguments(inputPath, height)); + REQUIRE(preflightResult.outputActions.invalid()); + REQUIRE(preflightResult.outputActions.errors().size() == 1); + REQUIRE(preflightResult.outputActions.errors().front().code == -1874101); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast with unchanged type", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64, float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(fullyConnected); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 2, 1, {1, 3, 2, 4, 0, 5}); + const Arguments args = makeHConvexArguments(inputPath, 0.0, fullyConnected); + const HConvexImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 2, 3}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 2, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 6); + // Height 0 makes the marker equal to the mask. Reconstruction equals input, so input minus reconstruction is 0. + for(usize index = 0; index < outputStore.getSize(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == static_cast(0)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: floating fractional Height retains fractional contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(fullyConnected); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {0, 0, 0, 0, 2, 0, 0, 0, 0}); + const Arguments args = makeHConvexArguments(inputPath, 0.5, fullyConnected); + const HConvexImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 3, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + // For an isolated height-2 peak on zero background, reconstruction is 1.5 at the peak and 0 elsewhere. + const std::array expected = {0, 0, 0, 0, 0.5, 0, 0, 0, 0}; + REQUIRE(outputStore.getSize() == expected.size()); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: native 64-bit unit contrast above double precision", "[ImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + constexpr TestType inputValue = TestType{1} << 54; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); + const HConvexImageFilter filter; + const Arguments args = makeHConvexArguments(inputPath, 1.0); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 1); + // A one-pixel marker has no neighbor that can raise it: reconstruction is inputValue - 1, so contrast is 1. + // The baseline double conversion rounds the marker back to inputValue. Both values are safely inside T's range. + REQUIRE(outputStore.getValue(0) == TestType{1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::HConvexImageFilter: exact signed 64-bit marker boundaries in both directions", "[ImageProcessing][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + constexpr int64 minimum = std::numeric_limits::lowest(); + constexpr int64 maximum = std::numeric_limits::max(); + const bool lower = GENERATE(false, true); + std::array values = {minimum, minimum + 1, -1, 0, maximum}; + const std::array original = {minimum, minimum + 1, -1, 0, maximum}; + ImageProcessing::ReconstructionMarkerOptions options; + options.source = lower ? ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight : ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; + options.height = std::ldexp(1.0, 63); + options.ExactIntegerHeight = true; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(values.data(), values.size()), 0, {5, 1, 1}, options); + // MIN + 2^63 = 0; MAX - 2^63 = -1. Other values saturate at the corresponding native bound. + const std::array expected = lower ? std::array{minimum, minimum, minimum, minimum, -1} : std::array{0, 1, maximum, maximum, maximum}; + REQUIRE(values == expected); + DataStore input(ShapeType{5}, ShapeType{1}, 0); + DataStore marker(ShapeType{5}, ShapeType{1}, 0); + const auto inputResult = input.copyFromBuffer(0, nonstd::span(original.data(), original.size())); + SIMPLNX_RESULT_REQUIRE_VALID(inputResult); + const std::atomic_bool shouldCancel = false; + const auto markerResult = ImageProcessing::detail::BuildReconstructionMarker( + input, marker, {5, 1, 1}, lower ? ImageProcessing::detail::ReconMarker::MinusHeight : ImageProcessing::detail::ReconMarker::PlusHeight, options.height, shouldCancel, true); + SIMPLNX_RESULT_REQUIRE_VALID(markerResult); + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(marker.getValue(index) == expected.at(index)); + } +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: exact integer marker zero unit and huge Heights", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + constexpr TestType minimum = std::numeric_limits::lowest(); + constexpr TestType maximum = std::numeric_limits::max(); + const bool lower = GENERATE(false, true); + const float64 height = GENERATE(0.0, 1.0, std::ldexp(1.0, 64), std::numeric_limits::max()); + CAPTURE(lower, height); + std::array values = {minimum, 0, maximum}; + ImageProcessing::ReconstructionMarkerOptions options; + options.source = lower ? ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight : ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; + options.height = height; + options.ExactIntegerHeight = true; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(values.data(), values.size()), 0, {3, 1, 1}, options); + if(height == 0.0) + { + const std::array expected = {minimum, 0, maximum}; + REQUIRE(values == expected); + } + else if(height == 1.0) + { + const TestType zeroLowered = std::is_signed_v ? static_cast(-1) : TestType{0}; + const std::array expected = + lower ? std::array{minimum, zeroLowered, static_cast(maximum - 1)} : std::array{static_cast(minimum + 1), 1, maximum}; + REQUIRE(values == expected); + } + else + { + const TestType bound = lower ? minimum : maximum; + const std::array expected = {bound, bound, bound}; + REQUIRE(values == expected); + } +} + +TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker arithmetic requires explicit opt-in", "[ImageProcessing][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + constexpr int64 inputValue = (int64{1} << 54) + 1; + constexpr int64 expectedExistingValue = int64{1} << 54; + std::array values = {inputValue}; + ImageProcessing::ReconstructionMarkerOptions options; + options.source = ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; + options.height = 0.0; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(values.data(), values.size()), 0, {1, 1, 1}, options); + REQUIRE(values.at(0) == expectedExistingValue); + const DataStore input(ShapeType{1}, ShapeType{1}, inputValue); + DataStore marker(ShapeType{1}, ShapeType{1}, 0); + const std::atomic_bool shouldCancel = false; + const auto markerResult = ImageProcessing::detail::BuildReconstructionMarker(input, marker, {1, 1, 1}, ImageProcessing::detail::ReconMarker::MinusHeight, 0.0, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(markerResult); + REQUIRE(marker.getValue(0) == expectedExistingValue); +} + +TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker Height immediately below 2^64", "[ImageProcessing][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + ImageProcessing::ReconstructionMarkerOptions options; + options.source = ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight; + options.height = 0x1.fffffffffffffp63; + options.ExactIntegerHeight = true; + std::array unsignedValues = {std::numeric_limits::max()}; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(unsignedValues.data(), unsignedValues.size()), 0, {1, 1, 1}, options); + REQUIRE(unsignedValues.at(0) == uint64{2047}); + std::array loweredValues = {std::numeric_limits::max()}; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(loweredValues.data(), loweredValues.size()), 0, {1, 1, 1}, options); + REQUIRE(loweredValues.at(0) == -9223372036854773761LL); + options.source = ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; + std::array raisedValues = {std::numeric_limits::lowest()}; + ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(raisedValues.data(), raisedValues.size()), 0, {1, 1, 1}, options); + REQUIRE(raisedValues.at(0) == 9223372036854773760LL); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: signed contrast overflow and adjacent representable contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, int16, int32, int64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const bool overflow = GENERATE(false, true); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(overflow, fullyConnected); + constexpr TestType minimum = std::numeric_limits::lowest(); + constexpr TestType maximum = std::numeric_limits::max(); + const TestType peak = overflow ? maximum : TestType{-1}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {minimum, peak, minimum}); + const HConvexImageFilter filter; + // Height exceeds the entire native range, so reconstruction is the minimum everywhere. + const Arguments args = makeHConvexArguments(inputPath, std::ldexp(1.0, static_cast(8 * sizeof(TestType))), fullyConnected); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + if(overflow) + { + REQUIRE(executeResult.result.invalid()); + REQUIRE(executeResult.result.errors().size() == 1); + REQUIRE(executeResult.result.errors().front().code == -1874102); + REQUIRE(executeResult.result.errors().front().message.find("tuple 1") != std::string::npos); + REQUIRE(executeResult.result.errors().front().message.find(inputPath.toString()) != std::string::npos); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 3); + REQUIRE(outputStore.getValue(0) == 0); + REQUIRE(outputStore.getValue(1) == maximum); + REQUIRE(outputStore.getValue(2) == 0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast at native integer bounds", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {std::numeric_limits::lowest(), 0, std::numeric_limits::max()}); + const HConvexImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHConvexArguments(inputPath, 0.0)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 3); + for(usize index = 0; index < outputStore.getSize(); ++index) + { + REQUIRE(outputStore.getValue(index) == 0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::HConvexImageFilter: whole Height contrast is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const int32 background = GENERATE(-50, 50); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {background, background + 20, background}); + const HConvexImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHConvexArguments(inputPath, 10.0)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + REQUIRE(dataStructure.getDataRefAs(outputPath).getDataType() == DataType::int32); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 3); + REQUIRE(outputStore.getValue(0) == 0); + REQUIRE(outputStore.getValue(1) == 10); + REQUIRE(outputStore.getValue(2) == 0); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::HConvexImageFilter: chunked contrast preserves transfer Results and cancellation", "[ImageProcessing][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + HConvexTransferStore input(2); + HConvexTransferStore reconstruction(1); + HConvexTransferStore output(-1); + std::atomic_bool shouldCancel = false; + const DataPath inputPath({"Input"}); + SECTION("successful chunks retain all transfer warnings") + { + const auto result = ImageProcessing::detail::SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel, 2); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 6); + for(usize index = 0; index < 3; ++index) + { + REQUIRE(output.getValue(index) == 1); + } + } + SECTION("input failure returns its error before output writes") + { + input.setFailRead(true); + const auto result = ImageProcessing::detail::SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel, 2); + REQUIRE(result.invalid()); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == -18741001); + REQUIRE(output.getValue(0) == -1); + } + SECTION("reconstruction failure retains earlier input warning") + { + reconstruction.setFailRead(true); + const auto result = ImageProcessing::detail::SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel, 2); + REQUIRE(result.invalid()); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == -18741001); + REQUIRE(result.warnings().size() == 1); + REQUIRE(output.getValue(0) == -1); + } + SECTION("output failure retains both input warnings") + { + output.setFailWrite(true); + const auto result = ImageProcessing::detail::SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel, 2); + REQUIRE(result.invalid()); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == -18741002); + REQUIRE(result.warnings().size() == 2); + REQUIRE(output.getValue(0) == -1); + } + SECTION("pre-cancelled work leaves output unchanged") + { + shouldCancel = true; + const auto result = ImageProcessing::detail::SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel, 2); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().empty()); + REQUIRE(output.getValue(0) == -1); + } +} + // ----------------------------------------------------------------------------- // (2) Hand-computed behavior on a small 2D image: a flat background maps to 0; a bright bump whose contrast is // >= Height maps to exactly Height (its dome is capped); a bright bump whose contrast is < Height maps to its diff --git a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp index b58fe60c2f..25c5283b0f 100644 --- a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp @@ -40,10 +40,108 @@ struct ReconstructionMarkerOptions { ReconstructionMarkerSource source = ReconstructionMarkerSource::Provided; float64 height = 0.0; + // Callers opt in only after validating a finite, nonnegative whole Height for integer images. + bool ExactIntegerHeight = false; }; namespace detail { +/** + * @brief Lowers an integer marker value exactly, saturating at the native type minimum. + * @tparam T Integer scalar type. + * @param value Native input value. + * @param heightUnits Nonnegative integer Height below 2^64. + * @return The lowered value, saturated to the native minimum. + */ +template +T SubtractExactIntegerReconstructionHeight(T value, uint64 heightUnits) +{ + static_assert(std::is_integral_v); + if constexpr(std::is_signed_v) + { + if(value < 0) + { + // For a negative value this distance fits the signed type, including when value is its minimum. + const uint64 distanceToMinimum = static_cast(value - std::numeric_limits::lowest()); + return heightUnits >= distanceToMinimum ? std::numeric_limits::lowest() : static_cast(value - static_cast(heightUnits)); + } + const uint64 positiveValue = static_cast(value); + if(heightUnits <= positiveValue) + { + return static_cast(positiveValue - heightUnits); + } + const uint64 negativeMagnitude = heightUnits - positiveValue; + const uint64 minimumMagnitude = static_cast(std::numeric_limits::max()) + uint64{1}; + return negativeMagnitude >= minimumMagnitude ? std::numeric_limits::lowest() : static_cast(-static_cast(negativeMagnitude)); + } + else + { + const uint64 unsignedValue = static_cast(value); + return heightUnits >= unsignedValue ? T{0} : static_cast(unsignedValue - heightUnits); + } +} + +/** + * @brief Raises an integer marker value exactly, saturating at the native type maximum. + * @tparam T Integer scalar type. + * @param value Native input value. + * @param heightUnits Nonnegative integer Height below 2^64. + * @return The raised value, saturated to the native maximum. + */ +template +T AddExactIntegerReconstructionHeight(T value, uint64 heightUnits) +{ + static_assert(std::is_integral_v); + if constexpr(std::is_signed_v) + { + if(value >= 0) + { + const uint64 distanceToMaximum = static_cast(std::numeric_limits::max() - value); + return heightUnits >= distanceToMaximum ? std::numeric_limits::max() : static_cast(value + static_cast(heightUnits)); + } + // Add one before negating so even a signed minimum has a representable intermediate. + const uint64 negativeMagnitude = static_cast(-(value + T{1})) + uint64{1}; + if(heightUnits < negativeMagnitude) + { + const uint64 remainingMagnitude = negativeMagnitude - heightUnits; + const uint64 minimumMagnitude = static_cast(std::numeric_limits::max()) + uint64{1}; + return remainingMagnitude == minimumMagnitude ? std::numeric_limits::lowest() : static_cast(-static_cast(remainingMagnitude)); + } + const uint64 positiveValue = heightUnits - negativeMagnitude; + return positiveValue >= static_cast(std::numeric_limits::max()) ? std::numeric_limits::max() : static_cast(positiveValue); + } + else + { + const uint64 distanceToMaximum = static_cast(std::numeric_limits::max() - value); + return heightUnits >= distanceToMaximum ? std::numeric_limits::max() : static_cast(static_cast(value) + heightUnits); + } +} + +/** + * @brief Offsets integer marker values exactly, saturating at the native type bounds. + * @tparam T Integer scalar type. + * @param values Owned marker buffer to update. + * @param height Finite, nonnegative, whole Height, validated by the opting-in caller. + * @param lower Selects subtraction when true and addition when false. + * + * The Height conversion is once per buffer. Values never pass through floating point, and signed minima are never negated. + */ +template +void ApplyExactIntegerReconstructionHeight(nonstd::span values, float64 height, bool lower) +{ + static_assert(std::is_integral_v); + if(height >= std::ldexp(1.0, 64)) + { + std::fill(values.begin(), values.end(), lower ? std::numeric_limits::lowest() : std::numeric_limits::max()); + return; + } + const uint64 heightUnits = static_cast(height); + for(T& value : values) + { + value = lower ? SubtractExactIntegerReconstructionHeight(value, heightUnits) : AddExactIntegerReconstructionHeight(value, heightUnits); + } +} + template T SaturateReconstructionMarkerValue(float64 value) { @@ -81,6 +179,14 @@ void ApplyReconstructionMarkerOptions(nonstd::span values, usize start, const } if(options.source == ReconstructionMarkerSource::MaskMinusHeight || options.source == ReconstructionMarkerSource::MaskPlusHeight) { + if constexpr(std::is_integral_v) + { + if(options.ExactIntegerHeight) + { + ApplyExactIntegerReconstructionHeight(values, options.height, options.source == ReconstructionMarkerSource::MaskMinusHeight); + return; + } + } const float64 signedHeight = options.source == ReconstructionMarkerSource::MaskMinusHeight ? -options.height : options.height; for(T& value : values) { From f68d4f6b69379730f5c59a73512541f62ebfa34b Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 21:50:06 -0400 Subject: [PATCH 14/98] BUG: Preserve native HMinima intensities and Height offsets Require whole Height for integer images and opt into exact native marker raising with safe saturation. Preserve floating fractions and output types. Document intensity output, Float64 Height limits, migration and memory routes. Paired full Release builds and final refreshes pass: 48 in-core / 73 OOC HMinima cases, plus 40 / 46 shared-caller controls. Source review and clang-format pass. Configured static checks remain nonpassing on documented existing/shared/framework diagnostics. Signed-off-by: Jessica Marquis --- .../docs/HMinimaImageFilter.md | 22 +- .../Filters/HMinimaImageFilter.cpp | 22 +- .../Filters/HMinimaImageFilter.hpp | 18 +- .../test/HMinimaImageFilterTest.cpp | 320 ++++++++++++++++++ 4 files changed, 361 insertions(+), 21 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/HMinimaImageFilter.md b/src/Plugins/NXImageProcessing/docs/HMinimaImageFilter.md index af3a821585..3a9406a985 100644 --- a/src/Plugins/NXImageProcessing/docs/HMinimaImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/HMinimaImageFilter.md @@ -8,20 +8,24 @@ NXImageProcessing (Morphology) ## Description -Applies the **H-Minima transform** to a grayscale image. The transform removes every *regional minimum* whose **depth** -- its contrast below the surrounding level -- is less than the **Height** parameter, and raises each surviving minimum by exactly **Height**. Shallow dark features (whose contrast is below **Height**) are filled up to their surrounding background, while deeper dark features are preserved but raised in intensity by **Height**. +Applies the **H-Minima transform** to a grayscale image. A **regional minimum** is a connected region below its surrounding intensities. Its **depth** is the intensity difference to that surrounding level. The transform suppresses minima whose depth is less than **Height**. Deeper minima rise by Height, subject to the input type's maximum. The H-Minima transform is commonly used to remove insignificant intensity valleys caused by noise prior to a minima-detection or watershed step, so that only sufficiently prominent minima remain. It is the erosion counterpart of the H-Maxima transform. -Internally the filter performs a **reconstruction-by-erosion** of a marker image equal to the saturating sum `input + Height`, using the input image as the mask. Because the marker is formed with a *saturating* cast, a sum that overflows the element type's range is **clamped** to the type maximum (rather than wrapping), matching the legacy ITK behavior exactly. +The filter performs **reconstruction by erosion** of the raised marker `input + Height`, with the original input as its lower mask. Erosion lowers marker values through connected neighbors, but cannot lower them below the input mask. Output contains these reconstructed intensities. For example, an isolated minimum *30* in background *50* becomes *40* at Height *10*. It does not become a contrast value of *10*. A one-pixel image has no neighbor to lower its marker, so its output is the raised marker. -The input array must be single-component (scalar); the output element type matches the input element type. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK H Minima Image Filter. When all data is resident it uses a fast in-core hybrid reconstruction; when the arrays are out-of-core it streams the computation so memory use stays bounded regardless of image size. Both paths produce bit-identical output, and the result matches the legacy ITK filter exactly. +The input must have one component per tuple, called a **scalar image**. Output type, tuple dimensions and component dimensions match input. Output storage does not increase automatically. Resident and disk-backed arrays use the same numeric rules. ### Height -The *Height* parameter sets the contrast threshold in the same units as the image intensities: +**Height** is a contrast offset in the input's intensity units. It must be finite and nonnegative. -- Regional minima with a contrast **less than** *Height* are removed (filled up to the surrounding level). -- Regional minima with a contrast **greater than or equal to** *Height* survive but are raised by exactly *Height*. +- Integer images require a whole-number Height. Fractions such as *0.5* or *2.5* fail preflight. Choose an intentional whole Height, or explicitly convert the input to floating point before using a fraction. +- Floating images allow fractional Height. Their marker uses floating arithmetic and rounds to the input type's precision. +- Integer markers use exact native addition and saturate at the input type's maximum. Native 64-bit input values do not pass through floating point. Height *0* preserves every integer input value exactly, including native bounds. +- Height is stored as a float64 parameter. Above *2^53*, float64 cannot distinguish every consecutive integer Height. The filter uses the whole value represented by that parameter. A Height of *2^64* or more raises every integer marker value to the type maximum. + +For existing integer pipelines with fractional Height, choose a whole Height or convert the input explicitly. Exact arithmetic can change results that previously lost native 64-bit units. Review downstream minima selection, watershed seeds and segmentation when migrating such a pipeline. ### Fully Connected @@ -34,7 +38,11 @@ Geodesic morphology and grayscale reconstruction are described in Chapter 6 of P ## Algorithm -The filter raises the input by **Height** and reconstructs that marker under the input by erosion. Resident arrays use the Vincent hybrid reconstruction, whose raster, anti-raster, and fold-to-convergence sweeps run as dependency-exact, block-wavefront parallel plane sweeps when the reconstruction uses face connectivity (**Fully Connected** off); with full connectivity it falls back to the exact serial sweeps, since a same-row diagonal read can land in the horizontally adjacent block, which no rectangular block schedule can order. Disk-backed reconstruction iterates a forward raster sweep followed by a reverse anti-raster sweep over consecutive Z-plane slabs until stable, skipping any slab whose planes are already proven unchanged since that direction's last visit. Within a plane, the raster/anti-raster sweep is tiled into independent blocks that run in parallel; under face connectivity every voxel still sees exactly the values the un-blocked sweep would produce, and under full connectivity the iterated forward/reverse passes still converge to the identical final result. Each work and mask slab targets 16 MiB, plus one boundary plane. At least one plane is processed, so a single plane wider than the target can make the buffers exceed 16 MiB. Update order and output are unchanged, preserving exact storage-path parity. +The filter raises the input by Height and reconstructs that marker under the input by erosion. Resident input uses a materialized marker and keeps the Vincent hybrid reconstruction route. This route can allocate complete resident images for its marker and reconstruction work. + +Disk-backed input derives marker values in owned reconstruction buffers. It does not require a separate complete resident marker image. Reconstruction keeps its existing working-memory reservation and sweep routes. A granted working set can hold a complete image when it fits. Otherwise, 2-D data uses rows or tiles, and 3-D data uses slabs with a boundary plane. A 3-D streaming plan needs space for two working planes and one boundary plane. Execution fails if the grant cannot hold that minimum. The working-buffer budget excludes data stores, their cache and other process memory. + +Store transfers remain serial; parallel work uses stable owned buffers. Transfer errors, cancellation and existing allocation fallbacks retain their existing behavior. A later execution failure can leave completed output values or chunks written. An error does not promise an unchanged output array. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp index d481d892f0..c906ef5557 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp @@ -2,7 +2,12 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/BoolParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" @@ -15,7 +20,7 @@ #include "simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp" #include "simplnx/Utilities/SIMPLConversion.hpp" -#include +#include #include @@ -28,6 +33,7 @@ namespace // the -8552 (non-finite) and -8555 (negative) slots for its Height guards (HMaxima owns -8551/-8554). constexpr int32 k_NonFiniteHeight = -8552; constexpr int32 k_NegativeHeight = -8555; +constexpr int32 k_FractionalIntegerHeight = -1874111; } // namespace namespace nx::core @@ -58,7 +64,8 @@ Parameters HMinimaImageFilter::parameters() const params.insertSeparator(Parameters::Separator{"Input Parameter(s)"}); params.insert(std::make_unique(k_Height_Key, "Height", "The height (contrast) below which a regional minimum is suppressed. Every regional minimum whose depth is less than Height is removed; each " - "surviving minimum is raised by exactly Height. Must be finite.", + "surviving minimum is raised by exactly Height. Must be finite and nonnegative. Integer input requires a whole-number Height; floating input " + "allows fractions.", 2.0)); params.insert(std::make_unique(k_FullyConnected_Key, "Fully Connected", "Whether the connected components are defined strictly by face connectivity (Off) or by face+edge+vertex connectivity (On). Default is Off. For " @@ -106,6 +113,14 @@ IFilter::PreflightResult HMinimaImageFilter::preflightImpl(const DataStructure& { return {MakeErrorResult(k_NegativeHeight, fmt::format("H Minima requires a non-negative Height, but the supplied value was {}.", height))}; } + const auto* inputArrayPtr = dataStructure.getDataAs(selectedInputArray); + if(inputArrayPtr != nullptr && ImageProcessing::GetIntegerScalarTypes().contains(inputArrayPtr->getDataType()) && std::trunc(height) != height) + { + return {.outputActions = MakeErrorResult( + k_FractionalIntegerHeight, fmt::format("H Minima requires a whole-number Height for integer input array '{}', but received {}. Choose an intentional whole Height or explicitly " + "convert the input to floating point before using a fractional Height.", + selectedInputArray.toString(), height))}; + } // requireScalar=true: the grayscale reconstruction engine is defined on scalar images (matching the legacy ITK // operator); the shared preflight also validates type membership and the geometry/tuple match. @@ -126,7 +141,8 @@ Result<> HMinimaImageFilter::executeImpl(DataStructure& dataStructure, const Arg // HMinima == reconstruct-by-erosion of the saturating (input + Height) marker under the input mask; unlike // HMaxima the legacy ITK filter exposes a FullyConnected option, so honor it here. return ImageProcessing::ExecuteMorphologicalReconstructionImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Erosion, fullyConnected, - ImageProcessing::detail::ReconMarker::PlusHeight, height, shouldCancel, messageHandler); + ImageProcessing::detail::ReconMarker::PlusHeight, height, shouldCancel, messageHandler, /*differenceFromInput=*/false, + /*exactIntegerHeight=*/true); } namespace diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.hpp index f88b679ed0..aca34154c7 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.hpp @@ -9,19 +9,15 @@ namespace nx::core { /** * @class HMinimaImageFilter - * @brief ITK-free, out-of-core-capable H-Minima transform (suppresses regional minima whose depth/contrast is - * below a user-supplied Height). + * @brief Suppresses shallow regional minima by reconstructing the raised input marker by erosion. * - * The H-Minima transform removes every regional minimum whose depth (contrast below the surrounding level) is - * less than @c Height, and raises each surviving minimum by exactly @c Height, leaving the background otherwise - * unchanged. It is implemented as reconstruction-by-erosion of a marker image equal to the saturating sum - * (input + Height) under the input itself as the mask (the shared D3-split morphological reconstruction engine). - * The saturating marker matches the legacy ITK H Minima Image Filter exactly, including the clamp (rather than - * wrap) when (input + Height) overflows the element type's range. + * The marker is input plus Height, and the original input is the lower mask for reconstruction. + * Integer images require finite, nonnegative whole Height values. Integer marker addition uses native values and saturates at the type maximum. + * Height 0 preserves integer input exactly, including native 64-bit units. Floating images retain fractional Height and existing floating arithmetic. + * Height uses float64 precision, which cannot distinguish every consecutive integer above 2^53. * - * Output type == input type; the input must be single-component (scalar). This is out-of-core capable (the legacy - * filter rejected chunked arrays): reconstruction only copies or clamps existing values, so the result is - * bit-for-bit identical for integer AND float types and matches the legacy ITK output exactly. + * Input must have one component per tuple. Output contains reconstructed intensities with the same type and shape as input. + * Resident and disk-backed reconstruction retain their existing storage routes. Corrected integer markers can change downstream minima and watershed seeds. */ class NXIMAGEPROCESSING_EXPORT HMinimaImageFilter : public IFilter { diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index 22bbaa04df..a0e8b8bd28 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -2,11 +2,30 @@ #include "NXImageProcessing/Filters/HMinimaImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include + +#include #include +#include #include #include +#include +#include #include using namespace nx::core; @@ -17,8 +36,302 @@ namespace // Filter-local error codes for the Height guards (kept in sync with HMinimaImageFilter.cpp). constexpr int32 k_NonFiniteHeight = -8552; constexpr int32 k_NegativeHeight = -8555; + +Arguments makeHMinimaArguments(const DataPath& inputPath, float64 height, bool fullyConnected = false) +{ + Arguments args; + args.insertOrAssign(HMinimaImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(HMinimaImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(HMinimaImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(height)); + args.insertOrAssign(HMinimaImageFilter::k_FullyConnected_Key, std::make_any(fullyConnected)); + return args; +} } // namespace +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: integer fractional Height fails preflight", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + const float64 height = GENERATE(0.5, 2.5); + CAPTURE(height); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 2, 0}); + const HMinimaImageFilter filter; + const auto wholeHeightPreflightResult = filter.preflight(dataStructure, makeHMinimaArguments(inputPath, 2.0)); + SIMPLNX_RESULT_REQUIRE_VALID(wholeHeightPreflightResult.outputActions); + + // Integer images require a whole Height. Stop at preflight: the baseline must never execute this request. + const auto preflightResult = filter.preflight(dataStructure, makeHMinimaArguments(inputPath, height)); + REQUIRE(preflightResult.outputActions.invalid()); + REQUIRE(preflightResult.outputActions.errors().size() == 1); + REQUIRE(preflightResult.outputActions.errors().front().code == -1874111); + REQUIRE(preflightResult.outputActions.errors().front().message.find(inputPath.toString()) != std::string::npos); + REQUIRE(preflightResult.outputActions.errors().front().message.find(height == 0.5 ? "0.5" : "2.5") != std::string::npos); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves values and type", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, + uint64, float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(fullyConnected); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 2, 1, {1, 3, 2, 4, 0, 5}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const Arguments args = makeHMinimaArguments(inputPath, 0.0, fullyConnected); + const HMinimaImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 2, 3}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 2, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 6); + // Height 0 makes the marker equal to the mask, so reconstruction preserves every input value. + const std::array expected = {1, 3, 2, 4, 0, 5}; + for(usize index = 0; index < outputStore.getSize(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: floating fractional Height retains fractional minima", "[ImageProcessing][Issue1874][HMinimaImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(fullyConnected); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {2, 2, 2, 2, 0, 2, 2, 2, 2}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const Arguments args = makeHMinimaArguments(inputPath, 0.5, fullyConnected); + const HMinimaImageFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 3, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + // For an isolated zero-valued minimum on background 2, erosion reconstructs 0.5 at the minimum and 2 elsewhere. + const std::array expected = {2, 2, 2, 2, 0.5, 2, 2, 2, 2}; + REQUIRE(outputStore.getSize() == expected.size()); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: native 64-bit marker preserves units above double precision", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + constexpr TestType inputValue = TestType{1} << 54; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const HMinimaImageFilter filter; + const Arguments args = makeHMinimaArguments(inputPath, 1.0); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 1); + // A one-pixel marker has no neighbor that can lower it: reconstruction remains the exact inputValue + 1. + // The baseline double conversion rounds the marker back to inputValue. Both values are safely inside T's range. + REQUIRE(outputStore.getValue(0) == inputValue + TestType{1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves native integer bounds", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(scenario, fullyConnected); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + constexpr TestType minimum = std::numeric_limits::lowest(); + constexpr TestType maximum = std::numeric_limits::max(); + const std::array expected = {minimum, minimum + TestType{1}, maximum - TestType{1}, maximum}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 2, 2, 1, {minimum, minimum + TestType{1}, maximum - TestType{1}, maximum}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const HMinimaImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHMinimaArguments(inputPath, 0.0, fullyConnected)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 2, 2}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == expected.size()); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height raises or saturates native integer markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool fullyConnected = GENERATE(false, true); + const bool atMaximum = GENERATE(false, true); + const float64 height = GENERATE(1.0, std::ldexp(1.0, 64), std::numeric_limits::max()); + CAPTURE(scenario, fullyConnected, atMaximum, height); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + constexpr TestType minimum = std::numeric_limits::lowest(); + constexpr TestType maximum = std::numeric_limits::max(); + const TestType inputValue = atMaximum ? maximum : minimum; + // A single pixel retains its marker. One raises MIN by one; a Height of at least 2^64 saturates every integer type. + const TestType expected = atMaximum || height > 1.0 ? maximum : static_cast(minimum + TestType{1}); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const HMinimaImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHMinimaArguments(inputPath, height, fullyConnected)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 1); + REQUIRE(outputStore.getValue(0) == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: large whole Height retains exact native 64-bit markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool fullyConnected = GENERATE(false, true); + const float64 height = GENERATE(std::ldexp(1.0, 63), 0x1.fffffffffffffp63); + CAPTURE(scenario, fullyConnected, height); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {std::numeric_limits::lowest()}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const HMinimaImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHMinimaArguments(inputPath, height, fullyConnected)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == 1); + // The second Height is exactly 2^64 - 2048, the largest float64 Height below 2^64. + if constexpr(std::is_signed_v) + { + REQUIRE(outputStore.getValue(0) == (height == std::ldexp(1.0, 63) ? int64{0} : int64{9223372036854773760LL})); + } + else + { + REQUIRE(outputStore.getValue(0) == (height == std::ldexp(1.0, 63) ? uint64{9223372036854775808ULL} : uint64{18446744073709549568ULL})); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HMinimaImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool fullyConnected = GENERATE(false, true); + const bool positiveBaseline = GENERATE(false, true); + CAPTURE(scenario, fullyConnected, positiveBaseline); + UnitTest::AlgorithmTestScope algorithmTestScope(scenario); + const std::vector input = positiveBaseline ? std::vector{50, 30, 50} : std::vector{-50, -70, -50}; + const std::array expected = positiveBaseline ? std::array{50, 40, 50} : std::array{-50, -60, -50}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, input); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + algorithmTestScope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const HMinimaImageFilter filter; + const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, makeHMinimaArguments(inputPath, 10.0, fullyConnected)); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + algorithmTestScope.requireExpectedStore(output); + REQUIRE(output.getDataType() == DataType::int32); + REQUIRE(output.getTupleShape() == ShapeType{1, 1, 3}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + REQUIRE(outputStore.getSize() == expected.size()); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + REQUIRE(outputStore.getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + // ----------------------------------------------------------------------------- // (2) Hand-computed behavior on a small 2D image: a flat background with two isolated dark dents. The dent // whose contrast (>= Height) survives is raised by exactly Height; the dent whose contrast (< Height) is @@ -48,6 +361,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: hand-computed minima suppressi rt::RunReconstructionFilter(filter, ds, inputPath, rt::HeightAndFullyConnectedParamSetter(), /*fullyConnected=*/false, height); const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + REQUIRE_NOTHROW(ds.getDataRefAs>(outputPath)); const auto& outStore = ds.getDataRefAs>(outputPath).getDataStoreRef(); REQUIRE(outStore.getValue(rt::FlatIndex(2, 2, 0, DX, DY)) == deepDent + static_cast(height)); // 30 -> 40 (raised by Height) @@ -84,6 +398,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: hand-computed minima suppressi UnitTest::AlgorithmTestScope scope(scenario); DataStructure ds; const DataPath inputPath = rt::BuildImageFromPattern(ds, DX, DY, DZ, pattern); + REQUIRE_NOTHROW(ds.getDataRefAs(inputPath)); scope.requireExpectedStore(ds.getDataRefAs(inputPath)); HMinimaImageFilter filter; @@ -98,6 +413,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: hand-computed minima suppressi SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + REQUIRE_NOTHROW(ds.getDataRefAs>(outputPath)); auto& outArray = ds.getDataRefAs>(outputPath); scope.requireExpectedStore(outArray); const auto& outStore = outArray.getDataStoreRef(); @@ -127,6 +443,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: Height 0 is the identity", "[N rt::RunReconstructionFilter(filter, ds, inputPath, rt::HeightAndFullyConnectedParamSetter(), /*fullyConnected=*/false, /*height=*/0.0); const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + REQUIRE_NOTHROW(ds.getDataRefAs>(outputPath)); const auto& outStore = ds.getDataRefAs>(outputPath).getDataStoreRef(); REQUIRE(outStore.getSize() == pattern.size()); for(usize i = 0; i < outStore.getSize(); ++i) @@ -160,6 +477,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: uint8 full-overflow clamps to rt::RunReconstructionFilter(filter, ds, inputPath, rt::HeightAndFullyConnectedParamSetter(), /*fullyConnected=*/false, height); const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + REQUIRE_NOTHROW(ds.getDataRefAs>(outputPath)); const auto& outStore = ds.getDataRefAs>(outputPath).getDataStoreRef(); for(usize i = 0; i < outStore.getSize(); ++i) { @@ -221,6 +539,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: Preflight non-finite Height gu TEST_CASE("NXImageProcessing::HMinimaImageFilter: FromSIMPLJson", "[NXImageProcessing][HMinimaImageFilter]") { + UnitTest::LoadPlugins(); const nlohmann::json json = {{"Height", 3.5}, {"FullyConnected", true}, {"SelectedCellArrayPath", {{"Data Container Name", "Image Geometry"}, {"Attribute Matrix Name", "CellData"}, {"Data Array Name", "Input"}}}, @@ -242,6 +561,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: FromSIMPLJson", "[NXImageProce // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima)", "[NXImageProcessing][ItkGolden][HMinimaImageFilter]") { + UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("RA-Short.nrrd", "7778067eeb752b6ac396dd9e362e8346", [](Arguments& args) { args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(2000.0)); }); } From 9bf8b073bfef76443f50ffc07106162b918f004e Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 22:56:10 -0400 Subject: [PATCH 15/98] BUG: Check Curvature Flow terminal numeric conversions Validate active controls and copy native values exactly for zero operations. Check final integer conversion after truncation, report clipping once, and reject nonfinite integer results while retaining floating behavior and float64 evolution. Cover resident, tiled and plane routes, warning propagation, transfer failures and cancellation. Explain precision limits and downstream clipping effects in the filter help. Signed-off-by: Jessica Marquis --- CMakeLists.txt | 1 + .../docs/CurvatureFlowImageFilter.md | 14 +- .../Filters/CurvatureFlowImageFilter.cpp | 18 +- .../utils/ImageFilterTemplateExecutors.hpp | 9 +- .../test/CurvatureFlowImageFilterTest.cpp | 793 ++++++++++++++++++ .../FiniteDifferenceEngine.hpp | 463 +++++++--- .../ImageProcessingConstants.hpp | 5 + .../TerminalImageConversion.hpp | 78 ++ 8 files changed, 1267 insertions(+), 114 deletions(-) create mode 100644 src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ab3dbf3cbd..1329d712c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -606,6 +606,7 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageGeometryCrop.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageGeometryResample.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/WorkingMemory.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/TerminalImageConversion.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ProgressEstimator.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ThrottledMessageHandler.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/StringUtilities.hpp diff --git a/src/Plugins/NXImageProcessing/docs/CurvatureFlowImageFilter.md b/src/Plugins/NXImageProcessing/docs/CurvatureFlowImageFilter.md index 0a8337318d..68557f2aff 100644 --- a/src/Plugins/NXImageProcessing/docs/CurvatureFlowImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/CurvatureFlowImageFilter.md @@ -16,7 +16,15 @@ where kappa is the curvature of the iso-brightness contour passing through each The evolution is computed with a multi-threaded finite-difference solver: at every iteration, an update is computed at each voxel from a frozen (Jacobi) neighborhood of the current image using a zero-flux Neumann (edge-clamp) boundary condition, and then `Output = Output + TimeStep * Update` is applied everywhere. The per-axis image spacing is used when computing derivatives, so the result depends on the Image Geometry's spacing. -The input array must be single-component (scalar) and may be **any** scalar type (signed/unsigned integer or floating point). The output has the **same type as the input** (type-preserving); for an integer input, each iteration's update is truncated to the input type exactly as the legacy filter does. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Curvature Flow Image Filter, and matches it exactly (bit-exact for integer types; exact/near-exact for floating point). +The input array must be single-component (scalar). It can use any signed integer, unsigned integer, float32, or float64 type. The output has the **same type and tuple shape as the input**. All working iterations use float64 values. Integer values are converted only once, after the last iteration; they are not truncated after each update. + +For integer output, each final value must be finite. The filter truncates the value toward zero, then clips it to the output type's range if needed. For example, 93.75 becomes 93. For uint8 output, -0.5 becomes 0 without clipping, while -1.5 clips to 0. If any value is clipped, the filter reports one warning for the execution. The warning states that the evolution may be unstable. A nonfinite final integer value causes an execution error instead of an unsafe conversion. + +Floating output retains NaN and infinity behavior. A finite result beyond the float32 range becomes signed infinity explicitly. float64 output retains the float64 result. Input and working precision still limit accuracy: ordinary evolution can lose integer detail above 2^53, even for int64 and uint64 output. Changing the output type does not restore that detail. + +Time Step must be finite and nonnegative. Spacing on each active axis must be finite and nonzero; finite negative spacing remains accepted. X and Y are active in a 2D image. Z is active only when the Z dimension is greater than one. Unused Z spacing is ignored in 2D. The filter validates these controls even for a no-op request. A zero Time Step or zero Number Of Iterations then copies input values exactly, including native 64-bit integers, without float64 conversion. + +Pipelines that previously produced an out-of-range integer cast now receive clipped values and a warning, or an error for a nonfinite integer result. Clipping can create plateaus, change masks, and merge segmented regions. Check downstream thresholds and segmentation when the warning occurs. Clipping does not correct an unstable evolution. The timestep should be small enough to ensure numerical stability. Stability is guaranteed when the timestep meets the CFL (Courant-Friedrichs-Levy) condition; broadly speaking, this condition ensures that each contour does not move more than one grid position per timestep. The timestep typically must be manually tuned to the application. @@ -26,9 +34,11 @@ For resident arrays, the solver keeps its intermediate and update images residen For a 3D image with an out-of-core input or output, the solver first asks the shared cache budget for the complete input, output, intermediate, update, and conversion-plane state. A complete grant reads the input once, runs the same resident Jacobi calculation, and writes the output once. A partial grant or allocation failure uses the bounded fallback: a three-plane rolling neighborhood plus two raw temporary-record working stores, with initial/final conversions and updates transferred one Z plane at a time. For true 2D, the existing checked row-block and X-tile plan remains bounded to 64 MiB. +The final conversion uses the existing resident, plane, or tile buffers. It does not add a full-image resident temporary to the bounded routes. Exact no-op copies use a transfer buffer of at most 1 MiB. Output storage size does not increase. + ### Time Step -The timestep between iteration updates. Default is 0.05. +The timestep between iteration updates. It must be finite and nonnegative. Default is 0.05. Zero copies the input exactly after control validation. The filter does not choose an automatic stability cutoff or adapt this value. ### Number Of Iterations diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp index 056997b53c..198afb367d 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp @@ -2,14 +2,20 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" #include "simplnx/Parameters/GeometrySelectionParameter.hpp" #include "simplnx/Parameters/NumberParameter.hpp" +#include "simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include + using namespace nx::core; namespace nx::core @@ -76,6 +82,16 @@ IFilter::PreflightResult CurvatureFlowImageFilter::preflightImpl(const DataStruc // the legacy ScalarPixelIdTypeList), matching the legacy ITK filter, which has no minimum-dimension requirement. Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter( dataStructure, selectedInputArray, imageGeomPath, outputArrayPath, /*requireScalar=*/true); + if(resultOutputActions.invalid()) + { + return {.outputActions = std::move(resultOutputActions)}; + } + const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); + const float64 timeStep = filterArgs.value(k_TimeStep_Key); + if(Result<> result = ImageProcessing::detail::ValidateCurvatureFlowControls(timeStep, imageGeom.getSpacing(), imageGeom.getDimensions()); result.invalid()) + { + return {.outputActions = MergeResults(std::move(resultOutputActions), ConvertResultTo(std::move(result), OutputActions{}))}; + } return {std::move(resultOutputActions)}; } @@ -90,7 +106,7 @@ Result<> CurvatureFlowImageFilter::executeImpl(DataStructure& dataStructure, con auto timeStep = filterArgs.value(k_TimeStep_Key); auto numberOfIterations = filterArgs.value(k_NumberOfIterations_Key); - return ImageProcessing::ExecuteFiniteDifferenceImageFilter( + return ImageProcessing::ExecuteFiniteDifferenceImageFilter( dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::detail::CurvatureFlowFn{}, timeStep, numberOfIterations, shouldCancel, messageHandler); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp index 8d2077e69f..a5edfb1fca 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp @@ -218,7 +218,7 @@ struct AxisProjectionExecuteFn * ConductanceParameter, ...) are supplied by the caller; this functor only resolves the T-typed input/output * stores and forwards to the streaming @ref ApplyFiniteDifference driver. */ -template +template struct FiniteDifferenceExecuteFn { DataStructure& dataStructure; @@ -237,7 +237,7 @@ struct FiniteDifferenceExecuteFn { const auto& inArray = dataStructure.getDataRefAs>(inputArrayPath); auto& outArray = dataStructure.getDataRefAs>(outputArrayPath); - return ApplyFiniteDifference(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); + return ApplyFiniteDifference(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); } }; @@ -382,8 +382,9 @@ Result<> ExecuteAxisProjectionImageFilter(DataStructure& dataStructure, const Da * (CurvatureFlowFn etc.), which may itself hold extra parameters set by the caller. @p TypeSetPolicy * selects which scalar types the filter accepts (AllNumeric for CurvatureFlow; FloatingScalar for the * other three). The caller's preflight must have enforced a scalar input. + * CheckedOutput is an explicit Curvature Flow opt-in; other finite-difference callers keep the default. */ -template +template Result<> ExecuteFiniteDifferenceImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { @@ -392,7 +393,7 @@ Result<> ExecuteFiniteDifferenceImageFilter(DataStructure& dataStructure, const const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); const SizeVec3 dims = imageGeom.getDimensions(); const FloatVec3 spacing = imageGeom.getSpacing(); - detail::FiniteDifferenceExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler}; + const detail::FiniteDifferenceExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index d32461205b..ff8657c9fb 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -3,16 +3,49 @@ #include "NXImageProcessing/Filters/CurvatureFlowImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Array.hpp" +#include "simplnx/Common/Extent.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp" +#include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" +#include "simplnx/Utilities/Parsing/HDF5/IO/DatasetIO.hpp" #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include #include using namespace nx::core; @@ -59,6 +92,766 @@ std::vector MakeRamp(usize dx, usize dy, usize dz) } } // namespace +namespace +{ +// A real resident forwarding endpoint that is deliberately not a DataStore. It selects the streamed final-cast +// fallback without pretending to be disk-backed. Optional transfer faults exercise the checked no-op I/O contract. +template +class CurvatureFlowForwardingStore : public AbstractDataStore +{ +public: + explicit CurvatureFlowForwardingStore(const ShapeType& shape, bool failRead = false, bool failWrite = false, bool reportOutOfCore = false, std::atomic_bool* cancelOnReadPtr = nullptr) + : m_Store(shape, ShapeType{1}, T{}) + , m_FailRead(failRead) + , m_FailWrite(failWrite) + , m_ReportOutOfCore(reportOutOfCore) + , m_CancelOnReadPtr(cancelOnReadPtr) + { + } + ~CurvatureFlowForwardingStore() override = default; + CurvatureFlowForwardingStore(const CurvatureFlowForwardingStore&) = delete; + CurvatureFlowForwardingStore(CurvatureFlowForwardingStore&&) = delete; + CurvatureFlowForwardingStore& operator=(const CurvatureFlowForwardingStore&) = delete; + CurvatureFlowForwardingStore& operator=(CurvatureFlowForwardingStore&&) = delete; + + usize getNumberOfTuples() const override + { + return m_Store.getNumberOfTuples(); + } + const ShapeType& getTupleShape() const override + { + return m_Store.getTupleShape(); + } + usize getNumberOfComponents() const override + { + return 1; + } + const ShapeType& getComponentShape() const override + { + return m_Store.getComponentShape(); + } + Result<> resizeTuples(const ShapeType& shape) override + { + return m_Store.resizeTuples(shape); + } + DataType getDataType() const override + { + return m_Store.getDataType(); + } + IDataStore::StoreType getStoreType() const override + { + return m_ReportOutOfCore ? IDataStore::StoreType::OutOfCore : IDataStore::StoreType::InMemory; + } + std::map getRecoveryMetadata() const override + { + return {}; + } + usize getTypeSize() const override + { + return sizeof(T); + } + std::unique_ptr deepCopy(const std::string& format) const override + { + return m_Store.deepCopy(format); + } + std::unique_ptr createNewInstance() const override + { + return m_Store.createNewInstance(); + } + std::pair writeBinaryFile(const std::string& path) const override + { + return m_Store.writeBinaryFile(path); + } + std::pair writeBinaryFile(std::ostream& stream) const override + { + return m_Store.writeBinaryFile(stream); + } + T getValue(usize index) const override + { + return m_Store.getValue(index); + } + void setValue(usize index, T value) override + { + m_Store.setValue(index, value); + } + Result<> copyIntoBuffer(usize offset, nonstd::span values) const override + { + ++m_ReadCount; + m_MaxReadValues = std::max(m_MaxReadValues, values.size()); + if(m_CancelOnReadPtr != nullptr) + { + m_CancelOnReadPtr->store(true); + } + Result<> result; + if(m_FailRead) + { + result = MakeErrorResult(-28890, "Injected Curvature Flow input transfer failure."); + } + else if(m_ReadCount == m_FailReadAt) + { + result = MakeErrorResult(-28892, "Injected delayed Curvature Flow input transfer failure."); + } + else + { + result = m_Store.copyIntoBuffer(offset, values); + } + if(m_WarnOnRead) + { + result.warnings().push_back({.code = -28893, .message = "Injected Curvature Flow read warning."}); + } + return result; + } + Result<> copyFromBuffer(usize offset, nonstd::span values) override + { + ++m_WriteCount; + m_MaxWriteValues = std::max(m_MaxWriteValues, values.size()); + Result<> result = m_FailWrite ? MakeErrorResult(-28891, "Injected Curvature Flow output transfer failure.") : m_Store.copyFromBuffer(offset, values); + if(m_WarnOnWrite) + { + result.warnings().push_back({.code = -28894, .message = "Injected Curvature Flow write warning."}); + } + if(m_CancelOnWritePtr != nullptr && m_WriteCount == m_CancelWriteAt) + { + m_CancelOnWritePtr->store(true); + } + return result; + } + Result> readExtent(const Extent& extent) const override + { + return m_Store.readExtent(extent); + } + Result<> readExtentIntoBuffer(const Extent& extent, nonstd::span destination) const override + { + return m_Store.readExtentIntoBuffer(extent, destination); + } + Result<> writeExtent(const Extent& extent, nonstd::span values) override + { + return m_Store.writeExtent(extent, values); + } + T at(usize index) const override + { + return m_Store.at(index); + } + void add(usize index, T value) override + { + m_Store.add(index, value); + } + void sub(usize index, T value) override + { + m_Store.sub(index, value); + } + void mul(usize index, T value) override + { + m_Store.mul(index, value); + } + void div(usize index, T value) override + { + m_Store.div(index, value); + } + void rem(usize index, T value) override + { + m_Store.rem(index, value); + } + void bitwiseAND(usize index, T value) override + { + m_Store.bitwiseAND(index, value); + } + void bitwiseOR(usize index, T value) override + { + m_Store.bitwiseOR(index, value); + } + void bitwiseXOR(usize index, T value) override + { + m_Store.bitwiseXOR(index, value); + } + void bitwiseLShift(usize index, T value) override + { + m_Store.bitwiseLShift(index, value); + } + void bitwiseRShift(usize index, T value) override + { + m_Store.bitwiseRShift(index, value); + } + void byteSwap(usize index) override + { + m_Store.byteSwap(index); + } + void swap(usize left, usize right) override + { + m_Store.swap(left, right); + } + Result<> readHdf5(const HDF5::DatasetIO& dataset) override + { + return m_Store.readHdf5(dataset); + } + Result<> writeHdf5(HDF5::DatasetIO& dataset) const override + { + return m_Store.writeHdf5(dataset); + } + usize readCount() const + { + return m_ReadCount; + } + usize writeCount() const + { + return m_WriteCount; + } + usize maxReadValues() const + { + return m_MaxReadValues; + } + usize maxWriteValues() const + { + return m_MaxWriteValues; + } + void failReadAt(usize call) + { + m_FailReadAt = call; + } + void warnOnReads() + { + m_WarnOnRead = true; + } + void warnOnWrites() + { + m_WarnOnWrite = true; + } + void cancelOnWrite(usize call, std::atomic_bool& shouldCancel) + { + m_CancelWriteAt = call; + m_CancelOnWritePtr = &shouldCancel; + } + +private: + DataStore m_Store; + bool m_FailRead = false; + bool m_FailWrite = false; + bool m_ReportOutOfCore = false; + std::atomic_bool* m_CancelOnReadPtr = nullptr; + mutable usize m_ReadCount = 0; + usize m_WriteCount = 0; + mutable usize m_MaxReadValues = 0; + usize m_MaxWriteValues = 0; + usize m_FailReadAt = std::numeric_limits::max(); + bool m_WarnOnRead = false; + bool m_WarnOnWrite = false; + usize m_CancelWriteAt = std::numeric_limits::max(); + std::atomic_bool* m_CancelOnWritePtr = nullptr; +}; + +Arguments makeCurvatureFlowArgs(const DataPath& inputPath, float64 timeStep, uint32 iterations) +{ + Arguments args; + args.insertOrAssign(CurvatureFlowImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + args.insertOrAssign(CurvatureFlowImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(CurvatureFlowImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(timeStep)); + args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(iterations)); + return args; +} + +template +void requireCurvatureFlowConstant(DataStructure& dataStructure, const DataPath& inputPath, T expected, float64 timeStep, uint32 iterations) +{ + const CurvatureFlowImageFilter filter; + const Arguments args = makeCurvatureFlowArgs(inputPath, timeStep, iterations); + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 1}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + REQUIRE(executeResult.result.warnings().empty()); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* valuesPtr = nullptr; + REQUIRE_NOTHROW(valuesPtr = &dataStructure.getDataRefAs>(outputPath)); + REQUIRE(valuesPtr->getDataStoreRef().getValue(0) == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +} // namespace + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid time step is rejected before execution", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const float64 timeStep = GENERATE(-0.05, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()); + const uint32 iterations = GENERATE(0u, 1u); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {7}); + const CurvatureFlowImageFilter filter; + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, timeStep, iterations)); + // Preflight only: never evolve or cast an invalid-control baseline. + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == ImageProcessing::k_CurvatureFlowInvalidTimeStep); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid active spacing is rejected for no-op requests", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool is3D = GENERATE(false, true); + const uint32 iterations = GENERATE(0u, 1u); + const float32 invalidSpacing = GENERATE(0.0F, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()); + const usize activeAxes = is3D ? 3 : 2; + for(usize axis = 0; axis < activeAxes; ++axis) + { + DYNAMIC_SECTION("active axis " << axis) + { + DataStructure dataStructure; + const usize dimZ = is3D ? 2 : 1; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, dimZ, std::vector(dimZ, 7)); + ImageGeom* geometryPtr = nullptr; + REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(inputPath.getParent().getParent())); + std::array spacing = {1.0F, 1.0F, 1.0F}; + spacing.at(axis) = invalidSpacing; + geometryPtr->setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); + const CurvatureFlowImageFilter filter; + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, 0.0, iterations)); + // All executions are deferred: nonfinite spacing can poison even a constant baseline. + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == ImageProcessing::k_CurvatureFlowInvalidSpacing); + UnitTest::CheckArraysInheritTupleDims(dataStructure); + } + } +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: finite negative spacing and unused Z remain accepted", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const float32 unusedSpacing = GENERATE(0.0F, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {7}); + ImageGeom* geometryPtr = nullptr; + REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(inputPath.getParent().getParent())); + geometryPtr->setSpacing({-1.0F, -2.0F, unusedSpacing}); + const CurvatureFlowImageFilter filter; + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, 0.0, 0)); + SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: constant finite input preserves type and value", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64, float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {TestType{7}}); + // A constant has zero first derivatives, so the independent expected update is exactly zero. + requireCurvatureFlowConstant(dataStructure, inputPath, TestType{7}, 0.05, 2); +} + +TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: zero operation copies int64 values exactly", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool zeroIterations = GENERATE(false, true); + constexpr TestType inputValue = (TestType{1} << 53) + TestType{1}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); + // The baseline rounds this finite constant to 2^53. Both values are well inside either native integer range. + // Its constant-image derivative guard returns zero, so zero timestep cannot create a nonfinite update. + requireCurvatureFlowConstant(dataStructure, inputPath, inputValue, zeroIterations ? 0.05 : 0.0, zeroIterations ? 0u : 1u); +} + +TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: terminal integer conversion has exact bounds", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + using ImageProcessing::detail::ConvertTerminalImageValue; + using ImageProcessing::detail::TerminalImageConversionStatus; + TestType output = 7; + REQUIRE(ConvertTerminalImageValue(93.75, output) == TerminalImageConversionStatus::Valid); + REQUIRE(output == TestType{93}); + REQUIRE(ConvertTerminalImageValue(-0.5, output) == TerminalImageConversionStatus::Valid); + REQUIRE(output == TestType{0}); + const double upper = std::ldexp(1.0, std::numeric_limits::digits); + REQUIRE(ConvertTerminalImageValue(upper, output) == TerminalImageConversionStatus::Clipped); + REQUIRE(output == std::numeric_limits::max()); + if constexpr(std::numeric_limits::digits <= 32) + { + REQUIRE(ConvertTerminalImageValue(static_cast(std::numeric_limits::max()) + 0.5, output) == TerminalImageConversionStatus::Valid); + REQUIRE(output == std::numeric_limits::max()); + } + if constexpr(std::is_signed_v) + { + REQUIRE(ConvertTerminalImageValue(-upper, output) == TerminalImageConversionStatus::Valid); + REQUIRE(output == std::numeric_limits::lowest()); + REQUIRE(ConvertTerminalImageValue(std::nextafter(-upper - 1.0, -std::numeric_limits::infinity()), output) == TerminalImageConversionStatus::Clipped); + REQUIRE(output == std::numeric_limits::lowest()); + REQUIRE(ConvertTerminalImageValue(-1.5, output) == TerminalImageConversionStatus::Valid); + REQUIRE(output == TestType{-1}); + } + else + { + REQUIRE(ConvertTerminalImageValue(-1.5, output) == TerminalImageConversionStatus::Clipped); + REQUIRE(output == TestType{0}); + } + for(const double value : {std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}) + { + output = 7; + REQUIRE(ConvertTerminalImageValue(value, output) == TerminalImageConversionStatus::NonFinite); + REQUIRE(output == TestType{7}); + } +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating narrowing retains IEEE values", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + using ImageProcessing::detail::NarrowFloatingImageValue; + const auto positive = NarrowFloatingImageValue(std::numeric_limits::max()); + const auto negative = NarrowFloatingImageValue(-std::numeric_limits::max()); + REQUIRE(std::isinf(positive)); + REQUIRE_FALSE(std::signbit(positive)); + REQUIRE(std::isinf(negative)); + REQUIRE(std::signbit(negative)); + REQUIRE(std::isnan(NarrowFloatingImageValue(std::numeric_limits::quiet_NaN()))); + REQUIRE(std::signbit(NarrowFloatingImageValue(-0.0))); + REQUIRE(NarrowFloatingImageValue(std::numeric_limits::max()) == static_cast(std::numeric_limits::max())); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stencil truncates then clips once", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool clip = GENERATE(false, true); + const bool negativeSpacing = GENERATE(false, true); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {0, 0, 0, 0, 100, 100, 0, 100, 100}); + if(negativeSpacing) + { + ImageGeom* geometryPtr = nullptr; + REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(inputPath.getParent().getParent())); + geometryPtr->setSpacing({-1.0F, -1.0F, std::numeric_limits::quiet_NaN()}); + } + const CurvatureFlowImageFilter filter; + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, clip ? 10.0 : 0.05, 1)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == (clip ? 1 : 0)); + if(clip) + { + REQUIRE(result.result.warnings().front().code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput); + } + const IDataArray* outputArrayPtr = nullptr; + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::uint8); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 3, 3}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const UInt8Array* outputPtr = nullptr; + REQUIRE_NOTHROW(outputPtr = &dataStructure.getDataRefAs(outputPath)); + // At the center: gradient=(50,50), second derivatives=(-100,-100), mixed derivative=25. + // The level-set curvature update is -125: 100 + 0.05*(-125) = 93.75; dt=10 yields -1150. + REQUIRE(outputPtr->getDataStoreRef().getValue(4) == (clip ? uint8{0} : uint8{93})); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op preserves integer endpoints", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const TestType value = GENERATE(std::numeric_limits::lowest(), std::numeric_limits::max()); + const bool zeroIterations = GENERATE(false, true); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {value}); + requireCurvatureFlowConstant(dataStructure, inputPath, value, zeroIterations ? 0.05 : 0.0, zeroIterations ? 0u : 1u); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer result errors", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {0, 0, 0, 0, 100, 100, 0, 100, 100}); + const CurvatureFlowImageFilter filter; + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, std::numeric_limits::max(), 1)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == ImageProcessing::k_CurvatureFlowNonFiniteIntegerOutput); + REQUIRE(result.result.errors().front().message.find("index 4") != std::string::npos); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong terminal routes honor the stencil", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool pingPong = GENERATE(false, true); + const bool nonFinite = GENERATE(false, true); + const ShapeType shape{2, 3, 3}; + const SizeVec3 dims{3, 3, 2}; + const std::vector field{0, 0, 0, 0, 100, 100, 0, 100, 100, 0, 0, 0, 0, 100, 100, 0, 100, 100}; + DataStore input(shape, ShapeType{1}, uint8{0}); + const auto copyResult = input.copyFromBuffer(0, field); + SIMPLNX_RESULT_REQUIRE_VALID(copyResult); + CurvatureFlowForwardingStore output(shape); + DataStore first(shape, ShapeType{1}, 0.0); + DataStore second(shape, ShapeType{1}, 0.0); + const std::atomic_bool shouldCancel{false}; + const float64 step = nonFinite ? std::numeric_limits::max() : 10.0; + Result<> result; + if(pingPong) + { + result = ImageProcessing::ApplyFiniteDifference3DPingPong(input, output, first, second, dims, {1.0, 1.0, 1.0}, + ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, {}); + } + else + { + result = ImageProcessing::ApplyFiniteDifference(input, output, dims, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, + shouldCancel, {}); + } + if(nonFinite) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == ImageProcessing::k_CurvatureFlowNonFiniteIntegerOutput); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput); + REQUIRE(output.getValue(4) == 0); + REQUIRE(output.getValue(13) == 0); + REQUIRE(output.writeCount() == 2); + } + REQUIRE(output.getTupleShape() == shape); + REQUIRE(output.getComponentShape() == ShapeType{1}); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op propagates transfer failures and cancellation", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const int scenario = GENERATE(0, 1, 2, 3); + const ShapeType shape{1, 1, 1}; + std::atomic_bool shouldCancel{scenario == 2}; + CurvatureFlowForwardingStore input(shape, scenario == 0, false, false, scenario == 3 ? &shouldCancel : nullptr); + CurvatureFlowForwardingStore output(shape, false, scenario == 1); + input.setValue(0, std::numeric_limits::max()); + output.setValue(0, 7); + const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {1, 1, 1}, {1.0F, 1.0F, 1.0F}, + ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); + if(scenario >= 2) + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(input.readCount() == (scenario == 2 ? 0 : 1)); + REQUIRE(output.writeCount() == 0); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == (scenario == 0 ? -28890 : -28891)); + } + REQUIRE(output.getValue(0) == 7); + REQUIRE(output.getTupleShape() == shape); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: resident wrapper merges clipping warning with copy failure", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const ImageProcessing::ScopedWorkingMemoryTuningOverride tuning(1024 * 1024); + const ShapeType shape{2, 3, 3}; + DataStore input(shape, ShapeType{1}, uint8{0}); + const std::vector field{0, 0, 0, 0, 100, 100, 0, 100, 100, 0, 0, 0, 0, 100, 100, 0, 100, 100}; + const auto copyResult = input.copyFromBuffer(0, field); + SIMPLNX_RESULT_REQUIRE_VALID(copyResult); + // This endpoint selects the wrapper and injects its final-copy fault. Real backing is checked in the paired tests. + CurvatureFlowForwardingStore output(shape, false, true, true); + const std::atomic_bool shouldCancel{false}; + const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {3, 3, 2}, {1.0F, 1.0F, 1.0F}, + ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28891); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput); + REQUIRE(output.writeCount() == 1); +} + +TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output retains nonfinite and overflow behavior", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool nanInput = GENERATE(false, true); + DataStructure dataStructure; + const std::vector field = nanInput ? std::vector(9, std::numeric_limits::quiet_NaN()) : std::vector{0, 0, 0, 0, 100, 100, 0, 100, 100}; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, field); + const CurvatureFlowImageFilter filter; + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, nanInput ? 0.05 : 1.0e40, 1)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 3, 3}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const DataArray* outputPtr = nullptr; + REQUIRE_NOTHROW(outputPtr = &dataStructure.getDataRefAs>(outputPath)); + const TestType center = outputPtr->getDataStoreRef().getValue(4); + if(nanInput) + { + REQUIRE(std::isnan(center)); + } + else if constexpr(std::is_same_v) + { + REQUIRE(std::isinf(center)); + REQUIRE(std::signbit(center)); + } + else + { + REQUIRE(center == Approx(-1.25e42)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded native transfers", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize valuesPerMiB = (usize{1024} * 1024) / sizeof(uint64); + constexpr usize count = (2 * valuesPerMiB) + 1; + const ShapeType shape{1, 1, count}; + CurvatureFlowForwardingStore input(shape); + CurvatureFlowForwardingStore output(shape); + input.setValue(0, std::numeric_limits::max()); + input.setValue(count - 1, (uint64{1} << 53) + 1); + const std::atomic_bool shouldCancel{false}; + const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {count, 1, 1}, {1.0F, 1.0F, 1.0F}, + ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().empty()); + REQUIRE(input.readCount() == 3); + REQUIRE(output.writeCount() == 3); + REQUIRE(input.maxReadValues() == valuesPerMiB); + REQUIRE(output.maxWriteValues() == valuesPerMiB); + REQUIRE(output.getValue(0) == std::numeric_limits::max()); + REQUIRE(output.getValue(count - 1) == (uint64{1} << 53) + 1); + REQUIRE(output.getTupleShape() == shape); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits retain clipping and transfer warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const int route = GENERATE(0, 1, 2); // ping-pong delayed read, ping-pong cancellation, resident fallback cancellation + const ShapeType shape{3, 3, 3}; + CurvatureFlowForwardingStore input(shape); + CurvatureFlowForwardingStore output(shape); + const std::array plane{0, 0, 0, 0, 100, 100, 0, 100, 100}; + for(usize index = 0; index < 27; ++index) + { + input.setValue(index, plane.at(index % plane.size())); + } + input.warnOnReads(); + output.warnOnWrites(); + std::atomic_bool shouldCancel{false}; + if(route == 0) + { + input.failReadAt(3); // planes 0 and 1 are loaded before the first clipped output; loading plane 2 then fails + } + else + { + output.cancelOnWrite(1, shouldCancel); + } + Result<> result; + if(route == 2) + { + result = ImageProcessing::ApplyFiniteDifference(input, output, {3, 3, 3}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, + 10.0, 1, shouldCancel, {}); + } + else + { + DataStore first(shape, ShapeType{1}, 0.0); + DataStore second(shape, ShapeType{1}, 0.0); + result = ImageProcessing::ApplyFiniteDifference3DPingPong(input, output, first, second, {3, 3, 3}, {1.0, 1.0, 1.0}, + ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); + } + if(route == 0) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28892); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel.load()); + } + REQUIRE(output.writeCount() == 1); + REQUIRE(output.getValue(4) == 0); + const auto countWarnings = [&](int32 code) { + return static_cast(std::count_if(result.warnings().begin(), result.warnings().end(), [code](const Warning& warning) { return warning.code == code; })); + }; + REQUIRE(countWarnings(ImageProcessing::k_CurvatureFlowClippedIntegerOutput) == 1); + REQUIRE(countWarnings(-28893) == input.readCount()); + REQUIRE(countWarnings(-28894) == output.writeCount()); + REQUIRE(result.warnings().size() == input.readCount() + output.writeCount() + 1); + REQUIRE(output.getTupleShape() == shape); +} + +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D terminal exits retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool cancel = GENERATE(false, true); + const ShapeType shape{1, 3, 3}; + DataStore input(shape, ShapeType{1}, uint8{0}); + const std::vector field{0, 0, 0, 0, 100, 100, 0, 100, 100}; + const auto copyResult = input.copyFromBuffer(0, field); + SIMPLNX_RESULT_REQUIRE_VALID(copyResult); + CurvatureFlowForwardingStore output(shape); + CurvatureFlowForwardingStore first(shape); + CurvatureFlowForwardingStore second(shape); + second.warnOnReads(); + output.warnOnWrites(); + std::atomic_bool shouldCancel{false}; + if(cancel) + { + output.cancelOnWrite(2, shouldCancel); // row 1 clips; cancellation follows its output write + } + else + { + second.failReadAt(3); // final row read follows the clipped center row + } + constexpr usize limit = ImageProcessing::detail::k_FiniteDifference2DFixedStateBytes + (12 * sizeof(float64)); + const auto plan = ImageProcessing::detail::BuildFiniteDifference2DBufferPlan(3, 3, 1, sizeof(float64), limit); + REQUIRE(plan.coreCols == 3); + REQUIRE(plan.coreRows == 1); + const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( + input, output, first, second, 3, 3, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); + if(cancel) + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel.load()); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28892); + } + REQUIRE(output.writeCount() == 2); + REQUIRE(output.getValue(4) == 0); + const auto countWarnings = [&](int32 code) { + return static_cast(std::count_if(result.warnings().begin(), result.warnings().end(), [code](const Warning& warning) { return warning.code == code; })); + }; + REQUIRE(countWarnings(ImageProcessing::k_CurvatureFlowClippedIntegerOutput) == 1); + REQUIRE(countWarnings(-28893) == second.readCount()); + REQUIRE(countWarnings(-28894) == output.writeCount()); + REQUIRE(result.warnings().size() == second.readCount() + output.writeCount() + 1); + REQUIRE(output.getTupleShape() == shape); +} + // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a valid scalar input produces a // SameAsInput output array (unlike GradientMagnitude's fixed float32, CurvatureFlow preserves the input type). diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 8afc77a976..31a5d5d3a0 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -8,6 +8,8 @@ #include "simplnx/DataStructure/IO/Generic/ITemporaryRecordStore.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" // CreateDataStoreWithFormat (update buffer) +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp" #include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" #include "simplnx/Utilities/ParallelDataAlgorithm.hpp" // CalculateChange in-plane parallelization #include "simplnx/Utilities/ThrottledMessageHandler.hpp" @@ -37,6 +39,177 @@ namespace nx::core::ImageProcessing { namespace detail { +/** @brief Checks the selected Curvature Flow controls, including requests that only copy the input. */ +inline Result<> ValidateCurvatureFlowControls(float64 timeStep, const FloatVec3& spacing, const SizeVec3& dims) +{ + if(!std::isfinite(timeStep) || timeStep < 0.0) + { + return MakeErrorResult(k_CurvatureFlowInvalidTimeStep, fmt::format("Curvature Flow Time Step must be finite and nonnegative; received {}. Set Time Step to zero to copy the input.", timeStep)); + } + const usize activeAxes = dims[2] > 1 ? 3 : 2; + for(usize axis = 0; axis < activeAxes; ++axis) + { + const float64 value = spacing[axis]; + if(!std::isfinite(value) || value == 0.0) + { + return MakeErrorResult(k_CurvatureFlowInvalidSpacing, + fmt::format("Curvature Flow image spacing for active axis {} must be finite and nonzero; received {}. Correct the Image Geometry spacing before execution.", axis, value)); + } + } + return {}; +} + +class CurvatureFlowConversionState +{ +public: + CurvatureFlowConversionState() = default; + ~CurvatureFlowConversionState() = default; + CurvatureFlowConversionState(const CurvatureFlowConversionState&) = delete; + CurvatureFlowConversionState(CurvatureFlowConversionState&&) = delete; + CurvatureFlowConversionState& operator=(const CurvatureFlowConversionState&) = delete; + CurvatureFlowConversionState& operator=(CurvatureFlowConversionState&&) = delete; + + void recordClipping() noexcept + { + m_Clipped.store(true, std::memory_order_relaxed); + } + bool clipped() const noexcept + { + return m_Clipped.load(std::memory_order_relaxed); + } + void recordNonFinite(usize index) noexcept + { + usize previous = m_NonFiniteIndex.load(std::memory_order_relaxed); + while(index < previous && !m_NonFiniteIndex.compare_exchange_weak(previous, index, std::memory_order_relaxed)) + { + } + } + usize nonFiniteIndex() const noexcept + { + return m_NonFiniteIndex.load(std::memory_order_relaxed); + } + +private: + std::atomic m_Clipped{false}; + std::atomic m_NonFiniteIndex{std::numeric_limits::max()}; +}; + +template +bool ConvertFiniteDifferenceOutputBuffer(nonstd::span input, nonstd::span output, usize offset, CurvatureFlowConversionState& state, const std::atomic_bool& shouldCancel, + usize valuesPerRow = 0, usize rowStride = 0) +{ + bool clipped = false; + for(usize index = 0; index < input.size(); ++index) + { + if((index & 4095ULL) == 0 && shouldCancel) + { + if(clipped) + { + state.recordClipping(); + } + return false; + } + if constexpr(CheckedOutput) + { + const auto status = ConvertTerminalImageValue(input[index], output[index]); + if(status == TerminalImageConversionStatus::NonFinite) + { + const usize failedIndex = valuesPerRow == 0 ? offset + index : offset + ((index / valuesPerRow) * rowStride) + (index % valuesPerRow); + state.recordNonFinite(failedIndex); + if(clipped) + { + state.recordClipping(); + } + return false; + } + clipped = clipped || status == TerminalImageConversionStatus::Clipped; + } + else + { + output[index] = static_cast(input[index]); + } + } + if(clipped) + { + state.recordClipping(); + } + return true; +} + +template +Result<> FinishFiniteDifferenceConversion(const CurvatureFlowConversionState& state) +{ + Result<> result; + if constexpr(CheckedOutput) + { + const usize failedIndex = state.nonFiniteIndex(); + if(failedIndex != std::numeric_limits::max()) + { + result = MakeErrorResult( + k_CurvatureFlowNonFiniteIntegerOutput, + fmt::format("Curvature Flow produced a nonfinite integer output at flat value index {}. Reduce Time Step or use a floating-point input array to retain nonfinite output.", failedIndex)); + } + if(state.clipped()) + { + result.warnings().push_back({.code = k_CurvatureFlowClippedIntegerOutput, + .message = "Curvature Flow clipped final integer values to the output type's range after truncation. The evolution may be unstable. " + "Reduce Time Step and check downstream masks and segmentation for clipping plateaus."}); + } + } + return result; +} + +template +Result<> CopyCurvatureFlowInput(const AbstractDataStore& input, AbstractDataStore& output, const std::atomic_bool& shouldCancel) +{ + constexpr usize bufferBytes = usize{1024} * 1024; + const usize capacity = std::min(input.getSize(), bufferBytes / sizeof(T)); + std::vector buffer(capacity); + Result<> result; + for(usize offset = 0; offset < input.getSize();) + { + if(shouldCancel) + { + return result; + } + const usize count = std::min(capacity, input.getSize() - offset); + result = MergeResults(std::move(result), input.copyIntoBuffer(offset, nonstd::span(buffer.data(), count))); + if(result.invalid()) + { + return result; + } + if(shouldCancel) + { + return result; + } + result = MergeResults(std::move(result), output.copyFromBuffer(offset, nonstd::span(buffer.data(), count))); + if(result.invalid()) + { + return result; + } + offset += count; + } + return result; +} + +// Selected routes retain successful serial transfer warnings until their single outer finalization point. +template +Result<> RecordFiniteDifferenceIoResult(Result<>& accumulated, Result<> transfer) +{ + if constexpr(CheckedOutput) + { + if(transfer.valid()) + { + for(auto& warning : transfer.warnings()) + { + accumulated.warnings().push_back(std::move(warning)); + } + return {}; + } + } + return transfer; +} + inline std::string SelectFiniteDifferenceWorkingDataFormat(IDataStore::StoreType inputStoreType, std::string inputDataFormat, IDataStore::StoreType outputStoreType, std::string outputDataFormat) { if(inputStoreType == IDataStore::StoreType::OutOfCore) @@ -1328,11 +1501,20 @@ Result<> UpdateGlobalConductance2DBounded(F& fn, const StoreT& currentStore, usi return {}; } -template +template Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, AbstractDataStore& outStore, WorkingStoreT& firstStore, WorkingStoreT& secondStore, usize nx, usize ny, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit) { + detail::CurvatureFlowConversionState conversionState; + Result<> ioResult; + const auto finish = [&](Result<> result = {}) { + if constexpr(CheckedOutput) + { + return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + } + return result; + }; ThrottledMessageHandler progressThrottle(messageHandler); messageHandler.sendInfoMessage("Applying Finite Difference Iterations"); @@ -1379,21 +1561,25 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs { if(shouldCancel) { - return {}; + return finish(); } const usize xEnd = xBegin + std::min(plan.coreCols, nx - xBegin); const usize valueCount = (yEnd - yBegin) * (xEnd - xBegin); - if(Result<> result = ReadFiniteDifference2DRegion(inStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(inputBlock.data(), inputBlock.size())); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, ReadFiniteDifference2DRegion(inStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); + result.invalid()) { - return result; + return finish(std::move(result)); } for(usize index = 0; index < valueCount; ++index) { castBlock[index] = static_cast(inputBlock[index]); } - if(Result<> result = WriteFiniteDifference2DRegion(firstStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(castBlock.data(), valueCount)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, WriteFiniteDifference2DRegion(firstStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(castBlock.data(), valueCount))); + result.invalid()) { - return result; + return finish(std::move(result)); } } } @@ -1408,13 +1594,15 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs { if(shouldCancel) { - return {}; + return finish(); } if constexpr(F::k_NeedsGlobalGradient) { - if(Result<> result = UpdateGlobalConductance2DBounded(fn, *currentStore, nx, ny, sc, plan, nonstd::span(inputBlock.data(), inputBlock.size()), iter, shouldCancel); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, UpdateGlobalConductance2DBounded(fn, *currentStore, nx, ny, sc, plan, nonstd::span(inputBlock.data(), inputBlock.size()), iter, shouldCancel)); + result.invalid()) { - return result; + return finish(std::move(result)); } } @@ -1429,7 +1617,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs { if(shouldCancel) { - return {}; + return finish(); } const usize xEnd = xBegin + std::min(plan.coreCols, nx - xBegin); const usize loadXBegin = xBegin > radius ? xBegin - radius : 0; @@ -1437,9 +1625,11 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs const usize loadedWidth = loadXEnd - loadXBegin; const usize coreWidth = xEnd - xBegin; const usize coreRows = yEnd - yBegin; - if(Result<> result = ReadFiniteDifference2DRegion(*currentStore, nx, loadXBegin, loadXEnd, loadYBegin, loadYEnd, nonstd::span(inputBlock.data(), inputBlock.size())); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, loadXBegin, loadXEnd, loadYBegin, loadYEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); + result.invalid()) { - return result; + return finish(std::move(result)); } auto calculateRows = [&](const Range& rowRange) { @@ -1471,9 +1661,11 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs calculateRows(Range{0, coreRows}); } const usize valueCount = coreRows * coreWidth; - if(Result<> result = WriteFiniteDifference2DRegion(*nextStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputBlock.data(), valueCount)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, WriteFiniteDifference2DRegion(*nextStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputBlock.data(), valueCount))); + result.invalid()) { - return result; + return finish(std::move(result)); } completedValues += valueCount; progressThrottle.updatePercent(completedValues); @@ -1497,33 +1689,47 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs { if(shouldCancel) { - return {}; + return finish(); } const usize xEnd = xBegin + std::min(plan.coreCols, nx - xBegin); const usize valueCount = (yEnd - yBegin) * (xEnd - xBegin); - if(Result<> result = ReadFiniteDifference2DRegion(*currentStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(workingBlock.data(), workingBlock.size())); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(workingBlock.data(), workingBlock.size()))); + result.invalid()) { - return result; + return finish(std::move(result)); } - for(usize index = 0; index < valueCount; ++index) + if(!detail::ConvertFiniteDifferenceOutputBuffer(nonstd::span(workingBlock.data(), valueCount), nonstd::span(outputValues.data(), valueCount), + (yBegin * nx) + xBegin, conversionState, shouldCancel, xEnd - xBegin, nx)) { - outputValues[index] = static_cast(workingBlock[index]); + return finish(); } - if(Result<> result = WriteFiniteDifference2DRegion(outStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputValues.data(), valueCount)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult( + ioResult, WriteFiniteDifference2DRegion(outStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputValues.data(), valueCount))); + result.invalid()) { - return result; + return finish(std::move(result)); } } } } - return {}; + return finish(); } -template +template Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, AbstractDataStore& outStore, WorkingStoreT& firstStore, WorkingStoreT& secondStore, const SizeVec3& dims, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { + detail::CurvatureFlowConversionState conversionState; + Result<> ioResult; + const auto finish = [&](Result<> result = {}) { + if constexpr(CheckedOutput) + { + return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + } + return result; + }; ThrottledMessageHandler progressThrottle(messageHandler); messageHandler.sendInfoMessage("Applying Finite Difference Iterations"); @@ -1541,27 +1747,31 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab { if(shouldCancel) { - return {}; + return finish(); } - if(Result<> result = inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice))); + result.invalid()) { - return result; + return finish(std::move(result)); } - if(Result<> result = outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice))); + result.invalid()) { - return result; + return finish(std::move(result)); } } - return {}; + return finish(); } if constexpr(F::k_NeedsGlobalGradient) { if(!fn.gradientMagnitudeFixed) { - if(Result<> result = detail::InitializeFiniteDifferenceWorkingStoreAndConductance(fn, inStore, firstStore, dims, sc, shouldCancel); result.invalid()) + if(Result<> result = + detail::RecordFiniteDifferenceIoResult(ioResult, detail::InitializeFiniteDifferenceWorkingStoreAndConductance(fn, inStore, firstStore, dims, sc, shouldCancel)); + result.invalid()) { - return result; + return finish(std::move(result)); } } else @@ -1572,19 +1782,22 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab { if(shouldCancel) { - return {}; + return finish(); } - if(Result<> result = inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputPlane.data(), slice))); + result.invalid()) { - return result; + return finish(std::move(result)); } for(usize index = 0; index < slice; ++index) { workingPlane[index] = static_cast(inputPlane[index]); } - if(Result<> result = firstStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(workingPlane.data(), slice)); result.invalid()) + if(Result<> result = + detail::RecordFiniteDifferenceIoResult(ioResult, firstStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(workingPlane.data(), slice))); + result.invalid()) { - return result; + return finish(std::move(result)); } } } @@ -1611,7 +1824,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab return {}; } } - if(Result<> result = loadPlane(window[slot], z); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(window[slot], z)); result.invalid()) { return result; } @@ -1620,7 +1833,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab }; for(int64 slot = 0; slot < static_cast(windowPlanes); ++slot) { - if(Result<> result = loadWindowPlane(static_cast(slot), slot - radius); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, loadWindowPlane(static_cast(slot), slot - radius)); result.invalid()) { return result; } @@ -1689,16 +1902,21 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab algorithm.execute(calculateRows); if(finalIteration) { - for(usize index = 0; index < slice; ++index) + if(!detail::ConvertFiniteDifferenceOutputBuffer(nonstd::span(nextPlane.data(), slice), nonstd::span(finalOutputPlane.data(), slice), + static_cast(z) * slice, conversionState, shouldCancel)) { - finalOutputPlane[index] = static_cast(nextPlane[index]); + return {}; } - if(Result<> result = outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(finalOutputPlane.data(), slice)); result.invalid()) + if(Result<> result = + detail::RecordFiniteDifferenceIoResult(ioResult, outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(finalOutputPlane.data(), slice))); + result.invalid()) { return result; } } - else if(Result<> result = destinationStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice)); result.invalid()) + else if(Result<> result = + detail::RecordFiniteDifferenceIoResult(ioResult, destinationStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice))); + result.invalid()) { return result; } @@ -1710,7 +1928,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab std::swap(window[slot], window[slot + 1]); std::swap(windowZ[slot], windowZ[slot + 1]); } - if(Result<> result = loadWindowPlane(windowPlanes - 1, z + 1 + radius); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, loadWindowPlane(windowPlanes - 1, z + 1 + radius)); result.invalid()) { return result; } @@ -1723,16 +1941,17 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab { if(shouldCancel) { - return {}; + return finish(); } if constexpr(F::k_NeedsGlobalGradient) { const bool initializedConductance = iter == 0 && !fn.gradientMagnitudeFixed; if(!initializedConductance) { - if(Result<> result = detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, 3, iter, shouldCancel); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, 3, iter, shouldCancel)); + result.invalid()) { - return result; + return finish(std::move(result)); } } } @@ -1748,7 +1967,8 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab const int64 z = clampi(wantedZ, nZ - 1); if(!loadedInputZ.has_value() || *loadedInputZ != z) { - if(Result<> result = inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputCastPlane.data(), slice)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(inputCastPlane.data(), slice))); + result.invalid()) { return result; } @@ -1760,9 +1980,9 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab } return {}; }; - if(Result<> result = processIteration(loadInputPlane, &firstStore, finalIteration); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, processIteration(loadInputPlane, &firstStore, finalIteration)); result.invalid()) { - return result; + return finish(std::move(result)); } if(finalIteration) { @@ -1770,7 +1990,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab { throttleProgress(progressThrottle, iter, numberOfIterations); } - return {}; + return finish(); } currentStore = &firstStore; nextStore = &secondStore; @@ -1786,9 +2006,9 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab const int64 z = clampi(wantedZ, nZ - 1); return currentStore->copyIntoBuffer(static_cast(z) * slice, nonstd::span(destination.data(), slice)); }; - if(Result<> result = processIteration(loadWorkingPlane, nextStore, finalIteration); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, processIteration(loadWorkingPlane, nextStore, finalIteration)); result.invalid()) { - return result; + return finish(std::move(result)); } if(finalIteration) { @@ -1796,7 +2016,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab { throttleProgress(progressThrottle, iter, numberOfIterations); } - return {}; + return finish(); } std::swap(currentStore, nextStore); if(!shouldCancel) @@ -1804,10 +2024,10 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab throttleProgress(progressThrottle, iter, numberOfIterations); } } - return {}; + return finish(); } -template +template Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit2D) { @@ -1873,9 +2093,10 @@ Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, Abstra TemporaryFiniteDifferenceStore secondStore(*secondRecords, shouldCancel); if(nz == 1) { - return ApplyFiniteDifference2DBounded(inStore, outStore, firstStore, secondStore, nx, ny, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); + return ApplyFiniteDifference2DBounded(inStore, outStore, firstStore, secondStore, nx, ny, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, + residentLimit2D); } - return ApplyFiniteDifference3DPingPong(inStore, outStore, firstStore, secondStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); + return ApplyFiniteDifference3DPingPong(inStore, outStore, firstStore, secondStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); } // Full ITK-free FiniteDifference driver. Resident DataStore execution retains the original full-volume @@ -1900,14 +2121,32 @@ Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, Abstra // CurvatureAnisoFn sets k_NativePrecision=true so Real==T reproduces THAT model instead (verified: with Real // hardcoded to float64, the float32-input live-ITK grid diverged from ITK by ~1 ULP at essentially every voxel -- // a deterministic precision-MODEL mismatch, not thread-order FP noise -- and resolved once Real became T here). -template +// CheckedOutput opts into the selected Curvature Flow controls, exact no-ops, and terminal conversion only. +// Other finite-difference filters retain the default behavior and their own precision models. +template Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, FloatVec3 spacing, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit2D = detail::k_FiniteDifference2DResidentLimit) { using Real = std::conditional_t; + detail::CurvatureFlowConversionState conversionState; + Result<> ioResult; + const auto finish = [&](Result<> result = {}) { + if constexpr(CheckedOutput) + { + return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + } + return result; + }; + if constexpr(CheckedOutput) + { + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, detail::ValidateCurvatureFlowControls(timeStep, spacing, dims)); result.invalid()) + { + return finish(std::move(result)); + } + } if(dims[0] == 0 || dims[1] == 0 || dims[2] == 0) { - return {}; + return finish(); } if(dims[0] > static_cast(std::numeric_limits::max()) || dims[1] > static_cast(std::numeric_limits::max()) || dims[2] > static_cast(std::numeric_limits::max())) @@ -1938,9 +2177,16 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData return MakeErrorResult(-8633, fmt::format("Finite-difference output store size does not match the image dimensions. Actual size: {}; expected size: {}; dimensions: {} x {} x {}.", outStore.getSize(), volume, dims[0], dims[1], dims[2])); } + if constexpr(CheckedOutput) + { + if(timeStep == 0.0 || numberOfIterations == 0) + { + return detail::CopyCurvatureFlowInput(inStore, outStore, shouldCancel); + } + } if(shouldCancel) { - return {}; + return finish(); } const bool hasOutOfCoreEndpoint = inStore.getStoreType() == IDataStore::StoreType::OutOfCore || outStore.getStoreType() == IDataStore::StoreType::OutOfCore; @@ -1958,20 +2204,23 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { DataStore residentInput(ShapeType{dims[2], dims[1], dims[0]}, ShapeType{1}, std::nullopt); DataStore residentOutput(ShapeType{dims[2], dims[1], dims[0]}, ShapeType{1}, std::nullopt); - if(Result<> result = inStore.copyIntoBuffer(0, residentInput.createSpan()); result.invalid()) + Result<> inputResult = inStore.copyIntoBuffer(0, residentInput.createSpan()); + if(inputResult.invalid()) { - return result; + return finish(std::move(inputResult)); } - if(Result<> result = ApplyFiniteDifference(residentInput, residentOutput, dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); result.invalid()) + Result<> evolutionResult = MergeResults(std::move(inputResult), ApplyFiniteDifference(residentInput, residentOutput, dims, spacing, fn, timeStep, numberOfIterations, + shouldCancel, messageHandler, residentLimit2D)); + if(evolutionResult.invalid()) { - return result; + return finish(std::move(evolutionResult)); } if(shouldCancel) { - return {}; + return finish(std::move(evolutionResult)); } const auto outputSpan = residentOutput.createSpan(); - return outStore.copyFromBuffer(0, nonstd::span(outputSpan.data(), outputSpan.size())); + return MergeResults(std::move(evolutionResult), outStore.copyFromBuffer(0, nonstd::span(outputSpan.data(), outputSpan.size()))); } catch(const std::bad_alloc&) { // Release the complete-state reservation before entering the raw-record fallback. @@ -1987,16 +2236,16 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { return MakeErrorResult(-8634, fmt::format("Finite-difference neighborhood radius must be nonnegative and addressable. Radius: {}.", r)); } - // sc[i] = 1/spacing[i] (ITK InitializeFunctionCoefficients, UseImageSpacing on). A zero/near-zero spacing yields inf - // here -- this MATCHES ITK's own unguarded 1.0/spacing[i], so (per the "ITK-faithful guard only" decision) no spacing - // guard is added; the degenerate input is the caller's responsibility, as in ITK. + // sc[i] = 1/spacing[i] (ITK InitializeFunctionCoefficients, UseImageSpacing on). Unchecked callers retain the + // legacy unguarded reciprocal. Checked Curvature Flow validates + // finite nonzero active-axis spacing before reaching this point; inactive Z spacing remains unused in 2D. const std::array sc = {1.0 / static_cast(spacing[0]), 1.0 / static_cast(spacing[1]), 1.0 / static_cast(spacing[2])}; auto clampi = [](int64 v, int64 hi) { return v < 0 ? int64{0} : (v > hi ? hi : v); }; const std::vector shape{static_cast(nZ), static_cast(nY), static_cast(nX)}; if(hasOutOfCoreEndpoint) { - return ApplyFiniteDifferenceOocRaw(inStore, outStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); + return ApplyFiniteDifferenceOocRaw(inStore, outStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); } // Resident/fallback stores: accum (Real) = cast(in), streamed plane by plane -- ITK's IntermediateType @@ -2019,19 +2268,20 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { if(shouldCancel) { - return {}; + return finish(); } - if(Result<> rr = inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); rr.invalid()) { - return rr; + return finish(std::move(rr)); } for(usize i = 0; i < slice; ++i) { planeOut[i] = static_cast(planeIn[i]); } - if(Result<> rr = accum.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, accum.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); + rr.invalid()) { - return rr; + return finish(std::move(rr)); } } } @@ -2054,13 +2304,14 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { if(shouldCancel) { - return {}; + return finish(); } if constexpr(F::k_NeedsGlobalGradient) { - if(Result<> result = detail::UpdateGlobalConductance(fn, accum, dims, sc, effDim, iter, shouldCancel); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(ioResult, detail::UpdateGlobalConductance(fn, accum, dims, sc, effDim, iter, shouldCancel)); + result.invalid()) { - return result; + return finish(std::move(result)); } } @@ -2116,7 +2367,7 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData calculateAlgorithm.execute(calculateChange); if(shouldCancel) { - return {}; + return finish(); } progressThrottle.reset(volume, fmt::format("Finite Difference: Applying Changes for Iteration {} of {}", iter + 1, numberOfIterations)); @@ -2147,7 +2398,7 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData applyAlgorithm.execute(applyUpdate); if(shouldCancel) { - return {}; + return finish(); } if(!shouldCancel) { @@ -2156,19 +2407,13 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData } auto castOutput = [&](const Range& range) { - for(usize tuple = range.min(); tuple < range.max(); ++tuple) - { - if(((tuple - range.min()) & 4095ULL) == 0 && shouldCancel) - { - return; - } - outputValues[tuple] = static_cast(accumValues[tuple]); - } + detail::ConvertFiniteDifferenceOutputBuffer(nonstd::span(accumValues.data() + range.min(), range.max() - range.min()), + nonstd::span(outputValues.data() + range.min(), range.max() - range.min()), range.min(), conversionState, shouldCancel); }; ParallelDataAlgorithm castAlgorithm; castAlgorithm.setRange(0, volume); castAlgorithm.execute(castOutput); - return {}; + return finish(); } // Non-DataStore resident fallback: two Real stores are ping-ponged. CalculateChange and ApplyUpdate are fused per @@ -2189,15 +2434,15 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { if(shouldCancel) { - return {}; + return finish(); } // (aniso) global reduction hook -- no-op unless F::k_NeedsGlobalGradient (Task 4 specializes this). if constexpr(F::k_NeedsGlobalGradient) { - if(Result<> rr = detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, effDim, iter, shouldCancel); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, effDim, iter, shouldCancel)); rr.invalid()) { - return rr; + return finish(std::move(rr)); } } @@ -2205,9 +2450,9 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData // seed the window at z = -r .. +r for(int64 k = 0; k < static_cast(windowPlanes); ++k) { - if(Result<> rr = loadPlane(win[static_cast(k)], (k - r)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[static_cast(k)], (k - r))); rr.invalid()) { - return rr; + return finish(std::move(rr)); } } progressThrottle.reset(static_cast(nZ), fmt::format("Finite Difference: Updating Slices for Iteration {} of {}", iter + 1, numberOfIterations)); @@ -2215,7 +2460,7 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { if(shouldCancel) { - return {}; + return finish(); } // In-plane voxel loop, parallelized over the row range [0, nY). Each `nextPlane[y*nX+x]` depends ONLY on the // frozen (read-only) rolling window `win`, the shared const functor `fn`, and `sc` -- there is no shared mutable @@ -2263,9 +2508,10 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, static_cast(nY)); parallelAlgorithm.execute(calculateChangeRows); - if(Result<> rr = nextStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, nextStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice))); + rr.invalid()) { - return rr; + return finish(std::move(rr)); } progressThrottle.updateCount(static_cast(z + 1)); // advance the rolling window by one z (drop the oldest plane, load clamp(z+1+r)) @@ -2275,9 +2521,9 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { std::swap(win[k], win[k + 1]); } - if(Result<> rr = loadPlane(win[windowPlanes - 1], z + 1 + r); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[windowPlanes - 1], z + 1 + r)); rr.invalid()) { - return rr; + return finish(std::move(rr)); } } } @@ -2296,22 +2542,25 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { if(shouldCancel) { - return {}; + return finish(); } - if(Result<> rr = currentStore->copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, currentStore->copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); + rr.invalid()) { - return rr; + return finish(std::move(rr)); } - for(usize i = 0; i < slice; ++i) + if(!detail::ConvertFiniteDifferenceOutputBuffer(nonstd::span(planeIn.data(), slice), nonstd::span(planeOut.data(), slice), static_cast(z) * slice, + conversionState, shouldCancel)) { - planeOut[i] = static_cast(planeIn[i]); + return finish(); } - if(Result<> rr = outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice)); rr.invalid()) + if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); + rr.invalid()) { - return rr; + return finish(std::move(rr)); } } } - return {}; + return finish(); } } // namespace nx::core::ImageProcessing diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp index da5aabbd52..5efb14f831 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp @@ -30,6 +30,11 @@ inline constexpr int32 k_GradientMagnitudeZeroSpacing = -8573; // exceeds the CFL stability bound minSpacing/2^(ImageDimension+1). Shared by the anisotropic-diffusion pair // (GradientAnisotropicDiffusion now; CurvatureAnisotropicDiffusion later) via AppendUnstableTimeStepWarning. inline constexpr int32 k_AnisotropicDiffusionUnstableTimeStep = -8580; +// Scoped Curvature Flow validation and final-conversion diagnostics. +inline constexpr int32 k_CurvatureFlowInvalidTimeStep = -28800; +inline constexpr int32 k_CurvatureFlowInvalidSpacing = -28801; +inline constexpr int32 k_CurvatureFlowNonFiniteIntegerOutput = -28802; +inline constexpr int32 k_CurvatureFlowClippedIntegerOutput = -28803; // RelabelComponent's consecutive output-label counter overflows the (possibly narrow, e.g. int8) SameAsInput output // type when there are more surviving objects than the type can represent. Mirrors itk::RelabelComponentImageFilter:: // GenerateData's NumericTraits::max() guard (which itkExceptionMacro-throws in the same situation). diff --git a/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp new file mode 100644 index 0000000000..cdbea9ef0b --- /dev/null +++ b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include +#include + +namespace nx::core::ImageProcessing::detail +{ +/** @brief Reports the final conversion without allocating diagnostics inside a pixel loop. */ +enum class TerminalImageConversionStatus : std::uint8_t +{ + Valid, + Clipped, + NonFinite +}; + +/** + * @brief Retains floating behavior and explicitly maps finite narrowing overflow to signed infinity. + * The input must already be float32 or float64. Preserve existing direct integer-to-float32 conversions at callers; + * passing those integers through float64 first can introduce double rounding. + */ +template +OutputType NarrowFloatingImageValue(InputType value) +{ + static_assert(std::is_floating_point_v && std::is_floating_point_v); + static_assert(std::is_same_v || std::is_same_v); + static_assert(std::is_same_v || std::is_same_v); + if constexpr(std::is_same_v) + { + return value; + } + if(std::isfinite(value) && std::abs(static_cast(value)) > static_cast(std::numeric_limits::max())) + { + return std::signbit(value) ? -std::numeric_limits::infinity() : std::numeric_limits::infinity(); + } + return static_cast(value); +} + +/** + * @brief Converts one final image value. Finite integers truncate before clipping; nonfinite integers fail. + * The binary exclusive upper bound is exact at 64-bit widths, unlike a floating conversion of the native maximum. + * This function does not change intermediate image precision or aggregate warnings. + */ +template +TerminalImageConversionStatus ConvertTerminalImageValue(InputType value, OutputType& output) +{ + static_assert(std::is_floating_point_v); + static_assert(std::is_same_v || std::is_same_v); + if constexpr(std::is_integral_v) + { + if(!std::isfinite(value)) + { + return TerminalImageConversionStatus::NonFinite; + } + const double truncated = std::trunc(static_cast(value)); + const double upperExclusive = std::ldexp(1.0, std::numeric_limits::digits); + const double lowerInclusive = std::is_signed_v ? -upperExclusive : 0.0; + if(truncated < lowerInclusive) + { + output = std::numeric_limits::lowest(); + return TerminalImageConversionStatus::Clipped; + } + if(truncated >= upperExclusive) + { + output = std::numeric_limits::max(); + return TerminalImageConversionStatus::Clipped; + } + output = static_cast(truncated); + return TerminalImageConversionStatus::Valid; + } + else + { + output = NarrowFloatingImageValue(value); + return TerminalImageConversionStatus::Valid; + } +} +} // namespace nx::core::ImageProcessing::detail From 0539aac9edf2ec5ed606767481d0bc03e87c13f5 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 26 Oct 2026 23:41:03 -0400 Subject: [PATCH 16/98] BUG: Check recursive Gaussian smoothing conversions Validate active controls, effective scale and formed coefficients. Preserve signed output types, Float32 inter-pass values and existing recurrence order while checking final integer conversion and making floating narrowing explicit. Retain one clipping warning through selected sinks and later failures. Cover native endpoints, precision, transfers and bounded routes, and explain the limits in the filter help. Signed-off-by: Jessica Marquis --- .../SmoothingRecursiveGaussianImageFilter.md | 32 +- .../SmoothingRecursiveGaussianImageFilter.cpp | 16 +- .../SmoothingRecursiveGaussianImageFilter.hpp | 4 +- .../utils/GaussianImageFilterExecutors.cpp | 91 +++- ...othingRecursiveGaussianImageFilterTest.cpp | 490 +++++++++++++++++- .../ImageProcessingConstants.hpp | 7 + .../RecursiveGaussianEngine.hpp | 481 ++++++++++++----- 7 files changed, 952 insertions(+), 169 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md index 19a7455a6a..417796d55f 100644 --- a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md @@ -12,13 +12,15 @@ Convolves the input image with a Gaussian kernel, computed with the Deriche 4th- **Sigma** is specified per-axis (X, Y, Z), in the units of image spacing, so anisotropic smoothing and anisotropic spacing are both supported. Larger sigma produces stronger blurring along that axis. A `Z` size of 1 is treated as a 2D image and only the X and Y axes are filtered. -The input array must be single-component (scalar) and must be a **signed** type: `int8`, `int16`, `int32`, `int64`, `float32`, or `float64`. Unsigned types are not allowed because the recursive filter has a small negative overshoot that would underflow an unsigned round-trip. The output has the **same type as the input** (type-preserving). This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Smoothing Recursive Gaussian Image Filter, and matches it exactly for integer and float64 inputs. +The input array must have one component (scalar) and use `int8`, `int16`, `int32`, `int64`, `float32`, or `float64`. Unsigned types are not supported. The output has the **same type, tuple shape, and component shape as the input**. This is an ITK-free implementation of the legacy filter's recursive approximation. Exact agreement for all data and scales is not guaranteed. The filter requires at least 4 pixels along every filtered axis. ### Sigma -The standard deviation of the Gaussian along each axis (X, Y, Z), in the units of image spacing. Default is 1 along every axis. +The standard deviation along each axis (X, Y, Z), in the units of image spacing. Default is 1 along every axis. For each active axis, Sigma must be finite and positive. Image spacing must be finite with an absolute value greater than `1e-8`. Finite negative spacing is accepted. The effective scale is Sigma divided by the absolute spacing; this quotient must be finite and positive, and the formed recurrence coefficients must be finite. + +These checks reject invalid coefficient formation. They do not impose a maximum Sigma or guarantee accuracy for extreme scales. When Z has size 1, Z Sigma and Z spacing are unused and are not checked. ### Normalize Across Scale @@ -28,7 +30,31 @@ Whether to normalize the Gaussian over scale-space (Lindeberg's scale normalizat The filter applies one separable zero-order recursive-Gaussian pass along each active image axis. Resident **Data Arrays** retain the direct parallel cascade. For a three-dimensional disk-backed image, the Z pass first batches consecutive Y rows across the volume; the following X and Y passes are fused within each Z plane and cast directly to the output. A two-dimensional image fuses its Y and X passes directly from input to output and needs no full-image working array. Store reads and writes remain serial and contiguous; only staged in-memory lines are filtered in parallel. -Each recurrence explicitly materializes its result as `float32` before the next axis, and the terminal plane is cast back to the input type. Three-dimensional working memory is bounded by a fixed number of image planes plus a Z slab whose combined buffers target at most 64 MiB; one XZ row is the indivisible minimum. The single full-volume intermediate adopts an out-of-core endpoint's format. Two-dimensional disk-backed execution uses only plane buffers and therefore creates no full-volume scratch array. +Each recurrence uses double coefficients and arithmetic, then stores its result as **Float32 before the next axis**. This rounding is part of the filter's contract. Float64 output does not restore detail lost in those Float32 values. In particular, this filter does not preserve arbitrary Int64 intensity detail. + +Resident execution retains its Float32 working image and direct parallel path. Three-dimensional disk execution uses a file-backed Float32 working image, a fixed number of plane buffers, and a Z slab whose buffers target at most 64 MiB; one XZ row is the minimum transfer unit. Two-dimensional disk execution uses bounded row or column blocks and file-backed recurrence checkpoints. Split-column plans use a file-backed Float32 working image. Scratch files consume disk space without requiring a resident array for the whole image. Store transfers remain serial. + +### Final values and diagnostics + +For integer output, finite final values **truncate toward zero, then clip to the native output range only if needed**. For example, an Int8 result of `127.75` becomes `127` without clipping, while `128` clips to `127`. One warning reports any clipping during a filter run. A constant Int32 maximum can round to `2147483648` between passes; its final output clips back to `2147483647` and reports the warning. + +A nonfinite final value (`NaN` or either infinity) cannot be stored as an integer. The filter returns an error with the flat value index and output type. Floating output retains floating nonfinite behavior. Finite overflow when narrowing a double recurrence result to Float32 is explicitly mapped to infinity with the same sign; later arithmetic can produce NaN. No integer clipping occurs between axes. + +The same terminal policy applies to all five output routes: + +| Output route | Final conversion | +| --- | --- | +| Direct resident output | Checked parallel conversion of Float32 values | +| Buffered store output | Checked conversion in bounded transfer buffers | +| Two-dimensional plane cascade | Checked conversion before a plane is written | +| Three-dimensional plane cascade | Checked conversion after the Z, X, and Y passes | +| Bounded two-dimensional disk cascade | Checked conversion before each output block is written | + +Warnings from completed transfers remain available if a later operation fails or the run is cancelled. Cancellation and later I/O failures can leave completed output blocks in place; this filter does not promise an all-or-nothing output update. + +### Migration and downstream use + +Earlier versions could use an unchecked integer conversion, including an invalid result at the Int32 maximum. Review clipping warnings when updating an existing pipeline. Clipping and recursive overshoot can change the result of later thresholding or segmentation. The array size stays unchanged. Use floating input when you need to retain fractional or nonfinite output, while allowing for the unchanged Float32 inter-pass precision. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp index 0236398ab6..29db2e1b85 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp @@ -12,8 +12,11 @@ #include "simplnx/Parameters/VectorParameter.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp" #include "simplnx/Utilities/SIMPLConversion.hpp" +#include + using namespace nx::core; namespace nx::core @@ -78,7 +81,7 @@ IFilter::PreflightResult SmoothingRecursiveGaussianImageFilter::preflightImpl(co const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); // requireScalar=true: the recursive Gaussian is defined on scalar images. The output is SameAsInput (type-preserving), - // so an input type T produces a float32-free, T-typed output array. The SignedScalar policy admits only signed integer + // so an input type T produces a T-typed output array with float32 inter-pass precision. The SignedScalar policy admits only signed integer // + floating point types (the legacy SignedScalarPixelIdTypeList); unsigned types are rejected. Result resultOutputActions = ImageProcessing::PreflightFullOverwriteImageFilter( dataStructure, selectedInputArray, imageGeomPath, outputArrayPath, /*requireScalar=*/true); @@ -94,15 +97,10 @@ IFilter::PreflightResult SmoothingRecursiveGaussianImageFilter::preflightImpl(co } auto sigma = filterArgs.value(k_Sigma_Key); - if(Result<> sigmaCheck = ImageProcessing::ValidatePositiveSigma(sigma, imageGeom.getDimensions(), ImageProcessing::k_RecursiveGaussianNonPositiveSigma); sigmaCheck.invalid()) - { - return {ConvertResultTo(std::move(sigmaCheck), OutputActions{})}; - } - - if(Result<> spacingCheck = ImageProcessing::ValidatePositiveSpacing(imageGeom.getSpacing(), imageGeom.getDimensions(), ImageProcessing::k_RecursiveGaussianNonPositiveSpacing, 1e-8); - spacingCheck.invalid()) + if(Result<> controls = ImageProcessing::detail::ValidateSmoothingGaussianControls(sigma, imageGeom.getSpacing(), imageGeom.getDimensions()); controls.invalid()) { - return {ConvertResultTo(std::move(spacingCheck), OutputActions{})}; + controls.errors().front().message += fmt::format(" Image Geometry '{}'.", imageGeomPath.toString()); + return {.outputActions = ConvertResultTo(std::move(controls), OutputActions{})}; } return {std::move(resultOutputActions)}; } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp index 7e12df2c7d..86a7f8fcb8 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp @@ -13,7 +13,9 @@ namespace nx::core * * Convolves the input with a Gaussian (per-axis sigma) using the Deriche 4th-order recursive IIR approximation. * The input may be any SIGNED scalar type (int8/16/32/64 or float32/64); the output has the SAME type as the input. - * Matches the legacy ITK Smoothing Recursive Gaussian Image Filter exactly for integer/float64 inputs. + * Each axis materializes float32 values, including for float64 output. Finite final integer values truncate and clip + * to the output range with one warning. Nonfinite final integer values fail. Active Sigma, spacing, effective scale, + * and formed coefficients are validated; inactive Z controls remain unused for a two-dimensional image. */ class NXIMAGEPROCESSING_EXPORT SmoothingRecursiveGaussianImageFilter : public IFilter { diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp index a5309638eb..9dc57e10c2 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp @@ -221,21 +221,27 @@ template Result<> ApplySmoothingRecursiveGaussian3D(const AbstractDataStore& input, AbstractDataStore& output, WorkStoreT& work, const SizeVec3& dims, const FloatVec3& spacing, const std::vector& sigma, bool normalizeAcrossScale, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { - if(Result<> result = RecursiveGaussianAxisPass(input, work, dims, 2, sigma[2], static_cast(spacing[2]), 0, normalizeAcrossScale, shouldCancel, messageHandler); result.invalid()) + detail::SmoothingGaussianConversionState conversionState; + RecursiveGaussianPassOptions options; + options.CheckedSmoothing = true; + Result<> accumulated = RecursiveGaussianAxisPass(input, work, dims, 2, sigma[2], static_cast(spacing[2]), 0, normalizeAcrossScale, shouldCancel, messageHandler, options); + if(accumulated.invalid()) { - return result; + return accumulated; } std::vector outputPlane(dims[0] * dims[1]); auto outputSink = [&](usize planeOffset, nonstd::span filteredPlane) -> Result<> { - for(usize index = 0; index < filteredPlane.size(); ++index) + if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filteredPlane.data(), filteredPlane.size()), nonstd::span(outputPlane.data(), filteredPlane.size()), planeOffset, + conversionState, shouldCancel)) { - outputPlane[index] = static_cast(filteredPlane[index]); + return detail::SmoothingGaussianNonFiniteResult(conversionState); } return output.copyFromBuffer(planeOffset, nonstd::span(outputPlane.data(), filteredPlane.size())); }; - const RecursiveGaussianPlaneAxis first{0, sigma[0], static_cast(spacing[0]), 0, normalizeAcrossScale}; - const RecursiveGaussianPlaneAxis second{1, sigma[1], static_cast(spacing[1]), 0, normalizeAcrossScale}; - return RecursiveGaussianXYPlaneCascadeToSink(work, dims, first, second, shouldCancel, messageHandler, outputSink); + const RecursiveGaussianPlaneAxis first{.axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + const RecursiveGaussianPlaneAxis second{.axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + accumulated = MergeResults(std::move(accumulated), RecursiveGaussianXYPlaneCascadeToSink(work, dims, first, second, shouldCancel, messageHandler, outputSink)); + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } template @@ -338,22 +344,27 @@ Result<> ApplySmoothingRecursiveGaussian2D(const AbstractDataStore& input, Ab const FloatVec3& spacing, const std::vector& sigma, bool normalizeAcrossScale, const detail::RecursiveGaussian2DBufferPlan& plan, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { + detail::SmoothingGaussianConversionState conversionState; std::vector outputBuffer; auto terminalSink = [&](usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span filtered, nonstd::span) -> Result<> { if constexpr(std::is_same_v) { - return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size())); + return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size()), true); } outputBuffer.resize(filtered.size()); - for(usize index = 0; index < filtered.size(); ++index) + if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filtered.data(), filtered.size()), nonstd::span(outputBuffer.data(), outputBuffer.size()), (yBegin * dims[0]) + xBegin, + conversionState, shouldCancel, columnCount, dims[0])) { - outputBuffer[index] = static_cast(filtered[index]); + return detail::SmoothingGaussianNonFiniteResult(conversionState); } - return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(outputBuffer.data(), outputBuffer.size())); + return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(outputBuffer.data(), outputBuffer.size()), true); }; - const RecursiveGaussianPlaneAxis smoothY{1, sigma[1], static_cast(spacing[1]), 0, normalizeAcrossScale}; - const RecursiveGaussianPlaneAxis smoothX{0, sigma[0], static_cast(spacing[0]), 0, normalizeAcrossScale}; - return RecursiveGaussian2DYXCascadeToSink(input, work, checkpoints, dims, smoothY, smoothX, plan, shouldCancel, messageHandler, terminalSink); + const RecursiveGaussianPlaneAxis smoothY{ + .axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + const RecursiveGaussianPlaneAxis smoothX{ + .axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + Result<> result = RecursiveGaussian2DYXCascadeToSink(input, work, checkpoints, dims, smoothY, smoothX, plan, shouldCancel, messageHandler, terminalSink); + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(result)); } /// @brief Dispatched execute body for the GradientMagnitudeRecursiveGaussian filter. @@ -891,17 +902,22 @@ struct SmoothingRecursiveGaussianExecuteFn if(usePlaneCascade) { std::vector outputPlane(dims[0] * dims[1]); + detail::SmoothingGaussianConversionState conversionState; auto outputSink = [&](usize planeOffset, nonstd::span filteredPlane) -> Result<> { - for(usize i = 0; i < filteredPlane.size(); ++i) + if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filteredPlane.data(), filteredPlane.size()), nonstd::span(outputPlane.data(), filteredPlane.size()), planeOffset, + conversionState, shouldCancel)) { - outputPlane[i] = static_cast(filteredPlane[i]); + return detail::SmoothingGaussianNonFiniteResult(conversionState); } return outStore.copyFromBuffer(planeOffset, nonstd::span(outputPlane.data(), filteredPlane.size())); }; - const RecursiveGaussianPlaneAxis first{1, sigma[1], static_cast(spacing[1]), 0, normalizeAcrossScale}; - const RecursiveGaussianPlaneAxis second{0, sigma[0], static_cast(spacing[0]), 0, normalizeAcrossScale}; - return RecursiveGaussianXYPlaneCascadeToSink(inStore, dims, first, second, shouldCancel, messageHandler, outputSink); + const RecursiveGaussianPlaneAxis first{ + .axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + const RecursiveGaussianPlaneAxis second{ + .axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + Result<> result = RecursiveGaussianXYPlaneCascadeToSink(inStore, dims, first, second, shouldCancel, messageHandler, outputSink); + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(result)); } const std::string format = detail::SelectRecursiveGaussianWorkingDataFormat(inStore.getStoreType(), inStore.getDataFormat(), outStore.getStoreType(), outStore.getDataFormat()); @@ -918,27 +934,32 @@ struct SmoothingRecursiveGaussianExecuteFn } bool first = true; + Result<> accumulated; + RecursiveGaussianPassOptions options; + options.CheckedSmoothing = true; for(uint32 axis : axisOrder) { const double s = sigma[axis]; const double sp = static_cast(spacing[axis]); if(first) { - if(Result<> r = RecursiveGaussianAxisPass(inStore, work, dims, axis, s, sp, /*order=*/0, normalizeAcrossScale, shouldCancel, messageHandler); r.invalid()) + accumulated = MergeResults(std::move(accumulated), RecursiveGaussianAxisPass(inStore, work, dims, axis, s, sp, /*order=*/0, normalizeAcrossScale, shouldCancel, messageHandler, options)); + if(accumulated.invalid()) { - return r; + return accumulated; } first = false; } else { - if(Result<> r = RecursiveGaussianAxisPass(work, work, dims, axis, s, sp, /*order=*/0, normalizeAcrossScale, shouldCancel, messageHandler); r.invalid()) + accumulated = MergeResults(std::move(accumulated), RecursiveGaussianAxisPass(work, work, dims, axis, s, sp, /*order=*/0, normalizeAcrossScale, shouldCancel, messageHandler, options)); + if(accumulated.invalid()) { - return r; + return accumulated; } } } - return CastFloat32StoreTo(work, outStore, shouldCancel); + return MergeResults(std::move(accumulated), CastFloat32StoreTo(work, outStore, shouldCancel)); } }; @@ -1027,8 +1048,28 @@ Result<> ExecuteSmoothingRecursiveGaussianImageFilter(DataStructure& dataStructu const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); const SizeVec3 dims = imageGeom.getDimensions(); const FloatVec3 spacing = imageGeom.getSpacing(); + if(Result<> controls = detail::ValidateSmoothingGaussianControls(sigma, spacing, dims); controls.invalid()) + { + controls.errors().front().message += fmt::format(" Image Geometry '{}'.", imageGeomPath.toString()); + return controls; + } detail::SmoothingRecursiveGaussianExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, spacing, sigma, normalizeAcrossScale, shouldCancel, messageHandler}; - return TypeSetPolicy::dispatch(inputType, executeFn); + Result<> result = TypeSetPolicy::dispatch(inputType, executeFn); + if(result.invalid()) + { + for(auto& error : result.errors()) + { + error.message += fmt::format(" Output array '{}'.", outputArrayPath.toString()); + } + } + for(auto& warning : result.warnings()) + { + if(warning.code == k_SmoothingGaussianClippedIntegerOutput) + { + warning.message += fmt::format(" Output array '{}'.", outputArrayPath.toString()); + } + } + return result; } // ----------------------------------------------------------------------------- diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index b668c8e2ce..57243babdd 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -3,16 +3,37 @@ #include "NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Parameters/VectorParameter.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp" #include +#include +#include +#include +#include +#include #include +#include #include #include #include @@ -24,6 +45,92 @@ namespace rt = recon_test; namespace { using SmoothFilter = SmoothingRecursiveGaussianImageFilter; + +Arguments makeSmoothingArguments(const DataPath& inputPath, const std::vector& sigma) +{ + Arguments args; + args.insertOrAssign(SmoothFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + args.insertOrAssign(SmoothFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(SmoothFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + args.insertOrAssign(SmoothFilter::k_Sigma_Key, std::make_any>(sigma)); + args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); + return args; +} + +void requireSmoothingInput(const DataStructure& dataStructure, const DataPath& inputPath, usize dimZ) +{ + const IDataArray* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(inputPath)); + REQUIRE(inputArrayPtr->getDataType() == DataType::float32); + REQUIRE(inputArrayPtr->getTupleShape() == ShapeType{dimZ, 4, 4}); + REQUIRE(inputArrayPtr->getComponentShape() == ShapeType{1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +struct SmoothingTransferFaults +{ + usize ReadAt = 0; + usize WriteAt = 0; + std::atomic_bool* CancelOnWritePtr = nullptr; +}; + +// Resident endpoints with real data and controlled transfer outcomes. These are not disk-storage witnesses. +template +class SmoothingTransferStore : public DataStore +{ +public: + explicit SmoothingTransferStore(usize size, T value, SmoothingTransferFaults faults = {}) + : DataStore(ShapeType{size}, ShapeType{1}, value) + , m_FailReadAt(faults.ReadAt) + , m_FailWriteAt(faults.WriteAt) + , m_CancelOnWritePtr(faults.CancelOnWritePtr) + { + } + ~SmoothingTransferStore() noexcept override = default; + SmoothingTransferStore(const SmoothingTransferStore&) = delete; + SmoothingTransferStore(SmoothingTransferStore&&) = delete; + SmoothingTransferStore& operator=(const SmoothingTransferStore&) = delete; + SmoothingTransferStore& operator=(SmoothingTransferStore&&) = delete; + + Result<> copyIntoBuffer(usize offset, nonstd::span values) const override + { + ++m_ReadCount; + if(m_ReadCount == m_FailReadAt) + { + return MakeErrorResult(-23690, "Injected smoothing read failure."); + } + Result<> result = DataStore::copyIntoBuffer(offset, values); + result.warnings().push_back({.code = -23692, .message = "Injected smoothing read warning."}); + return result; + } + Result<> copyFromBuffer(usize offset, nonstd::span values) override + { + ++m_WriteCount; + if(m_WriteCount == m_FailWriteAt) + { + return MakeErrorResult(-23691, "Injected smoothing write failure."); + } + Result<> result = DataStore::copyFromBuffer(offset, values); + result.warnings().push_back({.code = -23693, .message = "Injected smoothing write warning."}); + if(m_CancelOnWritePtr != nullptr) + { + m_CancelOnWritePtr->store(true); + } + return result; + } + +private: + usize m_FailReadAt = 0; + usize m_FailWriteAt = 0; + std::atomic_bool* m_CancelOnWritePtr = nullptr; + mutable usize m_ReadCount = 0; + usize m_WriteCount = 0; +}; + +usize countSmoothingWarning(const Result<>& result, int32 code) +{ + return static_cast(std::count_if(result.warnings().begin(), result.warnings().end(), [code](const auto& warning) { return warning.code == code; })); +} // NOTE: the tests below use analytic oracles and ITK's committed baseline image. No live ITK filter runs here. // A centrally-symmetric radial blob (for the symmetry-preservation check). @@ -93,6 +200,384 @@ std::vector ReadArray(const DataStructure& ds, const DataPath& path) } } // namespace +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active sigma is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize dimZ = GENERATE(usize{1}, usize{4}); + const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; + const usize axis = GENERATE_COPY(from_range(activeAxes)); + const float64 invalidSigma = GENERATE(std::numeric_limits::infinity(), -std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, dimZ, std::vector(16 * dimZ, 1.0F)); + requireSmoothingInput(dataStructure, inputPath, dimZ); + std::vector sigma{1.0, 1.0, 1.0}; + sigma.at(axis) = invalidSigma; + const SmoothFilter filter; + const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); + // This deliberately stops at preflight. Invalid coefficients must never reach an unchecked output conversion. + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize dimZ = GENERATE(usize{1}, usize{4}); + const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; + const usize axis = GENERATE_COPY(from_range(activeAxes)); + const float32 invalidSpacing = GENERATE(std::numeric_limits::infinity(), -std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, dimZ, std::vector(16 * dimZ, 1.0F)); + requireSmoothingInput(dataStructure, inputPath, dimZ); + const DataPath geometryPath = inputPath.getParent().getParent(); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(geometryPath)); + std::array spacing{1.0F, 1.0F, 1.0F}; + spacing.at(axis) = invalidSpacing; + dataStructure.getDataRefAs(geometryPath).setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); + const SmoothFilter filter; + const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, 1.0})); + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize dimZ = GENERATE(usize{1}, usize{4}); + const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; + const usize axis = GENERATE_COPY(from_range(activeAxes)); + const bool underflow = GENERATE(false, true); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, dimZ, std::vector(16 * dimZ, 1.0F)); + requireSmoothingInput(dataStructure, inputPath, dimZ); + const DataPath geometryPath = inputPath.getParent().getParent(); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(geometryPath)); + std::array spacing{1.0F, 1.0F, 1.0F}; + spacing.at(axis) = underflow ? std::numeric_limits::max() : 0.5F; + dataStructure.getDataRefAs(geometryPath).setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); + std::vector sigma{1.0, 1.0, 1.0}; + sigma.at(axis) = underflow ? std::numeric_limits::denorm_min() : std::numeric_limits::max(); + const SmoothFilter filter; + const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); + // Positive finite operands can produce zero or infinity for sigma / abs(spacing). No cutoff on finite scales is imposed. + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negative spacing and unused Z remain accepted", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const float64 unusedSigma = GENERATE(0.0, -1.0, std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); + const float32 unusedSpacing = GENERATE(0.0F, -1.0F, std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, 1, std::vector(16, 1.0F)); + requireSmoothingInput(dataStructure, inputPath, 1); + const DataPath geometryPath = inputPath.getParent().getParent(); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(geometryPath)); + dataStructure.getDataRefAs(geometryPath).setSpacing({-1.0F, -2.0F, unusedSpacing}); + const SmoothFilter filter; + const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, unusedSigma})); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected validation branch has a diagnostic", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize failure = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); + const std::array expectedCodes{ImageProcessing::k_SmoothingGaussianInvalidSigma, ImageProcessing::k_SmoothingGaussianInvalidSpacing, ImageProcessing::k_SmoothingGaussianInvalidScale, + ImageProcessing::k_SmoothingGaussianInvalidCoefficients, ImageProcessing::k_SmoothingGaussianInvalidCoefficients}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, 1, std::vector(16, 1.0F)); + requireSmoothingInput(dataStructure, inputPath, 1); + std::vector sigma{1.0, 1.0, 0.0}; + if(failure == 0) + { + sigma.at(0) = 0.0; + } + if(failure == 1) + { + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath.getParent().getParent())); + dataStructure.getDataRefAs(inputPath.getParent().getParent()).setSpacing({1e-9F, 1.0F, 1.0F}); + } + if(failure == 2) + { + sigma.at(0) = std::numeric_limits::max(); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath.getParent().getParent())); + dataStructure.getDataRefAs(inputPath.getParent().getParent()).setSpacing({0.5F, 1.0F, 1.0F}); + } + // At this finite scale exp(L/scale) and cos(W/scale) round to 1, giving 1-4+6-4+1 == 0 in coefficient denominators. + if(failure == 3) + { + sigma.at(0) = 1e20; + } + // This quotient stays positive, but the trigonometric coefficient arguments overflow. + if(failure == 4) + { + sigma.at(0) = std::numeric_limits::denorm_min(); + } + const SmoothFilter filter; + const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + REQUIRE(preflight.outputActions.errors().front().code == expectedCodes.at(failure)); + REQUIRE(preflight.outputActions.errors().front().message.find("Image Geometry") != std::string::npos); +} + +TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: constants retain type shape and selected endpoint behavior", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int8, int16, int32, int64, float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope algorithmScope(scenario); + const usize dimZ = GENERATE(usize{1}, usize{4}); + const usize endpoint = GENERATE(usize{0}, usize{1}, usize{2}); + TestType value = TestType{0}; + if constexpr(std::is_integral_v) + { + if(endpoint == 1) + { + value = std::numeric_limits::max(); + } + if(endpoint == 2) + { + value = std::numeric_limits::lowest(); + } + } + const bool clips = endpoint == 1 && std::is_integral_v && sizeof(TestType) >= sizeof(int32); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, dimZ, std::vector(16 * dimZ, value)); + const SmoothFilter filter; + const Arguments args = makeSmoothingArguments(inputPath, {1.0, 1.0, 1.0}); + const auto preflight = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + REQUIRE(preflight.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflight.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{dimZ, 4, 4}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + const auto executed = algorithmScope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executed.result); + REQUIRE(countSmoothingWarning(executed.result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == (clips ? 1 : 0)); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& output = dataStructure.getDataRefAs(outputPath); + REQUIRE(output.getDataType() == GetDataType()); + REQUIRE(output.getTupleShape() == ShapeType{dimZ, 4, 4}); + REQUIRE(output.getComponentShape() == ShapeType{1}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); + const auto& values = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); + for(usize index = 0; index < values.getSize(); ++index) + { + REQUIRE(values.getValue(index) == value); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 output retains float32 interpass precision", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope algorithmScope(scenario); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 4, 4, 1, std::vector(16, 16777216.25)); + const SmoothFilter filter; + const auto result = algorithmScope.executeFilter(filter, dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, 0.0})); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + REQUIRE(dataStructure.getDataRefAs(outputPath).getDataType() == DataType::float64); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(outputPath)); + const auto& values = dataStructure.getDataRefAs(outputPath).getDataStoreRef(); + // Unit DC gain preserves the constant until the required Float32 materialization rounds it to 2^24. + for(usize index = 0; index < values.getSize(); ++index) + { + REQUIRE(values.getValue(index) == 16777216.0); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and buffered terminal conversion follow literal expectations", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool buffered = GENERATE(false, true); + const std::array input{93.75F, 127.75F, 128.0F, -128.75F, -129.0F}; + const std::array expected{93, 127, 127, -128, -128}; + const usize fixture = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); + const DataStore source(ShapeType{1}, ShapeType{1}, input.at(fixture)); + DataStore output(ShapeType{1}, ShapeType{1}, int8{0}); + ImageProcessing::detail::PointwiseExecutionOptions options; + options.forceBuffered = buffered; + options.targetScratchBytes = 5; + const std::atomic_bool shouldCancel{false}; + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == (fixture == 2 || fixture == 4 ? 1 : 0)); + REQUIRE(output.getValue(0) == expected.at(fixture)); +} + +TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: floating terminal values retain nonfinite behavior", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", + float32, float64) +{ + UnitTest::LoadPlugins(); + const bool buffered = GENERATE(false, true); + DataStore source(ShapeType{4}, ShapeType{1}, 93.75F); + source.setValue(1, std::numeric_limits::infinity()); + source.setValue(2, -std::numeric_limits::infinity()); + source.setValue(3, std::numeric_limits::quiet_NaN()); + DataStore output(ShapeType{4}, ShapeType{1}, TestType{0}); + ImageProcessing::detail::PointwiseExecutionOptions options; + options.forceBuffered = buffered; + options.targetScratchBytes = 5; + const std::atomic_bool shouldCancel{false}; + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().empty()); + REQUIRE(output.getValue(0) == TestType{93.75}); + REQUIRE(std::isinf(output.getValue(1))); + REQUIRE(output.getValue(1) > TestType{0}); + REQUIRE(std::isinf(output.getValue(2))); + REQUIRE(output.getValue(2) < TestType{0}); + REQUIRE(std::isnan(output.getValue(3))); +} + +TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: terminal nonfinite integer errors and binary64 boundaries", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int32, int64) +{ + UnitTest::LoadPlugins(); + const bool buffered = GENERATE(false, true); + const bool nonfinite = GENERATE(false, true); + const float32 invalidValue = GENERATE(std::numeric_limits::infinity(), -std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); + const float32 upper = std::is_same_v ? 2147483648.0F : 9223372036854775808.0F; + DataStore source(ShapeType{2}, ShapeType{1}, upper); + source.setValue(1, nonfinite ? invalidValue : -upper); + DataStore output(ShapeType{2}, ShapeType{1}, TestType{0}); + ImageProcessing::detail::PointwiseExecutionOptions options; + options.forceBuffered = buffered; + options.targetScratchBytes = 1; + const std::atomic_bool shouldCancel{false}; + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == 1); + if(nonfinite) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == ImageProcessing::k_SmoothingGaussianNonFiniteIntegerOutput); + REQUIRE(result.errors().front().message.find("index 1") != std::string::npos); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(output.getValue(0) == std::numeric_limits::max()); + REQUIRE(output.getValue(1) == std::numeric_limits::lowest()); + } +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warning survives later transfer failure and cancellation", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const usize outcome = GENERATE(usize{0}, usize{1}, usize{2}); + std::atomic_bool shouldCancel{false}; + const SmoothingTransferStore source(8, 128.0F, {.ReadAt = outcome == 0 ? usize{2} : usize{0}}); + SmoothingTransferStore output(8, int8{0}, {.WriteAt = outcome == 1 ? usize{2} : usize{0}, .CancelOnWritePtr = outcome == 2 ? &shouldCancel : nullptr}); + ImageProcessing::detail::PointwiseExecutionOptions options; + options.forceBuffered = true; + options.targetScratchBytes = 5; + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == 1); + REQUIRE(countSmoothingWarning(result, -23692) >= 1); + REQUIRE(countSmoothingWarning(result, -23693) >= 1); + REQUIRE(output.getValue(0) == 127); + if(outcome < 2) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == (outcome == 0 ? -23690 : -23691)); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel.load()); + } +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plane sinks retain successful warnings before error or cancellation", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const SmoothingTransferStore source(64, 0.0F); + std::atomic_bool shouldCancel{false}; + const bool cancel = GENERATE(false, true); + usize calls = 0; + const ImageProcessing::RecursiveGaussianPlaneAxis first{.axis = 0, .sigma = 1.0, .spacing = 1.0, .order = 0, .normalizeAcrossScale = false, .CheckedSmoothing = true}; + const ImageProcessing::RecursiveGaussianPlaneAxis second{.axis = 1, .sigma = 1.0, .spacing = 1.0, .order = 0, .normalizeAcrossScale = false, .CheckedSmoothing = true}; + const auto sink = [&](usize, nonstd::span) -> Result<> { + ++calls; + if(calls == 2) + { + return MakeErrorResult(-23694, "Injected later smoothing sink failure."); + } + if(cancel) + { + shouldCancel.store(true); + } + return MakeWarningVoidResult(-23695, "Injected smoothing sink warning."); + }; + const Result<> result = ImageProcessing::RecursiveGaussianXYPlaneCascadeToSink(source, SizeVec3{4, 4, 4}, first, second, shouldCancel, {}, sink); + REQUIRE(countSmoothingWarning(result, -23695) == 1); + REQUIRE(countSmoothingWarning(result, -23692) >= 1); + if(cancel) + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(calls == 1); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -23694); + } +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tiled transfers keep prior row warnings on failure", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool write = GENERATE(false, true); + SmoothingTransferStore store(8, 0.0F, {.ReadAt = write ? usize{0} : usize{2}, .WriteAt = write ? usize{2} : usize{0}}); + std::array values{1.0F, 2.0F}; + const Result<> result = write ? ImageProcessing::WriteRecursiveGaussian2DBlock(store, 4, 0, 0, 2, 1, values, true) : + ImageProcessing::detail::ReadRecursiveGaussian2DBlock(store, 4, 0, 2, 0, 1, values, true); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == (write ? -23691 : -23690)); + REQUIRE(countSmoothingWarning(result, write ? -23693 : -23692) == 1); +} + +TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis materializations narrow finite overflow explicitly", + "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const uint32 axis = GENERATE(uint32{0}, uint32{1}, uint32{2}); + const bool fast = GENERATE(false, true); + const DataStore source(ShapeType{5, 5, 5}, ShapeType{1}, 2.0 * static_cast(std::numeric_limits::max())); + DataStore output(ShapeType{5, 5, 5}, ShapeType{1}, 0.0F); + ImageProcessing::RecursiveGaussianPassOptions options; + options.CheckedSmoothing = true; + options.useInMemoryFastPath = fast; + options.stagingByteBudget = 256; + const std::atomic_bool shouldCancel{false}; + const Result<> result = ImageProcessing::RecursiveGaussianAxisPass(source, output, SizeVec3{5, 5, 5}, axis, 1.0, 1.0, 0, false, shouldCancel, {}, options); + SIMPLNX_RESULT_REQUIRE_VALID(result); + for(usize index = 0; index < output.getSize(); ++index) + { + REQUIRE(std::isinf(output.getValue(index))); + REQUIRE(output.getValue(index) > 0.0F); + } +} + // ----------------------------------------------------------------------------- // (1) Smoothing preserves a constant image (all signed types). A normalized (DC-gain-1) Gaussian of a constant is the // constant, incl. at the edge-extended boundary. Margin 1.0 tolerates the final float32->int static_cast truncation. @@ -251,7 +736,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); const auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); - REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_RecursiveGaussianNonPositiveSigma); + REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSigma); } SECTION("degenerate spacing rejected") { @@ -269,7 +754,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); const auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); - REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_RecursiveGaussianNonPositiveSpacing); + REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSpacing); } } @@ -279,6 +764,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: FromSIMPLJson", "[NXImageProcessing][SmoothingRecursiveGaussianImageFilter]") { + UnitTest::LoadPlugins(); const nlohmann::json json = { {"Sigma", {{"x", 2.0}, {"y", 3.0}, {"z", 4.0}}}, {"NormalizeAcrossScale", true}, diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp index 5efb14f831..b42fcc900a 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp @@ -23,6 +23,13 @@ inline constexpr int32 k_RecursiveGaussianNonPositiveSigma = -8571; // a filtered axis's spacing is < 1e-8 ("suspiciously small"). Without this guard a zero/near-zero spacing yields NaN // coefficients and silently NaN output. inline constexpr int32 k_RecursiveGaussianNonPositiveSpacing = -8572; +// Selected Smoothing Recursive Gaussian validation and terminal conversion diagnostics. +inline constexpr int32 k_SmoothingGaussianInvalidSigma = -23630; +inline constexpr int32 k_SmoothingGaussianInvalidSpacing = -23631; +inline constexpr int32 k_SmoothingGaussianInvalidScale = -23632; +inline constexpr int32 k_SmoothingGaussianInvalidCoefficients = -23633; +inline constexpr int32 k_SmoothingGaussianNonFiniteIntegerOutput = -23634; +inline constexpr int32 k_SmoothingGaussianClippedIntegerOutput = -23635; // The central-difference gradient magnitude scales each axis derivative by 1/spacing; ITK's GradientMagnitudeImageFilter // throws "Image spacing cannot be zero" when UseImageSpacing is on and an axis spacing is exactly zero. inline constexpr int32 k_GradientMagnitudeZeroSpacing = -8573; diff --git a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp index 502328eccb..a89267401d 100644 --- a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp @@ -3,11 +3,14 @@ #include "simplnx/Common/Array.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Utilities/ImageProcessing/GaussianTemporaryStore.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp" #include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" #include "simplnx/Utilities/ParallelDataAlgorithm.hpp" #include "simplnx/Utilities/StringUtilities.hpp" @@ -44,6 +47,8 @@ struct RecursiveGaussianPassOptions static constexpr usize k_Default2DResidentLimit = 64ULL * 1024ULL * 1024ULL; usize stagingByteBudget = k_FallbackStagingBytes; bool useInMemoryFastPath = true; + // Opt in only for the selected smoothing contract; other recursive filters retain their behavior. + bool CheckedSmoothing = false; }; struct RecursiveGaussianPlaneAxis @@ -53,10 +58,127 @@ struct RecursiveGaussianPlaneAxis double spacing = 1.0; int order = 0; bool normalizeAcrossScale = false; + bool CheckedSmoothing = false; }; namespace detail { +/** @brief Narrows the existing double recurrence result without changing its arithmetic or pass precision. */ +inline float32 NarrowRecursiveGaussianValue(double value, bool checkedSmoothing) +{ + return checkedSmoothing ? NarrowFloatingImageValue(value) : static_cast(value); +} + +/** @brief Retains successful transfer/sink warnings only for the selected smoothing path. */ +inline Result<> MergeRecursiveGaussianResults(bool checkedSmoothing, Result<> previous, Result<> current) +{ + if(checkedSmoothing) + { + return MergeResults(std::move(previous), std::move(current)); + } + return current.invalid() ? std::move(current) : Result<>{}; +} + +class SmoothingGaussianConversionState +{ +public: + SmoothingGaussianConversionState() = default; + ~SmoothingGaussianConversionState() = default; + SmoothingGaussianConversionState(const SmoothingGaussianConversionState&) = delete; + SmoothingGaussianConversionState(SmoothingGaussianConversionState&&) = delete; + SmoothingGaussianConversionState& operator=(const SmoothingGaussianConversionState&) = delete; + SmoothingGaussianConversionState& operator=(SmoothingGaussianConversionState&&) = delete; + + void recordClipping() noexcept + { + m_Clipped.store(true, std::memory_order_relaxed); + } + bool clipped() const noexcept + { + return m_Clipped.load(std::memory_order_relaxed); + } + void recordNonFinite(usize index) noexcept + { + usize previous = m_NonFiniteIndex.load(std::memory_order_relaxed); + while(index < previous && !m_NonFiniteIndex.compare_exchange_weak(previous, index, std::memory_order_relaxed)) + { + } + } + usize nonFiniteIndex() const noexcept + { + return m_NonFiniteIndex.load(std::memory_order_relaxed); + } + +private: + std::atomic m_Clipped{false}; + std::atomic m_NonFiniteIndex{std::numeric_limits::max()}; +}; + +template +bool ConvertSmoothingGaussianBuffer(nonstd::span input, nonstd::span output, usize offset, SmoothingGaussianConversionState& state, const std::atomic_bool& shouldCancel, + usize valuesPerRow = 0, usize rowStride = 0) +{ + bool clipped = false; + for(usize index = 0; index < input.size(); ++index) + { + if((index & 4095ULL) == 0 && shouldCancel) + { + if(clipped) + { + state.recordClipping(); + } + return false; + } + const auto status = ConvertTerminalImageValue(input[index], output[index]); + if(status == TerminalImageConversionStatus::NonFinite) + { + const usize failedIndex = valuesPerRow == 0 ? offset + index : offset + ((index / valuesPerRow) * rowStride) + (index % valuesPerRow); + state.recordNonFinite(failedIndex); + if(clipped) + { + state.recordClipping(); + } + return false; + } + clipped = clipped || status == TerminalImageConversionStatus::Clipped; + } + if(clipped) + { + state.recordClipping(); + } + return true; +} + +template +Result<> SmoothingGaussianNonFiniteResult(const SmoothingGaussianConversionState& state) +{ + if(state.nonFiniteIndex() == std::numeric_limits::max()) + { + return {}; + } + return MakeErrorResult(k_SmoothingGaussianNonFiniteIntegerOutput, fmt::format("Smoothing Recursive Gaussian produced a nonfinite value at flat value index {} for output type '{}'. Use a " + "floating-point input array to retain nonfinite output, or correct nonfinite input values.", + state.nonFiniteIndex(), DataTypeToString(GetDataType()))); +} + +/** @brief Finalizes the one owner warning on success, cancellation, or a later transfer/conversion failure. */ +template +Result<> FinishSmoothingGaussianConversion(const SmoothingGaussianConversionState& state, Result<> result) +{ + if(result.valid()) + { + result = MergeResults(std::move(result), SmoothingGaussianNonFiniteResult(state)); + } + if(state.clipped()) + { + result.warnings().push_back({.code = k_SmoothingGaussianClippedIntegerOutput, + .message = fmt::format("Smoothing Recursive Gaussian clipped finite final integer values to the '{}' range after truncation. Review the " + "output before thresholding or segmentation; use a floating-point input to retain these values.", + DataTypeToString(GetDataType()))}); + } + return result; +} + // Four-line batches are the computational unit, not the reporting unit. Group them so a worker // enters the shared progress seam once per this many batches. constexpr usize k_ProgressBatchGroup = 64; @@ -343,6 +465,48 @@ inline RecursiveGaussianCoefficients ComputeRecursiveGaussianCoefficients(double return c; } +/** @brief Checks the selected smoothing controls and formed coefficients without imposing an accuracy cutoff. */ +inline Result<> ValidateSmoothingGaussianControls(const std::vector& sigma, const FloatVec3& spacing, const SizeVec3& dims) +{ + const usize activeAxes = dims[2] > 1 ? 3 : 2; + for(usize axis = 0; axis < activeAxes; ++axis) + { + if(axis >= sigma.size() || !std::isfinite(sigma.at(axis)) || sigma.at(axis) <= 0.0) + { + return MakeErrorResult(k_SmoothingGaussianInvalidSigma, + fmt::format("Smoothing Recursive Gaussian Sigma for active axis {} must be finite and positive; received {}. Correct Sigma before execution.", axis, + axis < sigma.size() ? fmt::format("{}", sigma.at(axis)) : "no value")); + } + const double spacingValue = static_cast(spacing[axis]); + if(!std::isfinite(spacingValue) || !(std::abs(spacingValue) > 1e-8)) + { + return MakeErrorResult( + k_SmoothingGaussianInvalidSpacing, + fmt::format("Smoothing Recursive Gaussian spacing for active axis {} must be finite with absolute value greater than 1e-8; received {}. Correct the Image Geometry spacing.", axis, + spacingValue)); + } + const double scale = sigma.at(axis) / std::abs(spacingValue); + if(!std::isfinite(scale) || scale <= 0.0) + { + return MakeErrorResult( + k_SmoothingGaussianInvalidScale, + fmt::format("Smoothing Recursive Gaussian effective scale for active axis {} is {} (Sigma {}, spacing {}). Choose values whose Sigma / absolute spacing is finite and positive.", axis, scale, + sigma.at(axis), spacingValue)); + } + const auto coefficients = ComputeRecursiveGaussianCoefficients(sigma.at(axis), spacingValue, 0, false); + const std::array values{coefficients.n0, coefficients.n1, coefficients.n2, coefficients.n3, coefficients.d1, coefficients.d2, coefficients.d3, + coefficients.d4, coefficients.m1, coefficients.m2, coefficients.m3, coefficients.m4, coefficients.bn1, coefficients.bn2, + coefficients.bn3, coefficients.bn4, coefficients.bm1, coefficients.bm2, coefficients.bm3, coefficients.bm4}; + if(!std::ranges::all_of(values, [](double value) { return std::isfinite(value); })) + { + return MakeErrorResult(k_SmoothingGaussianInvalidCoefficients, fmt::format("Smoothing Recursive Gaussian coefficients are nonfinite for active axis {} (Sigma {}, spacing {}, effective scale " + "{}). Choose different Sigma or spacing values. Finite coefficients do not guarantee accuracy at extreme scales.", + axis, sigma.at(axis), spacingValue, scale)); + } + } + return {}; +} + // Port of RecursiveSeparableImageFilter::FilterDataArray. `data` and `outs` are length-`ln` double buffers // (ln >= 4). `outs` holds the causal result, then the anti-causal recurrence is accumulated directly using a // four-value rolling window. ITK materializes that second response in another full line buffer; the rolling form @@ -690,20 +854,22 @@ inline usize RecursiveGaussian2DXBlockCount(usize dimX, usize coreCols) } template -Result<> ReadRecursiveGaussian2DBlock(const StoreT& store, usize dimX, usize yBegin, usize rowCount, usize xBegin, usize columnCount, nonstd::span values) +Result<> ReadRecursiveGaussian2DBlock(const StoreT& store, usize dimX, usize yBegin, usize rowCount, usize xBegin, usize columnCount, nonstd::span values, bool checkedSmoothing = false) { + Result<> accumulated; if(columnCount == dimX) { return store.copyIntoBuffer(yBegin * dimX, values.subspan(0, rowCount * columnCount)); } for(usize row = 0; row < rowCount; ++row) { - if(Result<> result = store.copyIntoBuffer((yBegin + row) * dimX + xBegin, values.subspan(row * columnCount, columnCount)); result.invalid()) + accumulated = MergeRecursiveGaussianResults(checkedSmoothing, std::move(accumulated), store.copyIntoBuffer(((yBegin + row) * dimX) + xBegin, values.subspan(row * columnCount, columnCount))); + if(accumulated.invalid()) { - return result; + return accumulated; } } - return {}; + return accumulated; } template @@ -812,7 +978,7 @@ void ComputeFourRecursiveGaussianCausalBlocks(nonstd::span source, u template void ComputeRecursiveGaussianAntiCausalBlock(nonstd::span source, nonstd::span causal, usize rowCount, usize columnCount, usize column, bool lastBlock, - const RecursiveGaussianCoefficients& coefficients, std::array& antiState, nonstd::span filtered) + const RecursiveGaussianCoefficients& coefficients, std::array& antiState, nonstd::span filtered, bool checkedSmoothing = false) { if(lastBlock) { @@ -827,17 +993,17 @@ void ComputeRecursiveGaussianAntiCausalBlock(nonstd::span source, no next3 -= next4 * coefficients.d1 + outputValue * coefficients.bm2 + outputValue * coefficients.bm3 + outputValue * coefficients.bm4; next2 -= next3 * coefficients.d1 + next4 * coefficients.d2 + outputValue * coefficients.bm3 + outputValue * coefficients.bm4; next1 -= next2 * coefficients.d1 + next3 * coefficients.d2 + next4 * coefficients.d3 + outputValue * coefficients.bm4; - filtered[(rowCount - 1) * columnCount + column] = static_cast(causal[(rowCount - 1) * columnCount + column] + next4); - filtered[(rowCount - 2) * columnCount + column] = static_cast(causal[(rowCount - 2) * columnCount + column] + next3); - filtered[(rowCount - 3) * columnCount + column] = static_cast(causal[(rowCount - 3) * columnCount + column] + next2); - filtered[(rowCount - 4) * columnCount + column] = static_cast(causal[(rowCount - 4) * columnCount + column] + next1); + filtered[((rowCount - 1) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 1) * columnCount) + column] + next4, checkedSmoothing); + filtered[((rowCount - 2) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 2) * columnCount) + column] + next3, checkedSmoothing); + filtered[((rowCount - 3) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 3) * columnCount) + column] + next2, checkedSmoothing); + filtered[((rowCount - 4) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 4) * columnCount) + column] + next1, checkedSmoothing); antiState = {next1, next2, next3, next4}; for(usize inputRow = rowCount - 4; inputRow > 0; --inputRow) { double current = static_cast(source[inputRow * columnCount + column]) * coefficients.m1 + static_cast(source[(inputRow + 1) * columnCount + column]) * coefficients.m2 + static_cast(source[(inputRow + 2) * columnCount + column]) * coefficients.m3 + static_cast(source[(inputRow + 3) * columnCount + column]) * coefficients.m4; current -= antiState[0] * coefficients.d1 + antiState[1] * coefficients.d2 + antiState[2] * coefficients.d3 + antiState[3] * coefficients.d4; - filtered[(inputRow - 1) * columnCount + column] = static_cast(causal[(inputRow - 1) * columnCount + column] + current); + filtered[((inputRow - 1) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((inputRow - 1) * columnCount) + column] + current, checkedSmoothing); antiState = {current, antiState[0], antiState[1], antiState[2]}; } return; @@ -850,14 +1016,15 @@ void ComputeRecursiveGaussianAntiCausalBlock(nonstd::span source, no double current = static_cast(source[inputRow * columnCount + column]) * coefficients.m1 + static_cast(source[(inputRow + 1) * columnCount + column]) * coefficients.m2 + static_cast(source[(inputRow + 2) * columnCount + column]) * coefficients.m3 + static_cast(source[(inputRow + 3) * columnCount + column]) * coefficients.m4; current -= antiState[0] * coefficients.d1 + antiState[1] * coefficients.d2 + antiState[2] * coefficients.d3 + antiState[3] * coefficients.d4; - filtered[outputRow * columnCount + column] = static_cast(causal[outputRow * columnCount + column] + current); + filtered[(outputRow * columnCount) + column] = NarrowRecursiveGaussianValue(causal[(outputRow * columnCount) + column] + current, checkedSmoothing); antiState = {current, antiState[0], antiState[1], antiState[2]}; } } template void ComputeFourRecursiveGaussianAntiCausalBlocks(nonstd::span source, nonstd::span causal, usize rowCount, usize columnCount, usize firstColumn, bool lastBlock, - const RecursiveGaussianCoefficients& coefficients, nonstd::span> antiStates, nonstd::span filtered) + const RecursiveGaussianCoefficients& coefficients, nonstd::span> antiStates, nonstd::span filtered, + bool checkedSmoothing = false) { if(lastBlock) { @@ -876,10 +1043,10 @@ void ComputeFourRecursiveGaussianAntiCausalBlocks(nonstd::span sourc next3 -= next4 * coefficients.d1 + outputValue * coefficients.bm2 + outputValue * coefficients.bm3 + outputValue * coefficients.bm4; next2 -= next3 * coefficients.d1 + next4 * coefficients.d2 + outputValue * coefficients.bm3 + outputValue * coefficients.bm4; next1 -= next2 * coefficients.d1 + next3 * coefficients.d2 + next4 * coefficients.d3 + outputValue * coefficients.bm4; - filtered[(rowCount - 1) * columnCount + column] = static_cast(causal[(rowCount - 1) * columnCount + column] + next4); - filtered[(rowCount - 2) * columnCount + column] = static_cast(causal[(rowCount - 2) * columnCount + column] + next3); - filtered[(rowCount - 3) * columnCount + column] = static_cast(causal[(rowCount - 3) * columnCount + column] + next2); - filtered[(rowCount - 4) * columnCount + column] = static_cast(causal[(rowCount - 4) * columnCount + column] + next1); + filtered[((rowCount - 1) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 1) * columnCount) + column] + next4, checkedSmoothing); + filtered[((rowCount - 2) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 2) * columnCount) + column] + next3, checkedSmoothing); + filtered[((rowCount - 3) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 3) * columnCount) + column] + next2, checkedSmoothing); + filtered[((rowCount - 4) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((rowCount - 4) * columnCount) + column] + next1, checkedSmoothing); antiStates[column] = {next1, next2, next3, next4}; } for(usize inputRow = rowCount - 4; inputRow > 0; --inputRow) @@ -891,7 +1058,7 @@ void ComputeFourRecursiveGaussianAntiCausalBlocks(nonstd::span sourc double current = static_cast(source[inputRow * columnCount + column]) * coefficients.m1 + static_cast(source[(inputRow + 1) * columnCount + column]) * coefficients.m2 + static_cast(source[(inputRow + 2) * columnCount + column]) * coefficients.m3 + static_cast(source[(inputRow + 3) * columnCount + column]) * coefficients.m4; current -= antiState[0] * coefficients.d1 + antiState[1] * coefficients.d2 + antiState[2] * coefficients.d3 + antiState[3] * coefficients.d4; - filtered[(inputRow - 1) * columnCount + column] = static_cast(causal[(inputRow - 1) * columnCount + column] + current); + filtered[((inputRow - 1) * columnCount) + column] = NarrowRecursiveGaussianValue(causal[((inputRow - 1) * columnCount) + column] + current, checkedSmoothing); antiState = {current, antiState[0], antiState[1], antiState[2]}; } } @@ -908,7 +1075,7 @@ void ComputeFourRecursiveGaussianAntiCausalBlocks(nonstd::span sourc double current = static_cast(source[inputRow * columnCount + column]) * coefficients.m1 + static_cast(source[(inputRow + 1) * columnCount + column]) * coefficients.m2 + static_cast(source[(inputRow + 2) * columnCount + column]) * coefficients.m3 + static_cast(source[(inputRow + 3) * columnCount + column]) * coefficients.m4; current -= antiState[0] * coefficients.d1 + antiState[1] * coefficients.d2 + antiState[2] * coefficients.d3 + antiState[3] * coefficients.d4; - filtered[outputRow * columnCount + column] = static_cast(causal[outputRow * columnCount + column] + current); + filtered[(outputRow * columnCount) + column] = NarrowRecursiveGaussianValue(causal[(outputRow * columnCount) + column] + current, checkedSmoothing); antiState = {current, antiState[0], antiState[1], antiState[2]}; } } @@ -945,7 +1112,7 @@ void FilterRecursiveGaussianXYPlane(nonstd::span srcPlane, nonstd::s detail::FilterDataArray(outs.data(), data.data(), lineLength, coefficients); for(usize i = 0; i < lineLength; ++i) { - dstPlane[rowOffset + i] = static_cast(outs[i]); + dstPlane[rowOffset + i] = NarrowRecursiveGaussianValue(outs[i], pass.CheckedSmoothing); } } else @@ -957,7 +1124,7 @@ void FilterRecursiveGaussianXYPlane(nonstd::span srcPlane, nonstd::s detail::FilterDataArray(outs.data(), data.data(), lineLength, coefficients); for(usize i = 0; i < lineLength; ++i) { - dstPlane[i * dimX + line] = static_cast(outs[i]); + dstPlane[(i * dimX) + line] = NarrowRecursiveGaussianValue(outs[i], pass.CheckedSmoothing); } } } @@ -970,7 +1137,7 @@ void FilterRecursiveGaussianXYPlane(nonstd::span srcPlane, nonstd::s template void FilterFourRecursiveGaussianXRows(nonstd::span source, nonstd::span destination, usize dimX, usize dimY, const RecursiveGaussianCoefficients& coefficients, - const std::atomic_bool& shouldCancel, const std::function& reportProgress = {}) + const std::atomic_bool& shouldCancel, const std::function& reportProgress = {}, bool checkedSmoothing = false) { const usize batchCount = (dimY + 3) / 4; auto filterBatches = [&](const Range& batchRange) { @@ -1005,7 +1172,7 @@ void FilterFourRecursiveGaussianXRows(nonstd::span source, nonstd::s { for(usize lane = 0; lane < 4; ++lane) { - destination[(firstRow + lane) * dimX + x] = static_cast(batchOutput[x * 4 + lane]); + destination[((firstRow + lane) * dimX) + x] = NarrowRecursiveGaussianValue(batchOutput[(x * 4) + lane], checkedSmoothing); } } } @@ -1021,7 +1188,7 @@ void FilterFourRecursiveGaussianXRows(nonstd::span source, nonstd::s FilterDataArray(tailOutput.data(), tailData.data(), dimX, coefficients); for(usize x = 0; x < dimX; ++x) { - destination[rowOffset + x] = static_cast(tailOutput[x]); + destination[rowOffset + x] = NarrowRecursiveGaussianValue(tailOutput[x], checkedSmoothing); } } } @@ -1055,6 +1222,7 @@ template Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const SizeVec3& dims, const RecursiveGaussianPlaneAxis& pass, const detail::RecursiveGaussian2DBufferPlan& plan, CheckpointStoreT& checkpointStore, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, SinkT&& sink) { + Result<> accumulated; if(pass.axis != 1 || dims[2] != 1 || dims[0] == 0 || dims[1] < 4 || !plan.valid || plan.coreCols == 0 || plan.blockRows < 4 || plan.blockCount == 0) { return MakeErrorResult(-23613, @@ -1092,22 +1260,28 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size { if(shouldCancel) { - return {}; + return accumulated; } if(blockIndex > 0) { - if(Result<> result = checkpointStore.copyFromBuffer(blockIndex * plan.coreCols, nonstd::span(causalStates.data(), columnCount)); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults( + pass.CheckedSmoothing, std::move(accumulated), + checkpointStore.copyFromBuffer(blockIndex * plan.coreCols, nonstd::span(causalStates.data(), columnCount))); + if(accumulated.invalid()) { - return result; + return accumulated; } } const usize yBegin = detail::RecursiveGaussian2DBlockBegin(blockIndex, plan); const usize rowCount = detail::RecursiveGaussian2DBlockRows(blockIndex, plan, dims[1]); source.resize(rowCount * columnCount); causal.resize(rowCount * columnCount); - if(Result<> result = detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yBegin, rowCount, xBegin, columnCount, nonstd::span(source.data(), source.size())); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults( + pass.CheckedSmoothing, std::move(accumulated), + detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yBegin, rowCount, xBegin, columnCount, nonstd::span(source.data(), source.size()), pass.CheckedSmoothing)); + if(accumulated.invalid()) { - return result; + return accumulated; } const usize columnBatchCount = (columnCount + 3) / 4; auto computeForward = [&](const Range& batchRange) { @@ -1143,7 +1317,7 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size { if(shouldCancel) { - return {}; + return accumulated; } const usize blockIndex = reverseIndex - 1; const usize yBegin = detail::RecursiveGaussian2DBlockBegin(blockIndex, plan); @@ -1153,27 +1327,37 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size source.resize((rowCount + haloRows) * columnCount); causal.resize(rowCount * columnCount); filtered.resize(rowCount * columnCount); - if(Result<> result = detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yBegin, rowCount, xBegin, columnCount, nonstd::span(source.data(), rowCount * columnCount)); - result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults( + pass.CheckedSmoothing, std::move(accumulated), + detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yBegin, rowCount, xBegin, columnCount, nonstd::span(source.data(), rowCount * columnCount), pass.CheckedSmoothing)); + if(accumulated.invalid()) { - return result; + return accumulated; } if(haloRows > 0) { - if(Result<> result = detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yEnd, haloRows, xBegin, columnCount, - nonstd::span(source.data() + rowCount * columnCount, haloRows * columnCount)); - result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), + detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yEnd, haloRows, xBegin, columnCount, + nonstd::span(source.data() + (rowCount * columnCount), haloRows * columnCount), + pass.CheckedSmoothing)); + if(accumulated.invalid()) { - return result; + return accumulated; } } if(blockIndex == 0) { std::fill(causalStates.begin(), causalStates.end(), detail::RecursiveGaussian2DCheckpoint{}); } - else if(Result<> result = checkpointStore.copyIntoBuffer(blockIndex * plan.coreCols, nonstd::span(causalStates.data(), columnCount)); result.invalid()) + else + { + accumulated = + detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), + checkpointStore.copyIntoBuffer(blockIndex * plan.coreCols, nonstd::span(causalStates.data(), columnCount))); + } + if(accumulated.invalid()) { - return result; + return accumulated; } const usize columnBatchCount = (columnCount + 3) / 4; auto computeBackward = [&](const Range& batchRange) { @@ -1188,7 +1372,7 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size nonstd::span(causal.data(), causal.size())); detail::ComputeFourRecursiveGaussianAntiCausalBlocks( nonstd::span(source.data(), source.size()), nonstd::span(causal.data(), causal.size()), rowCount, columnCount, firstColumn, blockIndex + 1 == plan.blockCount, - coefficients, nonstd::span>(antiCausalStates.data(), antiCausalStates.size()), nonstd::span(filtered.data(), filtered.size())); + coefficients, nonstd::span>(antiCausalStates.data(), antiCausalStates.size()), nonstd::span(filtered.data(), filtered.size()), pass.CheckedSmoothing); } else { @@ -1199,7 +1383,7 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size causalStates[column], nonstd::span(causal.data(), causal.size())); detail::ComputeRecursiveGaussianAntiCausalBlock(nonstd::span(source.data(), source.size()), nonstd::span(causal.data(), causal.size()), rowCount, columnCount, column, blockIndex + 1 == plan.blockCount, coefficients, antiCausalStates[column], - nonstd::span(filtered.data(), filtered.size())); + nonstd::span(filtered.data(), filtered.size()), pass.CheckedSmoothing); } } } @@ -1209,39 +1393,45 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size parallelAlgorithm.execute(computeBackward); if(shouldCancel) { - return {}; + return accumulated; } - if(Result<> result = sink(yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size())); result.invalid()) + accumulated = + detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sink(yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } progressThrottle.incrementPercent(1, 1); } } - return {}; + return accumulated; } template -Result<> WriteRecursiveGaussian2DBlock(StoreT& store, usize dimX, usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span values) +Result<> WriteRecursiveGaussian2DBlock(StoreT& store, usize dimX, usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span values, bool checkedSmoothing = false) { + Result<> accumulated; if(columnCount == dimX) { return store.copyFromBuffer(yBegin * dimX, values.subspan(0, rowCount * columnCount)); } for(usize row = 0; row < rowCount; ++row) { - if(Result<> result = store.copyFromBuffer((yBegin + row) * dimX + xBegin, values.subspan(row * columnCount, columnCount)); result.invalid()) + accumulated = + detail::MergeRecursiveGaussianResults(checkedSmoothing, std::move(accumulated), store.copyFromBuffer(((yBegin + row) * dimX) + xBegin, values.subspan(row * columnCount, columnCount))); + if(accumulated.invalid()) { - return result; + return accumulated; } } - return {}; + return accumulated; } template Result<> RecursiveGaussian2DTiledXAxisToSink(const SrcStoreT& sourceStore, CheckpointStoreT& checkpointStore, const SizeVec3& dims, const RecursiveGaussianPlaneAxis& pass, const detail::RecursiveGaussian2DBufferPlan& plan, const std::atomic_bool& shouldCancel, SinkT&& sink) { + Result<> accumulated; const usize blockValues = detail::RecursiveGaussian2DXBlockValues(dims[0], plan.coreCols); const usize blockCount = detail::RecursiveGaussian2DXBlockCount(dims[0], plan.coreCols); if(dims[0] - (blockCount - 1) * blockValues < 4 || checkpointStore.getSize() < blockCount) @@ -1261,22 +1451,26 @@ Result<> RecursiveGaussian2DTiledXAxisToSink(const SrcStoreT& sourceStore, Check { if(shouldCancel) { - return {}; + return accumulated; } if(blockIndex > 0) { - if(Result<> result = checkpointStore.copyFromBuffer(blockIndex, nonstd::span(&causalState, 1)); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), + checkpointStore.copyFromBuffer(blockIndex, nonstd::span(&causalState, 1))); + if(accumulated.invalid()) { - return result; + return accumulated; } } const usize xBegin = blockIndex * blockValues; const usize valueCount = std::min(blockValues, dims[0] - xBegin); source.resize(valueCount); causal.resize(valueCount); - if(Result<> result = sourceStore.copyIntoBuffer(y * dims[0] + xBegin, nonstd::span(source.data(), source.size())); result.invalid()) + accumulated = + detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sourceStore.copyIntoBuffer((y * dims[0]) + xBegin, nonstd::span(source.data(), source.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } detail::ComputeRecursiveGaussianCausalBlock(nonstd::span(source.data(), source.size()), valueCount, 1, 0, blockIndex == 0, coefficients, causalState, nonstd::span(causal.data(), causal.size())); @@ -1287,7 +1481,7 @@ Result<> RecursiveGaussian2DTiledXAxisToSink(const SrcStoreT& sourceStore, Check { if(shouldCancel) { - return {}; + return accumulated; } const usize blockIndex = reverseIndex - 1; const usize xBegin = blockIndex * blockValues; @@ -1296,35 +1490,44 @@ Result<> RecursiveGaussian2DTiledXAxisToSink(const SrcStoreT& sourceStore, Check source.resize(valueCount + haloValues); causal.resize(valueCount); filtered.resize(valueCount); - if(Result<> result = sourceStore.copyIntoBuffer(y * dims[0] + xBegin, nonstd::span(source.data(), source.size())); result.invalid()) + accumulated = + detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sourceStore.copyIntoBuffer((y * dims[0]) + xBegin, nonstd::span(source.data(), source.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } if(blockIndex == 0) { causalState = {}; } - else if(Result<> result = checkpointStore.copyIntoBuffer(blockIndex, nonstd::span(&causalState, 1)); result.invalid()) + else { - return result; + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), + checkpointStore.copyIntoBuffer(blockIndex, nonstd::span(&causalState, 1))); + } + if(accumulated.invalid()) + { + return accumulated; } detail::ComputeRecursiveGaussianCausalBlock(nonstd::span(source.data(), source.size()), valueCount, 1, 0, blockIndex == 0, coefficients, causalState, nonstd::span(causal.data(), causal.size())); detail::ComputeRecursiveGaussianAntiCausalBlock(nonstd::span(source.data(), source.size()), nonstd::span(causal.data(), causal.size()), valueCount, 1, 0, - blockIndex + 1 == blockCount, coefficients, antiState, nonstd::span(filtered.data(), filtered.size())); - if(Result<> result = sink(y, xBegin, 1, valueCount, nonstd::span(filtered.data(), filtered.size())); result.invalid()) + blockIndex + 1 == blockCount, coefficients, antiState, nonstd::span(filtered.data(), filtered.size()), pass.CheckedSmoothing); + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sink(y, xBegin, 1, valueCount, nonstd::span(filtered.data(), filtered.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } } } - return {}; + return accumulated; } template Result<> RecursiveGaussian2DXAxisToSink(const SrcStoreT& sourceStore, const SizeVec3& dims, const RecursiveGaussianPlaneAxis& pass, const detail::RecursiveGaussian2DBufferPlan& plan, CheckpointStoreT& checkpointStore, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, SinkT&& sink) { + Result<> accumulated; if(pass.axis != 0 || dims[2] != 1 || dims[0] < 4 || dims[1] == 0 || !plan.valid || plan.blockRows == 0) { return MakeErrorResult(-23616, @@ -1359,29 +1562,31 @@ Result<> RecursiveGaussian2DXAxisToSink(const SrcStoreT& sourceStore, const Size { if(shouldCancel) { - return {}; + return accumulated; } const usize rowCount = std::min(rowsPerBlock, dims[1] - yBegin); source.resize(rowCount * dims[0]); filtered.resize(rowCount * dims[0]); - if(Result<> result = sourceStore.copyIntoBuffer(yBegin * dims[0], nonstd::span(source.data(), source.size())); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sourceStore.copyIntoBuffer(yBegin * dims[0], nonstd::span(source.data(), source.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } const SizeVec3 blockDims{dims[0], rowCount, 1}; detail::FilterFourRecursiveGaussianXRows(nonstd::span(source.data(), source.size()), nonstd::span(filtered.data(), filtered.size()), blockDims[0], blockDims[1], - coefficients, shouldCancel); + coefficients, shouldCancel, {}, pass.CheckedSmoothing); if(shouldCancel) { - return {}; + return accumulated; } - if(Result<> result = sink(yBegin, 0, rowCount, dims[0], nonstd::span(filtered.data(), filtered.size())); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), sink(yBegin, 0, rowCount, dims[0], nonstd::span(filtered.data(), filtered.size()))); + if(accumulated.invalid()) { - return result; + return accumulated; } progressThrottle.incrementPercent(1, 1); } - return {}; + return accumulated; } template @@ -1408,6 +1613,7 @@ Result<> RecursiveGaussian2DYXCascadeToSink(const SrcStoreT& sourceStore, WorkSt const RecursiveGaussianPlaneAxis& secondPass, const detail::RecursiveGaussian2DBufferPlan& plan, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, SinkT&& sink) { + Result<> accumulated; if(firstPass.axis != 1 || secondPass.axis != 0) { return MakeErrorResult(-23619, fmt::format("Recursive Gaussian bounded 2D YX cascade requires axes 1 then 0, but received axes {} then {}.", firstPass.axis, secondPass.axis)); @@ -1415,18 +1621,21 @@ Result<> RecursiveGaussian2DYXCascadeToSink(const SrcStoreT& sourceStore, WorkSt if(plan.coreCols != dims[0]) { auto workSink = [&](usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span values) { - return WriteRecursiveGaussian2DBlock(workStore, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(values.data(), values.size())); + return WriteRecursiveGaussian2DBlock(workStore, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(values.data(), values.size()), firstPass.CheckedSmoothing); }; - if(Result<> result = RecursiveGaussian2DYAxisToSink(sourceStore, dims, firstPass, plan, checkpointStore, shouldCancel, messageHandler, workSink); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(firstPass.CheckedSmoothing, std::move(accumulated), + RecursiveGaussian2DYAxisToSink(sourceStore, dims, firstPass, plan, checkpointStore, shouldCancel, messageHandler, workSink)); + if(accumulated.invalid()) { - return result; + return accumulated; } std::vector terminalScratch; auto terminalSink = [&](usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span filtered) -> Result<> { terminalScratch.resize(filtered.size()); return sink(yBegin, xBegin, rowCount, columnCount, filtered, nonstd::span(terminalScratch.data(), terminalScratch.size())); }; - return RecursiveGaussian2DXAxisToSink(workStore, dims, secondPass, plan, checkpointStore, shouldCancel, messageHandler, terminalSink); + return detail::MergeRecursiveGaussianResults(firstPass.CheckedSmoothing, std::move(accumulated), + RecursiveGaussian2DXAxisToSink(workStore, dims, secondPass, plan, checkpointStore, shouldCancel, messageHandler, terminalSink)); } const detail::RecursiveGaussianCoefficients secondCoefficients = detail::ComputeRecursiveGaussianCoefficients(secondPass.sigma, secondPass.spacing, secondPass.order, secondPass.normalizeAcrossScale); @@ -1435,7 +1644,7 @@ Result<> RecursiveGaussian2DYXCascadeToSink(const SrcStoreT& sourceStore, WorkSt secondOutput.resize(firstOutput.size()); const SizeVec3 blockDims{dims[0], rowCount, 1}; detail::FilterFourRecursiveGaussianXRows(nonstd::span(firstOutput.data(), firstOutput.size()), nonstd::span(secondOutput.data(), secondOutput.size()), - blockDims[0], blockDims[1], secondCoefficients, shouldCancel); + blockDims[0], blockDims[1], secondCoefficients, shouldCancel, {}, secondPass.CheckedSmoothing); if(shouldCancel) { return {}; @@ -1459,6 +1668,7 @@ template Result<> RecursiveGaussianXYPlaneCascadeToSink(const SrcStoreT& src, const SizeVec3& dims, const RecursiveGaussianPlaneAxis& firstPass, const RecursiveGaussianPlaneAxis& secondPass, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, SinkT&& sink) { + Result<> accumulated; if(firstPass.axis > 1 || secondPass.axis > 1 || firstPass.axis == secondPass.axis) { return MakeErrorResult(-23600, fmt::format("Recursive Gaussian XY plane cascade requires two distinct X/Y axes, but received axes {} and {}.", firstPass.axis, secondPass.axis)); @@ -1501,33 +1711,35 @@ Result<> RecursiveGaussianXYPlaneCascadeToSink(const SrcStoreT& src, const SizeV { if(shouldCancel) { - return {}; + return accumulated; } const usize planeOffset = z * planeValues; - if(Result<> result = src.copyIntoBuffer(planeOffset, nonstd::span(srcPlane.data(), planeValues)); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(firstPass.CheckedSmoothing, std::move(accumulated), src.copyIntoBuffer(planeOffset, nonstd::span(srcPlane.data(), planeValues))); + if(accumulated.invalid()) { - return result; + return accumulated; } detail::FilterRecursiveGaussianXYPlane(nonstd::span(srcPlane.data(), planeValues), nonstd::span(firstPlane.data(), planeValues), dims, firstPass, firstCoefficients, shouldCancel); if(shouldCancel) { - return {}; + return accumulated; } detail::FilterRecursiveGaussianXYPlane(nonstd::span(firstPlane.data(), planeValues), nonstd::span(secondPlane.data(), planeValues), dims, secondPass, secondCoefficients, shouldCancel); if(shouldCancel) { - return {}; + return accumulated; } - if(Result<> result = sink(planeOffset, nonstd::span(secondPlane.data(), planeValues)); result.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(firstPass.CheckedSmoothing, std::move(accumulated), sink(planeOffset, nonstd::span(secondPlane.data(), planeValues))); + if(accumulated.invalid()) { - return result; + return accumulated; } progressThrottle.incrementPercent(1, 1); } - return {}; + return accumulated; } // One separable recursive-Gaussian pass along `axis` (0=x,1=y,2=z). Reads `src` (any scalar SrcT), promotes each line @@ -1538,6 +1750,7 @@ template Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const SizeVec3& dims, uint32 axis, double sigma, double spacing, int order, bool normalizeAcrossScale, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, const RecursiveGaussianPassOptions& options = {}) { + Result<> accumulated; const int64 nX = static_cast(dims[0]); const int64 nY = static_cast(dims[1]); const int64 nZ = static_cast(dims[2]); @@ -1564,19 +1777,19 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S { if(shouldCancel) { - return {}; + return accumulated; } const nonstd::span srcValues = inMemorySrc->createSpan(); nonstd::span dstValues = inMemoryDst->createSpan(); progressThrottle.reset((static_cast(nY * nZ) + 3) / 4, progressLabel); - detail::FilterFourRecursiveGaussianXRows(srcValues, dstValues, static_cast(nX), static_cast(nY * nZ), coeffs, shouldCancel, sendThreadSafeProgress); - return {}; + detail::FilterFourRecursiveGaussianXRows(srcValues, dstValues, static_cast(nX), static_cast(nY * nZ), coeffs, shouldCancel, sendThreadSafeProgress, options.CheckedSmoothing); + return accumulated; } if(options.useInMemoryFastPath && inMemorySrc != nullptr && inMemoryDst != nullptr && axis != 0) { if(shouldCancel) { - return {}; + return accumulated; } const nonstd::span srcValues = inMemorySrc->createSpan(); @@ -1625,7 +1838,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S const usize destination = base + i * stride; for(usize lane = 0; lane < 4; ++lane) { - dstValues[destination + lane] = static_cast(batchOuts[i * 4 + lane]); + dstValues[destination + lane] = detail::NarrowRecursiveGaussianValue(batchOuts[(i * 4) + lane], options.CheckedSmoothing); } } } @@ -1640,7 +1853,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S detail::FilterDataArray(tailOuts.data(), tailData.data(), lineLength, coeffs); for(usize i = 0; i < lineLength; ++i) { - dstValues[base + lane + i * stride] = static_cast(tailOuts[i]); + dstValues[base + lane + (i * stride)] = detail::NarrowRecursiveGaussianValue(tailOuts[i], options.CheckedSmoothing); } } } @@ -1651,7 +1864,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, batchesPerPlane * planeCount); parallelAlgorithm.execute(filterLineBatches); - return {}; + return accumulated; } // NOTE: the per-line IIR scratch (`data`/`outs`/`scratch`) is NOT allocated here. Each axis pass stages one @@ -1684,12 +1897,13 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S { if(shouldCancel) { - return {}; + return accumulated; } const usize planeOff = static_cast(z) * slice; - if(Result<> r = src.copyIntoBuffer(planeOff, nonstd::span(srcPlane.data(), slice)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), src.copyIntoBuffer(planeOff, nonstd::span(srcPlane.data(), slice))); + if(accumulated.invalid()) { - return r; + return accumulated; } // Parallel over rows [0, nY): each worker filters a disjoint set of contiguous nX-rows with its OWN // thread-local scratch, gathering from srcPlane and scattering into dstPlane at disjoint indices. Pure @@ -1709,20 +1923,21 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S detail::FilterDataArray(outs.data(), data.data(), static_cast(nX), coeffs); for(int64 i = 0; i < nX; ++i) { - dstPlane[rowOff + static_cast(i)] = static_cast(outs[static_cast(i)]); + dstPlane[rowOff + static_cast(i)] = detail::NarrowRecursiveGaussianValue(outs[static_cast(i)], options.CheckedSmoothing); } } }; ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, static_cast(nY)); parallelAlgorithm.execute(filterRows); - if(Result<> r = dst.copyFromBuffer(planeOff, nonstd::span(dstPlane.data(), slice)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), dst.copyFromBuffer(planeOff, nonstd::span(dstPlane.data(), slice))); + if(accumulated.invalid()) { - return r; + return accumulated; } reportProgress(static_cast(z + 1)); } - return {}; + return accumulated; } if(axis == 1) @@ -1734,12 +1949,13 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S { if(shouldCancel) { - return {}; + return accumulated; } const usize planeOff = static_cast(z) * slice; - if(Result<> r = src.copyIntoBuffer(planeOff, nonstd::span(srcPlane.data(), slice)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), src.copyIntoBuffer(planeOff, nonstd::span(srcPlane.data(), slice))); + if(accumulated.invalid()) { - return r; + return accumulated; } // Parallel over columns [0, nX): each worker filters a disjoint set of columns (stride nX, count nY) with // its OWN thread-local scratch, gathering from srcPlane and scattering into dstPlane at disjoint indices. @@ -1758,20 +1974,21 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S detail::FilterDataArray(outs.data(), data.data(), static_cast(nY), coeffs); for(int64 j = 0; j < nY; ++j) { - dstPlane[static_cast(j) * static_cast(nX) + x] = static_cast(outs[static_cast(j)]); + dstPlane[(static_cast(j) * static_cast(nX)) + x] = detail::NarrowRecursiveGaussianValue(outs[static_cast(j)], options.CheckedSmoothing); } } }; ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, static_cast(nX)); parallelAlgorithm.execute(filterColumns); - if(Result<> r = dst.copyFromBuffer(planeOff, nonstd::span(dstPlane.data(), slice)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), dst.copyFromBuffer(planeOff, nonstd::span(dstPlane.data(), slice))); + if(accumulated.invalid()) { - return r; + return accumulated; } reportProgress(static_cast(z + 1)); } - return {}; + return accumulated; } // Z pass (3D only): stage consecutive Y rows across every Z plane. A batch converts many small per-row HDF5 @@ -1808,7 +2025,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S { if(shouldCancel) { - return {}; + return accumulated; } const usize yCount = std::min(maxYRows, dims[1] - yBegin); const usize valuesPerPlaneBlock = yCount * dims[0]; @@ -1819,12 +2036,14 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S { if(shouldCancel) { - return {}; + return accumulated; } const usize blockOffset = (z * dims[1] + yBegin) * dims[0]; - if(Result<> r = src.copyIntoBuffer(blockOffset, nonstd::span(srcSlab.data() + z * valuesPerPlaneBlock, valuesPerPlaneBlock)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), + src.copyIntoBuffer(blockOffset, nonstd::span(srcSlab.data() + (z * valuesPerPlaneBlock), valuesPerPlaneBlock))); + if(accumulated.invalid()) { - return r; + return accumulated; } } // Adjacent X/Y lines are independent and occupy adjacent lanes in every staged Z plane. Workers touch only @@ -1864,7 +2083,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S const usize destination = z * valuesPerPlaneBlock + firstLine; for(usize lane = 0; lane < 4; ++lane) { - dstSlab[destination + lane] = static_cast(batchOuts[z * 4 + lane]); + dstSlab[destination + lane] = detail::NarrowRecursiveGaussianValue(batchOuts[(z * 4) + lane], options.CheckedSmoothing); } } } @@ -1879,7 +2098,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S detail::FilterDataArray(tailOuts.data(), tailData.data(), dims[2], coeffs); for(usize z = 0; z < dims[2]; ++z) { - dstSlab[z * valuesPerPlaneBlock + firstLine + lane] = static_cast(tailOuts[z]); + dstSlab[(z * valuesPerPlaneBlock) + firstLine + lane] = detail::NarrowRecursiveGaussianValue(tailOuts[z], options.CheckedSmoothing); } } } @@ -1890,23 +2109,25 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S parallelAlgorithm.execute(filterZLineBatches); if(shouldCancel) { - return {}; + return accumulated; } for(usize z = 0; z < dims[2]; ++z) { if(shouldCancel) { - return {}; + return accumulated; } const usize blockOffset = (z * dims[1] + yBegin) * dims[0]; - if(Result<> r = dst.copyFromBuffer(blockOffset, nonstd::span(dstSlab.data() + z * valuesPerPlaneBlock, valuesPerPlaneBlock)); r.invalid()) + accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), + dst.copyFromBuffer(blockOffset, nonstd::span(dstSlab.data() + (z * valuesPerPlaneBlock), valuesPerPlaneBlock))); + if(accumulated.invalid()) { - return r; + return accumulated; } } reportProgress(yBegin + yCount); } - return {}; + return accumulated; } // ---- Element-wise helpers over float32 stores. Streamed paths use the shared shape-aware pointwise planner so every @@ -2375,10 +2596,12 @@ inline Result<> SqrtStoreInPlace(AbstractDataStore& store, const std::a return {}; } -// dst[i] = static_cast(src[i]) — Smoothing final cast float32 -> output type (ITK CastImageFilter). +// Smoothing final conversion retains float32 pass values and checks only the terminal integer conversion. template Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataStore& dst, const std::atomic_bool& shouldCancel, const detail::PointwiseExecutionOptions& options = {}) { + detail::SmoothingGaussianConversionState conversionState; + Result<> accumulated; const usize total = src.getSize(); if(dst.getSize() != total) { @@ -2393,12 +2616,10 @@ Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataS ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, total); parallelAlgorithm.execute([&](const Range& range) { - detail::TransformRecursiveGaussianBatch(range.max() - range.min(), shouldCancel, [&](usize localIndex) { - const usize i = range.min() + localIndex; - dstValues[i] = static_cast(srcValues[i]); - }); + detail::ConvertSmoothingGaussianBuffer(srcValues.subspan(range.min(), range.max() - range.min()), dstValues.subspan(range.min(), range.max() - range.min()), range.min(), conversionState, + shouldCancel); }); - return {}; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } Result planResult = detail::MakeRecursiveGaussianPostprocessingPlan( total, sizeof(float32) + sizeof(T), options.targetScratchBytes, @@ -2415,23 +2636,25 @@ Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataS { if(shouldCancel) { - return {}; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } const usize n = std::min(batchValues, total - start); - if(Result<> r = src.copyIntoBuffer(start, nonstd::span(s.data(), n)); r.invalid()) + accumulated = MergeResults(std::move(accumulated), src.copyIntoBuffer(start, nonstd::span(s.data(), n))); + if(accumulated.invalid()) { - return r; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } - if(!detail::TransformRecursiveGaussianBatch(n, shouldCancel, [&](usize i) { d[i] = static_cast(s[i]); })) + if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(s.data(), n), nonstd::span(d.data(), n), start, conversionState, shouldCancel)) { - return {}; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } - if(Result<> r = dst.copyFromBuffer(start, nonstd::span(d.data(), n)); r.invalid()) + accumulated = MergeResults(std::move(accumulated), dst.copyFromBuffer(start, nonstd::span(d.data(), n))); + if(accumulated.invalid()) { - return r; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } start += n; } - return {}; + return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); } } // namespace nx::core::ImageProcessing From ca2383a79332106168d3f5837d07028f0c8ef105 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 00:31:27 -0400 Subject: [PATCH 17/98] BUG: Guard Connected Component provisional label capacity Default to an execution error before creating an ID above UINT32_MAX. Offer explicit legacy continuation with one immediate and retained warning while preserving uint32 labels, strict shared counting and existing allocation fallback. Check all creation sites and warning lifetime. Cover native boundary decisions, small zero/reuse models, exact label maps and failure paths; document migration, memory costs and unreliable legacy output. Signed-off-by: Jessica Marquis --- .../docs/ConnectedComponentImageFilter.md | 19 +- .../Filters/ConnectedComponentImageFilter.cpp | 17 +- .../Filters/ConnectedComponentImageFilter.hpp | 6 +- .../utils/ComponentImageFilterExecutors.cpp | 12 +- .../utils/ImageFilterExecutors.hpp | 7 +- .../test/ConnectedComponentEngineTest.cpp | 364 ++++++++++++++++++ .../ConnectedComponentImageFilterTest.cpp | 158 ++++++++ .../ConnectedComponentEngine.hpp | 238 +++++++++--- .../ImageProcessingOperationTypes.hpp | 7 + 9 files changed, 759 insertions(+), 69 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md b/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md index 5f0a3644fa..5e892791dc 100644 --- a/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md @@ -8,14 +8,25 @@ NXImageProcessing (Segmentation) ## Description -Labels the objects in the input image: non-zero input voxels are considered foreground, zero-valued voxels are considered background. Each distinct foreground object is assigned a unique label. Objects that are reached earlier by a raster-order (X fastest, then Y, then Z) scan receive a lower label, so labels start at 1 and are consecutive; background voxels are always labeled 0. +Labels the objects in the input image: nonzero input voxels are foreground, and zero-valued voxels are background. Below the provisional ID limit, each distinct foreground object gets a unique label. Objects reached earlier by a raster-order scan (X fastest, then Y, then Z) get a lower label. Labels start at 1 and are consecutive; background is 0. -The input array must be single-component (scalar) and may be **any integer type** (signed or unsigned). The output is a fixed **uint32** label image regardless of the input element type. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Connected Component Image Filter, and matches it exactly (byte-identical label values). +The input array must be single-component (scalar) and may use **any integer type** (signed or unsigned). The filter tests each native input value only as zero or nonzero. A large uint64 value is valid foreground. Output is always **uint32**, with 4 bytes per value; input values are not converted to output label values. Below the provisional ID limit, the filter preserves the legacy ITK label values. ### Fully Connected Set/Get whether the connected components are defined strictly by face connectivity or by face+edge+vertex connectivity. Default is FullyConnectedOff. For objects that are 1 pixel wide, use FullyConnectedOn. +### Provisional Label Limit Handling + +A **provisional run ID** identifies one uninterrupted foreground run along X before connected runs are joined. Several provisional runs can form one final component. For example, stripes joined by a bridge can use many run IDs but produce one component. The limit therefore applies to provisional runs, not to the voxel count or the final component count. + +- **Error (default):** execution accepts provisional ID 4,294,967,295 and stops before creating ID 4,294,967,296. The check occurs when a new run needs an ID, so a large image can fail after a long first pass. New pipelines and existing pipelines without an explicit choice use Error. +- **Warn and Continue (Legacy Behavior):** execution sends one warning immediately when it first exceeds the limit. Normal completion and later processing or I/O errors retain that warning. The application's general cancellation handler can replace a cancelled result and its retained diagnostics; the immediate warning has already been sent. This choice keeps the old uint32 wrap behavior: ID 4,294,967,296 becomes background zero, the next ID reuses label 1, and later IDs reuse earlier labels. Foreground can disappear and distinct components can merge, including components already joined to other runs. Segmentation, feature counts, component sizes, and downstream per-feature statistics can be incorrect. The scanline route can join zero-labelled foreground runs to other runs; the tiled route skips zero labels in its previous-row neighbor checks. Different routes can therefore give different incorrect results after overflow. + +To run an old pipeline with intentional overflow compatibility, explicitly select *Warn and Continue (Legacy Behavior)*. Use this choice only if you accept the scientific consequences. It does not add capacity, preserve valid label identities after overflow, or guarantee completion. Correct support for more provisional IDs requires a separate algorithm or internal representation change. Shared component-counting and composite filters retain Error; they do not inherit this compatibility choice. + +Both choices retain independent allocation, addressability, I/O, and cancellation handling. Memory or I/O failures can still stop the filter. The policy does not add an all-or-nothing guarantee for an unrelated late output-write failure. + ## Algorithm The filter scans the image in X-fastest order. It encodes each foreground run along X. A union-find table joins runs that have the selected connectivity. @@ -36,7 +47,9 @@ For true 2D data (`Z=1`), a checked scheduler uses at most 64 MiB for blocks. An The tiled first pass can use temporary records for previous-row labels. The final pass rereads bounded input tiles and writes bounded output tiles. -All routes create labels at the same raster run starts. They preserve the same union order and consecutive-label order. +Below the provisional ID limit, all routes create labels at the same raster run starts and preserve the same union and consecutive-label order. Explicit legacy overflow retains each route's existing zero/reuse behavior. + +The equivalence tables remain resident memory proportional to the number of provisional runs, even when image transfers use bounded buffers or disk storage. Three uint32 tables with 2^32 entries alone need at least 48 GiB, before line records, image buffers, and other state. Allocation can fail before the ID limit is reached. This policy does not make the union-find tables bounded in size. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp index dca53aad78..690d4fc016 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp @@ -2,14 +2,20 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/BoolParameter.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" #include "simplnx/Parameters/GeometrySelectionParameter.hpp" +#include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include +#include + using namespace nx::core; namespace nx::core @@ -43,6 +49,12 @@ Parameters ConnectedComponentImageFilter::parameters() const "Set/Get whether the connected components are defined strictly by face connectivity or by face+edge+vertex connectivity. Default is FullyConnectedOff. " "For objects that are 1 pixel wide, use FullyConnectedOn.", false)); + params.insert(std::make_unique(k_ProvisionalLabelLimitHandling_Key, "Provisional Label Limit Handling", + "Controls what happens if more than 4,294,967,295 provisional run IDs are needed. Error (default) stops before IDs wrap. " + "Warn and Continue (Legacy Behavior) keeps uint32 wrap to background zero and reused IDs. Foreground can disappear, components can merge, " + "and segmentation, counts, and downstream statistics can be incorrect. Choose Warn only if you intentionally accept these consequences. " + "It does not increase label capacity or prevent memory or I/O errors.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -58,7 +70,7 @@ Parameters ConnectedComponentImageFilter::parameters() const IFilter::VersionType ConnectedComponentImageFilter::parametersVersion() const { - return 1; + return 2; } IFilter::UniquePointer ConnectedComponentImageFilter::clone() const { @@ -91,7 +103,8 @@ Result<> ConnectedComponentImageFilter::executeImpl(DataStructure& dataStructure const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); auto fullyConnected = filterArgs.value(k_FullyConnected_Key); + const auto labelLimitHandling = static_cast(filterArgs.value(k_ProvisionalLabelLimitHandling_Key)); - return ImageProcessing::ExecuteConnectedComponentImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, fullyConnected, shouldCancel, messageHandler); + return ImageProcessing::ExecuteConnectedComponentImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, fullyConnected, shouldCancel, messageHandler, labelLimitHandling); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp index 392b960a91..48723fddc1 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp @@ -12,10 +12,11 @@ namespace nx::core * @brief ITK-free, out-of-core-capable connected-component labeling via a streaming scanline union-find. * * Labels the objects in a binary image: non-zero input voxels are foreground, zero-valued voxels are background. - * Each distinct foreground object is assigned a unique consecutive label (1, 2, 3, ...) in raster-scan order of + * Below the provisional ID limit, each foreground object gets a unique consecutive label (1, 2, 3, ...) in raster-scan order of * first appearance; background voxels are labeled 0. The input may be ANY integer scalar type; the output is a * FIXED uint32 label image regardless of the input element type. Matches the legacy ITK Connected Component Image - * Filter exactly (byte-identical label values). Out-of-core capable. + * Filter below the provisional uint32 ID limit. Error is the default at that limit. Explicit legacy continuation + * retains wrap to zero and reused IDs, so segmentation and downstream counts/statistics can be incorrect. */ class NXIMAGEPROCESSING_EXPORT ConnectedComponentImageFilter : public IFilter { @@ -33,6 +34,7 @@ class NXIMAGEPROCESSING_EXPORT ConnectedComponentImageFilter : public IFilter static constexpr StringLiteral k_InputImageDataPath_Key = "input_image_data_path"; static constexpr StringLiteral k_OutputImageArrayName_Key = "output_array_name"; static constexpr StringLiteral k_FullyConnected_Key = "fully_connected"; + static constexpr StringLiteral k_ProvisionalLabelLimitHandling_Key = "provisional_label_limit_handling_index"; std::string name() const override; std::string className() const override; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp index edb9d8794b..74b2b56885 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp @@ -39,7 +39,8 @@ namespace * @brief Dispatched execute body for the (ITK-free) ConnectedComponent filter: labels foreground (non-zero) voxels * into consecutive uint32 components via the streaming scanline union-find @ref LabelConnectedComponents * engine. Output is a FIXED uint32 label image, background 0. Input must be an integer scalar type (the - * caller's preflight enforces this via the IntegerOnly policy). + * caller's preflight enforces this via the IntegerOnly policy). Explicit legacy overflow can lose foreground + * or merge components through zero and reused IDs. */ struct ConnectedComponentExecuteFn { @@ -51,13 +52,16 @@ struct ConnectedComponentExecuteFn const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; + ConnectedComponentLabelLimitHandling LabelLimitHandling; + template Result<> operator()() const { const auto& inArray = dataStructure.getDataRefAs>(inputArrayPath); auto& outArray = dataStructure.getDataRefAs>(outputArrayPath); const auto pred = [](T v) { return v != T{}; }; // compile-time predicate (Pred deduced by LabelConnectedComponents) - return LabelConnectedComponents(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, pred, fullyConnected, shouldCancel, messageHandler); + return LabelConnectedComponents(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, pred, fullyConnected, shouldCancel, messageHandler, k_ConnectedComponent2DTargetBytes, + LabelLimitHandling); } }; @@ -867,13 +871,13 @@ struct ThresholdMaxCCExecuteFn // ----------------------------------------------------------------------------- Result<> ExecuteConnectedComponentImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, bool fullyConnected, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, ConnectedComponentLabelLimitHandling labelLimitHandling) { using TypeSetPolicy = IntegerOnly; const auto& inputArray = dataStructure.getDataRefAs(inputArrayPath); const DataType inputType = inputArray.getDataType(); const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); - detail::ConnectedComponentExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, imageGeom.getDimensions(), fullyConnected, shouldCancel, messageHandler}; + detail::ConnectedComponentExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, imageGeom.getDimensions(), fullyConnected, shouldCancel, messageHandler, labelLimitHandling}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp index 084bf1df94..2f1db5ddea 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp @@ -379,11 +379,12 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteApproximateSignedDistanceMapImageFilter * @brief Execute the (ITK-free) ConnectedComponent filter: labels foreground (non-zero) voxels of an integer scalar * image into consecutive uint32 components (background 0), via the streaming scanline union-find @ref * LabelConnectedComponents engine. The output is a FIXED uint32 label image, not the input element type. - * Reproduces legacy ITK's ConnectedComponentImageFilter label VALUES exactly (see the Parity model). The - * caller's preflight must have enforced an integer scalar input. + * Below the provisional uint32 capacity limit, preserves the legacy label values. Only this filter exposes + * legacy overflow continuation; shared consumers retain Error. The caller must enforce an integer scalar input. */ NXIMAGEPROCESSING_EXPORT Result<> ExecuteConnectedComponentImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, - bool fullyConnected, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler); + bool fullyConnected, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, + ConnectedComponentLabelLimitHandling labelLimitHandling = ConnectedComponentLabelLimitHandling::Error); /** * @brief Execute the (ITK-free) ZeroCrossing filter: marks the voxels closest to sign changes (zero crossings) among diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp index 8176e64d9a..584c17c726 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp @@ -1,20 +1,32 @@ #include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" +#include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Array.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/CacheMemoryBudgetManager.hpp" #include "simplnx/Utilities/ImageProcessing/SweepTemporaryStore.hpp" +#include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" #include "simplnx/Utilities/InMemoryTemporaryRecordStore.hpp" #include +#include +#include #include #include #include +#include #include +#include +#include +#include +#include #include using namespace nx::core; @@ -144,6 +156,116 @@ class FailOnSecondReadDataStore : public DataStore mutable usize m_ReadCount = 0; }; +enum class ConnectedComponentReadFaultType : uint8 +{ + None, + Error, + BadAllocOnce, + LengthError, + Cancel +}; + +// A resident synthetic with a reported endpoint type, for deterministic Result/fallback control only. +class ConnectedComponentFaultInputStore : public OutOfCoreReportingDataStore +{ +public: + ConnectedComponentFaultInputStore(const ShapeType& shape, ConnectedComponentReadFaultType fault, std::atomic_bool& shouldCancel) + : OutOfCoreReportingDataStore(shape, ShapeType{1}, 1) + , m_Fault(fault) + , m_ShouldCancel(shouldCancel) + { + } + ~ConnectedComponentFaultInputStore() noexcept override = default; + ConnectedComponentFaultInputStore(const ConnectedComponentFaultInputStore&) = delete; + ConnectedComponentFaultInputStore(ConnectedComponentFaultInputStore&&) = delete; + ConnectedComponentFaultInputStore& operator=(const ConnectedComponentFaultInputStore&) = delete; + ConnectedComponentFaultInputStore& operator=(ConnectedComponentFaultInputStore&&) = delete; + + Result<> copyIntoBuffer(usize startIndex, nonstd::span buffer) const override + { + ++m_Attempts; + if(m_Fault == ConnectedComponentReadFaultType::Error) + { + return MakeErrorResult(-8791, "Injected connected-component input failure."); + } + if(m_Fault == ConnectedComponentReadFaultType::BadAllocOnce && m_Attempts == 1) + { + throw std::bad_alloc{}; + } + if(m_Fault == ConnectedComponentReadFaultType::LengthError) + { + throw std::length_error("Injected connected-component addressability failure."); + } + auto result = OutOfCoreReportingDataStore::copyIntoBuffer(startIndex, buffer); + if(m_Fault == ConnectedComponentReadFaultType::Cancel) + { + m_ShouldCancel = true; + } + return result; + } + + usize attempts() const noexcept + { + return m_Attempts; + } + +private: + ConnectedComponentReadFaultType m_Fault; + std::atomic_bool& m_ShouldCancel; + mutable usize m_Attempts = 0; +}; + +class ConnectedComponentFaultOutputStore : public OutOfCoreReportingDataStore +{ +public: + using OutOfCoreReportingDataStore::OutOfCoreReportingDataStore; + ~ConnectedComponentFaultOutputStore() noexcept override = default; + ConnectedComponentFaultOutputStore(const ConnectedComponentFaultOutputStore&) = delete; + ConnectedComponentFaultOutputStore(ConnectedComponentFaultOutputStore&&) = delete; + ConnectedComponentFaultOutputStore& operator=(const ConnectedComponentFaultOutputStore&) = delete; + ConnectedComponentFaultOutputStore& operator=(ConnectedComponentFaultOutputStore&&) = delete; + + Result<> copyFromBuffer([[maybe_unused]] usize startIndex, [[maybe_unused]] nonstd::span buffer) override + { + return MakeErrorResult(-8792, "Injected connected-component output failure."); + } +}; + +// Supplies literal zero labels for reads of the first row, modelling a previous row with wrapped IDs. +// All actual native IDs and allocations stay small; this is not a capacity-sized forest execution. +class ConnectedComponentZeroPreviousRowStore : public DataStore +{ +public: + ConnectedComponentZeroPreviousRowStore() + : DataStore(ShapeType{1, 2, 3}, ShapeType{1}, 0U) + { + } + ~ConnectedComponentZeroPreviousRowStore() noexcept override = default; + ConnectedComponentZeroPreviousRowStore(const ConnectedComponentZeroPreviousRowStore&) = delete; + ConnectedComponentZeroPreviousRowStore(ConnectedComponentZeroPreviousRowStore&&) = delete; + ConnectedComponentZeroPreviousRowStore& operator=(const ConnectedComponentZeroPreviousRowStore&) = delete; + ConnectedComponentZeroPreviousRowStore& operator=(ConnectedComponentZeroPreviousRowStore&&) = delete; + + Result<> copyIntoBuffer(usize startIndex, nonstd::span buffer) const override + { + auto result = DataStore::copyIntoBuffer(startIndex, buffer); + if(result.valid() && startIndex < 3 && buffer.size() <= 3 - startIndex) + { + std::ranges::fill(buffer, uint32{0}); + ++m_ZeroRowReads; + } + return result; + } + + usize zeroRowReads() const noexcept + { + return m_ZeroRowReads; + } + +private: + mutable usize m_ZeroRowReads = 0; +}; + std::vector ConnectedComponent2DOracle(const std::vector& mask, usize dimX, usize dimY, bool fullyConnected) { std::vector labels(mask.size(), 0u); @@ -1005,3 +1127,245 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: bounded true-2-D cancell } } } + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: native provisional capacity decisions require no large forest", + "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) + { + SUCCEED("A native index on this platform cannot exceed uint32 capacity; no overflowing candidate was executed."); + return; + } + const std::array candidates{1, 4294967295ULL, 4294967296ULL, 4294967297ULL, 8589934592ULL, 8589934593ULL}; + const std::array legacyLabels{1, 4294967295U, 0, 1, 0, 1}; + usize warningMessages = 0; + const IFilter::MessageHandler handler{[&warningMessages](const IFilter::Message& message) { + if(message.type == IFilter::Message::Type::Warning) + { + ++warningMessages; + } + }}; + ImageProcessing::detail::ConnectedComponentLabelState strictState(ConnectedComponentLabelLimitHandling::Error, handler); + ImageProcessing::detail::ConnectedComponentLabelState legacyState(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); + for(usize index = 0; index < candidates.size(); ++index) + { + const usize candidate = static_cast(candidates.at(index)); + CAPTURE(candidate); + const auto strictResult = strictState.labelFor(candidate); + if(index < 2) + { + SIMPLNX_RESULT_REQUIRE_VALID(strictResult); + REQUIRE(strictResult.value() == legacyLabels.at(index)); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(strictResult); + REQUIRE(strictResult.errors().front().code == -8771); + REQUIRE(strictResult.errors().front().message.find(std::to_string(candidate)) != std::string::npos); + REQUIRE(strictResult.errors().front().message.find("Warn and Continue (Legacy Behavior)") != std::string::npos); + } + const auto legacyResult = legacyState.labelFor(candidate); + SIMPLNX_RESULT_REQUIRE_VALID(legacyResult); + REQUIRE(legacyResult.value() == legacyLabels.at(index)); + REQUIRE(warningMessages == (index < 2 ? 0 : 1)); // immediate, before finish(), and only once + } + REQUIRE(warningMessages == 1); + const auto strictFinished = strictState.finish({}); + SIMPLNX_RESULT_REQUIRE_VALID(strictFinished); + REQUIRE(strictFinished.warnings().empty()); + const auto legacyFinished = legacyState.finish({}); + SIMPLNX_RESULT_REQUIRE_VALID(legacyFinished); + REQUIRE(legacyFinished.warnings().size() == 1); + REQUIRE(legacyFinished.warnings().front().code == 8772); +} + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reuse consequence model", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + // Literal wrapped IDs in a tiny real uint32 union-find. This is not a 2^32-entry forest execution. + const IFilter::MessageHandler handler; + ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); + ImageProcessing::detail::CcUnionFind forest(state); + forest.parent = {0, 1, 2, 3}; + forest.link(2, 3); + const ImageProcessing::detail::CcLine current{{.start = 0, .length = 1, .label = 0}}; + const ImageProcessing::detail::CcLine previous{{.start = 0, .length = 1, .label = 2}}; + ImageProcessing::detail::CompareLines(current, previous, 0, forest); + REQUIRE(forest.find(2) == 0); + REQUIRE(forest.find(3) == 0); // an already-linked component is also absorbed into background + forest.link(1, 2); // reused ID 1 now joins the zero root as well + REQUIRE(forest.parent == std::vector{0, 0, 0, 2}); + const std::atomic_bool shouldCancel{false}; + const auto resolved = forest.createConsecutive(shouldCancel, {}); + REQUIRE(resolved.second == 0); + REQUIRE(resolved.first == std::vector{0, 0, 0, 0}); +} + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor consequence model preserves a foreground root", + "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + const bool fullyConnected = GENERATE(false, true); + DataStore inputStore(ShapeType{1, 2, 3}, ShapeType{1}, 0); + inputStore.setValue(1, 1); + inputStore.setValue(4, 1); + ConnectedComponentZeroPreviousRowStore provisionalStore; + ImageProcessing::detail::ConnectedComponentProvisionalStore provisional(provisionalStore); + const IFilter::MessageHandler handler; + ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); + ImageProcessing::detail::CcUnionFind forest(state); + const usize tileFixedBytes = 2 * sizeof(uint32); + const usize targetBytes = tileFixedBytes + sizeof(int32) + (2 * sizeof(uint32)); + const auto planResult = ImageProcessing::detail::CreateConnectedComponent2DPlan(3, 2, true, targetBytes); + SIMPLNX_RESULT_REQUIRE_VALID(planResult); + REQUIRE(planResult.value().useTiles); + REQUIRE(planResult.value().coreColumns == 1); + const std::atomic_bool shouldCancel{false}; + const auto predicate = [](int32 value) { return value != 0; }; + + // The actual tile pass must skip the controlled zero-valued previous-row neighbor. + const auto passResult = + ImageProcessing::detail::Pass1TwoDimensionalTiles(inputStore, provisional, SizeVec3{3, 2, 1}, predicate, fullyConnected, shouldCancel, forest, nullptr, planResult.value(), {}); + SIMPLNX_RESULT_REQUIRE_VALID(passResult); + REQUIRE(provisionalStore.zeroRowReads() == 3); + REQUIRE(forest.parent == std::vector{0, 1, 2}); + REQUIRE(forest.find(2) == 2); + const auto resolved = forest.createConsecutive(shouldCancel, {}); + REQUIRE(resolved.second == 2); + REQUIRE(resolved.first == std::vector{0, 1, 2}); + const std::array finalLabels{0, resolved.first.at(forest.find(1)), 0, 0, resolved.first.at(forest.find(2)), 0}; + REQUIRE(finalLabels == std::array{0, 1, 0, 0, 2, 0}); + + // The real scanline union on the corresponding literal IDs absorbs the current run into zero instead. + ImageProcessing::detail::CcUnionFind scanlineForest(state); + scanlineForest.parent = {0, 1, 2}; + const ImageProcessing::detail::CcLine current{{.start = 1, .length = 1, .label = 2}}; + const ImageProcessing::detail::CcLine previous{{.start = 1, .length = 1, .label = 0}}; + ImageProcessing::detail::CompareLines(current, previous, fullyConnected ? 1 : 0, scanlineForest); + REQUIRE(scanlineForest.parent == std::vector{0, 1, 0}); + REQUIRE(scanlineForest.find(2) == 0); + const auto scanlineResolved = scanlineForest.createConsecutive(shouldCancel, {}); + REQUIRE(scanlineResolved.second == 1); + const std::array scanlineFinalLabels{0, scanlineResolved.first.at(scanlineForest.find(1)), 0, 0, scanlineResolved.first.at(scanlineForest.find(2)), 0}; + REQUIRE(scanlineFinalLabels == std::array{0, 1, 0, 0, 0, 0}); + // This exercises the production route's zero-neighbor rule using a controlled scratch-row model, not real overflow. +} + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: capacity warning context retains independent delivered diagnostics", + "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) + { + SUCCEED("No native overflowing candidate is available on this platform."); + return; + } + const IFilter::MessageHandler handler; + ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); + const auto labelResult = state.labelFor(static_cast(4294967296ULL)); + SIMPLNX_RESULT_REQUIRE_VALID(labelResult); + auto delivered = MergeResults(MakeWarningVoidResult(8793, "Delivered child warning."), MakeErrorResult(-8794, "Delivered later transfer failure.")); + const auto result = state.finish(std::move(delivered)); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == -8794); + REQUIRE(result.warnings().size() == 2); + REQUIRE(result.warnings().at(0).code == 8793); + REQUIRE(result.warnings().at(1).code == 8772); +} + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: seeded warning context survives resident retry copy error and cancellation", + "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) + { + SUCCEED("No native overflowing candidate is available on this platform."); + return; + } + const auto fault = GENERATE(ConnectedComponentReadFaultType::None, ConnectedComponentReadFaultType::Error, ConnectedComponentReadFaultType::BadAllocOnce, ConnectedComponentReadFaultType::Cancel); + const bool failCopy = GENERATE(false, true); + const ScopedWorkingMemoryTuningOverride tuning(65536); + const SizeVec3 dims{5, 6, 7}; + auto allocationResult = ImageProcessing::detail::ReserveConnectedComponentResidentWorkingMemory(dims); + SIMPLNX_RESULT_REQUIRE_VALID(allocationResult); + REQUIRE(allocationResult.value().holdsCompleteState()); + allocationResult.value().reservation.shrinkTo(0); + std::atomic_bool shouldCancel{false}; + const ConnectedComponentFaultInputStore inputStore(ShapeType{7, 6, 5}, fault, shouldCancel); + ConnectedComponentFaultOutputStore failedOutputStore(ShapeType{7, 6, 5}, ShapeType{1}, 99U); + DataStore successfulOutputStore(ShapeType{7, 6, 5}, ShapeType{1}, 99U); + AbstractDataStore& outputStore = failCopy ? static_cast&>(failedOutputStore) : successfulOutputStore; + usize warnings = 0; + const IFilter::MessageHandler handler{[&warnings](const IFilter::Message& message) { warnings += static_cast(message.type == IFilter::Message::Type::Warning); }}; + ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); + // Seed only production warning context using a native metadata boundary; the actual image remains tiny. + const auto boundaryResult = state.labelFor(static_cast(4294967296ULL)); + SIMPLNX_RESULT_REQUIRE_VALID(boundaryResult); + const auto predicate = [](int32 value) { return value != 0; }; + const ImageProcessing::detail::BeginProgressPhaseCallback beginPhase = [](std::string_view, usize) {}; + const ImageProcessing::detail::ProgressCallback report = [](usize) {}; + auto result = ImageProcessing::detail::LabelConnectedComponentsImpl(inputStore, outputStore, dims, predicate, false, shouldCancel, ImageProcessing::detail::k_ConnectedComponent2DTargetBytes, + beginPhase, report, report, state); + result = state.finish(std::move(result)); + REQUIRE(warnings == 1); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 8772); + if(fault == ConnectedComponentReadFaultType::Cancel) + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel); + } + else if(fault == ConnectedComponentReadFaultType::Error || failCopy) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == (fault == ConnectedComponentReadFaultType::Error ? -8791 : -8792)); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + for(usize index = 0; index < outputStore.getSize(); ++index) + { + REQUIRE(outputStore.getValue(index) == 1); + } + } + REQUIRE(inputStore.attempts() == (fault == ConnectedComponentReadFaultType::BadAllocOnce ? dims[2] + 1 : 1)); +} + +TEST_CASE("ImageProcessing::ConnectedComponentEngine: allocation addressability and I/O failures remain errors under both policies", + "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +{ + UnitTest::LoadPlugins(); + const auto handling = GENERATE(ConnectedComponentLabelLimitHandling::Error, ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy); + const auto fault = GENERATE(ConnectedComponentReadFaultType::Error, ConnectedComponentReadFaultType::BadAllocOnce, ConnectedComponentReadFaultType::LengthError); + std::atomic_bool shouldCancel{false}; + const ConnectedComponentFaultInputStore inputStore(ShapeType{1, 2, 3}, fault, shouldCancel); + DataStore outputStore(ShapeType{1, 2, 3}, ShapeType{1}, 99U); + const auto predicate = [](int32 value) { return value != 0; }; + const auto labelResult = + LabelConnectedComponents(inputStore, outputStore, SizeVec3{3, 2, 1}, predicate, false, shouldCancel, {}, ImageProcessing::detail::k_ConnectedComponent2DTargetBytes, handling); + SIMPLNX_RESULT_REQUIRE_INVALID(labelResult); + int32 labelCode = -8791; + int32 countCode = -8791; + if(fault == ConnectedComponentReadFaultType::BadAllocOnce) + { + labelCode = -8773; + countCode = -8774; + } + else if(fault == ConnectedComponentReadFaultType::LengthError) + { + labelCode = -8775; + countCode = -8776; + } + REQUIRE(labelResult.errors().front().code == labelCode); + REQUIRE(labelResult.warnings().empty()); + for(usize index = 0; index < outputStore.getSize(); ++index) + { + REQUIRE(outputStore.getValue(index) == 99); + } + const ConnectedComponentFaultInputStore countInputStore(ShapeType{1, 2, 3}, fault, shouldCancel); + const auto countResult = CountConnectedComponents(countInputStore, SizeVec3{3, 2, 1}, predicate, false, 1, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(countResult); + REQUIRE(countResult.errors().front().code == countCode); +} diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp index 51000f195d..41a6ebb064 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp @@ -3,15 +3,31 @@ #include "NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Filter/Parameters.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include +#include +#include +#include +#include +#include #include #include @@ -77,6 +93,148 @@ std::vector MakeBlobField(usize dimX, usize dimY, usize dimZ) } } // namespace +TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: provisional label limit policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +{ + UnitTest::LoadPlugins(); + const ConnectedComponentImageFilter filter; + REQUIRE(filter.parametersVersion() == 2); + const Parameters parameters = filter.parameters(); + const std::string policyKey = "provisional_label_limit_handling_index"; + + // Check the public schema before any lookup. This is a safe baseline failure with no label allocation or execution. + REQUIRE(parameters.contains(policyKey)); + const auto* policyPtr = dynamic_cast(parameters.at(policyKey).get()); + REQUIRE(policyPtr != nullptr); + REQUIRE(policyPtr->humanName() == "Provisional Label Limit Handling"); + REQUIRE(policyPtr->choices() == ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"}); + REQUIRE(policyPtr->defaultIndex() == 0); + const Arguments defaultArguments = filter.getDefaultArguments(); + REQUIRE(defaultArguments.value(policyKey) == 0); + + // Check required risk content without fixing the complete tooltip prose. + std::string tooltip = policyPtr->helpText(); + REQUIRE_FALSE(tooltip.empty()); + std::ranges::transform(tooltip, tooltip.begin(), [](unsigned char character) { return static_cast(std::tolower(character)); }); + REQUIRE((tooltip.find("4,294,967,295") != std::string::npos || tooltip.find("4294967295") != std::string::npos)); + for(const auto* riskTermPtr : {"default", "provisional", "wrap", "background", "reuse", "incorrect", "statistics"}) + { + INFO(riskTermPtr); + REQUIRE(tooltip.find(riskTermPtr) != std::string::npos); + } +} + +TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: both policies preserve literal connectivity labels and uint32 shape", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + const bool fullyConnected = GENERATE(false, true); + const bool threeDimensional = GENERATE(false, true); + CAPTURE(handling, fullyConnected, threeDimensional); + // Opposite corners touch only with full connectivity in both 2D and 3D. Native uint64 max is just foreground. + const usize dimZ = threeDimensional ? 2 : 1; + std::vector input(4 * dimZ, 0); + input.at(0) = std::numeric_limits::max(); + input.at(input.size() - 1) = 1; + std::vector expected(input.size(), 0); + expected.at(0) = 1; + expected.at(expected.size() - 1) = fullyConnected ? 1 : 2; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 2, 2, dimZ, input); + const ConnectedComponentImageFilter filter; + Arguments arguments; + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_FullyConnected_Key, std::make_any(fullyConnected)); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_ProvisionalLabelLimitHandling_Key, std::make_any(handling)); + const auto preflightResult = filter.preflight(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::uint32); + REQUIRE(actionPtr->dims() == ShapeType{dimZ, 2, 2}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + const auto executeResult = filter.execute(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + REQUIRE(executeResult.result.warnings().empty()); + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* arrayPtr = nullptr; + REQUIRE_NOTHROW(arrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(arrayPtr->getDataType() == DataType::uint32); + REQUIRE(arrayPtr->getTupleShape() == ShapeType{dimZ, 2, 2}); + REQUIRE(arrayPtr->getComponentShape() == ShapeType{1}); + const UInt32Array* labelsPtr = nullptr; + REQUIRE_NOTHROW(labelsPtr = &dataStructure.getDataRefAs(outputPath)); + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(labelsPtr->getDataStoreRef().getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: many provisional runs can form one exact component", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + const std::vector input{1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1}; + const std::vector expected{1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1}; + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 5, 3, 1, input); + Arguments arguments; + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_ProvisionalLabelLimitHandling_Key, std::make_any(handling)); + const ConnectedComponentImageFilter filter; + const auto executeResult = filter.execute(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + REQUIRE(executeResult.result.warnings().empty()); + const IDataArray* arrayPtr = nullptr; + REQUIRE_NOTHROW(arrayPtr = &dataStructure.getDataRefAs(inputPath.replaceName("Output"))); + REQUIRE(arrayPtr->getDataType() == DataType::uint32); + REQUIRE(arrayPtr->getTupleShape() == ShapeType{1, 3, 5}); + REQUIRE(arrayPtr->getComponentShape() == ShapeType{1}); + const UInt32Array* labelsPtr = nullptr; + REQUIRE_NOTHROW(labelsPtr = &dataStructure.getDataRefAs(inputPath.replaceName("Output"))); + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(labelsPtr->getDataStoreRef().getValue(index) == expected.at(index)); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: execution cancellation keeps the filter cancellation result", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 2, 1, std::vector(6, 1)); + Arguments arguments; + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(ConnectedComponentImageFilter::k_ProvisionalLabelLimitHandling_Key, std::make_any(handling)); + std::atomic_bool shouldCancel{false}; + const IFilter::MessageHandler handler{[&shouldCancel](const IFilter::Message& message) { + if(message.type == IFilter::Message::Type::Info && message.message == "Labeling Connected Components") + { + shouldCancel = true; + } + }}; + const ConnectedComponentImageFilter filter; + const auto result = filter.execute(dataStructure, arguments, nullptr, handler, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(shouldCancel); + REQUIRE(result.result.errors().size() == 1); + REQUIRE(result.result.errors().front().code == -1); + REQUIRE(result.result.warnings().empty()); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a non-integer (float32) input is // rejected (IntegerOnly); a valid integer scalar input produces a FIXED uint32 output array (AlwaysUInt32). diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index 16b410c653..a30ef4d83b 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -7,6 +7,7 @@ #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" // CreateDataStoreWithFormat (fallback uint32 provisional scratch) +#include "simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp" #include "simplnx/Utilities/ImageProcessing/SweepTemporaryStore.hpp" #include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" #include "simplnx/Utilities/ParallelDataAlgorithm.hpp" @@ -24,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -438,15 +440,85 @@ class ConnectedComponentProvisionalStore SweepTemporaryStore* m_FixedRecordStore = nullptr; }; -// Union-find over provisional labels (1-based; index 0 unused). union-by-smaller-root, matching ITK ScanlineFilterCommon. +/** + * @brief Checks native provisional IDs and owns one warning across recursive resident attempts and retries. + * The decision takes the actual prospective parent-table index. It does not impose a voxel or component limit. + */ +class ConnectedComponentLabelState +{ +public: + ConnectedComponentLabelState(ConnectedComponentLabelLimitHandling handling, const IFilter::MessageHandler& messageHandler) + : m_Handling(handling) + , m_MessageHandler(messageHandler) + { + } + ~ConnectedComponentLabelState() noexcept = default; + ConnectedComponentLabelState(const ConnectedComponentLabelState&) = delete; + ConnectedComponentLabelState(ConnectedComponentLabelState&&) = delete; + ConnectedComponentLabelState& operator=(const ConnectedComponentLabelState&) = delete; + ConnectedComponentLabelState& operator=(ConnectedComponentLabelState&&) = delete; + + Result labelFor(usize candidate) + { + if(candidate > std::numeric_limits::max()) + { + if(m_Handling == ConnectedComponentLabelLimitHandling::Error) + { + return MakeErrorResult( + -8771, fmt::format("Connected-component provisional run ID {} exceeds the uint32 limit of 4,294,967,295. Execution stopped before this ID was created. " + "Reduce the number of foreground runs. In Connected Component only, select 'Warn and Continue (Legacy Behavior)' under 'Provisional Label Limit Handling' " + "only if you intentionally accept lost foreground, merged components, and incorrect counts and statistics.", + candidate)); + } + if(!m_WarningIssued) + { + m_Warnings = MakeWarningVoidResult(8772, fmt::format("Connected-component provisional run ID {} exceeds 4,294,967,295. 'Warn and Continue (Legacy Behavior)' retains uint32 wrap to " + "background zero and reused IDs. Foreground can disappear, components can merge, and counts and downstream statistics can be incorrect. " + "Execution routes can give different incorrect results. Memory and I/O failures still stop execution.", + candidate)); + m_WarningIssued = true; + m_MessageHandler.sendWarningMessage(m_Warnings.warnings().front().message); + } + } + return {static_cast(candidate)}; + } + + // Called once by the outer execution wrapper, including after cancellation, retry, or an independent error. + Result<> finish(Result<> result) + { + return MergeResults(std::move(result), std::move(m_Warnings)); + } + +private: + ConnectedComponentLabelLimitHandling m_Handling; + const IFilter::MessageHandler& m_MessageHandler; + bool m_WarningIssued = false; + Result<> m_Warnings; +}; + +// Union-find over uint32 provisional labels. Zero is reserved below capacity; explicit legacy overflow can union it. struct CcUnionFind { - std::vector parent{0u}; // parent[0] unused; grows as labels are created - uint32 makeLabel() // create a new provisional label = its own root + explicit CcUnionFind(ConnectedComponentLabelState& labelState) + : m_LabelState(labelState) { - const uint32 l = static_cast(parent.size()); - parent.push_back(l); - return l; + } + ~CcUnionFind() noexcept = default; + CcUnionFind(const CcUnionFind&) = delete; + CcUnionFind(CcUnionFind&&) = delete; + CcUnionFind& operator=(const CcUnionFind&) = delete; + CcUnionFind& operator=(CcUnionFind&&) = delete; + + std::vector parent{0u}; // zero is background; explicit legacy wrap can link foreground to it + Result makeLabel() // check the actual next table index before conversion, append, or assignment + { + auto labelResult = m_LabelState.labelFor(parent.size()); + if(labelResult.invalid()) + { + return labelResult; + } + parent.push_back(labelResult.value()); + return labelResult; } uint32 find(uint32 l) const noexcept // LookupSet: follow parents to the root { @@ -500,6 +572,9 @@ struct CcUnionFind } return {consecutive, count}; } + +private: + ConnectedComponentLabelState& m_LabelState; }; // Encode one gathered x-row into unlabeled foreground runs. `Pred` is a compile-time predicate functor so the @@ -529,13 +604,19 @@ inline void EncodeLineGeometryInto(nonstd::span row, int64 nX, const Pr // Encode one gathered x-row and assign each run a fresh provisional label. template -inline void EncodeLineInto(nonstd::span row, int64 nX, const Pred& pred, CcUnionFind& uf, CcLine& line) +inline Result<> EncodeLineInto(nonstd::span row, int64 nX, const Pred& pred, CcUnionFind& uf, CcLine& line) { EncodeLineGeometryInto(row, nX, pred, line); for(CcRun& run : line) { - run.label = uf.makeLabel(); + auto labelResult = uf.makeLabel(); + if(labelResult.invalid()) + { + return ConvertResult(std::move(labelResult)); + } + run.label = labelResult.value(); } + return {}; } // CompareLines: union overlapping runs between the current line and an already-scanned neighbor line. `offset` = 0 for @@ -710,7 +791,12 @@ Result<> Pass1ThreeDimensional(const AbstractDataStore& inStore, ConnectedCom CcLine& line = curPlane[static_cast(y)]; for(CcRun& run : line) { - run.label = uf.makeLabel(); + auto labelResult = uf.makeLabel(); + if(labelResult.invalid()) + { + return ConvertResult(std::move(labelResult)); + } + run.label = labelResult.value(); } // ---- union against already-scanned neighbor lines ---- @@ -1088,7 +1174,10 @@ Result<> Pass1TwoDimensionalBlocks(const AbstractDataStore& inStore, Connecte for(usize localRow = 0; localRow < rowCount; ++localRow) { const usize rowOffset = localRow * dimX; - EncodeLineInto(nonstd::span(inputBlock.data() + rowOffset, dimX), signedDimX, pred, uf, currentLine); + if(auto result = EncodeLineInto(nonstd::span(inputBlock.data() + rowOffset, dimX), signedDimX, pred, uf, currentLine); result.invalid()) + { + return result; + } if(rowBegin + localRow > 0) { CompareLines(currentLine, previousLine, lineOffset, uf); @@ -1185,7 +1274,12 @@ Result<> Pass1TwoDimensionalTiles(const AbstractDataStore& inStore, Connected } if(!runContinues) { - currentRunLabel = uf.makeLabel(); + auto labelResult = uf.makeLabel(); + if(labelResult.invalid()) + { + return ConvertResult(std::move(labelResult)); + } + currentRunLabel = labelResult.value(); runContinues = true; if(sizeByLabel != nullptr && sizeByLabel->size() <= currentRunLabel) { @@ -1283,11 +1377,11 @@ namespace detail { // Full labeling writes consecutive uint32 labels and reserves zero for the background. Bounded OOC routes replay the // input after union resolution, so they do not store a complete provisional-label volume. An extreme 2D tile route can -// use temporary records for previous-row labels. Raster-order label creation and union operations remain byte-exact. +// use temporary records for previous-row labels. Legacy overflow deliberately retains route-specific zero handling. template Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, const Pred& pred, bool fullyConnected, const std::atomic_bool& shouldCancel, usize target2DBytes, const BeginProgressPhaseCallback& beginProgressPhase, const ProgressCallback& reportCount, - const ProgressCallback& reportPercent) + const ProgressCallback& reportPercent, ConnectedComponentLabelState& labelState) { usize sliceValues = 0; usize vol = 0; @@ -1324,17 +1418,18 @@ Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, Abstr { return result; } - if(Result<> result = LabelConnectedComponentsImpl(residentInput, residentOutput, dims, pred, fullyConnected, shouldCancel, target2DBytes, beginProgressPhase, reportCount, reportPercent); - result.invalid()) + Result<> residentResult = + LabelConnectedComponentsImpl(residentInput, residentOutput, dims, pred, fullyConnected, shouldCancel, target2DBytes, beginProgressPhase, reportCount, reportPercent, labelState); + if(residentResult.invalid()) { - return result; + return residentResult; } if(shouldCancel) { - return {}; + return residentResult; } const auto outputSpan = residentOutput.createSpan(); - return outStore.copyFromBuffer(0, nonstd::span(outputSpan.data(), outputSpan.size())); + return MergeResults(std::move(residentResult), outStore.copyFromBuffer(0, nonstd::span(outputSpan.data(), outputSpan.size()))); } catch(const std::bad_alloc&) { // Release the full-state reservation before trying the resident-provisional and bounded tiers. @@ -1370,7 +1465,7 @@ Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, Abstr provisional = std::make_unique(*provisionalDataStore); } - detail::CcUnionFind uf; + detail::CcUnionFind uf(labelState); beginProgressPhase("Labeling Connected Components", nZ == 1 ? vol : static_cast(nZ)); Result<> pass1Result = nZ == 1 ? detail::Pass1TwoDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, uf, nullptr, target2DBytes, reportPercent) : detail::Pass1ThreeDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, uf, nullptr, reportCount); @@ -1483,9 +1578,16 @@ Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, Abstr } // namespace detail +/** + * @brief Labels native foreground runs with fixed uint32 IDs and strict capacity checking by default. + * @param labelLimitHandling Only an explicit legacy choice permits zero/reused IDs after uint32 overflow. + * The immediate capacity warning is retained once in the engine Result on success, cancellation, or later failure. + * Resident retries share its state. Cancellation retains the existing valid engine Result convention. + */ template Result<> LabelConnectedComponents(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, const Pred& pred, bool fullyConnected, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize target2DBytes = detail::k_ConnectedComponent2DTargetBytes) + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize target2DBytes = detail::k_ConnectedComponent2DTargetBytes, + ConnectedComponentLabelLimitHandling labelLimitHandling = ConnectedComponentLabelLimitHandling::Error) { ThrottledMessageHandler progressThrottle(messageHandler); std::mutex progressMutex; @@ -1502,55 +1604,81 @@ Result<> LabelConnectedComponents(const AbstractDataStore& inStore, AbstractD const std::lock_guard guard(progressMutex); progressThrottle.updatePercent(completed); }; - return detail::LabelConnectedComponentsImpl(inStore, outStore, dims, pred, fullyConnected, shouldCancel, target2DBytes, beginProgressPhase, reportCount, reportPercent); + detail::ConnectedComponentLabelState labelState(labelLimitHandling, messageHandler); + try + { + return labelState.finish( + detail::LabelConnectedComponentsImpl(inStore, outStore, dims, pred, fullyConnected, shouldCancel, target2DBytes, beginProgressPhase, reportCount, reportPercent, labelState)); + } catch(const std::bad_alloc&) + { + return labelState.finish(MakeErrorResult(-8773, "Connected-component labeling could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory. " + "Legacy label continuation does not waive memory failures.")); + } catch(const std::length_error&) + { + return labelState.finish(MakeErrorResult(-8775, "Connected-component labeling exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs. " + "Legacy label continuation does not increase storage capacity.")); + } } // Count-only: number of components with size >= minSize. Pass 1 + resolve + per-root size tally, no output written. +// Provisional capacity handling is always strict; composite consumers do not inherit legacy continuation. // Returns Result (rather than a bare uint32) so a Pass1 I/O failure is distinguishable from a legitimate // "0 components" count. template Result CountConnectedComponents(const AbstractDataStore& inStore, const SizeVec3& dims, const Pred& pred, bool fullyConnected, uint64 minSize, const std::atomic_bool& shouldCancel, usize target2DBytes = detail::k_ConnectedComponent2DTargetBytes) { - usize sliceValues = 0; - usize volumeValues = 0; - if(Result<> validation = detail::ValidateConnectedComponentVolume(inStore, dims, sliceValues, volumeValues); validation.invalid()) - { - return ConvertResultTo(std::move(validation), uint32{0}); - } - if(volumeValues == 0) - { - return Result{0u}; - } - detail::CcUnionFind uf; - std::vector sizeByLabel; // sizeByLabel[provisional label] = total run length for that (pre-union) label - Result<> pass1Result = dims[2] == 1 ? detail::Pass1TwoDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, target2DBytes, detail::ProgressCallback{}) : - detail::Pass1ThreeDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, detail::ProgressCallback{}); - if(pass1Result.invalid()) - { - return ConvertResultTo(std::move(pass1Result), uint32{0}); - } - if(shouldCancel) + try { - return Result{0u}; - } + usize sliceValues = 0; + usize volumeValues = 0; + if(Result<> validation = detail::ValidateConnectedComponentVolume(inStore, dims, sliceValues, volumeValues); validation.invalid()) + { + return ConvertResultTo(std::move(validation), uint32{0}); + } + if(volumeValues == 0) + { + return Result{0u}; + } + const IFilter::MessageHandler messageHandler; + detail::ConnectedComponentLabelState labelState(ConnectedComponentLabelLimitHandling::Error, messageHandler); + detail::CcUnionFind uf(labelState); + std::vector sizeByLabel; // sizeByLabel[provisional label] = total run length for that (pre-union) label + Result<> pass1Result = dims[2] == 1 ? + detail::Pass1TwoDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, target2DBytes, detail::ProgressCallback{}) : + detail::Pass1ThreeDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, detail::ProgressCallback{}); + if(pass1Result.invalid()) + { + return ConvertResultTo(std::move(pass1Result), uint32{0}); + } + if(shouldCancel) + { + return Result{0u}; + } - // Resolve per-root sizes now that the union-find is final (roots can still change during Pass1, so this can only be - // done AFTER Pass1 completes -- summing by raw provisional label first, as Pass1 does, is what makes that safe). - std::vector rootSize(uf.parent.size(), 0u); - for(usize label = 1; label < sizeByLabel.size(); ++label) - { - rootSize[uf.find(static_cast(label))] += sizeByLabel[label]; - } + // Resolve per-root sizes now that the union-find is final (roots can still change during Pass1, so this can only be + // done AFTER Pass1 completes -- summing by raw provisional label first, as Pass1 does, is what makes that safe). + std::vector rootSize(uf.parent.size(), 0u); + for(usize label = 1; label < sizeByLabel.size(); ++label) + { + rootSize[uf.find(static_cast(label))] += sizeByLabel[label]; + } - uint32 numComponents = 0; - for(usize i = 1; i < uf.parent.size(); ++i) - { - if(uf.parent[i] == i && rootSize[i] >= minSize) // i is a root (one entry per component) + uint32 numComponents = 0; + for(usize i = 1; i < uf.parent.size(); ++i) { - ++numComponents; + if(uf.parent[i] == i && rootSize[i] >= minSize) // i is a root (one entry per component) + { + ++numComponents; + } } + return Result{numComponents}; + } catch(const std::bad_alloc&) + { + return MakeErrorResult(-8774, "Connected-component counting could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory."); + } catch(const std::length_error&) + { + return MakeErrorResult(-8776, "Connected-component counting exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs."); } - return Result{numComponents}; } } // namespace nx::core::ImageProcessing diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp index 6c132a13bf..0101d0f2ab 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp @@ -2,6 +2,13 @@ namespace nx::core::ImageProcessing { +/** @brief Only Connected Component exposes the explicit legacy wrap policy. Other callers keep Error. */ +enum class ConnectedComponentLabelLimitHandling : uint8 +{ + Error = 0, + WarnAndContinueLegacy = 1 +}; + /** * @brief The grayscale morphology operation to apply through a flat structuring element. * From 28b0d21d6512e92ba7a62367378575ba55792d8c Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 01:01:29 -0400 Subject: [PATCH 18/98] BUG: Guard watershed marker ID conversion Add Error and legacy warning choices before marker flooding. Preserve exact native diagnostics, marker output types, bounded copy routes and delivered phase warnings. Add independent native seed, transfer-failure and disk-route checks, and document migration and downstream effects. Validated paired full Windows Release builds, 28 InCore and 33 OOC cases, plus 17/19 related watershed checks. Source review and clang-format 19.1.1 pass. Configured clang-tidy remains nonpassing; existing/framework diagnostics and four nonblocking style findings are recorded for final review. Debug and cross-platform checks were not run. Signed-off-by: Jessica Marquis --- ...ologicalWatershedFromMarkersImageFilter.md | 42 +- ...logicalWatershedFromMarkersImageFilter.cpp | 15 +- ...logicalWatershedFromMarkersImageFilter.hpp | 1 + .../utils/ImageFilterExecutorDetail.hpp | 238 ++++++ .../utils/ImageFilterExecutors.hpp | 20 +- .../utils/WatershedImageFilterExecutors.cpp | 177 +---- ...calWatershedFromMarkersImageFilterTest.cpp | 692 ++++++++++++++++++ .../WatershedFromMarkersEngine.hpp | 6 +- 8 files changed, 1013 insertions(+), 178 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md b/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md index 169e7d6e63..9bb22752e2 100644 --- a/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md @@ -8,28 +8,50 @@ NXImageProcessing (Watersheds / Segmentation) ## Description -Floods a grayscale image from a set of user-supplied **marker** (seed) labels, growing each marked region outward over ascending gray levels until neighboring regions meet. The result is a label image in which every voxel is assigned the label of the marker whose basin claimed it. This is the marker-controlled watershed: unlike the parameter-free morphological watershed, the number and identity of the output regions are determined entirely by the markers, so it does not over-segment. +Floods a grayscale image from user-supplied **markers**, which are integer seed labels. Each marked region grows over ascending gray levels until regions meet. The output labels identify the resulting basins. The markers control the initial regions; select them to represent the regions you want to separate. Marker IDs are dimensionless identities, not gray levels or array indexes. Two flooding variants are provided via **Mark Watershed Line**: -- **On (Meyer's algorithm, the default):** voxels equidistant between two different marker labels are left as a *watershed line* (label 0), producing a thin ridge of background separating the basins. -- **Off (Beucher's algorithm):** every voxel is assigned to a basin (no separating line); this is also slightly faster. +- **On (Meyer's algorithm, the default):** watershed-line voxels where distinct basins meet are left as label 0, separating the basins. +- **Off (Beucher's algorithm):** reached voxels are assigned to a basin without a separating line. An image with no surviving nonzero seed remains unmarked. **Fully Connected** selects the neighborhood used for flooding: face connectivity (6-connected in 3D, 4-connected in 2D) when off, or face+edge+vertex connectivity (26-connected in 3D, 8-connected in 2D) when on. -### Required Inputs +### Input Guidance - **Input Cell Data (grayscale):** a single-component scalar image of any numeric type. The flood ascends this image's gray levels. - **Marker Cell Data:** a single-component **integer** scalar image with the same number of tuples as the grayscale input. Nonzero voxels are markers/seeds; a value of 0 means "unmarked". Voxels sharing the same nonzero label belong to the same seed region, so two disjoint blobs with the same label flood into one basin. -### Created Outputs +### Output Interpretation -- A label image with the **same element type as the marker image**. Basin labels are copied from the markers; watershed-line voxels (when Mark Watershed Line is on) are 0. +- A label image with the **same element type as the marker image**, the grayscale input's tuple and scalar component shapes, and the grayscale input's storage format. Watershed-line voxels (when Mark Watershed Line is on) are 0. Warn mode can change the original marker identities as described below. -### Notes / Limitations +### Marker ID Conversion Handling -- The output reproduces the legacy ITK `MorphologicalWatershedFromMarkersImageFilter` exactly, including the label *values*. -- Internally the flood runs in `uint32`. A marker label of `0xFFFFFFFF` is valid and is handled as a normal in-bounds marker. Labels stored in a wider integer type are narrowed to 32 bits during the flood. +The flood uses internal `uint32` labels. A **lossless round trip** means a native marker ID has the same value after conversion to `uint32` and back to its native type. + +- All `int8`, `uint8`, `int16`, `uint16`, `int32`, and `uint32` values round-trip, including negative signed values and their minimum values. +- For `int64` and `uint64`, the lossless direct range is **0 through 4294967295, inclusive**. Negative `int64` IDs do not round-trip. +- Zero means unmarked. `UINT32_MAX`, or 4294967295, is a valid in-bounds seed. It is not a reserved marker identity. + +**Marker ID Conversion Handling** has exactly two choices: + +- **Error (default):** check values during the existing bounded marker-copy pass. If an ID changes, stop before writing its affected scratch chunk and before flooding starts. The error identifies the marker array, flat index, native ID, and restored value. Previously copied scratch chunks or allocated output storage can remain; this is not a general rollback guarantee. +- **Warn and Continue (Legacy Behavior):** keep the old conversion, flood, and conversion back. If any marker changes, return one warning with the number of changed occurrences observed and the first changed marker's context. Valid round-tripping markers do not warn. This choice does not preserve unsupported native IDs or suppress independent allocation and I/O errors. + +For example, in Warn mode: + +| Native marker | Internal and restored value | Consequence | +| --- | --- | --- | +| `uint64` 4294967296 | 0 | The seed disappears into background. | +| `uint64` 4294967297 | 1 | It can merge with an existing seed 1. | +| `int64` -1 | 4294967295 | The seed is renamed in the `int64` output. | + +Seed loss, merging, or renaming can change basin boundaries and region sizes. Downstream feature counts and measurements can be incorrect for the original markers. Choose Warn only when you intentionally accept these consequences. + +Existing pipelines that omit the new choice use Error. To retain old lossy behavior, explicitly select Warn. Converting the marker array first preserves IDs only when every value fits the destination type; arbitrary narrowing is not a lossless repair. Lossless remapping is future work and is not an available choice. + +Copy, flood, and restore Results retain the conversion warning on normal completion and delivered later errors or cancellation. The generic filter wrapper can replace a successful cancelled execution Result and lose its warnings; warning retention at that inherited cancellation boundary is not guaranteed. Cancellation does not make a partial output a completed segmentation. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Morphological Watershed From Markers Image Filter. @@ -37,6 +59,8 @@ This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Morp Resident input uses an in-memory hierarchical queue. The filter processes the lowest gray level first. It uses FIFO order for voxels at the same level. +The initial marker-copy pass checks the selected conversion policy using the same native values it copies. Transfers use at most 65536 marker values per chunk, with one native buffer and one `uint32` buffer. The check adds constant work per value and constant diagnostic state. It adds no extra image scan, remapping table, or resident copy of the full native marker volume. Internal marker/output scratch stores remain `uint32` and follow the grayscale storage format. + An out-of-core endpoint keeps the same processing order with bounded, file-backed state. The engine gets an adaptive working-memory grant from the shared cache budget. It uses only the useful part of that grant and keeps a disk-backed fallback. If a complete state fits, the engine can keep it resident. Otherwise, it keeps fixed record pages in memory. Large partial 3-D state uses 32 x 32 x 8 voxel tiles so face neighbors usually share one page. Eight- and sixteen-bit gray types use ordered gray-level buckets. Each bucket stores 64-bit voxel indexes in file-backed FIFO blocks. Wider integers and floating-point types use a file-backed minimum heap keyed by flood level and insertion order. Marker values are streamed during initialization and are not kept in each voxel record. Input and output transfers use bounded bulk I/O. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp index d2f3ea1d37..68e9ba6932 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp @@ -6,6 +6,7 @@ #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/BoolParameter.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" #include "simplnx/Parameters/GeometrySelectionParameter.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" @@ -14,6 +15,7 @@ #include +#include #include using namespace nx::core; @@ -52,6 +54,14 @@ Parameters MorphologicalWatershedFromMarkersImageFilter::parameters() const "Set/Get whether the connected components are defined strictly by face connectivity or by face+edge+vertex connectivity. Default is FullyConnectedOff. " "For objects that are 1 pixel wide, use FullyConnectedOn.", false)); + params.insert(std::make_unique( + k_MarkerIdConversionHandling_Key, "Marker ID Conversion Handling", + "Controls marker IDs that change when converted to internal uint32 and back. Error (default) stops before flooding. " + "Warn and Continue (Legacy Behavior) keeps the old conversion, which can remove or rename seeds, merge basins, change boundaries and region sizes, " + "and make downstream feature measurements incorrect. Choose Warn only if you intentionally accept these consequences; it does not preserve unsupported IDs. " + "Output keeps the marker type. All 8/16/32-bit integer values, including signed negatives, are lossless. The int64/uint64 lossless range is 0 through 4294967295, inclusive. " + "Zero is unmarked; UINT32_MAX is a valid seed. For example, uint64 4294967296 becomes 0, 4294967297 becomes 1, and int64 -1 becomes 4294967295.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -70,7 +80,7 @@ Parameters MorphologicalWatershedFromMarkersImageFilter::parameters() const } IFilter::VersionType MorphologicalWatershedFromMarkersImageFilter::parametersVersion() const { - return 1; + return 2; } IFilter::UniquePointer MorphologicalWatershedFromMarkersImageFilter::clone() const { @@ -165,8 +175,9 @@ Result<> MorphologicalWatershedFromMarkersImageFilter::executeImpl(DataStructure auto markWatershedLine = filterArgs.value(k_MarkWatershedLine_Key); auto fullyConnected = filterArgs.value(k_FullyConnected_Key); + const auto conversionHandling = static_cast(filterArgs.value(k_MarkerIdConversionHandling_Key)); return ImageProcessing::ExecuteMorphologicalWatershedFromMarkersImageFilter(dataStructure, imageGeomPath, selectedInputArray, markerArrayPath, outputArrayPath, markWatershedLine, fullyConnected, - shouldCancel, messageHandler); + shouldCancel, messageHandler, conversionHandling); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp index 872b2f8735..a11390574e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp @@ -23,6 +23,7 @@ class NXIMAGEPROCESSING_EXPORT MorphologicalWatershedFromMarkersImageFilter : pu static constexpr StringLiteral k_MarkerImageDataPath_Key = "marker_image_data_path"; static constexpr StringLiteral k_MarkWatershedLine_Key = "mark_watershed_line"; static constexpr StringLiteral k_FullyConnected_Key = "fully_connected"; + static constexpr StringLiteral k_MarkerIdConversionHandling_Key = "marker_id_conversion_handling_index"; static constexpr StringLiteral k_OutputImageArrayName_Key = "output_array_name"; std::string name() const override; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index e1db9a23d5..2d82f461fb 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -1018,5 +1018,243 @@ Result<> SubtractHConvexContrast(const AbstractDataStore& input, const Abstra } } +/** @brief Constant-size native context for the existing bounded marker-copy pass. */ +template +class MarkerIdConversionState +{ +public: + MarkerIdConversionState() = default; + ~MarkerIdConversionState() noexcept = default; + MarkerIdConversionState(const MarkerIdConversionState&) = delete; + MarkerIdConversionState(MarkerIdConversionState&&) = delete; + MarkerIdConversionState& operator=(const MarkerIdConversionState&) = delete; + MarkerIdConversionState& operator=(MarkerIdConversionState&&) = delete; + + bool observe(T nativeValue, uint32 encodedValue, usize index) noexcept + { + const T restoredValue = static_cast(encodedValue); + if(restoredValue == nativeValue) + { + return false; + } + if(m_ChangedCount == 0) + { + m_FirstIndex = index; + m_FirstNativeValue = nativeValue; + m_FirstRestoredValue = restoredValue; + } + ++m_ChangedCount; + return true; + } + + Result<> finish(const DataPath& markerPath, MarkerIdConversionHandling handling, Result<> result = {}) const + { + if(m_ChangedCount == 0) + { + return result; + } + const auto context = fmt::format("Marker array '{}' has {} changed marker occurrence(s) observed. First change at flat index {}: native ID {} restores as {} after uint32 conversion. ", + markerPath.toString(), m_ChangedCount, m_FirstIndex, m_FirstNativeValue, m_FirstRestoredValue); + if(handling == MarkerIdConversionHandling::Error) + { + return MergeResults(std::move(result), + MakeErrorResult(-79073, context + + "Execution stopped before flooding. Use IDs that round-trip, or select 'Warn and Continue (Legacy Behavior)' under " + "'Marker ID Conversion Handling' only if you intentionally accept seed loss, renamed IDs, merged basins, and incorrect region measurements.")); + } + return MergeResults(std::move(result), + MakeWarningVoidResult(79074, context + "'Warn and Continue (Legacy Behavior)' retains the old conversion and flood. Seeds can disappear or merge, IDs can be " + "renamed, and basin boundaries, region sizes, and downstream feature measurements can be incorrect.")); + } + +private: + usize m_ChangedCount = 0; + usize m_FirstIndex = 0; + T m_FirstNativeValue = {}; + T m_FirstRestoredValue = {}; +}; + +/** + * @brief Functor that copies an integer marker array's values into a uint32 store, dispatched on + * the source element type S via the IntegerOnly policy (which calls operator()() through + * ExecuteDataFunctionIntType). The source is read through the SAFE typed accessor + * getDataRefAs> -- not a raw dynamic_cast -- so a type mismatch throws rather than being UB. + * Single-component is guaranteed by the caller's preflight, so getSize() (values) == the tuple count. + */ +struct WidenIntegerArrayToUInt32Fn +{ + const DataStructure& dataStructure; + const DataPath& srcPath; + AbstractDataStore& dst; + const std::atomic_bool& shouldCancel; + MarkerIdConversionHandling ConversionHandling = MarkerIdConversionHandling::Error; + + template + Result<> operator()() const + { + const AbstractDataStore& s = dataStructure.getDataRefAs>(srcPath).getDataStoreRef(); + const usize total = s.getSize(); + MarkerIdConversionState conversionState; + Result<> accumulated; + try + { + constexpr usize chunkValues = 65536; + const auto inBufferPtr = std::make_unique(std::min(chunkValues, total)); + const auto outBufferPtr = std::make_unique(std::min(chunkValues, total)); + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); + } + const usize count = std::min(chunkValues, total - start); + auto readResult = s.copyIntoBuffer(start, nonstd::span(inBufferPtr.get(), count)); + accumulated = MergeResults(std::move(accumulated), std::move(readResult)); + if(accumulated.invalid()) + { + return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); + } + for(usize index = 0; index < count; ++index) + { + const S nativeValue = inBufferPtr[index]; + const uint32 encodedValue = static_cast(nativeValue); + if(conversionState.observe(nativeValue, encodedValue, start + index) && ConversionHandling == MarkerIdConversionHandling::Error) + { + // Stop before writing this chunk or entering either flood route. + return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); + } + outBufferPtr[index] = encodedValue; + } + auto writeResult = dst.copyFromBuffer(start, nonstd::span(outBufferPtr.get(), count)); + accumulated = MergeResults(std::move(accumulated), std::move(writeResult)); + if(accumulated.invalid()) + { + return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); + } + // count <= total - start, so both the next offset and start + index remain in range. + start += count; + } + return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); + } catch(const std::bad_alloc&) + { + return conversionState.finish(srcPath, ConversionHandling, + MergeResults(std::move(accumulated), MakeErrorResult(-79045, "Morphological watershed could not allocate working storage while copying marker IDs."))); + } + } +}; + +/** + * @brief Copies the integer marker array at @p srcPath (any of the 8 integer element types) into @p dst as uint32. + * The flood runs in uint32 (see @ref ApplyWatershedFromMarkers) but the marker array may be any integer + * type. All native 8/16/32-bit integers round-trip, including negative signed values. The lossless int64/uint64 + * domain is 0 through UINT32_MAX, inclusive. Error stops during this copy before the affected chunk is written; + * Warn keeps the legacy encoding and returns one summary warning, also on cancellation or a later transfer error. + */ +inline Result<> WidenIntegerArrayToUInt32(const DataStructure& dataStructure, const DataPath& srcPath, AbstractDataStore& dst, const std::atomic_bool& shouldCancel, + MarkerIdConversionHandling conversionHandling = MarkerIdConversionHandling::Error) +{ + const auto& srcArray = dataStructure.getDataRefAs(srcPath); + return IntegerOnly::dispatch(srcArray.getDataType(), WidenIntegerArrayToUInt32Fn{dataStructure, srcPath, dst, shouldCancel, conversionHandling}); +} + +/** + * @brief Functor that copies a uint32 store's values into an integer output array (narrowing cast), dispatched on + * the destination element type D via the IntegerOnly policy. The destination is written through the SAFE + * typed accessor getDataRefAs> -- not a raw dynamic_cast. The output is created of the marker's + * type by preflight. This is the legacy conversion back from the internal uint32 labels; Warn may have already + * removed, aliased, or renamed native 64-bit marker identities during the initial copy. + */ +struct NarrowUInt32StoreToIntegerArrayFn +{ + const AbstractDataStore& src; + DataStructure& dataStructure; + const DataPath& dstPath; + const std::atomic_bool& shouldCancel; + + template + Result<> operator()() const + { + AbstractDataStore& d = dataStructure.getDataRefAs>(dstPath).getDataStoreRef(); + const usize total = src.getSize(); + Result<> accumulated; + try + { + constexpr usize chunkValues = 65536; + const auto inBufferPtr = std::make_unique(std::min(chunkValues, total)); + const auto outBufferPtr = std::make_unique(std::min(chunkValues, total)); + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + return accumulated; + } + const usize count = std::min(chunkValues, total - start); + auto readResult = src.copyIntoBuffer(start, nonstd::span(inBufferPtr.get(), count)); + accumulated = MergeResults(std::move(accumulated), std::move(readResult)); + if(accumulated.invalid()) + { + return accumulated; + } + for(usize index = 0; index < count; ++index) + { + outBufferPtr[index] = static_cast(inBufferPtr[index]); + } + auto writeResult = d.copyFromBuffer(start, nonstd::span(outBufferPtr.get(), count)); + accumulated = MergeResults(std::move(accumulated), std::move(writeResult)); + if(accumulated.invalid()) + { + return accumulated; + } + start += count; // count <= total - start, including the final short chunk. + } + return accumulated; + } catch(const std::bad_alloc&) + { + return MergeResults(std::move(accumulated), MakeErrorResult(-79045, "Morphological watershed could not allocate working storage while restoring marker IDs.")); + } + } +}; + +/** + * @brief Narrows the uint32 flood output into the integer output array at @p dstPath (whose element type is the + * marker's type, set by the filter's preflight). Inverse of @ref WidenIntegerArrayToUInt32. + */ +inline Result<> NarrowUInt32StoreToIntegerArray(const AbstractDataStore& src, DataStructure& dataStructure, const DataPath& dstPath, const std::atomic_bool& shouldCancel) +{ + const auto& dstArray = dataStructure.getDataRefAs(dstPath); + return IntegerOnly::dispatch(dstArray.getDataType(), NarrowUInt32StoreToIntegerArrayFn{src, dataStructure, dstPath, shouldCancel}); +} + +/** + * @brief The border sentinel for the uint32 flood: static_cast(NumericTraits::max()). ITK's + * MorphologicalWatershedFromMarkers uses NumericTraits::max() as the out-of-bounds marker-image + * boundary constant. This value substitutes only out-of-bounds neighbors; UINT32_MAX is a valid in-bounds seed. + * For uint32/int64/uint64 the integer cast yields 0xFFFFFFFF. It does not saturate marker values. + */ +inline uint32 MarkerTypeMaxAsUInt32(DataType markerType) +{ + switch(markerType) + { + case DataType::int8: + return static_cast(std::numeric_limits::max()); + case DataType::uint8: + return static_cast(std::numeric_limits::max()); + case DataType::int16: + return static_cast(std::numeric_limits::max()); + case DataType::uint16: + return static_cast(std::numeric_limits::max()); + case DataType::int32: + return static_cast(std::numeric_limits::max()); + case DataType::uint32: + return static_cast(std::numeric_limits::max()); + case DataType::int64: + return static_cast(std::numeric_limits::max()); + case DataType::uint64: + return static_cast(std::numeric_limits::max()); + default: + return std::numeric_limits::max(); + } +} + } // namespace detail } // namespace nx::core::ImageProcessing diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp index 2f1db5ddea..45cdf0ba86 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp @@ -15,6 +15,13 @@ namespace nx::core::ImageProcessing { +/** @brief Policy for native marker IDs that change during uint32 encoding and restoration. */ +enum class MarkerIdConversionHandling : uint8 +{ + Error = 0, + WarnAndContinueLegacy = 1 +}; + /** * @brief The composite grayscale-morphology operation to apply, each built from the Dilate/Erode * primitives (and, for the three difference forms, a single elementwise store subtraction): @@ -445,16 +452,17 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteThresholdMaximumConnectedComponentsImag /** * @brief Execute the (ITK-free, out-of-core) MorphologicalWatershedFromMarkers filter: a marker-controlled - * hierarchical-queue (FAH) watershed flood (see @ref ApplyWatershedFromMarkers). - * Dispatches the grayscale input over the 10 numeric scalar types in @c AllNumeric. The marker array is - * any integer type and the output is created of the marker's type by the filter's preflight. Reproduces legacy - * ITK's MorphologicalWatershedFromMarkersImageFilter output EXACTLY, including label values (see the Parity - * model and the direct-ITK oracle test). The caller's preflight must have validated the grayscale (scalar, + * hierarchical-queue (FAH) watershed flood (see @ref ApplyWatershedFromMarkers). Dispatches on the grayscale + * input element type (all 10 scalar types); the marker array is + * any integer type and the output is created of the marker's type by preflight. Error protects native marker + * identity before flooding; Warn retains the legacy conversion and flood with one conversion diagnostic. + * The caller's preflight must have validated the grayscale (scalar, * allowed type, tuple==cells) and the marker (integer scalar, tuple==grayscale). */ NXIMAGEPROCESSING_EXPORT Result<> ExecuteMorphologicalWatershedFromMarkersImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& markerArrayPath, const DataPath& outputArrayPath, bool markWatershedLine, bool fullyConnected, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler); + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, + MarkerIdConversionHandling conversionHandling = MarkerIdConversionHandling::Error); /** * @brief Execute the (ITK-free, out-of-core) MorphologicalWatershed filter: the ITK composite (HMinima -> diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp index 15c33ec3d0..60a778bdef 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp @@ -28,157 +28,10 @@ namespace detail { namespace { -/** - * @brief Functor that copies an integer marker array's values into a uint32 store (widening cast), dispatched on - * the source element type S via the IntegerOnly policy (which calls operator()() through - * ExecuteDataFunctionIntType). The source is read through the SAFE typed accessor - * getDataRefAs> -- not a raw dynamic_cast -- so a type mismatch throws rather than being UB. - * Single-component is guaranteed by the caller's preflight, so getSize() (values) == the tuple count. - */ -struct WidenIntegerArrayToUInt32Fn -{ - const DataStructure& dataStructure; - const DataPath& srcPath; - AbstractDataStore& dst; - const std::atomic_bool& shouldCancel; - - template - Result<> operator()() const - { - const AbstractDataStore& s = dataStructure.getDataRefAs>(srcPath).getDataStoreRef(); - const usize total = s.getSize(); - constexpr usize k_ChunkValues = 65536; - auto inBuf = std::make_unique(std::min(k_ChunkValues, total)); - auto outBuf = std::make_unique(std::min(k_ChunkValues, total)); - for(usize start = 0; start < total; start += k_ChunkValues) - { - if(shouldCancel) - { - return {}; - } - const usize count = std::min(k_ChunkValues, total - start); - if(Result<> r = s.copyIntoBuffer(start, nonstd::span(inBuf.get(), count)); r.invalid()) - { - return r; - } - for(usize i = 0; i < count; ++i) - { - outBuf[i] = static_cast(inBuf[i]); - } - if(Result<> r = dst.copyFromBuffer(start, nonstd::span(outBuf.get(), count)); r.invalid()) - { - return r; - } - } - return {}; - } -}; - -/** - * @brief Copies the integer marker array at @p srcPath (any of the 8 integer element types) into @p dst as uint32. - * The flood runs in uint32 (see @ref ApplyWatershedFromMarkers) but the marker array may be any integer - * type, so it is widened up front and the flood dispatches on the grayscale type only. Exact for all marker - * labels < 0xFFFFFFFF (i.e. <= 2^32 - 2; every realistic segmentation): 0xFFFFFFFF is reserved as the border - * sentinel, so a label of exactly 0xFFFFFFFF (or >= 2^32 on an int64/uint64 array, which would truncate) would - * alias it -- documented in the filter doc, tested by the direct-ITK grid at realistic labels. - */ -Result<> WidenIntegerArrayToUInt32(const DataStructure& dataStructure, const DataPath& srcPath, AbstractDataStore& dst, const std::atomic_bool& shouldCancel) -{ - const auto& srcArray = dataStructure.getDataRefAs(srcPath); - return IntegerOnly::dispatch(srcArray.getDataType(), WidenIntegerArrayToUInt32Fn{dataStructure, srcPath, dst, shouldCancel}); -} - -/** - * @brief Functor that copies a uint32 store's values into an integer output array (narrowing cast), dispatched on - * the destination element type D via the IntegerOnly policy. The destination is written through the SAFE - * typed accessor getDataRefAs> -- not a raw dynamic_cast. The output is created of the marker's - * type by the filter's preflight, so the flooded uint32 labels (all < 2^32 for realistic markers) round-trip. - */ -struct NarrowUInt32StoreToIntegerArrayFn -{ - const AbstractDataStore& src; - DataStructure& dataStructure; - const DataPath& dstPath; - const std::atomic_bool& shouldCancel; - - template - Result<> operator()() const - { - AbstractDataStore& d = dataStructure.getDataRefAs>(dstPath).getDataStoreRef(); - const usize total = src.getSize(); - constexpr usize k_ChunkValues = 65536; - auto inBuf = std::make_unique(std::min(k_ChunkValues, total)); - auto outBuf = std::make_unique(std::min(k_ChunkValues, total)); - for(usize start = 0; start < total; start += k_ChunkValues) - { - if(shouldCancel) - { - return {}; - } - const usize count = std::min(k_ChunkValues, total - start); - if(Result<> r = src.copyIntoBuffer(start, nonstd::span(inBuf.get(), count)); r.invalid()) - { - return r; - } - for(usize i = 0; i < count; ++i) - { - outBuf[i] = static_cast(inBuf[i]); - } - if(Result<> r = d.copyFromBuffer(start, nonstd::span(outBuf.get(), count)); r.invalid()) - { - return r; - } - } - return {}; - } -}; - -/** - * @brief Narrows the uint32 flood output into the integer output array at @p dstPath (whose element type is the - * marker's type, set by the filter's preflight). Inverse of @ref WidenIntegerArrayToUInt32. - */ -Result<> NarrowUInt32StoreToIntegerArray(const AbstractDataStore& src, DataStructure& dataStructure, const DataPath& dstPath, const std::atomic_bool& shouldCancel) -{ - const auto& dstArray = dataStructure.getDataRefAs(dstPath); - return IntegerOnly::dispatch(dstArray.getDataType(), NarrowUInt32StoreToIntegerArrayFn{src, dataStructure, dstPath, shouldCancel}); -} - -/** - * @brief The border sentinel for the uint32 flood: static_cast(NumericTraits::max()). ITK's - * MorphologicalWatershedFromMarkers uses NumericTraits::max() as the out-of-bounds marker-image - * boundary constant; widening the flood to uint32 requires reproducing that per-type constant so the flood - * is bit-identical to ITK's native-TLabel flood (incl. the label==type-max collision). For uint32/int64/ - * uint64 the max saturates to 0xFFFFFFFF on the cast, which is the intended sentinel for the uint32 flood. - */ -uint32 MarkerTypeMaxAsUInt32(DataType markerType) -{ - switch(markerType) - { - case DataType::int8: - return static_cast(std::numeric_limits::max()); - case DataType::uint8: - return static_cast(std::numeric_limits::max()); - case DataType::int16: - return static_cast(std::numeric_limits::max()); - case DataType::uint16: - return static_cast(std::numeric_limits::max()); - case DataType::int32: - return static_cast(std::numeric_limits::max()); - case DataType::uint32: - return static_cast(std::numeric_limits::max()); - case DataType::int64: - return static_cast(std::numeric_limits::max()); - case DataType::uint64: - return static_cast(std::numeric_limits::max()); - default: - return std::numeric_limits::max(); - } -} - /** * @brief Dispatched execute body for MorphologicalWatershedFromMarkers. Dispatches on the GRAYSCALE element type T - * (the FAH key). The all-OOC external endpoint streams native markers into its uint32 flood records and writes - * native output labels directly. Other endpoints retain the uint32 scratch-store path. + * (the FAH key). Every endpoint copies native markers into uint32 scratch, floods using the existing route, + * and converts the result back to the marker type. Copy, flood, and restore diagnostics are retained together. */ struct MorphologicalWatershedFromMarkersExecuteFn { @@ -191,6 +44,7 @@ struct MorphologicalWatershedFromMarkersExecuteFn bool fullyConnected; const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; + MarkerIdConversionHandling ConversionHandling = MarkerIdConversionHandling::Error; template // T = grayscale type Result<> operator()() const @@ -199,6 +53,7 @@ struct MorphologicalWatershedFromMarkersExecuteFn // == nullptr checks below only catch an out-of-core failure). Resident flood buffers and the external flood's // bounded buffers/cache metadata can also throw. Convert either allocation failure into a filter error instead of // allowing an exception to escape this execute path. + Result<> accumulated; try { const auto& inArray = dataStructure.getDataRefAs>(inputArrayPath); @@ -213,20 +68,26 @@ struct MorphologicalWatershedFromMarkersExecuteFn return MakeErrorResult(-79010, "MorphologicalWatershedFromMarkers: failed to allocate a uint32 scratch store."); } - if(Result<> r = WidenIntegerArrayToUInt32(dataStructure, markerArrayPath, *markerU32, shouldCancel); r.invalid()) + accumulated = WidenIntegerArrayToUInt32(dataStructure, markerArrayPath, *markerU32, shouldCancel, ConversionHandling); + if(accumulated.invalid()) { - return r; + return accumulated; } const uint32 borderSentinel = MarkerTypeMaxAsUInt32(markerArray.getDataType()); - if(Result<> r = ApplyWatershedFromMarkers(inStore, *markerU32, *outU32, dims, markWatershedLine, fullyConnected, borderSentinel, shouldCancel, messageHandler); r.invalid()) + auto floodResult = ApplyWatershedFromMarkers(inStore, *markerU32, *outU32, dims, markWatershedLine, fullyConnected, borderSentinel, shouldCancel, messageHandler); + accumulated = MergeResults(std::move(accumulated), std::move(floodResult)); + if(accumulated.invalid()) { - return r; + return accumulated; } - return NarrowUInt32StoreToIntegerArray(*outU32, dataStructure, outputArrayPath, shouldCancel); + auto restoreResult = NarrowUInt32StoreToIntegerArray(*outU32, dataStructure, outputArrayPath, shouldCancel); + return MergeResults(std::move(accumulated), std::move(restoreResult)); } catch(const std::bad_alloc&) { - return MakeErrorResult(-79045, "Morphological watershed could not allocate working storage for this volume. Resident execution requires its global working set in RAM; out-of-core execution " - "requires bounded buffers and external temporary records."); + return MergeResults( + std::move(accumulated), + MakeErrorResult(-79045, "Morphological watershed could not allocate working storage for this volume. Resident execution requires its global working set in RAM; out-of-core execution " + "requires bounded buffers and external temporary records.")); } } }; @@ -398,14 +259,14 @@ struct MorphologicalWatershedExecuteFn // ----------------------------------------------------------------------------- Result<> ExecuteMorphologicalWatershedFromMarkersImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& markerArrayPath, const DataPath& outputArrayPath, bool markWatershedLine, bool fullyConnected, const std::atomic_bool& shouldCancel, - const IFilter::MessageHandler& messageHandler) + const IFilter::MessageHandler& messageHandler, MarkerIdConversionHandling conversionHandling) { using TypeSetPolicy = AllNumeric; const auto& inputArray = dataStructure.getDataRefAs(inputArrayPath); const DataType inputType = inputArray.getDataType(); const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); detail::MorphologicalWatershedFromMarkersExecuteFn executeFn{dataStructure, inputArrayPath, markerArrayPath, outputArrayPath, imageGeom.getDimensions(), - markWatershedLine, fullyConnected, shouldCancel, messageHandler}; + markWatershedLine, fullyConnected, shouldCancel, messageHandler, conversionHandling}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index ec839c9fd8..4d18f672fb 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -3,15 +3,30 @@ #include "WatershedPreflightTestUtils.hpp" #include "NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" +#include "NXImageProcessing/utils/ImageFilterExecutorDetail.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Core/Application.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/EmptyDataStore.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Filter/IParameter.hpp" +#include "simplnx/Filter/Parameters.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" @@ -21,9 +36,15 @@ // filter is disabled/broken, so there is no runtime-createable legacy filter to compare against). #include +#include +#include +#include +#include +#include #include #include +#include #include #include #include @@ -36,6 +57,242 @@ namespace { using WSFilter = MorphologicalWatershedFromMarkersImageFilter; +template +Arguments createMarkerLineArguments(DataStructure& dataStructure, const std::vector& markers, bool markWatershedLine, bool fullyConnected, + std::shared_ptr> inputStorePtr = {}, std::shared_ptr> markerStorePtr = {}) +{ + REQUIRE_FALSE(markers.empty()); + const ShapeType tupleShape{1, 1, markers.size()}; + const ShapeType componentShape{1}; + const DataPath geometryPath({"Image Geometry"}); + const auto cellPath = geometryPath.createChildPath("CellData"); + const auto inputPath = cellPath.createChildPath("Input"); + const auto markerPath = cellPath.createChildPath("Marker"); + auto* geometryPtr = ImageGeom::Create(dataStructure, geometryPath.getTargetName()); + REQUIRE(geometryPtr != nullptr); + geometryPtr->setDimensions({markers.size(), 1, 1}); + geometryPtr->setSpacing({1.0F, 1.0F, 1.0F}); + geometryPtr->setOrigin({0.0F, 0.0F, 0.0F}); + auto* cellDataPtr = AttributeMatrix::Create(dataStructure, cellPath.getTargetName(), tupleShape, geometryPtr->getId()); + REQUIRE(cellDataPtr != nullptr); + geometryPtr->setCellData(*cellDataPtr); + + if(inputStorePtr == nullptr) + { + inputStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, inputPath, tupleShape, componentShape); + } + if(markerStorePtr == nullptr) + { + markerStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, markerPath, tupleShape, componentShape); + } + REQUIRE(inputStorePtr != nullptr); + REQUIRE(markerStorePtr != nullptr); + REQUIRE(inputStorePtr->getStoreType() == IDataStore::StoreType::InMemory); + REQUIRE(markerStorePtr->getStoreType() == IDataStore::StoreType::InMemory); + auto* inputArrayPtr = Float32Array::Create(dataStructure, inputPath.getTargetName(), inputStorePtr, cellDataPtr->getId()); + auto* markerArrayPtr = DataArray::Create(dataStructure, markerPath.getTargetName(), markerStorePtr, cellDataPtr->getId()); + REQUIRE(inputArrayPtr != nullptr); + REQUIRE(markerArrayPtr != nullptr); + for(usize index = 0; index < markers.size(); ++index) + { + inputStorePtr->setValue(index, 0.0F); + markerStorePtr->setValue(index, markers.at(index)); + } + REQUIRE(inputArrayPtr->getTupleShape() == tupleShape); + REQUIRE(markerArrayPtr->getTupleShape() == tupleShape); + REQUIRE(inputArrayPtr->getComponentShape() == componentShape); + REQUIRE(markerArrayPtr->getComponentShape() == componentShape); + + Arguments args; + args.insertOrAssign(WSFilter::k_InputImageGeomPath_Key, std::make_any(geometryPath)); + args.insertOrAssign(WSFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(WSFilter::k_MarkerImageDataPath_Key, std::make_any(markerPath)); + args.insertOrAssign(WSFilter::k_MarkWatershedLine_Key, std::make_any(markWatershedLine)); + args.insertOrAssign(WSFilter::k_FullyConnected_Key, std::make_any(fullyConnected)); + args.insertOrAssign(WSFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + return args; +} + +Arguments createSinglePixelMarkerArguments(DataStructure& dataStructure, uint64 marker, bool markWatershedLine, bool fullyConnected) +{ + return createMarkerLineArguments(dataStructure, {marker}, markWatershedLine, fullyConnected); +} + +void requireSinglePixelMarkerPreflight(const WSFilter& filter, const DataStructure& dataStructure, const Arguments& args) +{ + const auto result = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); + REQUIRE(result.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(result.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == DataType::uint64); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, 1}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); +} + +void requireSinglePixelMarkerOutput(const DataStructure& dataStructure) +{ + const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::uint64); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + REQUIRE(outputArrayPtr->getStoreType() == IDataStore::StoreType::InMemory); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +template +void requireMarkerLine(const std::vector& markers, const std::vector& expected, ChoicesParameter::ValueType handling, bool markWatershedLine, bool fullyConnected, usize changedCount = 0) +{ + DataStructure dataStructure; + auto args = createMarkerLineArguments(dataStructure, markers, markWatershedLine, fullyConnected); + args.insertOrAssign(WSFilter::k_MarkerIdConversionHandling_Key, std::make_any(handling)); + const WSFilter filter; + const auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + REQUIRE(preflightResult.outputActions.value().actions.size() == 1); + const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + REQUIRE(actionPtr != nullptr); + REQUIRE(actionPtr->type() == GetDataType()); + REQUIRE(actionPtr->dims() == ShapeType{1, 1, markers.size()}); + REQUIRE(actionPtr->componentDims() == ShapeType{1}); + bool enteredFlood = false; + const IFilter::MessageHandler handler{[&enteredFlood](const IFilter::Message& message) { enteredFlood |= message.message == "Computing Watershed From Markers"; }}; + const auto result = filter.execute(dataStructure, args, nullptr, handler); + UnitTest::CheckArraysInheritTupleDims(dataStructure); + if(handling == 0 && changedCount != 0) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().size() == 1); + REQUIRE(result.result.errors().front().code == -79073); + REQUIRE(result.result.errors().front().message.find("Image Geometry/CellData/Marker") != std::string::npos); + REQUIRE_FALSE(enteredFlood); + return; + } + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(enteredFlood); + REQUIRE(result.result.warnings().size() == (changedCount == 0 ? 0 : 1)); + if(changedCount != 0) + { + REQUIRE(result.result.warnings().front().code == 79074); + REQUIRE(result.result.warnings().front().message.find(std::to_string(changedCount) + " changed marker occurrence(s)") != std::string::npos); + } + const DataPath outputPath({"Image Geometry", "CellData", "Output"}); + const IDataArray* outputBasePtr = nullptr; + REQUIRE_NOTHROW(outputBasePtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputBasePtr->getDataType() == GetDataType()); + REQUIRE(outputBasePtr->getTupleShape() == ShapeType{1, 1, markers.size()}); + REQUIRE(outputBasePtr->getComponentShape() == ShapeType{1}); + REQUIRE(outputBasePtr->getStoreType() == IDataStore::StoreType::InMemory); + const DataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs>(outputPath)); + REQUIRE(expected.size() == markers.size()); + for(usize index = 0; index < expected.size(); ++index) + { + REQUIRE(outputArrayPtr->getDataStoreRef().getValue(index) == expected.at(index)); + } +} + +template +class MarkerTransferStore : public DataStore +{ +public: + explicit MarkerTransferStore(usize size) + : DataStore(ShapeType{1, 1, size}, ShapeType{1}, T{}) + { + } + ~MarkerTransferStore() noexcept override = default; + MarkerTransferStore(const MarkerTransferStore&) = delete; + MarkerTransferStore(MarkerTransferStore&&) = delete; + MarkerTransferStore& operator=(const MarkerTransferStore&) = delete; + MarkerTransferStore& operator=(MarkerTransferStore&&) = delete; + + Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override + { + ++m_ReadCount; + m_LargestRead = std::max(m_LargestRead, buffer.size()); + if(m_ReadCount == m_FailRead) + { + if(m_ThrowAllocationFailure) + { + throw std::bad_alloc{}; + } + return MakeErrorResult(-1874151, "Injected marker-watershed read failure."); + } + auto result = DataStore::copyIntoBuffer(start, buffer); + if(m_ReadCount == m_CancelAfterRead && m_ShouldCancelPtr != nullptr) + { + *m_ShouldCancelPtr = true; + } + if(m_WarnOnRead) + { + result = MergeResults(std::move(result), MakeWarningVoidResult(1874153, "Delivered marker-watershed read warning.")); + } + return result; + } + Result<> copyFromBuffer(usize start, nonstd::span buffer) override + { + ++m_WriteCount; + if(m_WriteCount == m_FailWrite) + { + return MakeErrorResult(-1874152, "Injected marker-watershed write failure."); + } + auto result = DataStore::copyFromBuffer(start, buffer); + if(m_WarnOnWrite) + { + result = MergeResults(std::move(result), MakeWarningVoidResult(1874154, "Delivered marker-watershed write warning.")); + } + return result; + } + void failRead(usize call, bool throwAllocationFailure = false) noexcept + { + m_FailRead = call; + m_ThrowAllocationFailure = throwAllocationFailure; + } + void failWrite(usize call) noexcept + { + m_FailWrite = call; + } + void cancelAfterRead(usize call, std::atomic_bool& shouldCancel) noexcept + { + m_CancelAfterRead = call; + m_ShouldCancelPtr = &shouldCancel; + } + void warnOnRead() noexcept + { + m_WarnOnRead = true; + } + void warnOnWrite() noexcept + { + m_WarnOnWrite = true; + } + usize readCount() const noexcept + { + return m_ReadCount; + } + usize writeCount() const noexcept + { + return m_WriteCount; + } + usize largestRead() const noexcept + { + return m_LargestRead; + } + +private: + mutable usize m_ReadCount = 0; + mutable usize m_LargestRead = 0; + usize m_WriteCount = 0; + usize m_FailRead = 0; + usize m_FailWrite = 0; + bool m_ThrowAllocationFailure = false; + usize m_CancelAfterRead = 0; + std::atomic_bool* m_ShouldCancelPtr = nullptr; + bool m_WarnOnRead = false; + bool m_WarnOnWrite = false; +}; + //------------------------------------------------------------------------------ // A deterministic MARKER (seed) field over the grid, exercising the interesting cases the flood must reproduce: // - interior seeds of distinct labels (1, 2), @@ -144,6 +401,441 @@ std::vector MakeNegativeMarkerField(usize dx, usize dy, usize dz) } } // namespace +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker conversion policy defaults to Error", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const WSFilter filter; + REQUIRE(filter.parametersVersion() == 2); + const Parameters parameters = filter.parameters(); + const std::string policyKey = "marker_id_conversion_handling_index"; + + // The public schema can fail safely before any image allocation or future parameter-symbol lookup. + REQUIRE(parameters.contains(policyKey)); + const auto* policyPtr = dynamic_cast(parameters.at(policyKey).get()); + REQUIRE(policyPtr != nullptr); + REQUIRE(policyPtr->humanName() == "Marker ID Conversion Handling"); + REQUIRE(policyPtr->choices() == ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"}); + REQUIRE(policyPtr->defaultIndex() == 0); + const Arguments defaultArguments = filter.getDefaultArguments(); + REQUIRE(defaultArguments.value(policyKey) == 0); + std::string tooltip = policyPtr->helpText(); + std::ranges::transform(tooltip, tooltip.begin(), [](unsigned char character) { return static_cast(std::tolower(character)); }); + for(const auto* termPtr : {"default", "before", "flood", "uint32", "seed", "merge", "boundaries", "region", "measurements", "4294967295", "4294967296", "4294967297", "-1"}) + { + INFO(termPtr); + REQUIRE(tooltip.find(termPtr) != std::string::npos); + } +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: default Error rejects a changed uint64 marker before flooding", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + const bool markWatershedLine = GENERATE(true, false); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(markWatershedLine, fullyConnected); + DataStructure dataStructure; + // Unsigned narrowing is defined: legacy uint64 4294967297 becomes uint32 1. No large ID-sized allocation is needed. + const Arguments args = createSinglePixelMarkerArguments(dataStructure, uint64{4294967297ULL}, markWatershedLine, fullyConnected); + const WSFilter filter; + requireSinglePixelMarkerPreflight(filter, dataStructure, args); + bool enteredFlood = false; + const IFilter::MessageHandler messageHandler{[&enteredFlood](const IFilter::Message& message) { + if(message.message == "Computing Watershed From Markers") + { + enteredFlood = true; + } + }}; + // Omit the new choice deliberately: existing pipelines must receive the safe default. + const auto result = filter.execute(dataStructure, args, nullptr, messageHandler); + requireSinglePixelMarkerOutput(dataStructure); + REQUIRE(result.result.invalid()); + REQUIRE(result.result.errors().size() == 1); + REQUIRE(result.result.errors().front().code == -79073); + REQUIRE(result.result.errors().front().message.find("Marker") != std::string::npos); + REQUIRE(result.result.errors().front().message.find("4294967297") != std::string::npos); + REQUIRE_FALSE(enteredFlood); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32_MAX marker remains valid with uint64 output", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + const bool markWatershedLine = GENERATE(true, false); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(markWatershedLine, fullyConnected); + DataStructure dataStructure; + // A seeded one-pixel image keeps its seed. The valid upper boundary is literal, independent of the conversion helper. + const Arguments args = createSinglePixelMarkerArguments(dataStructure, uint64{4294967295ULL}, markWatershedLine, fullyConnected); + const WSFilter filter; + requireSinglePixelMarkerPreflight(filter, dataStructure, args); + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireSinglePixelMarkerOutput(dataStructure); + const UInt64Array* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry", "CellData", "Output"}))); + REQUIRE(outputArrayPtr->getDataStoreRef().getValue(0) == uint64{4294967295ULL}); +} + +TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: native lossless seeds propagate exactly", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + const bool markWatershedLine = GENERATE(true, false); + const bool fullyConnected = GENERATE(false, true); + std::vector seeds{0, 1}; + if constexpr(sizeof(TestType) <= sizeof(uint32)) + { + seeds.push_back(std::numeric_limits::max()); + if constexpr(std::is_signed_v) + { + seeds.push_back(std::numeric_limits::lowest()); + seeds.push_back(-1); + } + } + else + { + seeds.push_back(TestType{4294967295ULL}); + } + for(const TestType seed : seeds) + { + CAPTURE(seed, handling, markWatershedLine, fullyConnected); + requireMarkerLine({seed, 0, 0}, {seed, seed, seed}, handling, markWatershedLine, fullyConnected); + } +} + +TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: wide marker bounds have literal legacy results", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + const bool markWatershedLine = GENERATE(true, false); + const bool fullyConnected = GENERATE(false, true); + requireMarkerLine({TestType{4294967296ULL}, 0, 0}, {0, 0, 0}, handling, markWatershedLine, fullyConnected, 1); + requireMarkerLine({TestType{4294967297ULL}, 0, 0}, {1, 1, 1}, handling, markWatershedLine, fullyConnected, 1); + requireMarkerLine({std::numeric_limits::max(), 0, 0}, {TestType{4294967295ULL}, TestType{4294967295ULL}, TestType{4294967295ULL}}, handling, markWatershedLine, fullyConnected, + 1); + if constexpr(std::is_signed_v) + { + requireMarkerLine({-1, 0, 0}, {TestType{4294967295ULL}, TestType{4294967295ULL}, TestType{4294967295ULL}}, handling, markWatershedLine, fullyConnected, 1); + requireMarkerLine({std::numeric_limits::lowest(), 0, 0}, {0, 0, 0}, handling, markWatershedLine, fullyConnected, 1); + } +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias and lost seed choices have exact flood consequences", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); + const bool markWatershedLine = GENERATE(true, false); + const bool fullyConnected = GENERATE(false, true); + requireMarkerLine({1, 0, 4294967297ULL}, {1, 1, 1}, handling, markWatershedLine, fullyConnected, 1); + requireMarkerLine({4294967296ULL, 0, 9}, {9, 9, 9}, handling, markWatershedLine, fullyConnected, 1); + requireMarkerLine({4294967297ULL, 0, 4294967297ULL}, {1, 1, 1}, handling, markWatershedLine, fullyConnected, 2); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker copy checks chunk boundaries before writing", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize changedIndex = GENERATE(usize{65535}, usize{65536}, usize{65537}); + const auto handling = GENERATE(ImageProcessing::MarkerIdConversionHandling::Error, ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + constexpr usize valueCount = 65538; + const auto markerStorePtr = std::make_shared>(valueCount); + std::vector markers(valueCount, 0); + markers.at(changedIndex) = 4294967297ULL; + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, markers, true, false, {}, markerStorePtr); + MarkerTransferStore destination(valueCount); + std::atomic_bool shouldCancel = false; + const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, handling); + const std::string* messagePtr = nullptr; + if(handling == ImageProcessing::MarkerIdConversionHandling::Error) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -79073); + REQUIRE(destination.writeCount() == changedIndex / 65536); + messagePtr = &result.errors().front().message; + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 79074); + REQUIRE(destination.writeCount() == 2); + REQUIRE(destination.getValue(changedIndex) == 1); + messagePtr = &result.warnings().front().message; + } + REQUIRE(messagePtr->find("flat index " + std::to_string(changedIndex)) != std::string::npos); + REQUIRE(messagePtr->find("native ID 4294967297 restores as 1") != std::string::npos); + REQUIRE(messagePtr->find("1 changed marker occurrence(s)") != std::string::npos); + REQUIRE(markerStorePtr->readCount() == (handling == ImageProcessing::MarkerIdConversionHandling::Error ? changedIndex / 65536 + 1 : 2)); + REQUIRE(markerStorePtr->largestRead() <= 65536); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invalid policy is rejected before execution", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + auto args = createSinglePixelMarkerArguments(dataStructure, 1, true, false); + args.insertOrAssign(WSFilter::k_MarkerIdConversionHandling_Key, std::make_any(2)); + const WSFilter filter; + const auto result = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == FilterParameter::Constants::k_Validate_OutOfRange_Error); + REQUIRE(result.outputActions.errors().front().message.find("Marker ID Conversion Handling") != std::string::npos); + REQUIRE(dataStructure.getData(DataPath({"Image Geometry", "CellData", "Output"})) == nullptr); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: diagnostic context keeps exact 64-bit extrema", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, {0, std::numeric_limits::max(), std::numeric_limits::max()}, true, false); + MarkerTransferStore destination(3); + const auto handling = GENERATE(ImageProcessing::MarkerIdConversionHandling::Error, ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + const std::atomic_bool shouldCancel = false; + const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, handling); + const std::string* messagePtr = nullptr; + if(handling == ImageProcessing::MarkerIdConversionHandling::Error) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -79073); + REQUIRE(destination.writeCount() == 0); + messagePtr = &result.errors().front().message; + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().message.find("2 changed marker occurrence(s)") != std::string::npos); + messagePtr = &result.warnings().front().message; + } + const std::string nativeText = std::is_signed_v ? "9223372036854775807" : "18446744073709551615"; + REQUIRE(messagePtr->find("native ID " + nativeText + " restores as 4294967295") != std::string::npos); + REQUIRE(messagePtr->find("First change at flat index 1") != std::string::npos); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy cancellation retains observed warning only", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize valueCount = 65537; + const auto markerStorePtr = std::make_shared>(valueCount); + DataStructure dataStructure; + std::vector markers(valueCount, 0); + markers.at(0) = 4294967297ULL; + markers.at(valueCount - 1) = 4294967296ULL; + createMarkerLineArguments(dataStructure, markers, true, false, {}, markerStorePtr); + MarkerTransferStore destination(valueCount); + std::atomic_bool shouldCancel = false; + markerStorePtr->cancelAfterRead(1, shouldCancel); + const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, + ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel.load()); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().message.find("1 changed marker occurrence(s)") != std::string::npos); + REQUIRE(markerStorePtr->readCount() == 1); + REQUIRE(destination.writeCount() == 1); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy warning survives transfer failure or allocation failure", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize valueCount = 65537; + const auto markerStorePtr = std::make_shared>(valueCount); + std::vector markers(valueCount, 0); + markers.at(0) = 4294967297ULL; + markers.at(valueCount - 1) = 4294967296ULL; + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, markers, true, false, {}, markerStorePtr); + MarkerTransferStore destination(valueCount); + int32 expectedCode = 0; + SECTION("later marker read fails") + { + markerStorePtr->failRead(2); + expectedCode = -1874151; + } + SECTION("scratch chunk write fails") + { + destination.failWrite(1); + expectedCode = -1874152; + } + SECTION("later marker read throws allocation failure") + { + markerStorePtr->failRead(2, true); + expectedCode = -79045; + } + std::atomic_bool shouldCancel = false; + const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, + ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == expectedCode); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().message.find("1 changed marker occurrence(s)") != std::string::npos); + REQUIRE(markerStorePtr->readCount() <= 2); + REQUIRE(destination.writeCount() == 1); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conversion warning survives later facade outcomes", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const auto inputStorePtr = std::make_shared>(3); + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, {4294967297ULL, 0, 0}, true, false, inputStorePtr); + const auto outputStorePtr = std::make_shared>(3); + const AttributeMatrix* cellDataPtr = nullptr; + REQUIRE_NOTHROW(cellDataPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry", "CellData"}))); + REQUIRE(UInt64Array::Create(dataStructure, "Output", outputStorePtr, cellDataPtr->getId()) != nullptr); + std::atomic_bool shouldCancel = false; + bool cancelAtFlood = false; + int32 expectedCode = 0; + SECTION("grayscale read error after conversion") + { + inputStorePtr->failRead(1); + expectedCode = -1874151; + } + SECTION("grayscale allocation failure after conversion") + { + inputStorePtr->failRead(1, true); + expectedCode = -79045; + } + SECTION("native restoration write error after conversion") + { + outputStorePtr->failWrite(1); + expectedCode = -1874152; + } + SECTION("facade cancellation after conversion retains valid Result warning") + { + cancelAtFlood = true; + } + const IFilter::MessageHandler handler{[&](const IFilter::Message& message) { + if(cancelAtFlood && message.message == "Computing Watershed From Markers") + { + shouldCancel = true; + } + }}; + // Call the facade directly: generic IFilter cancellation can replace a valid result and drop its warnings. + const auto result = ImageProcessing::ExecuteMorphologicalWatershedFromMarkersImageFilter(dataStructure, DataPath({"Image Geometry"}), DataPath({"Image Geometry", "CellData", "Input"}), + DataPath({"Image Geometry", "CellData", "Marker"}), DataPath({"Image Geometry", "CellData", "Output"}), true, + false, shouldCancel, handler, ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + if(cancelAtFlood) + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel.load()); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == expectedCode); + } + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == 79074); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: successful transfer warnings survive later marker-copy failure", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize valueCount = 65537; + const auto markerStorePtr = std::make_shared>(valueCount); + std::vector markers(valueCount, 0); + markers.at(0) = 4294967297ULL; + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, markers, true, false, {}, markerStorePtr); + markerStorePtr->warnOnRead(); + markerStorePtr->failRead(2); + MarkerTransferStore destination(valueCount); + destination.warnOnWrite(); + const std::atomic_bool shouldCancel = false; + const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, + ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -1874151); + REQUIRE(result.warnings().size() == 3); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 79074; }) == 1); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874153; }) == 1); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874154; }) == 1); + REQUIRE(markerStorePtr->readCount() == 2); + REQUIRE(destination.writeCount() == 1); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restoration retains transfer warnings through a short final chunk", + "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize valueCount = 65537; + DataStructure dataStructure; + createMarkerLineArguments(dataStructure, std::vector(valueCount, 0), true, false); + const auto outputStorePtr = std::make_shared>(valueCount); + const AttributeMatrix* cellDataPtr = nullptr; + REQUIRE_NOTHROW(cellDataPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry", "CellData"}))); + REQUIRE(UInt64Array::Create(dataStructure, "Output", outputStorePtr, cellDataPtr->getId()) != nullptr); + MarkerTransferStore source(valueCount); + source.setValue(0, 1); + source.setValue(valueCount - 1, 4294967295U); + source.warnOnRead(); + outputStorePtr->warnOnWrite(); + bool expectFailure = false; + SECTION("all delivered warnings survive success") + { + } + SECTION("earlier warnings survive final write failure") + { + outputStorePtr->failWrite(2); + expectFailure = true; + } + const std::atomic_bool shouldCancel = false; + const auto result = ImageProcessing::detail::NarrowUInt32StoreToIntegerArray(source, dataStructure, DataPath({"Image Geometry", "CellData", "Output"}), shouldCancel); + if(expectFailure) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -1874152); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(outputStorePtr->getValue(valueCount - 1) == 4294967295ULL); + } + REQUIRE(outputStorePtr->getValue(0) == 1); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874153; }) == 2); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874154; }) == (expectFailure ? 1 : 2)); + REQUIRE(source.readCount() == 2); + REQUIRE(outputStorePtr->writeCount() == 2); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + // ----------------------------------------------------------------------------- // (4) Preflight guards: a multi-component grayscale is rejected (requireScalar); a non-integer (float32) marker is // rejected; a marker whose tuple count mismatches the grayscale is rejected; a valid pair creates an output of diff --git a/src/simplnx/Utilities/ImageProcessing/WatershedFromMarkersEngine.hpp b/src/simplnx/Utilities/ImageProcessing/WatershedFromMarkersEngine.hpp index 63ec1dcf73..cacde9d572 100644 --- a/src/simplnx/Utilities/ImageProcessing/WatershedFromMarkersEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/WatershedFromMarkersEngine.hpp @@ -1024,8 +1024,8 @@ Result<> ApplyWatershedFromMarkersExternal(const AbstractDataStore& inSt // ITK-free port of MorphologicalWatershedFromMarkersImageFilter: a hierarchical-queue (FAH) marker-controlled // watershed flood, transcribed verbatim from itkMorphologicalWatershedFromMarkersImageFilter.hxx::GenerateData // (both Meyer/watershed-line and Beucher variants). Labels are flooded in uint32; `borderSentinel` reproduces ITK's -// NumericTraits::max() marker-image boundary constant (pass static_cast(markerType_max)) so uint32 -// flooding is bit-identical to ITK's native-TLabel flood including the label==type-max collision. `TInput` (the FAH +// NumericTraits::max() marker-image boundary constant (pass static_cast(markerType_max)). It only +// substitutes out-of-bounds neighbors; it does not reserve an in-bounds seed identity. `TInput` (the FAH // key = gray level) is the only template parameter. Resident endpoints use the original contiguous-RAM map-of-FIFOs // implementation below. An OOC 8/16-bit integral endpoint uses that contiguous implementation only when a conservative // full-state reservation fits the shared cache budget; a partial grant, allocation failure, or wider/floating input @@ -1046,7 +1046,7 @@ Result<> ApplyWatershedFromMarkersResident(const AbstractDataStore& inSt // Precondition: borderSentinel must be non-zero (it is NumericTraits::max() by contract). The Beucher branch // maps out-of-bounds neighbors to borderSentinel and gates propagation on `o == wsLabel(0)`, so a non-zero sentinel - // guarantees an OOB neighbor can never alias a real label (nor flat index 0). All type maxes are > 0, so this holds. + // guarantees an OOB neighbor never appears as background and never propagates to flat index 0. All type maxes are > 0. assert(borderSentinel != wsLabel); const int64 nX = static_cast(dims[0]); From 6aab281d460f74f13c3a7d4fddb5b86a80647a6a Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 02:00:43 -0400 Subject: [PATCH 19/98] BUG: Validate Otsu numeric input and add explicit masks Add Error and Warn nonfinite handling, linked bool/uint8 masking, distinct excluded labels and checked uint8 class intervals. Preserve selected nonfinite endpoint frequencies, ordinary finite solver behavior and native output labels. Guard unusable geometry and objective arithmetic, retain bounded parallel range scans and document migration and downstream effects. Validated full paired Windows Release builds, 29 InCore and 33 OOC cases plus 9 HistogramEngine controls in each. Independent literal and rational expectations include the demonstrated first-centroid regression. Source review and clang-format 19.1.1 pass. Configured clang-tidy remains nonpassing with documented framework and style diagnostics. Debug, cross-platform, installed SDK and universal scientific validation are not claimed. Signed-off-by: Jessica Marquis --- CMakeLists.txt | 3 + .../docs/OtsuMultipleThresholdsImageFilter.md | 41 +- .../OtsuMultipleThresholdsImageFilter.cpp | 181 +--- .../OtsuMultipleThresholdsImageFilter.hpp | 4 + .../OtsuMultipleThresholdsImageFilterTest.cpp | 935 +++++++++++++++++- .../ImageProcessing/HistogramEngine.hpp | 104 +- .../ImageProcessing/OtsuImageEngine.hpp | 723 ++++++++++++++ 7 files changed, 1801 insertions(+), 190 deletions(-) create mode 100644 src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1329d712c4..8c812369e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -607,6 +607,9 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageGeometryResample.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/WorkingMemory.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/TerminalImageConversion.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/OtsuImageEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/HistogramEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/PointwiseEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ProgressEstimator.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ThrottledMessageHandler.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/StringUtilities.hpp diff --git a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md index 2e3cdbaebc..29756099d1 100644 --- a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md @@ -10,7 +10,36 @@ NXImageProcessing (Thresholding) This filter creates a labeled image that separates the input image into various classes. The filter computes the thresholds using a multi-Otsu calculator and applies those thresholds to the input image to produce class labels. The **Number Of Histogram Bins** and **Number Of Thresholds** control the calculator. The **Label Offset** is added to every output label so labels can start from a value other than zero. -Each output voxel is assigned the index of the class its intensity falls into: the number of thresholds the value exceeds, plus the **Label Offset**. The output is always a `uint8` label image, regardless of the (numeric, single-component) input element type. +Each finite, selected voxel receives the number of thresholds that its intensity strictly exceeds, plus **Label Offset**. A value equal to a threshold belongs to the lower class. The input can use any scalar signed or unsigned integer type, `float32`, or `float64`. The output is always a scalar `uint8` label image with the input tuple shape. Threshold values use the input intensity units; output labels have no intensity units. + +The configured class interval is **Label Offset** through **Label Offset + Number Of Thresholds**, including both endpoints. The sum must not exceed 255. The filter rejects an invalid interval instead of wrapping labels. It does not renumber classes or widen the output type. + +### NaN and infinity + +**Nonfinite Input Handling** has two choices: + +- **Error** is the default. A selected NaN, negative infinity, or positive infinity stops the filter. Choose a deliberate handling method before rerunning the pipeline. +- **Warn and Continue** retains these selected samples in the histogram. Only selected finite values set the histogram bounds. NaN and negative infinity contribute to the first bin and receive the ordinary lowest class label. Positive infinity contributes to the last bin and receives the ordinary highest class label. These samples affect threshold fitting; they are not treated as excluded pixels. The filter returns one warning with counts for the three nonfinite kinds. Classification can be unreliable or unpredictable. + +Both choices require at least one selected finite sample. Empty data, an all-false mask, or data with no selected finite value cannot define a histogram range and cause an error. Warn does not silently remove nonfinite samples. + +### Explicit mask and excluded labels + +Enable **Use Mask Array** to select a **Mask Array**. The mask must be scalar `bool` or `uint8` and have the same tuple count as the input. Tuples are paired in flat order; the mask can have a different tuple shape or parent group. False/zero excludes a pixel; true or any nonzero `uint8` value selects it. The filter applies this selection before range, histogram, and nonfinite checks. An excluded NaN or infinity therefore causes no nonfinite error or warning. + +Excluded pixels receive **Masked Label**, which defaults to 255. This label must lie outside the entire configured class interval, even if some configured classes have no pixels. With a mask, at most 255 ordinary classes and one distinct excluded label fit in `uint8`. An interval that uses all labels 0 through 255 cannot leave a distinct Masked Label. The filter reports a collision and requires a control change. + +A distinct label does not make downstream filters ignore excluded pixels. Supply the mask or an explicit background/exclusion rule to consumers that must ignore them. Changing histogram participation can change phase fractions, feature counts, and connectivity. + +For example, with input `[0, 0, 4, 4, NaN, -inf, +inf]`, two bins, one threshold, and Label Offset 7, Warn produces histogram counts `[4, 3]` and labels `[7, 7, 8, 8, 7, 7, 8]`. The finite bounds remain 0 and 4. An explicit mask `[1, 1, 1, 1, 0, 0, 0]` instead produces counts `[2, 2]`, no nonfinite warning, and labels `[7, 7, 8, 8, 255, 255, 255]` with Masked Label 255. This mask also permits Error mode because it excludes all nonfinite values. + +### Precision and pipeline migration + +Histogram bounds, bin measurements, and threshold fitting use `float64`. The filter checks for detectable unusable range/bin geometry, nonfinite arithmetic, and objective underflow. For example, distinct adjacent large 64-bit integers can become equal after conversion to `float64`, and very small nonzero class means can square to zero. These conditions cause a numeric error. Rescale the finite data or use fewer bins where suitable. The checks do not establish exact arbitrary-precision Otsu accuracy. True finite constants retain the lower-class tie behavior. + +The finite tie tolerance, ordinary threshold search, and Valley Emphasis behavior are retained. **ReturnBinMidpoint** selects a bin midpoint instead of its maximum for the returned threshold. These checks add no automatic normalization or new scientific fitting rule. + +Existing pipelines receive Error, Use Mask Array off, and Masked Label 255 as defaults. Finite pipelines with valid label intervals retain their labels. Pipelines that previously contained NaN/infinity now stop by default. Choose Warn to keep the stated endpoint-bin participation, or supply an explicit mask to exclude samples. Replacing nonfinite values upstream adds replacement intensities to the histogram and can bias thresholds; it is not statistical exclusion. This filter also includes an option to use the valley emphasis algorithm from H.F. Ng, "Automatic thresholding for defect detection", Pattern Recognition Letters, (27): 1644-1649, 2006. The valley emphasis algorithm is particularly effective when the object to be thresholded is small. It is turned off by default; enable it with **Valley Emphasis**. @@ -18,9 +47,15 @@ ITK-free, out-of-core-capable reimplementation of the legacy ITK Otsu Multiple T ## Algorithm -For integral inputs whose observed intensity span is at most 65,535, the filter determines the input range and exact value frequencies in one global scan using a fixed 65,536-entry frequency ring, then populates the requested histogram without rereading the image. Wider integral ranges fall back to a second streaming scan after the range is known. Floating-point inputs retain the statistics scan followed by the histogram scan so their established NaN and range behavior remains unchanged. +For integral inputs whose selected native intensity span is at most 65,535, the filter can determine the input range and exact value frequencies in one scan using a fixed 65,536-entry frequency ring. It then fills the requested histogram without rereading the input. Wider integral ranges, or a working-memory grant that cannot hold this optional ring, use a second bounded histogram scan. Floating-point inputs use a selected finite range/count scan followed by the histogram scan. A true constant has an analytic tied-threshold path. + +The histogram uses the existing marginal upper bound `max + (max - min) / (numberOfBins * 100)`. An upper-bound extension that rounds back to `max` is permitted when the bin geometry remains usable; endpoint clamping keeps minimum/maximum samples inside their bins before an integer index conversion. Native full-span 64-bit inputs remain valid for a two-bin split. No native maximum is incremented. + +Thresholds are calculated from the histogram and applied independently to each selected voxel. Resident scans use direct spans. Other scans use serial bulk input/mask transfers with bounded owned buffers and working-memory reservations. Floating-point range scans reduce fixed blocks in parallel when multicore support is enabled and a batch contains more than one block. Workers read only resident spans or owned buffers. The main thread merges finite bounds and selected counts in block order. The summary table belongs to the current bounded batch and does not grow with the full image. + +The input, mask, and floating-range summaries share a scratch target of at most 64 MiB; a low grant can reduce a buffered scan to one tuple. The masked output pass reserves combined input, mask, and label buffers. Boolean masks use contiguous owned storage. The optional ring and histogram/solver arrays depend on the fixed ring size and requested bin/class counts, not on image volume. The unmasked output pass retains the existing resident/buffered pointwise routes. No full-volume finite-only copy is made. -The thresholds are calculated from the fixed-size histogram and applied independently to every voxel. Disk-backed global scans use byte-capped buffers of at most 64 MiB, the frequency ring has a fixed size independent of the image volume, and the output pass uses separate bounded input and output buffers. Store transfers remain serial and memory use does not grow with the image volume. +Cancellation is checked during scans and threshold search and before label writes. Transfer failures remain errors. Completed earlier label batches are not rolled back if a later output transfer fails. ![Otsu multiple-threshold label map.](Images/OtsuThreshold.png) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index f384c837bf..a6e859748a 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -1,181 +1,4 @@ #include "OtsuMultipleThresholdsImageFilter.hpp" -#include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" - -#include "simplnx/Common/TypesUtility.hpp" -#include "simplnx/Parameters/ArraySelectionParameter.hpp" -#include "simplnx/Parameters/BoolParameter.hpp" -#include "simplnx/Parameters/DataGroupSelectionParameter.hpp" -#include "simplnx/Parameters/DataObjectNameParameter.hpp" -#include "simplnx/Parameters/GeometrySelectionParameter.hpp" -#include "simplnx/Parameters/NumberParameter.hpp" -#include "simplnx/Parameters/StringParameter.hpp" -#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" -#include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" -#include "simplnx/Utilities/SIMPLConversion.hpp" - -#include - -#include -#include - -using namespace nx::core; - -namespace -{ -struct OtsuOperation -{ - uint32 numBins = 128u; - uint8 numThresholds = 1u; - uint8 labelOffset = 0u; - bool valleyEmphasis = false; - bool returnBinMidpoint = false; - - template - auto makeClassifyOp(const std::vector& thresholds) const - { - const uint8 offset = labelOffset; - return [thresholds, offset](T value) -> U { - // class index = count of thresholds this value STRICTLY EXCEEDS, + labelOffset. std::lower_bound gives the - // count of thresholds < value, i.e. a value exactly equal to a threshold lands in the LOWER class -- which - // is ITK's ThresholdLabeler convention ("values equal to a threshold are in the lower class"). Using - // std::upper_bound here put a value == threshold one class too high (e.g. every voxel of a constant image). - const auto idx = static_cast(std::lower_bound(thresholds.begin(), thresholds.end(), static_cast(value)) - thresholds.begin()); - return static_cast(offset + idx); - }; - } -}; -} // namespace - -namespace nx::core -{ -std::string OtsuMultipleThresholdsImageFilter::name() const -{ - return FilterTraits::name; -} -std::string OtsuMultipleThresholdsImageFilter::className() const -{ - return FilterTraits::className; -} -Uuid OtsuMultipleThresholdsImageFilter::uuid() const -{ - return FilterTraits::uuid; -} -std::string OtsuMultipleThresholdsImageFilter::humanName() const -{ - return "Otsu Multiple Thresholds Image Filter"; -} -std::vector OtsuMultipleThresholdsImageFilter::defaultTags() const -{ - return {className(), "NXImageProcessing", "OtsuMultipleThresholds", "Thresholding"}; -} -Parameters OtsuMultipleThresholdsImageFilter::parameters() const -{ - Parameters params; - params.insertSeparator(Parameters::Separator{"Input Parameter(s)"}); - params.insert(std::make_unique(k_NumberOfThresholds_Key, "Number Of Thresholds", "Set/Get the number of thresholds. Default is 1.", 1u)); - params.insert(std::make_unique(k_LabelOffset_Key, "Label Offset", "Set/Get the offset which labels have to start from. Default is 0.", 0u)); - params.insert(std::make_unique(k_NumberOfHistogramBins_Key, "Number Of Histogram Bins", "Set/Get the number of histogram bins. Default is 128.", 128u)); - params.insert(std::make_unique(k_ValleyEmphasis_Key, "Valley Emphasis", "Set/Get the use of valley emphasis. Default is false.", false)); - params.insert( - std::make_unique(k_ReturnBinMidpoint_Key, "ReturnBinMidpoint", "Should the threshold value be mid-point of the bin or the maximum? Default is to return bin maximum.", false)); - - params.insertSeparator(Parameters::Separator{"Input Cell Data"}); - params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), - GeometrySelectionParameter::AllowedTypes{IGeometry::Type::Image})); - params.insert(std::make_unique(k_InputImageDataPath_Key, "Input Cell Data", "The image data that will be processed by this filter.", DataPath{}, - nx::core::ImageProcessing::GetScalarNumericTypes())); - - params.insertSeparator(Parameters::Separator{"Output Cell Data"}); - params.insert(std::make_unique(k_OutputImageArrayName_Key, "Output Cell Data", - "The result of the processing will be stored in this Data Array inside the same group as the input data.", "Output Image Data")); - - return params; -} -IFilter::VersionType OtsuMultipleThresholdsImageFilter::parametersVersion() const -{ - return 1; -} -IFilter::UniquePointer OtsuMultipleThresholdsImageFilter::clone() const -{ - return std::make_unique(); -} -IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const DataStructure& dataStructure, const Arguments& filterArgs, const MessageHandler& messageHandler, - const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const -{ - auto imageGeomPath = filterArgs.value(k_InputImageGeomPath_Key); - auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); - auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); - auto numberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key); - auto numberOfHistogramBins = filterArgs.value(k_NumberOfHistogramBins_Key); - const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - - // Data-independent guard: an out-of-range threshold count makes the backend return an empty threshold - // set, which would silently classify every voxel into a single (labelOffset-valued) class. - if(numberOfThresholds == 0 || numberOfThresholds >= numberOfHistogramBins) - { - return { - MakeErrorResult(-8311, fmt::format("NumberOfThresholds ({}) must be >= 1 and < NumberOfHistogramBins ({}).", static_cast(numberOfThresholds), numberOfHistogramBins))}; - } - - const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); - if(inputArray.getNumberOfComponents() != 1) - { - return {MakeErrorResult(-8310, fmt::format("Otsu Multiple Thresholds requires a single-component (scalar) input array, but '{}' has {} components.", selectedInputArray.toString(), - inputArray.getNumberOfComponents()))}; - } - - // Output is a uint8 label image regardless of the (numeric) input element type. - Result resultOutputActions = - ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); - return {std::move(resultOutputActions)}; -} -Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, - const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const -{ - auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); - auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); - const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - - auto numberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key); - auto labelOffset = filterArgs.value(k_LabelOffset_Key); - auto numberOfHistogramBins = filterArgs.value(k_NumberOfHistogramBins_Key); - auto valleyEmphasis = filterArgs.value(k_ValleyEmphasis_Key); - auto returnBinMidpoint = filterArgs.value(k_ReturnBinMidpoint_Key); - - const OtsuOperation operation{numberOfHistogramBins, numberOfThresholds, labelOffset, valleyEmphasis, returnBinMidpoint}; - return ImageProcessing::ExecuteHistogramImageFilter(dataStructure, selectedInputArray, outputArrayPath, operation, shouldCancel, messageHandler); -} - -namespace -{ -namespace SIMPL -{ -constexpr StringLiteral k_NumberOfThresholdsKey = "NumberOfThresholds"; -constexpr StringLiteral k_LabelOffsetKey = "LabelOffset"; -constexpr StringLiteral k_NumberOfHistogramBinsKey = "NumberOfHistogramBins"; -constexpr StringLiteral k_ValleyEmphasisKey = "ValleyEmphasis"; -constexpr StringLiteral k_SelectedCellArrayPathKey = "SelectedCellArrayPath"; -constexpr StringLiteral k_NewCellArrayNameKey = "NewCellArrayName"; -} // namespace SIMPL -} // namespace - -Result OtsuMultipleThresholdsImageFilter::FromSIMPLJson(const nlohmann::json& json) -{ - Arguments args = OtsuMultipleThresholdsImageFilter().getDefaultArguments(); - - std::vector> results; - - results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_NumberOfThresholdsKey, k_NumberOfThresholds_Key)); - results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_LabelOffsetKey, k_LabelOffset_Key)); - results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_NumberOfHistogramBinsKey, k_NumberOfHistogramBins_Key)); - results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_ValleyEmphasisKey, k_ValleyEmphasis_Key)); - results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_SelectedCellArrayPathKey, k_InputImageGeomPath_Key)); - results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_SelectedCellArrayPathKey, k_InputImageDataPath_Key)); - results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_NewCellArrayNameKey, k_OutputImageArrayName_Key)); - - Result<> conversionResult = MergeResults(std::move(results)); - - return ConvertResultTo(std::move(conversionResult), std::move(args)); -} -} // namespace nx::core +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp index fc18c9b2a4..d29d3816aa 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp @@ -26,6 +26,10 @@ class NXIMAGEPROCESSING_EXPORT OtsuMultipleThresholdsImageFilter : public IFilte static constexpr StringLiteral k_NumberOfHistogramBins_Key = "number_of_histogram_bins"; static constexpr StringLiteral k_ValleyEmphasis_Key = "valley_emphasis"; static constexpr StringLiteral k_ReturnBinMidpoint_Key = "return_bin_midpoint"; + static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; + static constexpr StringLiteral k_UseMaskArray_Key = "use_mask_array"; + static constexpr StringLiteral k_MaskArrayPath_Key = "mask_array_path"; + static constexpr StringLiteral k_MaskedLabel_Key = "masked_label"; static Result FromSIMPLJson(const nlohmann::json& json); diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 5c9f51a63f..98c9752d25 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -1,9 +1,46 @@ #include "ItkGoldenTestUtils.hpp" +#include "ReconstructionFilterTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/AttributeMatrix.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Filter/IParameter.hpp" +#include "simplnx/Filter/Parameters.hpp" +#include "simplnx/Parameters/ArraySelectionParameter.hpp" +#include "simplnx/Parameters/BoolParameter.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" +#include "simplnx/Parameters/NumberParameter.hpp" +#include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp" + +#include +#include +#include + +#include +#include #include +#include +#include +#include +#include +#include +#include using namespace nx::core; @@ -38,7 +75,7 @@ DataPath BuildMultimodalImage(DataStructure& ds, usize dim) for(usize i = 0; i < total; ++i) { const usize cluster = std::min(3, i / quarter); - ref.setValue(i, clusterBase[cluster] + static_cast(i % 8)); // clusters at ~10-17, 70-77, 140-147, 210-217 + ref.setValue(i, clusterBase.at(cluster) + static_cast(i % 8)); // clusters at ~10-17, 70-77, 140-147, 210-217 } return inputPath; } @@ -83,6 +120,589 @@ void RunCompactInt16Otsu(DataStructure& ds, const DataPath& inputPath) } } // namespace +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +{ + UnitTest::LoadPlugins(); + const OtsuMultipleThresholdsImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string policyKey = "nonfinite_input_handling_index"; + + // Inspect the public schema before lookup. Do not enter the baseline histogram with NaN or infinity. + REQUIRE(parameters.contains(policyKey)); + const auto* policyPtr = dynamic_cast(parameters.at(policyKey).get()); + REQUIRE(policyPtr != nullptr); + REQUIRE(policyPtr->humanName() == "Nonfinite Input Handling"); + REQUIRE(policyPtr->choices() == ChoicesParameter::Choices{"Error", "Warn and Continue"}); + REQUIRE(policyPtr->defaultIndex() == 0); + const Arguments defaultArguments = filter.getDefaultArguments(); + REQUIRE(defaultArguments.value(policyKey) == 0); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask selection is optional and linked", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +{ + UnitTest::LoadPlugins(); + const OtsuMultipleThresholdsImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string useMaskKey = "use_mask_array"; + const std::string maskPathKey = "mask_array_path"; + + REQUIRE(parameters.contains(useMaskKey)); + REQUIRE(parameters.contains(maskPathKey)); + const auto* useMaskPtr = dynamic_cast(parameters.at(useMaskKey).get()); + const auto* maskPtr = dynamic_cast(parameters.at(maskPathKey).get()); + REQUIRE(useMaskPtr != nullptr); + REQUIRE(maskPtr != nullptr); + REQUIRE(useMaskPtr->humanName() == "Use Mask Array"); + REQUIRE_FALSE(std::any_cast(useMaskPtr->defaultValue())); + REQUIRE(maskPtr->allowedTypes() == ArraySelectionParameter::AllowedTypes{DataType::boolean, DataType::uint8}); + REQUIRE(maskPtr->requiredComponentShapes() == ArraySelectionParameter::AllowedComponentShapes{{1}}); + REQUIRE(maskPtr->allowsInMemory()); + REQUIRE(maskPtr->allowsOutOfCore()); + + REQUIRE(parameters.containsGroup(useMaskKey)); + const std::vector linkedKeys = parameters.getKeysInGroup(useMaskKey); + REQUIRE(std::find(linkedKeys.begin(), linkedKeys.end(), maskPathKey) != linkedKeys.end()); + // A Masked Label control may also belong to this group. Its presence must not break this test. + Arguments arguments = filter.getDefaultArguments(); + REQUIRE_FALSE(arguments.value(useMaskKey)); + REQUIRE_FALSE(parameters.isParameterActive(maskPathKey, arguments)); + arguments.insertOrAssign(useMaskKey, std::make_any(true)); + REQUIRE(parameters.isParameterActive(maskPathKey, arguments)); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has a separate uint8 control", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +{ + UnitTest::LoadPlugins(); + const OtsuMultipleThresholdsImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string maskedLabelKey = "masked_label"; + + REQUIRE(parameters.contains(maskedLabelKey)); + const auto* maskedLabelPtr = dynamic_cast(parameters.at(maskedLabelKey).get()); + REQUIRE(maskedLabelPtr != nullptr); + REQUIRE(maskedLabelPtr->humanName() == "Masked Label"); + REQUIRE(std::any_cast(maskedLabelPtr->defaultValue()) == uint8{255}); + const Arguments defaultArguments = filter.getDefaultArguments(); + REQUIRE(defaultArguments.value(maskedLabelKey) == uint8{255}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: preflight checks the complete configured uint8 class interval", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = BuildMultimodalImage(dataStructure, 2); + const OtsuMultipleThresholdsImageFilter filter; + Arguments arguments = filter.getDefaultArguments(); + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(256)); + + uint8 labelOffset = 0; + uint8 thresholdCount = 1; + bool expectValid = false; + SECTION("offset 254 plus two thresholds exceeds 255") + { + labelOffset = 254; + thresholdCount = 2; + } + SECTION("offset 255 plus one threshold exceeds 255") + { + labelOffset = 255; + thresholdCount = 1; + } + SECTION("offset 253 plus two thresholds reaches 255") + { + labelOffset = 253; + thresholdCount = 2; + expectValid = true; + } + SECTION("255 thresholds fit when the offset is zero and no mask is used") + { + labelOffset = 0; + thresholdCount = 255; + expectValid = true; + } + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(labelOffset)); + arguments.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(thresholdCount)); + + // This regression is preflight-only. Never execute overflowing labels or the 255-threshold solver case. + const auto preflightResult = filter.preflight(dataStructure, arguments); + if(expectValid) + { + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + } + else + { + REQUIRE(preflightResult.outputActions.invalid()); + REQUIRE_FALSE(preflightResult.outputActions.errors().empty()); + REQUIRE(preflightResult.outputActions.errors().front().code == -8312); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +namespace +{ +Arguments otsuArguments(const DataPath& inputPath) +{ + Arguments args = OtsuMultipleThresholdsImageFilter().getDefaultArguments(); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(2)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(7)); + return args; +} + +template +DataPath createOtsuMask(DataStructure& dataStructure, const std::array& values, const ShapeType& componentShape = {1}) +{ + const DataPath maskPath({"Mask"}); + auto storePtr = DataStoreUtilities::CreateDataStore(dataStructure, maskPath, {N / componentShape.front()}, componentShape); + const auto writeResult = storePtr->copyFromBuffer(0, nonstd::span(values.data(), values.size())); + SIMPLNX_RESULT_REQUIRE_VALID(writeResult); + auto* arrayPtr = DataArray::Create(dataStructure, "Mask", storePtr); + REQUIRE(arrayPtr != nullptr); + return maskPath; +} + +void requireOtsuLabels(const DataStructure& dataStructure, const DataPath& inputPath, const std::vector& expected) +{ + const DataPath outputPath = inputPath.replaceName("Output"); + const IDataArray* arrayPtr = nullptr; + REQUIRE_NOTHROW(arrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(arrayPtr->getDataType() == DataType::uint8); + REQUIRE(arrayPtr->getComponentShape() == ShapeType{1}); + REQUIRE(arrayPtr->getNumberOfTuples() == expected.size()); + const UInt8Array* labelsPtr = nullptr; + REQUIRE_NOTHROW(labelsPtr = &dataStructure.getDataRefAs(outputPath)); + std::vector actual(expected.size()); + const auto readResult = labelsPtr->getDataStoreRef().copyIntoBuffer(0, nonstd::span(actual.data(), actual.size())); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + REQUIRE(actual == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +template +class OtsuTransferStore : public DataStore +{ +public: + explicit OtsuTransferStore(usize size) + : DataStore({size}, {1}, T{}) + { + } + ~OtsuTransferStore() noexcept override = default; + OtsuTransferStore(const OtsuTransferStore&) = delete; + OtsuTransferStore(OtsuTransferStore&&) = delete; + OtsuTransferStore& operator=(const OtsuTransferStore&) = delete; + OtsuTransferStore& operator=(OtsuTransferStore&&) = delete; + + IDataStore::StoreType getStoreType() const override + { + // An abstract-store route witness only. This test store has no disk backing. + return IDataStore::StoreType::Empty; + } + Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override + { + ++m_Reads; + m_MaximumRead = std::max(m_MaximumRead, buffer.size()); + if(m_FailRead || (m_FailReadAt != 0 && m_Reads == m_FailReadAt)) + { + return MakeErrorResult(-28620, "Injected Otsu bulk-read failure"); + } + auto result = DataStore::copyIntoBuffer(start, buffer); + if(m_WarnOnFirstRead && m_Reads == 1) + { + result.warnings().push_back(Warning{.code = -28622, .message = "Injected Otsu bulk-read warning"}); + } + if(m_CancelPtr != nullptr) + { + m_CancelPtr->store(true); + } + return result; + } + Result<> copyFromBuffer(usize start, nonstd::span buffer) override + { + ++m_Writes; + if(m_FailWrite) + { + return MakeErrorResult(-28621, "Injected Otsu bulk-write failure"); + } + return DataStore::copyFromBuffer(start, buffer); + } + void failRead() noexcept + { + m_FailRead = true; + } + void failWrite() noexcept + { + m_FailWrite = true; + } + void failReadAt(usize read) noexcept + { + m_FailReadAt = read; + } + void warnOnFirstRead() noexcept + { + m_WarnOnFirstRead = true; + } + void cancelOnRead(std::atomic_bool& shouldCancel) noexcept + { + m_CancelPtr = &shouldCancel; + } + usize reads() const noexcept + { + return m_Reads; + } + usize writes() const noexcept + { + return m_Writes; + } + usize maximumRead() const noexcept + { + return m_MaximumRead; + } + +private: + bool m_FailRead = false; + bool m_WarnOnFirstRead = false; + usize m_FailReadAt = 0; + bool m_FailWrite = false; + std::atomic_bool* m_CancelPtr = nullptr; + mutable usize m_Reads = 0; + mutable usize m_Writes = 0; + mutable usize m_MaximumRead = 0; +}; +} // namespace + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains literal endpoint frequencies and labels", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize rotation = GENERATE(usize{0}, usize{1}, usize{4}); + std::vector values{0, 0, 4, 4, std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}; + std::vector expected{7, 7, 8, 8, 7, 7, 8}; + std::rotate(values.begin(), values.begin() + static_cast::difference_type>(rotation), values.end()); + std::rotate(expected.begin(), expected.begin() + static_cast::difference_type>(rotation), expected.end()); + DataStructure dataStructure; + const DataPath inputPath = recon_test::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); + const Float64Array* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.min == 0); + REQUIRE(histogramResult.value().HistogramData.max == 4); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{4, 3}); + REQUIRE(histogramResult.value().NaNCount == 1); + REQUIRE(histogramResult.value().NegativeInfinityCount == 1); + REQUIRE(histogramResult.value().PositiveInfinityCount == 1); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -8319); + REQUIRE(result.result.warnings().front().message.find("1 NaN, 1 negative infinity and 1 positive infinity") != std::string::npos); + REQUIRE(result.result.warnings().front().message.find("unreliable") != std::string::npos); + requireOtsuLabels(dataStructure, inputPath, expected); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const float64 value = GENERATE(std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, value, 4}); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, otsuArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -8324); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", bool, uint8) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern( + dataStructure, 7, 1, 1, {0, 0, 4, 4, std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}); + constexpr TestType selected = static_cast(255); + const auto maskPath = createOtsuMask(dataStructure, std::array{selected, selected, selected, selected, false, false, false}); + const uint8 maskedLabel = GENERATE(uint8{0}, uint8{255}); + const DataArray* maskPtr = nullptr; + const Float64Array* inputPtr = nullptr; + REQUIRE_NOTHROW(maskPtr = &dataStructure.getDataRefAs>(maskPath)); + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), &maskPtr->getDataStoreRef(), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{2, 2}); + REQUIRE(histogramResult.value().FiniteCount == 4); + REQUIRE(histogramResult.value().NaNCount == 0); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(maskedLabel)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireOtsuLabels(dataStructure, inputPath, {7, 7, 8, 8, maskedLabel, maskedLabel, maskedLabel}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite data is required in both modes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const uint64 policy = GENERATE(uint64{0}, uint64{1}); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 4}); + const auto maskPath = createOtsuMask(dataStructure, std::array{0, 0}); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(policy)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -8325); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask collision and tuple validation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 0, 4, 4}); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + int32 expectedCode = -8313; + SECTION("unused interior class is still reserved") + { + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(2)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(8)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}))); + } + SECTION("interval lower endpoint") + { + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(7)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}))); + } + SECTION("interval upper endpoint") + { + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(8)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}))); + } + SECTION("different tuple count") + { + expectedCode = -8314; + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1}))); + } + SECTION("full unmasked interval leaves no masked label") + { + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(256)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(255)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(0)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}))); + } + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == expectedCode); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", + int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool midpoint = GENERATE(false, true); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::lowest(), std::numeric_limits::max()}); + const DataArray* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs>(inputPath)); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{1, 1}); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(0)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ReturnBinMidpoint_Key, std::make_any(midpoint)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireOtsuLabels(dataStructure, inputPath, {0, 1}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants and detectable numeric collapse are distinct", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; + SECTION("true tiny constant is valid") + { + const DataStore input({2}, {1}, 1e-200); + DataStore output({2}, {1}, uint8{99}); + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(output.getValue(0) == 0); + REQUIRE(output.getValue(1) == 0); + } + SECTION("rounded-away marginal extension retains usable bins") + { + DataStore input({2}, {1}, 1.0); + input.setValue(1, 1.0 + (4.0 * std::numeric_limits::epsilon())); + DataStore output({2}, {1}, uint8{99}); + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{1, 1}); + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(output.getValue(0) == 0); + REQUIRE(output.getValue(1) == 1); + } + SECTION("adjacent uint64 extrema collapse only after conversion") + { + DataStore input({2}, {1}, std::numeric_limits::max()); + input.setValue(0, std::numeric_limits::max() - 1); + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8326); + } + SECTION("opposite float64 extrema overflow the range") + { + DataStore input({2}, {1}, std::numeric_limits::max()); + input.setValue(0, -std::numeric_limits::max()); + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8326); + } + SECTION("nonzero means square to zero") + { + const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 0, .max = 3e-200, .binWidth = 1e-200}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8320); + } + SECTION("finite means square to infinity") + { + const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 1e200, .max = 4e200, .binWidth = 1e200}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8320); + } + SECTION("unique rational optimum stays unchanged at ordinary scale") + { + // Doubled bin centers are [1,3,5]. Split 1 scores 2508; split 0 scores 253110/101. + // The positive exact margin is 198/101, so the unique optimum is after bin 1. + const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 0, .max = 3, .binWidth = 1}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value() == std::vector{2}); + } +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + std::atomic_bool shouldCancel{false}; + OtsuTransferStore input(8); + OtsuTransferStore mask(8); + OtsuTransferStore output(8); + for(usize i = 0; i < 8; ++i) + { + input.setValue(i, i < 4 ? 0 : 4); + mask.setValue(i, true); + } + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .TargetBytes = 8}; + int32 expectedCode = 0; + bool expectWarning = false; + bool wideRange = false; + SECTION("bounded compact route and mask output") + { + } + SECTION("input read failure") + { + input.failRead(); + expectedCode = -28620; + } + SECTION("mask read failure") + { + mask.failRead(); + expectedCode = -28620; + } + SECTION("classification read failure") + { + input.failReadAt(9); + expectedCode = -28620; + } + SECTION("wide native range uses a second bounded histogram scan") + { + wideRange = true; + for(usize i = 4; i < 8; ++i) + { + input.setValue(i, 70000); + } + } + SECTION("read warning survives a later output failure") + { + input.warnOnFirstRead(); + output.failWrite(); + expectedCode = -28621; + expectWarning = true; + } + SECTION("output write failure") + { + output.failWrite(); + expectedCode = -28621; + } + SECTION("cancellation during selected scan does not classify partial histogram") + { + input.cancelOnRead(shouldCancel); + } + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(&mask), options, shouldCancel, {}); + if(expectedCode != 0) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == expectedCode); + if(expectWarning) + { + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == -28622); + } + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + if(shouldCancel) + { + REQUIRE(output.writes() == 0); + } + else + { + REQUIRE(input.reads() > 1); + REQUIRE(mask.reads() > 1); + REQUIRE(input.maximumRead() <= 2); + REQUIRE(input.reads() == (wideRange ? 24 : 16)); + REQUIRE(output.getValue(0) == 0); + REQUIRE(output.getValue(7) == 1); + } + } +} + // ----------------------------------------------------------------------------- // (3) Validation: a data-independent guard must reject a threshold count that is 0 or >= the bin count // (which would otherwise make the backend return an empty threshold set and silently collapse the @@ -346,3 +966,316 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: ITK real-image }, "fb65e730472c8001185f355fb626ca3f"); } + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeline imports receive intentional defaults", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const OtsuMultipleThresholdsImageFilter filter; + auto json = filter.toJson(filter.getDefaultArguments()); + json.erase("nonfinite_input_handling_index"); + json.erase("use_mask_array"); + json.erase("mask_array_path"); + json.erase("masked_label"); + const auto result = filter.fromJson(json); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 4); + REQUIRE(result.value().value("nonfinite_input_handling_index") == 0); + REQUIRE_FALSE(result.value().value("use_mask_array")); + REQUIRE(result.value().value("masked_label") == 255); + const nlohmann::json legacy{{"NumberOfThresholds", 1}, + {"LabelOffset", 7}, + {"NumberOfHistogramBins", 128}, + {"ValleyEmphasis", false}, + {"SelectedCellArrayPath", {{"Data Container Name", "Image Geometry"}, {"Attribute Matrix Name", "CellData"}, {"Data Array Name", "Input"}}}, + {"NewCellArrayName", "Output"}}; + const auto legacyResult = OtsuMultipleThresholdsImageFilter::FromSIMPLJson(legacy); + SIMPLNX_RESULT_REQUIRE_VALID(legacyResult); + REQUIRE(legacyResult.value().value("nonfinite_input_handling_index") == 0); + REQUIRE_FALSE(legacyResult.value().value("use_mask_array")); + REQUIRE(legacyResult.value().value("masked_label") == 255); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 4}); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + int32 expectedCode = 0; + SECTION("wrong mask type") + { + expectedCode = FilterParameter::Constants::k_Validate_AllowedType_Error; + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1}))); + } + SECTION("multiple mask components") + { + expectedCode = FilterParameter::Constants::k_Validate_TupleShapeValue; + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}, {2}))); + } + SECTION("disabled stale mask path") + { + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(false)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(DataPath({"Does Not Exist"}))); + } + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.preflight(dataStructure, args); + if(expectedCode == 0) + { + SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); + } + else + { + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == expectedCode); + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep explicit Warn endpoints and empty selected data errors", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; + DataStore input({4}, {1}, 42.0); + input.setValue(1, std::numeric_limits::quiet_NaN()); + input.setValue(2, -std::numeric_limits::infinity()); + input.setValue(3, std::numeric_limits::infinity()); + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{3, 1}); + DataStore output({4}, {1}, uint8{99}); + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.warnings().size() == 1); + REQUIRE(output.getValue(0) == 7); + REQUIRE(output.getValue(1) == 7); + REQUIRE(output.getValue(2) == 7); + REQUIRE(output.getValue(3) == 8); + input.setValue(0, std::numeric_limits::quiet_NaN()); + const auto noFiniteResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(noFiniteResult); + REQUIRE(noFiniteResult.errors().front().code == -8325); + const DataStore empty({0}, {1}, 0.0); + const auto emptyResult = ImageProcessing::ComputeOtsuHistogram(empty, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(emptyResult); + REQUIRE(emptyResult.errors().front().code == -8325); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const std::atomic_bool shouldCancel{false}; + ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; + DataStore input({2}, {1}, 0.0); + input.setValue(1, 4.0); + SECTION("bin count") + { + options.NumberOfBins = 1; + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28600); + } + SECTION("mask size") + { + DataStore mask({1}, {1}, uint8{1}); + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(&mask), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8329); + } + SECTION("output size") + { + DataStore output({1}, {1}, uint8{0}); + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28602); + } + SECTION("label interval") + { + options.LabelOffset = 255; + DataStore output({2}, {1}, uint8{0}); + const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28603); + } + SECTION("threshold count") + { + options.NumberOfThresholds = 0; + const ImageProcessing::Histogram histogram{.counts = {1, 1}, .min = 0, .max = 4, .binWidth = 2}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28601); + } + SECTION("empty histogram") + { + const ImageProcessing::Histogram histogram{.counts = {0, 0}, .min = 0, .max = 4, .binWidth = 2}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28605); + } + SECTION("nonfinite histogram metadata") + { + const ImageProcessing::Histogram histogram{.counts = {1, 1}, .min = std::numeric_limits::infinity(), .max = std::numeric_limits::infinity(), .binWidth = 0}; + const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28606); + } + SECTION("collapsed required bin boundaries") + { + options.NumberOfBins = 128; + input.setValue(0, 1e20); + input.setValue(1, 1e20 + 32768.0); + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -8327); + } +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal class labels coexist with masked label zero", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 4, std::numeric_limits::quiet_NaN()}); + const auto maskPath = createOtsuMask(dataStructure, std::array{1, 255, 0}); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(254)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(0)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireOtsuLabels(dataStructure, inputPath, {254, 255, 0}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds keep ordinary Warn endpoint classes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern( + dataStructure, 6, 1, 1, {0, 2, 4, std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}); + const Float64Array* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 3, .NumberOfThresholds = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{3, 1, 2}); + // Three bins and two thresholds have exactly one possible split: each finite intensity gets its own class. + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(2)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + requireOtsuLabels(dataStructure, inputPath, {7, 8, 9, 7, 7, 9}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid may equal minimum with usable two-bin separation", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuCentroidSafeRed]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool midpoint = GENERATE(false, true); + REQUIRE(std::numeric_limits::is_iec559); + REQUIRE(std::numeric_limits::radix == 2); + REQUIRE(std::numeric_limits::digits == 53); + constexpr float64 epsilon = std::numeric_limits::epsilon(); + constexpr float64 minimum = 1.0; + constexpr float64 maximum = 1.0 + (2.0 * epsilon); + REQUIRE(maximum > minimum); + REQUIRE(maximum - minimum == 2.0 * epsilon); + REQUIRE(minimum + epsilon > minimum); + REQUIRE(minimum + epsilon < maximum); + REQUIRE(minimum + (0.5 * epsilon) == minimum); + REQUIRE(maximum + ((maximum - minimum) / 200.0) == maximum); + + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {minimum, maximum}); + const Float64Array* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + REQUIRE(inputPtr->getTupleShape() == ShapeType{1, 1, 2}); + REQUIRE(inputPtr->getComponentShape() == ShapeType{1}); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .ReturnBinMidpoint = midpoint}; + const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); + // Binary64 centroids are [minimum, maximum], with one finite, distinct split. Rejecting the first centroid is incorrect. + SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{1, 1}); + + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(0)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ReturnBinMidpoint_Key, std::make_any(midpoint)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const IDataArray* outputPtr = nullptr; + REQUIRE_NOTHROW(outputPtr = &dataStructure.getDataRefAs(inputPath.replaceName("Output"))); + REQUIRE(outputPtr->getDataType() == DataType::uint8); + REQUIRE(outputPtr->getTupleShape() == ShapeType{1, 1, 2}); + REQUIRE(outputPtr->getComponentShape() == ShapeType{1}); + requireOtsuLabels(dataStructure, inputPath, {0, 1}); +} + +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summaries preserve literal counts across resident and buffered batches", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool buffered = GENERATE(false, true); + const bool excludeNonfinite = GENERATE(false, true); + const usize targetBytes = GENERATE(usize{4096}, usize{64 * 1024 * 1024}); + constexpr usize valueCount = 524296; // 65,537 periods; spans multiple 262,144-value reduction blocks plus a short tail. + std::unique_ptr> inputPtr; + if(buffered) + { + inputPtr = std::make_unique>(valueCount); + } + else + { + inputPtr = std::make_unique>(ShapeType{valueCount}, ShapeType{1}, 0.0); + } + DataStore mask({valueCount}, {1}, uint8{0}); + const std::array values{0, + 4, + std::numeric_limits::quiet_NaN(), + -std::numeric_limits::infinity(), + std::numeric_limits::infinity(), + 99, + std::numeric_limits::quiet_NaN(), + -std::numeric_limits::infinity()}; + for(usize index = 0; index < valueCount; ++index) + { + const usize periodIndex = index % values.size(); + inputPtr->setValue(index, values.at(periodIndex)); + mask.setValue(index, periodIndex < (excludeNonfinite ? 2 : 5) ? uint8{255} : uint8{0}); + } + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .WarnOnNonfinite = !excludeNonfinite, .TargetBytes = targetBytes}; + const std::atomic_bool shouldCancel{false}; + const auto result = ImageProcessing::ComputeOtsuHistogram(*inputPtr, static_cast*>(&mask), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().HistogramData.min == 0); + REQUIRE(result.value().HistogramData.max == 4); + REQUIRE(result.value().FiniteCount == 131074); + REQUIRE(result.value().NaNCount == (excludeNonfinite ? 0 : 65537)); + REQUIRE(result.value().NegativeInfinityCount == (excludeNonfinite ? 0 : 65537)); + REQUIRE(result.value().PositiveInfinityCount == (excludeNonfinite ? 0 : 65537)); + // Per period, Warn contributes [0,NaN,-inf] to bin 0 and [4,+inf] to bin 1. Excluded values, including 99, contribute nothing. + const std::vector expected = excludeNonfinite ? std::vector{65537, 65537} : std::vector{196611, 131074}; + REQUIRE(result.value().HistogramData.counts == expected); + if(buffered) + { + const auto* transferPtr = dynamic_cast*>(inputPtr.get()); + REQUIRE(transferPtr != nullptr); + REQUIRE(transferPtr->reads() > 1); + REQUIRE(transferPtr->maximumRead() <= (targetBytes / (sizeof(float64) + sizeof(uint8)))); + } + // These are output/transfer-bound assertions. No thread-count or throughput claim follows from them. +} diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 64be02465d..03a5080b40 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include namespace nx::core::ImageProcessing @@ -398,27 +399,57 @@ inline bool FloatAlmostEqual(float64 x1, float64 x2, int64 maxUlps = 1) * Bin->value conversion uses the bin maximum (min + (bin+1)*binWidth), matching ITK's GetMaxs(), unless * @p returnBinMidpoint is set, in which case the bin midpoint (min + (bin+0.5)*binWidth) is used. */ -inline std::vector OtsuMultipleThresholds(const Histogram& h, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint) +namespace detail +{ +inline bool OtsuObjectiveIsUsable(float64 globalMean, const std::vector& classMean, const std::vector& classFrequency, float64 objective, bool allowZero = false) +{ + if(!std::isfinite(globalMean) || !std::isfinite(objective) || objective < 0.0) + { + return false; + } + bool hasNonzeroTerm = false; + for(usize j = 0; j < classMean.size(); ++j) + { + const float64 mean = classMean[j]; + const float64 square = mean * mean; + if(!std::isfinite(mean) || !std::isfinite(classFrequency[j]) || classFrequency[j] < 0.0 || (classFrequency[j] > 0.0 && (!std::isfinite(square) || (mean != 0.0 && square == 0.0)))) + { + return false; + } + hasNonzeroTerm = hasNonzeroTerm || (classFrequency[j] > 0.0 && square > 0.0); + } + return allowZero || objective != 0.0 || !hasNonzeroTerm; +} + +template +Result> ComputeOtsuThresholds(const Histogram& h, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint, const std::atomic_bool* shouldCancelPtr) { const usize numBins = h.counts.size(); std::vector thresholds; if(numBins == 0 || numThresholds == 0) { - return thresholds; + return {thresholds}; } if(numThresholds >= numBins) { // Not enough bins to place this many thresholds; the init loop below would index past the end. - return thresholds; + return {thresholds}; } float64 totalCount = 0.0; - for(usize c : h.counts) + for(usize bin = 0; bin < numBins; ++bin) { - totalCount += static_cast(c); + if constexpr(Checked) + { + if((bin % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) + { + return {std::vector{}}; + } + } + totalCount += static_cast(h.counts[bin]); } if(totalCount <= 0.0) { - return thresholds; + return {thresholds}; } // Per-bin measurement value == bin midpoint (ITK Histogram::GetMeasurementVector(bin)[0]). @@ -431,6 +462,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num float64 globalMean = 0.0; for(usize b = 0; b < numBins; ++b) { + if constexpr(Checked) + { + if((b % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) + { + return {std::vector{}}; + } + } globalMean += measurement(b) * frequency(b); } globalMean /= totalCount; @@ -457,6 +495,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num std::vector imgPDF(numBins, 0.0); for(usize b = 0; b < numBins; ++b) { + if constexpr(Checked) + { + if((b % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) + { + return {std::vector{}}; + } + } imgPDF[b] = frequency(b) / totalCount; } @@ -478,6 +523,15 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num } maxVarBetween /= totalCount; + // Opt-in checks leave the established arithmetic and finite tie rule unchanged. + if constexpr(Checked) + { + if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween)) + { + return MakeErrorResult>(-8320, "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); + } + } + // Valley emphasis weighting of the initial configuration. NOTE: this deliberately reproduces the ITK // quirk where the initial factor is assigned (=) per threshold rather than accumulated (+=); only the // last threshold's PDF survives. The in-loop factor below uses the correct accumulation. @@ -490,6 +544,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num } valleyEmphasisFactor = 1.0 - valleyEmphasisFactor; maxVarBetween = maxVarBetween * valleyEmphasisFactor; + if constexpr(Checked) + { + if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween, valleyEmphasisFactor == 0.0)) + { + return MakeErrorResult>(-8321, "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + } + } } // --- IncrementThresholds: advance the odometer-style threshold configuration by one step. --- @@ -557,6 +618,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num // --- Explore every configuration; keep the one with maximum (valley-weighted) between-class variance. --- while(incrementThresholds()) { + if constexpr(Checked) + { + if(shouldCancelPtr != nullptr && shouldCancelPtr->load()) + { + return {std::vector{}}; + } + } float64 varBetween = 0.0; for(usize j = 0; j < numberOfClasses; ++j) { @@ -565,6 +633,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num varBetween += classFrequency[j] * (classMean[j] * classMean[j]); } varBetween /= totalCount; + if constexpr(Checked) + { + if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween)) + { + return MakeErrorResult>(-8322, "Otsu candidate class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); + } + } if(valleyEmphasis) { @@ -575,6 +650,13 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num } valleyEmphasisFactor = 1.0 - valleyEmphasisFactor; varBetween = varBetween * valleyEmphasisFactor; + if constexpr(Checked) + { + if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween, valleyEmphasisFactor == 0.0)) + { + return MakeErrorResult>(-8323, "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + } + } } if(varBetween > maxVarBetween && !detail::FloatAlmostEqual(maxVarBetween, varBetween, 1)) @@ -591,6 +673,14 @@ inline std::vector OtsuMultipleThresholds(const Histogram& h, usize num const float64 offset = returnBinMidpoint ? 0.5 : 1.0; thresholds[t] = h.min + (static_cast(maxVarThresholdIndexes[t]) + offset) * h.binWidth; } - return thresholds; + return {std::move(thresholds)}; +} +} // namespace detail + +/** @brief Retains the established unchecked solver contract for existing direct consumers. */ +inline std::vector OtsuMultipleThresholds(const Histogram& h, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint) +{ + auto result = detail::ComputeOtsuThresholds(h, numThresholds, valleyEmphasis, returnBinMidpoint, nullptr); + return std::move(result.value()); } } // namespace nx::core::ImageProcessing diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp new file mode 100644 index 0000000000..9573a991fc --- /dev/null +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -0,0 +1,723 @@ +#pragma once + +#include "simplnx/Common/Range.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Utilities/CacheMemoryBudgetManager.hpp" +#include "simplnx/Utilities/ImageProcessing/HistogramEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" +#include "simplnx/Utilities/ParallelDataAlgorithm.hpp" +#include "simplnx/Utilities/ThrottledMessageHandler.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nx::core::ImageProcessing +{ +/** @brief Selected Otsu controls. All output class labels remain uint8. */ +struct OtsuImageOptions +{ + uint32 NumberOfBins = 128; + uint8 NumberOfThresholds = 1; + uint8 LabelOffset = 0; + uint8 MaskedLabel = 255; + bool WarnOnNonfinite = false; + bool ValleyEmphasis = false; + bool ReturnBinMidpoint = false; + usize TargetBytes = 64ULL * 1024ULL * 1024ULL; +}; + +/** @brief Histogram plus selected sample counts. Excluded samples are absent from every count. */ +struct OtsuHistogramResult +{ + Histogram HistogramData; + usize FiniteCount = 0; + usize NaNCount = 0; + usize NegativeInfinityCount = 0; + usize PositiveInfinityCount = 0; +}; + +namespace detail +{ +// Match the existing floating statistics reduction grain; batch caps also bound the summary table. +inline constexpr usize k_OtsuRangeBlockValues = 262144; +inline constexpr usize k_OtsuMaximumBatchBytes = 64ULL * 1024ULL * 1024ULL; + +template +struct OtsuFloatingRangeSummary +{ + T Minimum{}; + T Maximum{}; + usize FiniteCount = 0; + usize NaNCount = 0; + usize NegativeInfinityCount = 0; + usize PositiveInfinityCount = 0; +}; + +template +OtsuFloatingRangeSummary ReduceOtsuFloatingBlock(nonstd::span values, nonstd::span maskValues, const std::atomic_bool& shouldCancel) +{ + OtsuFloatingRangeSummary summary; + for(usize index = 0; index < values.size(); ++index) + { + if((index % 65536) == 0 && shouldCancel) + { + return summary; + } + if(!maskValues.empty() && !maskValues[index]) + { + continue; + } + const T value = values[index]; + if(!std::isfinite(value)) + { + if(std::isnan(value)) + { + ++summary.NaNCount; + } + else if(std::signbit(value)) + { + ++summary.NegativeInfinityCount; + } + else + { + ++summary.PositiveInfinityCount; + } + continue; + } + if(summary.FiniteCount == 0) + { + summary.Minimum = value; + summary.Maximum = value; + } + else + { + summary.Minimum = std::min(summary.Minimum, value); + summary.Maximum = std::max(summary.Maximum, value); + } + ++summary.FiniteCount; + } + return summary; +} + +template +void MergeOtsuFloatingSummary(OtsuFloatingRangeSummary& summary, const OtsuFloatingRangeSummary& block) +{ + if(block.FiniteCount != 0) + { + if(summary.FiniteCount == 0) + { + summary.Minimum = block.Minimum; + summary.Maximum = block.Maximum; + } + else + { + summary.Minimum = std::min(summary.Minimum, block.Minimum); + summary.Maximum = std::max(summary.Maximum, block.Maximum); + } + } + summary.FiniteCount += block.FiniteCount; + summary.NaNCount += block.NaNCount; + summary.NegativeInfinityCount += block.NegativeInfinityCount; + summary.PositiveInfinityCount += block.PositiveInfinityCount; +} + +template +OtsuFloatingRangeSummary ReduceOtsuFloatingRange(nonstd::span values, nonstd::span maskValues, const std::atomic_bool& shouldCancel) +{ + if(values.size() <= k_OtsuRangeBlockValues) + { + return ReduceOtsuFloatingBlock(values, maskValues, shouldCancel); + } + // The caller caps each resident view/owned batch. This table never has one entry per full-image block. + const usize blockCount = 1 + ((values.size() - 1) / k_OtsuRangeBlockValues); + std::vector> summaries(blockCount); + ParallelDataAlgorithm algorithm; + algorithm.setRange(0, blockCount); + algorithm.execute([&](const Range& range) { + for(usize block = range.min(); block < range.max(); ++block) + { + if(shouldCancel) + { + return; + } + const usize start = block * k_OtsuRangeBlockValues; + const usize count = std::min(k_OtsuRangeBlockValues, values.size() - start); + summaries[block] = ReduceOtsuFloatingBlock(values.subspan(start, count), maskValues.empty() ? nonstd::span{} : maskValues.subspan(start, count), shouldCancel); + } + }); + OtsuFloatingRangeSummary summary; + for(const auto& block : summaries) + { + if(shouldCancel) + { + return {}; + } + MergeOtsuFloatingSummary(summary, block); + } + return summary; +} + +struct OtsuBufferPlan +{ + CacheMemoryBudgetManager::WorkingMemoryReservation Reservation; + usize Values = 1; +}; + +inline OtsuBufferPlan ReserveOtsuBuffers(usize total, usize bytesPerValue, usize targetBytes, bool usesExternalStore, usize summaryBytesPerBlock = 0) +{ + OtsuBufferPlan plan; + const usize usefulBytes = total > std::numeric_limits::max() / bytesPerValue ? std::numeric_limits::max() : total * bytesPerValue; + usize bytes = std::min({targetBytes, k_OtsuMaximumBatchBytes, usefulBytes}); + if(usesExternalStore) + { + plan.Reservation = ReserveWorkingMemory(bytes, usefulBytes); + bytes = static_cast(std::min(plan.Reservation.sizeBytes(), std::numeric_limits::max())); + } + // One tuple is the bounded fallback when the aggregate grant cannot hold a complete tuple. + plan.Values = std::max(1, std::min(total, bytes / bytesPerValue)); + if(summaryBytesPerBlock != 0 && plan.Values > k_OtsuRangeBlockValues) + { + // Charge the preliminary summary table before choosing payload size. A smaller payload cannot need more summaries. + const usize summaryCount = 1 + ((plan.Values - 1) / k_OtsuRangeBlockValues); + const usize summaryBytes = summaryCount * summaryBytesPerBlock; + plan.Values = std::max(1, std::min(total, (bytes - summaryBytes) / bytesPerValue)); + } + const usize summaryCount = summaryBytesPerBlock != 0 && plan.Values > k_OtsuRangeBlockValues ? 1 + ((plan.Values - 1) / k_OtsuRangeBlockValues) : 0; + plan.Reservation.shrinkTo((plan.Values * bytesPerValue) + (summaryCount * summaryBytesPerBlock)); + return plan; +} + +template +Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel, const VisitorT& visitor, + bool parallelRange = false) +{ + Result<> status; + const usize total = input.getSize(); + const auto* inputPtr = dynamic_cast*>(&input); + const auto* residentMaskPtr = maskPtr == nullptr ? nullptr : dynamic_cast*>(maskPtr); + const bool direct = input.getStoreType() == IDataStore::StoreType::InMemory && inputPtr != nullptr && + (maskPtr == nullptr || (maskPtr->getStoreType() == IDataStore::StoreType::InMemory && residentMaskPtr != nullptr)); + if(direct) + { + const auto values = inputPtr->createSpan(); + const auto maskValues = residentMaskPtr == nullptr ? nonstd::span{} : residentMaskPtr->createSpan(); + const usize blockValues = parallelRange ? std::max(1, std::min(options.TargetBytes, k_OtsuMaximumBatchBytes) / (sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)))) : 65536; + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + return status; + } + const usize count = std::min(blockValues, total - start); + status = MergeResults(std::move(status), visitor(values.subspan(start, count), maskPtr == nullptr ? nonstd::span{} : maskValues.subspan(start, count))); + if(status.invalid()) + { + return status; + } + start += count; + } + return status; + } + + auto plan = ReserveOtsuBuffers(total, sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)), options.TargetBytes, + input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore), + parallelRange ? sizeof(OtsuFloatingRangeSummary) : 0); + auto valuesPtr = std::make_unique_for_overwrite(plan.Values); + auto maskValuesPtr = maskPtr == nullptr ? nullptr : std::make_unique_for_overwrite(plan.Values); + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + return status; + } + const usize count = std::min(plan.Values, total - start); + if(maskPtr != nullptr) + { + Result<> readMaskResult = maskPtr->copyIntoBuffer(start, nonstd::span(maskValuesPtr.get(), count)); + status = MergeResults(std::move(status), std::move(readMaskResult)); + if(status.invalid()) + { + return status; + } + } + if(shouldCancel) + { + return status; + } + Result<> readResult = input.copyIntoBuffer(start, nonstd::span(valuesPtr.get(), count)); + status = MergeResults(std::move(status), std::move(readResult)); + if(status.invalid()) + { + return status; + } + if(shouldCancel) + { + return status; + } + status = MergeResults(std::move(status), + visitor(nonstd::span(valuesPtr.get(), count), maskPtr == nullptr ? nonstd::span{} : nonstd::span(maskValuesPtr.get(), count))); + if(status.invalid()) + { + return status; + } + start += count; + } + return status; +} + +inline Result<> ConfigureCheckedOtsuBins(Histogram& histogram, bool nativeConstant, const std::atomic_bool& shouldCancel) +{ + if(nativeConstant) + { + histogram.binWidth = 0.0; + return {}; + } + const float64 range = histogram.max - histogram.min; + const float64 numBins = static_cast(histogram.counts.size()); + const float64 upper = histogram.max + (range / (numBins * 100.0)); + histogram.binWidth = (upper - histogram.min) / numBins; + if(!std::isfinite(range) || range <= 0.0 || !std::isfinite(upper) || upper <= histogram.min || !std::isfinite(histogram.binWidth) || histogram.binWidth <= 0.0 || + !std::isfinite(1.0 / histogram.binWidth)) + { + return MakeErrorResult(-8326, "Otsu cannot form a finite positive histogram range/bin width from the selected native values. Rescale the finite input or use fewer histogram bins."); + } + float64 previousBoundary = histogram.min; + float64 previousMeasurement = histogram.min; + for(usize bin = 0; bin < histogram.counts.size(); ++bin) + { + if((bin % 65536) == 0 && shouldCancel) + { + return {}; + } + const float64 boundary = histogram.min + ((static_cast(bin) + 1.0) * histogram.binWidth); + const float64 measurement = histogram.min + ((static_cast(bin) + 0.5) * histogram.binWidth); + if(!std::isfinite(boundary) || !std::isfinite(measurement) || boundary <= previousBoundary || (bin != 0 && measurement <= previousMeasurement)) + { + return MakeErrorResult(-8327, "Otsu histogram boundaries or measurements collapse at the selected precision. Rescale the finite input or use fewer histogram bins."); + } + previousBoundary = boundary; + previousMeasurement = measurement; + } + return {}; +} + +template +struct OtsuFrequencyEntry +{ + T Value{}; + usize Count = 0; +}; + +template +Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T maxValue, usize frequency = 1) +{ + usize bin = 0; + if(value >= maxValue && minValue != maxValue) + { + bin = histogram.counts.size() - 1; + } + else if(value > minValue && minValue != maxValue) + { + const float64 coordinate = (static_cast(value) - histogram.min) * (1.0 / histogram.binWidth); + const float64 lastBin = static_cast(histogram.counts.size() - 1); + if(!std::isfinite(coordinate) || coordinate < 0.0) + { + return MakeErrorResult(-8328, "Otsu produced an unusable histogram coordinate for a selected finite value. Rescale the input before thresholding."); + } + // Narrow only a finite value already inside the bin-index range. Endpoint rounding cannot escape the histogram. + bin = coordinate >= lastBin ? histogram.counts.size() - 1 : static_cast(coordinate); + } + histogram.counts[bin] += frequency; + return {}; +} +} // namespace detail + +/** + * @brief Builds the selected Otsu histogram without changing generic histogram consumers. + * @details Mask selection precedes nonfinite/range checks. Compact native integral ranges retain exact fixed-ring + * frequencies; other ranges use a second bounded pass. A valid cancellation is returned to the owning filter. + */ +template +Result ComputeOtsuHistogram(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) +{ + if(options.NumberOfBins < 2) + { + return MakeErrorResult(-28600, "Otsu requires at least two histogram bins. Increase Number Of Histogram Bins."); + } + if(static_cast(options.NumberOfBins) > std::vector{}.max_size()) + { + return MakeErrorResult(-28604, "Otsu histogram bin storage exceeds the addressable allocation size. Reduce Number Of Histogram Bins."); + } + if(input.getNumberOfComponents() != 1 || (maskPtr != nullptr && (maskPtr->getNumberOfComponents() != 1 || maskPtr->getSize() != input.getSize()))) + { + return MakeErrorResult(-8329, "Otsu input and optional mask must be scalar and contain the same number of tuples. Select a scalar mask with one value per input tuple."); + } + OtsuHistogramResult result; + if(shouldCancel) + { + return {std::move(result)}; + } + result.HistogramData.counts.assign(options.NumberOfBins, 0); + T minValue{}; + T maxValue{}; + constexpr usize ringSize = 65536; + std::unique_ptr[]> frequenciesPtr; + CacheMemoryBudgetManager::WorkingMemoryReservation ringReservation; + using CompactRangeType = std::conditional_t, bool, const bool>; + CompactRangeType compactRange = false; + if constexpr(std::is_integral_v) + { + const bool external = input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore); + const usize ringBytes = ringSize * sizeof(detail::OtsuFrequencyEntry); + if(external) + { + ringReservation = ReserveWorkingMemory(ringBytes, ringBytes); + } + compactRange = !external || ringReservation.sizeBytes() >= ringBytes; + if(compactRange) + { + frequenciesPtr = std::make_unique[]>(ringSize); + } + else + { + ringReservation = {}; + } + } + Result<> rangeResult = detail::VisitOtsuValues( + input, maskPtr, options, shouldCancel, + [&](nonstd::span values, nonstd::span maskValues) -> Result<> { + if constexpr(std::is_floating_point_v) + { + const auto summary = detail::ReduceOtsuFloatingRange(values, maskValues, shouldCancel); + if(shouldCancel) + { + return {}; + } + if(!options.WarnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) + { + return MakeErrorResult(-8324, "Otsu found a selected NaN or infinity. Choose Warn and Continue to retain endpoint-bin participation, or explicitly exclude it with Use Mask Array."); + } + if(summary.FiniteCount != 0) + { + if(result.FiniteCount == 0) + { + minValue = summary.Minimum; + maxValue = summary.Maximum; + } + else + { + minValue = std::min(minValue, summary.Minimum); + maxValue = std::max(maxValue, summary.Maximum); + } + } + result.FiniteCount += summary.FiniteCount; + result.NaNCount += summary.NaNCount; + result.NegativeInfinityCount += summary.NegativeInfinityCount; + result.PositiveInfinityCount += summary.PositiveInfinityCount; + return {}; + } + else + { + for(usize i = 0; i < values.size(); ++i) + { + if((i % 65536) == 0 && shouldCancel) + { + return {}; + } + if(maskPtr != nullptr && !maskValues[i]) + { + continue; + } + const T value = values[i]; + if(result.FiniteCount == 0) + { + minValue = value; + maxValue = value; + } + else + { + minValue = std::min(minValue, value); + maxValue = std::max(maxValue, value); + } + ++result.FiniteCount; + if constexpr(std::is_integral_v) + { + if(compactRange) + { + using KeyType = detail::UnsignedIntegralType; + const KeyType key = detail::IntegralMonotonicKey(value); + compactRange = (detail::IntegralMonotonicKey(maxValue) - detail::IntegralMonotonicKey(minValue)) <= static_cast(ringSize - 1); + if(compactRange) + { + auto& entry = frequenciesPtr[static_cast(key & static_cast(ringSize - 1))]; + if(entry.Count != 0 && entry.Value != value) + { + compactRange = false; + } + else + { + entry.Value = value; + ++entry.Count; + } + } + } + } + } + return {}; + } + }, + std::is_floating_point_v); + if(rangeResult.invalid()) + { + return ConvertInvalidResult(std::move(rangeResult)); + } + if(shouldCancel) + { + return ConvertResultTo(std::move(rangeResult), std::move(result)); + } + if(result.FiniteCount == 0) + { + return ConvertResultTo( + MergeResults(std::move(rangeResult), MakeErrorResult(-8325, "Otsu has no selected finite samples. Select at least one finite input value with the optional mask before thresholding.")), + std::move(result)); + } + auto& histogram = result.HistogramData; + histogram.min = static_cast(minValue); + histogram.max = static_cast(maxValue); + Result<> geometryResult = detail::ConfigureCheckedOtsuBins(histogram, minValue == maxValue, shouldCancel); + if(geometryResult.invalid()) + { + return ConvertResultTo(MergeResults(std::move(rangeResult), std::move(geometryResult)), std::move(result)); + } + if(shouldCancel) + { + return ConvertResultTo(std::move(rangeResult), std::move(result)); + } + if(minValue == maxValue) + { + histogram.counts.front() = result.FiniteCount; + } + else if(compactRange) + { + for(usize i = 0; i < ringSize; ++i) + { + if(shouldCancel) + { + return ConvertResultTo(std::move(rangeResult), std::move(result)); + } + const auto& entry = frequenciesPtr[i]; + if(entry.Count != 0) + { + Result<> addResult = detail::AccumulateCheckedOtsuValue(histogram, entry.Value, minValue, maxValue, entry.Count); + if(addResult.invalid()) + { + return ConvertResultTo(MergeResults(std::move(rangeResult), std::move(addResult)), std::move(result)); + } + } + } + } + else + { + frequenciesPtr.reset(); + ringReservation = {}; + Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { + for(usize i = 0; i < values.size(); ++i) + { + if((i % 65536) == 0 && shouldCancel) + { + return {}; + } + if((maskPtr != nullptr && !maskValues[i]) || !std::isfinite(static_cast(values[i]))) + { + continue; + } + Result<> addResult = detail::AccumulateCheckedOtsuValue(histogram, values[i], minValue, maxValue); + if(addResult.invalid()) + { + return addResult; + } + } + return {}; + }); + rangeResult = MergeResults(std::move(rangeResult), std::move(histogramResult)); + if(rangeResult.invalid()) + { + return ConvertInvalidResult(std::move(rangeResult)); + } + } + histogram.counts.front() += result.NaNCount + result.NegativeInfinityCount; + histogram.counts.back() += result.PositiveInfinityCount; + return ConvertResultTo(std::move(rangeResult), std::move(result)); +} + +/** @brief Runs the existing finite solver with opt-in numeric checks and cooperative cancellation. */ +inline Result> ComputeCheckedOtsuThresholds(const Histogram& histogram, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) +{ + if(shouldCancel) + { + return {std::vector{}}; + } + if(options.NumberOfThresholds == 0 || options.NumberOfThresholds >= histogram.counts.size()) + { + return MakeErrorResult>(-28601, "Otsu Number Of Thresholds must be positive and less than the histogram bin count."); + } + if(std::ranges::all_of(histogram.counts, [](usize count) { return count == 0; })) + { + return MakeErrorResult>(-28605, "Otsu threshold calculation requires at least one histogram sample."); + } + if(!std::isfinite(histogram.min) || !std::isfinite(histogram.max) || !std::isfinite(histogram.binWidth) || histogram.binWidth < 0.0) + { + return MakeErrorResult>(-28606, + "Otsu threshold calculation requires finite histogram bounds and a nonnegative finite bin width. Rebuild the histogram from selected finite data."); + } + if(histogram.binWidth == 0.0 && histogram.min == histogram.max) + { + return {std::vector(options.NumberOfThresholds, histogram.min)}; + } + return detail::ComputeOtsuThresholds(histogram, options.NumberOfThresholds, options.ValleyEmphasis, options.ReturnBinMidpoint, &shouldCancel); +} + +/** @brief Maps selected values to ordinary uint8 classes; equality stays in the lower class. */ +template +uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const OtsuImageOptions& options) +{ + if constexpr(std::is_floating_point_v) + { + if(!std::isfinite(value)) + { + return std::isinf(value) && value > 0 ? static_cast(static_cast(options.LabelOffset) + options.NumberOfThresholds) : options.LabelOffset; + } + } + const usize index = static_cast(std::ranges::lower_bound(thresholds, static_cast(value)) - thresholds.begin()); + return static_cast(static_cast(options.LabelOffset) + index); +} + +/** @brief Applies a checked Otsu result; an optional mask is read in flat tuple order in bounded owned chunks. */ +template +Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore& output, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) +{ + if(input.getSize() != output.getSize() || output.getNumberOfComponents() != 1) + { + return MakeErrorResult(-28602, "Otsu output must be scalar and match the input tuple count. Recreate the output array before thresholding."); + } + if(static_cast(options.LabelOffset) + options.NumberOfThresholds > 255 || + (maskPtr != nullptr && options.MaskedLabel >= options.LabelOffset && static_cast(options.MaskedLabel) <= static_cast(options.LabelOffset) + options.NumberOfThresholds)) + { + return MakeErrorResult(-28603, "Otsu labels do not fit a distinct uint8 class interval. Reduce Label Offset/Number Of Thresholds or select a Masked Label outside that interval."); + } + auto histogramResult = ComputeOtsuHistogram(input, maskPtr, options, shouldCancel); + if(histogramResult.invalid()) + { + return ConvertResult(std::move(histogramResult)); + } + if(shouldCancel) + { + return ConvertResult(std::move(histogramResult)); + } + Result<> result; + result.warnings() = std::move(histogramResult.warnings()); + const auto& counts = histogramResult.value(); + if(counts.NaNCount != 0 || counts.NegativeInfinityCount != 0 || counts.PositiveInfinityCount != 0) + { + result.warnings().push_back(Warning{.code = -8319, + .message = fmt::format("Otsu retained {} NaN, {} negative infinity and {} positive infinity samples in the endpoint histogram bins. Classification can be " + "unreliable or unpredictable. Use an explicit mask to exclude these samples.", + counts.NaNCount, counts.NegativeInfinityCount, counts.PositiveInfinityCount)}); + } + auto thresholdsResult = ComputeCheckedOtsuThresholds(histogramResult.value().HistogramData, options, shouldCancel); + if(thresholdsResult.invalid()) + { + return MergeResults(std::move(result), ConvertResult(std::move(thresholdsResult))); + } + if(shouldCancel) + { + return result; + } + const auto& thresholds = thresholdsResult.value(); + if(maskPtr == nullptr) + { + result = MergeResults(std::move(result), ApplyPointwise(input, output, [&](T value) { return ClassifyOtsuValue(value, thresholds, options); }, shouldCancel, messageHandler)); + } + else + { + auto plan = detail::ReserveOtsuBuffers(input.getSize(), sizeof(T) + sizeof(MaskT) + sizeof(uint8), options.TargetBytes, + input.getStoreType() == IDataStore::StoreType::OutOfCore || maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore || + output.getStoreType() == IDataStore::StoreType::OutOfCore); + auto valuesPtr = std::make_unique_for_overwrite(plan.Values); + auto maskValuesPtr = std::make_unique_for_overwrite(plan.Values); + auto labelsPtr = std::make_unique_for_overwrite(plan.Values); + messageHandler.sendInfoMessage("Applying masked Otsu labels in bounded batches"); + ThrottledMessageHandler progress(messageHandler); + progress.reset(1 + ((input.getSize() - 1) / plan.Values), "Applying masked Otsu labels"); + for(usize start = 0; start < input.getSize();) + { + if(shouldCancel) + { + return result; + } + const usize count = std::min(plan.Values, input.getSize() - start); + Result<> maskReadResult = maskPtr->copyIntoBuffer(start, nonstd::span(maskValuesPtr.get(), count)); + result = MergeResults(std::move(result), std::move(maskReadResult)); + if(result.invalid()) + { + return result; + } + if(shouldCancel) + { + return result; + } + Result<> inputReadResult = input.copyIntoBuffer(start, nonstd::span(valuesPtr.get(), count)); + result = MergeResults(std::move(result), std::move(inputReadResult)); + if(result.invalid()) + { + return result; + } + if(shouldCancel) + { + return result; + } + ParallelDataAlgorithm algorithm; + algorithm.setRange(0, count); + algorithm.execute([&](const Range& range) { + for(usize i = range.min(); i < range.max(); ++i) + { + if((i % 65536) == 0 && shouldCancel) + { + return; + } + labelsPtr[i] = maskValuesPtr[i] ? ClassifyOtsuValue(valuesPtr[i], thresholds, options) : options.MaskedLabel; + } + }); + if(shouldCancel) + { + return result; + } + Result<> writeResult = output.copyFromBuffer(start, nonstd::span(labelsPtr.get(), count)); + result = MergeResults(std::move(result), std::move(writeResult)); + if(result.invalid()) + { + return result; + } + start += count; + progress.incrementPercent(1, 1); + } + } + return result; +} +} // namespace nx::core::ImageProcessing From 9bd44692f9d82baba9b3c449ca52d3d1c2cfed7c Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 02:43:24 -0400 Subject: [PATCH 20/98] BUG: Guard nonfinite color exports and native normalization Add linked Error and Warn choices for unmasked nonfinite color-table input. Validate the global range before committing any slice, preserve masked finite range values and raw writing, and use writer-scoped native-distance and scaled floating normalization. Document first-color fallback, migration, precision and per-slice failure limits. Validated paired full Windows Release builds and 58 InCore/62 OOC cases. Independent decoded RGB, native/subnormal interiors, raw floating files, slice layouts, all-slice sentinel preservation, I/O failures and real disk/mixed backing pass. Source review and clang-format 19.1.1 pass. Configured clang-tidy remains nonpassing with documented existing/framework and lexical diagnostics; Debug, cross-platform and measured peak memory are not claimed. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/CMakeLists.txt | 3 +- .../docs/WriteImageFilter.md | 31 +- .../Filters/Algorithms/WriteImage.hpp | 3 +- .../Algorithms/WriteImageColorUtilities.hpp | 309 +++++++++ .../Filters/Algorithms/WriteImageDirect.cpp | 115 ++-- .../Filters/Algorithms/WriteImageScanline.cpp | 134 ++-- .../Filters/WriteImageFilter.cpp | 10 +- .../Filters/WriteImageFilter.hpp | 3 +- .../NXImageProcessing/test/WriteImageTest.cpp | 650 ++++++++++++++++++ 9 files changed, 1121 insertions(+), 137 deletions(-) create mode 100644 src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp diff --git a/src/Plugins/NXImageProcessing/CMakeLists.txt b/src/Plugins/NXImageProcessing/CMakeLists.txt index b32628e482..7bef20a849 100644 --- a/src/Plugins/NXImageProcessing/CMakeLists.txt +++ b/src/Plugins/NXImageProcessing/CMakeLists.txt @@ -130,6 +130,8 @@ if(MSVC) endif() set(PLUGIN_EXTRA_SOURCES + "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/Filters/Algorithms/WriteImageColorUtilities.hpp" + "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/MathOutputUtilities.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/ImageFilterExecutors.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/ImageFilterTemplateExecutors.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/ImageFilterExecutorDetail.hpp" @@ -144,7 +146,6 @@ set(PLUGIN_EXTRA_SOURCES "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/GaussianImageFilterExecutors.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/ComponentImageFilterExecutors.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/WatershedImageFilterExecutors.cpp" - "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/MathOutputUtilities.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/FijiMontageUtilities.hpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/FijiMontageUtilities.cpp" "${${PLUGIN_NAME}_SOURCE_DIR}/src/${PLUGIN_NAME}/utils/MetaImageUtilities.hpp" diff --git a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md index 34055a068a..0ad15a8d36 100644 --- a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md @@ -34,7 +34,7 @@ If an existing pipeline contains the ITK Write Image filter UUID, the NXImagePro If the input array represents a 3D volume, the filter will output a series of slices along one of the orthogonal axes. The options are to produce XY slices along the Z axis, XZ slices along the Y axis, or YZ slices along the X axis. The output files will be numbered sequentially starting at the *Index Offset* and ending at *Index Offset + (dim - 1)* for the chosen axis. For example, if the Z axis has 117 dimensions and *Index Offset* is 0, 117 XY image files will be produced and numbered 000 through 116. If the volume produces only a single slice, no index is appended and the file is written with exactly the user-specified name. -Writes are performed through an `AtomicFile`, so partially-written slices from an aborted run will not corrupt existing output files. +The filter encodes each slice in a temporary file, then commits that slice through `AtomicFile`. This is a separate operation for each slice. An unrelated failure in a later slice can leave earlier completed slices in the output folder. File replacement can also fail if the operating system cannot rename the temporary file. ### Plane @@ -62,12 +62,25 @@ This option only affects the written image files. The input Image Geometry and i ### Inline Color Table (Optional) -Enabling *Create Color Table* converts a single-component **Data Array** to an RGB image using a selected color preset immediately before writing, so there is no need to run the Create Color Map **Filter** as a separate step beforehand. The results are identical to running Create Color Map followed by Write Image. +Enabling *Create Color Table* converts a single-component **Data Array** to an RGB image using the selected color preset. The filter checks the color range before it writes the first slice. The nonfinite policy below applies to this inline color conversion. - *Select Preset...* chooses the color preset (for example grayscale, rainbow, or jet) that is applied to the array's values. - When *Create Color Table* is enabled, the **Input Image Data Array** must be a single-component numeric array, but it may be **any** numeric type: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, or float64. This is wider than the type restrictions that apply when writing an array's raw pixel values directly (see *Pixel Data Type Support By Format* above), since color-table mode always produces a uint8 RGB image regardless of the input array's type. If the selected array has more than one component, the filter will fail during preflight. -- Colorization uses bounded source groups and writes one RGB slice at a time. The filter never holds a full RGB volume in memory. The filter first computes the minimum and maximum input values while ignoring the mask. It uses this range to normalize each voxel against the preset's control points. -- If every voxel in the input array has the same value (a constant array), every voxel normalizes to the same value and is colored using the preset's first control color. +- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar and optional mask pages. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. +- If the finite range is constant, each unmasked voxel uses the preset's first control color. Masked voxels use the mask color. + +#### Nonfinite Input Handling + +This choice is active only when *Create Color Table* is enabled: + +- **Error** (default) reports an execution error for an unmasked NaN, positive infinity, or negative infinity. The check runs during the initial scan, before any slice file is written or replaced. This also protects existing files when the invalid value occurs in a later slice. +- **Warn and Continue** uses the preset's first control color for each unmasked nonfinite value. The filter returns one warning with the number of these values. It uses the finite values for the color range. The fallback can look identical to a valid finite minimum; it is not the preset's separate NaN color. + +The mask is applied before this policy. A masked nonfinite value uses *Masked Color (RGB)* and does not trigger the policy or its count. Finite masked values still participate in the range. Both choices report an error if no finite values remain, even if all nonfinite values are masked. A finite constant range remains valid. An entirely masked finite array also remains valid and uses the mask color. + +Older pipelines now use **Error** unless they explicitly select **Warn and Continue**. To export data with unmasked NaN or infinity, repair the data, mask those pixels, or select the warning choice deliberately. Raw floating TIFF and MHA exports do not use this policy. + +The writer keeps native integer endpoints and forms unsigned distances before floating conversion. This preserves adjacent int64 and uint64 ranges and avoids signed overflow. Floating normalization avoids overflowing differences across finite extrema. The normalized coordinate is float32, so interior colors still have finite precision. The existing linear interpolation and truncating conversion to uint8 channels remain in use. Different values can produce the same RGB bytes. Corrected extreme-range colors can differ from older exports. *Use Mask Array* is a separate, top-level toggle (it is not nested under *Create Color Table*) that assigns a fixed color to "bad" voxels instead of coloring them from the preset: @@ -103,13 +116,13 @@ the padded size. ## Algorithm -The filter selects an algorithm from the storage type of the input **Data Array** and the optional mask. Both algorithms produce the same files and preserve the same cancellation, flip, color-table, scale-bar, and atomic-write behavior. +The filter selects an algorithm from the storage type of the input **Data Array** and the optional mask. Both algorithms use the same color scan, nonfinite policy and normalization. They retain the existing slice extraction, flip, scale-bar and per-slice write paths. ### In-Core Path `WriteImageDirect` runs when all applicable arrays use in-memory storage. The algorithm uses the original per-slice extraction path. This path preserves the lowest measured in-memory execution time. -The algorithm reads one selected XY, XZ, or YZ slice. It converts the slice to the required byte layout and writes the slice before it reads the next slice. Color-table output first scans the scalar array in fixed pages to calculate the normalization range. It then colorizes one source slice and its optional mask at a time. +The algorithm reads one selected XY, XZ, or YZ slice. It converts the slice to the required byte layout and writes the slice before it reads the next slice. Color-table output first scans scalar and optional mask pages with a combined target of 1 MiB. It releases these pages before it colorizes one source slice and its optional mask at a time. ### Out-of-Core Path @@ -117,7 +130,11 @@ The algorithm reads one selected XY, XZ, or YZ slice. It converts the slice to t XY slices match the default out-of-core Z-slice chunk shape. The algorithm reads one XY slice per extent to prevent unnecessary chunk scatter. For XZ and YZ output, the algorithm groups adjacent output slices into a source slab. The slab size uses an active working-memory reservation with a preferred limit of 64 MiB. If one slice exceeds this limit, the algorithm processes one slice at a time. -The algorithm keeps the grouped source slab and one output slice in memory. A color-table mask shares the same slab plan. The algorithm performs store I/O on one thread and checks cancellation between output slices. +The algorithm keeps the grouped source slab and one output slice in memory. A color-table mask shares the same slab plan. The initial scalar/mask scan uses bounded pages before the slab allocation. The algorithm performs store I/O on one thread and checks cancellation between output slices. + +The filter does not allocate a complete RGB volume. It still requires a complete selected 2D RGB slice. A scale bar can require a second padded slice and a separate band. The image codec can allocate more memory for that slice. The source-buffer reservation is not a limit on total process memory. + +Cancellation can leave slices that were committed earlier. Execution errors and warnings are returned through the filter result. The shared filter wrapper can replace a valid execution result with cancellation and lose its execution warnings; this inherited limit does not change the initial nonfinite-error file-preservation guarantee. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp index 622334d918..752c803fa1 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp @@ -28,6 +28,7 @@ struct NXIMAGEPROCESSING_EXPORT WriteImageInputValues DataPath imageGeometryPath; DataPath imageDataArrayPath; bool createColorTable = false; + uint64 NonfiniteInputHandling = 0; std::string presetName; bool useMask = false; DataPath maskArrayPath; @@ -62,7 +63,7 @@ class NXIMAGEPROCESSING_EXPORT WriteImage /** * @brief Writes all slices for the selected plane. - * @return An error if slice extraction, compression, or file output fails. + * @return Color-policy warnings or an error from the initial scan, slice extraction, compression, or file output. */ Result<> operator()(); diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp new file mode 100644 index 0000000000..df80bc6c4d --- /dev/null +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -0,0 +1,309 @@ +#pragma once + +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** @namespace nx::core::WriteImageColor + * @brief Contains color safety operations used only by the image writer. + */ +namespace nx::core::WriteImageColor +{ +/** + * @brief Checks the products used by color slice, group and RGB indexes. + * @tparam T Scalar input type. + * @param width Output slice width. + * @param height Output slice height. + * @param slices Number of output slices. + * @param hasMask Whether the source group also holds a mask. + * @return An error for empty dimensions or unrepresentable buffer sizes. + */ +template +Result<> ValidateSize(usize width, usize height, usize slices, bool hasMask) +{ + constexpr usize maximum = std::numeric_limits::max(); + const usize bytesPerValue = sizeof(T) + (hasMask ? std::max(sizeof(bool), sizeof(uint8)) : 0); + if(width == 0 || height == 0 || slices == 0 || width > maximum / height) + { + return MakeErrorResult(-27030, "Write Image color slice dimensions are empty or too large. Reduce the image dimensions."); + } + const usize pixels = width * height; + if(pixels > maximum / 3 || pixels > maximum / bytesPerValue || slices > maximum / (pixels * bytesPerValue)) + { + return MakeErrorResult(-27032, "Write Image color buffer sizes exceed the supported index range. Reduce the image dimensions."); + } + return {}; +} + +/** + * @struct Range + * @brief Native endpoints and cancellation state from the writer's initial scan. + * @tparam T Scalar input type. + */ +template +struct Range +{ + T Minimum{}; + T Maximum{}; + bool Cancelled = false; +}; + +/** + * @brief Scans scalar and optional mask pages before any slice is opened. + * Finite masked values participate in the range. Masked nonfinite values do not + * trigger the policy, and no nonfinite value participates in the range. + * @tparam T Scalar input type. + * @param array Single-component source array. + * @param maskArray Optional single-component bool or uint8 mask. + * @param policy Error (0) or Warn and Continue (1). + * @param shouldCancel Stops the scan between pages. + * @param reportRange Synchronous callback after each complete page. + * @return Native range and warnings, cancellation state, or the first scan error. + */ +template +Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, uint64 policy, const std::atomic_bool& shouldCancel, const std::function& reportRange) +{ + const auto& store = array.getIDataStoreRefAs>(); + const usize count = store.getNumberOfTuples(); + if(count == 0) + { + return MakeErrorResult>(-27020, "Input array is empty."); + } + constexpr usize targetPageBytes = 1024 * 1024; + const usize maskBytes = maskArray == nullptr ? 0 : (maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8)); + const usize pageCount = std::min(count, std::max(1, targetPageBytes / (sizeof(T) + maskBytes))); + auto values = std::make_unique(pageCount); + std::unique_ptr boolMask; + std::unique_ptr byteMask; + if(maskArray != nullptr) + { + if(maskArray->getDataType() == DataType::boolean) + { + boolMask = std::make_unique(pageCount); + } + else + { + byteMask = std::make_unique(pageCount); + } + } + Range range; + Result<> status; + bool initialized = false; + usize nonfiniteCount = 0; + for(usize offset = 0; offset < count;) + { + if(shouldCancel) + { + range.Cancelled = true; + return ConvertResultTo>(std::move(status), std::move(range)); + } + const usize size = std::min(pageCount, count - offset); + status = MergeResults(std::move(status), store.copyIntoBuffer(offset, nonstd::span(values.get(), size))); + if(status.invalid()) + { + return ConvertInvalidResult>(std::move(status)); + } + if(boolMask != nullptr) + { + status = MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().copyIntoBuffer(offset, nonstd::span(boolMask.get(), size))); + } + else if(byteMask != nullptr) + { + status = MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().copyIntoBuffer(offset, nonstd::span(byteMask.get(), size))); + } + if(status.invalid()) + { + return ConvertInvalidResult>(std::move(status)); + } + for(usize index = 0; index < size; ++index) + { + const T value = values[index]; + if constexpr(std::is_floating_point_v) + { + if(!std::isfinite(value)) + { + const bool selected = boolMask != nullptr ? boolMask[index] : (byteMask == nullptr || byteMask[index] != 0); + if(selected) + { + if(policy == 0) + { + return ConvertResultTo>( + MergeResults(std::move(status), MakeErrorResult(-27027, fmt::format("Write Image array '{}' has an unmasked nonfinite value at tuple {}. Repair or mask this value, or select 'Warn " + "and Continue' in Nonfinite Input Handling. No image slice was written.", + array.getName(), offset + index))), + Range{}); + } + ++nonfiniteCount; + } + continue; + } + } + if(!initialized) + { + range.Minimum = value; + range.Maximum = value; + initialized = true; + } + else + { + range.Minimum = std::min(range.Minimum, value); + range.Maximum = std::max(range.Maximum, value); + } + } + offset += size; + if(reportRange) + { + reportRange(offset, count); + } + } + if(shouldCancel) + { + range.Cancelled = true; + return ConvertResultTo>(std::move(status), std::move(range)); + } + if(nonfiniteCount != 0) + { + status.warnings().push_back({.code = -27031, + .message = fmt::format("Write Image array '{}' contains {} unmasked nonfinite value(s). They use the preset's first control color, which is also used for the finite " + "minimum. The color range uses finite values only.", + array.getName(), nonfiniteCount)}); + } + if(!initialized) + { + return ConvertResultTo>(MergeResults(std::move(status), MakeErrorResult(-27028, fmt::format("Write Image array '{}' has no finite values for its color range. Supply at least one finite " + "value; masking nonfinite values does not create a finite range. No image slice was written.", + array.getName()))), + Range{}); + } + return ConvertResultTo>(std::move(status), std::move(range)); +} + +/** + * @brief Normalizes native distances without signed overflow or endpoint collapse. + * @tparam T Scalar input type. + * @param value Input value; nonfinite values use the first control color. + * @param minimum Finite native minimum. + * @param maximum Finite native maximum. + * @return Finite float32 coordinate in [0, 1]. + */ +template +float32 Normalize(T value, T minimum, T maximum) +{ + if constexpr(std::is_floating_point_v) + { + if(!std::isfinite(value)) + { + return 0.0F; + } + } + if(minimum == maximum || value <= minimum) + { + return 0.0F; + } + if(value >= maximum) + { + return 1.0F; + } + float64 fraction = 0; + if constexpr(std::is_same_v) + { + fraction = value ? 1.0 : 0.0; + } + else if constexpr(std::is_integral_v) + { + // Unsigned subtraction is defined modulo 2^N. Since minimum <= value <= + // maximum, both distances fit the corresponding unsigned native type. + using UnsignedType = std::make_unsigned_t; + const auto numerator = static_cast(static_cast(value) - static_cast(minimum)); + const auto denominator = static_cast(static_cast(maximum) - static_cast(minimum)); + fraction = static_cast(numerator) / static_cast(denominator); + } + else + { + const float64 low = static_cast(minimum); + const float64 high = static_cast(maximum); + const float64 current = static_cast(value); + if(low < 0 && high > 0 && high > std::numeric_limits::max() + low) + { + fraction = (current * 0.5 - low * 0.5) / (high * 0.5 - low * 0.5); + } + else + { + fraction = (current - low) / (high - low); + } + } + return static_cast(std::clamp(fraction, 0.0, 1.0)); +} + +/** + * @brief Validates the palette data the writer will index and interpolate. + * @param points Packed position/RGB controls. + * @param bins Normalized control positions. + * @return An error for invalid sizes, nonfinite values or unordered positions. + */ +inline Result<> ValidatePalette(const std::vector& points, const std::vector& bins) +{ + if(points.size() < 8 || points.size() % 4 != 0 || bins.size() != points.size() / 4 || !std::all_of(points.begin(), points.end(), [](float32 value) { return std::isfinite(value); }) || + !std::all_of(bins.begin(), bins.end(), [](float32 value) { return std::isfinite(value); }) || !std::is_sorted(bins.begin(), bins.end())) + { + return MakeErrorResult(-27029, "Write Image requires at least two finite color controls in position order. Select a valid color preset."); + } + return {}; +} + +/** + * @brief Uses the existing linear palette interpolation and truncating RGB bytes. + * Checked endpoints and usize indexes avoid invalid segment/channel casts. + * The caller validates the palette once before entering the pixel loop. + * @param value Finite normalized coordinate in [0, 1]. + * @param bins Validated normalized control positions. + * @param points Validated packed position/RGB controls. + * @return Three uint8 channels, with the existing truncation rule. + */ +inline std::array ComputeRgb(float32 value, const std::vector& bins, const std::vector& points) +{ + const usize last = bins.size() - 1; + usize left = 0; + usize right = 0; + float32 fraction = 0; + if(value >= 1) + { + left = last; + right = last; + } + else if(value > 0) + { + right = static_cast(std::lower_bound(bins.begin(), bins.end(), value) - bins.begin()); + right = std::min(last, std::max(1, right)); + left = right - 1; + const float32 width = bins[right] - bins[left]; + if(width > 0) + { + fraction = std::clamp((value - bins[left]) / width, 0.0F, 1.0F); + } + } + std::array rgb{}; + for(usize channel = 0; channel < rgb.size(); ++channel) + { + const float64 scaled = (points[left * 4 + channel + 1] * (1.0 - fraction) + points[right * 4 + channel + 1] * fraction) * 255; + rgb.at(channel) = static_cast(std::clamp(scaled, 0.0, 255.0)); + } + return rgb; +} +} // namespace nx::core::WriteImageColor diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 37ed0f935b..80ae6b4ead 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -1,7 +1,11 @@ #include "WriteImageDirect.hpp" +#include "WriteImageColorUtilities.hpp" + #include "simplnx/Common/AtomicFile.hpp" #include "simplnx/Common/Extent.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" @@ -20,11 +24,13 @@ #include #include +#include #include #include #include #include #include +#include #include namespace fs = std::filesystem; @@ -173,7 +179,7 @@ Result> ReadMaskSlice(const IDataArray* maskArray, const Exte const std::vector& boolMask = maskResult.value(); std::vector mask(boolMask.size()); std::transform(boolMask.cbegin(), boolMask.cend(), mask.begin(), [](bool value) { return value ? uint8{1} : uint8{0}; }); - return {std::move(mask)}; + return ConvertResultTo>(ConvertResult(std::move(maskResult)), std::move(mask)); } const auto& maskStore = maskArray->getIDataStoreRefAs>(); return maskStore.readExtent(extent); @@ -192,92 +198,66 @@ struct ColorizeVolumeFunctor // The operation calculates the range in pages and then colorizes bounded slices. template Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, usize sliceCount, usize sliceW, usize sliceH, const std::vector& binPoints, - const std::vector& controlPoints, usize numControlColors, const IDataArray* maskArray, const std::vector& invalidColor, const std::atomic_bool& shouldCancel, + const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const std::function& reportRange = {}, const std::function& reportRows = {}) { - const auto& dataStore = dataArrayRef.template getIDataStoreRefAs>(); - const usize numTuples = dataStore.getNumberOfTuples(); - if(numTuples == 0) + if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceW, sliceH, sliceCount, maskArray != nullptr); sizeResult.invalid()) { - return MakeErrorResult(-27020, "Input array is empty."); + return sizeResult; } - - // Include masked values to match Create Color Map range semantics. - constexpr usize k_TargetPageBytes = 1024 * 1024; - const usize pageElements = std::max(1, std::min(numTuples, k_TargetPageBytes / sizeof(T))); - auto valuePage = std::make_unique(pageElements); - T arrayMin{}; - T arrayMax{}; - bool initialized = false; - for(usize offset = 0; offset < numTuples; offset += pageElements) + auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, reportRange); + if(rangeResult.invalid()) { - if(shouldCancel) - { - return {}; - } - const usize count = std::min(pageElements, numTuples - offset); - if(Result<> result = dataStore.copyIntoBuffer(offset, nonstd::span(valuePage.get(), count)); result.invalid()) - { - return result; - } - for(usize i = 0; i < count; ++i) - { - if(!initialized) - { - arrayMin = valuePage[i]; - arrayMax = valuePage[i]; - initialized = true; - } - else - { - arrayMin = std::min(arrayMin, valuePage[i]); - arrayMax = std::max(arrayMax, valuePage[i]); - } - } - if(reportRange) - { - reportRange(offset + count, numTuples); - } + return ConvertResult(std::move(rangeResult)); } - + const auto range = rangeResult.value(); + Result<> status = ConvertResult(std::move(rangeResult)); + if(range.Cancelled) + { + return status; + } + const auto& dataStore = dataArrayRef.template getIDataStoreRefAs>(); std::vector sliceBuffer(sliceW * sliceH * 3); for(usize slice = 0; slice < sliceCount; ++slice) { if(shouldCancel) { - return {}; + return status; } const Extent sliceExtent = CreateSliceExtent(planeIndex, slice, dimX, dimY, dimZ); Result> sliceResult = dataStore.readExtent(sliceExtent); if(sliceResult.invalid()) { - return ConvertResult(std::move(sliceResult)); + return MergeResults(std::move(status), ConvertResult(std::move(sliceResult))); } + status.warnings().insert(status.warnings().end(), sliceResult.warnings().begin(), sliceResult.warnings().end()); const std::vector& sliceValues = sliceResult.value(); const usize pixelCount = sliceW * sliceH; if(sliceValues.size() != pixelCount) { - return MakeErrorResult(-27024, fmt::format("Could not bulk-read colorized image slice {}. Expected {} values but received {}.", slice, pixelCount, sliceValues.size())); + return MergeResults(std::move(status), + MakeErrorResult(-27024, fmt::format("Could not bulk-read colorized image slice {}. Expected {} values but received {}.", slice, pixelCount, sliceValues.size()))); } Result> maskResult = ReadMaskSlice(maskArray, sliceExtent); if(maskResult.invalid()) { - return ConvertResult(std::move(maskResult)); + return MergeResults(std::move(status), ConvertResult(std::move(maskResult))); } + status.warnings().insert(status.warnings().end(), maskResult.warnings().begin(), maskResult.warnings().end()); const std::vector& maskValues = maskResult.value(); if(maskArray != nullptr && maskValues.size() != pixelCount) { - return MakeErrorResult(-27025, fmt::format("Could not bulk-read mask slice {}. Expected {} values but received {}.", slice, pixelCount, maskValues.size())); + return MergeResults(std::move(status), MakeErrorResult(-27025, fmt::format("Could not bulk-read mask slice {}. Expected {} values but received {}.", slice, pixelCount, maskValues.size()))); } for(usize row = 0; row < sliceH; ++row) { if(shouldCancel) { - return {}; + return status; } for(usize col = 0; col < sliceW; ++col) { @@ -292,8 +272,8 @@ struct ColorizeVolumeFunctor } const T sourceValue = sliceValues[dstPixel]; - const float32 nValue = ColorTableUtilities::NormalizeValue(sourceValue, arrayMin, arrayMax); - const std::array rgb = ColorTableUtilities::ComputeRgbFromControlPoints(nValue, binPoints, controlPoints, numControlColors); + const float32 nValue = WriteImageColor::Normalize(sourceValue, range.Minimum, range.Maximum); + const std::array rgb = WriteImageColor::ComputeRgb(nValue, binPoints, controlPoints); sliceBuffer[dst + 0] = rgb[0]; sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; @@ -304,13 +284,13 @@ struct ColorizeVolumeFunctor } } - auto result = writeSlice(sliceBuffer, slice); - if(result.invalid()) + status = MergeResults(std::move(status), writeSlice(sliceBuffer, slice)); + if(status.invalid()) { - return result; + return status; } } - return {}; + return status; } }; } // namespace @@ -476,15 +456,29 @@ Result<> WriteImageDirect::operator()() m_MessageHandler.sendInfoMessage("Encoding image file"); } auto writeResult = imageIO->writePixelData(atomicFile.tempFilePath(), *writeBufferPtr, metadata); + if(m_InputValues.createColorTable) + { + Result<> fileStatus; + fileStatus.warnings() = std::move(atomicFileResult.warnings()); + writeResult = MergeResults(std::move(fileStatus), std::move(writeResult)); + } if(writeResult.invalid()) { return writeResult; } if(m_ShouldCancel) { + if(m_InputValues.createColorTable) + { + return MergeResults(std::move(writeResult), MakeErrorResult(-1, "Filter cancelled")); + } return MakeErrorResult(-1, "Filter cancelled"); } auto commitResult = atomicFile.commit(); + if(m_InputValues.createColorTable) + { + commitResult = MergeResults(std::move(writeResult), std::move(commitResult)); + } if(commitResult.valid()) { progressThrottle.updateCount(slice + 1); @@ -515,7 +509,10 @@ Result<> WriteImageDirect::operator()() return MakeErrorResult(-27022, fmt::format("Preset '{}' must define at least 2 control colors", m_InputValues.presetName)); } std::vector binPoints = ColorTableUtilities::NormalizeBinPoints(controlPoints); - const usize numControlColors = controlPoints.size() / 4; + if(Result<> paletteResult = WriteImageColor::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) + { + return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(paletteResult)); + } const IDataArray* maskArrayPtr = nullptr; if(m_InputValues.useMask) @@ -523,8 +520,10 @@ Result<> WriteImageDirect::operator()() maskArrayPtr = m_DataStructure.getDataAs(m_InputValues.maskArrayPath); } - return ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, binPoints, controlPoints, numControlColors, - maskArrayPtr, m_InputValues.invalidColor, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), reportRange, reportRows); + auto colorResult = + ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, binPoints, controlPoints, maskArrayPtr, + m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), reportRange, reportRows); + return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } // The non-color path preserves typed bytes for one slice at a time. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index ccc2d70569..09b8268664 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -1,8 +1,13 @@ #include "WriteImageScanline.hpp" +#include "WriteImageColorUtilities.hpp" + #include "simplnx/Common/AtomicFile.hpp" #include "simplnx/Common/Extent.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" @@ -26,6 +31,7 @@ #include #include #include +#include #include namespace fs = std::filesystem; @@ -367,9 +373,9 @@ struct ColorizeVolumeFunctor * @param dataArrayPath Identifies the scalar source array. * @param binPoints Normalized color-control positions. * @param controlPoints Color-control values. - * @param numControlColors Number of control colors. * @param maskArray Optional Boolean or uint8 mask array. * @param invalidColor RGB value for masked pixels. + * @param nonfinitePolicy Error (0) or Warn and Continue (1) for unmasked nonfinite input. * @param shouldCancel Stops processing between output slices. * @param writeSlice Writes one packed RGB output slice. * @param reportRange Optional synchronous callback for completed range-scan values. @@ -378,57 +384,26 @@ struct ColorizeVolumeFunctor */ template Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, usize sliceCount, usize sliceW, usize sliceH, const DataPath& dataArrayPath, - const std::vector& binPoints, const std::vector& controlPoints, usize numControlColors, const IDataArray* maskArray, const std::vector& invalidColor, + const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const std::function& reportRange = {}, const std::function& reportRows = {}) { - const auto& dataStore = dataArrayRef.template getIDataStoreRefAs>(); - const usize numTuples = dataStore.getNumberOfTuples(); - if(numTuples == 0) + if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceW, sliceH, sliceCount, maskArray != nullptr); sizeResult.invalid()) { - return MakeErrorResult(-27020, "Input array is empty."); + return sizeResult; } - - // Global min/max over the whole array (mask ignored) — matches Create Color Map semantics. - constexpr usize k_TargetPageBytes = 1024 * 1024; - const usize pageElements = std::max(1, std::min(numTuples, k_TargetPageBytes / sizeof(T))); - auto valuePage = std::make_unique(pageElements); - T arrayMin{}; - T arrayMax{}; - bool initialized = false; - for(usize offset = 0; offset < numTuples; offset += pageElements) + auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, reportRange); + if(rangeResult.invalid()) { - if(shouldCancel) - { - return {}; - } - const usize count = std::min(pageElements, numTuples - offset); - if(Result<> result = dataStore.copyIntoBuffer(offset, nonstd::span(valuePage.get(), count)); result.invalid()) - { - return result; - } - for(usize i = 0; i < count; ++i) - { - if(!initialized) - { - arrayMin = valuePage[i]; - arrayMax = valuePage[i]; - initialized = true; - } - else - { - arrayMin = std::min(arrayMin, valuePage[i]); - arrayMax = std::max(arrayMax, valuePage[i]); - } - } - if(reportRange) - { - reportRange(offset + count, numTuples); - } + return ConvertResult(std::move(rangeResult)); } - - valuePage.reset(); - + const auto range = rangeResult.value(); + Result<> status = ConvertResult(std::move(rangeResult)); + if(range.Cancelled) + { + return status; + } + const auto& dataStore = dataArrayRef.template getIDataStoreRefAs>(); const usize pixelCount = sliceW * sliceH; const uint64 sourceSliceBytes = static_cast(pixelCount) * sizeof(T); const uint64 maskSliceBytes = maskArray == nullptr ? 0 : static_cast(pixelCount); @@ -438,10 +413,12 @@ struct ColorizeVolumeFunctor const usize slicesPerGroup = CalculateSlicesPerGroup(planeIndex, sliceCount, groupSliceBytes, workingMemory.sizeBytes()); if(slicesPerGroup == 0) { - return MakeErrorResult(-27026, - fmt::format("Write Image cannot reserve one source slice for array '{}'. The scalar and mask buffers require {} bytes, but the working-memory manager granted {} bytes. " - "Increase the cache memory budget or reduce the image slice dimensions.", - dataArrayPath.toString(), groupSliceBytes, workingMemory.sizeBytes())); + return MergeResults( + std::move(status), + MakeErrorResult(-27026, + fmt::format("Write Image cannot reserve one source slice for array '{}'. The scalar and mask buffers require {} bytes, but the working-memory manager granted {} bytes. " + "Increase the cache memory budget or reduce the image slice dimensions.", + dataArrayPath.toString(), groupSliceBytes, workingMemory.sizeBytes()))); } std::unique_ptr groupValues = std::make_unique(slicesPerGroup * pixelCount); @@ -461,11 +438,11 @@ struct ColorizeVolumeFunctor std::vector sliceBuffer(sliceW * sliceH * 3); - for(usize firstSlice = 0; firstSlice < sliceCount; firstSlice += slicesPerGroup) + for(usize firstSlice = 0; firstSlice < sliceCount;) { if(shouldCancel) { - return {}; + return status; } const usize groupSliceCount = std::min(slicesPerGroup, sliceCount - firstSlice); @@ -473,22 +450,27 @@ struct ColorizeVolumeFunctor const Extent groupExtent = CreateSliceGroupExtent(planeIndex, firstSlice, groupSliceCount, dimX, dimY, dimZ); // A failed read leaves the group buffers holding stale or uninitialized values. // Stop here so the writer never commits those values as image pixels. - if(Result<> result = dataStore.readExtentIntoBuffer(groupExtent, nonstd::span(groupValues.get(), groupValueCount)); result.invalid()) + status = MergeResults(std::move(status), dataStore.readExtentIntoBuffer(groupExtent, nonstd::span(groupValues.get(), groupValueCount))); + if(status.invalid()) { - return result; + return status; } if(booleanMaskValues != nullptr) { - if(Result<> result = maskArray->getIDataStoreRefAs>().readExtentIntoBuffer(groupExtent, nonstd::span(booleanMaskValues.get(), groupValueCount)); result.invalid()) + status = + MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().readExtentIntoBuffer(groupExtent, nonstd::span(booleanMaskValues.get(), groupValueCount))); + if(status.invalid()) { - return result; + return status; } } else if(uint8MaskValues != nullptr) { - if(Result<> result = maskArray->getIDataStoreRefAs>().readExtentIntoBuffer(groupExtent, nonstd::span(uint8MaskValues.get(), groupValueCount)); result.invalid()) + status = + MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().readExtentIntoBuffer(groupExtent, nonstd::span(uint8MaskValues.get(), groupValueCount))); + if(status.invalid()) { - return result; + return status; } } @@ -496,14 +478,14 @@ struct ColorizeVolumeFunctor { if(shouldCancel) { - return {}; + return status; } for(usize row = 0; row < sliceH; ++row) { if(shouldCancel) { - return {}; + return status; } for(usize col = 0; col < sliceW; ++col) { @@ -520,8 +502,8 @@ struct ColorizeVolumeFunctor } const T sourceValue = groupValues[groupValueIndex]; - const float32 nValue = ColorTableUtilities::NormalizeValue(sourceValue, arrayMin, arrayMax); - const std::array rgb = ColorTableUtilities::ComputeRgbFromControlPoints(nValue, binPoints, controlPoints, numControlColors); + const float32 nValue = WriteImageColor::Normalize(sourceValue, range.Minimum, range.Maximum); + const std::array rgb = WriteImageColor::ComputeRgb(nValue, binPoints, controlPoints); sliceBuffer[dst + 0] = rgb[0]; sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; @@ -532,13 +514,15 @@ struct ColorizeVolumeFunctor } } - if(Result<> result = writeSlice(sliceBuffer, firstSlice + localSliceIndex); result.invalid()) + status = MergeResults(std::move(status), writeSlice(sliceBuffer, firstSlice + localSliceIndex)); + if(status.invalid()) { - return result; + return status; } } + firstSlice += groupSliceCount; } - return {}; + return status; } }; } // namespace @@ -706,11 +690,21 @@ Result<> WriteImageScanline::operator()() m_MessageHandler.sendInfoMessage("Encoding image file"); } auto writeResult = imageIO->writePixelData(atomicFile.tempFilePath(), *writeBufferPtr, metadata); + if(m_InputValues.createColorTable) + { + Result<> fileStatus; + fileStatus.warnings() = std::move(atomicFileResult.warnings()); + writeResult = MergeResults(std::move(fileStatus), std::move(writeResult)); + } if(writeResult.invalid()) { return writeResult; } auto commitResult = atomicFile.commit(); + if(m_InputValues.createColorTable) + { + commitResult = MergeResults(std::move(writeResult), std::move(commitResult)); + } if(commitResult.valid()) { progressThrottle.updateCount(slice + 1); @@ -741,7 +735,10 @@ Result<> WriteImageScanline::operator()() return MakeErrorResult(-27022, fmt::format("Preset '{}' must define at least 2 control colors", m_InputValues.presetName)); } std::vector binPoints = ColorTableUtilities::NormalizeBinPoints(controlPoints); - const usize numControlColors = controlPoints.size() / 4; + if(Result<> paletteResult = WriteImageColor::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) + { + return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(paletteResult)); + } const IDataArray* maskArrayPtr = nullptr; if(m_InputValues.useMask) @@ -749,9 +746,10 @@ Result<> WriteImageScanline::operator()() maskArrayPtr = m_DataStructure.getDataAs(m_InputValues.maskArrayPath); } - return ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, m_InputValues.imageDataArrayPath, binPoints, - controlPoints, numControlColors, maskArrayPtr, m_InputValues.invalidColor, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), reportRange, - reportRows); + auto colorResult = ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, m_InputValues.imageDataArrayPath, + binPoints, controlPoints, maskArrayPtr, m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, + std::function(std::vector&, usize)>(writeSlice), reportRange, reportRows); + return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } return ExecuteDataFunction(WriteDirectVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, nComp, m_InputValues.imageDataArrayPath, diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp index 466585ec6c..8bce53b253 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include namespace fs = std::filesystem; @@ -113,6 +114,10 @@ Parameters WriteImageFilter::parameters() const "When enabled, the single-component input array is converted to RGB using the selected preset before writing.", false)); params.insert( std::make_unique(k_SelectedPreset_Key, "Select Preset...", "Select a preset color scheme to apply to the input array", ColorTableUtilities::GetDefaultRGBPresetName())); + params.insert(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", + "Error stops before any slice is written if an unmasked value is NaN or infinity. Warn and Continue uses the first control color for those values " + "and reports their count. Masked finite values still participate in the finite color range.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertSeparator(Parameters::Separator{"Scale Bar (Optional)"}); params.insert(std::make_unique(k_AddScaleBar_Key, "Add Physical Scale Bar", @@ -128,6 +133,7 @@ Parameters WriteImageFilter::parameters() const params.insert(std::make_unique(k_LeadingDigitCharacter_Key, "Fill Character", "The character to use for the leading digits if needed", "0")); params.linkParameters(k_CreateColorTable_Key, k_SelectedPreset_Key, true); + params.linkParameters(k_CreateColorTable_Key, k_NonfiniteInputHandling_Key, true); // The mask is independent of color-table creation. // A linkable group cannot be the child of another linkable group. // Thus, the mask control stays at the top level and controls the mask parameters. @@ -144,7 +150,8 @@ IFilter::VersionType WriteImageFilter::parametersVersion() const // Version 3: Widened input array to all numeric types (color-table mode colorizes any numeric type). // Version 4: Added optional output-image flip (flip_mode_index). // Version 5: Added optional physical scale bar (add_scale_bar). - return 5; + // Version 6: Added color-table-only nonfinite input handling, defaulting to Error. + return 6; } //------------------------------------------------------------------------------ @@ -351,6 +358,7 @@ Result<> WriteImageFilter::executeImpl(DataStructure& dataStructure, const Argum inputValues.imageGeometryPath = filterArgs.value(k_ImageGeomPath_Key); inputValues.imageDataArrayPath = filterArgs.value(k_ImageArrayPath_Key); inputValues.createColorTable = filterArgs.value(k_CreateColorTable_Key); + inputValues.NonfiniteInputHandling = inputValues.createColorTable ? filterArgs.value(k_NonfiniteInputHandling_Key) : 0; inputValues.presetName = filterArgs.value(k_SelectedPreset_Key); inputValues.useMask = filterArgs.value(k_UseMask_Key); inputValues.maskArrayPath = filterArgs.value(k_MaskArrayPath_Key); diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp index c8206bcc84..a04b91e388 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp @@ -32,6 +32,7 @@ class NXIMAGEPROCESSING_EXPORT WriteImageFilter : public IFilter static constexpr StringLiteral k_TotalIndexDigits_Key = "total_index_digits"; static constexpr StringLiteral k_LeadingDigitCharacter_Key = "leading_digit_character"; static constexpr StringLiteral k_CreateColorTable_Key = "create_color_table"; + static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; static constexpr StringLiteral k_SelectedPreset_Key = "selected_preset"; static constexpr StringLiteral k_UseMask_Key = "use_mask"; static constexpr StringLiteral k_MaskArrayPath_Key = "mask_array_path"; @@ -115,7 +116,7 @@ class NXIMAGEPROCESSING_EXPORT WriteImageFilter : public IFilter * @param messageHandler Receives slice progress messages. * @param shouldCancel Stops output between slices when set. * @param executionContext Resolves relative file paths. - * @return An error if slice extraction, compression, or file output fails. + * @return Color-policy warnings or an error from the initial scan, slice extraction, compression, or file output. */ Result<> executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const override; diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 342760a1c6..972146e2fc 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -1,5 +1,7 @@ #include +#include +#include "NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp" #include "NXImageProcessing/Filters/ReadImageFilter.hpp" #include "NXImageProcessing/Filters/ReadImageStackFilter.hpp" #include "NXImageProcessing/Filters/ReadMhaFileFilter.hpp" @@ -7,11 +9,21 @@ #include "NXImageProcessing/NXImageProcessing_test_dirs.hpp" #include "SimplnxCore/Filters/CreateColorMapFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Extent.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Core/Application.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Filter/Parameters.hpp" #include "simplnx/Parameters/BoolParameter.hpp" #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/CreateColorMapParameter.hpp" @@ -19,18 +31,31 @@ #include "simplnx/Parameters/NumberParameter.hpp" #include "simplnx/Parameters/StringParameter.hpp" #include "simplnx/Parameters/VectorParameter.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/ColorTableUtilities.hpp" +#include "simplnx/Utilities/ImageIO/IImageIO.hpp" +#include "simplnx/Utilities/ImageIO/ImageIOFactory.hpp" #include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" +#include +#include #include #include +#include +#include #include +#include #include +#include +#include +#include #include +#include #include #include #include +#include namespace fs = std::filesystem; @@ -329,6 +354,631 @@ IFilter::PreflightResult RunFormatPreflightForType(const std::string& fileName, } // namespace +TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") +{ + UnitTest::LoadPlugins(); + const WriteImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string policyKey = "nonfinite_input_handling_index"; + + // Inspect the public schema without sending nonfinite values through the baseline writer. + REQUIRE(parameters.contains(policyKey)); + const auto* policyPtr = dynamic_cast(parameters.at(policyKey).get()); + REQUIRE(policyPtr != nullptr); + REQUIRE(policyPtr->humanName() == "Nonfinite Input Handling"); + REQUIRE(policyPtr->choices() == ChoicesParameter::Choices{"Error", "Warn and Continue"}); + REQUIRE(policyPtr->defaultIndex() == 0); + const Arguments arguments = filter.getDefaultArguments(); + REQUIRE(arguments.value(policyKey) == 0); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is active only for color-table output", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") +{ + UnitTest::LoadPlugins(); + const WriteImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string policyKey = "nonfinite_input_handling_index"; + + REQUIRE(parameters.contains(policyKey)); + REQUIRE(parameters.contains(WriteImageFilter::k_CreateColorTable_Key)); + Arguments arguments = filter.getDefaultArguments(); + REQUIRE_FALSE(arguments.value(WriteImageFilter::k_CreateColorTable_Key)); + REQUIRE_FALSE(parameters.isParameterActive(policyKey, arguments)); + arguments.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(true)); + REQUIRE(parameters.isParameterActive(policyKey, arguments)); + arguments.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(false)); + REQUIRE_FALSE(parameters.isParameterActive(policyKey, arguments)); +} + +namespace +{ +template +DataPath createWriteImageNumericInput(DataStructure& dataStructure, const std::array& values, const ShapeType& shape = {1, 1, Size}) +{ + auto* geometryPtr = ImageGeom::Create(dataStructure, "NumericImage"); + REQUIRE(geometryPtr != nullptr); + geometryPtr->setDimensions({shape.at(2), shape.at(1), shape.at(0)}); + auto* cellsPtr = AttributeMatrix::Create(dataStructure, "CellData", shape, geometryPtr->getId()); + REQUIRE(cellsPtr != nullptr); + geometryPtr->setCellData(*cellsPtr); + auto* arrayPtr = UnitTest::CreateTestDataArray(dataStructure, "Input", shape, {1}, cellsPtr->getId()); + REQUIRE(arrayPtr != nullptr); + REQUIRE(arrayPtr->getDataType() == GetDataType()); + REQUIRE(arrayPtr->getTupleShape() == shape); + REQUIRE(arrayPtr->getNumberOfTuples() == Size); + for(usize index = 0; index < Size; ++index) + { + arrayPtr->getDataStoreRef().setValue(index, values.at(index)); + } + return DataPath({"NumericImage", "CellData", "Input"}); +} + +template +DataPath createWriteImageNumericMask(DataStructure& dataStructure, const std::array& values) +{ + const DataPath cellsPath({"NumericImage", "CellData"}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(cellsPath)); + const auto& cells = dataStructure.getDataRefAs(cellsPath); + auto* maskPtr = UnitTest::CreateTestDataArray(dataStructure, "Mask", cells.getShape(), {1}, cells.getId()); + REQUIRE(maskPtr != nullptr); + REQUIRE(maskPtr->getNumberOfTuples() == Size); + for(usize index = 0; index < Size; ++index) + { + maskPtr->getDataStoreRef().setValue(index, values.at(index)); + } + return cellsPath.createChildPath("Mask"); +} + +Arguments writeImageNumericArguments(const fs::path& output, uint64 policy = 0, const std::string& preset = "Grayscale", bool useMask = false) +{ + const WriteImageFilter filter; + Arguments arguments = filter.getDefaultArguments(); + arguments.insertOrAssign(WriteImageFilter::k_ImageGeomPath_Key, std::make_any(DataPath({"NumericImage"}))); + arguments.insertOrAssign(WriteImageFilter::k_ImageArrayPath_Key, std::make_any(DataPath({"NumericImage", "CellData", "Input"}))); + arguments.insertOrAssign(WriteImageFilter::k_FileName_Key, std::make_any(output)); + arguments.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(true)); + arguments.insertOrAssign(WriteImageFilter::k_NonfiniteInputHandling_Key, std::make_any(policy)); + arguments.insertOrAssign(WriteImageFilter::k_SelectedPreset_Key, std::make_any(preset)); + arguments.insertOrAssign(WriteImageFilter::k_UseMask_Key, std::make_any(useMask)); + arguments.insertOrAssign(WriteImageFilter::k_MaskArrayPath_Key, std::make_any(DataPath({"NumericImage", "CellData", "Mask"}))); + arguments.insertOrAssign(WriteImageFilter::k_InvalidColorValue_Key, std::make_any>(std::vector{10, 20, 30})); + return arguments; +} + +std::vector readWriteImageNumericPng(const fs::path& path, usize width, usize height) +{ + auto imageIoResult = CreateImageIO(path); + SIMPLNX_RESULT_REQUIRE_VALID(imageIoResult); + REQUIRE(imageIoResult.value() != nullptr); + auto metadata = imageIoResult.value()->readMetadata(path); + SIMPLNX_RESULT_REQUIRE_VALID(metadata); + REQUIRE(metadata.value().width == width); + REQUIRE(metadata.value().height == height); + REQUIRE(metadata.value().dataType == DataType::uint8); + REQUIRE(metadata.value().numComponents == 3); + REQUIRE(metadata.value().numPages == 1); + std::vector bytes(width * height * 3); + auto readResult = imageIoResult.value()->readPixelData(path, std::span(bytes)); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + return bytes; +} + +template +void requireWriteImageGray(UnitTest::AlgorithmTestScope& scope, const std::array& values, const std::array& expected) +{ + DataStructure dataStructure; + const DataPath inputPath = createWriteImageNumericInput(dataStructure, values); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + scope.requireExpectedStore(dataStructure.getDataRefAs(inputPath)); + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "numeric.png")); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + const auto bytes = readWriteImageNumericPng(output.path() / "numeric.png", Size, 1); + for(usize index = 0; index < Size; ++index) + { + for(usize channel = 0; channel < 3; ++channel) + { + REQUIRE(bytes.at((index * 3) + channel) == expected.at(index)); + } + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +std::vector readWriteImageSentinel(const fs::path& path) +{ + std::ifstream input(path, std::ios::binary); + REQUIRE(input.is_open()); + return {std::istreambuf_iterator(input), std::istreambuf_iterator()}; +} + +template +class WriteImageReadProbeStore : public DataStore +{ +public: + WriteImageReadProbeStore(const ShapeType& shape, bool failFlat, bool failExtent) + : DataStore(shape, ShapeType{1}, T{}) + , m_FailFlat(failFlat) + , m_FailExtent(failExtent) + { + } + ~WriteImageReadProbeStore() noexcept override = default; + WriteImageReadProbeStore(const WriteImageReadProbeStore&) = delete; + WriteImageReadProbeStore(WriteImageReadProbeStore&&) = delete; + WriteImageReadProbeStore& operator=(const WriteImageReadProbeStore&) = delete; + WriteImageReadProbeStore& operator=(WriteImageReadProbeStore&&) = delete; + Result<> copyIntoBuffer(usize offset, nonstd::span values) const override + { + m_FlatReadCount++; + m_MaximumReadBytes = std::max(m_MaximumReadBytes, values.size() * sizeof(T)); + if(m_FailFlat) + { + return MakeErrorResult(-27901, "Controlled Write Image page read failure."); + } + auto result = DataStore::copyIntoBuffer(offset, values); + if(result.valid()) + { + result.warnings().push_back({-27903, "Controlled Write Image page read warning."}); + } + return result; + } + Result> readExtent(const Extent& extent) const override + { + if(m_FailExtent) + { + return MakeErrorResult>(-27902, "Controlled Write Image slice read failure."); + } + return DataStore::readExtent(extent); + } + Result<> readExtentIntoBuffer(const Extent& extent, nonstd::span values) const override + { + if(m_FailExtent) + { + return MakeErrorResult(-27902, "Controlled Write Image slice read failure."); + } + return DataStore::readExtentIntoBuffer(extent, values); + } + usize flatReadCount() const + { + return m_FlatReadCount; + } + usize maximumReadBytes() const + { + return m_MaximumReadBytes; + } + +private: + bool m_FailFlat = false; + bool m_FailExtent = false; + mutable usize m_FlatReadCount = 0; + mutable usize m_MaximumReadBytes = 0; +}; +} // namespace + +TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: native endpoints decode to literal RGB", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64, float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + requireWriteImageGray(scope, std::array{std::numeric_limits::lowest(), std::numeric_limits::max()}, std::array{0, 255}); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: wide and tiny interiors decode without native range collapse", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const std::array expected{0, 127, 255}; + requireWriteImageGray(scope, std::array{-std::numeric_limits::max(), 0, std::numeric_limits::max()}, expected); + requireWriteImageGray(scope, std::array{-std::numeric_limits::max(), 0, std::numeric_limits::max()}, expected); + constexpr int64 minimum = std::numeric_limits::min(); + constexpr int64 maximum = std::numeric_limits::max(); + constexpr uint64 unsignedMaximum = std::numeric_limits::max(); + requireWriteImageGray(scope, std::array{minimum, minimum + 1, minimum + 2}, expected); + requireWriteImageGray(scope, std::array{maximum - 2, maximum - 1, maximum}, expected); + requireWriteImageGray(scope, std::array{unsignedMaximum - 2, unsignedMaximum - 1, unsignedMaximum}, expected); + requireWriteImageGray(scope, std::array{0, std::numeric_limits::min(), 2 * std::numeric_limits::min()}, expected); + requireWriteImageGray(scope, std::array{0, 1, 2, 3}, std::array{0, 85, 170, 255}); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a distinct runtime witness", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const float64 tiny = std::numeric_limits::denorm_min(); + const std::array values{tiny, tiny * 2, tiny * 3}; + REQUIRE(std::fpclassify(values.at(0)) == FP_SUBNORMAL); + REQUIRE(std::fpclassify(values.at(1)) == FP_SUBNORMAL); + REQUIRE(std::fpclassify(values.at(2)) == FP_SUBNORMAL); + REQUIRE(values.at(0) < values.at(1)); + REQUIRE(values.at(1) < values.at(2)); + requireWriteImageGray(scope, values, std::array{0, 127, 255}); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite masked range decode literally", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool booleanMask = GENERATE(false, true); + const uint64 policy = GENERATE(uint64{0}, uint64{1}); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, + std::array{0, 3, 6, 9, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}); + if(booleanMask) + { + createWriteImageNumericMask(dataStructure, std::array{false, true, true, true, false, false, false}); + } + else + { + createWriteImageNumericMask(dataStructure, std::array{0, 2, 1, 1, 0, 0, 0}); + } + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "mask.png", policy, "Grayscale", true)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + const std::vector expected{10, 20, 30, 85, 85, 85, 170, 170, 170, 255, 255, 255, 10, 20, 30, 10, 20, 30, 10, 20, 30}; + REQUIRE(readWriteImageNumericPng(output.path() / "mask.png", 7, 1) == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the first actual control color", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool useMask = GENERATE(false, true); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, + std::array{std::numeric_limits::quiet_NaN(), 0, std::numeric_limits::infinity(), 2, -std::numeric_limits::infinity()}); + createWriteImageNumericMask(dataStructure, std::array{useMask ? uint8{0} : uint8{1}, 1, 1, 1, 1}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "warn.png", 1, "X Ray", useMask)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -27031); + REQUIRE(result.result.warnings().front().message.find(useMask ? "2 unmasked" : "3 unmasked") != std::string::npos); + std::vector expected{255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255}; + if(useMask) + { + expected.at(0) = 10; + expected.at(1) = 20; + expected.at(2) = 30; + } + REQUIRE(readWriteImageNumericPng(output.path() / "warn.png", 5, 1) == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: late strict nonfinite preserves every existing slice byte", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const usize invalidKind = GENERATE(usize{0}, usize{1}, usize{2}); + UnitTest::AlgorithmTestScope scope(scenario); + const std::array invalidValues{std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}; + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{0, 1, 2, 3, 4, 5, 6, invalidValues.at(invalidKind)}, {2, 2, 2}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const std::array names{"slice_000.png", "slice_001.png"}; + const std::array sentinels{"first-existing-slice", "second-existing-slice"}; + for(usize index = 0; index < names.size(); ++index) + { + std::ofstream file(output.path() / names.at(index), std::ios::binary); + REQUIRE(file.is_open()); + file << sentinels.at(index); + } + const WriteImageFilter filter; + const auto arguments = writeImageNumericArguments(output.path() / "slice.png"); + const auto preflight = filter.preflight(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = scope.executeFilter(filter, dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -27027); + for(usize index = 0; index < names.size(); ++index) + { + const auto bytes = readWriteImageSentinel(output.path() / names.at(index)); + REQUIRE(std::string(bytes.begin(), bytes.end()) == sentinels.at(index)); + } + REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: no finite color range errors in either mode", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const uint64 policy = GENERATE(uint64{0}, uint64{1}); + const bool allMasked = GENERATE(false, true); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}); + createWriteImageNumericMask(dataStructure, std::array{false, false, false}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "none.png", policy, "Grayscale", allMasked)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == ((!allMasked && policy == 0) ? -27027 : -27028)); + REQUIRE(result.result.warnings().size() == ((!allMasked && policy == 1) ? 1 : 0)); + REQUIRE(fs::is_empty(output.path())); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: finite constants and all finite masked values stay valid", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool allMasked = GENERATE(false, true); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{5, 5, 5}); + createWriteImageNumericMask(dataStructure, std::array{!allMasked, false, !allMasked}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "constant.png", 0, "X Ray", true)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + const std::vector expected = allMasked ? std::vector{10, 20, 30, 10, 20, 30, 10, 20, 30} : std::vector{255, 255, 255, 10, 20, 30, 255, 255, 255}; + REQUIRE(readWriteImageNumericPng(output.path() / "constant.png", 3, 1) == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal layouts in every plane", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const uint64 plane = GENERATE(uint64{0}, uint64{1}, uint64{2}); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{0, 3, 6, 9, 0, 3, 6, 9}, {2, 2, 2}); + const ScopedTempDir output(k_BinaryTestOutputDir); + auto arguments = writeImageNumericArguments(output.path() / "slice.png"); + arguments.insertOrAssign(WriteImageFilter::k_Plane_Key, std::make_any(plane)); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const std::array, 3> expected{{{0, 85, 170, 255, 0, 85, 170, 255}, {0, 85, 0, 85, 170, 255, 170, 255}, {0, 170, 0, 170, 85, 255, 85, 255}}}; + for(usize slice = 0; slice < 2; ++slice) + { + const auto bytes = readWriteImageNumericPng(output.path() / (slice == 0 ? "slice_000.png" : "slice_001.png"), 2, 2); + for(usize pixel = 0; pixel < 4; ++pixel) + { + for(usize channel = 0; channel < 3; ++channel) + { + REQUIRE(bytes.at((pixel * 3) + channel) == expected.at(plane).at((slice * 4) + pixel)); + } + } + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: raw floating pixels ignore color policy and mask", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]", float32, float64) +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const uint64 policy = GENERATE(uint64{0}, uint64{1}); + const bool useMask = GENERATE(false, true); + UnitTest::AlgorithmTestScope scope(scenario); + const std::array values{TestType{-2}, TestType{0}, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}; + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, values); + createWriteImageNumericMask(dataStructure, std::array{false, false, false, false, false}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const fs::path path = output.path() / (std::is_same_v ? "raw.tif" : "raw.mha"); + auto arguments = writeImageNumericArguments(path, policy, "Grayscale", useMask); + arguments.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(false)); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + std::array actual{}; + if constexpr(std::is_same_v) + { + const auto imageIoResult = CreateImageIO(path); + SIMPLNX_RESULT_REQUIRE_VALID(imageIoResult); + auto metadata = imageIoResult.value()->readMetadata(path); + SIMPLNX_RESULT_REQUIRE_VALID(metadata); + REQUIRE(metadata.value().dataType == DataType::float32); + REQUIRE(metadata.value().numComponents == 1); + std::array bytes{}; + const auto readResult = imageIoResult.value()->readPixelData(path, std::span(bytes)); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + std::memcpy(actual.data(), bytes.data(), bytes.size()); + } + else + { + const ReadMhaFileFilter reader; + auto readerArguments = reader.getDefaultArguments(); + readerArguments.insertOrAssign(ReadMhaFileFilter::k_InputFilePath_Key, std::make_any(path)); + readerArguments.insertOrAssign(ReadMhaFileFilter::k_CreatedImageGeometryPath_Key, std::make_any(DataPath({"RawRead"}))); + readerArguments.insertOrAssign(ReadMhaFileFilter::k_CellAttributeMatrixName_Key, std::make_any("Cells")); + readerArguments.insertOrAssign(ReadMhaFileFilter::k_ImageDataArrayName_Key, std::make_any("Pixels")); + DataStructure readStructure; + const auto readResult = reader.execute(readStructure, readerArguments); + SIMPLNX_RESULT_REQUIRE_VALID(readResult.result); + const DataPath readPath({"RawRead", "Cells", "Pixels"}); + REQUIRE_NOTHROW(readStructure.getDataRefAs(readPath)); + REQUIRE(readStructure.getDataRefAs(readPath).getDataType() == DataType::float64); + REQUIRE_NOTHROW(readStructure.getDataRefAs(readPath)); + const auto copyResult = readStructure.getDataRefAs(readPath).getDataStoreRef().copyIntoBuffer(0, nonstd::span(actual.data(), actual.size())); + SIMPLNX_RESULT_REQUIRE_VALID(copyResult); + UnitTest::CheckArraysInheritTupleDims(readStructure); + } + REQUIRE(actual.at(0) == -2); + REQUIRE(actual.at(1) == 0); + REQUIRE(std::isnan(actual.at(2))); + REQUIRE(std::isinf(actual.at(3))); + REQUIRE_FALSE(std::signbit(actual.at(3))); + REQUIRE(std::isinf(actual.at(4))); + REQUIRE(std::signbit(actual.at(4))); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan opens no slice", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{0, 1, 2}); + const ScopedTempDir output(k_BinaryTestOutputDir); + std::atomic_bool cancel = false; + const IFilter::MessageHandler handler{[&cancel](const IFilter::Message& message) { + if(message.message.find("Computing color range") != std::string::npos) + { + cancel = true; + } + }}; + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "cancel.png"), nullptr, handler, cancel); + REQUIRE(cancel.load()); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -1); + REQUIRE(fs::is_empty(output.path())); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards use actionable results", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto empty = WriteImageColor::ValidateSize(0, 1, 1, false); + SIMPLNX_RESULT_REQUIRE_INVALID(empty); + REQUIRE(empty.errors().front().code == -27030); + const auto large = WriteImageColor::ValidateSize(std::numeric_limits::max(), 1, 1, true); + SIMPLNX_RESULT_REQUIRE_INVALID(large); + REQUIRE(large.errors().front().code == -27032); + const auto invalidPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {1, 0}); + SIMPLNX_RESULT_REQUIRE_INVALID(invalidPalette); + REQUIRE(invalidPalette.errors().front().code == -27029); + const auto validPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {0, 1}); + SIMPLNX_RESULT_REQUIRE_VALID(validPalette); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: page and slice IO failures preserve diagnostics and warnings", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const usize failure = GENERATE(usize{0}, usize{1}, usize{2}); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + const DataPath inputPath = createWriteImageNumericInput(dataStructure, std::array{0, std::numeric_limits::quiet_NaN(), 2}); + const DataPath maskPath = createWriteImageNumericMask(dataStructure, std::array{1, 1, 1}); + if(failure != 1) + { + auto store = std::make_shared>(ShapeType{1, 1, 3}, failure == 0, failure == 2); + store->setValue(0, 0); + store->setValue(1, std::numeric_limits::quiet_NaN()); + store->setValue(2, 2); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(inputPath)); + const auto replace = dataStructure.getDataRefAs(inputPath).setDataStore(store); + SIMPLNX_RESULT_REQUIRE_VALID(replace); + } + else + { + auto store = std::make_shared>(ShapeType{1, 1, 3}, true, false); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(maskPath)); + const auto replace = dataStructure.getDataRefAs(maskPath).setDataStore(store); + SIMPLNX_RESULT_REQUIRE_VALID(replace); + } + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "failure.png", 1, "Grayscale", true)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == (failure == 2 ? -27902 : -27901)); + if(failure == 2) + { + REQUIRE(result.result.warnings().size() == 2); + REQUIRE(result.result.warnings().at(0).code == -27903); + REQUIRE(result.result.warnings().at(1).code == -27031); + } + REQUIRE(fs::is_empty(output.path())); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: late nonfinite scan uses bounded scalar and mask pages", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool useMask = GENERATE(false, true); + UnitTest::AlgorithmTestScope scope(scenario); + constexpr usize pageByteTarget = usize{1024} * usize{1024}; + const usize pageValues = pageByteTarget / (sizeof(float64) + (useMask ? sizeof(uint8) : 0)); + const ShapeType shape{2, 1, pageValues}; + DataStructure dataStructure; + auto* geometryPtr = ImageGeom::Create(dataStructure, "NumericImage"); + REQUIRE(geometryPtr != nullptr); + geometryPtr->setDimensions({pageValues, 1, 2}); + auto* cellsPtr = AttributeMatrix::Create(dataStructure, "CellData", shape, geometryPtr->getId()); + REQUIRE(cellsPtr != nullptr); + geometryPtr->setCellData(*cellsPtr); + auto store = std::make_shared>(shape, false, false); + store->setValue(pageValues, std::numeric_limits::quiet_NaN()); + REQUIRE(Float64Array::Create(dataStructure, "Input", store, cellsPtr->getId()) != nullptr); + std::shared_ptr> maskStore; + if(useMask) + { + maskStore = std::make_shared>(shape, false, false); + for(usize index = 0; index < 2 * pageValues; ++index) + { + maskStore->setValue(index, 1); + } + REQUIRE(UInt8Array::Create(dataStructure, "Mask", maskStore, cellsPtr->getId()) != nullptr); + } + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "page.png", 0, "Grayscale", useMask)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -27027); + REQUIRE(store->flatReadCount() == 2); + REQUIRE(store->maximumReadBytes() == pageValues * sizeof(float64)); + if(useMask) + { + REQUIRE(maskStore->flatReadCount() == 2); + REQUIRE(maskStore->maximumReadBytes() == pageValues * sizeof(uint8)); + REQUIRE((store->maximumReadBytes() + maskStore->maximumReadBytes()) <= pageByteTarget); + } + REQUIRE(result.result.warnings().size() == (useMask ? 4 : 2)); + REQUIRE(fs::is_empty(output.path())); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::WriteImageFilter: later commit failure keeps earlier slice and policy warning", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure dataStructure; + createWriteImageNumericInput(dataStructure, std::array{0, std::numeric_limits::quiet_NaN(), 2, 3}, {2, 1, 2}); + const ScopedTempDir output(k_BinaryTestOutputDir); + const fs::path blockedTarget = output.path() / "slice_001.png"; + REQUIRE(fs::create_directory(blockedTarget)); + { + std::ofstream sentinel(blockedTarget / "keep.txt", std::ios::binary); + REQUIRE(sentinel.is_open()); + sentinel << "keep"; + } + const WriteImageFilter filter; + const auto arguments = writeImageNumericArguments(output.path() / "slice.png", 1); + const auto preflight = filter.preflight(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = scope.executeFilter(filter, dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -15782); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -27031); + REQUIRE(fs::is_regular_file(output.path() / "slice_000.png")); + const auto first = readWriteImageNumericPng(output.path() / "slice_000.png", 2, 1); + REQUIRE(first == std::vector{0, 0, 0, 0, 0, 0}); + const auto sentinel = readWriteImageSentinel(blockedTarget / "keep.txt"); + REQUIRE(std::string(sentinel.begin(), sentinel.end()) == "keep"); + REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + TEST_CASE("NXImageProcessing::WriteImageFilter: Write Stack", "[NXImageProcessing][WriteImageFilter]") { auto app = Application::GetOrCreateInstance(); From 75ba53089b78488ba73c018b7a31a1c71c2da364 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 04:03:46 -0400 Subject: [PATCH 21/98] BUG: Correct threshold search arithmetic and nonfinite handling Use native safe midpoints and spans, clamp upper controls before conversion, and add one explicit Error/Warn image policy. Preserve the two-unit stop, strict component counting and existing scoring. Define empty/no-finite fallbacks and rounded-state warnings, retain bounded parallel scans and document segmentation migration. Validated paired full Windows Release builds, 40 InCore/48 OOC owning cases and 42/54 related controls. Independent native masks, actual disk/tile/3-D routes and failures pass. The float real-image regression actively retains legacy and corrected literal references; separate face-connected flood fill isolates the approved seed correction and 159 changed pixels. Source/fix review and clang-format 19.1.1 pass. Configured clang-tidy remains nonpassing; Debug, cross-platform, installed SDK and universal scientific proof are not claimed. Signed-off-by: Jessica Marquis --- CMakeLists.txt | 5 + ...ldMaximumConnectedComponentsImageFilter.md | 44 +- ...dMaximumConnectedComponentsImageFilter.cpp | 54 +- ...dMaximumConnectedComponentsImageFilter.hpp | 10 +- .../utils/ComponentImageFilterExecutors.cpp | 194 +--- .../utils/ImageFilterExecutors.hpp | 10 +- ...imumConnectedComponentsImageFilterTest.cpp | 836 +++++++++++++++++- ...esholdMaximumConnectedComponentsEngine.hpp | 505 +++++++++++ 8 files changed, 1430 insertions(+), 228 deletions(-) create mode 100644 src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c812369e8..4fcfe7d456 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -414,6 +414,7 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/CoreDataIOManager.hpp ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/DataIOCollection.hpp ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/IExternalSort.hpp + ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/ITemporaryRecordStore.hpp ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/IDataFactory.hpp ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/IDataIOManager.hpp ${SIMPLNX_SOURCE_DIR}/DataStructure/IO/Generic/IDataStoreFormatResolver.hpp @@ -608,6 +609,10 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/WorkingMemory.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/TerminalImageConversion.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/OtsuImageEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ConnectedComponentEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/StreamingStatistics.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/SweepTemporaryStore.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/HistogramEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/PointwiseEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ProgressEstimator.hpp diff --git a/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md b/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md index a42644f2e2..873316892c 100644 --- a/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md @@ -1,40 +1,56 @@ # Threshold Maximum Connected Components Image Filter -Finds the threshold value of an image based on maximizing the number of objects in the image that are larger than a given minimal size. - ## Group (Subgroup) NXImageProcessing (Segmentation) ## Description -This filter is an iterative auto-thresholding method: it bisection-searches for the threshold value that **maximizes** the number of connected components (objects) in the image that are at least **Minimum Object Size In Pixels** in size, then outputs the binary threshold at that value. This method is based on Topological Stable State Thresholding and is particularly effective when there are a large number of objects in a microscopy image. +This filter selects a lower intensity threshold with a **component-count heuristic**: it compares the numbers of connected regions at two candidate thresholds and narrows the interval. It does not guarantee a global maximum. The input is a scalar cell array in the selected image geometry. Integer and floating inputs produce the same fixed **uint8** output type, with one byte per pixel or voxel. Output values are **Inside Value** and **Outside Value**; the threshold itself is not saved as an image. + +A pixel is inside when its native intensity is at or above the selected lower threshold and at or below **Upper Boundary**. Both bounds are inclusive. The search uses **face connectivity**, which joins pixels through shared edges in 2-D or shared faces in 3-D. Diagonal contact alone does not join regions. + +### Search score and stopping units -At each step of the search, the image is binary-thresholded at a candidate threshold value (paired with the fixed **Upper Boundary**), the resulting foreground is labeled into connected components (face connectivity only), and the number of components at least **Minimum Object Size In Pixels** in size is counted. The bisection converges on the threshold value that maximizes this count. +The score counts connected components of the would-be **nonzero output**, including components whose size is exactly **Minimum Object Size In Pixels**. This parameter is a pixel/voxel count and defaults to *0*. It changes the search score; it does not remove small objects from the final output. -The input array must be single-component (scalar) and may be **any scalar type** (integer or floating point). The output is a fixed **uint8** binary image (**Inside Value** / **Outside Value**) regardless of the input element type. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Threshold Maximum Connected Components Image Filter, and matches it exactly (byte-identical binary output). +**Inside Value** defaults to *1* and **Outside Value** defaults to *0*. Both are dimensionless uint8 values. With these defaults, the score counts in-band regions. Inside0/Outside1 scores the complement. If both values are nonzero, all pixels participate as foreground. If both are zero, none participate. A nonzero Outside Value can connect regions that would otherwise be separate. -### Minimum Object Size In Pixels +The search stops when the interval is at most **2 input-intensity units** wide. This is an absolute rule for integers and floating inputs. It is not a relative tolerance or a measure of segmentation accuracy. For example, normalized values spanning 0 to 1 skip the search. Binary input `[0,1,0,1]` selects threshold0 and gives `[1,1,1,1]`, although another threshold could give more regions. Scaling the intensities can change the selected mask. The right candidate wins only if its score is strictly greater; ties select the left candidate. -Set the minimum pixel area used to count objects on the image. Only objects that have a pixel area greater than or equal to the minimum pixel area are counted as an object in the optimization portion of this filter. Essentially, it eliminates noise from being counted as an object. The default value is zero. +For floating input, native rounding can repeat the complete search state while the interval is still wider than 2. The filter stops, retains the candidate selected by the normal comparison, and issues one precision warning. It does not choose a different endpoint or perform an exhaustive search. ### Upper Boundary -This class automatically calculates the lower threshold boundary. The upper threshold boundary, inside value, and outside value can be defined by the user, however the standard values are used as default if not set by the user. Default is 65536.0. +Upper Boundary is a Float64 parameter in input-intensity units. Its default is *65536*. Integer input truncates an in-range fractional request toward zero. Thus an unsigned request of *-0.5* becomes *0*; *-1* requests an empty native band. A signed request that truncates to the native minimum remains valid. In-range Float64-to-float32 conversion retains ordinary native rounding. + +A finite request above the native maximum, or positive infinity, selects the exact native maximum before conversion. For uint8, the default therefore becomes *255*. int64/uint64 limits are checked before casting; a rounded Float64 value at 2^63 or 2^64 is never cast into the corresponding integer. Float64 cannot express every exact 64-bit integer cutoff above 2^53, even though image bounds and integer search candidates remain exact native integers. + +NaN Upper Boundary is an invalid parameter and always causes a clear error, including in the image Warn mode. A finite request below the native range after integer truncation, a cap below the finite image minimum, or negative infinity produces an empty band. The filter skips search, fills Outside Value and issues an empty-band warning. It does not clamp a low cutoff upward or treat this output as an optimized threshold. Structurally valid zero-length input remains empty and is distinct from a nonempty all-nonfinite image. -### Inside Value +### Nonfinite Input Handling -The value assigned to voxels at or above the calculated threshold (and at or below the Upper Boundary). Default is 1. +This single choice applies to image NaN, positive infinity and negative infinity: -### Outside Value +- *Error* is the default. The initial image scan stops with an error before component search or classification writes. An empty requested band does not bypass this scan. Output allocation can already have occurred through normal preflight actions. +- *Warn and Continue* uses only finite image values to establish native search bounds. Ordinary inclusive comparisons assign every nonfinite pixel Outside Value. One aggregate warning reports separate NaN and infinity counts. A positive-infinity parameter cap selects a finite native maximum; it does not include positive-infinity image pixels. +- If a nonempty Warn input has no finite pixels, the filter skips all threshold/component searches and fills Outside Value. One aggregate warning states the counts and that no threshold was calculated. This is a defined fallback, not a fitted segmentation. No additional UI choice is required. -The value assigned to voxels outside the calculated threshold range. Default is 0. +Outside0 gives no nonzero foreground in an all-Outside fallback. A nonzero Outside Value can make the entire image foreground downstream, or bridge finite regions in a mixed image. These changes affect object counts, fractions, boundaries and measurements. Later filters that require objects can fail on an empty mask or report different results on a whole-image mask. Warn does not waive cancellation, I/O, memory, addressability or strict component-capacity failures. + +### Numeric migration + +The starting midpoint now includes the image minimum, and interval arithmetic avoids native integer overflow. This corrects offset, negative and wide-range cases. For input `[100,102]`, a nonbinding cap and Inside1/Outside0, the old starting threshold1 gave mask `[1,1]`; the corrected midpoint101 gives `[0,1]`. The output type is unchanged, but segmentation and downstream measurements can change. Constant images use their actual constant as the threshold. Old pipelines that omit the new nonfinite choice use Error and can now stop on questionable data that previously completed. + +The finite float32 `RA-Float.nrrd` regression fixture demonstrates this migration on a real image. Its range is -1146 to 32767. With default controls, the corrected initial midpoint changes from 16956.5 to 15810.5, and the final threshold changes from 24164.05859375 to 24166.107421875. Exactly 159 pixels change: foreground decreases from 99487 to 99328 pixels, and face-connected components decrease from 3715 to 3712. An independent six-neighbor flood fill confirms these counts; correcting only the initial seed reproduces the new mask. The retained heuristic can select fewer components after the arithmetic correction. Neither the count nor this regression is a segmentation-accuracy score or a global-maximum guarantee. ## Algorithm -The filter performs a bisection search over candidate lower thresholds. For each candidate it raster-scans the image, encodes foreground runs, joins connected runs with union-find, and counts components meeting the minimum-size requirement. The union-find and component-size tables contain one entry per provisional object run. After selecting the threshold, the filter writes the requested binary inside and outside values in bounded chunks. +One initial scan collects native finite extrema and bounded nonfinite summaries. Existing resident spans are reduced directly; other stores are read serially into bounded owned buffers, whose independent blocks can be reduced in parallel. The filter does not create a full-image finite mask or convert the integer image to Float64. Initial, left and right integer midpoint calculations retain native 64-bit distinctions and their left/right rounding rules. + +Each iteration performs two connected-component scans and compares their scores. 3-D counting reads budget-derived bands with a one-plane fallback and retains current/previous run planes. True 2-D counting uses bounded row blocks; very wide rows use bounded X tiles and provisional scratch, using disk storage for out-of-core input. These are shared traversal routes for resident and disk-backed arrays, with serial store I/O and stable owned compute buffers. Final classification and all-Outside fills use bounded writes. -For images with more than one Z plane, each connected-component scan reads one Z plane at a time and retains the current and previous plane's run descriptions. For a true two-dimensional image (`Z=1`), a checked scheduler uses at most 64 MiB of full-width row blocks while retaining only two run-encoded rows. Exceptionally wide rows use bounded X tiles and a provisional label image to reconnect neighboring tiles and rows. With an out-of-core input, that scratch uses the registered raw temporary-record store when available and otherwise falls back to the selected array storage format. This keeps cell-scale state disk-backed for out-of-core inputs while preserving the exact threshold search, connectivity, and component-count results. +Union-find and size tables still grow with the number of provisional foreground runs. Bounded staging is not a bound on total RAM. A provisional run ID beyond uint32 capacity, an allocation/addressability failure, or an I/O error stops execution; the image Warn choice does not permit label wrap or incorrect counts. Search changes can change the number of full-image count passes and therefore CPU time and disk I/O. No fixed runtime improvement or legacy byte-parity guarantee is implied. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp index 5fa3be25f9..ecd76a7ec2 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp @@ -2,14 +2,26 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Filter/Output.hpp" +#include "simplnx/Filter/Parameters.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" #include "simplnx/Parameters/GeometrySelectionParameter.hpp" #include "simplnx/Parameters/NumberParameter.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include +#include +#include + using namespace nx::core; namespace nx::core @@ -41,27 +53,23 @@ Parameters ThresholdMaximumConnectedComponentsImageFilter::parameters() const params.insertSeparator(Parameters::Separator{"Input Parameter(s)"}); params.insert(std::make_unique( k_MinimumObjectSizeInPixels_Key, "Minimum Object Size In Pixels", - "The pixel type must support comparison operators. Set the minimum pixel area used to count objects on the image. Thus, only objects that have a pixel area greater than the minimum pixel area " - "will be counted as an object in the optimization portion of this filter. Essentially, it eliminates noise from being counted as an object. The default value is zero.", + "Count face-connected nonzero output components with at least this many pixels or voxels. This changes the search score; it does not remove small objects from the output. Default: 0 pixels.", 0u)); - params.insert(std::make_unique( - k_UpperBoundary_Key, "Upper Boundary", - "The following Set/Get methods are for the binary threshold function. This class automatically calculates the lower threshold boundary. The upper threshold boundary, inside value, and outside " - "value can be defined by the user, however the standard values are used as default if not set by the user. The default value of the: Inside value is the maximum pixel type intensity. Outside " - "value is the minimum pixel type intensity. Upper threshold boundary is the maximum pixel type intensity.", - 65536.0)); - params.insert(std::make_unique( - k_InsideValue_Key, "Inside Value", - "The following Set/Get methods are for the binary threshold function. This class automatically calculates the lower threshold boundary. The upper threshold boundary, inside value, and outside " - "value can be defined by the user, however the standard values are used as default if not set by the user. The default value of the: Inside value is the maximum pixel type intensity. Outside " - "value is the minimum pixel type intensity. Upper threshold boundary is the maximum pixel type intensity.", - 1u)); + params.insert( + std::make_unique(k_UpperBoundary_Key, "Upper Boundary", + "Inclusive upper cutoff in input intensity units. Default: 65536. Integer inputs truncate toward zero. High values or positive infinity select the native " + "maximum; low/empty bands or negative infinity produce Outside Value with a warning. NaN is an error. Float64 cannot express every 64-bit integer cutoff.", + 65536.0)); + params.insert( + std::make_unique(k_InsideValue_Key, "Inside Value", + "Dimensionless uint8 value assigned inside the inclusive threshold band. Default: 1. Only nonzero output pixels participate in the component-count score.", 1u)); params.insert(std::make_unique( k_OutsideValue_Key, "Outside Value", - "The following Set/Get methods are for the binary threshold function. This class automatically calculates the lower threshold boundary. The upper threshold boundary, inside value, and outside " - "value can be defined by the user, however the standard values are used as default if not set by the user. The default value of the: Inside value is the maximum pixel type intensity. Outside " - "value is the minimum pixel type intensity. Upper threshold boundary is the maximum pixel type intensity.", - 0u)); + "Dimensionless uint8 value assigned outside the band. Default: 0. A nonzero value participates in scoring and can connect regions, including nonfinite pixels in Warn mode.", 0u)); + params.insert(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", + "Error stops on image NaN or either infinity before search/output. Warn and Continue uses finite bounds and assigns nonfinite pixels Outside Value. " + "With no finite pixels, it skips search, fills Outside Value and warns that no threshold was calculated.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -77,7 +85,7 @@ Parameters ThresholdMaximumConnectedComponentsImageFilter::parameters() const IFilter::VersionType ThresholdMaximumConnectedComponentsImageFilter::parametersVersion() const { - return 1; + return 2; } IFilter::UniquePointer ThresholdMaximumConnectedComponentsImageFilter::clone() const { @@ -87,6 +95,11 @@ IFilter::UniquePointer ThresholdMaximumConnectedComponentsImageFilter::clone() c IFilter::PreflightResult ThresholdMaximumConnectedComponentsImageFilter::preflightImpl(const DataStructure& dataStructure, const Arguments& filterArgs, const MessageHandler& messageHandler, const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const { + if(std::isnan(filterArgs.value(k_UpperBoundary_Key))) + { + return {.outputActions = MakeErrorResult( + -28610, "Upper Boundary is NaN. Enter a valid numeric cutoff; positive infinity selects the native maximum and negative infinity requests an empty band.")}; + } auto imageGeomPath = filterArgs.value(k_InputImageGeomPath_Key); auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); @@ -114,6 +127,7 @@ Result<> ThresholdMaximumConnectedComponentsImageFilter::executeImpl(DataStructu auto outsideValue = filterArgs.value(k_OutsideValue_Key); return ImageProcessing::ExecuteThresholdMaximumConnectedComponentsImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, minimumObjectSizeInPixels, upperBoundary, - insideValue, outsideValue, shouldCancel, messageHandler); + insideValue, outsideValue, shouldCancel, messageHandler, + filterArgs.value(k_NonfiniteInputHandling_Key) == 1); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp index 5d4bd2e3bf..a11967eb0e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp @@ -9,13 +9,12 @@ namespace nx::core { /** * @class ThresholdMaximumConnectedComponentsImageFilter - * @brief ITK-free, out-of-core-capable iterative auto-threshold that bisection-searches for the threshold value - * that MAXIMIZES the number of connected components in the image (subject to a minimum object size), then outputs - * the binary threshold at that value. + * @brief Searches native finite bounds with a component-count heuristic and an absolute two-intensity-unit stop. * * The input may be ANY scalar type; the output is a FIXED uint8 binary image (Inside Value / Outside Value) - * regardless of the input element type. Matches the legacy ITK Threshold Maximum Connected Components Image - * Filter exactly (byte-identical binary output). Out-of-core capable. + * regardless of the input element type. Safe midpoint arithmetic can change legacy segmentations. Image NaN and + * infinities support Error or warned Outside participation. The shared traversal supports both storage types; + * component run tables still grow with image topology. The search does not guarantee a global maximum. */ class NXIMAGEPROCESSING_EXPORT ThresholdMaximumConnectedComponentsImageFilter : public IFilter { @@ -36,6 +35,7 @@ class NXIMAGEPROCESSING_EXPORT ThresholdMaximumConnectedComponentsImageFilter : static constexpr StringLiteral k_UpperBoundary_Key = "upper_boundary"; static constexpr StringLiteral k_InsideValue_Key = "inside_value"; static constexpr StringLiteral k_OutsideValue_Key = "outside_value"; + static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; std::string name() const override; std::string className() const override; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp index 74b2b56885..0dbc0afb4d 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp @@ -9,6 +9,7 @@ #include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ZeroCrossingEngine.hpp" #include @@ -677,195 +678,25 @@ struct RelabelComponentExecuteFn } }; -/** - * @brief Clamps the user's Float64 Upper Boundary parameter to the input pixel type T's representable range, - * mirroring the legacy ITK bridge functor's SetUpperBoundary call - * (ITKThresholdMaximumConnectedComponentsImageFilter.cpp::createFilter): `static_cast( - * std::min(upperBoundary, itk::NumericTraits::max()))`, where MinType is PixelType - * itself for int64/uint64 (whose max cannot be represented exactly as a float64, so the comparison is done - * in T to avoid that precision loss) and float64 for every other scalar type (all other integers plus - * float32/float64). This is the ONE-TIME clamp applied before the bisection (ITK's `m_UpperBoundary`), - * distinct from the per-run LOCAL bisection bound computed in @ref ThresholdMaxCCExecuteFn. - */ -template -T ClampThresholdMaxCCUpperBoundary(float64 upperBoundary) -{ - if constexpr(std::is_same_v || std::is_same_v) - { - return std::min(static_cast(upperBoundary), std::numeric_limits::max()); - } - else - { - return static_cast(std::min(upperBoundary, static_cast(std::numeric_limits::max()))); - } -} - -/** - * @brief Dispatched execute body for the (ITK-free) ThresholdMaximumConnectedComponents filter: bisection-searches - * the threshold value that MAXIMIZES the number of connected components with size >= - * @c minimumObjectSizeInPixels, then writes the final binary threshold at the converged value. Transcribed - * VERBATIM from itk::ThresholdMaximumConnectedComponentsImageFilter::GenerateData + ComputeConnectedComponents - * (itkThresholdMaximumConnectedComponentsImageFilter.hxx): - * - lowerBound/upperBound = input min/max (one streaming @ref ComputeArrayMinMax pass); upperBound is - * then clamped to the (type-clamped) Upper Boundary parameter -- @ref ClampThresholdMaxCCUpperBoundary. - * - midpoint = (upperBound-lowerBound)/2; midpointL = lowerBound+(midpoint-lowerBound)/2; midpointR = - * upperBound-(upperBound-midpoint)/2 -- PixelType (T) arithmetic (integer division for integer T, so - * this is NOT the true midpoint of [lowerBound,upperBound] when lowerBound != 0); this is ITK's own - * formula, transcribed exactly as written, not "fixed". - * - while((upperBound-lowerBound) > 2): count components at midpointR and midpointL (each count binary- - * thresholds [candidateThreshold, UpperBoundary] and counts connected components with size >= - * minimumObjectSizeInPixels, via @ref CountConnectedComponents); if the RIGHT count is STRICTLY greater - * than the LEFT count, move lowerBound=midpoint, midpoint=midpointR; otherwise (right <= left, i.e. a - * TIE TAKES THE LOWER/LEFT branch, matching ITK's `if (connectedComponentsRight > connectedComponentsLeft)` - * exactly) move upperBound=midpoint, midpoint=midpointL. Recompute midpointL/midpointR each iteration. - * - final: thresholdValue = midpoint; out = (v >= thresholdValue && v <= UpperBoundary) ? insideValue : - * outsideValue. - * The internal ConnectedComponentImageFilter connectivity is ITK's own default, FullyConnected=false (the - * bridge never calls SetFullyConnected; itkScanlineFilterCommon.h's m_FullyConnected member defaults to - * false). Output is a FIXED uint8 binary image. Input may be ANY scalar type (the caller's preflight - * enforces this via the AllNumeric policy). - */ +/** @brief Native threshold search adapter; the scoped engine owns validation, warnings and bounded I/O. */ struct ThresholdMaxCCExecuteFn { DataStructure& dataStructure; const DataPath& inputArrayPath; const DataPath& outputArrayPath; SizeVec3 dims; - uint32 minimumObjectSizeInPixels; - float64 upperBoundary; - uint8 insideValue; - uint8 outsideValue; + ThresholdMaxCCOptions Options; const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; template Result<> operator()() const { - const auto& inStore = dataStructure.getDataRefAs>(inputArrayPath).getDataStoreRef(); - auto& outStore = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); - - // Input min/max (itk::MinimumMaximumImageCalculator equivalent): one streaming bulk-I/O pass. Only min/max - // are consumed, so the parallel min/max reduction replaces the full statistics pass. - Result> statsResult = ComputeArrayMinMax(inStore, shouldCancel); - if(statsResult.invalid()) - { - return ConvertResult(std::move(statsResult)); - } - if(shouldCancel) - { - return {}; - } - const ArrayMinMax& stats = statsResult.value(); - - // m_UpperBoundary (ITK): the Upper Boundary parameter clamped to T's representable range -- FIXED for the - // whole run (unlike the local bisection `upperBound` below, which the loop mutates). - const T upperBoundaryFixed = ClampThresholdMaxCCUpperBoundary(upperBoundary); - - T lowerBound = stats.min; - T upperBound = std::min(stats.max, upperBoundaryFixed); - - T midpoint = (upperBound - lowerBound) / 2; - T midpointL = lowerBound + (midpoint - lowerBound) / 2; - T midpointR = upperBound - (upperBound - midpoint) / 2; - - const uint64 minSize = static_cast(minimumObjectSizeInPixels); - // ITK's ComputeConnectedComponents (itkThresholdMaximumConnectedComponentsImageFilter.hxx:76-81,108-110) counts - // components on the BINARY-THRESHOLD OUTPUT, whose foreground is `value != 0`: the internal BinaryThresholdImageFilter - // writes m_InsideValue to in-band pixels [thr, m_UpperBoundary] and m_OutsideValue elsewhere, then the - // ConnectedComponentImageFilter (background=0) labels every NONZERO pixel. So a pixel is counted-foreground iff its - // threshold output would be nonzero -- (in-band ? InsideValue!=0 : OutsideValue!=0) -- NOT simply "in-band". For the - // DEFAULT (InsideValue=1, OutsideValue=0) this reduces to exactly (in-band ? true : false) = in-band, unchanged; for - // non-default Inside/Outside (e.g. Inside=0/Outside=1) ITK instead maximizes components in the OUT-of-band region. - // The internal ConnectedComponentImageFilter connectivity is ITK's default (FullyConnected=false). - const bool insideNonZero = (insideValue != 0); - const bool outsideNonZero = (outsideValue != 0); - auto countAt = [&](T thr) -> Result { - const auto pred = [thr, upperBoundaryFixed, insideNonZero, outsideNonZero](T v) { return (v >= thr && v <= upperBoundaryFixed) ? insideNonZero : outsideNonZero; }; - return CountConnectedComponents(inStore, dims, pred, /*fullyConnected=*/false, minSize, shouldCancel); - }; - - while((upperBound - lowerBound) > T{2}) - { - if(shouldCancel) - { - return {}; - } - Result rightResult = countAt(midpointR); - if(rightResult.invalid()) - { - return ConvertResult(std::move(rightResult)); - } - Result leftResult = countAt(midpointL); - if(leftResult.invalid()) - { - return ConvertResult(std::move(leftResult)); - } - - // itkThresholdMaximumConnectedComponentsImageFilter.hxx::GenerateData: ties (right <= left) take the ELSE - // branch -- i.e. the LOWER threshold wins on a tie. - if(rightResult.value() > leftResult.value()) - { - lowerBound = midpoint; - midpoint = midpointR; - } - else - { - upperBound = midpoint; - midpoint = midpointL; - } - - midpointL = lowerBound + (midpoint - lowerBound) / 2; - midpointR = upperBound - (upperBound - midpoint) / 2; - } - - const T thresholdValue = midpoint; - - // Final binary threshold [thresholdValue, upperBoundaryFixed] -> insideValue/outsideValue, streamed in a - // bounded, working-memory-derived band instead of a fixed 64 KiB chunk (identical in-core and out-of-core). - // A 64 KiB call is smaller than one store chunk at the certified geometry (a Z-plane is exactly one 1 MiB - // chunk), so every call touched only part of a single chunk and the codec never left its serial branch. - // Reserving from the shared working-memory budget widens each call to many whole chunks when the grant - // allows it, and degrades to exactly the previous 64 KiB floor -- never smaller -- under a tight grant. - const usize total = inStore.getSize(); - constexpr usize k_FinalPassMinimumValues = 65536; - constexpr usize k_FinalPassPreferredBytes = 32ULL * 1024ULL * 1024ULL; - constexpr usize k_FinalPassValueBytes = sizeof(T) + sizeof(uint8); - const bool finalPassByteCountFits = total <= std::numeric_limits::max() / k_FinalPassValueBytes; - const uint64 usefulFinalPassBytes = finalPassByteCountFits ? static_cast(total) * k_FinalPassValueBytes : std::numeric_limits::max(); - auto finalPassReservation = ReserveWorkingMemory(k_FinalPassPreferredBytes, usefulFinalPassBytes); - const usize chunkValues = - std::max(k_FinalPassMinimumValues, std::min(total, static_cast(std::min(finalPassReservation.sizeBytes() / k_FinalPassValueBytes, std::numeric_limits::max())))); - // shrinkTo is a no-op when the actual footprint is not smaller than the grant (e.g. the k_FinalPassMinimumValues - // floor under a tight grant), and otherwise releases the unused portion back to the shared budget immediately, - // rather than holding it for this call's remaining lifetime. - finalPassReservation.shrinkTo(static_cast(std::min(chunkValues, total)) * k_FinalPassValueBytes); - auto inBuffer = std::make_unique(std::min(chunkValues, total)); - auto outBuffer = std::make_unique(std::min(chunkValues, total)); - for(usize start = 0; start < total; start += chunkValues) - { - if(shouldCancel) - { - return {}; - } - const usize count = std::min(chunkValues, total - start); - if(Result<> r = inStore.copyIntoBuffer(start, nonstd::span(inBuffer.get(), count)); r.invalid()) - { - return r; - } - for(usize i = 0; i < count; ++i) - { - const T v = inBuffer[i]; - outBuffer[i] = (v >= thresholdValue && v <= upperBoundaryFixed) ? insideValue : outsideValue; - } - if(Result<> r = outStore.copyFromBuffer(start, nonstd::span(outBuffer.get(), count)); r.invalid()) - { - return r; - } - } - return {}; + const auto& input = dataStructure.getDataRefAs>(inputArrayPath).getDataStoreRef(); + auto& output = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); + return ApplyThresholdMaximumConnectedComponents(input, output, dims, Options, shouldCancel, messageHandler); } }; - } // namespace } // namespace detail @@ -933,14 +764,21 @@ Result<> ExecuteBinaryThinningImageFilter(DataStructure& dataStructure, const Da // ----------------------------------------------------------------------------- Result<> ExecuteThresholdMaximumConnectedComponentsImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, uint32 minimumObjectSizeInPixels, float64 upperBoundary, uint8 insideValue, uint8 outsideValue, const std::atomic_bool& shouldCancel, - const IFilter::MessageHandler& messageHandler) + const IFilter::MessageHandler& messageHandler, bool warnOnNonfinite) { using TypeSetPolicy = AllNumeric; const auto& inputArray = dataStructure.getDataRefAs(inputArrayPath); const DataType inputType = inputArray.getDataType(); const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); - detail::ThresholdMaxCCExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, imageGeom.getDimensions(), minimumObjectSizeInPixels, upperBoundary, insideValue, - outsideValue, shouldCancel, messageHandler}; + const ThresholdMaxCCOptions options{ + .MinimumObjectSize = minimumObjectSizeInPixels, .UpperBoundary = upperBoundary, .InsideValue = insideValue, .OutsideValue = outsideValue, .WarnOnNonfinite = warnOnNonfinite}; + const detail::ThresholdMaxCCExecuteFn executeFn{.dataStructure = dataStructure, + .inputArrayPath = inputArrayPath, + .outputArrayPath = outputArrayPath, + .dims = imageGeom.getDimensions(), + .Options = options, + .shouldCancel = shouldCancel, + .messageHandler = messageHandler}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp index 45cdf0ba86..53ff0feeda 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp @@ -439,16 +439,16 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteBinaryThinningImageFilter(DataStructure const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler); /** - * @brief Execute the (ITK-free) ThresholdMaximumConnectedComponents filter: bisection-searches the threshold value - * that MAXIMIZES the number of connected components (size >= MinimumObjectSizeInPixels) via @ref + * @brief Execute the native ThresholdMaximumConnectedComponents heuristic using component scores (size >= MinimumObjectSizeInPixels) via @ref * detail::ThresholdMaxCCExecuteFn (which uses @ref CountConnectedComponents each iteration), then writes the * final binary threshold [ThresholdValue, UpperBoundary] as a FIXED uint8 image (InsideValue/OutsideValue). - * Reproduces legacy ITK's ThresholdMaximumConnectedComponentsImageFilter output exactly (see the Parity - * model). The caller's preflight must have enforced a scalar input. + * Uses native safe arithmetic, the absolute two-unit stop and an explicit image nonfinite policy. + * The caller's preflight must have enforced a scalar input. */ NXIMAGEPROCESSING_EXPORT Result<> ExecuteThresholdMaximumConnectedComponentsImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, uint32 minimumObjectSizeInPixels, float64 upperBoundary, uint8 insideValue, - uint8 outsideValue, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler); + uint8 outsideValue, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, + bool warnOnNonfinite = false); /** * @brief Execute the (ITK-free, out-of-core) MorphologicalWatershedFromMarkers filter: a marker-controlled diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index 12c138e37e..319f1e48b8 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -3,15 +3,38 @@ #include "NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp" +#include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Filter/Parameters.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp" +#include "simplnx/Utilities/MD5.hpp" #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -43,6 +66,725 @@ void RunTMCC(IFilter& filter, DataStructure& ds, const DataPath& inputPath, uint } } // namespace +namespace +{ +Arguments thresholdArguments(const DataPath& inputPath, float64 cap = std::numeric_limits::infinity(), bool warn = false, uint8 inside = 1, uint8 outside = 0, uint32 minimumSize = 0) +{ + const ThresholdMaximumConnectedComponentsImageFilter filter; + Arguments arguments = filter.getDefaultArguments(); + arguments.insertOrAssign(TMCCFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + arguments.insertOrAssign(TMCCFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(TMCCFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(TMCCFilter::k_UpperBoundary_Key, std::make_any(cap)); + arguments.insertOrAssign(TMCCFilter::k_NonfiniteInputHandling_Key, std::make_any(warn ? 1 : 0)); + arguments.insertOrAssign(TMCCFilter::k_InsideValue_Key, std::make_any(inside)); + arguments.insertOrAssign(TMCCFilter::k_OutsideValue_Key, std::make_any(outside)); + arguments.insertOrAssign(TMCCFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(minimumSize)); + return arguments; +} + +void requireThresholdMask(const DataStructure& dataStructure, const DataPath& inputPath, const std::vector& expected) +{ + const IDataArray* inputPtr = nullptr; + const IDataArray* outputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + const DataPath outputPath = inputPath.replaceName("Output"); + REQUIRE_NOTHROW(outputPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputPtr->getDataType() == DataType::uint8); + REQUIRE(outputPtr->getTupleShape() == inputPtr->getTupleShape()); + REQUIRE(outputPtr->getComponentShape() == ShapeType{1}); + REQUIRE(outputPtr->getNumberOfTuples() == expected.size()); + const UInt8Array* maskPtr = nullptr; + REQUIRE_NOTHROW(maskPtr = &dataStructure.getDataRefAs(outputPath)); + std::vector actual(expected.size()); + const auto readResult = maskPtr->getDataStoreRef().copyIntoBuffer(0, nonstd::span(actual.data(), actual.size())); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + REQUIRE(actual == expected); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +enum class ThresholdFaultType : uint8 +{ + None, + ReadError, + BadAllocation, + LengthError +}; + +template +class ThresholdTransferStore : public DataStore +{ +public: + explicit ThresholdTransferStore(const std::vector& values) + : DataStore(ShapeType{1, 1, values.size()}, ShapeType{1}, T{}) + { + for(usize index = 0; index < values.size(); ++index) + { + DataStore::setValue(index, values[index]); + } + } + ~ThresholdTransferStore() noexcept override = default; + ThresholdTransferStore(const ThresholdTransferStore&) = delete; + ThresholdTransferStore(ThresholdTransferStore&&) = delete; + ThresholdTransferStore& operator=(const ThresholdTransferStore&) = delete; + ThresholdTransferStore& operator=(ThresholdTransferStore&&) = delete; + + IDataStore::StoreType getStoreType() const override + { + return IDataStore::StoreType::Empty; // Abstract bulk-transfer route only; this fixture has no disk backing. + } + Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override + { + ++m_Reads; + if(m_Reads == m_FaultRead) + { + if(m_Fault == ThresholdFaultType::ReadError) + { + return MakeErrorResult(-28631, "Injected Threshold read error"); + } + if(m_Fault == ThresholdFaultType::BadAllocation) + { + throw std::bad_alloc{}; + } + if(m_Fault == ThresholdFaultType::LengthError) + { + throw std::length_error("Injected Threshold addressability failure"); + } + } + auto result = DataStore::copyIntoBuffer(start, buffer); + if(m_WarnOnRead && m_Reads == 1) + { + result.warnings().push_back(Warning{.code = -28633, .message = "Injected Threshold read warning"}); + } + return result; + } + Result<> copyFromBuffer(usize start, nonstd::span buffer) override + { + ++m_Writes; + if(m_FailWrite) + { + return MakeErrorResult(-28632, "Injected Threshold write error"); + } + auto result = DataStore::copyFromBuffer(start, buffer); + if(m_CancelPtr != nullptr) + { + m_CancelPtr->store(true); + } + return result; + } + void setReadFault(usize read, ThresholdFaultType fault) noexcept + { + m_FaultRead = read; + m_Fault = fault; + } + void failWrite() noexcept + { + m_FailWrite = true; + } + void warnOnRead() noexcept + { + m_WarnOnRead = true; + } + void cancelOnWrite(std::atomic_bool& cancel) noexcept + { + m_CancelPtr = &cancel; + } + usize reads() const noexcept + { + return m_Reads; + } + usize writes() const noexcept + { + return m_Writes; + } + +private: + mutable usize m_Reads = 0; + usize m_Writes = 0; + usize m_FaultRead = 0; + ThresholdFaultType m_Fault = ThresholdFaultType::None; + bool m_FailWrite = false; + bool m_WarnOnRead = false; + std::atomic_bool* m_CancelPtr = nullptr; +}; +} // namespace + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonfinite policy is explicit and defaults to Error", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") +{ + UnitTest::LoadPlugins(); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const Parameters parameters = filter.parameters(); + const std::string policyKey = "nonfinite_input_handling_index"; + + // One choice covers image NaN and both infinities, including the no-finite-pixels fallback. + // Inspect the public schema without passing nonfinite data to the baseline search. + REQUIRE(parameters.contains(policyKey)); + const auto* policyPtr = dynamic_cast(parameters.at(policyKey).get()); + REQUIRE(policyPtr != nullptr); + REQUIRE(policyPtr->humanName() == "Nonfinite Input Handling"); + REQUIRE(policyPtr->choices() == ChoicesParameter::Choices{"Error", "Warn and Continue"}); + REQUIRE(policyPtr->defaultIndex() == 0); + const Arguments defaultArguments = filter.getDefaultArguments(); + REQUIRE(defaultArguments.value(policyKey) == 0); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: initial midpoint includes the finite image minimum", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, std::vector{100, 102}); + const DataPath outputPath = inputPath.replaceName("Output"); + const ThresholdMaximumConnectedComponentsImageFilter filter; + Arguments arguments = filter.getDefaultArguments(); + arguments.insertOrAssign(TMCCFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + arguments.insertOrAssign(TMCCFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); + arguments.insertOrAssign(TMCCFilter::k_OutputImageArrayName_Key, std::make_any("Output")); + arguments.insertOrAssign(TMCCFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(0)); + arguments.insertOrAssign(TMCCFilter::k_UpperBoundary_Key, std::make_any(65536.0)); + arguments.insertOrAssign(TMCCFilter::k_InsideValue_Key, std::make_any(1)); + arguments.insertOrAssign(TMCCFilter::k_OutsideValue_Key, std::make_any(0)); + + const auto preflightResult = filter.preflight(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + const auto executeResult = filter.execute(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + + const IDataArray* outputArrayPtr = nullptr; + REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + REQUIRE(outputArrayPtr->getDataType() == DataType::uint8); + REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 2}); + REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + const UInt8Array* maskPtr = nullptr; + REQUIRE_NOTHROW(maskPtr = &dataStructure.getDataRefAs(outputPath)); + + // Width 2 keeps the approved stopping rule: midpoint 101 gives the literal inclusive-band mask [0, 1]. + // The baseline int32 arithmetic and the finite 65536 boundary conversion are defined for this fixture. + CHECK(maskPtr->getValue(0) == uint8{0}); + CHECK(maskPtr->getValue(1) == uint8{1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native integer endpoints retain exact width-two masks", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool nearMaximum = GENERATE(false, true); + const TestType low = nearMaximum ? static_cast(std::numeric_limits::max() - TestType{2}) : std::numeric_limits::lowest(); + const TestType high = static_cast(low + TestType{2}); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {low, high}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + // Native midpoint is low+1 even at int64/uint64 endpoints beyond Float64's exact integer range. + requireThresholdMask(dataStructure, inputPath, {0, 1}); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: full native integer spans keep the absolute-two heuristic", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::lowest(), std::numeric_limits::max()}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + // Two adjacent pixels always score one component. Left ties halve the odd span until width 1; midpoint is then the native minimum. + requireThresholdMask(dataStructure, inputPath, {1, 1}); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: caps clamp before 64-bit conversion and preserve lower truncation", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + SECTION("signed exclusive high and finite overflow") + { + const float64 cap = GENERATE(0x1p63, std::numeric_limits::max(), std::numeric_limits::infinity()); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::max() - 2, std::numeric_limits::max()}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireThresholdMask(dataStructure, inputPath, {0, 1}); + } + SECTION("unsigned exclusive high and finite overflow") + { + const float64 cap = GENERATE(0x1p64, std::numeric_limits::max(), std::numeric_limits::infinity()); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::max() - 2, std::numeric_limits::max()}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireThresholdMask(dataStructure, inputPath, {0, 1}); + } + SECTION("unsigned negative fraction remains zero") + { + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 2}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, -0.5)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireThresholdMask(dataStructure, inputPath, {1, 0}); + } + SECTION("signed low fraction truncates into range") + { + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {-128, -126}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, -128.75)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireThresholdMask(dataStructure, inputPath, {1, 0}); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: low and empty bands return warned Outside values", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const float64 cap = GENERATE(-1.0, -std::numeric_limits::infinity(), 99.0); + const uint8 outside = GENERATE(uint8{0}, uint8{7}); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {100, 102}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap, false, 1, outside)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -28615); + requireThresholdMask(dataStructure, inputPath, {outside, outside}); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: NaN Upper Boundary is a hard parameter error in both image modes", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool warn = GENERATE(false, true); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {100, 102}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.preflight(dataStructure, thresholdArguments(inputPath, std::numeric_limits::quiet_NaN(), warn)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + REQUIRE(result.outputActions.errors().front().code == -28610); + REQUIRE(result.outputActions.errors().front().message.find("Upper Boundary") != std::string::npos); + REQUIRE(dataStructure.getDataAs(inputPath.replaceName("Output")) == nullptr); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: one image policy covers every nonfinite class and order", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const usize placement = GENERATE(usize{0}, usize{1}, usize{2}); + const bool warn = GENERATE(false, true); + const uint8 outside = GENERATE(uint8{0}, uint8{7}); + std::vector values{0, 2}; + values.insert(values.begin() + static_cast::difference_type>(placement), + {std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}); + std::vector expected{outside, 1}; + expected.insert(expected.begin() + static_cast::difference_type>(placement), {outside, outside, outside}); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, std::numeric_limits::infinity(), warn, 1, outside)); + if(!warn) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -28611); + UnitTest::CheckArraysInheritTupleDims(dataStructure); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -28614); + REQUIRE(result.result.warnings().front().message.find("1 NaN, 1 negative infinity and 1 positive infinity") != std::string::npos); + requireThresholdMask(dataStructure, inputPath, expected); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: all nonfinite Warn skips search and calculates no threshold", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const int32 kind = GENERATE(0, 1, 2, 3); + const uint8 outside = GENERATE(uint8{0}, uint8{7}); + const uint8 inside = GENERATE(uint8{0}, uint8{1}); + const std::array nonfinite{std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}; + const std::vector values = kind == 3 ? std::vector{nonfinite.at(2), nonfinite.at(0), nonfinite.at(1)} : std::vector(3, nonfinite.at(static_cast(kind))); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, values); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, true, inside, outside, 10)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -28614); + REQUIRE(result.result.warnings().front().message.find("no threshold was calculated") != std::string::npos); + requireThresholdMask(dataStructure, inputPath, {outside, outside, outside}); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: floating rounding distinguishes convergence from a repeated wide state", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const bool roundUp = GENERATE(false, true); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, roundUp ? std::vector{33554436.0F, 33554440.0F} : std::vector{33554432.0F, 33554436.0F}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == (roundUp ? 1 : 0)); + if(roundUp) + { + REQUIRE(result.result.warnings().front().code == -28616); + } + // A lower rounded midpoint moves high down to low (width 0); only the upper-rounded state repeats at width 4. + requireThresholdMask(dataStructure, inputPath, roundUp ? std::vector{0, 1} : std::vector{1, 1}); +} + +TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: opposite finite floating extrema keep finite search candidates", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", float32, float64) +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {-std::numeric_limits::max(), std::numeric_limits::max()}); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().code == -28616); + // Every score ties. Near -MAX, adjacent native endpoints are farther than 2 apart; ties-to-even retains the upper candidate. + requireThresholdMask(dataStructure, inputPath, {0, 1}); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonzero scoring minimum sizes and the fixed scale stop are preserved", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const ThresholdMaximumConnectedComponentsImageFilter filter; + SECTION("strict-right score selects isolated bright peaks") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {0, 0, 1, 0, 1, 0, 0, 1}); + } + SECTION("minimum size affects scoring but does not remove the final singleton") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 0, 2)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {0, 1, 1, 1, 1, 1, 0, 1}); + } + SECTION("size exactly one qualifies for a minimum of one") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 0, 1)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {0, 0, 1, 0, 1, 0, 0, 1}); + } + SECTION("zero Inside and nonzero Outside score the complement") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 0, 4, 0, 4, 0, 8}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 0, 1)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {1, 0, 1, 0, 1, 0, 1, 0}); + } + SECTION("both nonzero score the whole image and ties select left") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 7)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {7, 1, 1, 1, 1, 1, 7, 1}); + } + SECTION("binary width one still skips search") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 1, 0, 1}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {1, 1, 1, 1}); + } + SECTION("constant negative data use their actual constant") + { + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {-5, -5}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, {1, 1}); + } + SECTION("normalized width one remains distinct from the scaled image") + { + const bool scaled = GENERATE(false, true); + const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, scaled ? std::vector{0, 40, 10, 40, 10, 40, 0, 100} : std::vector{0, .4, .1, .4, .1, .4, 0, 1}); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + requireThresholdMask(dataStructure, inputPath, scaled ? std::vector{0, 1, 0, 1, 0, 1, 0, 1} : std::vector{0, 0, 0, 0, 0, 0, 0, 1}); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: old JSON receives Error and version two preserves explicit Warn", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const ThresholdMaximumConnectedComponentsImageFilter filter; + REQUIRE(filter.parametersVersion() == 2); + auto json = filter.toJson(filter.getDefaultArguments()); + json.erase("nonfinite_input_handling_index"); + json["parameters_version"] = 1; + const auto oldArguments = filter.fromJson(json); + SIMPLNX_RESULT_REQUIRE_VALID(oldArguments); + REQUIRE(oldArguments.value().value(TMCCFilter::k_NonfiniteInputHandling_Key) == 0); + REQUIRE(oldArguments.warnings().size() == 1); + REQUIRE(oldArguments.warnings().front().code == -5432); + auto arguments = filter.getDefaultArguments(); + arguments.insertOrAssign(TMCCFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + const auto roundTrip = filter.fromJson(filter.toJson(arguments)); + SIMPLNX_RESULT_REQUIRE_VALID(roundTrip); + REQUIRE(roundTrip.value().value(TMCCFilter::k_NonfiniteInputHandling_Key) == 1); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine rejects image nonfinites before empty-cap output and distinguishes empty input", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const std::atomic_bool shouldCancel{false}; + const float64 nonfinite = GENERATE(std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()); + const ThresholdTransferStore input({0, nonfinite, 2}); + ThresholdTransferStore output({99, 99, 99}); + const ImageProcessing::ThresholdMaxCCOptions options{.UpperBoundary = -std::numeric_limits::infinity()}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28611); + REQUIRE(input.reads() == 1); + REQUIRE(output.writes() == 0); + REQUIRE(output.getValue(0) == 99); + const ThresholdTransferStore emptyInput(std::vector{}); + ThresholdTransferStore emptyOutput(std::vector{}); + const auto emptyResult = ImageProcessing::ApplyThresholdMaximumConnectedComponents(emptyInput, emptyOutput, SizeVec3{0, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(emptyResult); + REQUIRE(emptyResult.warnings().empty()); + REQUIRE(emptyInput.reads() == 0); + REQUIRE(emptyOutput.writes() == 0); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no-finite fallback avoids count and reread passes and retains warnings on write failure", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool failWrite = GENERATE(false, true); + const ThresholdTransferStore input({std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}); + ThresholdTransferStore output({99, 99, 99}); + if(failWrite) + { + output.failWrite(); + } + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::ThresholdMaxCCOptions options{.OutsideValue = 7, .WarnOnNonfinite = true}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); + REQUIRE(input.reads() == 1); + REQUIRE(output.writes() == 1); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == -28614); + if(failWrite) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -28632); + REQUIRE(output.getValue(0) == 99); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(output.getValue(0) == 7); + REQUIRE(output.getValue(2) == 7); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: count failures remain strict under image Warn", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto fault = GENERATE(ThresholdFaultType::ReadError, ThresholdFaultType::BadAllocation, ThresholdFaultType::LengthError); + ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 4}); + input.setReadFault(2, fault); // Initial summary succeeds; the first component-count read fails. + ThresholdTransferStore output({99, 99, 99}); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::ThresholdMaxCCOptions options{.WarnOnNonfinite = true}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + int32 expectedCode = -28631; + if(fault == ThresholdFaultType::BadAllocation) + { + expectedCode = -8774; + } + else if(fault == ThresholdFaultType::LengthError) + { + expectedCode = -8776; + } + REQUIRE(result.errors().front().code == expectedCode); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == -28614); + REQUIRE(output.writes() == 0); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: summary and final transfers preserve failures and collected diagnostics", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const int32 stage = GENERATE(0, 1, 2, 3, 4, 5, 6); + ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); + ThresholdTransferStore output({99, 99, 99}); + int32 expectedCode = -28631; + if(stage == 0) + { + input.setReadFault(1, ThresholdFaultType::ReadError); + } + else if(stage == 1) + { + input.setReadFault(1, ThresholdFaultType::BadAllocation); + expectedCode = -28617; + } + else if(stage == 2) + { + input.setReadFault(1, ThresholdFaultType::LengthError); + expectedCode = -28618; + } + else if(stage == 3) + { + input.warnOnRead(); + input.setReadFault(2, ThresholdFaultType::ReadError); + } + else if(stage == 4) + { + output.failWrite(); + expectedCode = -28632; + } + else + { + input.warnOnRead(); + input.setReadFault(2, stage == 5 ? ThresholdFaultType::BadAllocation : ThresholdFaultType::LengthError); + expectedCode = stage == 5 ? -28617 : -28618; + } + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::ThresholdMaxCCOptions options{.WarnOnNonfinite = true}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == expectedCode); + REQUIRE(result.warnings().size() == (stage == 3 || stage >= 5 ? 2 : (stage == 4 ? 1 : 0))); + if(stage == 3 || stage >= 5) + { + REQUIRE(result.warnings().at(0).code == -28633); + REQUIRE(result.warnings().at(1).code == -28614); + } + REQUIRE(output.getValue(0) == 99); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine cancellation keeps warning Results without inventing a local cancellation error", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool cancelBefore = GENERATE(false, true); + std::atomic_bool shouldCancel{cancelBefore}; + const ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); + ThresholdTransferStore output({99, 99, 99}); + output.cancelOnWrite(shouldCancel); + const ImageProcessing::ThresholdMaxCCOptions options{.WarnOnNonfinite = true}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(shouldCancel); + REQUIRE(input.reads() == (cancelBefore ? 0 : 2)); + REQUIRE(output.writes() == (cancelBefore ? 0 : 1)); + REQUIRE(result.warnings().size() == (cancelBefore ? 0 : 1)); + if(!cancelBefore) + { + REQUIRE(result.warnings().front().code == -28614); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: direct engine validates hard parameters and output shape before transfer", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool nanParameter = GENERATE(false, true); + const ThresholdTransferStore input({std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()}); + ThresholdTransferStore output({99}); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::ThresholdMaxCCOptions options{.UpperBoundary = nanParameter ? std::numeric_limits::quiet_NaN() : 65536, .WarnOnNonfinite = true}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{2, 1, 1}, options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == (nanParameter ? -28610 : -28612)); + REQUIRE(input.reads() == 0); + REQUIRE(output.writes() == 0); +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: resident and owned multi-block summaries use finite native extrema", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + constexpr usize count = 131073; + std::vector values(count, 0); + values.at(1) = 2; + values.at(65536) = std::numeric_limits::quiet_NaN(); + values.at(count - 1) = std::numeric_limits::infinity(); + const std::atomic_bool shouldCancel{false}; + SECTION("owned bulk-transfer buffer") + { + const ThresholdTransferStore input(values); + const auto result = ImageProcessing::detail::ScanThresholdMaxCC(input, true, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().Minimum == 0); + REQUIRE(result.value().Maximum == 2); + REQUIRE(result.value().FiniteCount == count - 2); + REQUIRE(result.value().NaNCount == 1); + REQUIRE(result.value().PositiveInfinityCount == 1); + } + SECTION("direct resident span") + { + DataStore input(ShapeType{1, 1, count}, ShapeType{1}, 0.0F); + const auto writeResult = input.copyFromBuffer(0, nonstd::span(values.data(), values.size())); + SIMPLNX_RESULT_REQUIRE_VALID(writeResult); + const auto result = ImageProcessing::detail::ScanThresholdMaxCC(input, true, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().Minimum == 0); + REQUIRE(result.value().Maximum == 2); + REQUIRE(result.value().FiniteCount == count - 2); + REQUIRE(result.value().NaNCount == 1); + REQUIRE(result.value().PositiveInfinityCount == 1); + } +} + +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native span and cap math retain exact bounds and ordinary floating rounding", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(std::numeric_limits::lowest(), std::numeric_limits::max())); + REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(std::numeric_limits::lowest(), std::numeric_limits::max())); + REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(-std::numeric_limits::max(), std::numeric_limits::max())); + REQUIRE_FALSE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(-1.0, 1.0)); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(33554438.0).Value == 33554440.0F); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(33554434.0).Value == 33554432.0F); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(std::numeric_limits::max()).Value == std::numeric_limits::max()); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(-std::numeric_limits::max()).Empty); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(std::nextafter(0x1p63, 0.0)).Value == std::numeric_limits::max() - 1023); + REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(std::nextafter(0x1p64, 0.0)).Value == std::numeric_limits::max() - 2047); +} + // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a valid scalar input (of ANY numeric // type, incl. float) produces a FIXED uint8 output array (AlwaysUInt8). @@ -58,8 +800,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: pr const DataPath scalarPath = rt::BuildImageFromPattern(ds, 6, 6, 1, std::vector(36, 0)); const DataPath vecPath({"Image Geometry", "CellData", "Vec"}); auto vecStore = DataStoreUtilities::CreateDataStore(ds, vecPath, {1, 6, 6}, {3}); - const auto& cellAM = ds.getDataRefAs(DataPath({"Image Geometry", "CellData"})); - DataArray::Create(ds, "Vec", vecStore, cellAM.getId()); + const AttributeMatrix* cellMatrixPtr = nullptr; + REQUIRE_NOTHROW(cellMatrixPtr = &ds.getDataRefAs(DataPath({"Image Geometry", "CellData"}))); + DataArray::Create(ds, "Vec", vecStore, cellMatrixPtr->getId()); const DataPath geomPath = vecPath.getParent().getParent(); TMCCFilter filter; Arguments args; @@ -82,7 +825,10 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: pr TMCCFilter filter; RunTMCC(filter, ds, inputPath, /*minimumObjectSizeInPixels=*/0u, /*upperBoundary=*/65536.0, /*insideValue=*/1u, /*outsideValue=*/0u); const DataPath outputPath({"Image Geometry", "CellData", "Output"}); - REQUIRE(ds.getDataRefAs(outputPath).getDataType() == DataType::uint8); + const IDataArray* outputPtr = nullptr; + REQUIRE_NOTHROW(outputPtr = &ds.getDataRefAs(outputPath)); + REQUIRE(outputPtr->getDataType() == DataType::uint8); + UnitTest::CheckArraysInheritTupleDims(ds); } } @@ -92,22 +838,100 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: pr // ForceInCore. // default: cthead1.png, defaults (MinimumObjectSizeInPixels 0, UpperBoundary 65536, Inside 1, Outside 0). // parameters: cthead1.png, MinimumObjectSizeInPixels = 40 AND UpperBoundary = 150. -// float: RA-Float.nrrd (float32 input, uint8 output), defaults. +// The float fixture below has a separate corrected-midpoint regression with an active legacy reference. // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ITK real-image golden (default)", "[NXImageProcessing][ItkGolden][ThresholdMaximumConnectedComponentsImageFilter]") { + UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("cthead1.png", "c84b75c78c33844251a1095d9cbcffb9"); } TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ITK real-image golden (parameters)", "[NXImageProcessing][ItkGolden][ThresholdMaximumConnectedComponentsImageFilter]") { + UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("cthead1.png", "27c6cf8494fcc4e414f1c420e7a9ca6f", [](Arguments& args) { args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(40u)); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(150.0)); }); } -TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ITK real-image golden (float)", "[NXImageProcessing][ItkGolden][ThresholdMaximumConnectedComponentsImageFilter]") +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: corrected-midpoint real-image regression (float)", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][MidpointMigration]") { - rt::RunReconstructionMd5ItkGolden("RA-Float.nrrd", "e475b27bd0dd66ede330c4eab93c17e9"); + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const DataPath geometryPath({"Image Geometry"}); + const DataPath inputPath({"Image Geometry", "CellData", "Input"}); + const auto readResult = ip_golden::ReadInputImage(dataStructure, ip_golden::InputPath("RA-Float.nrrd"), geometryPath, "CellData", "Input"); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + const IDataArray* inputArrayPtr = nullptr; + REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(inputPath)); + REQUIRE(inputArrayPtr->getDataType() == DataType::float32); + REQUIRE(inputArrayPtr->getTupleShape() == ShapeType{64, 64, 64}); + REQUIRE(inputArrayPtr->getComponentShape() == ShapeType{1}); + REQUIRE(inputArrayPtr->getNumberOfTuples() == 262144); + const Float32Array* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + + // An independent six-neighbor set/stack trace reproduced both hashes twice; correcting only the seed + // also reproduces the new mask. See the issue-1874 companion's supporting/threshold-ra-float-evidence.{py,json}. + // These literal cutoffs classify the real fixture without re-running production search to create expectations. + constexpr float32 legacyThreshold = 24164.05859375F; + constexpr float32 correctedThreshold = 24166.107421875F; + constexpr float32 upperBoundary = 65536.0F; + std::vector legacyMask(inputPtr->getNumberOfTuples()); + std::vector correctedMask(inputPtr->getNumberOfTuples()); + usize legacyForeground = 0; + usize correctedForeground = 0; + usize changedPixels = 0; + bool allFinite = true; + float32 minimum = std::numeric_limits::max(); + float32 maximum = std::numeric_limits::lowest(); + for(usize index = 0; index < inputPtr->getNumberOfTuples(); ++index) + { + const float32 value = inputPtr->getValue(index); + allFinite = allFinite && std::isfinite(value); + if(value < minimum) + { + minimum = value; + } + if(value > maximum) + { + maximum = value; + } + const bool legacyInside = value >= legacyThreshold && value <= upperBoundary; + const bool correctedInside = value >= correctedThreshold && value <= upperBoundary; + legacyMask[index] = legacyInside ? uint8{1} : uint8{0}; + correctedMask[index] = correctedInside ? uint8{1} : uint8{0}; + legacyForeground += static_cast(legacyInside); + correctedForeground += static_cast(correctedInside); + changedPixels += static_cast(legacyInside != correctedInside); + } + REQUIRE(allFinite); + REQUIRE(minimum == -1146.0F); + REQUIRE(maximum == 32767.0F); + REQUIRE(legacyForeground == 99487); + REQUIRE(correctedForeground == 99328); + REQUIRE(changedPixels == 159); + + MD5 legacyHash; + legacyHash.update(legacyMask.data(), static_cast(legacyMask.size())); + legacyHash.finalize(); + REQUIRE(legacyHash.hexdigest() == "e475b27bd0dd66ede330c4eab93c17e9"); // Active reference to the legacy segmentation. + MD5 correctedHash; + correctedHash.update(correctedMask.data(), static_cast(correctedMask.size())); + correctedHash.finalize(); + REQUIRE(correctedHash.hexdigest() == "63d4c611a5633ba010f1857401b13150"); + + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto preflight = filter.preflight(dataStructure, thresholdArguments(inputPath, 65536.0)); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536.0)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().empty()); + requireThresholdMask(dataStructure, inputPath, correctedMask); + REQUIRE(ip_golden::ComputeMd5Hash(dataStructure, inputPath.replaceName("Output")) == "63d4c611a5633ba010f1857401b13150"); + // Independent flood-fill evidence gives 3715 legacy versus 3712 corrected components. The retained search + // can select fewer components after the arithmetic fix; this regression is not an ITK-parity or optimum claim. } diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp new file mode 100644 index 0000000000..0854d9152d --- /dev/null +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -0,0 +1,505 @@ +#pragma once + +#include "simplnx/Common/Array.hpp" +#include "simplnx/Common/Range.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataStore.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/StreamingStatistics.hpp" +#include "simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp" +#include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" +#include "simplnx/Utilities/ParallelDataAlgorithm.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nx::core::ImageProcessing +{ +/** @brief Controls the native threshold search. Byte targets affect staging, not segmentation. */ +struct ThresholdMaxCCOptions +{ + uint32 MinimumObjectSize = 0; + float64 UpperBoundary = 65536.0; + uint8 InsideValue = 1; + uint8 OutsideValue = 0; + bool WarnOnNonfinite = false; + usize CountTarget2DBytes = detail::k_ConnectedComponent2DTargetBytes; +}; + +namespace detail +{ +template +struct ThresholdMaxCCSummary +{ + T Minimum{}; + T Maximum{}; + usize FiniteCount = 0; + usize NaNCount = 0; + usize NegativeInfinityCount = 0; + usize PositiveInfinityCount = 0; +}; + +template +void MergeThresholdMaxCCSummary(ThresholdMaxCCSummary& summary, const ThresholdMaxCCSummary& block) +{ + if(block.FiniteCount != 0) + { + if(summary.FiniteCount == 0) + { + summary.Minimum = block.Minimum; + summary.Maximum = block.Maximum; + } + else + { + summary.Minimum = std::min(summary.Minimum, block.Minimum); + summary.Maximum = std::max(summary.Maximum, block.Maximum); + } + } + summary.FiniteCount += block.FiniteCount; + summary.NaNCount += block.NaNCount; + summary.NegativeInfinityCount += block.NegativeInfinityCount; + summary.PositiveInfinityCount += block.PositiveInfinityCount; +} + +template +ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, bool warnOnNonfinite, const std::atomic_bool& shouldCancel) +{ + ThresholdMaxCCSummary summary; + for(usize index = 0; index < values.size(); ++index) + { + if((index % 65536) == 0 && shouldCancel) + { + break; + } + const T value = values[index]; + if constexpr(std::is_floating_point_v) + { + if(!std::isfinite(value)) + { + if(std::isnan(value)) + { + ++summary.NaNCount; + } + else if(std::signbit(value)) + { + ++summary.NegativeInfinityCount; + } + else + { + ++summary.PositiveInfinityCount; + } + if(!warnOnNonfinite) + { + break; + } + continue; + } + } + if(summary.FiniteCount == 0) + { + summary.Minimum = value; + summary.Maximum = value; + } + else + { + summary.Minimum = std::min(summary.Minimum, value); + summary.Maximum = std::max(summary.Maximum, value); + } + ++summary.FiniteCount; + } + return summary; +} + +/** @brief Scans finite native bounds without changing shared statistics or making a full-image temporary. */ +template +Result> ScanThresholdMaxCC(const AbstractDataStore& input, bool warnOnNonfinite, const std::atomic_bool& shouldCancel) +{ + ThresholdMaxCCSummary summary; + Result<> status; + try + { + const usize total = input.getSize(); + if(total == 0) + { + return {summary}; + } + constexpr usize blockValues = 65536; + constexpr usize residentBatchBytes = 32ULL * 1024ULL * 1024ULL; + const auto* residentPtr = dynamic_cast*>(&input); + const bool direct = input.getStoreType() == IDataStore::StoreType::InMemory && residentPtr != nullptr; + StreamingScanBufferPlan plan; + usize batchValues = std::min(total, residentBatchBytes / sizeof(T)); + usize availableBytes = batchValues * sizeof(T); + if(!direct) + { + auto planResult = CreateStreamingScanBufferPlan(input); + if(planResult.invalid()) + { + return ConvertInvalidResult>(std::move(planResult)); + } + plan = std::move(planResult.value()); + batchValues = std::min(total, plan.batchValues); + availableBytes = batchValues * sizeof(T); + } + usize blockCapacity = 1 + ((batchValues - 1) / blockValues); + if(blockCapacity <= 1 || blockCapacity > (availableBytes / sizeof(ThresholdMaxCCSummary))) + { + blockCapacity = 0; + } + else if(!direct) + { + // Charge the bounded summary table before selecting the owned input payload. + const usize summaryBytes = blockCapacity * sizeof(ThresholdMaxCCSummary); + if(availableBytes - summaryBytes < sizeof(T)) + { + blockCapacity = 0; + } + else + { + batchValues = (availableBytes - summaryBytes) / sizeof(T); + plan.reservation.shrinkTo((batchValues * sizeof(T)) + summaryBytes); + } + } + std::vector> blocks(blockCapacity); + auto valuesPtr = direct ? nullptr : std::make_unique_for_overwrite(batchValues); + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + break; + } + const usize count = std::min(batchValues, total - start); + if(!direct) + { + auto readResult = input.copyIntoBuffer(start, nonstd::span(valuesPtr.get(), count)); + status = MergeResults(std::move(status), std::move(readResult)); + if(status.invalid()) + { + return ConvertInvalidResult>(std::move(status)); + } + } + const nonstd::span values = direct ? residentPtr->createSpan().subspan(start, count) : nonstd::span(valuesPtr.get(), count); + if(blockCapacity == 0) + { + MergeThresholdMaxCCSummary(summary, ReduceThresholdMaxCCBlock(values, warnOnNonfinite, shouldCancel)); + } + else + { + const usize blockCount = 1 + ((count - 1) / blockValues); + ParallelDataAlgorithm algorithm; + algorithm.setRange(0, blockCount); + algorithm.execute([&](const Range& range) { + for(usize block = range.min(); block < range.max(); ++block) + { + const usize offset = block * blockValues; + blocks[block] = ReduceThresholdMaxCCBlock(values.subspan(offset, std::min(blockValues, count - offset)), warnOnNonfinite, shouldCancel); + } + }); + for(usize block = 0; block < blockCount; ++block) + { + MergeThresholdMaxCCSummary(summary, blocks[block]); + } + } + if(!warnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) + { + return ConvertResultTo(MergeResults(std::move(status), MakeErrorResult(-28611, "Threshold Maximum Connected Components found image NaN or infinity during the initial scan. Select Warn and " + "Continue under Nonfinite Input Handling only if Outside Value participation is intended.")), + std::move(summary)); + } + start += count; + } + return {summary, std::move(status.warnings())}; + } catch(const std::bad_alloc&) + { + return ConvertResultTo( + MergeResults(std::move(status), MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its scan buffers. Reduce the data size or provide more memory.")), + std::move(summary)); + } catch(const std::length_error&) + { + return ConvertResultTo(MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable scan buffer size. Reduce the image dimensions.")), + std::move(summary)); + } +} + +template +struct ThresholdMaxCCCap +{ + T Value{}; + bool Empty = false; +}; + +/** @brief NaN is rejected by the caller. Finite integer requests truncate before the lower-bound check. */ +template +ThresholdMaxCCCap ConvertThresholdMaxCCCap(float64 value) +{ + if(value == -std::numeric_limits::infinity()) + { + return {T{}, true}; + } + if(value == std::numeric_limits::infinity()) + { + return {std::numeric_limits::max(), false}; + } + if constexpr(std::is_integral_v) + { + if(std::trunc(value) < static_cast(std::numeric_limits::lowest())) + { + return {T{}, true}; + } + T converted{}; + // The shared pure conversion checks exact exclusive 2^63/2^64 before any integer cast. + ConvertTerminalImageValue(value, converted); + return {converted, false}; + } + else + { + if(value < static_cast(std::numeric_limits::lowest())) + { + return {T{}, true}; + } + return {value >= static_cast(std::numeric_limits::max()) ? std::numeric_limits::max() : NarrowFloatingImageValue(value), false}; + } +} + +template +bool ThresholdMaxCCSpanExceedsTwo(T low, T high) +{ + if constexpr(std::is_integral_v) + { + using UnsignedType = std::make_unsigned_t; + const UnsignedType distance = static_cast(static_cast(high) - static_cast(low)); + return distance > UnsignedType{2}; + } + else + { + // Same-sign subtraction cannot overflow. Opposite large signs already prove width > 2. + if(low < T{0} && high > T{0} && (low < T{-2} || high > T{2})) + { + return true; + } + return (high - low) > T{2}; + } +} + +template +Result SearchThresholdMaxCC(const AbstractDataStore& input, const SizeVec3& dims, T low, T high, T fixedCap, const ThresholdMaxCCOptions& options, const std::atomic_bool& shouldCancel) +{ + T midpoint = std::midpoint(low, high); + T left = std::midpoint(low, midpoint); + T right = std::midpoint(high, midpoint); + Result<> status; + while(ThresholdMaxCCSpanExceedsTwo(low, high) && !shouldCancel) + { + const T oldLow = low; + const T oldHigh = high; + const T oldMidpoint = midpoint; + const auto rightPredicate = [&](T value) { return (value >= right && value <= fixedCap) ? options.InsideValue != 0 : options.OutsideValue != 0; }; + auto rightResult = CountConnectedComponents(input, dims, rightPredicate, false, options.MinimumObjectSize, shouldCancel, options.CountTarget2DBytes); + if(rightResult.invalid()) + { + return ConvertResultTo(MergeResults(std::move(status), ConvertResult(std::move(rightResult))), T{midpoint}); + } + const uint32 rightCount = rightResult.value(); + status = MergeResults(std::move(status), ConvertResult(std::move(rightResult))); + if(shouldCancel) + { + break; + } + const auto leftPredicate = [&](T value) { return (value >= left && value <= fixedCap) ? options.InsideValue != 0 : options.OutsideValue != 0; }; + auto leftResult = CountConnectedComponents(input, dims, leftPredicate, false, options.MinimumObjectSize, shouldCancel, options.CountTarget2DBytes); + if(leftResult.invalid()) + { + return ConvertResultTo(MergeResults(std::move(status), ConvertResult(std::move(leftResult))), T{midpoint}); + } + const uint32 leftCount = leftResult.value(); + status = MergeResults(std::move(status), ConvertResult(std::move(leftResult))); + if(shouldCancel) + { + break; + } + if(rightCount > leftCount) + { + low = midpoint; + midpoint = right; + } + else + { + high = midpoint; + midpoint = left; + } + left = std::midpoint(low, midpoint); + right = std::midpoint(high, midpoint); + if constexpr(std::is_floating_point_v) + { + if(low == oldLow && high == oldHigh && midpoint == oldMidpoint && ThresholdMaxCCSpanExceedsTwo(low, high)) + { + status.warnings().push_back(Warning{.code = -28616, + .message = "Threshold search stopped because native floating precision repeated the complete search state while the interval was wider than " + "2 intensity units. The candidate selected by the normal component-count comparison was retained; segmentation is heuristic."}); + break; + } + } + } + return ConvertResultTo(std::move(status), std::move(midpoint)); +} + +template +Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStore& output, bool fillOutside, T threshold, T cap, const ThresholdMaxCCOptions& options, + const std::atomic_bool& shouldCancel) +{ + Result<> status; + try + { + const usize total = input.getSize(); + constexpr usize minimumValues = 65536; + constexpr usize preferredBytes = 32ULL * 1024ULL * 1024ULL; + const usize valueBytes = fillOutside ? sizeof(uint8) : sizeof(T) + sizeof(uint8); + const uint64 usefulBytes = total > std::numeric_limits::max() / valueBytes ? std::numeric_limits::max() : static_cast(total) * valueBytes; + auto reservation = ReserveWorkingMemory(preferredBytes, usefulBytes); + const usize chunkValues = std::max(minimumValues, std::min(total, static_cast(std::min(reservation.sizeBytes() / valueBytes, std::numeric_limits::max())))); + reservation.shrinkTo(static_cast(std::min(chunkValues, total)) * valueBytes); + auto inputPtr = fillOutside ? nullptr : std::make_unique_for_overwrite(std::min(chunkValues, total)); + auto outputPtr = std::make_unique_for_overwrite(std::min(chunkValues, total)); + for(usize start = 0; start < total;) + { + if(shouldCancel) + { + break; + } + const usize count = std::min(chunkValues, total - start); + if(!fillOutside) + { + auto readResult = input.copyIntoBuffer(start, nonstd::span(inputPtr.get(), count)); + status = MergeResults(std::move(status), std::move(readResult)); + if(status.invalid() || shouldCancel) + { + return status; + } + } + for(usize index = 0; index < count; ++index) + { + outputPtr[index] = !fillOutside && inputPtr[index] >= threshold && inputPtr[index] <= cap ? options.InsideValue : options.OutsideValue; + } + auto writeResult = output.copyFromBuffer(start, nonstd::span(outputPtr.get(), count)); + status = MergeResults(std::move(status), std::move(writeResult)); + if(status.invalid()) + { + return status; + } + start += count; + } + return status; + } catch(const std::bad_alloc&) + { + return MergeResults(std::move(status), MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its output buffers. Reduce the data size or provide more memory.")); + } catch(const std::length_error&) + { + return MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable output buffer size. Reduce the image dimensions.")); + } +} +} // namespace detail + +/** + * @brief Searches finite native bounds with the inherited absolute-two stop and nonzero-output component objective. + * NaN Upper Boundary always fails. Image Error fails in the scan before search/output, even for empty bands. + * Warn retains ordinary inclusive comparisons, or fills Outside with no calculated threshold when no finite data exist. + * Count capacity remains strict. Owned scan/output buffers are bounded; component run tables still depend on image topology. + */ +template +Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& input, AbstractDataStore& output, const SizeVec3& dims, const ThresholdMaxCCOptions& options, + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler = {}) +{ + Result<> status; + if(std::isnan(options.UpperBoundary)) + { + return MakeErrorResult(-28610, "Upper Boundary is NaN. Enter a valid numeric cutoff; positive infinity selects the native maximum and negative infinity requests an empty band."); + } + if(input.getNumberOfComponents() != 1 || output.getNumberOfComponents() != 1 || output.getSize() != input.getSize()) + { + return MakeErrorResult(-28612, "Threshold Maximum Connected Components requires scalar input and uint8 output with matching tuple counts. Recreate the output for the selected image."); + } + usize sliceValues = 0; + usize volumeValues = 0; + if(auto validation = detail::ValidateConnectedComponentVolume(input, dims, sliceValues, volumeValues); validation.invalid()) + { + return validation; + } + try + { + auto summaryResult = detail::ScanThresholdMaxCC(input, options.WarnOnNonfinite, shouldCancel); + if(summaryResult.invalid()) + { + return ConvertResult(std::move(summaryResult)); + } + const auto summary = summaryResult.value(); + status = ConvertResult(std::move(summaryResult)); + if(shouldCancel || input.getSize() == 0) + { + return status; + } + const auto cap = detail::ConvertThresholdMaxCCCap(options.UpperBoundary); + const bool noFinite = summary.FiniteCount == 0; + const bool emptyBand = cap.Empty || (!noFinite && cap.Value < summary.Minimum); + if(summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0) + { + status.warnings().push_back(Warning{ + .code = -28614, + .message = + fmt::format("Input contains {} NaN, {} negative infinity and {} positive infinity pixels. {} Outside Value is {}. Nonzero Outside pixels remain foreground and can connect regions.", + summary.NaNCount, summary.NegativeInfinityCount, summary.PositiveInfinityCount, + noFinite ? "No finite pixels are available; threshold search was skipped and no threshold was calculated. Every output pixel is set to Outside Value." : + "Finite pixels define the search range; nonfinite pixels receive Outside Value through ordinary comparisons.", + options.OutsideValue)}); + } + if(emptyBand && !noFinite) + { + status.warnings().push_back(Warning{.code = -28615, + .message = fmt::format("Upper Boundary requests an empty native intensity band. Threshold search was skipped; every output pixel is set to Outside Value " + "({}). A nonzero Outside Value can make the whole image foreground downstream.", + options.OutsideValue)}); + } + if(noFinite || emptyBand) + { + auto writeResult = detail::WriteThresholdMaxCC(input, output, true, T{}, T{}, options, shouldCancel); + return MergeResults(std::move(status), std::move(writeResult)); + } + auto thresholdResult = detail::SearchThresholdMaxCC(input, dims, summary.Minimum, std::min(summary.Maximum, cap.Value), cap.Value, options, shouldCancel); + if(thresholdResult.invalid()) + { + return MergeResults(std::move(status), ConvertResult(std::move(thresholdResult))); + } + const T threshold = thresholdResult.value(); + status = MergeResults(std::move(status), ConvertResult(std::move(thresholdResult))); + if(shouldCancel) + { + return status; + } + messageHandler.sendInfoMessage("Applying the selected native threshold in bounded batches"); + auto writeResult = detail::WriteThresholdMaxCC(input, output, false, threshold, cap.Value, options, shouldCancel); + return MergeResults(std::move(status), std::move(writeResult)); + } catch(const std::bad_alloc&) + { + return MergeResults(std::move(status), + MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its scan or output buffers. Reduce the data size or provide more memory.")); + } catch(const std::length_error&) + { + return MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable buffer size. Reduce the image dimensions.")); + } +} +} // namespace nx::core::ImageProcessing From c94a86589851e78877cbdd9fec3f0156787a4a2d Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 04:36:29 -0400 Subject: [PATCH 22/98] BUG: Clarify ImageProcessing numeric migration help Describe the public cancellation limit on smoothing warnings and qualify Otsu label compatibility when numeric checks reject a finite calculation. Validation: source-aligned scoped review and documentation checks passed. The unchanged numeric code passed paired full Release builds and all ImageProcessing cases: 1445 InCore and 1651 OOC. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md | 2 +- .../docs/SmoothingRecursiveGaussianImageFilter.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md index 29756099d1..ee4ef2f16f 100644 --- a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md @@ -39,7 +39,7 @@ Histogram bounds, bin measurements, and threshold fitting use `float64`. The fil The finite tie tolerance, ordinary threshold search, and Valley Emphasis behavior are retained. **ReturnBinMidpoint** selects a bin midpoint instead of its maximum for the returned threshold. These checks add no automatic normalization or new scientific fitting rule. -Existing pipelines receive Error, Use Mask Array off, and Masked Label 255 as defaults. Finite pipelines with valid label intervals retain their labels. Pipelines that previously contained NaN/infinity now stop by default. Choose Warn to keep the stated endpoint-bin participation, or supply an explicit mask to exclude samples. Replacing nonfinite values upstream adds replacement intensities to the histogram and can bias thresholds; it is not statistical exclusion. +Existing pipelines receive Error, Use Mask Array off, and Masked Label 255 as defaults. Ordinary finite pipelines with valid label intervals and usable histogram and solver arithmetic retain their labels. A finite pipeline that previously completed can now stop with a numeric error if the new checks detect an unusable calculation. Pipelines that previously contained NaN/infinity now stop by default. Choose Warn to keep the stated endpoint-bin participation, or supply an explicit mask to exclude samples. Replacing nonfinite values upstream adds replacement intensities to the histogram and can bias thresholds; it is not statistical exclusion. This filter also includes an option to use the valley emphasis algorithm from H.F. Ng, "Automatic thresholding for defect detection", Pattern Recognition Letters, (27): 1644-1649, 2006. The valley emphasis algorithm is particularly effective when the object to be thresholded is small. It is turned off by default; enable it with **Valley Emphasis**. diff --git a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md index 417796d55f..feff6acd8c 100644 --- a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md @@ -50,7 +50,7 @@ The same terminal policy applies to all five output routes: | Three-dimensional plane cascade | Checked conversion after the Z, X, and Y passes | | Bounded two-dimensional disk cascade | Checked conversion before each output block is written | -Warnings from completed transfers remain available if a later operation fails or the run is cancelled. Cancellation and later I/O failures can leave completed output blocks in place; this filter does not promise an all-or-nothing output update. +The smoothing helpers retain warnings delivered by completed transfers on their own success, error, and cancellation returns. However, the public filter wrapper can replace a successful execution result with a cancellation error and discard its execution warnings. Warning retention after cancellation is therefore not guaranteed at the public filter boundary. Cancellation and later I/O failures can leave completed output blocks in place; this filter does not promise an all-or-nothing output update. ### Migration and downstream use From 862799caa4ba96baac28c0dc096f00deab50c5fb Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 27 Oct 2026 11:06:30 -0400 Subject: [PATCH 23/98] DOC: Complete ImageProcessing numeric helper contracts Document template and parameter roles, output state, threshold caller preconditions, cancellation results and existing numerical rationale. Executable C++ tokens and API signatures are unchanged. Validation: exact clang-format 19.1.1, lexical token comparison and independent local review passed. New and changed comment blocks have no audit findings; unchanged older comments remain outside this correction. The unchanged numerical code passed 516 InCore and 611 OOC test cases. Signed-off-by: Jessica Marquis --- .../utils/MathOutputUtilities.hpp | 13 +++- .../ImageProcessingConstants.hpp | 1 + .../ImageProcessing/OtsuImageEngine.hpp | 61 ++++++++++++++++--- .../TerminalImageConversion.hpp | 35 ++++++++--- ...esholdMaximumConnectedComponentsEngine.hpp | 44 ++++++++++--- 5 files changed, 127 insertions(+), 27 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp index 5c397b253c..b9faff33fd 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp @@ -7,13 +7,20 @@ namespace nx::core::ImageProcessing { -/** @brief Preserve floating input types and store integer-input math results as float64. */ +/** + * @brief Maps integer input to float64 and retains floating input types. + * @tparam T Numeric input type. + */ template using IntegerToFloat64Type = std::conditional_t, float64, T>; /** - * @brief Convert a floating math result, using signed infinity outside the float32 finite range. - * NaN, infinities, signed zero, and ordinary subnormal or zero underflow retain floating semantics. + * @brief Converts a math result to its selected floating output type. + * @tparam OutputType float32 or float64 output type. + * @param value Result calculated in float64. + * @return The converted value. For float32 output, values outside the finite range become signed infinity. + * + * NaN, infinity, signed zero, and subnormal or zero underflow retain floating behavior. */ template OutputType ConvertMathResult(float64 value) noexcept diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp index b42fcc900a..aa4de3abc2 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp @@ -55,6 +55,7 @@ using SameAsInput = T; /** * @brief Maps integer input to float32 and retains floating input types. + * @tparam T Numeric input type. */ template using IntegerToFloat32Type = std::conditional_t, float32, T>; diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 9573a991fc..93df02c855 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -28,7 +28,10 @@ namespace nx::core::ImageProcessing { -/** @brief Selected Otsu controls. All output class labels remain uint8. */ +/** + * @struct OtsuImageOptions + * @brief Holds Otsu controls for fixed uint8 output labels. + */ struct OtsuImageOptions { uint32 NumberOfBins = 128; @@ -41,7 +44,10 @@ struct OtsuImageOptions usize TargetBytes = 64ULL * 1024ULL * 1024ULL; }; -/** @brief Histogram plus selected sample counts. Excluded samples are absent from every count. */ +/** + * @struct OtsuHistogramResult + * @brief Holds the selected histogram and sample counts. Excluded samples are absent from every count. + */ struct OtsuHistogramResult { Histogram HistogramData; @@ -349,8 +355,17 @@ Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T /** * @brief Builds the selected Otsu histogram without changing generic histogram consumers. - * @details Mask selection precedes nonfinite/range checks. Compact native integral ranges retain exact fixed-ring - * frequencies; other ranges use a second bounded pass. A valid cancellation is returned to the owning filter. + * @tparam T Numeric input value type. + * @tparam MaskT Boolean or uint8 selection-mask type. + * @param input Scalar source values. + * @param maskPtr Optional scalar mask with the input tuple count, paired in flat order. Null selects every input value. + * @param options Histogram-bin count, nonfinite policy, and transfer-memory target. + * @param shouldCancel Requests cancellation during scans and histogram construction. + * @return Histogram and selected finite and nonfinite sample counts, or an input, numeric, or transfer error. + * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. + * + * Mask selection precedes nonfinite and range checks. Compact native integer ranges retain exact frequencies in a fixed ring. + * Other ranges use a second bounded pass. */ template Result ComputeOtsuHistogram(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) @@ -565,7 +580,16 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp return ConvertResultTo(std::move(rangeResult), std::move(result)); } -/** @brief Runs the existing finite solver with opt-in numeric checks and cooperative cancellation. */ +/** + * @brief Runs the existing finite Otsu solver with opt-in numeric checks and cooperative cancellation. + * @param histogram Selected histogram with usable bounds, bin width, and counts. + * @param options Threshold count, Valley Emphasis, and bin-midpoint selection. + * @param shouldCancel Requests cancellation during threshold search. + * @return Ordered threshold values, or a configuration or numeric error. Cancellation can return a valid empty vector. + * The caller must check shouldCancel before classification. + * + * A true constant uses repeated thresholds at that constant. Finite tie and Valley Emphasis rules remain unchanged. + */ inline Result> ComputeCheckedOtsuThresholds(const Histogram& histogram, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) { if(shouldCancel) @@ -592,7 +616,15 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram return detail::ComputeOtsuThresholds(histogram, options.NumberOfThresholds, options.ValleyEmphasis, options.ReturnBinMidpoint, &shouldCancel); } -/** @brief Maps selected values to ordinary uint8 classes; equality stays in the lower class. */ +/** + * @brief Maps one selected value to an ordinary uint8 class; equality stays in the lower class. + * @tparam T Numeric input value type. + * @param value Native value to classify. NaN and negative infinity use the lowest class; positive infinity uses the highest class. + * @param thresholds Finite thresholds in ascending order, with exactly NumberOfThresholds entries. + * @param options Class offset and threshold count. Their sum must not exceed 255. + * @return The ordinary class label. Masked-label selection belongs to the caller. + * @pre The caller has validated the class interval and threshold vector and has enforced the selected nonfinite policy. + */ template uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const OtsuImageOptions& options) { @@ -607,7 +639,22 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O return static_cast(static_cast(options.LabelOffset) + index); } -/** @brief Applies a checked Otsu result; an optional mask is read in flat tuple order in bounded owned chunks. */ +/** + * @brief Fits an Otsu histogram and writes selected classes and optional masked labels. + * @tparam T Numeric input value type. + * @tparam MaskT Boolean or uint8 selection-mask type. + * @param input Scalar source image. + * @param output Scalar uint8 destination with the input value count. + * @param maskPtr Optional scalar mask with the input tuple count, paired in flat order. Null selects every value. + * @param options Histogram, class-label, nonfinite, and transfer controls. + * @param shouldCancel Requests cancellation during scans, search, and output work. + * @param messageHandler Receives output-phase progress messages. + * @return Collected diagnostics, including the nonfinite participation warning, or a validation, numeric, or transfer error. + * Cancellation retains the valid engine-result convention. + * + * Mask selection precedes histogram and nonfinite checks. The optional mask is read in bounded owned chunks. + * A later write failure or cancellation can leave completed output batches in place. + */ template Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore& output, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) diff --git a/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp index cdbea9ef0b..77be4ddf96 100644 --- a/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp +++ b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp @@ -7,18 +7,25 @@ namespace nx::core::ImageProcessing::detail { -/** @brief Reports the final conversion without allocating diagnostics inside a pixel loop. */ +/** + * @enum TerminalImageConversionStatus + * @brief Reports the final conversion without allocating diagnostics inside a pixel loop. + */ enum class TerminalImageConversionStatus : std::uint8_t { - Valid, - Clipped, - NonFinite + Valid, ///< A converted result is available without integer clipping. + Clipped, ///< A finite integer result was clipped after truncation. + NonFinite ///< A nonfinite integer result was rejected; output is unchanged. }; /** - * @brief Retains floating behavior and explicitly maps finite narrowing overflow to signed infinity. - * The input must already be float32 or float64. Preserve existing direct integer-to-float32 conversions at callers; - * passing those integers through float64 first can introduce double rounding. + * @brief Converts floating image values and maps finite narrowing overflow to signed infinity. + * @tparam OutputType float or double destination type. + * @tparam InputType float or double source type. + * @param value Floating value to convert. + * @return The converted value, retaining NaN, infinity, and signed zero behavior. + * + * Preserve direct integer-to-float32 conversions at callers. Passing those integers through float64 first can introduce double rounding. */ template OutputType NarrowFloatingImageValue(InputType value) @@ -38,9 +45,17 @@ OutputType NarrowFloatingImageValue(InputType value) } /** - * @brief Converts one final image value. Finite integers truncate before clipping; nonfinite integers fail. - * The binary exclusive upper bound is exact at 64-bit widths, unlike a floating conversion of the native maximum. - * This function does not change intermediate image precision or aggregate warnings. + * @brief Converts one final image value without changing intermediate image precision. + * @tparam OutputType Integer, float, or double output type. + * @tparam InputType float or double working-value type. + * @param value Final working value to convert. + * @param output Receives the converted value. Remains unchanged when NonFinite is returned. + * @return Valid for a floating conversion or an integer conversion within range. Clipped for a finite integer result outside the range after truncation. + * NonFinite only for a nonfinite value with an integer output type. + * + * A Valid floating result can be NaN or infinity. Finite integer values truncate toward zero before clipping. + * The exact binary exclusive upper bound avoids rounding a native 64-bit maximum into an unsafe cast. + * This helper does not aggregate warnings. */ template TerminalImageConversionStatus ConvertTerminalImageValue(InputType value, OutputType& output) diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index 0854d9152d..14dbf8db98 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -31,7 +31,10 @@ namespace nx::core::ImageProcessing { -/** @brief Controls the native threshold search. Byte targets affect staging, not segmentation. */ +/** + * @struct ThresholdMaxCCOptions + * @brief Controls the native threshold search. Byte targets affect staging, not segmentation. + */ struct ThresholdMaxCCOptions { uint32 MinimumObjectSize = 0; @@ -126,7 +129,15 @@ ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, return summary; } -/** @brief Scans finite native bounds without changing shared statistics or making a full-image temporary. */ +/** + * @brief Scans finite native bounds without changing shared statistics or making a full-image temporary. + * @tparam T Numeric input value type. + * @param input Source values to scan. + * @param warnOnNonfinite If true, counts nonfinite samples and continues. If false, a nonfinite sample causes an error. + * @param shouldCancel Requests cancellation during the scan. + * @return Native finite bounds and sample counts, or a transfer or allocation error, or an error for disallowed nonfinite samples. + * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. + */ template Result> ScanThresholdMaxCC(const AbstractDataStore& input, bool warnOnNonfinite, const std::atomic_bool& shouldCancel) { @@ -244,7 +255,15 @@ struct ThresholdMaxCCCap bool Empty = false; }; -/** @brief NaN is rejected by the caller. Finite integer requests truncate before the lower-bound check. */ +/** + * @brief Converts an Upper Boundary request to a native finite cap or an empty-band result. + * @tparam T Numeric input value type used for the cap. + * @param value Upper Boundary request. Positive infinity selects the native maximum; negative infinity requests an empty band. + * @return Native cap with Empty false, or Empty true for negative infinity or a request below the native lower bound. + * @pre The caller has rejected NaN. + * + * For integer input types, finite requests truncate before the lower-bound check. Values above the native maximum use the native maximum. + */ template ThresholdMaxCCCap ConvertThresholdMaxCCCap(float64 value) { @@ -416,10 +435,21 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor } // namespace detail /** - * @brief Searches finite native bounds with the inherited absolute-two stop and nonzero-output component objective. - * NaN Upper Boundary always fails. Image Error fails in the scan before search/output, even for empty bands. - * Warn retains ordinary inclusive comparisons, or fills Outside with no calculated threshold when no finite data exist. - * Count capacity remains strict. Owned scan/output buffers are bounded; component run tables still depend on image topology. + * @brief Searches native finite bounds using the nonzero-output component-count heuristic and absolute two-unit stop. + * @tparam T Numeric input value type. + * @param input Scalar source image in X-fastest order. + * @param output Scalar uint8 destination with the input value count. + * @param dims Image dimensions in X, Y, Z order. + * @param options Component-size, cutoff, output-value, nonfinite, and staging controls. + * @param shouldCancel Requests cancellation during scan, search, and output work. + * @param messageHandler Receives output-phase progress messages. + * @return Collected warnings, or a parameter, shape, numeric-capacity, allocation, or transfer error. + * Cancellation retains the valid engine-result convention. + * + * NaN Upper Boundary always fails. Image Error fails in the initial scan, including for an empty requested band. + * Warn retains ordinary inclusive comparisons. With no finite data, the engine fills Outside Value and reports that no threshold was calculated. + * Count capacity remains strict. Owned scan and output buffers are bounded; component-run tables depend on image topology. + * Completed output is not rolled back after a late failure or cancellation. */ template Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& input, AbstractDataStore& output, const SizeVec3& dims, const ThresholdMaxCCOptions& options, From 89369f316e544edc0435d4d269a313c86eb68546 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 2 Nov 2026 16:50:40 -0500 Subject: [PATCH 24/98] BUG: Save C15 policy defaults in shipped pipelines Add the new policy and mask parameters to the affected pipeline files. Keep the approved Error defaults and disabled Otsu masking. Update the matching examples and sidecars without changing scientific settings. The strict prebuilt parameter-key test passes in both configurations. Semantic and syntax checks pass for all 42 changed artifacts. Signed-off-by: Jessica Marquis --- ...5_AM_XCT_Porosity_Segmentation.d3dpipeline | 12 ++++++-- .../05_AM_XCT_Porosity_Segmentation.md | 2 ++ .../05_AM_XCT_Porosity_Segmentation.yaml | 4 ++- ...AM_Powder-Bed_Layer_Inspection.d3dpipeline | 12 ++++++-- .../06_AM_Powder-Bed_Layer_Inspection.md | 2 ++ .../06_AM_Powder-Bed_Layer_Inspection.yaml | 2 ++ ...Melt-Pool_and_Track_Inspection.d3dpipeline | 30 +++++++++++++++++-- .../07_AM_Melt-Pool_and_Track_Inspection.md | 6 ++++ .../07_AM_Melt-Pool_and_Track_Inspection.yaml | 8 ++++- ...article_Watershed_Segmentation.d3dpipeline | 12 ++++++-- ..._Powder_Particle_Watershed_Segmentation.md | 2 ++ ...owder_Particle_Watershed_Segmentation.yaml | 4 ++- ...ask_Repair_and_Skeletonization.d3dpipeline | 6 +++- ..._Binary_Mask_Repair_and_Skeletonization.md | 1 + ...inary_Mask_Repair_and_Skeletonization.yaml | 3 +- ...ntific_Volume_Interoperability.d3dpipeline | 12 ++++++-- .../17_Scientific_Volume_Interoperability.md | 2 ++ ...17_Scientific_Volume_Interoperability.yaml | 2 ++ ...l-Section_Stack_Reconstruction.d3dpipeline | 6 +++- .../19_Serial-Section_Stack_Reconstruction.md | 1 + ...9_Serial-Section_Stack_Reconstruction.yaml | 1 + .../EBSD_Hexagonal_Data_Analysis.d3dpipeline | 12 ++++++-- .../Edax_IPF_Colors.d3dpipeline | 6 +++- .../TxCopper_Exposed.d3dpipeline | 6 +++- .../TxCopper_Unexposed.d3dpipeline | 6 +++- .../aptr12_Analysis.d3dpipeline | 18 +++++++++-- .../avtr12_Analysis.d3dpipeline | 18 +++++++++-- .../Import_CSV_File.d3dpipeline | 6 +++- .../05_AM_XCT_Porosity_Segmentation.py | 2 ++ .../06_AM_Powder-Bed_Layer_Inspection.py | 2 ++ .../07_AM_Melt-Pool_and_Track_Inspection.py | 6 ++++ ..._Powder_Particle_Watershed_Segmentation.py | 2 ++ ..._Binary_Mask_Repair_and_Skeletonization.py | 1 + .../17_Scientific_Volume_Interoperability.py | 2 ++ .../19_Serial-Section_Stack_Reconstruction.py | 1 + .../OrientationAnalysis/APTR12_Analysis.py | 3 ++ .../OrientationAnalysis/AVTR12_Analysis.py | 3 ++ .../OrientationAnalysis/Edax_IPF_Colors.py | 1 + .../OrientationAnalysis/TxCopper_Exposed.py | 1 + .../OrientationAnalysis/TxCopper_Unexposed.py | 1 + .../pipelines/Simplnx/Import_ASCII.py | 1 + .../python/examples/scripts/basic_ebsd_ipf.py | 1 + 42 files changed, 200 insertions(+), 29 deletions(-) diff --git a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.d3dpipeline index f0c7939be3..78771a8053 100644 --- a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.d3dpipeline @@ -284,7 +284,11 @@ "value": "Pore Labels", "version": 1 }, - "parameters_version": 1 + "parameters_version": 2, + "provisional_label_limit_handling_index": { + "value": 0, + "version": 1 + } }, "comments": "Assign one label to each face-connected pore.", "filter": { @@ -444,7 +448,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.md b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.md index ea05d3e74a..97cbd40ff7 100644 --- a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.md +++ b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.md @@ -216,6 +216,7 @@ Key parameters: - `input_image_data_path`: `"AM XCT/Cell Data/Closed Pore Mask"` - `input_image_geometry_path`: `"AM XCT"` - `output_array_name`: `"Pore Labels"` +- `provisional_label_limit_handling_index`: `0` Resolved inputs: @@ -316,6 +317,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` diff --git a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.yaml b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.yaml index de6b515054..e8a0095a7d 100644 --- a/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/05_AM_XCT_Porosity_Segmentation/05_AM_XCT_Porosity_Segmentation.yaml @@ -285,7 +285,8 @@ "fully_connected": false, "input_image_data_path": "AM XCT/Cell Data/Closed Pore Mask", "input_image_geometry_path": "AM XCT", - "output_array_name": "Pore Labels" + "output_array_name": "Pore Labels", + "provisional_label_limit_handling_index": 0 } }, { @@ -410,6 +411,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, diff --git a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.d3dpipeline index 348af15e43..d96fcea94d 100644 --- a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.d3dpipeline @@ -267,6 +267,10 @@ "value": 16, "version": 1 }, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, "output_array_name": { "value": "Automatic Anomaly Mask", "version": 1 @@ -275,7 +279,7 @@ "value": 0, "version": 1 }, - "parameters_version": 1, + "parameters_version": 2, "upper_boundary": { "value": 255.0, "version": 1 @@ -377,7 +381,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.md b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.md index 7d3aab5bda..fd33d3a9f1 100644 --- a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.md +++ b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.md @@ -172,6 +172,7 @@ Key parameters: - `input_image_geometry_path`: `"Powder Bed Layers"` - `inside_value`: `1` - `minimum_object_size_in_pixels`: `16` +- `nonfinite_input_handling_index`: `0` - `output_array_name`: `"Automatic Anomaly Mask"` - `outside_value`: `0` - `upper_boundary`: `255.0` @@ -228,6 +229,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` diff --git a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.yaml b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.yaml index 3d1c7d809c..609ed56aeb 100644 --- a/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/06_AM_Powder-Bed_Layer_Inspection/06_AM_Powder-Bed_Layer_Inspection.yaml @@ -270,6 +270,7 @@ "input_image_geometry_path": "Powder Bed Layers", "inside_value": 1, "minimum_object_size_in_pixels": 16, + "nonfinite_input_handling_index": 0, "output_array_name": "Automatic Anomaly Mask", "outside_value": 0, "upper_boundary": 255.0 @@ -342,6 +343,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, diff --git a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.d3dpipeline index 30568d57a6..a354606b35 100644 --- a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.d3dpipeline @@ -183,6 +183,18 @@ "value": 0, "version": 1 }, + "mask_array_path": { + "value": "", + "version": 1 + }, + "masked_label": { + "value": 255, + "version": 1 + }, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, "number_of_histogram_bins": { "value": 128, "version": 1 @@ -195,11 +207,15 @@ "value": "Otsu Regions", "version": 1 }, - "parameters_version": 1, + "parameters_version": 2, "return_bin_midpoint": { "value": false, "version": 1 }, + "use_mask_array": { + "value": false, + "version": 1 + }, "valley_emphasis": { "value": true, "version": 1 @@ -281,7 +297,11 @@ "value": "Track Labels", "version": 1 }, - "parameters_version": 1 + "parameters_version": 2, + "provisional_label_limit_handling_index": { + "value": 0, + "version": 1 + } }, "comments": "Label the melt pool and separated spatter regions.", "filter": { @@ -398,7 +418,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.md b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.md index ac78e5a8e5..e2054295ec 100644 --- a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.md +++ b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.md @@ -143,10 +143,14 @@ Key parameters: - `input_image_data_path`: `"Melt Pool Image/Cell Data/Sigmoid Contrast"` - `input_image_geometry_path`: `"Melt Pool Image"` - `label_offset`: `0` +- `mask_array_path`: `""` +- `masked_label`: `255` +- `nonfinite_input_handling_index`: `0` - `number_of_histogram_bins`: `128` - `number_of_thresholds`: `2` - `output_array_name`: `"Otsu Regions"` - `return_bin_midpoint`: `false` +- `use_mask_array`: `false` - `valley_emphasis`: `true` Resolved inputs: @@ -198,6 +202,7 @@ Key parameters: - `input_image_data_path`: `"Melt Pool Image/Cell Data/Track Mask"` - `input_image_geometry_path`: `"Melt Pool Image"` - `output_array_name`: `"Track Labels"` +- `provisional_label_limit_handling_index`: `0` Resolved inputs: @@ -272,6 +277,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` diff --git a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.yaml b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.yaml index ebee4e1ad1..d619c0bbf2 100644 --- a/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/07_AM_Melt-Pool_and_Track_Inspection/07_AM_Melt-Pool_and_Track_Inspection.yaml @@ -196,10 +196,14 @@ "input_image_data_path": "Melt Pool Image/Cell Data/Sigmoid Contrast", "input_image_geometry_path": "Melt Pool Image", "label_offset": 0, + "mask_array_path": "", + "masked_label": 255, + "nonfinite_input_handling_index": 0, "number_of_histogram_bins": 128, "number_of_thresholds": 2, "output_array_name": "Otsu Regions", "return_bin_midpoint": false, + "use_mask_array": false, "valley_emphasis": true } }, @@ -262,7 +266,8 @@ "fully_connected": true, "input_image_data_path": "Melt Pool Image/Cell Data/Track Mask", "input_image_geometry_path": "Melt Pool Image", - "output_array_name": "Track Labels" + "output_array_name": "Track Labels", + "provisional_label_limit_handling_index": 0 } }, { @@ -355,6 +360,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, diff --git a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.d3dpipeline index 6d51681ed0..9e7e6295e9 100644 --- a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.d3dpipeline @@ -273,7 +273,11 @@ "value": "Marker Labels", "version": 1 }, - "parameters_version": 1 + "parameters_version": 2, + "provisional_label_limit_handling_index": { + "value": 0, + "version": 1 + } }, "comments": "Assign a unique label to each watershed marker.", "filter": { @@ -300,6 +304,10 @@ "value": true, "version": 1 }, + "marker_id_conversion_handling_index": { + "value": 0, + "version": 1 + }, "marker_image_data_path": { "value": "Powder Particles/Cell Data/Marker Labels", "version": 1 @@ -308,7 +316,7 @@ "value": "Particle Labels", "version": 1 }, - "parameters_version": 1 + "parameters_version": 2 }, "comments": "Grow the particle markers across the gradient surface without merging touching particles.", "filter": { diff --git a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.md b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.md index 7b2fac2d1d..ddc72fc608 100644 --- a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.md +++ b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.md @@ -194,6 +194,7 @@ Key parameters: - `input_image_data_path`: `"Powder Particles/Cell Data/Marker Mask"` - `input_image_geometry_path`: `"Powder Particles"` - `output_array_name`: `"Marker Labels"` +- `provisional_label_limit_handling_index`: `0` Resolved inputs: @@ -216,6 +217,7 @@ Key parameters: - `input_image_data_path`: `"Powder Particles/Cell Data/Particle Gradient"` - `input_image_geometry_path`: `"Powder Particles"` - `mark_watershed_line`: `true` +- `marker_id_conversion_handling_index`: `0` - `marker_image_data_path`: `"Powder Particles/Cell Data/Marker Labels"` - `output_array_name`: `"Particle Labels"` diff --git a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.yaml b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.yaml index 65eaa6fe09..ca2159ff07 100644 --- a/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/08_Powder_Particle_Watershed_Segmentation/08_Powder_Particle_Watershed_Segmentation.yaml @@ -262,7 +262,8 @@ "fully_connected": true, "input_image_data_path": "Powder Particles/Cell Data/Marker Mask", "input_image_geometry_path": "Powder Particles", - "output_array_name": "Marker Labels" + "output_array_name": "Marker Labels", + "provisional_label_limit_handling_index": 0 } }, { @@ -291,6 +292,7 @@ "input_image_data_path": "Powder Particles/Cell Data/Particle Gradient", "input_image_geometry_path": "Powder Particles", "mark_watershed_line": true, + "marker_id_conversion_handling_index": 0, "marker_image_data_path": "Powder Particles/Cell Data/Marker Labels", "output_array_name": "Particle Labels" } diff --git a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.d3dpipeline index febd6733c1..7d0895ef35 100644 --- a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.d3dpipeline @@ -398,7 +398,11 @@ "value": "Object Labels", "version": 1 }, - "parameters_version": 1 + "parameters_version": 2, + "provisional_label_limit_handling_index": { + "value": 0, + "version": 1 + } }, "comments": "Assign one label to each mask object.", "filter": { diff --git a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.md b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.md index f7e66e94c8..a365b94a4b 100644 --- a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.md +++ b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.md @@ -268,6 +268,7 @@ Key parameters: - `input_image_data_path`: `"Binary Mask/Cell Data/Mask"` - `input_image_geometry_path`: `"Binary Mask"` - `output_array_name`: `"Object Labels"` +- `provisional_label_limit_handling_index`: `0` Resolved inputs: diff --git a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.yaml b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.yaml index 7df583e2ed..a0c5557161 100644 --- a/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/10_Binary_Mask_Repair_and_Skeletonization/10_Binary_Mask_Repair_and_Skeletonization.yaml @@ -366,7 +366,8 @@ "fully_connected": true, "input_image_data_path": "Binary Mask/Cell Data/Mask", "input_image_geometry_path": "Binary Mask", - "output_array_name": "Object Labels" + "output_array_name": "Object Labels", + "provisional_label_limit_handling_index": 0 } }, { diff --git a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.d3dpipeline index cce2b29ef7..9ccc93c1bb 100644 --- a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.d3dpipeline @@ -198,7 +198,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -269,7 +273,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.md b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.md index 918b53e715..7b2b0871e0 100644 --- a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.md +++ b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.md @@ -120,6 +120,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` @@ -151,6 +152,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` diff --git a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.yaml b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.yaml index 78e5f4fcb3..1ac9daa228 100644 --- a/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/17_Scientific_Volume_Interoperability/17_Scientific_Volume_Interoperability.yaml @@ -202,6 +202,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, @@ -240,6 +241,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, diff --git a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.d3dpipeline b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.d3dpipeline index 9c0848d4aa..9323c27b63 100644 --- a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.d3dpipeline +++ b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.d3dpipeline @@ -186,7 +186,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.md b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.md index 6d40697fb3..6c82f0407c 100644 --- a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.md +++ b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.md @@ -108,6 +108,7 @@ Key parameters: - `invalid_color_value`: `[0,0,0]` - `leading_digit_character`: `"0"` - `mask_array_path`: `""` +- `nonfinite_input_handling_index`: `0` - `plane_index`: `0` - `selected_preset`: `"Black-Body Radiation"` - `total_index_digits`: `3` diff --git a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.yaml b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.yaml index 5829cf0ac1..3d75eb78dd 100644 --- a/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.yaml +++ b/src/Plugins/NXImageProcessing/pipelines/19_Serial-Section_Stack_Reconstruction/19_Serial-Section_Stack_Reconstruction.yaml @@ -176,6 +176,7 @@ ], "leading_digit_character": "0", "mask_array_path": "", + "nonfinite_input_handling_index": 0, "plane_index": 0, "selected_preset": "Black-Body Radiation", "total_index_digits": 3, diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/EBSD_Hexagonal_Data_Analysis.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/EBSD_Hexagonal_Data_Analysis.d3dpipeline index 153502e6af..aa2614049b 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/EBSD_Hexagonal_Data_Analysis.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/EBSD_Hexagonal_Data_Analysis.d3dpipeline @@ -274,7 +274,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -633,7 +637,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/Edax_IPF_Colors.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/Edax_IPF_Colors.d3dpipeline index 0c1b21196f..81db1cb2be 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/Edax_IPF_Colors.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/Edax_IPF_Colors.d3dpipeline @@ -265,7 +265,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Exposed.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Exposed.d3dpipeline index 82cbc8a627..6a7a01f10b 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Exposed.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Exposed.d3dpipeline @@ -332,7 +332,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Unexposed.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Unexposed.d3dpipeline index b4cfec64c1..b749ffb4ec 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Unexposed.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/TxCopper_Unexposed.d3dpipeline @@ -332,7 +332,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/aptr12_Analysis.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/aptr12_Analysis.d3dpipeline index b5a8a450bc..d240a63d3a 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/aptr12_Analysis.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/aptr12_Analysis.d3dpipeline @@ -316,7 +316,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -434,7 +438,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -552,7 +560,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/avtr12_Analysis.d3dpipeline b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/avtr12_Analysis.d3dpipeline index e74810c72e..9a0f3d852c 100644 --- a/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/avtr12_Analysis.d3dpipeline +++ b/src/Plugins/OrientationAnalysis/pipelines/EBSD_File_Processing/avtr12_Analysis.d3dpipeline @@ -316,7 +316,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -434,7 +438,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 @@ -552,7 +560,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/src/Plugins/SimplnxCore/pipelines/Read_Data_Files/Import_CSV_File.d3dpipeline b/src/Plugins/SimplnxCore/pipelines/Read_Data_Files/Import_CSV_File.d3dpipeline index f4b5689bf1..62b0fca82b 100644 --- a/src/Plugins/SimplnxCore/pipelines/Read_Data_Files/Import_CSV_File.d3dpipeline +++ b/src/Plugins/SimplnxCore/pipelines/Read_Data_Files/Import_CSV_File.d3dpipeline @@ -331,7 +331,11 @@ "value": "", "version": 1 }, - "parameters_version": 5, + "nonfinite_input_handling_index": { + "value": 0, + "version": 1 + }, + "parameters_version": 6, "plane_index": { "value": 0, "version": 1 diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/05_AM_XCT_Porosity_Segmentation.py b/wrapping/python/examples/pipelines/NXImageProcessing/05_AM_XCT_Porosity_Segmentation.py index bdc6536791..3eb9a24a71 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/05_AM_XCT_Porosity_Segmentation.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/05_AM_XCT_Porosity_Segmentation.py @@ -99,6 +99,7 @@ # Filter 7: Connected Component Image Filter result = nximgproc.ConnectedComponentImageFilter.execute( data_structure=data_structure, + provisional_label_limit_handling_index=0, fully_connected=False, input_image_data_path=nx.DataPath('AM XCT/Cell Data/Closed Pore Mask'), input_image_geometry_path=nx.DataPath('AM XCT'), @@ -145,6 +146,7 @@ # Filter 11: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=False, file_name='Data/Output/ImageProcessing_Examples/01_AM_XCT/pore_contours.tif', diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/06_AM_Powder-Bed_Layer_Inspection.py b/wrapping/python/examples/pipelines/NXImageProcessing/06_AM_Powder-Bed_Layer_Inspection.py index 5e1b57a20f..3a6818bfa7 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/06_AM_Powder-Bed_Layer_Inspection.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/06_AM_Powder-Bed_Layer_Inspection.py @@ -91,6 +91,7 @@ # Filter 5: Threshold Maximum Connected Components Image Filter result = nximgproc.ThresholdMaximumConnectedComponentsImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, input_image_data_path=nx.DataPath('Powder Bed Layers/Cell Data/Bright Anomalies'), input_image_geometry_path=nx.DataPath('Powder Bed Layers'), inside_value=1, @@ -117,6 +118,7 @@ # Filter 7: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=False, file_name='Data/Output/ImageProcessing_Examples/02_AM_Powder_Bed/anomaly_mask.tif', diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/07_AM_Melt-Pool_and_Track_Inspection.py b/wrapping/python/examples/pipelines/NXImageProcessing/07_AM_Melt-Pool_and_Track_Inspection.py index e4c1d6ed60..602f06a4d5 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/07_AM_Melt-Pool_and_Track_Inspection.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/07_AM_Melt-Pool_and_Track_Inspection.py @@ -65,6 +65,10 @@ # Filter 4: Otsu Multiple Thresholds Image Filter result = nximgproc.OtsuMultipleThresholdsImageFilter.execute( data_structure=data_structure, + mask_array_path=nx.DataPath(''), + masked_label=255, + nonfinite_input_handling_index=0, + use_mask_array=False, input_image_data_path=nx.DataPath('Melt Pool Image/Cell Data/Sigmoid Contrast'), input_image_geometry_path=nx.DataPath('Melt Pool Image'), label_offset=0, @@ -95,6 +99,7 @@ # Filter 6: Connected Component Image Filter result = nximgproc.ConnectedComponentImageFilter.execute( data_structure=data_structure, + provisional_label_limit_handling_index=0, fully_connected=True, input_image_data_path=nx.DataPath('Melt Pool Image/Cell Data/Track Mask'), input_image_geometry_path=nx.DataPath('Melt Pool Image'), @@ -127,6 +132,7 @@ # Filter 9: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=True, file_name='Data/Output/ImageProcessing_Examples/03_AM_Melt_Pool/track_contours.tif', diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/08_Powder_Particle_Watershed_Segmentation.py b/wrapping/python/examples/pipelines/NXImageProcessing/08_Powder_Particle_Watershed_Segmentation.py index eee5eb9461..66ced87408 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/08_Powder_Particle_Watershed_Segmentation.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/08_Powder_Particle_Watershed_Segmentation.py @@ -91,6 +91,7 @@ # Filter 6: Connected Component Image Filter result = nximgproc.ConnectedComponentImageFilter.execute( data_structure=data_structure, + provisional_label_limit_handling_index=0, fully_connected=True, input_image_data_path=nx.DataPath('Powder Particles/Cell Data/Marker Mask'), input_image_geometry_path=nx.DataPath('Powder Particles'), @@ -101,6 +102,7 @@ # Filter 7: Morphological Watershed From Markers Image Filter result = nximgproc.MorphologicalWatershedFromMarkersImageFilter.execute( data_structure=data_structure, + marker_id_conversion_handling_index=0, fully_connected=True, input_image_data_path=nx.DataPath('Powder Particles/Cell Data/Particle Gradient'), input_image_geometry_path=nx.DataPath('Powder Particles'), diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/10_Binary_Mask_Repair_and_Skeletonization.py b/wrapping/python/examples/pipelines/NXImageProcessing/10_Binary_Mask_Repair_and_Skeletonization.py index a374267726..d06f0beda5 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/10_Binary_Mask_Repair_and_Skeletonization.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/10_Binary_Mask_Repair_and_Skeletonization.py @@ -129,6 +129,7 @@ # Filter 9: Connected Component Image Filter result = nximgproc.ConnectedComponentImageFilter.execute( data_structure=data_structure, + provisional_label_limit_handling_index=0, fully_connected=True, input_image_data_path=nx.DataPath('Binary Mask/Cell Data/Mask'), input_image_geometry_path=nx.DataPath('Binary Mask'), diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/17_Scientific_Volume_Interoperability.py b/wrapping/python/examples/pipelines/NXImageProcessing/17_Scientific_Volume_Interoperability.py index 8a81d084eb..324f3e8cf3 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/17_Scientific_Volume_Interoperability.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/17_Scientific_Volume_Interoperability.py @@ -64,6 +64,7 @@ # Filter 3: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=False, file_name='Data/Output/ImageProcessing_Examples/13_Scientific_IO/mha_round_trip.mha', @@ -84,6 +85,7 @@ # Filter 4: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=False, file_name='Data/Output/ImageProcessing_Examples/13_Scientific_IO/nifti_slice.tif', diff --git a/wrapping/python/examples/pipelines/NXImageProcessing/19_Serial-Section_Stack_Reconstruction.py b/wrapping/python/examples/pipelines/NXImageProcessing/19_Serial-Section_Stack_Reconstruction.py index 5b9088aaab..fadc2d612d 100644 --- a/wrapping/python/examples/pipelines/NXImageProcessing/19_Serial-Section_Stack_Reconstruction.py +++ b/wrapping/python/examples/pipelines/NXImageProcessing/19_Serial-Section_Stack_Reconstruction.py @@ -57,6 +57,7 @@ # Filter 2: Write Image result = nximgproc.WriteImageFilter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, add_scale_bar=False, create_color_table=False, file_name=str(nxtest.get_data_directory() / 'Output/ImageProcessing_Examples/15_Serial_Sections/section_preview.tif'), diff --git a/wrapping/python/examples/pipelines/OrientationAnalysis/APTR12_Analysis.py b/wrapping/python/examples/pipelines/OrientationAnalysis/APTR12_Analysis.py index d7d9116a87..307e5f0652 100644 --- a/wrapping/python/examples/pipelines/OrientationAnalysis/APTR12_Analysis.py +++ b/wrapping/python/examples/pipelines/OrientationAnalysis/APTR12_Analysis.py @@ -110,6 +110,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-aptr12-corr/fw-ar-IF1-aptr12-corr_001.png", image_array_path=nx.DataPath("fw-ar-IF1-aptr12-corr/Cell Data/IPF_001"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-aptr12-corr"), @@ -140,6 +141,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-aptr12-corr/fw-ar-IF1-aptr12-corr_010.png", image_array_path=nx.DataPath("fw-ar-IF1-aptr12-corr/Cell Data/IPF_010"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-aptr12-corr"), @@ -169,6 +171,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-aptr12-corr/fw-ar-IF1-aptr12-corr_100.png", image_array_path=nx.DataPath("fw-ar-IF1-aptr12-corr/Cell Data/IPF_100"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-aptr12-corr"), diff --git a/wrapping/python/examples/pipelines/OrientationAnalysis/AVTR12_Analysis.py b/wrapping/python/examples/pipelines/OrientationAnalysis/AVTR12_Analysis.py index 3acf22e44d..37510a80cb 100644 --- a/wrapping/python/examples/pipelines/OrientationAnalysis/AVTR12_Analysis.py +++ b/wrapping/python/examples/pipelines/OrientationAnalysis/AVTR12_Analysis.py @@ -110,6 +110,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-avtr12-corr/fw-ar-IF1-avtr12-corr_001.png", image_array_path=nx.DataPath("fw-ar-IF1-avtr12-corr/Cell Data/IPF_001"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-avtr12-corr"), @@ -140,6 +141,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-avtr12-corr/fw-ar-IF1-avtr12-corr_010.png", image_array_path=nx.DataPath("fw-ar-IF1-avtr12-corr/Cell Data/IPF_010"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-avtr12-corr"), @@ -169,6 +171,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/fw-ar-IF1-avtr12-corr/fw-ar-IF1-avtr12-corr_100.png", image_array_path=nx.DataPath("fw-ar-IF1-avtr12-corr/Cell Data/IPF_100"), input_image_geometry_path=nx.DataPath("fw-ar-IF1-avtr12-corr"), diff --git a/wrapping/python/examples/pipelines/OrientationAnalysis/Edax_IPF_Colors.py b/wrapping/python/examples/pipelines/OrientationAnalysis/Edax_IPF_Colors.py index ca5418b760..1292f5ef2e 100644 --- a/wrapping/python/examples/pipelines/OrientationAnalysis/Edax_IPF_Colors.py +++ b/wrapping/python/examples/pipelines/OrientationAnalysis/Edax_IPF_Colors.py @@ -94,6 +94,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=output_file_path, image_array_path=nx.DataPath("DataContainer/Cell Data/IPFColors"), input_image_geometry_path=nx.DataPath("DataContainer"), diff --git a/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Exposed.py b/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Exposed.py index bf4d58665e..6f3de4fdef 100644 --- a/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Exposed.py +++ b/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Exposed.py @@ -93,6 +93,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/TexturedCopper/IPF_Exposed.png", image_array_path=nx.DataPath("Cugrid_after 2nd_15kv_2kx_2/EBSD Scan Data/IPF_Exposed_001"), input_image_geometry_path=nx.DataPath("Cugrid_after 2nd_15kv_2kx_2"), diff --git a/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Unexposed.py b/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Unexposed.py index 58dceb63be..736f19e735 100644 --- a/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Unexposed.py +++ b/wrapping/python/examples/pipelines/OrientationAnalysis/TxCopper_Unexposed.py @@ -92,6 +92,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_data_directory() / "Output/TexturedCopper/IPF_Unexposed.png", image_array_path=nx.DataPath("Cugrid_after 2nd_15kv_2kx_2/EBSD Scan Data/IPF_Unexposed_001"), input_image_geometry_path=nx.DataPath("Cugrid_after 2nd_15kv_2kx_2"), diff --git a/wrapping/python/examples/pipelines/Simplnx/Import_ASCII.py b/wrapping/python/examples/pipelines/Simplnx/Import_ASCII.py index 6f341e1b8a..d96f2f190a 100644 --- a/wrapping/python/examples/pipelines/Simplnx/Import_ASCII.py +++ b/wrapping/python/examples/pipelines/Simplnx/Import_ASCII.py @@ -142,6 +142,7 @@ # Execute Filter with Parameters result = nx_filter.execute( data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=output_file_path, image_array_path=nx.DataPath("[Image Geometry]/Cell Data/IPFColors"), input_image_geometry_path=nx.DataPath("[Image Geometry]"), diff --git a/wrapping/python/examples/scripts/basic_ebsd_ipf.py b/wrapping/python/examples/scripts/basic_ebsd_ipf.py index 6b4d7cb326..a68d3fa14e 100644 --- a/wrapping/python/examples/scripts/basic_ebsd_ipf.py +++ b/wrapping/python/examples/scripts/basic_ebsd_ipf.py @@ -129,6 +129,7 @@ # Write the IPF colors to a PNG file #------------------------------------------------------------------------------ result = nximgproc.WriteImageFilter.execute(data_structure=data_structure, + nonfinite_input_handling_index=0, file_name=nxtest.get_test_temp_directory() / "Small_IN100_IPF_Z.png", image_array_path=nx.DataPath(["Small IN100", "Scan Data", "IPFColors"]), input_image_geometry_path=nx.DataPath(["Small IN100"]), From 88cc583ce234c9ac348fe04c64f44a44c2c68787 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:23:11 -0500 Subject: [PATCH 25/98] TEST: Check portable exponential underflow bounds Accept zero or the positive bounded Float64 subnormal at the selected underflow arguments. Retain the ordinary and Float32 assertions. Validated the paired Release and targeted Debug selections. Signed-off-by: Jessica Marquis --- .../NXImageProcessing/test/ExpImageFilterTest.cpp | 15 ++++++++++++--- .../test/ExpNegativeImageFilterTest.cpp | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp index 29611f6459..e6974fde6c 100644 --- a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp @@ -173,9 +173,18 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values reta REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-15)); REQUIRE(store.getValue(14) == Approx(4.48961281917434524628e38).epsilon(1.0e-15)); REQUIRE(store.getValue(15) == Approx(8.21840746155497218924e307).epsilon(1.0e-15)); - REQUIRE(store.getValue(17) == 2 * std::numeric_limits::denorm_min()); - REQUIRE(store.getValue(18) == std::numeric_limits::denorm_min()); - REQUIRE(store.getValue(19) == 0); + // exp(-744) < 2d, exp(-745) < d, and exp(-746) < d/2, where d = 2^-1074. + // Accept zero or bounded subnormal results without requiring a particular libm rounding. + for(usize index = 17; index < 20; ++index) + { + const float64 value = store.getValue(index); + const float64 upperBound = index == 17 ? 2 * std::numeric_limits::denorm_min() : std::numeric_limits::denorm_min(); + CAPTURE(index); + REQUIRE(std::isfinite(value)); + REQUIRE(value >= 0); + REQUIRE_FALSE(std::signbit(value)); + REQUIRE(value <= upperBound); + } } UnitTest::CheckArraysInheritTupleDims(dataStructure); } diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index 137fc22b2f..1585c20613 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -175,9 +175,18 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE val REQUIRE(store.getValue(13) == Approx(1.65163625499400185553e38).epsilon(1.0e-15)); REQUIRE(store.getValue(14) == Approx(4.48961281917434524628e38).epsilon(1.0e-15)); REQUIRE(store.getValue(15) == Approx(8.21840746155497218924e307).epsilon(1.0e-15)); - REQUIRE(store.getValue(17) == 2 * std::numeric_limits::denorm_min()); - REQUIRE(store.getValue(18) == std::numeric_limits::denorm_min()); - REQUIRE(store.getValue(19) == 0); + // exp(-744) < 2d, exp(-745) < d, and exp(-746) < d/2, where d = 2^-1074. + // Accept zero or bounded subnormal results without requiring a particular libm rounding. + for(usize index = 17; index < 20; ++index) + { + const float64 value = store.getValue(index); + const float64 upperBound = index == 17 ? 2 * std::numeric_limits::denorm_min() : std::numeric_limits::denorm_min(); + CAPTURE(index); + REQUIRE(std::isfinite(value)); + REQUIRE(value >= 0); + REQUIRE_FALSE(std::signbit(value)); + REQUIRE(value <= upperBound); + } } UnitTest::CheckArraysInheritTupleDims(dataStructure); } From 1a1c0d07e932bc77789368c7c8bea0186973ea5e Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:23:12 -0500 Subject: [PATCH 26/98] STY: Use sized owners for C15 working buffers Use vectors for newly introduced non-Boolean buffers. Keep contiguous Boolean buffers and document the exception. Preserve capacity, grants, transfer order and result propagation. Validated the paired Release ownership families and targeted Debug cases. Signed-off-by: Jessica Marquis --- .../utils/ImageFilterExecutorDetail.hpp | 12 ++++++------ .../ImageProcessing/OtsuImageEngine.hpp | 18 +++++++++++------- ...resholdMaximumConnectedComponentsEngine.hpp | 8 +++++--- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index 2d82f461fb..09f45e8ac5 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -942,8 +942,8 @@ Result<> SubtractHConvexContrastChunked(const AbstractDataStore& input, const static_assert(std::is_integral_v && std::is_signed_v); const usize size = input.getSize(); chunkValues = std::max(1, chunkValues); - auto inputBuffer = std::make_unique_for_overwrite(std::min(chunkValues, size)); - auto reconstructionBuffer = std::make_unique_for_overwrite(std::min(chunkValues, size)); + auto inputBuffer = std::make_unique>(std::min(chunkValues, size)); + auto reconstructionBuffer = std::make_unique>(std::min(chunkValues, size)); Result<> accumulatedResult; for(usize start = 0; start < size; start += chunkValues) { @@ -952,22 +952,22 @@ Result<> SubtractHConvexContrastChunked(const AbstractDataStore& input, const return accumulatedResult; } const usize count = std::min(chunkValues, size - start); - accumulatedResult = MergeResults(std::move(accumulatedResult), input.copyIntoBuffer(start, nonstd::span(inputBuffer.get(), count))); + accumulatedResult = MergeResults(std::move(accumulatedResult), input.copyIntoBuffer(start, nonstd::span(inputBuffer->data(), count))); if(accumulatedResult.invalid()) { return accumulatedResult; } - accumulatedResult = MergeResults(std::move(accumulatedResult), reconstruction.copyIntoBuffer(start, nonstd::span(reconstructionBuffer.get(), count))); + accumulatedResult = MergeResults(std::move(accumulatedResult), reconstruction.copyIntoBuffer(start, nonstd::span(reconstructionBuffer->data(), count))); if(accumulatedResult.invalid()) { return accumulatedResult; } - accumulatedResult = MergeResults(std::move(accumulatedResult), SubtractHConvexContrastRange(inputBuffer.get(), reconstructionBuffer.get(), inputBuffer.get(), count, start, inputPath)); + accumulatedResult = MergeResults(std::move(accumulatedResult), SubtractHConvexContrastRange(inputBuffer->data(), reconstructionBuffer->data(), inputBuffer->data(), count, start, inputPath)); if(accumulatedResult.invalid()) { return accumulatedResult; } - accumulatedResult = MergeResults(std::move(accumulatedResult), output.copyFromBuffer(start, nonstd::span(inputBuffer.get(), count))); + accumulatedResult = MergeResults(std::move(accumulatedResult), output.copyFromBuffer(start, nonstd::span(inputBuffer->data(), count))); if(accumulatedResult.invalid()) { return accumulatedResult; diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 93df02c855..99573e3d8e 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -243,7 +243,9 @@ Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataSt auto plan = ReserveOtsuBuffers(total, sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)), options.TargetBytes, input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore), parallelRange ? sizeof(OtsuFloatingRangeSummary) : 0); + // T may be bool. A raw array provides the contiguous storage that vector cannot provide. auto valuesPtr = std::make_unique_for_overwrite(plan.Values); + // MaskT may be bool and must provide contiguous values for store transfers. auto maskValuesPtr = maskPtr == nullptr ? nullptr : std::make_unique_for_overwrite(plan.Values); for(usize start = 0; start < total;) { @@ -391,7 +393,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp T minValue{}; T maxValue{}; constexpr usize ringSize = 65536; - std::unique_ptr[]> frequenciesPtr; + std::unique_ptr>> frequenciesPtr; CacheMemoryBudgetManager::WorkingMemoryReservation ringReservation; using CompactRangeType = std::conditional_t, bool, const bool>; CompactRangeType compactRange = false; @@ -406,7 +408,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp compactRange = !external || ringReservation.sizeBytes() >= ringBytes; if(compactRange) { - frequenciesPtr = std::make_unique[]>(ringSize); + frequenciesPtr = std::make_unique>>(ringSize); } else { @@ -479,7 +481,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp compactRange = (detail::IntegralMonotonicKey(maxValue) - detail::IntegralMonotonicKey(minValue)) <= static_cast(ringSize - 1); if(compactRange) { - auto& entry = frequenciesPtr[static_cast(key & static_cast(ringSize - 1))]; + auto& entry = (*frequenciesPtr)[static_cast(key & static_cast(ringSize - 1))]; if(entry.Count != 0 && entry.Value != value) { compactRange = false; @@ -535,7 +537,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { return ConvertResultTo(std::move(rangeResult), std::move(result)); } - const auto& entry = frequenciesPtr[i]; + const auto& entry = (*frequenciesPtr)[i]; if(entry.Count != 0) { Result<> addResult = detail::AccumulateCheckedOtsuValue(histogram, entry.Value, minValue, maxValue, entry.Count); @@ -706,9 +708,11 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStoregetStoreType() == IDataStore::StoreType::OutOfCore || output.getStoreType() == IDataStore::StoreType::OutOfCore); + // T may be bool. A raw array provides the contiguous storage that vector cannot provide. auto valuesPtr = std::make_unique_for_overwrite(plan.Values); + // MaskT may be bool and must provide contiguous values for store transfers. auto maskValuesPtr = std::make_unique_for_overwrite(plan.Values); - auto labelsPtr = std::make_unique_for_overwrite(plan.Values); + auto labelsPtr = std::make_unique>(plan.Values); messageHandler.sendInfoMessage("Applying masked Otsu labels in bounded batches"); ThrottledMessageHandler progress(messageHandler); progress.reset(1 + ((input.getSize() - 1) / plan.Values), "Applying masked Otsu labels"); @@ -748,14 +752,14 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore writeResult = output.copyFromBuffer(start, nonstd::span(labelsPtr.get(), count)); + Result<> writeResult = output.copyFromBuffer(start, nonstd::span(labelsPtr->data(), count)); result = MergeResults(std::move(result), std::move(writeResult)); if(result.invalid()) { diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index 14dbf8db98..a925007cf1 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -188,6 +188,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& } } std::vector> blocks(blockCapacity); + // T may be bool. A raw array provides the contiguous storage that vector cannot provide. auto valuesPtr = direct ? nullptr : std::make_unique_for_overwrite(batchValues); for(usize start = 0; start < total;) { @@ -393,8 +394,9 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor auto reservation = ReserveWorkingMemory(preferredBytes, usefulBytes); const usize chunkValues = std::max(minimumValues, std::min(total, static_cast(std::min(reservation.sizeBytes() / valueBytes, std::numeric_limits::max())))); reservation.shrinkTo(static_cast(std::min(chunkValues, total)) * valueBytes); + // T may be bool and must provide contiguous values for store transfers. auto inputPtr = fillOutside ? nullptr : std::make_unique_for_overwrite(std::min(chunkValues, total)); - auto outputPtr = std::make_unique_for_overwrite(std::min(chunkValues, total)); + auto outputPtr = std::make_unique>(std::min(chunkValues, total)); for(usize start = 0; start < total;) { if(shouldCancel) @@ -413,9 +415,9 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor } for(usize index = 0; index < count; ++index) { - outputPtr[index] = !fillOutside && inputPtr[index] >= threshold && inputPtr[index] <= cap ? options.InsideValue : options.OutsideValue; + (*outputPtr)[index] = !fillOutside && inputPtr[index] >= threshold && inputPtr[index] <= cap ? options.InsideValue : options.OutsideValue; } - auto writeResult = output.copyFromBuffer(start, nonstd::span(outputPtr.get(), count)); + auto writeResult = output.copyFromBuffer(start, nonstd::span(outputPtr->data(), count)); status = MergeResults(std::move(status), std::move(writeResult)); if(status.invalid()) { From ad711f9608c166f2d6bbcb1710d8e374ac9acf52 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:23:12 -0500 Subject: [PATCH 27/98] TEST: Verify C15 resident consumer chains Run nine small producer-consumer cases with literal typed results. Cover segmentation, persistence, export, morphology, edges and labels. Require dispatch counters only from filters that expose them. Validated all nine cases in Release and in the targeted Debug selection. Signed-off-by: Jessica Marquis --- .../test/C15CompatibilityTestUtils.hpp | 258 ++++++++++++++++++ .../test/CurvatureFlowImageFilterTest.cpp | 61 +++++ .../test/ExpNegativeImageFilterTest.cpp | 117 ++++++++ .../test/HConvexImageFilterTest.cpp | 26 ++ .../test/HMinimaImageFilterTest.cpp | 54 ++++ .../OtsuMultipleThresholdsImageFilterTest.cpp | 53 ++++ ...othingRecursiveGaussianImageFilterTest.cpp | 66 +++++ .../test/SquareImageFilterTest.cpp | 43 +++ ...imumConnectedComponentsImageFilterTest.cpp | 67 +++++ .../NXImageProcessing/test/WriteImageTest.cpp | 98 +++++++ 10 files changed, 843 insertions(+) create mode 100644 src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp diff --git a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp new file mode 100644 index 0000000000..6c2da745e6 --- /dev/null +++ b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp @@ -0,0 +1,258 @@ +#pragma once + +#include "ReconstructionFilterTestUtils.hpp" +#include "UnaryFilterTestUtils.hpp" + +#include "ImageProcessing/Filters/BinaryThresholdImageFilter.hpp" +#include "ImageProcessing/Filters/ConnectedComponentImageFilter.hpp" +#include "ImageProcessing/Filters/ReadMhaFileFilter.hpp" +#include "ImageProcessing/Filters/WriteImageFilter.hpp" +#include "ImageProcessing/ImageProcessing_test_dirs.hpp" +#include "SimplnxCore/Filters/ConvertDataFilter.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" +#include "simplnx/Filter/FilterList.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" +#include "simplnx/Utilities/ImageIO/ImageIOFactory.hpp" +#include "simplnx/Utilities/Parsing/DREAM3D/Dream3dIO.hpp" +#include "simplnx/Utilities/Parsing/HDF5/IO/DatasetIO.hpp" +#include "simplnx/Utilities/Parsing/HDF5/IO/FileIO.hpp" + +#include +#include +#include +#include +#include + +// Small transport helpers only. Each owning test states its independent numerical expectations. +namespace c15_test +{ +using namespace nx::core; + +class Directory +{ +public: + Directory() + : m_Path(std::filesystem::path(unit_test::k_BinaryDir.view()) / "test_output" / ("C15Compatibility-" + std::to_string(std::random_device{}()))) + { + REQUIRE(std::filesystem::create_directories(m_Path)); + } + ~Directory() noexcept + { + std::error_code error; + std::filesystem::remove_all(m_Path, error); + } + Directory(const Directory&) = delete; + Directory(Directory&&) = delete; + Directory& operator=(const Directory&) = delete; + Directory& operator=(Directory&&) = delete; + const std::filesystem::path& path() const noexcept + { + return m_Path; + } + +private: + std::filesystem::path m_Path; +}; + +template +void CheckSaved(const std::filesystem::path& path, const DataPath& arrayPath, const ShapeType& tupleShape, const std::vector& expected) +{ + auto file = HDF5::FileIO::ReadFile(path); + REQUIRE(file.isValid()); + auto dataset = file.openDataset("DataStructure/" + arrayPath.toString()); + REQUIRE(dataset.getId() > 0); + REQUIRE(dataset.isValid()); + const auto type = dataset.getDataType(); + SIMPLNX_RESULT_REQUIRE_VALID(type); + REQUIRE(type.value() == GetDataType()); + ShapeType shape = tupleShape; + shape.push_back(1); + REQUIRE(dataset.getDimensions() == shape); + std::vector actual(expected.size()); + const auto read = dataset.readIntoSpan(nonstd::span(actual.data(), actual.size())); + SIMPLNX_RESULT_REQUIRE_VALID(read); + REQUIRE(actual == expected); +} + +inline void ExecuteChecked(const IFilter& filter, DataStructure& dataStructure, const Arguments& args) +{ + const auto preflight = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); +} + +inline DataPath ConvertToFloat32(DataStructure& dataStructure, const DataPath& input) +{ + const auto filter = Application::Instance()->getFilterList()->createFilter(FilterTraits::uuid); + REQUIRE(filter != nullptr); + auto args = filter->getDefaultArguments(); + args.insertOrAssign(ConvertDataFilter::k_ScalarType_Key, std::make_any(static_cast(DataType::float32))); + args.insertOrAssign(ConvertDataFilter::k_ArrayToConvertPath_Key, std::make_any(input)); + args.insertOrAssign(ConvertDataFilter::k_ConvertedArrayName_Key, std::make_any("Converted")); + args.insertOrAssign(ConvertDataFilter::k_DeleteOriginal_Key, std::make_any(false)); + ExecuteChecked(*filter, dataStructure, args); + return input.replaceName("Converted"); +} + +struct StoreExpectation +{ + IDataStore::StoreType type; + + void requireExpectedStore(const IDataArray& array) const + { + INFO("Array '" << array.getName() << "' expected store type " << static_cast(type)); + REQUIRE(array.getIDataStoreRef().getStoreType() == type); + } +}; + +template +std::vector Read(DataStructure& dataStructure, const DataPath& path, const ShapeType& shape, const Scope& scope) +{ + const DataArray* array = nullptr; + REQUIRE_NOTHROW(array = &dataStructure.getDataRefAs>(path)); + REQUIRE(array->getDataType() == GetDataType()); + REQUIRE(array->getTupleShape() == shape); + REQUIRE(array->getComponentShape() == ShapeType{1}); + scope.requireExpectedStore(*array); + const auto& store = array->getDataStoreRef(); + if(store.getStoreType() == IDataStore::StoreType::OutOfCore) + { + REQUIRE(store.getDataFormat() == "HDF5-OOC"); + const auto metadata = store.getRecoveryMetadata(); + const auto backing = metadata.find("OocBackingFilePath"); + REQUIRE(backing != metadata.end()); + REQUIRE(std::filesystem::is_regular_file(backing->second)); + } + std::vector values(array->getSize()); + const auto result = store.copyIntoBuffer(0, nonstd::span(values.data(), values.size())); + SIMPLNX_RESULT_REQUIRE_VALID(result); + return values; +} + +template +Arguments ImageArguments(const Filter& filter, const DataPath& input, const std::string& output) +{ + auto args = filter.getDefaultArguments(); + args.insertOrAssign(Filter::k_InputImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(Filter::k_InputImageDataPath_Key, std::make_any(input)); + args.insertOrAssign(Filter::k_OutputImageArrayName_Key, std::make_any(output)); + return args; +} + +template +DataPath Pointwise(DataStructure& dataStructure, const DataPath& input, const std::string& output) +{ + const Filter filter; + const auto args = ImageArguments(filter, input, output); + ExecuteChecked(filter, dataStructure, args); + return input.replaceName(output); +} + +template +void Execute(const Filter& filter, DataStructure& dataStructure, const Arguments& args, Scope& scope) +{ + const auto preflight = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = scope.executeFilter(filter, dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); +} + +// BinaryThreshold has one pointwise implementation and does not select an algorithm. +inline DataPath Select(DataStructure& dataStructure, const DataPath& input, float64 lower, float64 upper, const std::string& name) +{ + const BinaryThresholdImageFilter filter; + auto args = ImageArguments(filter, input, name); + args.insertOrAssign(BinaryThresholdImageFilter::k_LowerThreshold_Key, std::make_any(lower)); + args.insertOrAssign(BinaryThresholdImageFilter::k_UpperThreshold_Key, std::make_any(upper)); + args.insertOrAssign(BinaryThresholdImageFilter::k_InsideValue_Key, std::make_any(1)); + args.insertOrAssign(BinaryThresholdImageFilter::k_OutsideValue_Key, std::make_any(0)); + ExecuteChecked(filter, dataStructure, args); + return input.replaceName(name); +} + +inline DataPath Components(DataStructure& dataStructure, const DataPath& input, const std::string& name) +{ + const ConnectedComponentImageFilter filter; + auto args = ImageArguments(filter, input, name); + args.insertOrAssign(ConnectedComponentImageFilter::k_FullyConnected_Key, std::make_any(false)); + args.insertOrAssign(ConnectedComponentImageFilter::k_ProvisionalLabelLimitHandling_Key, std::make_any(0)); + ExecuteChecked(filter, dataStructure, args); + return input.replaceName(name); +} + +inline void SetGeometry(DataStructure& dataStructure) +{ + ImageGeom* geometry = nullptr; + REQUIRE_NOTHROW(geometry = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); + geometry->setSpacing({1, 2, 3}); + geometry->setOrigin({4, 5, 6}); + geometry->setUnits(IGeometry::LengthUnit::Millimeter); +} + +inline void CheckGeometry(DataStructure& dataStructure, const SizeVec3& dimensions) +{ + const ImageGeom* geometry = nullptr; + REQUIRE_NOTHROW(geometry = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(geometry->getDimensions() == dimensions); + REQUIRE(geometry->getSpacing() == FloatVec3{1, 2, 3}); + REQUIRE(geometry->getOrigin() == FloatVec3{4, 5, 6}); + REQUIRE(geometry->getUnits() == IGeometry::LengthUnit::Millimeter); +} + +inline Arguments WriteArguments(const DataPath& input, const std::filesystem::path& output) +{ + const WriteImageFilter filter; + auto args = filter.getDefaultArguments(); + args.insertOrAssign(WriteImageFilter::k_ImageGeomPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(WriteImageFilter::k_ImageArrayPath_Key, std::make_any(input)); + args.insertOrAssign(WriteImageFilter::k_FileName_Key, std::make_any(output)); + args.insertOrAssign(WriteImageFilter::k_Plane_Key, std::make_any(0)); + args.insertOrAssign(WriteImageFilter::k_TotalIndexDigits_Key, std::make_any(3)); + args.insertOrAssign(WriteImageFilter::k_LeadingDigitCharacter_Key, std::make_any("0")); + args.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(false)); + args.insertOrAssign(WriteImageFilter::k_UseMask_Key, std::make_any(false)); + args.insertOrAssign(WriteImageFilter::k_FlipMode_Key, std::make_any(0)); + args.insertOrAssign(WriteImageFilter::k_AddScaleBar_Key, std::make_any(false)); + args.insertOrAssign(WriteImageFilter::k_NonfiniteInputHandling_Key, std::make_any(0)); + return args; +} + +template +std::vector Decode(const std::filesystem::path& path, usize width, usize height, usize components = 1) +{ + const auto io = CreateImageIO(path); + SIMPLNX_RESULT_REQUIRE_VALID(io); + REQUIRE(io.value() != nullptr); + const auto metadata = io.value()->readMetadata(path); + SIMPLNX_RESULT_REQUIRE_VALID(metadata); + REQUIRE(metadata.value().dataType == GetDataType()); + REQUIRE(metadata.value().width == width); + REQUIRE(metadata.value().height == height); + REQUIRE(metadata.value().numComponents == components); + REQUIRE(metadata.value().numPages == 1); + std::vector bytes(width * height * components * sizeof(T)); + const auto read = io.value()->readPixelData(path, std::span(bytes)); + SIMPLNX_RESULT_REQUIRE_VALID(read); + std::vector values(width * height * components); + std::memcpy(values.data(), bytes.data(), bytes.size()); + return values; +} + +inline DataStructure ReadMha(const std::filesystem::path& path) +{ + const ReadMhaFileFilter filter; + auto args = filter.getDefaultArguments(); + args.insertOrAssign(ReadMhaFileFilter::k_InputFilePath_Key, std::make_any(path)); + args.insertOrAssign(ReadMhaFileFilter::k_CreatedImageGeometryPath_Key, std::make_any(DataPath({"Image Geometry"}))); + args.insertOrAssign(ReadMhaFileFilter::k_CellAttributeMatrixName_Key, std::make_any("CellData")); + args.insertOrAssign(ReadMhaFileFilter::k_ImageDataArrayName_Key, std::make_any("Pixels")); + DataStructure result; + const auto read = filter.execute(result, args); + SIMPLNX_RESULT_REQUIRE_VALID(read.result); + UnitTest::CheckArraysInheritTupleDims(result); + return result; +} +} // namespace c15_test diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index ff8657c9fb..2e18d63d63 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" +#include "ImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -931,3 +933,62 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero iterations is a no- const DataPath outputPath({"Image Geometry", "CellData", "Output"}); rt::RequireExact(ds.getDataRefAs(outputPath), ds.getDataRefAs(inputPath)); } + +TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[ImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const bool step = GENERATE(false, true); + CAPTURE(step, scenario); + std::vector original(16 * 4, 4); + if(step) + { + for(usize index = 0; index < original.size(); ++index) + { + original[index] = index % 16 < 8 ? 0 : 1; + } + } + DataStructure ds; + const auto input = recon_test::BuildImageFromPattern(ds, 16, 4, 1, original); + c15_test::SetGeometry(ds); + REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + const CurvatureFlowImageFilter filter; + auto args = c15_test::ImageArguments(filter, input, "Smoothed"); + args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(1)); + args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(0.125)); + c15_test::ExecuteChecked(filter, ds, args); + const auto smoothed = input.replaceName("Smoothed"); + const auto values = c15_test::Read(ds, smoothed, {1, 4, 16}, scope); + const GradientMagnitudeImageFilter gradient; + args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); + args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); + c15_test::Execute(gradient, ds, args, scope); + const auto edges = c15_test::Read(ds, input.replaceName("Gradient"), {1, 4, 16}, scope); + for(usize y = 0; y < 4; ++y) + { + for(usize x = 0; x < 16; ++x) + { + const usize index = y * 16 + x; + CAPTURE(x, y); + REQUIRE(std::isfinite(values[index])); + REQUIRE(std::isfinite(edges[index])); + if(!step) + { + REQUIRE(values[index] == Approx(4).margin(1.0e-6)); + REQUIRE(std::abs(edges[index]) <= 1.0e-6); + } + else + { + // A planar step has zero transverse curvature. Its X central differences are 0.5 at columns 7 and 8. + REQUIRE(values[index] == Approx(original[index]).margin(1.0e-6)); + const float32 expectedGradient = (x == 7 || x == 8) ? 0.5F : 0.0F; + REQUIRE(edges[index] == Approx(expectedGradient).margin(1.0e-6)); + } + } + } + REQUIRE(c15_test::Read(ds, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + c15_test::CheckGeometry(ds, {16, 4, 1}); + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index 1585c20613..f35b3e4ef6 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -1,3 +1,8 @@ +#include "C15CompatibilityTestUtils.hpp" +#include "ImageProcessing/Filters/ExpImageFilter.hpp" +#include "ImageProcessing/Filters/Log10ImageFilter.hpp" +#include "ImageProcessing/Filters/LogImageFilter.hpp" +#include "ImageProcessing/Filters/SqrtImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" @@ -337,3 +342,115 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: ITK real-image golden (def UnitTest::CheckArraysInheritTupleDims(ds); } + +TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain masks feed components", "[ImageProcessing][C15Compatibility][Issue1874][ExpNegativeImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}, usize{5}, usize{6}); + CAPTURE(variant); + DataStructure ds; + DataPath input; + DataPath quantitative; + std::vector expected; + std::vector selected; + std::vector components; + float64 lower = 0; + float64 upper = 2; + const auto nan = std::numeric_limits::quiet_NaN(); + const auto inf = std::numeric_limits::infinity(); + switch(variant) + { + case 0: + input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {1, 0.36787944117144232160, 0.13533528323661269189}; + lower = 0.5; + upper = 1; + selected = {1, 0, 0}; + components = {1, 0, 0}; + REQUIRE(c15_test::Read(ds, input, {1, 1, 3}, expectedStore) == std::vector{0, 1, 2}); + break; + case 1: + input = ip_test::BuildScalarValuesImage(ds, {0, 1}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {1, 2.71828182845904523536}; + lower = 1; + selected = {1, 0}; + components = {1, 0}; + REQUIRE(c15_test::Read(ds, input, {1, 1, 2}, expectedStore) == std::vector{0, 1}); + break; + case 2: + input = ip_test::BuildScalarValuesImage(ds, {0, 1000, nan, -inf, inf}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {1, inf, nan, 0, inf}; + selected = {1, 0, 0, 1, 0}; + components = {1, 0, 0, 2, 0}; + break; + case 3: + input = ip_test::BuildScalarValuesImage(ds, {1, 0, -1, inf, nan}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {0, -inf, nan, inf, nan}; + lower = -1; + upper = 1; + selected = {1, 0, 0, 0, 0}; + components = {1, 0, 0, 0, 0}; + break; + case 4: + input = ip_test::BuildScalarValuesImage(ds, {1, 10, 0, -1}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {0, 1, -inf, nan}; + upper = 1; + selected = {1, 1, 0, 0}; + components = {1, 1, 0, 0}; + break; + case 5: + input = ip_test::BuildScalarValuesImage(ds, {0, 4, -1, inf, nan}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {0, 2, nan, inf, nan}; + selected = {1, 1, 0, 0, 0}; + components = {1, 1, 0, 0, 0}; + break; + default: + input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2}); + quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + expected = {1, 0.36787944117144232160, 0.13533528323661269189}; + lower = 0.5; + upper = 1; + selected = {1, 0, 0}; + components = {1, 0, 0}; + break; + } + const ShapeType shape{1, 1, expected.size()}; + auto checkQuantitative = [&] { + const auto values = c15_test::Read(ds, quantitative, shape, expectedStore); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + if(std::isnan(expected[index])) + { + REQUIRE(std::isnan(values[index])); + } + else if(std::isinf(expected[index]) || expected[index] == 0) + { + REQUIRE(values[index] == expected[index]); + } + else + { + REQUIRE(values[index] == Approx(expected[index]).epsilon(1.0e-15)); + } + } + }; + if(variant >= 2) + { + REQUIRE(c15_test::Read(ds, input, shape, expectedStore).size() == expected.size()); + } + checkQuantitative(); + const auto mask = c15_test::Select(ds, quantitative, lower, upper, "Selected"); + REQUIRE(c15_test::Read(ds, mask, shape, expectedStore) == selected); + const auto labels = c15_test::Components(ds, mask, "Components"); + REQUIRE(c15_test::Read(ds, labels, shape, expectedStore) == components); + checkQuantitative(); + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 2fd992b123..9e9522990c 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" + #include "ReconstructionFilterTestUtils.hpp" #include "NXImageProcessing/Filters/HConvexImageFilter.hpp" @@ -741,3 +743,27 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: ITK real-image golden (HConvex rt::RunReconstructionMd5ItkGolden("RA-Short.nrrd", "f3a7b95a51710d51b3b73e0eb77eb1eb", [](Arguments& args) { args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(10000.0)); }); } + +TEST_CASE("ImageProcessing::C15Compatibility resident exact widening supports fractional HConvex height", "[ImageProcessing][C15Compatibility][Issue1874][HConvexImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure ds; + const std::vector original{2, 2, 2, 2, 4, 2, 2, 2, 2}; + const auto input = recon_test::BuildImageFromPattern(ds, 3, 3, 1, original); + c15_test::SetGeometry(ds); + const auto converted = c15_test::ConvertToFloat32(ds, input); + const std::vector exactConverted{2, 2, 2, 2, 4, 2, 2, 2, 2}; + REQUIRE(c15_test::Read(ds, converted, {1, 3, 3}, scope) == exactConverted); + const HConvexImageFilter filter; + auto args = c15_test::ImageArguments(filter, converted, "Contrast"); + args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(0.5)); + args.insertOrAssign(HConvexImageFilter::k_FullyConnected_Key, std::make_any(false)); + c15_test::Execute(filter, ds, args, scope); + REQUIRE(c15_test::Read(ds, input.replaceName("Contrast"), {1, 3, 3}, scope) == std::vector{0, 0, 0, 0, 0.5F, 0, 0, 0, 0}); + REQUIRE(c15_test::Read(ds, converted, {1, 3, 3}, scope) == exactConverted); + REQUIRE(c15_test::Read(ds, input, {1, 3, 3}, scope) == original); + c15_test::CheckGeometry(ds, {3, 3, 1}); + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index a0e8b8bd28..eed968a546 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -1,3 +1,6 @@ +#include "C15CompatibilityTestUtils.hpp" +#include "ImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" +#include "ImageProcessing/Filters/RegionalMinimaImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" #include "NXImageProcessing/Filters/HMinimaImageFilter.hpp" @@ -565,3 +568,54 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima rt::RunReconstructionMd5ItkGolden("RA-Short.nrrd", "7778067eeb752b6ac396dd9e362e8346", [](Arguments& args) { args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(2000.0)); }); } + +TEST_CASE("ImageProcessing::C15Compatibility resident fractional minima produce watershed markers", "[ImageProcessing][C15Compatibility][Issue1874][HMinimaImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + DataStructure ds; + const auto input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2, 3, 2, 1, 0}); + c15_test::SetGeometry(ds); + const ShapeType shape{1, 1, 7}; + REQUIRE(c15_test::Read(ds, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + const auto converted = c15_test::ConvertToFloat32(ds, input); + REQUIRE(c15_test::Read(ds, converted, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + const HMinimaImageFilter minimaFilter; + auto args = c15_test::ImageArguments(minimaFilter, converted, "RaisedMinima"); + args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(0.5)); + args.insertOrAssign(HMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); + c15_test::Execute(minimaFilter, ds, args, scope); + const auto raised = input.replaceName("RaisedMinima"); + const std::vector expectedRaised{0.5F, 1, 2, 3, 2, 1, 0.5F}; + REQUIRE(c15_test::Read(ds, raised, shape, scope) == expectedRaised); + const RegionalMinimaImageFilter regional; + args = c15_test::ImageArguments(regional, raised, "RegionalMinima"); + args.insertOrAssign(RegionalMinimaImageFilter::k_ForegroundValue_Key, std::make_any(1)); + args.insertOrAssign(RegionalMinimaImageFilter::k_BackgroundValue_Key, std::make_any(0)); + args.insertOrAssign(RegionalMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); + args.insertOrAssign(RegionalMinimaImageFilter::k_FlatIsMinima_Key, std::make_any(true)); + c15_test::Execute(regional, ds, args, scope); + const auto regionalPath = input.replaceName("RegionalMinima"); + REQUIRE(c15_test::Read(ds, regionalPath, shape, scope) == std::vector{1, 0, 0, 0, 0, 0, 1}); + const auto markers = c15_test::Components(ds, regionalPath, "Markers"); + const std::vector expectedMarkers{1, 0, 0, 0, 0, 0, 2}; + REQUIRE(c15_test::Read(ds, markers, shape, scope) == expectedMarkers); + const MorphologicalWatershedFromMarkersImageFilter watershed; + args = c15_test::ImageArguments(watershed, raised, "Basins"); + args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerImageDataPath_Key, std::make_any(markers)); + args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkWatershedLine_Key, std::make_any(true)); + args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_FullyConnected_Key, std::make_any(false)); + args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerIdConversionHandling_Key, std::make_any(0)); + c15_test::ExecuteChecked(watershed, ds, args); + const auto labels = c15_test::Read(ds, input.replaceName("Basins"), shape, scope); + // The unique central ridge separates three cells in each basin. Zero marks the watershed line. + REQUIRE(labels == std::vector{1, 1, 1, 0, 2, 2, 2}); + REQUIRE(std::count(labels.begin(), labels.end(), uint32{1}) == 3); + REQUIRE(std::count(labels.begin(), labels.end(), uint32{2}) == 3); + REQUIRE(c15_test::Read(ds, raised, shape, scope) == expectedRaised); + REQUIRE(c15_test::Read(ds, markers, shape, scope) == expectedMarkers); + REQUIRE(c15_test::Read(ds, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + c15_test::CheckGeometry(ds, {7, 1, 1}); + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 98c9752d25..3759983fd9 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" + #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" @@ -1279,3 +1281,54 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summarie } // These are output/transfer-bound assertions. No thread-count or throughput claim follows from them. } + +TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds selected components", "[ImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const bool includeNonfinite = GENERATE(false, true); + const auto nan = std::numeric_limits::quiet_NaN(); + const auto inf = std::numeric_limits::infinity(); + const std::vector original = includeNonfinite ? std::vector{0, 0, 4, 4, nan, -inf, inf} : std::vector{0, 0, 4, 4}; + const ShapeType shape{1, 1, original.size()}; + DataStructure ds; + const auto input = ip_test::BuildScalarValuesImage(ds, original); + const auto validity = c15_test::Select(ds, input, 0, 4, "Validity"); + const std::vector expectedValidity = includeNonfinite ? std::vector{1, 1, 1, 1, 0, 0, 0} : std::vector{1, 1, 1, 1}; + REQUIRE(c15_test::Read(ds, validity, shape, expectedStore) == expectedValidity); + const OtsuMultipleThresholdsImageFilter filter; + auto args = c15_test::ImageArguments(filter, input, "Classes"); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(2)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(1)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(7)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(255)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(validity)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(0)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ValleyEmphasis_Key, std::make_any(false)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ReturnBinMidpoint_Key, std::make_any(false)); + c15_test::ExecuteChecked(filter, ds, args); + const auto classes = input.replaceName("Classes"); + const std::vector expectedClasses = includeNonfinite ? std::vector{7, 7, 8, 8, 255, 255, 255} : std::vector{7, 7, 8, 8}; + REQUIRE(c15_test::Read(ds, classes, shape, expectedStore) == expectedClasses); + const auto selected = c15_test::Select(ds, classes, 8, 8, "DesiredClass"); + const std::vector expectedSelected = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; + REQUIRE(c15_test::Read(ds, selected, shape, expectedStore) == expectedSelected); + const auto labels = c15_test::Components(ds, selected, "Components"); + const std::vector expectedLabels = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; + const auto actualLabels = c15_test::Read(ds, labels, shape, expectedStore); + REQUIRE(actualLabels == expectedLabels); + REQUIRE(std::count(actualLabels.begin(), actualLabels.end(), uint32{1}) == 2); + REQUIRE(c15_test::Read(ds, classes, shape, expectedStore) == expectedClasses); + REQUIRE(c15_test::Read(ds, validity, shape, expectedStore) == expectedValidity); + const auto values = c15_test::Read(ds, input, shape, expectedStore); + REQUIRE(std::vector(values.begin(), values.begin() + 4) == std::vector{0, 0, 4, 4}); + if(includeNonfinite) + { + REQUIRE(std::isnan(values[4])); + REQUIRE(values[5] == -inf); + REQUIRE(values[6] == inf); + } + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 57243babdd..6241ef5244 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" +#include "ImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -978,3 +980,67 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: ITK real-im UnitTest::CheckArraysInheritTupleDims(ds); } + +TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", + "[ImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const bool step = GENERATE(false, true); + CAPTURE(step, scenario); + std::vector original(16 * 4, 4); + if(step) + { + for(usize index = 0; index < original.size(); ++index) + { + original[index] = index % 16 < 8 ? 0 : 1; + } + } + DataStructure ds; + const auto input = recon_test::BuildImageFromPattern(ds, 16, 4, 1, original); + c15_test::SetGeometry(ds); + REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + const SmoothingRecursiveGaussianImageFilter filter; + auto args = c15_test::ImageArguments(filter, input, "Smoothed"); + args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_Sigma_Key, std::make_any>(std::vector{1, 1, 1})); + args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); + c15_test::Execute(filter, ds, args, scope); + const auto smoothed = input.replaceName("Smoothed"); + const auto values = c15_test::Read(ds, smoothed, {1, 4, 16}, scope); + const GradientMagnitudeImageFilter gradient; + args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); + args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); + c15_test::Execute(gradient, ds, args, scope); + const auto edges = c15_test::Read(ds, input.replaceName("Gradient"), {1, 4, 16}, scope); + for(usize y = 0; y < 4; ++y) + { + for(usize x = 0; x < 16; ++x) + { + const usize index = y * 16 + x; + CAPTURE(x, y); + REQUIRE(std::isfinite(values[index])); + REQUIRE(std::isfinite(edges[index])); + if(!step) + { + REQUIRE(values[index] == Approx(4).margin(1.0e-6)); + REQUIRE(std::abs(edges[index]) <= 1.0e-6); + } + else + { + // Reflection/complement and row invariance are independent of the recursive implementation. + REQUIRE(values[index] + values[y * 16 + 15 - x] == Approx(1).margin(1.0e-5)); + REQUIRE(values[index] == Approx(values[x]).margin(1.0e-5)); + REQUIRE(edges[index] == Approx(edges[y * 16 + 15 - x]).margin(1.0e-5)); + REQUIRE(edges[index] == Approx(edges[x]).margin(1.0e-5)); + REQUIRE(edges[index] <= edges[y * 16 + 7] + 1.0e-5F); + REQUIRE(edges[y * 16 + 7] > 0); + REQUIRE(edges[y * 16 + 7] == Approx(edges[y * 16 + 8]).margin(1.0e-5)); + } + } + } + REQUIRE(c15_test::Read(ds, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + c15_test::CheckGeometry(ds, {16, 4, 1}); + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 7e889f3d8b..86dd3442f2 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" + #include "ItkGoldenTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" @@ -306,3 +308,44 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: ITK real-image golden (defaults UnitTest::CheckArraysInheritTupleDims(ds); } + +TEST_CASE("ImageProcessing::C15Compatibility resident widened Square persists and feeds integer components", "[ImageProcessing][C15Compatibility][Issue1874][SquareImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const c15_test::Directory output; + DataStructure source; + const std::vector original{0, 2147483647, 0, 2}; + const auto input = ip_test::BuildScalarValuesImage(source, original); + c15_test::SetGeometry(source); + REQUIRE(c15_test::Read(source, input, {1, 1, 4}, scope) == original); + const auto square = c15_test::Pointwise(source, input, "ExactSquare"); + // (2^31-1)^2 is exact in Int64 and cannot be transported exactly through Float64. + const std::vector expected{0, 4611686014132420609LL, 0, 4}; + REQUIRE(c15_test::Read(source, square, {1, 1, 4}, scope) == expected); + const auto save = DREAM3D::WriteFile(output.path() / "square.dream3d", source); + SIMPLNX_RESULT_REQUIRE_VALID(save); + c15_test::CheckSaved(output.path() / "square.dream3d", square, {1, 1, 4}, expected); + auto load = DREAM3D::LoadDataStructure(output.path() / "square.dream3d"); + SIMPLNX_RESULT_REQUIRE_VALID(load); + auto& reloaded = load.value(); + REQUIRE(c15_test::Read(reloaded, square, {1, 1, 4}, scope) == expected); + c15_test::CheckGeometry(reloaded, {4, 1, 1}); + const WriteImageFilter writer; + c15_test::Execute(writer, reloaded, c15_test::WriteArguments(square, output.path() / "square.mha"), scope); + auto raw = c15_test::ReadMha(output.path() / "square.mha"); + REQUIRE(c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 4}, scope) == expected); + const ImageGeom* rawGeometry = nullptr; + REQUIRE_NOTHROW(rawGeometry = &raw.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(rawGeometry->getDimensions() == SizeVec3{4, 1, 1}); + REQUIRE(rawGeometry->getSpacing() == FloatVec3{1, 2, 1}); + REQUIRE(rawGeometry->getOrigin() == FloatVec3{4, 5, 0}); + const auto labels = c15_test::Components(reloaded, square, "Components"); + REQUIRE(c15_test::Read(reloaded, labels, {1, 1, 4}, scope) == std::vector{0, 1, 0, 2}); + REQUIRE(c15_test::Read(reloaded, square, {1, 1, 4}, scope) == expected); + REQUIRE(c15_test::Read(source, square, {1, 1, 4}, scope) == expected); + REQUIRE(c15_test::Read(source, input, {1, 1, 4}, scope) == original); + UnitTest::CheckArraysInheritTupleDims(source); + UnitTest::CheckArraysInheritTupleDims(reloaded); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index 319f1e48b8..ccbea7737c 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -1,3 +1,5 @@ +#include "C15CompatibilityTestUtils.hpp" + #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -935,3 +937,68 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co // Independent flood-fill evidence gives 3715 legacy versus 3712 corrected components. The retained search // can select fewer components after the arithmetic fix; this regression is not an ITK-parity or optimum claim. } + +TEST_CASE("ImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", "[ImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); + const auto inf = std::numeric_limits::infinity(); + DataStructure ds; + const bool noFinite = variant >= 3; + const uint8 outside = variant == 4 ? 7 : 0; + const auto input = noFinite ? ip_test::BuildScalarValuesImage(ds, {std::numeric_limits::quiet_NaN(), -inf, inf}) : ip_test::BuildScalarValuesImage(ds, {100, 102}); + const ShapeType shape{1, 1, noFinite ? usize{3} : usize{2}}; + const ThresholdMaximumConnectedComponentsImageFilter filter; + auto args = c15_test::ImageArguments(filter, input, "Thresholded"); + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(0)); + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(variant == 1 ? 99 : (variant == 2 ? -inf : 65536))); + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_InsideValue_Key, std::make_any(1)); + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_OutsideValue_Key, std::make_any(outside)); + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(noFinite ? 1 : 0)); + const auto preflight = filter.preflight(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == (variant == 0 ? 0 : 1)); + if(noFinite) + { + REQUIRE(result.result.warnings().front().code == -28614); + REQUIRE(result.result.warnings().front().message.find("1 NaN, 1 negative infinity and 1 positive infinity") != std::string::npos); + REQUIRE(result.result.warnings().front().message.find("No finite pixels are available") != std::string::npos); + } + if(variant == 1 || variant == 2) + { + REQUIRE(result.result.warnings().front().code == -28615); + REQUIRE(result.result.warnings().front().message.find("empty native intensity band") != std::string::npos); + } + const auto thresholded = input.replaceName("Thresholded"); + const std::vector expected = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, outside); + REQUIRE(c15_test::Read(ds, thresholded, shape, expectedStore) == expected); + if(outside == 7) + { + // CC treats all nonzero values as foreground, including the documented expert Outside Value. + const auto direct = c15_test::Components(ds, thresholded, "DirectComponents"); + REQUIRE(c15_test::Read(ds, direct, shape, expectedStore) == std::vector{1, 1, 1}); + } + const auto desired = c15_test::Select(ds, thresholded, 1, 1, "DesiredInside"); + const std::vector expectedDesired = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); + REQUIRE(c15_test::Read(ds, desired, shape, expectedStore) == expectedDesired); + const auto labels = c15_test::Components(ds, desired, "Components"); + const std::vector expectedLabels = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); + REQUIRE(c15_test::Read(ds, labels, shape, expectedStore) == expectedLabels); + REQUIRE(c15_test::Read(ds, thresholded, shape, expectedStore) == expected); + if(noFinite) + { + const auto sourceValues = c15_test::Read(ds, input, shape, expectedStore); + REQUIRE(std::isnan(sourceValues[0])); + REQUIRE(sourceValues[1] == -inf); + REQUIRE(sourceValues[2] == inf); + } + else + { + REQUIRE(c15_test::Read(ds, input, shape, expectedStore) == std::vector{100, 102}); + } + UnitTest::CheckArraysInheritTupleDims(ds); +} \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 972146e2fc..9f93b7988b 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -1,3 +1,8 @@ +#include "C15CompatibilityTestUtils.hpp" +#include "ImageProcessing/Filters/AcosImageFilter.hpp" +#include "ImageProcessing/Filters/AsinImageFilter.hpp" +#include "ImageProcessing/Filters/TanImageFilter.hpp" +#include "simplnx/Parameters/CreateColorMapParameter.hpp" #include #include @@ -2789,3 +2794,96 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: rejects a source-group grant sma REQUIRE(executeResult.result.errors().front().code == -27026); } } + +TEST_CASE("ImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[ImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + UnitTest::AlgorithmTestScope scope(scenario); + const usize operation = GENERATE(usize{0}, usize{1}, usize{2}); + CAPTURE(operation, scenario); + const c15_test::Directory output; + const std::vector original = operation == 0 ? std::vector{1, 0, -1, -1, 0, 1} : (operation == 1 ? std::vector{-1, 0, 1, 1, 0, -1} : std::vector{0, 1, -1, -1, 1, 0}); + const std::vector expected = operation == 0 ? + std::vector{0, 1.57079632679489661923F, 3.14159265358979323846F, 3.14159265358979323846F, 1.57079632679489661923F, 0} : + (operation == 1 ? std::vector{-1.57079632679489661923F, 0, 1.57079632679489661923F, 1.57079632679489661923F, 0, -1.57079632679489661923F} : + std::vector{0, 1.5574077246549022305F, -1.5574077246549022305F, -1.5574077246549022305F, 1.5574077246549022305F, 0}); + DataStructure source; + const auto input = recon_test::BuildImageFromPattern(source, 3, 1, 2, original); + c15_test::SetGeometry(source); + REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); + const std::string outputName = operation == 2 ? "Tangent" : "Radians"; + const auto quantitative = operation == 0 ? c15_test::Pointwise(source, input, outputName) : + (operation == 1 ? c15_test::Pointwise(source, input, outputName) : c15_test::Pointwise(source, input, outputName)); + const auto values = c15_test::Read(source, quantitative, {2, 1, 3}, scope); + for(usize index = 0; index < expected.size(); ++index) + { + CAPTURE(index); + if(expected[index] == 0) + { + REQUIRE(values[index] == 0); + } + else + { + REQUIRE(values[index] == Approx(expected[index]).epsilon(1.0e-6)); + } + } + // Inverse-trigonometric outputs are radians; tangent values are dimensionless. + // Geometry length units are a separate .dream3d field. + const auto save = DREAM3D::WriteFile(output.path() / "quantitative.dream3d", source); + SIMPLNX_RESULT_REQUIRE_VALID(save); + c15_test::CheckSaved(output.path() / "quantitative.dream3d", quantitative, {2, 1, 3}, values); + auto load = DREAM3D::LoadDataStructure(output.path() / "quantitative.dream3d"); + SIMPLNX_RESULT_REQUIRE_VALID(load); + auto& reloaded = load.value(); + REQUIRE(c15_test::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); + c15_test::CheckGeometry(reloaded, {3, 1, 2}); + const WriteImageFilter writer; + c15_test::Execute(writer, reloaded, c15_test::WriteArguments(quantitative, output.path() / "raw.mha"), scope); + if(operation == 0) + { + c15_test::Execute(writer, reloaded, c15_test::WriteArguments(quantitative, output.path() / "raw.tif"), scope); + auto displayArgs = c15_test::WriteArguments(quantitative, output.path() / "display.png"); + displayArgs.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(true)); + displayArgs.insertOrAssign(WriteImageFilter::k_SelectedPreset_Key, std::make_any("Grayscale")); + c15_test::Execute(writer, reloaded, displayArgs, scope); + } + for(usize slice = 0; slice < 2; ++slice) + { + const auto suffix = slice == 0 ? "_000" : "_001"; + auto raw = c15_test::ReadMha(output.path() / (std::string("raw") + suffix + ".mha")); + const auto pixels = c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 3}, scope); + const ImageGeom* rawGeometry = nullptr; + REQUIRE_NOTHROW(rawGeometry = &raw.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(rawGeometry->getDimensions() == SizeVec3{3, 1, 1}); + REQUIRE(rawGeometry->getSpacing() == FloatVec3{1, 2, 1}); + REQUIRE(rawGeometry->getOrigin() == FloatVec3{4, 5, 0}); + for(usize x = 0; x < 3; ++x) + { + REQUIRE(pixels[x] == values[slice * 3 + x]); + REQUIRE(pixels[x] == Approx(expected[slice * 3 + x]).margin(1.0e-6)); + } + if(operation == 0) + { + const auto tiffPath = output.path() / (std::string("raw") + suffix + ".tif"); + const auto tiff = c15_test::Decode(tiffPath, 3, 1); + REQUIRE(tiff == pixels); + const auto io = CreateImageIO(tiffPath); + SIMPLNX_RESULT_REQUIRE_VALID(io); + const auto metadata = io.value()->readMetadata(tiffPath); + SIMPLNX_RESULT_REQUIRE_VALID(metadata); + REQUIRE(metadata.value().spacing.has_value()); + REQUIRE(metadata.value().origin.has_value()); + REQUIRE(metadata.value().spacing.value() == FloatVec3{1, 2, 1}); + REQUIRE(metadata.value().origin.value() == FloatVec3{4, 5, 0}); + const auto display = c15_test::Decode(output.path() / (std::string("display") + suffix + ".png"), 3, 1, 3); + const std::vector expectedDisplay = slice == 0 ? std::vector{0, 0, 0, 127, 127, 127, 255, 255, 255} : std::vector{255, 255, 255, 127, 127, 127, 0, 0, 0}; + REQUIRE(display == expectedDisplay); + } + } + REQUIRE(c15_test::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); + REQUIRE(c15_test::Read(source, quantitative, {2, 1, 3}, scope) == values); + REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); + UnitTest::CheckArraysInheritTupleDims(source); + UnitTest::CheckArraysInheritTupleDims(reloaded); +} \ No newline at end of file From 019ca3c6a94a6e5c56f158946db9a572c4b5dcf5 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:47:53 -0500 Subject: [PATCH 28/98] BUG: Reserve Otsu scan payload before the optional ring Give required scan batches priority and admit the optional ring only from remaining charged capacity. Keep batched fallback and exact bins. Reproduced the exact-grant one-value read regression. Paired Release and targeted Debug tests passed with the correction. Signed-off-by: Jessica Marquis --- .../docs/OtsuMultipleThresholdsImageFilter.md | 2 +- .../OtsuMultipleThresholdsImageFilterTest.cpp | 48 ++++++++++++++- .../ImageProcessing/OtsuImageEngine.hpp | 58 +++++++++++-------- 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md index ee4ef2f16f..0ceb97c227 100644 --- a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md @@ -47,7 +47,7 @@ ITK-free, out-of-core-capable reimplementation of the legacy ITK Otsu Multiple T ## Algorithm -For integral inputs whose selected native intensity span is at most 65,535, the filter can determine the input range and exact value frequencies in one scan using a fixed 65,536-entry frequency ring. It then fills the requested histogram without rereading the input. Wider integral ranges, or a working-memory grant that cannot hold this optional ring, use a second bounded histogram scan. Floating-point inputs use a selected finite range/count scan followed by the histogram scan. A true constant has an analytic tied-threshold path. +For integral inputs whose selected native intensity span is at most 65,535, the filter can determine the input range and exact value frequencies in one scan using a fixed 65,536-entry frequency ring. It then fills the requested histogram without rereading the input. Wider integral ranges use a second bounded histogram scan. Buffered scans also use a second scan when the working-memory grant cannot hold the ring after reserving the input and any mask buffers. Floating-point inputs use a selected finite range/count scan followed by the histogram scan. A true constant has an analytic tied-threshold path. The histogram uses the existing marginal upper bound `max + (max - min) / (numberOfBins * 100)`. An upper-bound extension that rounds back to `max` is permitted when the bin geometry remains usable; endpoint clamping keeps minimum/maximum samples inside their bins before an integer index conversion. Native full-span 64-bit inputs remain valid for a two-bin split. No native maximum is incremented. diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 3759983fd9..081c62b282 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -27,6 +27,7 @@ #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/NumberParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/CacheMemoryBudgetManager.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp" @@ -291,8 +292,9 @@ template class OtsuTransferStore : public DataStore { public: - explicit OtsuTransferStore(usize size) + explicit OtsuTransferStore(usize size, IDataStore::StoreType storeType = IDataStore::StoreType::Empty) : DataStore({size}, {1}, T{}) + , m_StoreType(storeType) { } ~OtsuTransferStore() noexcept override = default; @@ -304,7 +306,7 @@ class OtsuTransferStore : public DataStore IDataStore::StoreType getStoreType() const override { // An abstract-store route witness only. This test store has no disk backing. - return IDataStore::StoreType::Empty; + return m_StoreType; } Result<> copyIntoBuffer(usize start, nonstd::span buffer) const override { @@ -368,6 +370,7 @@ class OtsuTransferStore : public DataStore } private: + const IDataStore::StoreType m_StoreType; bool m_FailRead = false; bool m_WarnOnFirstRead = false; usize m_FailReadAt = 0; @@ -617,6 +620,47 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants an } } +TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer ring preserves full payload batches at its grant boundary", + "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuRingGrant]") +{ + // The counting store exercises charged scan buffers and records caller transfers. It has no disk backing. + constexpr usize k_ValueCount = 16; + constexpr uint64 k_PayloadBytes = k_ValueCount * sizeof(uint16); + constexpr uint64 k_RingBytes = 65536ULL * sizeof(ImageProcessing::detail::OtsuFrequencyEntry); + const uint64 grant = GENERATE_COPY(k_RingBytes - 1, k_RingBytes, k_RingBytes + k_PayloadBytes); + CAPTURE(grant); + + OtsuTransferStore input(k_ValueCount, IDataStore::StoreType::OutOfCore); + for(usize i = 1; i < k_ValueCount; i += 2) + { + input.setValue(i, uint16{4}); + } + + auto& budget = CacheMemoryBudgetManager::instance(); + const uint64 reservedBefore = budget.reservedWorkingMemoryBytes(); + const uint64 ceiling = budget.maximumWorkingMemoryBytes(); + REQUIRE(ceiling >= reservedBefore + grant); + const uint64 pressureBytes = ceiling - reservedBefore - grant; + { + const auto pressure = budget.tryReserveWorkingMemoryWithoutEviction(pressureBytes); + REQUIRE(pressure.sizeBytes() == pressureBytes); + const uint64 reservedWithPressure = budget.reservedWorkingMemoryBytes(); + REQUIRE(ceiling - reservedWithPressure == grant); + + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; + const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().FiniteCount == 16); + REQUIRE(result.value().HistogramData.counts == std::vector{8, 8}); + REQUIRE(budget.reservedWorkingMemoryBytes() == reservedWithPressure); + // The grant must fit both resources for a single pass. The fallback reads one full buffer per pass. + CHECK(input.maximumRead() == k_ValueCount); + CHECK(input.reads() == (grant == k_RingBytes + k_PayloadBytes ? 1 : 2)); + } + REQUIRE(budget.reservedWorkingMemoryBytes() == reservedBefore); +} + TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 99573e3d8e..5a3bcd82fb 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -209,8 +209,8 @@ inline OtsuBufferPlan ReserveOtsuBuffers(usize total, usize bytesPerValue, usize } template -Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel, const VisitorT& visitor, - bool parallelRange = false) +Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel, usize batchValues, + const VisitorT& visitor, bool parallelRange = false) { Result<> status; const usize total = input.getSize(); @@ -240,20 +240,17 @@ Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataSt return status; } - auto plan = ReserveOtsuBuffers(total, sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)), options.TargetBytes, - input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore), - parallelRange ? sizeof(OtsuFloatingRangeSummary) : 0); // T may be bool. A raw array provides the contiguous storage that vector cannot provide. - auto valuesPtr = std::make_unique_for_overwrite(plan.Values); + auto valuesPtr = std::make_unique_for_overwrite(batchValues); // MaskT may be bool and must provide contiguous values for store transfers. - auto maskValuesPtr = maskPtr == nullptr ? nullptr : std::make_unique_for_overwrite(plan.Values); + auto maskValuesPtr = maskPtr == nullptr ? nullptr : std::make_unique_for_overwrite(batchValues); for(usize start = 0; start < total;) { if(shouldCancel) { return status; } - const usize count = std::min(plan.Values, total - start); + const usize count = std::min(batchValues, total - start); if(maskPtr != nullptr) { Result<> readMaskResult = maskPtr->copyIntoBuffer(start, nonstd::span(maskValuesPtr.get(), count)); @@ -366,8 +363,8 @@ Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T * @return Histogram and selected finite and nonfinite sample counts, or an input, numeric, or transfer error. * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. * - * Mask selection precedes nonfinite and range checks. Compact native integer ranges retain exact frequencies in a fixed ring. - * Other ranges use a second bounded pass. + * Mask selection precedes nonfinite and range checks. Compact native integer ranges can retain exact frequencies in a fixed ring. + * The ring uses capacity left after the input and mask buffers. A scan without usable ring frequencies uses a second bounded pass for nonconstant input. */ template Result ComputeOtsuHistogram(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) @@ -392,31 +389,32 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp result.HistogramData.counts.assign(options.NumberOfBins, 0); T minValue{}; T maxValue{}; + const bool external = input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore); + const usize total = input.getSize(); + const usize bytesPerValue = sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)); + constexpr bool k_ParallelRange = std::is_floating_point_v; + auto plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external, k_ParallelRange ? sizeof(detail::OtsuFloatingRangeSummary) : 0); constexpr usize ringSize = 65536; - std::unique_ptr>> frequenciesPtr; CacheMemoryBudgetManager::WorkingMemoryReservation ringReservation; + std::unique_ptr>> frequenciesPtr; using CompactRangeType = std::conditional_t, bool, const bool>; CompactRangeType compactRange = false; if constexpr(std::is_integral_v) { - const bool external = input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore); const usize ringBytes = ringSize * sizeof(detail::OtsuFrequencyEntry); - if(external) + // Admit the optional ring in full only after the required payload, without evicting cache entries. + if(external && ResolvePreferredWorkingMemoryBytes(ringBytes) >= ringBytes) { - ringReservation = ReserveWorkingMemory(ringBytes, ringBytes); + ringReservation = CacheMemoryBudgetManager::instance().tryReserveWorkingMemoryWithoutEviction(ringBytes); } - compactRange = !external || ringReservation.sizeBytes() >= ringBytes; + compactRange = !external || ringReservation.sizeBytes() == ringBytes; if(compactRange) { frequenciesPtr = std::make_unique>>(ringSize); } - else - { - ringReservation = {}; - } } Result<> rangeResult = detail::VisitOtsuValues( - input, maskPtr, options, shouldCancel, + input, maskPtr, options, shouldCancel, plan.Values, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { if constexpr(std::is_floating_point_v) { @@ -492,13 +490,22 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp ++entry.Count; } } + if(!compactRange) + { + frequenciesPtr.reset(); + ringReservation = {}; + } } } } return {}; } }, - std::is_floating_point_v); + k_ParallelRange); + if(k_ParallelRange || compactRange || minValue == maxValue) + { + plan.Reservation = {}; + } if(rangeResult.invalid()) { return ConvertInvalidResult(std::move(rangeResult)); @@ -550,9 +557,12 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } else { - frequenciesPtr.reset(); - ringReservation = {}; - Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { + if constexpr(k_ParallelRange) + { + // The floating-point histogram pass needs no range summaries. + plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external); + } + Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, plan.Values, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { for(usize i = 0; i < values.size(); ++i) { if((i % 65536) == 0 && shouldCancel) From 9e157e326ca5ce05e7022d072c8faca93819a0ef Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:47:53 -0500 Subject: [PATCH 29/98] BUG: Retain checked Curvature Flow tile warnings Forward each reached row result in the checked X-tile route. Keep the prior warnings on a later transfer error and the existing cancellation boundaries. Reproduced dropped row warnings, then passed the paired Release and targeted Debug cases. Signed-off-by: Jessica Marquis --- .../test/CurvatureFlowImageFilterTest.cpp | 107 +++++++++++++++++- .../FiniteDifferenceEngine.hpp | 34 ++++-- 2 files changed, 128 insertions(+), 13 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 2e18d63d63..8e4d7f895d 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -207,7 +207,7 @@ class CurvatureFlowForwardingStore : public AbstractDataStore { ++m_WriteCount; m_MaxWriteValues = std::max(m_MaxWriteValues, values.size()); - Result<> result = m_FailWrite ? MakeErrorResult(-28891, "Injected Curvature Flow output transfer failure.") : m_Store.copyFromBuffer(offset, values); + Result<> result = (m_FailWrite || m_WriteCount == m_FailWriteAt) ? MakeErrorResult(-28891, "Injected Curvature Flow output transfer failure.") : m_Store.copyFromBuffer(offset, values); if(m_WarnOnWrite) { result.warnings().push_back({.code = -28894, .message = "Injected Curvature Flow write warning."}); @@ -310,6 +310,10 @@ class CurvatureFlowForwardingStore : public AbstractDataStore { m_FailReadAt = call; } + void failWriteAt(usize call) + { + m_FailWriteAt = call; + } void warnOnReads() { m_WarnOnRead = true; @@ -335,6 +339,7 @@ class CurvatureFlowForwardingStore : public AbstractDataStore mutable usize m_MaxReadValues = 0; usize m_MaxWriteValues = 0; usize m_FailReadAt = std::numeric_limits::max(); + usize m_FailWriteAt = std::numeric_limits::max(); bool m_WarnOnRead = false; bool m_WarnOnWrite = false; usize m_CancelWriteAt = std::numeric_limits::max(); @@ -854,6 +859,106 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D termi REQUIRE(output.getTupleShape() == shape); } +TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +{ + enum class Exit + { + Success, + HaloReadFailure, + OutputWriteFailure, + CancelAfterWrite + }; + const auto exit = GENERATE(Exit::Success, Exit::HaloReadFailure, Exit::OutputWriteFailure, Exit::CancelAfterWrite); + CAPTURE(exit); + const bool failRead = exit == Exit::HaloReadFailure; + const bool failWrite = exit == Exit::OutputWriteFailure; + const bool cancel = exit == Exit::CancelAfterWrite; + + // Use resident forwarding stores to inject diagnostics into the real X-tile transfers. + constexpr usize nx = 5; + constexpr usize ny = 3; + const ShapeType shape{1, ny, nx}; + DataStore input(shape, ShapeType{1}, uint8{0}); + const std::vector field{0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 0, 100, 100, 100, 100}; + const auto copyResult = input.copyFromBuffer(0, field); + SIMPLNX_RESULT_REQUIRE_VALID(copyResult); + CurvatureFlowForwardingStore first(shape); + CurvatureFlowForwardingStore second(shape); + CurvatureFlowForwardingStore output(shape); + first.warnOnReads(); + output.warnOnWrites(); + output.setValue(6, uint8{255}); + output.setValue(7, uint8{255}); + std::atomic_bool shouldCancel{false}; + if(failRead) + { + first.failReadAt(2); // The second row of the first halo fails after the first row warned. + } + if(failWrite) + { + output.failWriteAt(5); // The output tile after the clipped center fails. + } + if(cancel) + { + output.cancelOnWrite(4, shouldCancel); // The clipped tile finishes its write before cancellation. + } + + constexpr usize limit = ImageProcessing::detail::k_FiniteDifference2DFixedStateBytes + (14 * sizeof(float64)); + const auto plan = ImageProcessing::detail::BuildFiniteDifference2DBufferPlan(nx, ny, 1, sizeof(float64), limit); + REQUIRE(plan.valid); + REQUIRE(plan.coreCols == 2); + REQUIRE(plan.coreRows == 1); + const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( + input, output, first, second, nx, ny, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); + if(failRead || failWrite) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().size() == 1); + CHECK(result.errors().front().code == (failRead ? -28892 : -28891)); + CHECK(result.errors().front().message == (failRead ? "Injected delayed Curvature Flow input transfer failure." : "Injected Curvature Flow output transfer failure.")); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result); + } + + // Three X tiles each read 2, 3, and 2 halo rows for the three Y rows. + const usize expectedReads = failRead ? 2 : 21; + usize expectedWrites = 9; + if(failRead) + { + expectedWrites = 0; + } + else if(failWrite) + { + expectedWrites = 5; + } + else if(cancel) + { + expectedWrites = 4; + } + CHECK(first.readCount() == expectedReads); + CHECK(first.maxReadValues() == (failRead ? 3 : 4)); + CHECK(output.writeCount() == expectedWrites); + CHECK(shouldCancel.load() == cancel); + // At (1, 1), the independently derived update is -125. A time step of 10 clips 100 - 1250 to zero. + CHECK(output.getValue(6) == (failRead ? 255 : 0)); + if(failRead || failWrite || cancel) + { + CHECK(output.getValue(7) == 255); + } + + const auto countWarnings = [&](int32 code, const std::string& message) { + return static_cast(std::count_if(result.warnings().begin(), result.warnings().end(), [&](const Warning& warning) { return warning.code == code && warning.message == message; })); + }; + const usize expectedClipping = failRead ? 0 : 1; + CHECK(countWarnings(-28893, "Injected Curvature Flow read warning.") == expectedReads); + CHECK(countWarnings(-28894, "Injected Curvature Flow write warning.") == expectedWrites); + CHECK(static_cast(std::count_if(result.warnings().begin(), result.warnings().end(), + [](const Warning& warning) { return warning.code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput; })) == expectedClipping); + CHECK(result.warnings().size() == expectedReads + expectedWrites + expectedClipping); +} + // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a valid scalar input produces a // SameAsInput output array (unlike GradientMagnitude's fixed float32, CurvatureFlow preserves the input type). diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 31a5d5d3a0..33672a2f22 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -1378,7 +1378,7 @@ class TemporaryFiniteDifferenceStore const std::atomic_bool& m_ShouldCancel; }; -template +template Result<> ReadFiniteDifference2DRegion(const StoreT& store, usize nx, usize xBegin, usize xEnd, usize yBegin, usize yEnd, nonstd::span buffer) { const usize width = xEnd - xBegin; @@ -1388,17 +1388,22 @@ Result<> ReadFiniteDifference2DRegion(const StoreT& store, usize nx, usize xBegi { return store.copyIntoBuffer(yBegin * nx, buffer.subspan(0, valueCount)); } + Result<> accumulated; for(usize row = 0; row < rows; ++row) { - if(Result<> result = store.copyIntoBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyIntoBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width))); result.invalid()) { + if constexpr(CheckedOutput) + { + return MergeResults(std::move(accumulated), std::move(result)); + } return result; } } - return {}; + return accumulated; } -template +template Result<> WriteFiniteDifference2DRegion(StoreT& store, usize nx, usize xBegin, usize xEnd, usize yBegin, usize yEnd, nonstd::span buffer) { const usize width = xEnd - xBegin; @@ -1408,14 +1413,19 @@ Result<> WriteFiniteDifference2DRegion(StoreT& store, usize nx, usize xBegin, us { return store.copyFromBuffer(yBegin * nx, buffer.subspan(0, valueCount)); } + Result<> accumulated; for(usize row = 0; row < rows; ++row) { - if(Result<> result = store.copyFromBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width)); result.invalid()) + if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyFromBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width))); result.invalid()) { + if constexpr(CheckedOutput) + { + return MergeResults(std::move(accumulated), std::move(result)); + } return result; } } - return {}; + return accumulated; } template @@ -1566,7 +1576,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs const usize xEnd = xBegin + std::min(plan.coreCols, nx - xBegin); const usize valueCount = (yEnd - yBegin) * (xEnd - xBegin); if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, ReadFiniteDifference2DRegion(inStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); + ioResult, ReadFiniteDifference2DRegion(inStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); result.invalid()) { return finish(std::move(result)); @@ -1576,7 +1586,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs castBlock[index] = static_cast(inputBlock[index]); } if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, WriteFiniteDifference2DRegion(firstStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(castBlock.data(), valueCount))); + ioResult, WriteFiniteDifference2DRegion(firstStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(castBlock.data(), valueCount))); result.invalid()) { return finish(std::move(result)); @@ -1626,7 +1636,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs const usize coreWidth = xEnd - xBegin; const usize coreRows = yEnd - yBegin; if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, loadXBegin, loadXEnd, loadYBegin, loadYEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); + ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, loadXBegin, loadXEnd, loadYBegin, loadYEnd, nonstd::span(inputBlock.data(), inputBlock.size()))); result.invalid()) { return finish(std::move(result)); @@ -1662,7 +1672,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs } const usize valueCount = coreRows * coreWidth; if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, WriteFiniteDifference2DRegion(*nextStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputBlock.data(), valueCount))); + ioResult, WriteFiniteDifference2DRegion(*nextStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputBlock.data(), valueCount))); result.invalid()) { return finish(std::move(result)); @@ -1694,7 +1704,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs const usize xEnd = xBegin + std::min(plan.coreCols, nx - xBegin); const usize valueCount = (yEnd - yBegin) * (xEnd - xBegin); if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(workingBlock.data(), workingBlock.size()))); + ioResult, ReadFiniteDifference2DRegion(*currentStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(workingBlock.data(), workingBlock.size()))); result.invalid()) { return finish(std::move(result)); @@ -1705,7 +1715,7 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs return finish(); } if(Result<> result = detail::RecordFiniteDifferenceIoResult( - ioResult, WriteFiniteDifference2DRegion(outStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputValues.data(), valueCount))); + ioResult, WriteFiniteDifference2DRegion(outStore, nx, xBegin, xEnd, yBegin, yEnd, nonstd::span(outputValues.data(), valueCount))); result.invalid()) { return finish(std::move(result)); From ec48d116a1e7d2d2587b6d401a6b275bbea0290c Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:47:54 -0500 Subject: [PATCH 30/98] STY: Size the image writer byte-mask buffer Use a sized UInt8 mask page and document contiguous Boolean ownership. Keep the page limit and image-write behavior. Paired writer cases passed. Signed-off-by: Jessica Marquis --- .../Filters/Algorithms/WriteImageColorUtilities.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index df80bc6c4d..8ca397e343 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -88,9 +88,11 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, constexpr usize targetPageBytes = 1024 * 1024; const usize maskBytes = maskArray == nullptr ? 0 : (maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8)); const usize pageCount = std::min(count, std::max(1, targetPageBytes / (sizeof(T) + maskBytes))); + // Scalar input can use bool. The store needs a contiguous bool buffer, which std::vector cannot provide. auto values = std::make_unique(pageCount); + // The Boolean mask store also needs a contiguous bool buffer. std::unique_ptr boolMask; - std::unique_ptr byteMask; + std::unique_ptr> byteMask; if(maskArray != nullptr) { if(maskArray->getDataType() == DataType::boolean) @@ -99,7 +101,7 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, } else { - byteMask = std::make_unique(pageCount); + byteMask = std::make_unique>(pageCount); } } Range range; @@ -125,7 +127,7 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, } else if(byteMask != nullptr) { - status = MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().copyIntoBuffer(offset, nonstd::span(byteMask.get(), size))); + status = MergeResults(std::move(status), maskArray->getIDataStoreRefAs>().copyIntoBuffer(offset, nonstd::span(byteMask->data(), size))); } if(status.invalid()) { @@ -138,7 +140,7 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, { if(!std::isfinite(value)) { - const bool selected = boolMask != nullptr ? boolMask[index] : (byteMask == nullptr || byteMask[index] != 0); + const bool selected = boolMask != nullptr ? boolMask[index] : (byteMask == nullptr || (*byteMask)[index] != 0); if(selected) { if(policy == 0) From a31a96c7dde78eb0a1ac7ef399d11192b1114d2c Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 09:47:54 -0500 Subject: [PATCH 31/98] BUG: Give new Otsu diagnostics unique codes Move the eight new errors away from existing OrientationAnalysis codes. Update the exact assertions without changing validation or policy. Paired owning tests passed. Signed-off-by: Jessica Marquis --- .../OtsuMultipleThresholdsImageFilterTest.cpp | 18 +++++++++--------- .../ImageProcessing/HistogramEngine.hpp | 9 +++++---- .../ImageProcessing/OtsuImageEngine.hpp | 8 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 081c62b282..045ad812bf 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -427,7 +427,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects eac const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, otsuArguments(inputPath)); SIMPLNX_RESULT_REQUIRE_INVALID(result.result); - REQUIRE(result.result.errors().front().code == -8324); + REQUIRE(result.result.errors().front().code == -28654); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -479,7 +479,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite d const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, args); SIMPLNX_RESULT_REQUIRE_INVALID(result.result); - REQUIRE(result.result.errors().front().code == -8325); + REQUIRE(result.result.errors().front().code == -28655); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -585,7 +585,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants an input.setValue(0, std::numeric_limits::max() - 1); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8326); + REQUIRE(result.errors().front().code == -28656); } SECTION("opposite float64 extrema overflow the range") { @@ -593,21 +593,21 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants an input.setValue(0, -std::numeric_limits::max()); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8326); + REQUIRE(result.errors().front().code == -28656); } SECTION("nonzero means square to zero") { const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 0, .max = 3e-200, .binWidth = 1e-200}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8320); + REQUIRE(result.errors().front().code == -28650); } SECTION("finite means square to infinity") { const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 1e200, .max = 4e200, .binWidth = 1e200}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8320); + REQUIRE(result.errors().front().code == -28650); } SECTION("unique rational optimum stays unchanged at ordinary scale") { @@ -1104,11 +1104,11 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep ex input.setValue(0, std::numeric_limits::quiet_NaN()); const auto noFiniteResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(noFiniteResult); - REQUIRE(noFiniteResult.errors().front().code == -8325); + REQUIRE(noFiniteResult.errors().front().code == -28655); const DataStore empty({0}, {1}, 0.0); const auto emptyResult = ImageProcessing::ComputeOtsuHistogram(empty, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(emptyResult); - REQUIRE(emptyResult.errors().front().code == -8325); + REQUIRE(emptyResult.errors().front().code == -28655); } TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") @@ -1176,7 +1176,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper co input.setValue(1, 1e20 + 32768.0); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8327); + REQUIRE(result.errors().front().code == -28657); } } diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 03a5080b40..211299bc39 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -528,7 +528,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween)) { - return MakeErrorResult>(-8320, "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); + return MakeErrorResult>(-28650, "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); } } @@ -548,7 +548,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween, valleyEmphasisFactor == 0.0)) { - return MakeErrorResult>(-8321, "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + return MakeErrorResult>(-28651, "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } } } @@ -637,7 +637,8 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween)) { - return MakeErrorResult>(-8322, "Otsu candidate class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); + return MakeErrorResult>(-28652, + "Otsu candidate class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); } } @@ -654,7 +655,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween, valleyEmphasisFactor == 0.0)) { - return MakeErrorResult>(-8323, "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + return MakeErrorResult>(-28653, "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } } } diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 5a3bcd82fb..298836cc90 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -299,7 +299,7 @@ inline Result<> ConfigureCheckedOtsuBins(Histogram& histogram, bool nativeConsta if(!std::isfinite(range) || range <= 0.0 || !std::isfinite(upper) || upper <= histogram.min || !std::isfinite(histogram.binWidth) || histogram.binWidth <= 0.0 || !std::isfinite(1.0 / histogram.binWidth)) { - return MakeErrorResult(-8326, "Otsu cannot form a finite positive histogram range/bin width from the selected native values. Rescale the finite input or use fewer histogram bins."); + return MakeErrorResult(-28656, "Otsu cannot form a finite positive histogram range/bin width from the selected native values. Rescale the finite input or use fewer histogram bins."); } float64 previousBoundary = histogram.min; float64 previousMeasurement = histogram.min; @@ -313,7 +313,7 @@ inline Result<> ConfigureCheckedOtsuBins(Histogram& histogram, bool nativeConsta const float64 measurement = histogram.min + ((static_cast(bin) + 0.5) * histogram.binWidth); if(!std::isfinite(boundary) || !std::isfinite(measurement) || boundary <= previousBoundary || (bin != 0 && measurement <= previousMeasurement)) { - return MakeErrorResult(-8327, "Otsu histogram boundaries or measurements collapse at the selected precision. Rescale the finite input or use fewer histogram bins."); + return MakeErrorResult(-28657, "Otsu histogram boundaries or measurements collapse at the selected precision. Rescale the finite input or use fewer histogram bins."); } previousBoundary = boundary; previousMeasurement = measurement; @@ -425,7 +425,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } if(!options.WarnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) { - return MakeErrorResult(-8324, "Otsu found a selected NaN or infinity. Choose Warn and Continue to retain endpoint-bin participation, or explicitly exclude it with Use Mask Array."); + return MakeErrorResult(-28654, "Otsu found a selected NaN or infinity. Choose Warn and Continue to retain endpoint-bin participation, or explicitly exclude it with Use Mask Array."); } if(summary.FiniteCount != 0) { @@ -517,7 +517,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp if(result.FiniteCount == 0) { return ConvertResultTo( - MergeResults(std::move(rangeResult), MakeErrorResult(-8325, "Otsu has no selected finite samples. Select at least one finite input value with the optional mask before thresholding.")), + MergeResults(std::move(rangeResult), MakeErrorResult(-28655, "Otsu has no selected finite samples. Select at least one finite input value with the optional mask before thresholding.")), std::move(result)); } auto& histogram = result.HistogramData; From 682b4b78891be68f9fb02b1352629d87fa9892aa Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 10:05:36 -0500 Subject: [PATCH 32/98] PERF: Skip unused mask reads in integral color range scans Read a range-scan mask only for floating nonfinite classification. Use the selected mask for both writers' slice reads. The regression checks the mask transfer counts and decoded pixels in both algorithms. Paired Release and targeted Debug checks passed. Signed-off-by: Jessica Marquis --- .../docs/WriteImageFilter.md | 2 +- .../Algorithms/WriteImageColorUtilities.hpp | 7 ++- .../NXImageProcessing/test/WriteImageTest.cpp | 58 ++++++++++++++++--- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md index 0ad15a8d36..84992545c5 100644 --- a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md @@ -66,7 +66,7 @@ Enabling *Create Color Table* converts a single-component **Data Array** to an R - *Select Preset...* chooses the color preset (for example grayscale, rainbow, or jet) that is applied to the array's values. - When *Create Color Table* is enabled, the **Input Image Data Array** must be a single-component numeric array, but it may be **any** numeric type: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, or float64. This is wider than the type restrictions that apply when writing an array's raw pixel values directly (see *Pixel Data Type Support By Format* above), since color-table mode always produces a uint8 RGB image regardless of the input array's type. If the selected array has more than one component, the filter will fail during preflight. -- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar and optional mask pages. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. +- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar pages. For floating-point input, it also reads the selected mask to check nonfinite values. Integer and Boolean inputs use the mask when rendering each slice. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. - If the finite range is constant, each unmasked voxel uses the preset's first control color. Masked voxels use the mask color. #### Nonfinite Input Handling diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 8ca397e343..5dd922d5c4 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -65,7 +65,7 @@ struct Range }; /** - * @brief Scans scalar and optional mask pages before any slice is opened. + * @brief Scans scalar pages before any slice is opened. For floating-point input, reads the optional mask to check nonfinite values. * Finite masked values participate in the range. Masked nonfinite values do not * trigger the policy, and no nonfinite value participates in the range. * @tparam T Scalar input type. @@ -86,14 +86,15 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, return MakeErrorResult>(-27020, "Input array is empty."); } constexpr usize targetPageBytes = 1024 * 1024; - const usize maskBytes = maskArray == nullptr ? 0 : (maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8)); + const bool scanMask = std::is_floating_point_v && maskArray != nullptr; + const usize maskBytes = scanMask ? (maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8)) : 0; const usize pageCount = std::min(count, std::max(1, targetPageBytes / (sizeof(T) + maskBytes))); // Scalar input can use bool. The store needs a contiguous bool buffer, which std::vector cannot provide. auto values = std::make_unique(pageCount); // The Boolean mask store also needs a contiguous bool buffer. std::unique_ptr boolMask; std::unique_ptr> byteMask; - if(maskArray != nullptr) + if(scanMask) { if(maskArray->getDataType() == DataType::boolean) { diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 9f93b7988b..66ce9898ad 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -1,7 +1,7 @@ #include "C15CompatibilityTestUtils.hpp" -#include "ImageProcessing/Filters/AcosImageFilter.hpp" -#include "ImageProcessing/Filters/AsinImageFilter.hpp" -#include "ImageProcessing/Filters/TanImageFilter.hpp" +#include "NXImageProcessing/Filters/AcosImageFilter.hpp" +#include "NXImageProcessing/Filters/AsinImageFilter.hpp" +#include "NXImageProcessing/Filters/TanImageFilter.hpp" #include "simplnx/Parameters/CreateColorMapParameter.hpp" #include #include @@ -499,11 +499,11 @@ std::vector readWriteImageSentinel(const fs::path& path) } template -class WriteImageReadProbeStore : public DataStore +class WriteImageReadProbeStore : public ExtentCountingDataStore { public: WriteImageReadProbeStore(const ShapeType& shape, bool failFlat, bool failExtent) - : DataStore(shape, ShapeType{1}, T{}) + : ExtentCountingDataStore(shape, ShapeType{1}, T{}) , m_FailFlat(failFlat) , m_FailExtent(failExtent) { @@ -534,7 +534,7 @@ class WriteImageReadProbeStore : public DataStore { return MakeErrorResult>(-27902, "Controlled Write Image slice read failure."); } - return DataStore::readExtent(extent); + return ExtentCountingDataStore::readExtent(extent); } Result<> readExtentIntoBuffer(const Extent& extent, nonstd::span values) const override { @@ -542,7 +542,7 @@ class WriteImageReadProbeStore : public DataStore { return MakeErrorResult(-27902, "Controlled Write Image slice read failure."); } - return DataStore::readExtentIntoBuffer(extent, values); + return ExtentCountingDataStore::readExtentIntoBuffer(extent, values); } usize flatReadCount() const { @@ -631,6 +631,48 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite UnitTest::CheckArraysInheritTupleDims(dataStructure); } +TEST_CASE("NXImageProcessing::WriteImageFilter: integer color reads masks only for slices", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + UnitTest::LoadPlugins(); + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const ShapeType shape{2, 1, 2}; + DataStructure dataStructure; + const DataPath cellsPath = createWriteImageNumericInput(dataStructure, std::array{0, 3, 6, 9}, shape).getParent(); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(cellsPath)); + const auto& cells = dataStructure.getDataRefAs(cellsPath); + auto maskStore = std::make_shared>(shape, false, false); + maskStore->setValue(0, 1); + maskStore->setValue(1, 1); + maskStore->setValue(2, 1); + REQUIRE(UInt8Array::Create(dataStructure, "Mask", maskStore, cells.getId()) != nullptr); + + const ScopedTempDir output(k_BinaryTestOutputDir); + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "integer.png", 0, "Grayscale", true)); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + // The masked 9 still sets the maximum, so 3 and 6 map to one and two thirds of the grayscale range. + const std::vector firstSlice{0, 0, 0, 85, 85, 85}; + const std::vector secondSlice{170, 170, 170, 10, 20, 30}; + REQUIRE(readWriteImageNumericPng(output.path() / "integer_000.png", 2, 1) == firstSlice); + REQUIRE(readWriteImageNumericPng(output.path() / "integer_001.png", 2, 1) == secondSlice); + if(scenario == UnitTest::AlgorithmTestScenario::InCoreAlgorithmOnInMemoryStore) + { + REQUIRE(maskStore->returnedExtentReadCount() == 2); + REQUIRE(maskStore->callerProvidedExtentReadCount() == 0); + } + else + { + REQUIRE(maskStore->returnedExtentReadCount() == 0); + REQUIRE(maskStore->callerProvidedExtentReadCount() == 2); + REQUIRE(maskStore->maxCallerProvidedExtentValues() == 2); + } + REQUIRE(maskStore->flatReadCount() == 0); + REQUIRE(result.result.warnings().empty()); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the first actual control color", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -2795,7 +2837,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: rejects a source-group grant sma } } -TEST_CASE("ImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[ImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[NXImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); From a3a6aebec4e2a3301cc913538d81608f8913b397 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 10:05:36 -0500 Subject: [PATCH 33/98] BUG: Distinguish threshold transfer and fallback errors Give output and outer failures unique diagnostic IDs. Keep scan IDs, messages, partial-output behavior and collected warnings. Exercise the outer catches through the existing late info callback. Paired Release and targeted Debug checks passed. Signed-off-by: Jessica Marquis --- ...imumConnectedComponentsImageFilterTest.cpp | 46 +++++++++++++++++-- ...esholdMaximumConnectedComponentsEngine.hpp | 8 ++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index ccbea7737c..b3ea6bb941 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -681,13 +681,22 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: su { input.warnOnRead(); input.setReadFault(2, stage == 5 ? ThresholdFaultType::BadAllocation : ThresholdFaultType::LengthError); - expectedCode = stage == 5 ? -28617 : -28618; + expectedCode = stage == 5 ? -28634 : -28635; } const std::atomic_bool shouldCancel{false}; const ImageProcessing::ThresholdMaxCCOptions options{.WarnOnNonfinite = true}; const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == expectedCode); + CAPTURE(stage); + CHECK(result.errors().front().code == expectedCode); + if(stage == 1 || stage == 2) + { + REQUIRE(result.errors().front().message.find("scan buffer") != std::string::npos); + } + else if(stage >= 5) + { + REQUIRE(result.errors().front().message.find("output buffer") != std::string::npos); + } REQUIRE(result.warnings().size() == (stage == 3 || stage >= 5 ? 2 : (stage == 4 ? 1 : 0))); if(stage == 3 || stage >= 5) { @@ -697,6 +706,36 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: su REQUIRE(output.getValue(0) == 99); } +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: outer exceptions preserve collected warnings before output", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + UnitTest::LoadPlugins(); + const bool allocationFailure = GENERATE(false, true); + const ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); + ThresholdTransferStore output({99, 99, 99}); + const std::atomic_bool shouldCancel{false}; + const ImageProcessing::ThresholdMaxCCOptions options{.WarnOnNonfinite = true}; + // The engine sends this message after collecting the nonfinite warning and before writing any output. + const IFilter::MessageHandler handler{[allocationFailure](const IFilter::Message& message) { + if(message.type == IFilter::Message::Type::Info && message.message == "Applying the selected native threshold in bounded batches") + { + if(allocationFailure) + { + throw std::bad_alloc{}; + } + throw std::length_error("Injected Threshold addressability failure"); + } + }}; + const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{3, 1, 1}, options, shouldCancel, handler); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.warnings().size() == 1); + REQUIRE(result.warnings().front().code == -28614); + REQUIRE(input.reads() == 1); + REQUIRE(output.writes() == 0); + REQUIRE(output.getValue(0) == 99); + REQUIRE(result.errors().front().code == (allocationFailure ? -28636 : -28637)); +} + TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine cancellation keeps warning Results without inventing a local cancellation error", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -938,7 +977,8 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co // can select fewer components after the arithmetic fix; this regression is not an ITK-parity or optimum claim. } -TEST_CASE("ImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", "[ImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", + "[NXImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index a925007cf1..dd8f33ec34 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -428,10 +428,10 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor return status; } catch(const std::bad_alloc&) { - return MergeResults(std::move(status), MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its output buffers. Reduce the data size or provide more memory.")); + return MergeResults(std::move(status), MakeErrorResult(-28634, "Threshold Maximum Connected Components could not allocate its output buffers. Reduce the data size or provide more memory.")); } catch(const std::length_error&) { - return MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable output buffer size. Reduce the image dimensions.")); + return MergeResults(std::move(status), MakeErrorResult(-28635, "Threshold Maximum Connected Components exceeded an addressable output buffer size. Reduce the image dimensions.")); } } } // namespace detail @@ -528,10 +528,10 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in } catch(const std::bad_alloc&) { return MergeResults(std::move(status), - MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its scan or output buffers. Reduce the data size or provide more memory.")); + MakeErrorResult(-28636, "Threshold Maximum Connected Components could not allocate its scan or output buffers. Reduce the data size or provide more memory.")); } catch(const std::length_error&) { - return MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable buffer size. Reduce the image dimensions.")); + return MergeResults(std::move(status), MakeErrorResult(-28637, "Threshold Maximum Connected Components exceeded an addressable buffer size. Reduce the image dimensions.")); } } } // namespace nx::core::ImageProcessing From f397c6ce0d003c9c491f19c9cdd4c57540d275d6 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 10:18:21 -0500 Subject: [PATCH 34/98] BUG: Scale finite color ranges at the rounded overflow bound A rounded-up limit can equal the high endpoint even when the unscaled width rounds to infinity. Use the existing scaled ratio at equality and assert the decoded midpoint in both writers. Reproduced the two decoded failures and passed paired Release and targeted Debug checks. Signed-off-by: Jessica Marquis --- .../Filters/Algorithms/WriteImageColorUtilities.hpp | 3 ++- src/Plugins/NXImageProcessing/test/WriteImageTest.cpp | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 5dd922d5c4..144218d73c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -242,7 +242,8 @@ float32 Normalize(T value, T minimum, T maximum) const float64 low = static_cast(minimum); const float64 high = static_cast(maximum); const float64 current = static_cast(value); - if(low < 0 && high > 0 && high > std::numeric_limits::max() + low) + // The bound can round up, so scale at equality to avoid an overflowing width. + if(low < 0 && high > 0 && high >= std::numeric_limits::max() + low) { fraction = (current * 0.5 - low * 0.5) / (high * 0.5 - low * 0.5); } diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 66ce9898ad..744890c6ea 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -586,6 +586,11 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: wide and tiny interiors decode w requireWriteImageGray(scope, std::array{unsignedMaximum - 2, unsignedMaximum - 1, unsignedMaximum}, expected); requireWriteImageGray(scope, std::array{0, std::numeric_limits::min(), 2 * std::numeric_limits::min()}, expected); requireWriteImageGray(scope, std::array{0, 1, 2, 3}, std::array{0, 85, 170, 255}); + + // The unscaled width rounds to infinity at the overflow midpoint. + INFO("Binary64 width at the overflow midpoint"); + constexpr float64 high = 0x1.ffffffffffffep+1023; + requireWriteImageGray(scope, std::array{-0x1.8p+971, high * 0.5, high}, expected); } TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a distinct runtime witness", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") From 612454a0fc7581313b3ec91358b07c0655956496 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 10:29:51 -0500 Subject: [PATCH 35/98] TEST: Name the promoted Log10 output accurately Describe the Float64 output that the test already asserts. The renamed case passes with 26 assertions in both Release and Debug on the in-core and OOC configurations. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp index 33fcec23bf..71600c1ca2 100644 --- a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp @@ -220,7 +220,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating bound UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("NXImageProcessing::Log10ImageFilter: integer output preserves tuple and component shapes", "[NXImageProcessing][Issue1874][Log10ImageFilter]") +TEST_CASE("NXImageProcessing::Log10ImageFilter: promoted Float64 output preserves tuple and component shapes", "[NXImageProcessing][Issue1874][Log10ImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); From 71cedb4e7b1d1e7a7de49b04bbfdc5538f941931 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 11:04:56 -0500 Subject: [PATCH 36/98] REV: Address clang-tidy findings in C15 image processing Signed-off-by: Jessica Marquis --- .../test/ExpNegativeImageFilterTest.cpp | 68 ++++++++++--------- ...imumConnectedComponentsImageFilterTest.cpp | 46 ++++++------- .../NXImageProcessing/test/WriteImageTest.cpp | 67 +++++++++++------- .../ConnectedComponentEngine.hpp | 2 +- .../FiniteDifferenceEngine.hpp | 52 ++++++++------ .../ImageProcessing/OtsuImageEngine.hpp | 9 ++- .../RecursiveGaussianEngine.hpp | 5 +- 7 files changed, 140 insertions(+), 109 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index f35b3e4ef6..f41da50999 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -18,6 +18,7 @@ #include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/DataStructure/IDataStore.hpp" #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Filter/Arguments.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" @@ -28,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -39,12 +41,16 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input pro { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); - std::vector values = {0, 1, std::numeric_limits::max()}; - if constexpr(std::is_signed_v) - { - values.push_back(-1); - values.push_back(std::numeric_limits::lowest()); - } + const auto values = [] { + if constexpr(std::is_signed_v) + { + return std::vector{0, 1, std::numeric_limits::max(), -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{0, 1, std::numeric_limits::max()}; + } + }(); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); const DataPath output = input.replaceName("Output"); @@ -350,7 +356,7 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}, usize{5}, usize{6}); CAPTURE(variant); - DataStructure ds; + DataStructure dataStructure; DataPath input; DataPath quantitative; std::vector expected; @@ -363,34 +369,34 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas switch(variant) { case 0: - input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {1, 0.36787944117144232160, 0.13533528323661269189}; lower = 0.5; upper = 1; selected = {1, 0, 0}; components = {1, 0, 0}; - REQUIRE(c15_test::Read(ds, input, {1, 1, 3}, expectedStore) == std::vector{0, 1, 2}); + REQUIRE(c15_test::Read(dataStructure, input, {1, 1, 3}, expectedStore) == std::vector{0, 1, 2}); break; case 1: - input = ip_test::BuildScalarValuesImage(ds, {0, 1}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); - expected = {1, 2.71828182845904523536}; + input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + expected = {1, std::numbers::e}; lower = 1; selected = {1, 0}; components = {1, 0}; - REQUIRE(c15_test::Read(ds, input, {1, 1, 2}, expectedStore) == std::vector{0, 1}); + REQUIRE(c15_test::Read(dataStructure, input, {1, 1, 2}, expectedStore) == std::vector{0, 1}); break; case 2: - input = ip_test::BuildScalarValuesImage(ds, {0, 1000, nan, -inf, inf}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1000, nan, -inf, inf}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {1, inf, nan, 0, inf}; selected = {1, 0, 0, 1, 0}; components = {1, 0, 0, 2, 0}; break; case 3: - input = ip_test::BuildScalarValuesImage(ds, {1, 0, -1, inf, nan}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {1, 0, -1, inf, nan}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {0, -inf, nan, inf, nan}; lower = -1; upper = 1; @@ -398,23 +404,23 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas components = {1, 0, 0, 0, 0}; break; case 4: - input = ip_test::BuildScalarValuesImage(ds, {1, 10, 0, -1}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {1, 10, 0, -1}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {0, 1, -inf, nan}; upper = 1; selected = {1, 1, 0, 0}; components = {1, 1, 0, 0}; break; case 5: - input = ip_test::BuildScalarValuesImage(ds, {0, 4, -1, inf, nan}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {0, 4, -1, inf, nan}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {0, 2, nan, inf, nan}; selected = {1, 1, 0, 0, 0}; components = {1, 1, 0, 0, 0}; break; default: - input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2}); - quantitative = c15_test::Pointwise(ds, input, "Quantitative"); + input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2}); + quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); expected = {1, 0.36787944117144232160, 0.13533528323661269189}; lower = 0.5; upper = 1; @@ -424,7 +430,7 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas } const ShapeType shape{1, 1, expected.size()}; auto checkQuantitative = [&] { - const auto values = c15_test::Read(ds, quantitative, shape, expectedStore); + const auto values = c15_test::Read(dataStructure, quantitative, shape, expectedStore); for(usize index = 0; index < expected.size(); ++index) { CAPTURE(index); @@ -444,13 +450,13 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas }; if(variant >= 2) { - REQUIRE(c15_test::Read(ds, input, shape, expectedStore).size() == expected.size()); + REQUIRE(c15_test::Read(dataStructure, input, shape, expectedStore).size() == expected.size()); } checkQuantitative(); - const auto mask = c15_test::Select(ds, quantitative, lower, upper, "Selected"); - REQUIRE(c15_test::Read(ds, mask, shape, expectedStore) == selected); - const auto labels = c15_test::Components(ds, mask, "Components"); - REQUIRE(c15_test::Read(ds, labels, shape, expectedStore) == components); + const auto mask = c15_test::Select(dataStructure, quantitative, lower, upper, "Selected"); + REQUIRE(c15_test::Read(dataStructure, mask, shape, expectedStore) == selected); + const auto labels = c15_test::Components(dataStructure, mask, "Components"); + REQUIRE(c15_test::Read(dataStructure, labels, shape, expectedStore) == components); checkQuantitative(); - UnitTest::CheckArraysInheritTupleDims(ds); + UnitTest::CheckArraysInheritTupleDims(dataStructure); } \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index b3ea6bb941..ada487271b 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -2,6 +2,7 @@ #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" +#include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp" @@ -30,6 +31,7 @@ #include #include +#include #include #include #include @@ -933,14 +935,8 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co { const float32 value = inputPtr->getValue(index); allFinite = allFinite && std::isfinite(value); - if(value < minimum) - { - minimum = value; - } - if(value > maximum) - { - maximum = value; - } + minimum = std::min(minimum, value); + maximum = std::max(maximum, value); const bool legacyInside = value >= legacyThreshold && value <= upperBoundary; const bool correctedInside = value >= correctedThreshold && value <= upperBoundary; legacyMask[index] = legacyInside ? uint8{1} : uint8{0}; @@ -985,21 +981,23 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selec const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); const auto inf = std::numeric_limits::infinity(); - DataStructure ds; + DataStructure dataStructure; const bool noFinite = variant >= 3; const uint8 outside = variant == 4 ? 7 : 0; - const auto input = noFinite ? ip_test::BuildScalarValuesImage(ds, {std::numeric_limits::quiet_NaN(), -inf, inf}) : ip_test::BuildScalarValuesImage(ds, {100, 102}); + const auto input = + noFinite ? ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), -inf, inf}) : ip_test::BuildScalarValuesImage(dataStructure, {100, 102}); const ShapeType shape{1, 1, noFinite ? usize{3} : usize{2}}; const ThresholdMaximumConnectedComponentsImageFilter filter; auto args = c15_test::ImageArguments(filter, input, "Thresholded"); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(0)); - args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(variant == 1 ? 99 : (variant == 2 ? -inf : 65536))); + const std::array upperBounds{65536, 99, -inf, 65536, 65536}; + args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(upperBounds.at(variant))); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_InsideValue_Key, std::make_any(1)); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_OutsideValue_Key, std::make_any(outside)); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(noFinite ? 1 : 0)); - const auto preflight = filter.preflight(ds, args); + const auto preflight = filter.preflight(dataStructure, args); SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); - const auto result = filter.execute(ds, args); + const auto result = filter.execute(dataStructure, args); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == (variant == 0 ? 0 : 1)); if(noFinite) @@ -1015,30 +1013,30 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selec } const auto thresholded = input.replaceName("Thresholded"); const std::vector expected = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, outside); - REQUIRE(c15_test::Read(ds, thresholded, shape, expectedStore) == expected); + REQUIRE(c15_test::Read(dataStructure, thresholded, shape, expectedStore) == expected); if(outside == 7) { // CC treats all nonzero values as foreground, including the documented expert Outside Value. - const auto direct = c15_test::Components(ds, thresholded, "DirectComponents"); - REQUIRE(c15_test::Read(ds, direct, shape, expectedStore) == std::vector{1, 1, 1}); + const auto direct = c15_test::Components(dataStructure, thresholded, "DirectComponents"); + REQUIRE(c15_test::Read(dataStructure, direct, shape, expectedStore) == std::vector{1, 1, 1}); } - const auto desired = c15_test::Select(ds, thresholded, 1, 1, "DesiredInside"); + const auto desired = c15_test::Select(dataStructure, thresholded, 1, 1, "DesiredInside"); const std::vector expectedDesired = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); - REQUIRE(c15_test::Read(ds, desired, shape, expectedStore) == expectedDesired); - const auto labels = c15_test::Components(ds, desired, "Components"); + REQUIRE(c15_test::Read(dataStructure, desired, shape, expectedStore) == expectedDesired); + const auto labels = c15_test::Components(dataStructure, desired, "Components"); const std::vector expectedLabels = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); - REQUIRE(c15_test::Read(ds, labels, shape, expectedStore) == expectedLabels); - REQUIRE(c15_test::Read(ds, thresholded, shape, expectedStore) == expected); + REQUIRE(c15_test::Read(dataStructure, labels, shape, expectedStore) == expectedLabels); + REQUIRE(c15_test::Read(dataStructure, thresholded, shape, expectedStore) == expected); if(noFinite) { - const auto sourceValues = c15_test::Read(ds, input, shape, expectedStore); + const auto sourceValues = c15_test::Read(dataStructure, input, shape, expectedStore); REQUIRE(std::isnan(sourceValues[0])); REQUIRE(sourceValues[1] == -inf); REQUIRE(sourceValues[2] == inf); } else { - REQUIRE(c15_test::Read(ds, input, shape, expectedStore) == std::vector{100, 102}); + REQUIRE(c15_test::Read(dataStructure, input, shape, expectedStore) == std::vector{100, 102}); } - UnitTest::CheckArraysInheritTupleDims(ds); + UnitTest::CheckArraysInheritTupleDims(dataStructure); } \ No newline at end of file diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 744890c6ea..1f38b2cbfd 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/Filters/AcosImageFilter.hpp" #include "NXImageProcessing/Filters/AsinImageFilter.hpp" #include "NXImageProcessing/Filters/TanImageFilter.hpp" -#include "simplnx/Parameters/CreateColorMapParameter.hpp" +#include "ReconstructionFilterTestUtils.hpp" #include #include @@ -42,6 +42,7 @@ #include "simplnx/Utilities/ImageIO/IImageIO.hpp" #include "simplnx/Utilities/ImageIO/ImageIOFactory.hpp" #include "simplnx/Utilities/ImageProcessing/WorkingMemory.hpp" +#include "simplnx/Utilities/Parsing/DREAM3D/Dream3dIO.hpp" #include #include @@ -55,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -2850,29 +2852,46 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan const usize operation = GENERATE(usize{0}, usize{1}, usize{2}); CAPTURE(operation, scenario); const c15_test::Directory output; - const std::vector original = operation == 0 ? std::vector{1, 0, -1, -1, 0, 1} : (operation == 1 ? std::vector{-1, 0, 1, 1, 0, -1} : std::vector{0, 1, -1, -1, 1, 0}); - const std::vector expected = operation == 0 ? - std::vector{0, 1.57079632679489661923F, 3.14159265358979323846F, 3.14159265358979323846F, 1.57079632679489661923F, 0} : - (operation == 1 ? std::vector{-1.57079632679489661923F, 0, 1.57079632679489661923F, 1.57079632679489661923F, 0, -1.57079632679489661923F} : - std::vector{0, 1.5574077246549022305F, -1.5574077246549022305F, -1.5574077246549022305F, 1.5574077246549022305F, 0}); + using InputSamplesType = std::array; + using OutputSamplesType = std::array; + constexpr auto piRadians = std::numbers::pi_v; + constexpr auto halfPi = piRadians / 2.0F; + constexpr float32 tangent = 1.5574077246549022305F; + constexpr std::array inputSamples{InputSamplesType{1, 0, -1, -1, 0, 1}, InputSamplesType{-1, 0, 1, 1, 0, -1}, InputSamplesType{0, 1, -1, -1, 1, 0}}; + constexpr std::array outputSamples{OutputSamplesType{0, halfPi, piRadians, piRadians, halfPi, 0}, OutputSamplesType{-halfPi, 0, halfPi, halfPi, 0, -halfPi}, + OutputSamplesType{0, tangent, -tangent, -tangent, tangent, 0}}; + const auto& inputValues = inputSamples.at(operation); + const std::vector original(inputValues.begin(), inputValues.end()); + const auto& expected = outputSamples.at(operation); DataStructure source; const auto input = recon_test::BuildImageFromPattern(source, 3, 1, 2, original); c15_test::SetGeometry(source); REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); const std::string outputName = operation == 2 ? "Tangent" : "Radians"; - const auto quantitative = operation == 0 ? c15_test::Pointwise(source, input, outputName) : - (operation == 1 ? c15_test::Pointwise(source, input, outputName) : c15_test::Pointwise(source, input, outputName)); + DataPath quantitative; + switch(operation) + { + case 0: + quantitative = c15_test::Pointwise(source, input, outputName); + break; + case 1: + quantitative = c15_test::Pointwise(source, input, outputName); + break; + default: + quantitative = c15_test::Pointwise(source, input, outputName); + break; + } const auto values = c15_test::Read(source, quantitative, {2, 1, 3}, scope); for(usize index = 0; index < expected.size(); ++index) { CAPTURE(index); - if(expected[index] == 0) + if(expected.at(index) == 0) { REQUIRE(values[index] == 0); } else { - REQUIRE(values[index] == Approx(expected[index]).epsilon(1.0e-6)); + REQUIRE(values[index] == Approx(expected.at(index)).epsilon(1.0e-6)); } } // Inverse-trigonometric outputs are radians; tangent values are dimensionless. @@ -2897,32 +2916,32 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan } for(usize slice = 0; slice < 2; ++slice) { - const auto suffix = slice == 0 ? "_000" : "_001"; + const std::string suffix = slice == 0 ? "_000" : "_001"; auto raw = c15_test::ReadMha(output.path() / (std::string("raw") + suffix + ".mha")); const auto pixels = c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 3}, scope); - const ImageGeom* rawGeometry = nullptr; - REQUIRE_NOTHROW(rawGeometry = &raw.getDataRefAs(DataPath({"Image Geometry"}))); - REQUIRE(rawGeometry->getDimensions() == SizeVec3{3, 1, 1}); - REQUIRE(rawGeometry->getSpacing() == FloatVec3{1, 2, 1}); - REQUIRE(rawGeometry->getOrigin() == FloatVec3{4, 5, 0}); - for(usize x = 0; x < 3; ++x) + const ImageGeom* rawGeometryPtr = nullptr; + REQUIRE_NOTHROW(rawGeometryPtr = &raw.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(rawGeometryPtr->getDimensions() == SizeVec3{3, 1, 1}); + REQUIRE(rawGeometryPtr->getSpacing() == FloatVec3{1, 2, 1}); + REQUIRE(rawGeometryPtr->getOrigin() == FloatVec3{4, 5, 0}); + for(usize column = 0; column < 3; ++column) { - REQUIRE(pixels[x] == values[slice * 3 + x]); - REQUIRE(pixels[x] == Approx(expected[slice * 3 + x]).margin(1.0e-6)); + REQUIRE(pixels[column] == values[(slice * 3) + column]); + REQUIRE(pixels[column] == Approx(expected.at((slice * 3) + column)).margin(1.0e-6)); } if(operation == 0) { const auto tiffPath = output.path() / (std::string("raw") + suffix + ".tif"); const auto tiff = c15_test::Decode(tiffPath, 3, 1); REQUIRE(tiff == pixels); - const auto io = CreateImageIO(tiffPath); - SIMPLNX_RESULT_REQUIRE_VALID(io); - const auto metadata = io.value()->readMetadata(tiffPath); + const auto imageIO = CreateImageIO(tiffPath); + SIMPLNX_RESULT_REQUIRE_VALID(imageIO); + const auto metadata = imageIO.value()->readMetadata(tiffPath); SIMPLNX_RESULT_REQUIRE_VALID(metadata); REQUIRE(metadata.value().spacing.has_value()); REQUIRE(metadata.value().origin.has_value()); - REQUIRE(metadata.value().spacing.value() == FloatVec3{1, 2, 1}); - REQUIRE(metadata.value().origin.value() == FloatVec3{4, 5, 0}); + REQUIRE(metadata.value().spacing == FloatVec3{1, 2, 1}); + REQUIRE(metadata.value().origin == FloatVec3{4, 5, 0}); const auto display = c15_test::Decode(output.path() / (std::string("display") + suffix + ".png"), 3, 1, 3); const std::vector expectedDisplay = slice == 0 ? std::vector{0, 0, 0, 127, 127, 127, 255, 255, 255} : std::vector{255, 255, 255, 127, 127, 127, 0, 0, 0}; REQUIRE(display == expectedDisplay); diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index a30ef4d83b..d7c9c685d1 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -1174,7 +1174,7 @@ Result<> Pass1TwoDimensionalBlocks(const AbstractDataStore& inStore, Connecte for(usize localRow = 0; localRow < rowCount; ++localRow) { const usize rowOffset = localRow * dimX; - if(auto result = EncodeLineInto(nonstd::span(inputBlock.data() + rowOffset, dimX), signedDimX, pred, uf, currentLine); result.invalid()) + if(auto result = EncodeLineInto(nonstd::span(inputBlock.data(), blockValues).subspan(rowOffset, dimX), signedDimX, pred, uf, currentLine); result.invalid()) { return result; } diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 33672a2f22..0f59e35a65 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -1391,7 +1391,8 @@ Result<> ReadFiniteDifference2DRegion(const StoreT& store, usize nx, usize xBegi Result<> accumulated; for(usize row = 0; row < rows; ++row) { - if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyIntoBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width))); result.invalid()) + const usize imageOffset = ((yBegin + row) * nx) + xBegin; + if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyIntoBuffer(imageOffset, buffer.subspan(row * width, width))); result.invalid()) { if constexpr(CheckedOutput) { @@ -1416,7 +1417,8 @@ Result<> WriteFiniteDifference2DRegion(StoreT& store, usize nx, usize xBegin, us Result<> accumulated; for(usize row = 0; row < rows; ++row) { - if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyFromBuffer((yBegin + row) * nx + xBegin, buffer.subspan(row * width, width))); result.invalid()) + const usize imageOffset = ((yBegin + row) * nx) + xBegin; + if(Result<> result = detail::RecordFiniteDifferenceIoResult(accumulated, store.copyFromBuffer(imageOffset, buffer.subspan(row * width, width))); result.invalid()) { if constexpr(CheckedOutput) { @@ -2280,18 +2282,20 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { return finish(); } - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); rr.invalid()) + if(Result<> transferResult = detail::RecordFiniteDifferenceIoResult(ioResult, inStore.copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } for(usize i = 0; i < slice; ++i) { planeOut[i] = static_cast(planeIn[i]); } - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, accum.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); - rr.invalid()) + if(Result<> transferResult = + detail::RecordFiniteDifferenceIoResult(ioResult, accum.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } } } @@ -2450,9 +2454,10 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData // (aniso) global reduction hook -- no-op unless F::k_NeedsGlobalGradient (Task 4 specializes this). if constexpr(F::k_NeedsGlobalGradient) { - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, effDim, iter, shouldCancel)); rr.invalid()) + if(Result<> transferResult = detail::RecordFiniteDifferenceIoResult(ioResult, detail::UpdateGlobalConductance(fn, *currentStore, dims, sc, effDim, iter, shouldCancel)); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } } @@ -2460,9 +2465,9 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData // seed the window at z = -r .. +r for(int64 k = 0; k < static_cast(windowPlanes); ++k) { - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[static_cast(k)], (k - r))); rr.invalid()) + if(Result<> transferResult = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[static_cast(k)], (k - r))); transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } } progressThrottle.reset(static_cast(nZ), fmt::format("Finite Difference: Updating Slices for Iteration {} of {}", iter + 1, numberOfIterations)); @@ -2518,10 +2523,11 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData ParallelDataAlgorithm parallelAlgorithm; parallelAlgorithm.setRange(0, static_cast(nY)); parallelAlgorithm.execute(calculateChangeRows); - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, nextStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice))); - rr.invalid()) + if(Result<> transferResult = + detail::RecordFiniteDifferenceIoResult(ioResult, nextStore->copyFromBuffer(static_cast(z) * slice, nonstd::span(nextPlane.data(), slice))); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } progressThrottle.updateCount(static_cast(z + 1)); // advance the rolling window by one z (drop the oldest plane, load clamp(z+1+r)) @@ -2531,9 +2537,9 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { std::swap(win[k], win[k + 1]); } - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[windowPlanes - 1], z + 1 + r)); rr.invalid()) + if(Result<> transferResult = detail::RecordFiniteDifferenceIoResult(ioResult, loadPlane(win[windowPlanes - 1], z + 1 + r)); transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } } } @@ -2554,20 +2560,22 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { return finish(); } - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, currentStore->copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); - rr.invalid()) + if(Result<> transferResult = + detail::RecordFiniteDifferenceIoResult(ioResult, currentStore->copyIntoBuffer(static_cast(z) * slice, nonstd::span(planeIn.data(), slice))); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } if(!detail::ConvertFiniteDifferenceOutputBuffer(nonstd::span(planeIn.data(), slice), nonstd::span(planeOut.data(), slice), static_cast(z) * slice, conversionState, shouldCancel)) { return finish(); } - if(Result<> rr = detail::RecordFiniteDifferenceIoResult(ioResult, outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); - rr.invalid()) + if(Result<> transferResult = + detail::RecordFiniteDifferenceIoResult(ioResult, outStore.copyFromBuffer(static_cast(z) * slice, nonstd::span(planeOut.data(), slice))); + transferResult.invalid()) { - return finish(std::move(rr)); + return finish(std::move(transferResult)); } } } diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 298836cc90..c0df789ff3 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -392,8 +392,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp const bool external = input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore); const usize total = input.getSize(); const usize bytesPerValue = sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)); - constexpr bool k_ParallelRange = std::is_floating_point_v; - auto plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external, k_ParallelRange ? sizeof(detail::OtsuFloatingRangeSummary) : 0); + auto plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external, std::is_floating_point_v ? sizeof(detail::OtsuFloatingRangeSummary) : 0); constexpr usize ringSize = 65536; CacheMemoryBudgetManager::WorkingMemoryReservation ringReservation; std::unique_ptr>> frequenciesPtr; @@ -501,8 +500,8 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp return {}; } }, - k_ParallelRange); - if(k_ParallelRange || compactRange || minValue == maxValue) + std::is_floating_point_v); + if(std::is_floating_point_v || compactRange || minValue == maxValue) { plan.Reservation = {}; } @@ -557,7 +556,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } else { - if constexpr(k_ParallelRange) + if constexpr(std::is_floating_point_v) { // The floating-point histogram pass needs no range summaries. plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external); diff --git a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp index a89267401d..3956ef9ddf 100644 --- a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp @@ -2118,8 +2118,9 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S return accumulated; } const usize blockOffset = (z * dims[1] + yBegin) * dims[0]; - accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), - dst.copyFromBuffer(blockOffset, nonstd::span(dstSlab.data() + (z * valuesPerPlaneBlock), valuesPerPlaneBlock))); + accumulated = + detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), + dst.copyFromBuffer(blockOffset, nonstd::span(dstSlab.data(), dstSlab.size()).subspan(z * valuesPerPlaneBlock, valuesPerPlaneBlock))); if(accumulated.invalid()) { return accumulated; From e3370f3228844d35b3cce94c91af7a52d8ae7f3a Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 3 Nov 2026 12:05:03 -0500 Subject: [PATCH 37/98] REV: Resolve C15 clang-tidy diagnostics Use named arguments and bounded views in numeric helpers. Keep the existing overwrite allocations. Limit test macro annotations to the two checks triggered by assertion expansion. Signed-off-by: Jessica Marquis --- .../Filters/AcosImageFilter.cpp | 2 + .../Algorithms/WriteImageColorUtilities.hpp | 20 +- .../Filters/Algorithms/WriteImageDirect.cpp | 37 +-- .../Filters/Algorithms/WriteImageScanline.cpp | 45 ++-- .../Filters/AsinImageFilter.cpp | 2 + .../Filters/ConnectedComponentImageFilter.hpp | 1 + .../Filters/ExpImageFilter.cpp | 2 + .../Filters/ExpNegativeImageFilter.cpp | 2 + .../Filters/Log10ImageFilter.cpp | 2 + .../Filters/LogImageFilter.cpp | 2 + ...logicalWatershedFromMarkersImageFilter.hpp | 1 + .../OtsuMultipleThresholdsImageFilter.cpp | 214 ++++++++++++++++++ .../OtsuMultipleThresholdsImageFilter.hpp | 2 + .../Filters/SqrtImageFilter.cpp | 2 + .../Filters/SquareImageFilter.cpp | 3 +- .../Filters/TanImageFilter.cpp | 2 + ...dMaximumConnectedComponentsImageFilter.hpp | 1 + .../Filters/WriteImageFilter.hpp | 1 + .../utils/ImageFilterExecutorDetail.hpp | 71 +++--- .../test/AcosImageFilterTest.cpp | 33 ++- .../test/AsinImageFilterTest.cpp | 33 ++- .../test/C15CompatibilityTestUtils.hpp | 65 +++--- .../test/ConnectedComponentEngineTest.cpp | 32 ++- .../ConnectedComponentImageFilterTest.cpp | 8 + .../test/CurvatureFlowImageFilterTest.cpp | 128 +++++++---- .../test/ExpImageFilterTest.cpp | 10 + .../test/ExpNegativeImageFilterTest.cpp | 22 +- .../test/HConvexImageFilterTest.cpp | 50 +++- .../test/HMinimaImageFilterTest.cpp | 61 +++-- .../test/Log10ImageFilterTest.cpp | 24 +- .../test/LogImageFilterTest.cpp | 36 ++- ...calWatershedFromMarkersImageFilterTest.cpp | 70 +++++- .../OtsuMultipleThresholdsImageFilterTest.cpp | 99 +++++--- ...othingRecursiveGaussianImageFilterTest.cpp | 83 +++++-- .../test/SqrtImageFilterTest.cpp | 31 ++- .../test/SquareImageFilterTest.cpp | 46 +++- .../test/TanImageFilterTest.cpp | 19 ++ ...imumConnectedComponentsImageFilterTest.cpp | 105 +++++++-- .../NXImageProcessing/test/WriteImageTest.cpp | 40 +++- .../ConnectedComponentEngine.hpp | 83 ++++--- .../FiniteDifferenceEngine.hpp | 1 + .../ImageProcessing/HistogramEngine.hpp | 58 +++-- .../MorphologicalReconstructionEngine.hpp | 2 +- .../ImageProcessing/OtsuImageEngine.hpp | 87 ++++--- .../ImageProcessing/PointwiseEngine.hpp | 1 + .../RecursiveGaussianEngine.hpp | 10 +- ...esholdMaximumConnectedComponentsEngine.hpp | 22 +- 47 files changed, 1260 insertions(+), 411 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp index 98b12653e7..6b27793a01 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AcosImageFilter.cpp @@ -2,6 +2,8 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 144218d73c..5cae16c59d 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -25,18 +25,30 @@ */ namespace nx::core::WriteImageColor { +struct SliceExtents +{ + usize Width = 0; + usize Height = 0; + usize Count = 0; +}; + +struct ProgressCallbacks +{ + std::function Range; + std::function Rows; +}; + /** * @brief Checks the products used by color slice, group and RGB indexes. * @tparam T Scalar input type. - * @param width Output slice width. - * @param height Output slice height. - * @param slices Number of output slices. + * @param extents Output width, height, and slice count. * @param hasMask Whether the source group also holds a mask. * @return An error for empty dimensions or unrepresentable buffer sizes. */ template -Result<> ValidateSize(usize width, usize height, usize slices, bool hasMask) +Result<> ValidateSize(const SliceExtents& extents, bool hasMask) { + const auto& [width, height, slices] = extents; constexpr usize maximum = std::numeric_limits::max(); const usize bytesPerValue = sizeof(T) + (hasMask ? std::max(sizeof(bool), sizeof(uint8)) : 0); if(width == 0 || height == 0 || slices == 0 || width > maximum / height) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 80ae6b4ead..9c7786dd4b 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -197,16 +197,16 @@ struct ColorizeVolumeFunctor { // The operation calculates the range in pages and then colorizes bounded slices. template - Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, usize sliceCount, usize sliceW, usize sliceH, const std::vector& binPoints, + Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const WriteImageColor::SliceExtents& sliceExtents, const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, const std::atomic_bool& shouldCancel, - const std::function(std::vector&, usize)>& writeSlice, const std::function& reportRange = {}, - const std::function& reportRows = {}) + const std::function(std::vector&, usize)>& writeSlice, const WriteImageColor::ProgressCallbacks& progress) { - if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceW, sliceH, sliceCount, maskArray != nullptr); sizeResult.invalid()) + const auto& [sliceW, sliceH, sliceCount] = sliceExtents; + if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) { return sizeResult; } - auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, reportRange); + auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); if(rangeResult.invalid()) { return ConvertResult(std::move(rangeResult)); @@ -278,9 +278,9 @@ struct ColorizeVolumeFunctor sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; } - if(reportRows) + if(progress.Rows) { - reportRows(row + 1, sliceH); + progress.Rows(row + 1, sliceH); } } @@ -413,13 +413,15 @@ Result<> WriteImageDirect::operator()() ThrottledMessageHandler progressThrottle(m_MessageHandler); progressThrottle.reset(sliceCount, "Writing image slices"); m_MessageHandler.sendInfoMessage("Preparing and writing image slices"); - const std::function reportRange = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }; - const std::function reportRows = [&](usize current, usize total) { - if(sliceCount == 1) - { - progressThrottle.updatePercent("Preparing image rows", current, total); - } - }; + const WriteImageColor::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, + .Rows = + [&](usize current, usize total) { + if(sliceCount == 1) + { + progressThrottle.updatePercent("Preparing image rows", current, total); + } + }}; + const WriteImageColor::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; // Encode and atomically commit one independently named slice file. auto writeSlice = [&](std::vector& sliceBuffer, usize slice) -> Result<> { @@ -520,9 +522,8 @@ Result<> WriteImageDirect::operator()() maskArrayPtr = m_DataStructure.getDataAs(m_InputValues.maskArrayPath); } - auto colorResult = - ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, binPoints, controlPoints, maskArrayPtr, - m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), reportRange, reportRows); + auto colorResult = ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceExtents, binPoints, controlPoints, maskArrayPtr, + m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } @@ -535,7 +536,7 @@ Result<> WriteImageDirect::operator()() { return {}; } - auto extractResult = ExecuteDataFunction(ExtractSliceFunctor{}, dataType, imageArray, sliceBuffer, slice, m_InputValues.planeIndex, dimX, dimY, dimZ, nComp, reportRows); + auto extractResult = ExecuteDataFunction(ExtractSliceFunctor{}, dataType, imageArray, sliceBuffer, slice, m_InputValues.planeIndex, dimX, dimY, dimZ, nComp, progress.Rows); if(extractResult.invalid()) { return extractResult; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index 09b8268664..c20c9d3174 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -367,9 +367,7 @@ struct ColorizeVolumeFunctor * @param dimX Number of source tuples on the X axis. * @param dimY Number of source tuples on the Y axis. * @param dimZ Number of source tuples on the Z axis. - * @param sliceCount Number of output slices. - * @param sliceW Output width in pixels. - * @param sliceH Output height in pixels. + * @param sliceExtents Output width, height, and slice count. * @param dataArrayPath Identifies the scalar source array. * @param binPoints Normalized color-control positions. * @param controlPoints Color-control values. @@ -378,21 +376,20 @@ struct ColorizeVolumeFunctor * @param nonfinitePolicy Error (0) or Warn and Continue (1) for unmasked nonfinite input. * @param shouldCancel Stops processing between output slices. * @param writeSlice Writes one packed RGB output slice. - * @param reportRange Optional synchronous callback for completed range-scan values. - * @param reportRows Optional synchronous callback for completed output rows. + * @param progress Synchronous callbacks for range-scan values and output rows. * @return Error from source I/O or the image writer. */ template - Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, usize sliceCount, usize sliceW, usize sliceH, const DataPath& dataArrayPath, + Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const WriteImageColor::SliceExtents& sliceExtents, const DataPath& dataArrayPath, const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, - const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const std::function& reportRange = {}, - const std::function& reportRows = {}) + const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const WriteImageColor::ProgressCallbacks& progress) { - if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceW, sliceH, sliceCount, maskArray != nullptr); sizeResult.invalid()) + const auto& [sliceW, sliceH, sliceCount] = sliceExtents; + if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) { return sizeResult; } - auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, reportRange); + auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); if(rangeResult.invalid()) { return ConvertResult(std::move(rangeResult)); @@ -508,9 +505,9 @@ struct ColorizeVolumeFunctor sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; } - if(reportRows) + if(progress.Rows) { - reportRows(row + 1, sliceH); + progress.Rows(row + 1, sliceH); } } @@ -645,13 +642,15 @@ Result<> WriteImageScanline::operator()() ThrottledMessageHandler progressThrottle(m_MessageHandler); progressThrottle.reset(sliceCount, "Writing image slices"); m_MessageHandler.sendInfoMessage("Preparing and writing image slices"); - const std::function reportRange = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }; - const std::function reportRows = [&](usize current, usize total) { - if(sliceCount == 1) - { - progressThrottle.updatePercent("Preparing image rows", current, total); - } - }; + const WriteImageColor::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, + .Rows = + [&](usize current, usize total) { + if(sliceCount == 1) + { + progressThrottle.updatePercent("Preparing image rows", current, total); + } + }}; + const WriteImageColor::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; // Shared per-slice writer: names the file, writes via the ImageIO layer, commits atomically. auto writeSlice = [&](std::vector& sliceBuffer, usize slice) -> Result<> { @@ -746,12 +745,12 @@ Result<> WriteImageScanline::operator()() maskArrayPtr = m_DataStructure.getDataAs(m_InputValues.maskArrayPath); } - auto colorResult = ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, m_InputValues.imageDataArrayPath, - binPoints, controlPoints, maskArrayPtr, m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, - std::function(std::vector&, usize)>(writeSlice), reportRange, reportRows); + auto colorResult = + ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceExtents, m_InputValues.imageDataArrayPath, binPoints, controlPoints, + maskArrayPtr, m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } return ExecuteDataFunction(WriteDirectVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceCount, sliceW, sliceH, nComp, m_InputValues.imageDataArrayPath, - m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), reportRows); + m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress.Rows); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp index 52643e6c1e..42bcb1372e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/AsinImageFilter.cpp @@ -2,6 +2,8 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "simplnx/Common/Result.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp index 48723fddc1..873a6080a7 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp @@ -34,6 +34,7 @@ class NXIMAGEPROCESSING_EXPORT ConnectedComponentImageFilter : public IFilter static constexpr StringLiteral k_InputImageDataPath_Key = "input_image_data_path"; static constexpr StringLiteral k_OutputImageArrayName_Key = "output_array_name"; static constexpr StringLiteral k_FullyConnected_Key = "fully_connected"; + // NOLINTNEXTLINE(readability-identifier-naming) -- Use the same k_Name_Key spelling as the other filter argument keys. static constexpr StringLiteral k_ProvisionalLabelLimitHandling_Key = "provisional_label_limit_handling_index"; std::string name() const override; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp index 49f5101eac..5c6b68f3b2 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp index 12b51de636..e38e35d246 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp index 7016f29d27..6d9fa80ae8 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp index 1106904987..03f959f887 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp index a11390574e..b134f508c6 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp @@ -23,6 +23,7 @@ class NXIMAGEPROCESSING_EXPORT MorphologicalWatershedFromMarkersImageFilter : pu static constexpr StringLiteral k_MarkerImageDataPath_Key = "marker_image_data_path"; static constexpr StringLiteral k_MarkWatershedLine_Key = "mark_watershed_line"; static constexpr StringLiteral k_FullyConnected_Key = "fully_connected"; + // NOLINTNEXTLINE(readability-identifier-naming) -- Use the same k_Name_Key spelling as the other filter argument keys. static constexpr StringLiteral k_MarkerIdConversionHandling_Key = "marker_id_conversion_handling_index"; static constexpr StringLiteral k_OutputImageArrayName_Key = "output_array_name"; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index a6e859748a..4282f2448c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -1,4 +1,218 @@ #include "OtsuMultipleThresholdsImageFilter.hpp" +#include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" + #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataArray.hpp" +#include "simplnx/DataStructure/DataPath.hpp" +#include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Filter/IFilter.hpp" +#include "simplnx/Filter/Parameters.hpp" +#include "simplnx/Parameters/ArraySelectionParameter.hpp" +#include "simplnx/Parameters/BoolParameter.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" +#include "simplnx/Parameters/DataGroupSelectionParameter.hpp" +#include "simplnx/Parameters/DataObjectNameParameter.hpp" +#include "simplnx/Parameters/GeometrySelectionParameter.hpp" +#include "simplnx/Parameters/NumberParameter.hpp" +#include "simplnx/Parameters/StringParameter.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp" +#include "simplnx/Utilities/SIMPLConversion.hpp" + +#include + +#include +#include +#include +#include + +using namespace nx::core; + +namespace nx::core +{ +std::string OtsuMultipleThresholdsImageFilter::name() const +{ + return FilterTraits::name; +} +std::string OtsuMultipleThresholdsImageFilter::className() const +{ + return FilterTraits::className; +} +Uuid OtsuMultipleThresholdsImageFilter::uuid() const +{ + return FilterTraits::uuid; +} +std::string OtsuMultipleThresholdsImageFilter::humanName() const +{ + return "Otsu Multiple Thresholds Image Filter"; +} +std::vector OtsuMultipleThresholdsImageFilter::defaultTags() const +{ + return {className(), "NXImageProcessing", "OtsuMultipleThresholds", "Thresholding"}; +} +Parameters OtsuMultipleThresholdsImageFilter::parameters() const +{ + Parameters params; + params.insertSeparator(Parameters::Separator{"Input Parameter(s)"}); + params.insert(std::make_unique(k_NumberOfThresholds_Key, "Number Of Thresholds", "Set/Get the number of thresholds. Default is 1.", 1u)); + params.insert(std::make_unique(k_LabelOffset_Key, "Label Offset", "Set/Get the offset which labels have to start from. Default is 0.", 0u)); + params.insert(std::make_unique(k_NumberOfHistogramBins_Key, "Number Of Histogram Bins", "Set/Get the number of histogram bins. Default is 128.", 128u)); + params.insert(std::make_unique(k_ValleyEmphasis_Key, "Valley Emphasis", "Set/Get the use of valley emphasis. Default is false.", false)); + params.insert( + std::make_unique(k_ReturnBinMidpoint_Key, "ReturnBinMidpoint", "Should the threshold value be mid-point of the bin or the maximum? Default is to return bin maximum.", false)); + params.insert(std::make_unique( + k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", + "Error stops on selected NaN or infinity. Warn and Continue retains NaN/negative infinity in the first histogram bin and positive infinity in the last; classification may be unreliable.", 0, + ChoicesParameter::Choices{"Error", "Warn and Continue"})); + params.insertLinkableParameter( + std::make_unique(k_UseMaskArray_Key, "Use Mask Array", "Explicitly exclude pixels with a zero/false mask value before range and nonfinite checks.", false)); + params.insert(std::make_unique(k_MaskArrayPath_Key, "Mask Array", "Select a scalar bool or uint8 mask with one value per input tuple. Nonzero values select pixels.", + DataPath{}, ArraySelectionParameter::AllowedTypes{DataType::boolean, DataType::uint8}, ArraySelectionParameter::AllowedComponentShapes{{1}})); + params.insert(std::make_unique(k_MaskedLabel_Key, "Masked Label", + "Label for excluded pixels. It must be outside the entire configured class interval; downstream filters must explicitly honor it.", uint8{255})); + params.linkParameters(k_UseMaskArray_Key, k_MaskArrayPath_Key, true); + params.linkParameters(k_UseMaskArray_Key, k_MaskedLabel_Key, true); + + params.insertSeparator(Parameters::Separator{"Input Cell Data"}); + params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), + GeometrySelectionParameter::AllowedTypes{IGeometry::Type::Image})); + params.insert(std::make_unique(k_InputImageDataPath_Key, "Input Cell Data", "The image data that will be processed by this filter.", DataPath{}, + nx::core::ImageProcessing::GetScalarNumericTypes())); + + params.insertSeparator(Parameters::Separator{"Output Cell Data"}); + params.insert(std::make_unique(k_OutputImageArrayName_Key, "Output Cell Data", + "The result of the processing will be stored in this Data Array inside the same group as the input data.", "Output Image Data")); + + return params; +} +IFilter::VersionType OtsuMultipleThresholdsImageFilter::parametersVersion() const +{ + return 2; +} +IFilter::UniquePointer OtsuMultipleThresholdsImageFilter::clone() const +{ + return std::make_unique(); +} +IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const DataStructure& dataStructure, const Arguments& filterArgs, const MessageHandler& messageHandler, + const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const +{ + auto imageGeomPath = filterArgs.value(k_InputImageGeomPath_Key); + auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); + auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); + auto numberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key); + auto numberOfHistogramBins = filterArgs.value(k_NumberOfHistogramBins_Key); + const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); + + // Data-independent guard: an out-of-range threshold count makes the backend return an empty threshold + // set, which would silently classify every voxel into a single (labelOffset-valued) class. + if(numberOfThresholds == 0 || numberOfThresholds >= numberOfHistogramBins) + { + return { + MakeErrorResult(-8311, fmt::format("NumberOfThresholds ({}) must be >= 1 and < NumberOfHistogramBins ({}).", static_cast(numberOfThresholds), numberOfHistogramBins))}; + } + const uint32 labelOffset = filterArgs.value(k_LabelOffset_Key); + const uint32 lastLabel = labelOffset + static_cast(numberOfThresholds); + if(lastLabel > 255) + { + return {.outputActions = + MakeErrorResult(-8312, fmt::format("Label Offset ({}) plus Number Of Thresholds ({}) exceeds 255. Reduce either value to keep every class label within uint8.", + labelOffset, static_cast(numberOfThresholds)))}; + } + if(filterArgs.value(k_UseMaskArray_Key)) + { + const uint32 maskedLabel = filterArgs.value(k_MaskedLabel_Key); + if(maskedLabel >= labelOffset && maskedLabel <= lastLabel) + { + return {.outputActions = MakeErrorResult( + -8313, fmt::format("Masked Label ({}) collides with the configured class interval [{}, {}]. Select a Masked Label outside this interval or reduce the class interval.", maskedLabel, + labelOffset, lastLabel))}; + } + const auto maskPath = filterArgs.value(k_MaskArrayPath_Key); + const auto& mask = dataStructure.getDataRefAs(maskPath); + const auto& input = dataStructure.getDataRefAs(selectedInputArray); + if(mask.getNumberOfTuples() != input.getNumberOfTuples()) + { + return {.outputActions = MakeErrorResult(-8314, fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", + maskPath.toString(), mask.getNumberOfTuples(), selectedInputArray.toString(), input.getNumberOfTuples()))}; + } + } + + const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); + if(inputArray.getNumberOfComponents() != 1) + { + return {MakeErrorResult(-8310, fmt::format("Otsu Multiple Thresholds requires a single-component (scalar) input array, but '{}' has {} components.", selectedInputArray.toString(), + inputArray.getNumberOfComponents()))}; + } + + // Output is a uint8 label image regardless of the (numeric) input element type. + Result resultOutputActions = + ImageProcessing::PreflightFullOverwriteImageFilter(dataStructure, selectedInputArray, imageGeomPath, outputArrayPath); + return {std::move(resultOutputActions)}; +} +Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, + const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const +{ + auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); + auto outputArrayName = filterArgs.value(k_OutputImageArrayName_Key); + const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); + + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = filterArgs.value(k_NumberOfHistogramBins_Key), + .NumberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key), + .LabelOffset = filterArgs.value(k_LabelOffset_Key), + .MaskedLabel = filterArgs.value(k_MaskedLabel_Key), + .WarnOnNonfinite = filterArgs.value(k_NonfiniteInputHandling_Key) == 1, + .ValleyEmphasis = filterArgs.value(k_ValleyEmphasis_Key), + .ReturnBinMidpoint = filterArgs.value(k_ReturnBinMidpoint_Key)}; + const IDataArray* maskPtr = filterArgs.value(k_UseMaskArray_Key) ? &dataStructure.getDataRefAs(filterArgs.value(k_MaskArrayPath_Key)) : nullptr; + // Dispatch is synchronous, so the filter arguments and local paths remain live. + return ImageProcessing::AllNumeric::dispatch(dataStructure.getDataRefAs(selectedInputArray).getDataType(), [&]() -> Result<> { + const auto& input = dataStructure.getDataRefAs>(selectedInputArray).getDataStoreRef(); + auto& output = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); + if(maskPtr != nullptr && maskPtr->getDataType() == DataType::boolean) + { + return ImageProcessing::ApplyOtsuImage(input, output, &maskPtr->getIDataStoreRefAs>(), options, shouldCancel, messageHandler); + } + const auto* maskStorePtr = maskPtr == nullptr ? nullptr : &maskPtr->getIDataStoreRefAs>(); + return ImageProcessing::ApplyOtsuImage(input, output, maskStorePtr, options, shouldCancel, messageHandler); + }); +} + +namespace +{ +namespace SIMPL +{ +constexpr StringLiteral k_NumberOfThresholdsKey = "NumberOfThresholds"; +constexpr StringLiteral k_LabelOffsetKey = "LabelOffset"; +constexpr StringLiteral k_NumberOfHistogramBinsKey = "NumberOfHistogramBins"; +constexpr StringLiteral k_ValleyEmphasisKey = "ValleyEmphasis"; +constexpr StringLiteral k_SelectedCellArrayPathKey = "SelectedCellArrayPath"; +constexpr StringLiteral k_NewCellArrayNameKey = "NewCellArrayName"; +} // namespace SIMPL +} // namespace + +Result OtsuMultipleThresholdsImageFilter::FromSIMPLJson(const nlohmann::json& json) +{ + Arguments args = OtsuMultipleThresholdsImageFilter().getDefaultArguments(); + + std::vector> results; + + results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_NumberOfThresholdsKey, k_NumberOfThresholds_Key)); + results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_LabelOffsetKey, k_LabelOffset_Key)); + results.push_back(SIMPLConversion::ConvertParameter>(args, json, SIMPL::k_NumberOfHistogramBinsKey, k_NumberOfHistogramBins_Key)); + results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_ValleyEmphasisKey, k_ValleyEmphasis_Key)); + results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_SelectedCellArrayPathKey, k_InputImageGeomPath_Key)); + results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_SelectedCellArrayPathKey, k_InputImageDataPath_Key)); + results.push_back(SIMPLConversion::ConvertParameter(args, json, SIMPL::k_NewCellArrayNameKey, k_OutputImageArrayName_Key)); + + Result<> conversionResult = MergeResults(std::move(results)); + + return ConvertResultTo(std::move(conversionResult), std::move(args)); +} +} // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp index d29d3816aa..70ff1830ae 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp @@ -26,10 +26,12 @@ class NXIMAGEPROCESSING_EXPORT OtsuMultipleThresholdsImageFilter : public IFilte static constexpr StringLiteral k_NumberOfHistogramBins_Key = "number_of_histogram_bins"; static constexpr StringLiteral k_ValleyEmphasis_Key = "valley_emphasis"; static constexpr StringLiteral k_ReturnBinMidpoint_Key = "return_bin_midpoint"; + // NOLINTBEGIN(readability-identifier-naming) -- Use the same k_Name_Key spelling as the other filter argument keys. static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; static constexpr StringLiteral k_UseMaskArray_Key = "use_mask_array"; static constexpr StringLiteral k_MaskArrayPath_Key = "mask_array_path"; static constexpr StringLiteral k_MaskedLabel_Key = "masked_label"; + // NOLINTEND(readability-identifier-naming) static Result FromSIMPLJson(const nlohmann::json& json); diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp index a2dddaacb2..cc1e988acf 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp index 9e852d9b1c..0b6854f3fc 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp @@ -11,6 +11,7 @@ #include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" @@ -94,7 +95,7 @@ struct SquareOperation return [](T value) -> U { if constexpr(std::is_integral_v) { - const U widenedValue = static_cast(value); + const U widenedValue = static_cast(+value); return static_cast(widenedValue * widenedValue); } else diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp index 44023cf257..6e16def947 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp @@ -4,7 +4,9 @@ #include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Filter/Output.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/DataGroupSelectionParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp index a11967eb0e..ca47c4947f 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.hpp @@ -35,6 +35,7 @@ class NXIMAGEPROCESSING_EXPORT ThresholdMaximumConnectedComponentsImageFilter : static constexpr StringLiteral k_UpperBoundary_Key = "upper_boundary"; static constexpr StringLiteral k_InsideValue_Key = "inside_value"; static constexpr StringLiteral k_OutsideValue_Key = "outside_value"; + // NOLINTNEXTLINE(readability-identifier-naming) -- Use the same k_Name_Key spelling as the other filter argument keys. static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; std::string name() const override; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp index a04b91e388..f071d7235c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.hpp @@ -32,6 +32,7 @@ class NXIMAGEPROCESSING_EXPORT WriteImageFilter : public IFilter static constexpr StringLiteral k_TotalIndexDigits_Key = "total_index_digits"; static constexpr StringLiteral k_LeadingDigitCharacter_Key = "leading_digit_character"; static constexpr StringLiteral k_CreateColorTable_Key = "create_color_table"; + // NOLINTNEXTLINE(readability-identifier-naming) -- Use the same k_Name_Key spelling as the other filter argument keys. static constexpr StringLiteral k_NonfiniteInputHandling_Key = "nonfinite_input_handling_index"; static constexpr StringLiteral k_SelectedPreset_Key = "selected_preset"; static constexpr StringLiteral k_UseMask_Key = "use_mask"; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index 09f45e8ac5..8323a49d8b 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -890,29 +890,35 @@ inline usize SelectRelabelStagingValues(usize numValues, uint64 grantedBytes, us /** * @brief Subtracts HConvex reconstruction values in one owned resident range without signed overflow. * @tparam T Signed integer scalar type. - * @param inputValuesPtr Original image values. - * @param reconstructionValuesPtr Reconstruction values, each no greater than the corresponding input. - * @param outputValuesPtr Contrast output; may alias the reconstruction buffer. - * @param count Number of values in the range. + * @param inputValues Original image values. + * @param reconstructionValues Reconstruction values, each no greater than the corresponding input. + * @param outputValues Contrast output; may alias either input span. * @param start Global tuple offset for diagnostics. * @param inputPath Original array path for diagnostics. * @return The first contrast overflow, or a valid result. */ template -Result<> SubtractHConvexContrastRange(const T* inputValuesPtr, const T* reconstructionValuesPtr, T* outputValuesPtr, usize count, usize start, const DataPath& inputPath) +// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) -- The two read-only spans have the same type but distinct subtraction roles. +Result<> SubtractHConvexContrastRange(nonstd::span inputValues, nonstd::span reconstructionValues, nonstd::span outputValues, usize start, const DataPath& inputPath) { static_assert(std::is_integral_v && std::is_signed_v); + const usize count = inputValues.size(); std::atomic firstInvalid{count}; - ApplyElementwiseResidentRange(inputValuesPtr, reconstructionValuesPtr, outputValuesPtr, count, [&firstInvalid](const T* inputPtr, const T* reconstructionPtr, T* outputPtr, usize index) { - const T inputValue = inputPtr[index]; - const T reconstructionValue = reconstructionPtr[index]; - // max + a negative value is representable. Check before subtracting, including for int64 minima. - if(reconstructionValue < 0 && inputValue > std::numeric_limits::max() + reconstructionValue) + ParallelDataAlgorithm parallelAlgorithm; + parallelAlgorithm.setRange(0, count); + parallelAlgorithm.execute([=, &firstInvalid](const Range& range) { + for(usize index = range.min(); index < range.max(); ++index) { - PublishSmallerIndex(firstInvalid, index); - return; + const T inputValue = inputValues[index]; + const T reconstructionValue = reconstructionValues[index]; + // max + a negative value is representable. Check before subtracting, including for int64 minima. + if(reconstructionValue < 0 && inputValue > std::numeric_limits::max() + reconstructionValue) + { + PublishSmallerIndex(firstInvalid, index); + continue; + } + outputValues[index] = static_cast(inputValue - reconstructionValue); } - outputPtr[index] = static_cast(inputValue - reconstructionValue); }); const usize invalid = firstInvalid.load(std::memory_order_relaxed); if(invalid != count) @@ -936,6 +942,7 @@ Result<> SubtractHConvexContrastRange(const T* inputValuesPtr, const T* reconstr * @return The first transfer or contrast error, with warnings from completed transfers. */ template +// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) -- The input and reconstruction are distinct stores of the same element type. Result<> SubtractHConvexContrastChunked(const AbstractDataStore& input, const AbstractDataStore& reconstruction, AbstractDataStore& output, const DataPath& inputPath, const std::atomic_bool& shouldCancel, usize chunkValues = k_ElementwiseChunkValues) { @@ -962,7 +969,9 @@ Result<> SubtractHConvexContrastChunked(const AbstractDataStore& input, const { return accumulatedResult; } - accumulatedResult = MergeResults(std::move(accumulatedResult), SubtractHConvexContrastRange(inputBuffer->data(), reconstructionBuffer->data(), inputBuffer->data(), count, start, inputPath)); + accumulatedResult = + MergeResults(std::move(accumulatedResult), SubtractHConvexContrastRange(nonstd::span(inputBuffer->data(), count), nonstd::span(reconstructionBuffer->data(), count), + nonstd::span(inputBuffer->data(), count), start, inputPath)); if(accumulatedResult.invalid()) { return accumulatedResult; @@ -1011,7 +1020,7 @@ Result<> SubtractHConvexContrast(const AbstractDataStore& input, const Abstra const auto inputValues = residentInputPtr->createSpan(); const auto reconstructionValues = residentReconstructionPtr->createSpan(); auto outputValues = residentOutputPtr->createSpan(); - return SubtractHConvexContrastRange(inputValues.data(), reconstructionValues.data(), outputValues.data(), inputValues.size(), 0, inputPath); + return SubtractHConvexContrastRange(inputValues, reconstructionValues, outputValues, 0, inputPath); } return SubtractHConvexContrastChunked(input, reconstruction, output, inputPath, shouldCancel); @@ -1030,7 +1039,7 @@ class MarkerIdConversionState MarkerIdConversionState& operator=(const MarkerIdConversionState&) = delete; MarkerIdConversionState& operator=(MarkerIdConversionState&&) = delete; - bool observe(T nativeValue, uint32 encodedValue, usize index) noexcept + bool observe(usize index, T nativeValue, uint32 encodedValue) noexcept { const T restoredValue = static_cast(encodedValue); if(restoredValue == nativeValue) @@ -1099,8 +1108,8 @@ struct WidenIntegerArrayToUInt32Fn try { constexpr usize chunkValues = 65536; - const auto inBufferPtr = std::make_unique(std::min(chunkValues, total)); - const auto outBufferPtr = std::make_unique(std::min(chunkValues, total)); + std::vector inputValues(std::min(chunkValues, total)); + std::vector encodedValues(std::min(chunkValues, total)); for(usize start = 0; start < total;) { if(shouldCancel) @@ -1108,7 +1117,7 @@ struct WidenIntegerArrayToUInt32Fn return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); } const usize count = std::min(chunkValues, total - start); - auto readResult = s.copyIntoBuffer(start, nonstd::span(inBufferPtr.get(), count)); + auto readResult = s.copyIntoBuffer(start, nonstd::span(inputValues.data(), count)); accumulated = MergeResults(std::move(accumulated), std::move(readResult)); if(accumulated.invalid()) { @@ -1116,16 +1125,17 @@ struct WidenIntegerArrayToUInt32Fn } for(usize index = 0; index < count; ++index) { - const S nativeValue = inBufferPtr[index]; - const uint32 encodedValue = static_cast(nativeValue); - if(conversionState.observe(nativeValue, encodedValue, start + index) && ConversionHandling == MarkerIdConversionHandling::Error) + const S nativeValue = inputValues[index]; + // Promote narrow signed IDs without changing the legacy conversion modulo 2^32. + const uint32 encodedValue = static_cast(+nativeValue); + if(conversionState.observe(start + index, nativeValue, encodedValue) && ConversionHandling == MarkerIdConversionHandling::Error) { // Stop before writing this chunk or entering either flood route. return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); } - outBufferPtr[index] = encodedValue; + encodedValues[index] = encodedValue; } - auto writeResult = dst.copyFromBuffer(start, nonstd::span(outBufferPtr.get(), count)); + auto writeResult = dst.copyFromBuffer(start, nonstd::span(encodedValues.data(), count)); accumulated = MergeResults(std::move(accumulated), std::move(writeResult)); if(accumulated.invalid()) { @@ -1154,7 +1164,8 @@ inline Result<> WidenIntegerArrayToUInt32(const DataStructure& dataStructure, co MarkerIdConversionHandling conversionHandling = MarkerIdConversionHandling::Error) { const auto& srcArray = dataStructure.getDataRefAs(srcPath); - return IntegerOnly::dispatch(srcArray.getDataType(), WidenIntegerArrayToUInt32Fn{dataStructure, srcPath, dst, shouldCancel, conversionHandling}); + return IntegerOnly::dispatch(srcArray.getDataType(), + WidenIntegerArrayToUInt32Fn{.dataStructure = dataStructure, .srcPath = srcPath, .dst = dst, .shouldCancel = shouldCancel, .ConversionHandling = conversionHandling}); } /** @@ -1180,8 +1191,8 @@ struct NarrowUInt32StoreToIntegerArrayFn try { constexpr usize chunkValues = 65536; - const auto inBufferPtr = std::make_unique(std::min(chunkValues, total)); - const auto outBufferPtr = std::make_unique(std::min(chunkValues, total)); + std::vector encodedValues(std::min(chunkValues, total)); + std::vector outputValues(std::min(chunkValues, total)); for(usize start = 0; start < total;) { if(shouldCancel) @@ -1189,7 +1200,7 @@ struct NarrowUInt32StoreToIntegerArrayFn return accumulated; } const usize count = std::min(chunkValues, total - start); - auto readResult = src.copyIntoBuffer(start, nonstd::span(inBufferPtr.get(), count)); + auto readResult = src.copyIntoBuffer(start, nonstd::span(encodedValues.data(), count)); accumulated = MergeResults(std::move(accumulated), std::move(readResult)); if(accumulated.invalid()) { @@ -1197,9 +1208,9 @@ struct NarrowUInt32StoreToIntegerArrayFn } for(usize index = 0; index < count; ++index) { - outBufferPtr[index] = static_cast(inBufferPtr[index]); + outputValues[index] = static_cast(encodedValues[index]); } - auto writeResult = d.copyFromBuffer(start, nonstd::span(outBufferPtr.get(), count)); + auto writeResult = d.copyFromBuffer(start, nonstd::span(outputValues.data(), count)); accumulated = MergeResults(std::move(accumulated), std::move(writeResult)); if(accumulated.invalid()) { diff --git a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp index 67debe4b38..2613f4e487 100644 --- a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp @@ -43,13 +43,18 @@ DataPath runMappedIntegerAcos(DataStructure& dataStructure, const DataPath& inpu args.insertOrAssign(AcosImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(AcosImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); // A mapping regression must fail before invalid integer values can reach a NaN-to-integer cast. + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::float32); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); return input.replaceName("Output"); } @@ -116,6 +121,7 @@ class AcosTransferStore : public DataStore // An angle at zero is pi/2 radians, independently of the input's integer width or signedness. // Keeping the input at zero makes the unfixed integer-output implementation safe to execute. +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer zero produces float32 radians", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -152,7 +158,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer zero produces floa UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: floating input retains type and propagates domain NaN", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -181,18 +189,24 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: floating input retains typ UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); - std::vector values = {0, 1, 2, std::numeric_limits::max()}; - if constexpr(std::is_signed_v) - { - values.push_back(-1); - values.push_back(std::numeric_limits::lowest()); - } + const auto values = [] { + if constexpr(std::is_signed_v) + { + return std::vector{0, 1, 2, std::numeric_limits::max(), -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{0, 1, 2, std::numeric_limits::max()}; + } + }(); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); @@ -216,7 +230,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer endpoints and inva } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -243,7 +259,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: adjacent domain boundaries REQUIRE(std::isnan(store.getValue(3))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::AcosImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AcosImageFilter]") { UnitTest::LoadPlugins(); @@ -278,7 +296,9 @@ TEST_CASE("ImageProcessing::AcosImageFilter: mapped multi-component output prese } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::AcosImageFilter: mapped buffered engine preserves cancellation and I/O failures", "[ImageProcessing][Issue1874][AcosImageFilter]") { UnitTest::LoadPlugins(); @@ -320,6 +340,7 @@ TEST_CASE("ImageProcessing::AcosImageFilter: mapped buffered engine preserves ca REQUIRE(output.getValue(index) == -7.0f); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // PART A domain edge (documents ITK-faithful behavior; does NOT "fix" it): acos is undefined for |x|>1. The op is // static_cast(std::acos(static_cast(x))) -- exactly ITK's equally-unguarded Acos functor -- so acos(2) diff --git a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp index d82922ed5d..167a811fc4 100644 --- a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp @@ -39,21 +39,29 @@ DataPath runMappedIntegerAsin(DataStructure& dataStructure, const DataPath& inpu args.insertOrAssign(AsinImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); args.insertOrAssign(AsinImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); // Stop an unfixed implementation before an invalid integer value can reach a NaN-to-integer conversion. + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::float32); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == componentShape); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); return inputPath.replaceName("Output"); } } // namespace // The principal angle at integer one is pi/2 radians for every integer width and signedness. +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer one produces float32 radians", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -71,18 +79,24 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer one produces float REQUIRE(anglesPtr->getDataStoreRef().getValue(0) == Approx(1.57079632679489661923).epsilon(0).margin(1.0e-7)); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); - std::vector inputValues = {0, 1, 2, std::numeric_limits::max()}; - if constexpr(std::is_signed_v) - { - inputValues.push_back(-1); - inputValues.push_back(std::numeric_limits::lowest()); - } + const auto inputValues = [] { + if constexpr(std::is_signed_v) + { + return std::vector{0, 1, 2, std::numeric_limits::max(), -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{0, 1, 2, std::numeric_limits::max()}; + } + }(); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, inputValues); const DataPath outputPath = runMappedIntegerAsin(dataStructure, inputPath, {1, 1, inputValues.size()}); @@ -105,7 +119,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer endpoints and inva } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: floating input retains type and principal angles", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -137,7 +153,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: floating input retains typ } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -163,7 +181,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: adjacent domain boundaries REQUIRE(std::isnan(angleStore.getValue(3))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::AsinImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AsinImageFilter]") { UnitTest::LoadPlugins(); @@ -199,6 +219,7 @@ TEST_CASE("ImageProcessing::AsinImageFilter: mapped multi-component output prese } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // PART A domain edge (documents ITK-faithful behavior): asin is undefined for |x|>1. The op is // static_cast(std::asin(static_cast(x))) -- ITK's equally-unguarded Asin functor -- so asin(2) is NaN. diff --git a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp index 6c2da745e6..8f79fa19c9 100644 --- a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp +++ b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp @@ -26,6 +26,7 @@ #include // Small transport helpers only. Each owning test states its independent numerical expectations. +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. namespace c15_test { using namespace nx::core; @@ -97,27 +98,34 @@ inline DataPath ConvertToFloat32(DataStructure& dataStructure, const DataPath& i return input.replaceName("Converted"); } -struct StoreExpectation +class StoreExpectation { - IDataStore::StoreType type; +public: + explicit StoreExpectation(IDataStore::StoreType type) + : m_Type(type) + { + } void requireExpectedStore(const IDataArray& array) const { - INFO("Array '" << array.getName() << "' expected store type " << static_cast(type)); - REQUIRE(array.getIDataStoreRef().getStoreType() == type); + INFO("Array '" << array.getName() << "' expected store type " << static_cast(m_Type)); + REQUIRE(array.getIDataStoreRef().getStoreType() == m_Type); } + +private: + IDataStore::StoreType m_Type; }; template std::vector Read(DataStructure& dataStructure, const DataPath& path, const ShapeType& shape, const Scope& scope) { - const DataArray* array = nullptr; - REQUIRE_NOTHROW(array = &dataStructure.getDataRefAs>(path)); - REQUIRE(array->getDataType() == GetDataType()); - REQUIRE(array->getTupleShape() == shape); - REQUIRE(array->getComponentShape() == ShapeType{1}); - scope.requireExpectedStore(*array); - const auto& store = array->getDataStoreRef(); + const DataArray* arrayPtr = nullptr; + REQUIRE_NOTHROW(arrayPtr = &dataStructure.getDataRefAs>(path)); + REQUIRE(arrayPtr->getDataType() == GetDataType()); + REQUIRE(arrayPtr->getTupleShape() == shape); + REQUIRE(arrayPtr->getComponentShape() == ShapeType{1}); + scope.requireExpectedStore(*arrayPtr); + const auto& store = arrayPtr->getDataStoreRef(); if(store.getStoreType() == IDataStore::StoreType::OutOfCore) { REQUIRE(store.getDataFormat() == "HDF5-OOC"); @@ -126,7 +134,7 @@ std::vector Read(DataStructure& dataStructure, const DataPath& path, const Sh REQUIRE(backing != metadata.end()); REQUIRE(std::filesystem::is_regular_file(backing->second)); } - std::vector values(array->getSize()); + std::vector values(arrayPtr->getSize()); const auto result = store.copyIntoBuffer(0, nonstd::span(values.data(), values.size())); SIMPLNX_RESULT_REQUIRE_VALID(result); return values; @@ -185,21 +193,21 @@ inline DataPath Components(DataStructure& dataStructure, const DataPath& input, inline void SetGeometry(DataStructure& dataStructure) { - ImageGeom* geometry = nullptr; - REQUIRE_NOTHROW(geometry = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); - geometry->setSpacing({1, 2, 3}); - geometry->setOrigin({4, 5, 6}); - geometry->setUnits(IGeometry::LengthUnit::Millimeter); + ImageGeom* geometryPtr = nullptr; + REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); + geometryPtr->setSpacing({1, 2, 3}); + geometryPtr->setOrigin({4, 5, 6}); + geometryPtr->setUnits(IGeometry::LengthUnit::Millimeter); } inline void CheckGeometry(DataStructure& dataStructure, const SizeVec3& dimensions) { - const ImageGeom* geometry = nullptr; - REQUIRE_NOTHROW(geometry = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); - REQUIRE(geometry->getDimensions() == dimensions); - REQUIRE(geometry->getSpacing() == FloatVec3{1, 2, 3}); - REQUIRE(geometry->getOrigin() == FloatVec3{4, 5, 6}); - REQUIRE(geometry->getUnits() == IGeometry::LengthUnit::Millimeter); + const ImageGeom* geometryPtr = nullptr; + REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(geometryPtr->getDimensions() == dimensions); + REQUIRE(geometryPtr->getSpacing() == FloatVec3{1, 2, 3}); + REQUIRE(geometryPtr->getOrigin() == FloatVec3{4, 5, 6}); + REQUIRE(geometryPtr->getUnits() == IGeometry::LengthUnit::Millimeter); } inline Arguments WriteArguments(const DataPath& input, const std::filesystem::path& output) @@ -223,10 +231,10 @@ inline Arguments WriteArguments(const DataPath& input, const std::filesystem::pa template std::vector Decode(const std::filesystem::path& path, usize width, usize height, usize components = 1) { - const auto io = CreateImageIO(path); - SIMPLNX_RESULT_REQUIRE_VALID(io); - REQUIRE(io.value() != nullptr); - const auto metadata = io.value()->readMetadata(path); + const auto imageIO = CreateImageIO(path); + SIMPLNX_RESULT_REQUIRE_VALID(imageIO); + REQUIRE(imageIO.value() != nullptr); + const auto metadata = imageIO.value()->readMetadata(path); SIMPLNX_RESULT_REQUIRE_VALID(metadata); REQUIRE(metadata.value().dataType == GetDataType()); REQUIRE(metadata.value().width == width); @@ -234,7 +242,7 @@ std::vector Decode(const std::filesystem::path& path, usize width, usize heig REQUIRE(metadata.value().numComponents == components); REQUIRE(metadata.value().numPages == 1); std::vector bytes(width * height * components * sizeof(T)); - const auto read = io.value()->readPixelData(path, std::span(bytes)); + const auto read = imageIO.value()->readPixelData(path, std::span(bytes)); SIMPLNX_RESULT_REQUIRE_VALID(read); std::vector values(width * height * components); std::memcpy(values.data(), bytes.data(), bytes.size()); @@ -255,4 +263,5 @@ inline DataStructure ReadMha(const std::filesystem::path& path) UnitTest::CheckArraysInheritTupleDims(result); return result; } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) } // namespace c15_test diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp index 584c17c726..8adf6067ae 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp @@ -1128,6 +1128,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: bounded true-2-D cancell } } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: native provisional capacity decisions require no large forest", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { @@ -1179,7 +1180,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: native provisional capacit REQUIRE(legacyFinished.warnings().size() == 1); REQUIRE(legacyFinished.warnings().front().code == 8772); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reuse consequence model", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); @@ -1187,7 +1190,12 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reus const IFilter::MessageHandler handler; ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); ImageProcessing::detail::CcUnionFind forest(state); - forest.parent = {0, 1, 2, 3}; + for(uint32 expectedLabel = 1; expectedLabel <= 3; ++expectedLabel) + { + const auto labelResult = forest.makeLabel(); + SIMPLNX_RESULT_REQUIRE_VALID(labelResult); + REQUIRE(labelResult.value() == expectedLabel); + } forest.link(2, 3); const ImageProcessing::detail::CcLine current{{.start = 0, .length = 1, .label = 0}}; const ImageProcessing::detail::CcLine previous{{.start = 0, .length = 1, .label = 2}}; @@ -1195,13 +1203,15 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reus REQUIRE(forest.find(2) == 0); REQUIRE(forest.find(3) == 0); // an already-linked component is also absorbed into background forest.link(1, 2); // reused ID 1 now joins the zero root as well - REQUIRE(forest.parent == std::vector{0, 0, 0, 2}); + REQUIRE(forest.parentTable() == std::vector{0, 0, 0, 2}); const std::atomic_bool shouldCancel{false}; const auto resolved = forest.createConsecutive(shouldCancel, {}); REQUIRE(resolved.second == 0); REQUIRE(resolved.first == std::vector{0, 0, 0, 0}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor consequence model preserves a foreground root", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { @@ -1229,7 +1239,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor conseq ImageProcessing::detail::Pass1TwoDimensionalTiles(inputStore, provisional, SizeVec3{3, 2, 1}, predicate, fullyConnected, shouldCancel, forest, nullptr, planResult.value(), {}); SIMPLNX_RESULT_REQUIRE_VALID(passResult); REQUIRE(provisionalStore.zeroRowReads() == 3); - REQUIRE(forest.parent == std::vector{0, 1, 2}); + REQUIRE(forest.parentTable() == std::vector{0, 1, 2}); REQUIRE(forest.find(2) == 2); const auto resolved = forest.createConsecutive(shouldCancel, {}); REQUIRE(resolved.second == 2); @@ -1239,11 +1249,16 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor conseq // The real scanline union on the corresponding literal IDs absorbs the current run into zero instead. ImageProcessing::detail::CcUnionFind scanlineForest(state); - scanlineForest.parent = {0, 1, 2}; + for(uint32 expectedLabel = 1; expectedLabel <= 2; ++expectedLabel) + { + const auto labelResult = scanlineForest.makeLabel(); + SIMPLNX_RESULT_REQUIRE_VALID(labelResult); + REQUIRE(labelResult.value() == expectedLabel); + } const ImageProcessing::detail::CcLine current{{.start = 1, .length = 1, .label = 2}}; const ImageProcessing::detail::CcLine previous{{.start = 1, .length = 1, .label = 0}}; ImageProcessing::detail::CompareLines(current, previous, fullyConnected ? 1 : 0, scanlineForest); - REQUIRE(scanlineForest.parent == std::vector{0, 1, 0}); + REQUIRE(scanlineForest.parentTable() == std::vector{0, 1, 0}); REQUIRE(scanlineForest.find(2) == 0); const auto scanlineResolved = scanlineForest.createConsecutive(shouldCancel, {}); REQUIRE(scanlineResolved.second == 1); @@ -1251,7 +1266,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor conseq REQUIRE(scanlineFinalLabels == std::array{0, 1, 0, 0, 0, 0}); // This exercises the production route's zero-neighbor rule using a controlled scratch-row model, not real overflow. } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: capacity warning context retains independent delivered diagnostics", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { @@ -1274,7 +1291,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: capacity warning context r REQUIRE(result.warnings().at(0).code == 8793); REQUIRE(result.warnings().at(1).code == 8772); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: seeded warning context survives resident retry copy error and cancellation", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { @@ -1332,7 +1351,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: seeded warning context sur } REQUIRE(inputStore.attempts() == (fault == ConnectedComponentReadFaultType::BadAllocOnce ? dims[2] + 1 : 1)); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentEngine: allocation addressability and I/O failures remain errors under both policies", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { @@ -1369,3 +1390,4 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: allocation addressability SIMPLNX_RESULT_REQUIRE_INVALID(countResult); REQUIRE(countResult.errors().front().code == countCode); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp index 41a6ebb064..a46f5d9392 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp @@ -93,6 +93,7 @@ std::vector MakeBlobField(usize dimX, usize dimY, usize dimZ) } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: provisional label limit policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); @@ -122,7 +123,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: provisional label lim REQUIRE(tooltip.find(riskTermPtr) != std::string::npos); } } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: both policies preserve literal connectivity labels and uint32 shape", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); @@ -173,7 +176,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: both policies preserv } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: many provisional runs can form one exact component", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); @@ -205,7 +210,9 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: many provisional runs } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: execution cancellation keeps the filter cancellation result", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); @@ -234,6 +241,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: execution cancellatio REQUIRE(result.result.warnings().empty()); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a non-integer (float32) input is diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 8e4d7f895d..aa26cfa14a 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -24,6 +24,7 @@ #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp" @@ -346,42 +347,59 @@ class CurvatureFlowForwardingStore : public AbstractDataStore std::atomic_bool* m_CancelOnWritePtr = nullptr; }; -Arguments makeCurvatureFlowArgs(const DataPath& inputPath, float64 timeStep, uint32 iterations) +struct CurvatureFlowTestOptions +{ + float64 TimeStep; + uint32 Iterations; +}; + +Arguments makeCurvatureFlowArgs(const DataPath& inputPath, const CurvatureFlowTestOptions& options) { Arguments args; args.insertOrAssign(CurvatureFlowImageFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); args.insertOrAssign(CurvatureFlowImageFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); args.insertOrAssign(CurvatureFlowImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(timeStep)); - args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(iterations)); + args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(options.TimeStep)); + args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(options.Iterations)); return args; } template -void requireCurvatureFlowConstant(DataStructure& dataStructure, const DataPath& inputPath, T expected, float64 timeStep, uint32 iterations) +void requireCurvatureFlowConstant(DataStructure& dataStructure, const DataPath& inputPath, T expected, const CurvatureFlowTestOptions& options) { const CurvatureFlowImageFilter filter; - const Arguments args = makeCurvatureFlowArgs(inputPath, timeStep, iterations); + const Arguments args = makeCurvatureFlowArgs(inputPath, options); const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == GetDataType()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == ShapeType{1, 1, 1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == ShapeType{1}); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); REQUIRE(executeResult.result.warnings().empty()); const DataPath outputPath = inputPath.replaceName("Output"); const IDataArray* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataType() == GetDataType()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); const DataArray* valuesPtr = nullptr; REQUIRE_NOTHROW(valuesPtr = &dataStructure.getDataRefAs>(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(valuesPtr->getDataStoreRef().getValue(0) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -396,9 +414,11 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid time step is rejec DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {7}); const CurvatureFlowImageFilter filter; - const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, timeStep, iterations)); + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = timeStep, .Iterations = iterations})); // Preflight only: never evolve or cast an invalid-control baseline. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.outputActions.errors().front().code == ImageProcessing::k_CurvatureFlowInvalidTimeStep); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -424,9 +444,11 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid active spacing is spacing.at(axis) = invalidSpacing; geometryPtr->setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); const CurvatureFlowImageFilter filter; - const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, 0.0, iterations)); + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = 0.0, .Iterations = iterations})); // All executions are deferred: nonfinite spacing can poison even a constant baseline. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.outputActions.errors().front().code == ImageProcessing::k_CurvatureFlowInvalidSpacing); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -444,7 +466,8 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: finite negative spacing an REQUIRE_NOTHROW(geometryPtr = &dataStructure.getDataRefAs(inputPath.getParent().getParent())); geometryPtr->setSpacing({-1.0F, -2.0F, unusedSpacing}); const CurvatureFlowImageFilter filter; - const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, 0.0, 0)); + const auto result = filter.preflight(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = 0.0, .Iterations = 0})); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -457,7 +480,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: constant finite i DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {TestType{7}}); // A constant has zero first derivatives, so the independent expected update is exactly zero. - requireCurvatureFlowConstant(dataStructure, inputPath, TestType{7}, 0.05, 2); + requireCurvatureFlowConstant(dataStructure, inputPath, TestType{7}, {.TimeStep = 0.05, .Iterations = 2}); } TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: zero operation copies int64 values exactly", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) @@ -470,9 +493,10 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: zero operation co const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); // The baseline rounds this finite constant to 2^53. Both values are well inside either native integer range. // Its constant-image derivative guard returns zero, so zero timestep cannot create a nonfinite update. - requireCurvatureFlowConstant(dataStructure, inputPath, inputValue, zeroIterations ? 0.05 : 0.0, zeroIterations ? 0u : 1u); + requireCurvatureFlowConstant(dataStructure, inputPath, inputValue, {.TimeStep = zeroIterations ? 0.05 : 0.0, .Iterations = zeroIterations ? 0u : 1u}); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: terminal integer conversion has exact bounds", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -513,6 +537,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: terminal integer REQUIRE(output == TestType{7}); } } +// NOLINTEND(bugprone-chained-comparison) TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating narrowing retains IEEE values", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { @@ -526,9 +551,11 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating narrowing retains REQUIRE(std::signbit(negative)); REQUIRE(std::isnan(NarrowFloatingImageValue(std::numeric_limits::quiet_NaN()))); REQUIRE(std::signbit(NarrowFloatingImageValue(-0.0))); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(NarrowFloatingImageValue(std::numeric_limits::max()) == static_cast(std::numeric_limits::max())); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stencil truncates then clips once", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -544,7 +571,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stenc geometryPtr->setSpacing({-1.0F, -1.0F, std::numeric_limits::quiet_NaN()}); } const CurvatureFlowImageFilter filter; - const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, clip ? 10.0 : 0.05, 1)); + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = clip ? 10.0 : 0.05, .Iterations = 1})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == (clip ? 1 : 0)); if(clip) @@ -564,6 +591,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stenc REQUIRE(outputPtr->getDataStoreRef().getValue(4) == (clip ? uint8{0} : uint8{93})); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op preserves integer endpoints", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) { @@ -573,9 +601,10 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op pres const bool zeroIterations = GENERATE(false, true); DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {value}); - requireCurvatureFlowConstant(dataStructure, inputPath, value, zeroIterations ? 0.05 : 0.0, zeroIterations ? 0u : 1u); + requireCurvatureFlowConstant(dataStructure, inputPath, value, {.TimeStep = zeroIterations ? 0.05 : 0.0, .Iterations = zeroIterations ? 0u : 1u}); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer result errors", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -583,13 +612,15 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {0, 0, 0, 0, 100, 100, 0, 100, 100}); const CurvatureFlowImageFilter filter; - const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, std::numeric_limits::max(), 1)); + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = std::numeric_limits::max(), .Iterations = 1})); SIMPLNX_RESULT_REQUIRE_INVALID(result.result); REQUIRE(result.result.errors().front().code == ImageProcessing::k_CurvatureFlowNonFiniteIntegerOutput); REQUIRE(result.result.errors().front().message.find("index 4") != std::string::npos); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong terminal routes honor the stencil", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -635,7 +666,9 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong ter REQUIRE(output.getTupleShape() == shape); REQUIRE(output.getComponentShape() == ShapeType{1}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op propagates transfer failures and cancellation", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -663,12 +696,14 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op propagates tr REQUIRE(output.getValue(0) == 7); REQUIRE(output.getTupleShape() == shape); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: resident wrapper merges clipping warning with copy failure", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); - const ImageProcessing::ScopedWorkingMemoryTuningOverride tuning(1024 * 1024); + const ImageProcessing::ScopedWorkingMemoryTuningOverride tuning(uint64{1024} * 1024); const ShapeType shape{2, 3, 3}; DataStore input(shape, ShapeType{1}, uint8{0}); const std::vector field{0, 0, 0, 0, 100, 100, 0, 100, 100, 0, 0, 0, 0, 100, 100, 0, 100, 100}; @@ -685,7 +720,9 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: resident wrapper merges cl REQUIRE(result.warnings().front().code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput); REQUIRE(output.writeCount() == 1); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output retains nonfinite and overflow behavior", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -695,7 +732,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output r const std::vector field = nanInput ? std::vector(9, std::numeric_limits::quiet_NaN()) : std::vector{0, 0, 0, 0, 100, 100, 0, 100, 100}; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, field); const CurvatureFlowImageFilter filter; - const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, nanInput ? 0.05 : 1.0e40, 1)); + const auto result = filter.execute(dataStructure, makeCurvatureFlowArgs(inputPath, {.TimeStep = nanInput ? 0.05 : 1.0e40, .Iterations = 1})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); const DataPath outputPath = inputPath.replaceName("Output"); @@ -722,7 +759,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output r } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded native transfers", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -747,7 +786,9 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded n REQUIRE(output.getValue(count - 1) == (uint64{1} << 53) + 1); REQUIRE(output.getTupleShape() == shape); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits retain clipping and transfer warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -806,7 +847,9 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits REQUIRE(result.warnings().size() == input.readCount() + output.writeCount() + 1); REQUIRE(output.getTupleShape() == shape); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D terminal exits retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -858,10 +901,12 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D termi REQUIRE(result.warnings().size() == second.readCount() + output.writeCount() + 1); REQUIRE(output.getTupleShape() == shape); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - enum class Exit + enum class Exit : uint8 { Success, HaloReadFailure, @@ -875,9 +920,9 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain w const bool cancel = exit == Exit::CancelAfterWrite; // Use resident forwarding stores to inject diagnostics into the real X-tile transfers. - constexpr usize nx = 5; - constexpr usize ny = 3; - const ShapeType shape{1, ny, nx}; + constexpr usize width = 5; + constexpr usize height = 3; + const ShapeType shape{1, height, width}; DataStore input(shape, ShapeType{1}, uint8{0}); const std::vector field{0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 0, 100, 100, 100, 100}; const auto copyResult = input.copyFromBuffer(0, field); @@ -904,12 +949,12 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain w } constexpr usize limit = ImageProcessing::detail::k_FiniteDifference2DFixedStateBytes + (14 * sizeof(float64)); - const auto plan = ImageProcessing::detail::BuildFiniteDifference2DBufferPlan(nx, ny, 1, sizeof(float64), limit); + const auto plan = ImageProcessing::detail::BuildFiniteDifference2DBufferPlan(width, height, 1, sizeof(float64), limit); REQUIRE(plan.valid); REQUIRE(plan.coreCols == 2); REQUIRE(plan.coreRows == 1); const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( - input, output, first, second, nx, ny, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); + input, output, first, second, width, height, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); if(failRead || failWrite) { SIMPLNX_RESULT_REQUIRE_INVALID(result); @@ -958,6 +1003,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain w [](const Warning& warning) { return warning.code == ImageProcessing::k_CurvatureFlowClippedIntegerOutput; })) == expectedClipping); CHECK(result.warnings().size() == expectedReads + expectedWrites + expectedClipping); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a valid scalar input produces a @@ -1039,6 +1085,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero iterations is a no- rt::RequireExact(ds.getDataRefAs(outputPath), ds.getDataRefAs(inputPath)); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[ImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); @@ -1046,7 +1093,7 @@ TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter f UnitTest::AlgorithmTestScope scope(scenario); const bool step = GENERATE(false, true); CAPTURE(step, scenario); - std::vector original(16 * 4, 4); + std::vector original(usize{16} * 4, 4); if(step) { for(usize index = 0; index < original.size(); ++index) @@ -1054,28 +1101,28 @@ TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter f original[index] = index % 16 < 8 ? 0 : 1; } } - DataStructure ds; - const auto input = recon_test::BuildImageFromPattern(ds, 16, 4, 1, original); - c15_test::SetGeometry(ds); - REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + DataStructure dataStructure; + const auto input = recon_test::BuildImageFromPattern(dataStructure, 16, 4, 1, original); + c15_test::SetGeometry(dataStructure); + REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); const CurvatureFlowImageFilter filter; auto args = c15_test::ImageArguments(filter, input, "Smoothed"); args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(1)); args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(0.125)); - c15_test::ExecuteChecked(filter, ds, args); + c15_test::ExecuteChecked(filter, dataStructure, args); const auto smoothed = input.replaceName("Smoothed"); - const auto values = c15_test::Read(ds, smoothed, {1, 4, 16}, scope); + const auto values = c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope); const GradientMagnitudeImageFilter gradient; args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); - c15_test::Execute(gradient, ds, args, scope); - const auto edges = c15_test::Read(ds, input.replaceName("Gradient"), {1, 4, 16}, scope); - for(usize y = 0; y < 4; ++y) + c15_test::Execute(gradient, dataStructure, args, scope); + const auto edges = c15_test::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); + for(usize row = 0; row < 4; ++row) { - for(usize x = 0; x < 16; ++x) + for(usize column = 0; column < 16; ++column) { - const usize index = y * 16 + x; - CAPTURE(x, y); + const usize index = (row * 16) + column; + CAPTURE(column, row); REQUIRE(std::isfinite(values[index])); REQUIRE(std::isfinite(edges[index])); if(!step) @@ -1087,13 +1134,14 @@ TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter f { // A planar step has zero transverse curvature. Its X central differences are 0.5 at columns 7 and 8. REQUIRE(values[index] == Approx(original[index]).margin(1.0e-6)); - const float32 expectedGradient = (x == 7 || x == 8) ? 0.5F : 0.0F; + const float32 expectedGradient = (column == 7 || column == 8) ? 0.5F : 0.0F; REQUIRE(edges[index] == Approx(expectedGradient).margin(1.0e-6)); } } } - REQUIRE(c15_test::Read(ds, smoothed, {1, 4, 16}, scope) == values); - REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); - c15_test::CheckGeometry(ds, {16, 4, 1}); - UnitTest::CheckArraysInheritTupleDims(ds); -} \ No newline at end of file + REQUIRE(c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); + c15_test::CheckGeometry(dataStructure, {16, 4, 1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp index e6974fde6c..9ef813927b 100644 --- a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp @@ -29,6 +29,7 @@ using namespace nx::core; +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: integer input produces float64 growth and decay", "[NXImageProcessing][Issue1874][ExpImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -102,7 +103,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: integer input produces fl } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values retain growth decay and range behavior", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -188,7 +191,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values reta } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ExpImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpImageFilter]") { UnitTest::LoadPlugins(); @@ -239,7 +244,9 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: mapped multi-component output pres } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::ExpImageFilter: floating result conversion handles finite narrowing boundaries", "[NXImageProcessing][Issue1874][ExpImageFilter]") { UnitTest::LoadPlugins(); @@ -263,7 +270,9 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: floating result conversion handles REQUIRE(ImageProcessing::ConvertMathResult(std::numeric_limits::denorm_min()) == std::numeric_limits::denorm_min()); REQUIRE(std::signbit(ImageProcessing::ConvertMathResult(-0.0))); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -302,6 +311,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating input retains ty REQUIRE(store.getValue(2) == Approx(2.71828182845904523536).epsilon(relativeTolerance)); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // exp(1000) exceeds the float64 finite range and produces positive infinity. // ConvertMathResult retains infinity and explicitly handles finite results above the float32 range. diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index f41da50999..312c71a286 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -1,9 +1,9 @@ #include "C15CompatibilityTestUtils.hpp" -#include "ImageProcessing/Filters/ExpImageFilter.hpp" -#include "ImageProcessing/Filters/Log10ImageFilter.hpp" -#include "ImageProcessing/Filters/LogImageFilter.hpp" -#include "ImageProcessing/Filters/SqrtImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" +#include "NXImageProcessing/Filters/ExpImageFilter.hpp" +#include "NXImageProcessing/Filters/Log10ImageFilter.hpp" +#include "NXImageProcessing/Filters/LogImageFilter.hpp" +#include "NXImageProcessing/Filters/SqrtImageFilter.hpp" #include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/ExpNegativeImageFilter.hpp" @@ -36,6 +36,7 @@ using namespace nx::core; +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input produces float64 decay", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -111,7 +112,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input pro } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE values retain sign reversed range behavior", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -201,7 +204,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE val } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: mapped multi component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]") { UnitTest::LoadPlugins(); @@ -251,7 +256,9 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: mapped multi component out } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -290,6 +297,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating input re REQUIRE(store.getValue(2) == Approx(0.36787944117144232160).epsilon(relativeTolerance)); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // exp(-(-1000)) exceeds the float64 finite range and produces positive infinity. // The shared math conversion also handles finite results above the float32 range explicitly. @@ -349,7 +357,8 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: ITK real-image golden (def UnitTest::CheckArraysInheritTupleDims(ds); } -TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain masks feed components", "[ImageProcessing][C15Compatibility][Issue1874][ExpNegativeImageFilter]") +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. +TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain masks feed components", "[NXImageProcessing][C15Compatibility][Issue1874][ExpNegativeImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -459,4 +468,5 @@ TEST_CASE("ImageProcessing::C15Compatibility resident exponential and domain mas REQUIRE(c15_test::Read(dataStructure, labels, shape, expectedStore) == components); checkQuantitative(); UnitTest::CheckArraysInheritTupleDims(dataStructure); -} \ No newline at end of file +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 9e9522990c..75de54d5a7 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -121,10 +121,13 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: integer fractional Heig // Integer images require a whole Height. Stop at preflight: the baseline must never execute this request. const auto preflightResult = filter.preflight(dataStructure, makeHConvexArguments(inputPath, height)); REQUIRE(preflightResult.outputActions.invalid()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().size() == 1); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().front().code == -1874101); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast with unchanged type", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { @@ -166,7 +169,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: floating fractional Height retains fractional contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -201,7 +206,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: floating fractional Hei } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: native 64-bit unit contrast above double precision", "[ImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); @@ -231,7 +238,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: native 64-bit unit cont REQUIRE(outputStore.getValue(0) == TestType{1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::HConvexImageFilter: exact signed 64-bit marker boundaries in both directions", "[ImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); @@ -261,7 +270,9 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact signed 64-bit marker bound REQUIRE(marker.getValue(index) == expected.at(index)); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: exact integer marker zero unit and huge Heights", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -296,7 +307,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: exact integer marker ze REQUIRE(values == expected); } } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker arithmetic requires explicit opt-in", "[ImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); @@ -315,7 +328,9 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker arithmetic requires SIMPLNX_RESULT_REQUIRE_VALID(markerResult); REQUIRE(marker.getValue(0) == expectedExistingValue); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker Height immediately below 2^64", "[ImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); @@ -334,7 +349,9 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker Height immediately ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(raisedValues.data(), raisedValues.size()), 0, {1, 1, 1}, options); REQUIRE(raisedValues.at(0) == 9223372036854773760LL); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: signed contrast overflow and adjacent representable contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, int16, int32, int64) { UnitTest::LoadPlugins(); @@ -381,7 +398,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: signed contrast overflo } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast at native integer bounds", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -409,7 +428,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::HConvexImageFilter: whole Height contrast is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); @@ -433,7 +454,9 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: whole Height contrast is stable REQUIRE(outputStore.getValue(2) == 0); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::HConvexImageFilter: chunked contrast preserves transfer Results and cancellation", "[ImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); @@ -490,6 +513,7 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: chunked contrast preserves trans REQUIRE(output.getValue(0) == -1); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (2) Hand-computed behavior on a small 2D image: a flat background maps to 0; a bright bump whose contrast is @@ -744,26 +768,28 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: ITK real-image golden (HConvex [](Arguments& args) { args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(10000.0)); }); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::C15Compatibility resident exact widening supports fractional HConvex height", "[ImageProcessing][C15Compatibility][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); - DataStructure ds; + DataStructure dataStructure; const std::vector original{2, 2, 2, 2, 4, 2, 2, 2, 2}; - const auto input = recon_test::BuildImageFromPattern(ds, 3, 3, 1, original); - c15_test::SetGeometry(ds); - const auto converted = c15_test::ConvertToFloat32(ds, input); + const auto input = recon_test::BuildImageFromPattern(dataStructure, 3, 3, 1, original); + c15_test::SetGeometry(dataStructure); + const auto converted = c15_test::ConvertToFloat32(dataStructure, input); const std::vector exactConverted{2, 2, 2, 2, 4, 2, 2, 2, 2}; - REQUIRE(c15_test::Read(ds, converted, {1, 3, 3}, scope) == exactConverted); + REQUIRE(c15_test::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); const HConvexImageFilter filter; auto args = c15_test::ImageArguments(filter, converted, "Contrast"); args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(0.5)); args.insertOrAssign(HConvexImageFilter::k_FullyConnected_Key, std::make_any(false)); - c15_test::Execute(filter, ds, args, scope); - REQUIRE(c15_test::Read(ds, input.replaceName("Contrast"), {1, 3, 3}, scope) == std::vector{0, 0, 0, 0, 0.5F, 0, 0, 0, 0}); - REQUIRE(c15_test::Read(ds, converted, {1, 3, 3}, scope) == exactConverted); - REQUIRE(c15_test::Read(ds, input, {1, 3, 3}, scope) == original); - c15_test::CheckGeometry(ds, {3, 3, 1}); - UnitTest::CheckArraysInheritTupleDims(ds); -} \ No newline at end of file + c15_test::Execute(filter, dataStructure, args, scope); + REQUIRE(c15_test::Read(dataStructure, input.replaceName("Contrast"), {1, 3, 3}, scope) == std::vector{0, 0, 0, 0, 0.5F, 0, 0, 0, 0}); + REQUIRE(c15_test::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); + REQUIRE(c15_test::Read(dataStructure, input, {1, 3, 3}, scope) == original); + c15_test::CheckGeometry(dataStructure, {3, 3, 1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index eed968a546..7dc82260f1 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -2,6 +2,7 @@ #include "ImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" #include "ImageProcessing/Filters/RegionalMinimaImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" +#include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/HMinimaImageFilter.hpp" @@ -16,12 +17,14 @@ #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Filter/Arguments.hpp" +#include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include +#include #include #include #include @@ -52,6 +55,7 @@ Arguments makeHMinimaArguments(const DataPath& inputPath, float64 height, bool f } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: integer fractional Height fails preflight", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -73,7 +77,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: integer fractional Heig REQUIRE(preflightResult.outputActions.errors().front().message.find(inputPath.toString()) != std::string::npos); REQUIRE(preflightResult.outputActions.errors().front().message.find(height == 0.5 ? "0.5" : "2.5") != std::string::npos); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves values and type", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { @@ -119,7 +125,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves v } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: floating fractional Height retains fractional minima", "[ImageProcessing][Issue1874][HMinimaImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -157,7 +165,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: floating fractional Hei } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: native 64-bit marker preserves units above double precision", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); @@ -190,7 +200,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: native 64-bit marker pr REQUIRE(outputStore.getValue(0) == inputValue + TestType{1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves native integer bounds", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -226,7 +238,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves n } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height raises or saturates native integer markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -262,7 +276,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height raises or REQUIRE(outputStore.getValue(0) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: large whole Height retains exact native 64-bit markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); @@ -299,7 +315,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: large whole Height reta } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HMinimaImageFilter]") { UnitTest::LoadPlugins(); @@ -334,6 +352,7 @@ TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height is stable under a s } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (2) Hand-computed behavior on a small 2D image: a flat background with two isolated dark dents. The dent @@ -569,53 +588,55 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima [](Arguments& args) { args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(2000.0)); }); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::C15Compatibility resident fractional minima produce watershed markers", "[ImageProcessing][C15Compatibility][Issue1874][HMinimaImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); - DataStructure ds; - const auto input = ip_test::BuildScalarValuesImage(ds, {0, 1, 2, 3, 2, 1, 0}); - c15_test::SetGeometry(ds); + DataStructure dataStructure; + const auto input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2, 3, 2, 1, 0}); + c15_test::SetGeometry(dataStructure); const ShapeType shape{1, 1, 7}; - REQUIRE(c15_test::Read(ds, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); - const auto converted = c15_test::ConvertToFloat32(ds, input); - REQUIRE(c15_test::Read(ds, converted, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + REQUIRE(c15_test::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + const auto converted = c15_test::ConvertToFloat32(dataStructure, input); + REQUIRE(c15_test::Read(dataStructure, converted, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); const HMinimaImageFilter minimaFilter; auto args = c15_test::ImageArguments(minimaFilter, converted, "RaisedMinima"); args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(0.5)); args.insertOrAssign(HMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); - c15_test::Execute(minimaFilter, ds, args, scope); + c15_test::Execute(minimaFilter, dataStructure, args, scope); const auto raised = input.replaceName("RaisedMinima"); const std::vector expectedRaised{0.5F, 1, 2, 3, 2, 1, 0.5F}; - REQUIRE(c15_test::Read(ds, raised, shape, scope) == expectedRaised); + REQUIRE(c15_test::Read(dataStructure, raised, shape, scope) == expectedRaised); const RegionalMinimaImageFilter regional; args = c15_test::ImageArguments(regional, raised, "RegionalMinima"); args.insertOrAssign(RegionalMinimaImageFilter::k_ForegroundValue_Key, std::make_any(1)); args.insertOrAssign(RegionalMinimaImageFilter::k_BackgroundValue_Key, std::make_any(0)); args.insertOrAssign(RegionalMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); args.insertOrAssign(RegionalMinimaImageFilter::k_FlatIsMinima_Key, std::make_any(true)); - c15_test::Execute(regional, ds, args, scope); + c15_test::Execute(regional, dataStructure, args, scope); const auto regionalPath = input.replaceName("RegionalMinima"); - REQUIRE(c15_test::Read(ds, regionalPath, shape, scope) == std::vector{1, 0, 0, 0, 0, 0, 1}); - const auto markers = c15_test::Components(ds, regionalPath, "Markers"); + REQUIRE(c15_test::Read(dataStructure, regionalPath, shape, scope) == std::vector{1, 0, 0, 0, 0, 0, 1}); + const auto markers = c15_test::Components(dataStructure, regionalPath, "Markers"); const std::vector expectedMarkers{1, 0, 0, 0, 0, 0, 2}; - REQUIRE(c15_test::Read(ds, markers, shape, scope) == expectedMarkers); + REQUIRE(c15_test::Read(dataStructure, markers, shape, scope) == expectedMarkers); const MorphologicalWatershedFromMarkersImageFilter watershed; args = c15_test::ImageArguments(watershed, raised, "Basins"); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerImageDataPath_Key, std::make_any(markers)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkWatershedLine_Key, std::make_any(true)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_FullyConnected_Key, std::make_any(false)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerIdConversionHandling_Key, std::make_any(0)); - c15_test::ExecuteChecked(watershed, ds, args); - const auto labels = c15_test::Read(ds, input.replaceName("Basins"), shape, scope); + c15_test::ExecuteChecked(watershed, dataStructure, args); + const auto labels = c15_test::Read(dataStructure, input.replaceName("Basins"), shape, scope); // The unique central ridge separates three cells in each basin. Zero marks the watershed line. REQUIRE(labels == std::vector{1, 1, 1, 0, 2, 2, 2}); REQUIRE(std::count(labels.begin(), labels.end(), uint32{1}) == 3); REQUIRE(std::count(labels.begin(), labels.end(), uint32{2}) == 3); - REQUIRE(c15_test::Read(ds, raised, shape, scope) == expectedRaised); - REQUIRE(c15_test::Read(ds, markers, shape, scope) == expectedMarkers); - REQUIRE(c15_test::Read(ds, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); - c15_test::CheckGeometry(ds, {7, 1, 1}); - UnitTest::CheckArraysInheritTupleDims(ds); -} \ No newline at end of file + REQUIRE(c15_test::Read(dataStructure, raised, shape, scope) == expectedRaised); + REQUIRE(c15_test::Read(dataStructure, markers, shape, scope) == expectedMarkers); + REQUIRE(c15_test::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + c15_test::CheckGeometry(dataStructure, {7, 1, 1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp index 71600c1ca2..212c8b55ad 100644 --- a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp @@ -75,17 +75,22 @@ constexpr float64 maximumIntegerLog10() } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: integer values produce float64 logarithms and domain results", "[NXImageProcessing][Issue1874][Log10ImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); - std::vector values = {1, 10, 100, 2, 0, std::numeric_limits::max()}; - if constexpr(std::is_signed_v) - { - values.push_back(-1); - values.push_back(std::numeric_limits::lowest()); - } + const auto values = [] { + if constexpr(std::is_signed_v) + { + return std::vector{1, 10, 100, 2, 0, std::numeric_limits::max(), -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{1, 10, 100, 2, 0, std::numeric_limits::max()}; + } + }(); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); @@ -129,7 +134,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: integer values produce } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: floating input retains type and IEEE domain behavior", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -179,7 +186,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: floating input retains REQUIRE(std::isnan(store.getValue(9))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating boundaries have finite ordered logarithms", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -219,7 +228,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating bound REQUIRE(store.getValue(5) > 0); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::Log10ImageFilter: promoted Float64 output preserves tuple and component shapes", "[NXImageProcessing][Issue1874][Log10ImageFilter]") { UnitTest::LoadPlugins(); @@ -273,6 +284,7 @@ TEST_CASE("NXImageProcessing::Log10ImageFilter: promoted Float64 output preserve REQUIRE(outputStore.getValue(3) == Approx(0.30102999566398119521).epsilon(0).margin(1.0e-15)); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // Negative input produces NaN; zero produces negative infinity. Floating output represents both results. TEST_CASE("NXImageProcessing::Log10ImageFilter: log10 of negative is NaN and log10(0) is -inf (domain edge)", "[NXImageProcessing][Log10ImageFilter]") diff --git a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp index 03b5330f2f..103b3af799 100644 --- a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -40,20 +41,28 @@ DataPath runMappedIntegerLog(DataStructure& dataStructure, const DataPath& input args.insertOrAssign(LogImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(LogImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); // Stop before zero or negative integer input can produce an unsafe nonfinite-to-integer cast. + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::float64); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == componentShape); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); return input.replaceName("Output"); } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce float64 natural logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -61,12 +70,16 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce fl const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); constexpr int32 highestPower = std::numeric_limits::digits - 1; constexpr TestType highestPowerOfTwo = static_cast(TestType{1} << highestPower); - std::vector values = {1, 2, 0, std::numeric_limits::max(), highestPowerOfTwo}; - if constexpr(std::is_signed_v) - { - values.push_back(-1); - values.push_back(std::numeric_limits::lowest()); - } + const auto values = [] { + if constexpr(std::is_signed_v) + { + return std::vector{1, 2, 0, std::numeric_limits::max(), highestPowerOfTwo, -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{1, 2, 0, std::numeric_limits::max(), highestPowerOfTwo}; + } + }(); const ShapeType tupleShape = {1, 1, values.size()}; DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); @@ -84,7 +97,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce fl REQUIRE(std::isinf(store.getValue(2))); REQUIRE(std::signbit(store.getValue(2))); // 2^(digits-1) <= maximum < 2^digits. Rounding can make the upper logarithm equal for 64-bit types. - constexpr float64 logTwo = 0.69314718055994530942; + constexpr float64 logTwo = std::numbers::ln2; constexpr float64 lowerBound = highestPower * logTwo; constexpr float64 upperBound = std::numeric_limits::digits * logTwo; REQUIRE(std::isfinite(store.getValue(3))); @@ -98,7 +111,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce fl } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -149,7 +164,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: floating input retains ty REQUIRE(std::isnan(store.getValue(9))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundaries retain finite logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -167,7 +184,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundar REQUIRE_NOTHROW(logarithmsPtr = &dataStructure.getDataRefAs>(output)); const auto& store = logarithmsPtr->getDataStoreRef(); // IEEE binary types have minimum normal 2^(min_exponent-1) and minimum subnormal 2^(min_exponent-digits). - constexpr float64 logTwo = 0.69314718055994530942; + constexpr float64 logTwo = std::numbers::ln2; constexpr float64 normalLogarithm = (std::numeric_limits::min_exponent - 1) * logTwo; constexpr float64 subnormalLogarithm = (std::numeric_limits::min_exponent - std::numeric_limits::digits) * logTwo; constexpr float64 logarithmTolerance = std::is_same_v ? 1.0e-5 : 1.0e-12; @@ -179,7 +196,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundar REQUIRE(store.getValue(3) > TestType{0}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::LogImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][LogImageFilter]") { UnitTest::LoadPlugins(); @@ -216,6 +235,7 @@ TEST_CASE("NXImageProcessing::LogImageFilter: mapped multi-component output pres } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // Floating domain behavior: log(x<0) is NaN and log(0) is -infinity. TEST_CASE("NXImageProcessing::LogImageFilter: log of negative is NaN and log(0) is -inf (domain edge)", "[NXImageProcessing][LogImageFilter]") diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index 4d18f672fb..207999e62b 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -69,11 +69,13 @@ Arguments createMarkerLineArguments(DataStructure& dataStructure, const std::vec const auto inputPath = cellPath.createChildPath("Input"); const auto markerPath = cellPath.createChildPath("Marker"); auto* geometryPtr = ImageGeom::Create(dataStructure, geometryPath.getTargetName()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(geometryPtr != nullptr); geometryPtr->setDimensions({markers.size(), 1, 1}); geometryPtr->setSpacing({1.0F, 1.0F, 1.0F}); geometryPtr->setOrigin({0.0F, 0.0F, 0.0F}); auto* cellDataPtr = AttributeMatrix::Create(dataStructure, cellPath.getTargetName(), tupleShape, geometryPtr->getId()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(cellDataPtr != nullptr); geometryPtr->setCellData(*cellDataPtr); @@ -85,22 +87,32 @@ Arguments createMarkerLineArguments(DataStructure& dataStructure, const std::vec { markerStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, markerPath, tupleShape, componentShape); } + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputStorePtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(markerStorePtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputStorePtr->getStoreType() == IDataStore::StoreType::InMemory); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(markerStorePtr->getStoreType() == IDataStore::StoreType::InMemory); auto* inputArrayPtr = Float32Array::Create(dataStructure, inputPath.getTargetName(), inputStorePtr, cellDataPtr->getId()); auto* markerArrayPtr = DataArray::Create(dataStructure, markerPath.getTargetName(), markerStorePtr, cellDataPtr->getId()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(markerArrayPtr != nullptr); for(usize index = 0; index < markers.size(); ++index) { inputStorePtr->setValue(index, 0.0F); markerStorePtr->setValue(index, markers.at(index)); } + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr->getTupleShape() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(markerArrayPtr->getTupleShape() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr->getComponentShape() == componentShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(markerArrayPtr->getComponentShape() == componentShape); Arguments args; @@ -121,12 +133,18 @@ Arguments createSinglePixelMarkerArguments(DataStructure& dataStructure, uint64 void requireSinglePixelMarkerPreflight(const WSFilter& filter, const DataStructure& dataStructure, const Arguments& args) { const auto result = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(result.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::uint64); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == ShapeType{1, 1, 1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == ShapeType{1}); } @@ -135,9 +153,13 @@ void requireSinglePixelMarkerOutput(const DataStructure& dataStructure) const DataPath outputPath({"Image Geometry", "CellData", "Output"}); const IDataArray* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataType() == DataType::uint64); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getTupleShape() == ShapeType{1, 1, 1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getComponentShape() == ShapeType{1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getStoreType() == IDataStore::StoreType::InMemory); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -150,12 +172,18 @@ void requireMarkerLine(const std::vector& markers, const std::vector& expe args.insertOrAssign(WSFilter::k_MarkerIdConversionHandling_Key, std::make_any(handling)); const WSFilter filter; const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == GetDataType()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == ShapeType{1, 1, markers.size()}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == ShapeType{1}); bool enteredFlood = false; const IFilter::MessageHandler handler{[&enteredFlood](const IFilter::Message& message) { enteredFlood |= message.message == "Computing Watershed From Markers"; }}; @@ -163,33 +191,47 @@ void requireMarkerLine(const std::vector& markers, const std::vector& expe UnitTest::CheckArraysInheritTupleDims(dataStructure); if(handling == 0 && changedCount != 0) { + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().size() == 1); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == -79073); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().message.find("Image Geometry/CellData/Marker") != std::string::npos); REQUIRE_FALSE(enteredFlood); return; } + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(enteredFlood); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.warnings().size() == (changedCount == 0 ? 0 : 1)); if(changedCount != 0) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.warnings().front().code == 79074); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.warnings().front().message.find(std::to_string(changedCount) + " changed marker occurrence(s)") != std::string::npos); } const DataPath outputPath({"Image Geometry", "CellData", "Output"}); const IDataArray* outputBasePtr = nullptr; REQUIRE_NOTHROW(outputBasePtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputBasePtr->getDataType() == GetDataType()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputBasePtr->getTupleShape() == ShapeType{1, 1, markers.size()}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputBasePtr->getComponentShape() == ShapeType{1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputBasePtr->getStoreType() == IDataStore::StoreType::InMemory); const DataArray* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs>(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(expected.size() == markers.size()); for(usize index = 0; index < expected.size(); ++index) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataStoreRef().getValue(index) == expected.at(index)); } } @@ -401,6 +443,7 @@ std::vector MakeNegativeMarkerField(usize dx, usize dy, usize dz) } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker conversion policy defaults to Error", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); @@ -426,7 +469,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker REQUIRE(tooltip.find(termPtr) != std::string::npos); } } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: default Error rejects a changed uint64 marker before flooding", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -457,6 +502,7 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: defaul REQUIRE(result.result.errors().front().message.find("4294967297") != std::string::npos); REQUIRE_FALSE(enteredFlood); } +// NOLINTEND(bugprone-chained-comparison) TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32_MAX marker remains valid with uint64 output", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") @@ -472,11 +518,13 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32 const WSFilter filter; requireSinglePixelMarkerPreflight(filter, dataStructure, args); const auto result = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireSinglePixelMarkerOutput(dataStructure); const UInt64Array* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(DataPath({"Image Geometry", "CellData", "Output"}))); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataStoreRef().getValue(0) == uint64{4294967295ULL}); } @@ -541,6 +589,7 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias requireMarkerLine({4294967297ULL, 0, 4294967297ULL}, {1, 1, 1}, handling, markWatershedLine, fullyConnected, 2); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker copy checks chunk boundaries before writing", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -555,7 +604,7 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker DataStructure dataStructure; createMarkerLineArguments(dataStructure, markers, true, false, {}, markerStorePtr); MarkerTransferStore destination(valueCount); - std::atomic_bool shouldCancel = false; + const std::atomic_bool shouldCancel = false; const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, handling); const std::string* messagePtr = nullptr; if(handling == ImageProcessing::MarkerIdConversionHandling::Error) @@ -577,11 +626,13 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker REQUIRE(messagePtr->find("flat index " + std::to_string(changedIndex)) != std::string::npos); REQUIRE(messagePtr->find("native ID 4294967297 restores as 1") != std::string::npos); REQUIRE(messagePtr->find("1 changed marker occurrence(s)") != std::string::npos); - REQUIRE(markerStorePtr->readCount() == (handling == ImageProcessing::MarkerIdConversionHandling::Error ? changedIndex / 65536 + 1 : 2)); + REQUIRE(markerStorePtr->readCount() == (handling == ImageProcessing::MarkerIdConversionHandling::Error ? (changedIndex / 65536) + 1 : 2)); REQUIRE(markerStorePtr->largestRead() <= 65536); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invalid policy is rejected before execution", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); @@ -597,7 +648,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invali REQUIRE(dataStructure.getData(DataPath({"Image Geometry", "CellData", "Output"})) == nullptr); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: diagnostic context keeps exact 64-bit extrema", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) { @@ -630,7 +683,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilte REQUIRE(messagePtr->find("First change at flat index 1") != std::string::npos); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy cancellation retains observed warning only", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -657,7 +712,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy c REQUIRE(destination.writeCount() == 1); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy warning survives transfer failure or allocation failure", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -687,7 +744,7 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy w markerStorePtr->failRead(2, true); expectedCode = -79045; } - std::atomic_bool shouldCancel = false; + const std::atomic_bool shouldCancel = false; const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); SIMPLNX_RESULT_REQUIRE_INVALID(result); @@ -700,7 +757,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy w REQUIRE(destination.writeCount() == 1); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conversion warning survives later facade outcomes", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -760,7 +819,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conver REQUIRE(result.warnings().front().code == 79074); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: successful transfer warnings survive later marker-copy failure", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -789,7 +850,9 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: succes REQUIRE(destination.writeCount() == 1); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restoration retains transfer warnings through a short final chunk", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { @@ -835,6 +898,7 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restor REQUIRE(outputStorePtr->writeCount() == 2); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (4) Preflight guards: a multi-component grayscale is rejected (requireScalar); a non-integer (float32) marker is diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 045ad812bf..7c85f49ce2 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -33,12 +33,14 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -123,6 +125,7 @@ void RunCompactInt16Otsu(DataStructure& ds, const DataPath& inputPath) } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); @@ -140,7 +143,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy const Arguments defaultArguments = filter.getDefaultArguments(); REQUIRE(defaultArguments.value(policyKey) == 0); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask selection is optional and linked", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); @@ -164,7 +169,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask sel REQUIRE(parameters.containsGroup(useMaskKey)); const std::vector linkedKeys = parameters.getKeysInGroup(useMaskKey); - REQUIRE(std::find(linkedKeys.begin(), linkedKeys.end(), maskPathKey) != linkedKeys.end()); + REQUIRE(std::ranges::find(linkedKeys, maskPathKey) != linkedKeys.end()); // A Masked Label control may also belong to this group. Its presence must not break this test. Arguments arguments = filter.getDefaultArguments(); REQUIRE_FALSE(arguments.value(useMaskKey)); @@ -172,7 +177,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask sel arguments.insertOrAssign(useMaskKey, std::make_any(true)); REQUIRE(parameters.isParameterActive(maskPathKey, arguments)); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has a separate uint8 control", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); @@ -188,7 +195,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has const Arguments defaultArguments = filter.getDefaultArguments(); REQUIRE(defaultArguments.value(maskedLabelKey) == uint8{255}); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: preflight checks the complete configured uint8 class interval", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { @@ -287,6 +296,7 @@ void requireOtsuLabels(const DataStructure& dataStructure, const DataPath& input REQUIRE(actual == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) template class OtsuTransferStore : public DataStore @@ -382,6 +392,7 @@ class OtsuTransferStore : public DataStore }; } // namespace +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains literal endpoint frequencies and labels", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -389,8 +400,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains lite const usize rotation = GENERATE(usize{0}, usize{1}, usize{4}); std::vector values{0, 0, 4, 4, std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}; std::vector expected{7, 7, 8, 8, 7, 7, 8}; - std::rotate(values.begin(), values.begin() + static_cast::difference_type>(rotation), values.end()); - std::rotate(expected.begin(), expected.begin() + static_cast::difference_type>(rotation), expected.end()); + std::ranges::rotate(values, values.begin() + static_cast::difference_type>(rotation)); + std::ranges::rotate(expected, expected.begin() + static_cast::difference_type>(rotation)); DataStructure dataStructure; const DataPath inputPath = recon_test::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); const Float64Array* inputPtr = nullptr; @@ -416,6 +427,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains lite REQUIRE(result.result.warnings().front().message.find("unreliable") != std::string::npos); requireOtsuLabels(dataStructure, inputPath, expected); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { @@ -426,11 +438,14 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects eac const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, value, 4}); const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, otsuArguments(inputPath)); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == -28654); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", bool, uint8) { @@ -463,6 +478,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask pre REQUIRE(result.result.warnings().empty()); requireOtsuLabels(dataStructure, inputPath, {7, 7, 8, 8, maskedLabel, maskedLabel, maskedLabel}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite data is required in both modes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { @@ -478,7 +494,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite d args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == -28655); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -523,11 +541,14 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask c } const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.outputActions.errors().front().code == expectedCode); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", int64, uint64) { @@ -551,7 +572,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: full nat SIMPLNX_RESULT_REQUIRE_VALID(result.result); requireOtsuLabels(dataStructure, inputPath, {0, 1}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants and detectable numeric collapse are distinct", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -619,19 +642,21 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants an REQUIRE(result.value() == std::vector{2}); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer ring preserves full payload batches at its grant boundary", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuRingGrant]") { // The counting store exercises charged scan buffers and records caller transfers. It has no disk backing. - constexpr usize k_ValueCount = 16; - constexpr uint64 k_PayloadBytes = k_ValueCount * sizeof(uint16); - constexpr uint64 k_RingBytes = 65536ULL * sizeof(ImageProcessing::detail::OtsuFrequencyEntry); - const uint64 grant = GENERATE_COPY(k_RingBytes - 1, k_RingBytes, k_RingBytes + k_PayloadBytes); + constexpr usize valueCount = 16; + constexpr uint64 payloadBytes = valueCount * sizeof(uint16); + constexpr uint64 ringBytes = 65536ULL * sizeof(ImageProcessing::detail::OtsuFrequencyEntry); + const uint64 grant = GENERATE_COPY(ringBytes - 1, ringBytes, ringBytes + payloadBytes); CAPTURE(grant); - OtsuTransferStore input(k_ValueCount, IDataStore::StoreType::OutOfCore); - for(usize i = 1; i < k_ValueCount; i += 2) + OtsuTransferStore input(valueCount, IDataStore::StoreType::OutOfCore); + for(usize i = 1; i < valueCount; i += 2) { input.setValue(i, uint16{4}); } @@ -655,12 +680,14 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer REQUIRE(result.value().HistogramData.counts == std::vector{8, 8}); REQUIRE(budget.reservedWorkingMemoryBytes() == reservedWithPressure); // The grant must fit both resources for a single pass. The fallback reads one full buffer per pass. - CHECK(input.maximumRead() == k_ValueCount); - CHECK(input.reads() == (grant == k_RingBytes + k_PayloadBytes ? 1 : 2)); + CHECK(input.maximumRead() == valueCount); + CHECK(input.reads() == (grant == ringBytes + payloadBytes ? 1 : 2)); } REQUIRE(budget.reservedWorkingMemoryBytes() == reservedBefore); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -748,6 +775,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers } } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // ----------------------------------------------------------------------------- // (3) Validation: a data-independent guard must reject a threshold count that is 0 or >= the bin count @@ -1013,6 +1041,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: ITK real-image "fb65e730472c8001185f355fb626ca3f"); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeline imports receive intentional defaults", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -1040,7 +1069,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeli REQUIRE_FALSE(legacyResult.value().value("use_mask_array")); REQUIRE(legacyResult.value().value("masked_label") == 255); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { @@ -1079,7 +1110,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection va } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep explicit Warn endpoints and empty selected data errors", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { @@ -1110,7 +1143,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep ex SIMPLNX_RESULT_REQUIRE_INVALID(emptyResult); REQUIRE(emptyResult.errors().front().code == -28655); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -1179,6 +1214,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper co REQUIRE(result.errors().front().code == -28657); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal class labels coexist with masked label zero", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { @@ -1194,11 +1230,13 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal cla args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(0)); const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireOtsuLabels(dataStructure, inputPath, {254, 255, 0}); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds keep ordinary Warn endpoint classes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -1224,7 +1262,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds ke REQUIRE(result.result.warnings().size() == 1); requireOtsuLabels(dataStructure, inputPath, {7, 8, 9, 7, 7, 9}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid may equal minimum with usable two-bin separation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuCentroidSafeRed]") { @@ -1270,14 +1310,16 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid ma REQUIRE(outputPtr->getComponentShape() == ShapeType{1}); requireOtsuLabels(dataStructure, inputPath, {0, 1}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summaries preserve literal counts across resident and buffered batches", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); const bool excludeNonfinite = GENERATE(false, true); - const usize targetBytes = GENERATE(usize{4096}, usize{64 * 1024 * 1024}); + const usize targetBytes = GENERATE(usize{4096}, usize{64} * 1024 * 1024); constexpr usize valueCount = 524296; // 65,537 periods; spans multiple 262,144-value reduction blocks plus a short tail. std::unique_ptr> inputPtr; if(buffered) @@ -1325,7 +1367,9 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summarie } // These are output/transfer-bound assertions. No thread-count or throughput claim follows from them. } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds selected components", "[ImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -1336,11 +1380,11 @@ TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds const auto inf = std::numeric_limits::infinity(); const std::vector original = includeNonfinite ? std::vector{0, 0, 4, 4, nan, -inf, inf} : std::vector{0, 0, 4, 4}; const ShapeType shape{1, 1, original.size()}; - DataStructure ds; - const auto input = ip_test::BuildScalarValuesImage(ds, original); - const auto validity = c15_test::Select(ds, input, 0, 4, "Validity"); + DataStructure dataStructure; + const auto input = ip_test::BuildScalarValuesImage(dataStructure, original); + const auto validity = c15_test::Select(dataStructure, input, 0, 4, "Validity"); const std::vector expectedValidity = includeNonfinite ? std::vector{1, 1, 1, 1, 0, 0, 0} : std::vector{1, 1, 1, 1}; - REQUIRE(c15_test::Read(ds, validity, shape, expectedStore) == expectedValidity); + REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == expectedValidity); const OtsuMultipleThresholdsImageFilter filter; auto args = c15_test::ImageArguments(filter, input, "Classes"); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(2)); @@ -1352,21 +1396,21 @@ TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(0)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ValleyEmphasis_Key, std::make_any(false)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ReturnBinMidpoint_Key, std::make_any(false)); - c15_test::ExecuteChecked(filter, ds, args); + c15_test::ExecuteChecked(filter, dataStructure, args); const auto classes = input.replaceName("Classes"); const std::vector expectedClasses = includeNonfinite ? std::vector{7, 7, 8, 8, 255, 255, 255} : std::vector{7, 7, 8, 8}; - REQUIRE(c15_test::Read(ds, classes, shape, expectedStore) == expectedClasses); - const auto selected = c15_test::Select(ds, classes, 8, 8, "DesiredClass"); + REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == expectedClasses); + const auto selected = c15_test::Select(dataStructure, classes, 8, 8, "DesiredClass"); const std::vector expectedSelected = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; - REQUIRE(c15_test::Read(ds, selected, shape, expectedStore) == expectedSelected); - const auto labels = c15_test::Components(ds, selected, "Components"); + REQUIRE(c15_test::Read(dataStructure, selected, shape, expectedStore) == expectedSelected); + const auto labels = c15_test::Components(dataStructure, selected, "Components"); const std::vector expectedLabels = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; - const auto actualLabels = c15_test::Read(ds, labels, shape, expectedStore); + const auto actualLabels = c15_test::Read(dataStructure, labels, shape, expectedStore); REQUIRE(actualLabels == expectedLabels); REQUIRE(std::count(actualLabels.begin(), actualLabels.end(), uint32{1}) == 2); - REQUIRE(c15_test::Read(ds, classes, shape, expectedStore) == expectedClasses); - REQUIRE(c15_test::Read(ds, validity, shape, expectedStore) == expectedValidity); - const auto values = c15_test::Read(ds, input, shape, expectedStore); + REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == expectedClasses); + REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == expectedValidity); + const auto values = c15_test::Read(dataStructure, input, shape, expectedStore); REQUIRE(std::vector(values.begin(), values.begin() + 4) == std::vector{0, 0, 4, 4}); if(includeNonfinite) { @@ -1374,5 +1418,6 @@ TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds REQUIRE(values[5] == -inf); REQUIRE(values[6] == inf); } - UnitTest::CheckArraysInheritTupleDims(ds); -} \ No newline at end of file + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 6241ef5244..8784361705 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -63,8 +63,11 @@ void requireSmoothingInput(const DataStructure& dataStructure, const DataPath& i { const IDataArray* inputArrayPtr = nullptr; REQUIRE_NOTHROW(inputArrayPtr = &dataStructure.getDataRefAs(inputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr->getDataType() == DataType::float32); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr->getTupleShape() == ShapeType{dimZ, 4, 4}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr->getComponentShape() == ShapeType{1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -218,6 +221,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite act const SmoothFilter filter; const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); // This deliberately stops at preflight. Invalid coefficients must never reach an unchecked output conversion. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -240,6 +244,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite act dataStructure.getDataRefAs(geometryPath).setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); const SmoothFilter filter; const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, 1.0})); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -265,6 +270,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effec const SmoothFilter filter; const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); // Positive finite operands can produce zero or infinity for sigma / abs(spacing). No cutoff on finite scales is imposed. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -283,10 +289,12 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negati dataStructure.getDataRefAs(geometryPath).setSpacing({-1.0F, -2.0F, unusedSpacing}); const SmoothFilter filter; const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, unusedSigma})); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected validation branch has a diagnostic", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); @@ -329,7 +337,9 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected REQUIRE(preflight.outputActions.errors().front().code == expectedCodes.at(failure)); REQUIRE(preflight.outputActions.errors().front().message.find("Image Geometry") != std::string::npos); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: constants retain type shape and selected endpoint behavior", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int8, int16, int32, int64, float32, float64) { @@ -380,7 +390,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: cons } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 output retains float32 interpass precision", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); @@ -403,7 +415,9 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 outpu } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and buffered terminal conversion follow literal expectations", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { @@ -423,7 +437,9 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and bu REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == (fixture == 2 || fixture == 4 ? 1 : 0)); REQUIRE(output.getValue(0) == expected.at(fixture)); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: floating terminal values retain nonfinite behavior", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", float32, float64) { @@ -448,7 +464,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: floa REQUIRE(output.getValue(2) < TestType{0}); REQUIRE(std::isnan(output.getValue(3))); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: terminal nonfinite integer errors and binary64 boundaries", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int32, int64) { @@ -479,7 +497,9 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: term REQUIRE(output.getValue(1) == std::numeric_limits::lowest()); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warning survives later transfer failure and cancellation", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { @@ -507,7 +527,9 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warn REQUIRE(shouldCancel.load()); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plane sinks retain successful warnings before error or cancellation", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { @@ -544,7 +566,9 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plan REQUIRE(result.errors().front().code == -23694); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tiled transfers keep prior row warnings on failure", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); @@ -557,6 +581,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tile REQUIRE(result.errors().front().code == (write ? -23691 : -23690)); REQUIRE(countSmoothingWarning(result, write ? -23693 : -23692) == 1); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis materializations narrow finite overflow explicitly", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") @@ -572,10 +597,12 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis options.stagingByteBudget = 256; const std::atomic_bool shouldCancel{false}; const Result<> result = ImageProcessing::RecursiveGaussianAxisPass(source, output, SizeVec3{5, 5, 5}, axis, 1.0, 1.0, 0, false, shouldCancel, {}, options); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result); for(usize index = 0; index < output.getSize(); ++index) { REQUIRE(std::isinf(output.getValue(index))); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(output.getValue(index) > 0.0F); } } @@ -738,6 +765,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); const auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSigma); } SECTION("degenerate spacing rejected") @@ -756,6 +784,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); const auto preflightResult = filter.preflight(ds, args); SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSpacing); } } @@ -981,6 +1010,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: ITK real-im UnitTest::CheckArraysInheritTupleDims(ds); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", "[ImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") { @@ -989,7 +1019,7 @@ TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussian UnitTest::AlgorithmTestScope scope(scenario); const bool step = GENERATE(false, true); CAPTURE(step, scenario); - std::vector original(16 * 4, 4); + std::vector original(usize{16} * 4, 4); if(step) { for(usize index = 0; index < original.size(); ++index) @@ -997,28 +1027,29 @@ TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussian original[index] = index % 16 < 8 ? 0 : 1; } } - DataStructure ds; - const auto input = recon_test::BuildImageFromPattern(ds, 16, 4, 1, original); - c15_test::SetGeometry(ds); - REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); + DataStructure dataStructure; + const auto input = recon_test::BuildImageFromPattern(dataStructure, 16, 4, 1, original); + c15_test::SetGeometry(dataStructure); + REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); const SmoothingRecursiveGaussianImageFilter filter; auto args = c15_test::ImageArguments(filter, input, "Smoothed"); args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_Sigma_Key, std::make_any>(std::vector{1, 1, 1})); args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); - c15_test::Execute(filter, ds, args, scope); + c15_test::Execute(filter, dataStructure, args, scope); const auto smoothed = input.replaceName("Smoothed"); - const auto values = c15_test::Read(ds, smoothed, {1, 4, 16}, scope); + const auto values = c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope); const GradientMagnitudeImageFilter gradient; args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); - c15_test::Execute(gradient, ds, args, scope); - const auto edges = c15_test::Read(ds, input.replaceName("Gradient"), {1, 4, 16}, scope); - for(usize y = 0; y < 4; ++y) + c15_test::Execute(gradient, dataStructure, args, scope); + const auto edges = c15_test::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); + for(usize row = 0; row < 4; ++row) { - for(usize x = 0; x < 16; ++x) + const usize rowOffset = row * 16; + for(usize column = 0; column < 16; ++column) { - const usize index = y * 16 + x; - CAPTURE(x, y); + const usize index = rowOffset + column; + CAPTURE(column, row); REQUIRE(std::isfinite(values[index])); REQUIRE(std::isfinite(edges[index])); if(!step) @@ -1029,18 +1060,20 @@ TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussian else { // Reflection/complement and row invariance are independent of the recursive implementation. - REQUIRE(values[index] + values[y * 16 + 15 - x] == Approx(1).margin(1.0e-5)); - REQUIRE(values[index] == Approx(values[x]).margin(1.0e-5)); - REQUIRE(edges[index] == Approx(edges[y * 16 + 15 - x]).margin(1.0e-5)); - REQUIRE(edges[index] == Approx(edges[x]).margin(1.0e-5)); - REQUIRE(edges[index] <= edges[y * 16 + 7] + 1.0e-5F); - REQUIRE(edges[y * 16 + 7] > 0); - REQUIRE(edges[y * 16 + 7] == Approx(edges[y * 16 + 8]).margin(1.0e-5)); + const usize reflectedIndex = rowOffset + 15 - column; + REQUIRE(values[index] + values[reflectedIndex] == Approx(1).margin(1.0e-5)); + REQUIRE(values[index] == Approx(values[column]).margin(1.0e-5)); + REQUIRE(edges[index] == Approx(edges[reflectedIndex]).margin(1.0e-5)); + REQUIRE(edges[index] == Approx(edges[column]).margin(1.0e-5)); + REQUIRE(edges[index] <= edges[rowOffset + 7] + 1.0e-5F); + REQUIRE(edges[rowOffset + 7] > 0); + REQUIRE(edges[rowOffset + 7] == Approx(edges[rowOffset + 8]).margin(1.0e-5)); } } } - REQUIRE(c15_test::Read(ds, smoothed, {1, 4, 16}, scope) == values); - REQUIRE(c15_test::Read(ds, input, {1, 4, 16}, scope) == original); - c15_test::CheckGeometry(ds, {16, 4, 1}); - UnitTest::CheckArraysInheritTupleDims(ds); -} \ No newline at end of file + REQUIRE(c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); + c15_test::CheckGeometry(dataStructure, {16, 4, 1}); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp index 69f599d96c..05f4285ca8 100644 --- a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp @@ -40,31 +40,43 @@ DataPath runMappedIntegerSqrt(DataStructure& dataStructure, const DataPath& inpu args.insertOrAssign(SqrtImageFilter::k_InputImageDataPath_Key, std::make_any(input)); args.insertOrAssign(SqrtImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); // Stop before negative integer input can produce an unsafe NaN-to-integer cast. + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::float64); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == componentShape); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); return input.replaceName("Output"); } } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: integer values produce float64 square roots", "[NXImageProcessing][Issue1874][SqrtImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); - std::vector values = {0, 2, 4, std::numeric_limits::max()}; - if constexpr(std::is_signed_v) - { - values.push_back(-1); - values.push_back(std::numeric_limits::lowest()); - } + const auto values = [] { + if constexpr(std::is_signed_v) + { + return std::vector{0, 2, 4, std::numeric_limits::max(), -1, std::numeric_limits::lowest()}; + } + else + { + return std::vector{0, 2, 4, std::numeric_limits::max()}; + } + }(); const ShapeType tupleShape = {1, 1, values.size()}; DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, values); @@ -124,7 +136,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: integer values produce f } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -178,7 +192,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating input retains t REQUIRE(std::isnan(store.getValue(11))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating boundary roots match independent binary values", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -232,7 +248,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating boundary roots REQUIRE(store.getValue(8) == maximumRoot); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SqrtImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SqrtImageFilter]") { UnitTest::LoadPlugins(); @@ -268,6 +286,7 @@ TEST_CASE("NXImageProcessing::SqrtImageFilter: mapped multi-component output pre } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // Negative floating input produces a representable NaN. TEST_CASE("NXImageProcessing::SqrtImageFilter: sqrt of a negative is NaN (domain edge)", "[NXImageProcessing][SqrtImageFilter]") diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 86dd3442f2..8ab932972d 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -4,6 +4,7 @@ #include "UnaryFilterTestUtils.hpp" #include "NXImageProcessing/Filters/SquareImageFilter.hpp" +#include "NXImageProcessing/Filters/WriteImageFilter.hpp" #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" @@ -13,11 +14,14 @@ #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStructure.hpp" +#include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Filter/Arguments.hpp" +#include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/Parsing/DREAM3D/Dream3dIO.hpp" #include @@ -41,22 +45,32 @@ DataPath runSquareWithType(DataStructure& dataStructure, const DataPath& inputPa args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const SquareImageFilter filter; const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); // A hard preflight guard stops the old same-width output before any narrowing overflow or typed lookup. + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == expectedType); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == componentShape); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); const DataPath outputPath = inputPath.replaceName("Output"); const IDataArray* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataType() == expectedType); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getComponentShape() == componentShape); return outputPath; } @@ -64,6 +78,7 @@ DataPath runSquareWithType(DataStructure& dataStructure, const DataPath& inputPa template void requireSquareValues(const std::vector& values, const std::vector& expected) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(values.size() == expected.size()); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, values); @@ -73,6 +88,7 @@ void requireSquareValues(const std::vector& values, const std::vector const auto& store = outputArrayPtr->getDataStoreRef(); for(usize index = 0; index < expected.size(); ++index) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(store.getValue(index) == expected.at(index)); } UnitTest::CheckArraysInheritTupleDims(dataStructure); @@ -89,11 +105,17 @@ void requireSquareOverflow(InputType value, int32 expectedCode) args.insertOrAssign(SquareImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const SquareImageFilter filter; const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(executeResult.result.errors().size() == 1); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(executeResult.result.errors().front().code == expectedCode); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(executeResult.result.errors().front().message.find(inputPath.toString()) != std::string::npos); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(executeResult.result.errors().front().message.find(std::to_string(value)) != std::string::npos); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(executeResult.result.errors().front().message.find("float64") != std::string::npos); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -182,6 +204,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: empty 64-bit inputs re requireSquareValues({}, {}); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SquareImageFilter: cancellation preserves the filter cancellation result", "[NXImageProcessing][Issue1874][SquareImageFilter]") { UnitTest::LoadPlugins(); @@ -200,7 +223,9 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: cancellation preserves the filt REQUIRE(executeResult.result.errors().front().code == -1); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SquareImageFilter: widened multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SquareImageFilter]") { UnitTest::LoadPlugins(); @@ -229,7 +254,9 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: widened multi-component output } UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating inputs retain type and safe IEEE values", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -252,7 +279,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating inputs retain REQUIRE(std::isnan(store.getValue(6))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating overflow is positive infinity and underflow is positive zero", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -272,6 +301,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating overflow is p REQUIRE_FALSE(std::signbit(store.getValue(3))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison) // ----------------------------------------------------------------------------- // ITK-sourced real-image md5 golden (plan Sec.3/Sec.4/Sec.6). Mirrors ITKSquareImageTest.cpp(defaults): read @@ -309,7 +339,8 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: ITK real-image golden (defaults UnitTest::CheckArraysInheritTupleDims(ds); } -TEST_CASE("ImageProcessing::C15Compatibility resident widened Square persists and feeds integer components", "[ImageProcessing][C15Compatibility][Issue1874][SquareImageFilter]") +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. +TEST_CASE("NXImageProcessing::C15Compatibility resident widened Square persists and feeds integer components", "[NXImageProcessing][C15Compatibility][Issue1874][SquareImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -336,11 +367,11 @@ TEST_CASE("ImageProcessing::C15Compatibility resident widened Square persists an c15_test::Execute(writer, reloaded, c15_test::WriteArguments(square, output.path() / "square.mha"), scope); auto raw = c15_test::ReadMha(output.path() / "square.mha"); REQUIRE(c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 4}, scope) == expected); - const ImageGeom* rawGeometry = nullptr; - REQUIRE_NOTHROW(rawGeometry = &raw.getDataRefAs(DataPath({"Image Geometry"}))); - REQUIRE(rawGeometry->getDimensions() == SizeVec3{4, 1, 1}); - REQUIRE(rawGeometry->getSpacing() == FloatVec3{1, 2, 1}); - REQUIRE(rawGeometry->getOrigin() == FloatVec3{4, 5, 0}); + const ImageGeom* rawGeometryPtr = nullptr; + REQUIRE_NOTHROW(rawGeometryPtr = &raw.getDataRefAs(DataPath({"Image Geometry"}))); + REQUIRE(rawGeometryPtr->getDimensions() == SizeVec3{4, 1, 1}); + REQUIRE(rawGeometryPtr->getSpacing() == FloatVec3{1, 2, 1}); + REQUIRE(rawGeometryPtr->getOrigin() == FloatVec3{4, 5, 0}); const auto labels = c15_test::Components(reloaded, square, "Components"); REQUIRE(c15_test::Read(reloaded, labels, {1, 1, 4}, scope) == std::vector{0, 1, 0, 2}); REQUIRE(c15_test::Read(reloaded, square, {1, 1, 4}, scope) == expected); @@ -348,4 +379,5 @@ TEST_CASE("ImageProcessing::C15Compatibility resident widened Square persists an REQUIRE(c15_test::Read(source, input, {1, 1, 4}, scope) == original); UnitTest::CheckArraysInheritTupleDims(source); UnitTest::CheckArraysInheritTupleDims(reloaded); -} \ No newline at end of file +} +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) diff --git a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp index 10f2a1864c..3e6a69de51 100644 --- a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp @@ -31,6 +31,7 @@ using namespace nx::core; +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: integer radians produce signed fractional float32 responses", "[NXImageProcessing][Issue1874][TanImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { @@ -102,7 +103,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: integer radians produce s UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: floating responses retain type signed zero and finite near-pole values", "[NXImageProcessing][Issue1874][TanImageFilter]", float32, float64) { UnitTest::LoadPlugins(); @@ -165,6 +168,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: floating responses retain UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("NXImageProcessing::TanImageFilter: integer vector tuples preserve shape and component order", "[NXImageProcessing][Issue1874][TanImageFilter]") { @@ -175,15 +179,19 @@ TEST_CASE("NXImageProcessing::TanImageFilter: integer vector tuples preserve sha const ShapeType tupleShape = {1, 2, 2}; const ShapeType componentShape = {2}; auto* imageGeomPtr = ImageGeom::Create(dataStructure, "Image Geometry"); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(imageGeomPtr != nullptr); imageGeomPtr->setDimensions({2, 2, 1}); auto* cellDataPtr = AttributeMatrix::Create(dataStructure, "CellData", tupleShape, imageGeomPtr->getId()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(cellDataPtr != nullptr); imageGeomPtr->setCellData(*cellDataPtr); const DataPath inputPath({"Image Geometry", "CellData", "Input"}); auto inputStorePtr = DataStoreUtilities::CreateDataStore(dataStructure, inputPath, tupleShape, componentShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputStorePtr != nullptr); auto* inputArrayPtr = DataArray::Create(dataStructure, "Input", inputStorePtr, cellDataPtr->getId()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(inputArrayPtr != nullptr); constexpr std::array values = {0, 1, 3, -1, 1, 0, -1, 3}; for(usize index = 0; index < values.size(); ++index) @@ -197,27 +205,38 @@ TEST_CASE("NXImageProcessing::TanImageFilter: integer vector tuples preserve sha args.insertOrAssign(TanImageFilter::k_OutputImageArrayName_Key, std::make_any("Output")); const TanImageFilter filter; const auto preflightResult = filter.preflight(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.value().actions.size() == 1); const auto* actionPtr = dynamic_cast(preflightResult.outputActions.value().actions.front().get()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr != nullptr); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->type() == DataType::float32); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->dims() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actionPtr->componentDims() == componentShape); const auto executeResult = filter.execute(dataStructure, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); const DataPath outputPath = inputPath.replaceName("Output"); const IDataArray* outputArrayPtr = nullptr; REQUIRE_NOTHROW(outputArrayPtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getDataType() == DataType::float32); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getTupleShape() == tupleShape); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputArrayPtr->getComponentShape() == componentShape); const Float32Array* responsesPtr = nullptr; REQUIRE_NOTHROW(responsesPtr = &dataStructure.getDataRefAs(outputPath)); constexpr std::array expected = {0, 1.5574077246549022305, -0.1425465430742778053, -1.5574077246549022305, 1.5574077246549022305, 0, -1.5574077246549022305, -0.1425465430742778053}; for(usize index = 0; index < expected.size(); ++index) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(responsesPtr->getDataStoreRef().getValue(index) == Approx(expected.at(index)).epsilon(0).margin(1.0e-7)); } diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index ada487271b..c64fd5e895 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -72,18 +72,27 @@ void RunTMCC(IFilter& filter, DataStructure& ds, const DataPath& inputPath, uint namespace { -Arguments thresholdArguments(const DataPath& inputPath, float64 cap = std::numeric_limits::infinity(), bool warn = false, uint8 inside = 1, uint8 outside = 0, uint32 minimumSize = 0) +struct ThresholdTestOptions +{ + float64 UpperBoundary = std::numeric_limits::infinity(); + bool WarnOnNonfinite = false; + uint8 InsideValue = 1; + uint8 OutsideValue = 0; + uint32 MinimumSize = 0; +}; + +Arguments thresholdArguments(const DataPath& inputPath, const ThresholdTestOptions& options = {}) { const ThresholdMaximumConnectedComponentsImageFilter filter; Arguments arguments = filter.getDefaultArguments(); arguments.insertOrAssign(TMCCFilter::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); arguments.insertOrAssign(TMCCFilter::k_InputImageDataPath_Key, std::make_any(inputPath)); arguments.insertOrAssign(TMCCFilter::k_OutputImageArrayName_Key, std::make_any("Output")); - arguments.insertOrAssign(TMCCFilter::k_UpperBoundary_Key, std::make_any(cap)); - arguments.insertOrAssign(TMCCFilter::k_NonfiniteInputHandling_Key, std::make_any(warn ? 1 : 0)); - arguments.insertOrAssign(TMCCFilter::k_InsideValue_Key, std::make_any(inside)); - arguments.insertOrAssign(TMCCFilter::k_OutsideValue_Key, std::make_any(outside)); - arguments.insertOrAssign(TMCCFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(minimumSize)); + arguments.insertOrAssign(TMCCFilter::k_UpperBoundary_Key, std::make_any(options.UpperBoundary)); + arguments.insertOrAssign(TMCCFilter::k_NonfiniteInputHandling_Key, std::make_any(options.WarnOnNonfinite ? 1 : 0)); + arguments.insertOrAssign(TMCCFilter::k_InsideValue_Key, std::make_any(options.InsideValue)); + arguments.insertOrAssign(TMCCFilter::k_OutsideValue_Key, std::make_any(options.OutsideValue)); + arguments.insertOrAssign(TMCCFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(options.MinimumSize)); return arguments; } @@ -94,15 +103,21 @@ void requireThresholdMask(const DataStructure& dataStructure, const DataPath& in REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); const DataPath outputPath = inputPath.replaceName("Output"); REQUIRE_NOTHROW(outputPtr = &dataStructure.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputPtr->getDataType() == DataType::uint8); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputPtr->getTupleShape() == inputPtr->getTupleShape()); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputPtr->getComponentShape() == ShapeType{1}); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputPtr->getNumberOfTuples() == expected.size()); const UInt8Array* maskPtr = nullptr; REQUIRE_NOTHROW(maskPtr = &dataStructure.getDataRefAs(outputPath)); std::vector actual(expected.size()); const auto readResult = maskPtr->getDataStoreRef().copyIntoBuffer(0, nonstd::span(actual.data(), actual.size())); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(readResult); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(actual == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -213,6 +228,7 @@ class ThresholdTransferStore : public DataStore }; } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") { @@ -232,7 +248,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no const Arguments defaultArguments = filter.getDefaultArguments(); REQUIRE(defaultArguments.value(policyKey) == 0); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: initial midpoint includes the finite image minimum", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") { @@ -270,6 +288,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: in CHECK(maskPtr->getValue(1) == uint8{1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native integer endpoints retain exact width-two masks", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) @@ -283,6 +302,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {low, high}); const ThresholdMaximumConnectedComponentsImageFilter filter; const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); // Native midpoint is low+1 even at int64/uint64 endpoints beyond Float64's exact integer range. @@ -298,12 +318,14 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::lowest(), std::numeric_limits::max()}); const ThresholdMaximumConnectedComponentsImageFilter filter; const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); // Two adjacent pixels always score one component. Left ties halve the odd span until width 1; midpoint is then the native minimum. requireThresholdMask(dataStructure, inputPath, {1, 1}); } +// NOLINTBEGIN(cppcoreguidelines-avoid-do-while) The simplnx assertion macro triggers this check. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: caps clamp before 64-bit conversion and preserve lower truncation", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -315,7 +337,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ca DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::max() - 2, std::numeric_limits::max()}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = cap})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireThresholdMask(dataStructure, inputPath, {0, 1}); @@ -326,7 +348,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ca DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::max() - 2, std::numeric_limits::max()}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = cap})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireThresholdMask(dataStructure, inputPath, {0, 1}); @@ -336,7 +358,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ca DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 2}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, -0.5)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = -0.5})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireThresholdMask(dataStructure, inputPath, {1, 0}); @@ -346,13 +368,15 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ca DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {-128, -126}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, -128.75)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = -128.75})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireThresholdMask(dataStructure, inputPath, {1, 0}); } } +// NOLINTEND(cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: low and empty bands return warned Outside values", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -363,13 +387,15 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: lo DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {100, 102}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, cap, false, 1, outside)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = cap, .OutsideValue = outside})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == 1); REQUIRE(result.result.warnings().front().code == -28615); requireThresholdMask(dataStructure, inputPath, {outside, outside}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: NaN Upper Boundary is a hard parameter error in both image modes", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -379,14 +405,16 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: Na DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {100, 102}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.preflight(dataStructure, thresholdArguments(inputPath, std::numeric_limits::quiet_NaN(), warn)); + const auto result = filter.preflight(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = std::numeric_limits::quiet_NaN(), .WarnOnNonfinite = warn})); SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); REQUIRE(result.outputActions.errors().front().code == -28610); REQUIRE(result.outputActions.errors().front().message.find("Upper Boundary") != std::string::npos); REQUIRE(dataStructure.getDataAs(inputPath.replaceName("Output")) == nullptr); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: one image policy covers every nonfinite class and order", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -403,7 +431,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: on DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, std::numeric_limits::infinity(), warn, 1, outside)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.WarnOnNonfinite = warn, .OutsideValue = outside})); if(!warn) { SIMPLNX_RESULT_REQUIRE_INVALID(result.result); @@ -419,7 +447,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: on requireThresholdMask(dataStructure, inputPath, expected); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: all nonfinite Warn skips search and calculates no threshold", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -433,14 +463,17 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: al DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, values); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, true, inside, outside, 10)); + const auto result = + filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536, .WarnOnNonfinite = true, .InsideValue = inside, .OutsideValue = outside, .MinimumSize = 10})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == 1); REQUIRE(result.result.warnings().front().code == -28614); REQUIRE(result.result.warnings().front().message.find("no threshold was calculated") != std::string::npos); requireThresholdMask(dataStructure, inputPath, {outside, outside, outside}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: floating rounding distinguishes convergence from a repeated wide state", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -460,7 +493,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: fl // A lower rounded midpoint moves high down to low (width 0); only the upper-rounded state repeats at width 4. requireThresholdMask(dataStructure, inputPath, roundUp ? std::vector{0, 1} : std::vector{1, 1}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: opposite finite floating extrema keep finite search candidates", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", float32, float64) { @@ -476,7 +511,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF // Every score ties. Near -MAX, adjacent native endpoints are farther than 2 apart; ties-to-even retains the upper candidate. requireThresholdMask(dataStructure, inputPath, {0, 1}); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(cppcoreguidelines-avoid-do-while) The simplnx assertion macro triggers this check. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonzero scoring minimum sizes and the fixed scale stop are preserved", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -494,28 +531,28 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no SECTION("minimum size affects scoring but does not remove the final singleton") { const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 0, 2)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536, .MinimumSize = 2})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); requireThresholdMask(dataStructure, inputPath, {0, 1, 1, 1, 1, 1, 0, 1}); } SECTION("size exactly one qualifies for a minimum of one") { const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 0, 1)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536, .MinimumSize = 1})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); requireThresholdMask(dataStructure, inputPath, {0, 0, 1, 0, 1, 0, 0, 1}); } SECTION("zero Inside and nonzero Outside score the complement") { const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 0, 4, 0, 4, 0, 8}); - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 0, 1)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536, .InsideValue = 0, .OutsideValue = 1})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); requireThresholdMask(dataStructure, inputPath, {1, 0, 1, 0, 1, 0, 1, 0}); } SECTION("both nonzero score the whole image and ties select left") { const auto inputPath = rt::BuildImageFromPattern(dataStructure, 8, 1, 1, {0, 4, 6, 4, 6, 4, 0, 8}); - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536, false, 1, 7)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536, .OutsideValue = 7})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); requireThresholdMask(dataStructure, inputPath, {7, 1, 1, 1, 1, 1, 7, 1}); } @@ -542,7 +579,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no requireThresholdMask(dataStructure, inputPath, scaled ? std::vector{0, 1, 0, 1, 0, 1, 0, 1} : std::vector{0, 0, 0, 0, 0, 0, 0, 1}); } } +// NOLINTEND(cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: old JSON receives Error and version two preserves explicit Warn", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -563,7 +602,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ol SIMPLNX_RESULT_REQUIRE_VALID(roundTrip); REQUIRE(roundTrip.value().value(TMCCFilter::k_NonfiniteInputHandling_Key) == 1); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine rejects image nonfinites before empty-cap output and distinguishes empty input", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -587,7 +628,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: en REQUIRE(emptyInput.reads() == 0); REQUIRE(emptyOutput.writes() == 0); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no-finite fallback avoids count and reread passes and retains warnings on write failure", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -619,7 +662,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no REQUIRE(output.getValue(2) == 7); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: count failures remain strict under image Warn", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -646,7 +691,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co REQUIRE(result.warnings().front().code == -28614); REQUIRE(output.writes() == 0); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: summary and final transfers preserve failures and collected diagnostics", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -707,7 +754,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: su } REQUIRE(output.getValue(0) == 99); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: outer exceptions preserve collected warnings before output", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -737,7 +786,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ou REQUIRE(output.getValue(0) == 99); REQUIRE(result.errors().front().code == (allocationFailure ? -28636 : -28637)); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine cancellation keeps warning Results without inventing a local cancellation error", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -759,7 +810,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: en REQUIRE(result.warnings().front().code == -28614); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: direct engine validates hard parameters and output shape before transfer", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -775,7 +828,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: di REQUIRE(input.reads() == 0); REQUIRE(output.writes() == 0); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: resident and owned multi-block summaries use finite native extrema", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -811,7 +866,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: re REQUIRE(result.value().PositiveInfinityCount == 1); } } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native span and cap math retain exact bounds and ordinary floating rounding", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -827,6 +884,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: na REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(std::nextafter(0x1p63, 0.0)).Value == std::numeric_limits::max() - 1023); REQUIRE(ImageProcessing::detail::ConvertThresholdMaxCCCap(std::nextafter(0x1p64, 0.0)).Value == std::numeric_limits::max() - 2047); } +// NOLINTEND(bugprone-chained-comparison) // ----------------------------------------------------------------------------- // (1) Preflight guards: a multi-component input is rejected (requireScalar); a valid scalar input (of ANY numeric @@ -870,6 +928,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: pr const DataPath outputPath({"Image Geometry", "CellData", "Output"}); const IDataArray* outputPtr = nullptr; REQUIRE_NOTHROW(outputPtr = &ds.getDataRefAs(outputPath)); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(outputPtr->getDataType() == DataType::uint8); UnitTest::CheckArraysInheritTupleDims(ds); } @@ -898,6 +957,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: IT }); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: corrected-midpoint real-image regression (float)", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][MidpointMigration]") { @@ -962,9 +1022,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co REQUIRE(correctedHash.hexdigest() == "63d4c611a5633ba010f1857401b13150"); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto preflight = filter.preflight(dataStructure, thresholdArguments(inputPath, 65536.0)); + const auto preflight = filter.preflight(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536.0})); SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); - const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, 65536.0)); + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = 65536.0})); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); requireThresholdMask(dataStructure, inputPath, correctedMask); @@ -972,7 +1032,9 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co // Independent flood-fill evidence gives 3715 legacy versus 3712 corrected components. The retained search // can select fewer components after the arithmetic fix; this regression is not an ITK-parity or optimum claim. } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", "[NXImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { @@ -1039,4 +1101,5 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selec REQUIRE(c15_test::Read(dataStructure, input, shape, expectedStore) == std::vector{100, 102}); } UnitTest::CheckArraysInheritTupleDims(dataStructure); -} \ No newline at end of file +} +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 1f38b2cbfd..0988cc089c 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -361,6 +361,7 @@ IFilter::PreflightResult RunFormatPreflightForType(const std::string& fileName, } // namespace +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") { UnitTest::LoadPlugins(); @@ -378,7 +379,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is explicit and const Arguments arguments = filter.getDefaultArguments(); REQUIRE(arguments.value(policyKey) == 0); } +// NOLINTEND(bugprone-chained-comparison) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is active only for color-table output", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") { UnitTest::LoadPlugins(); @@ -499,6 +502,7 @@ std::vector readWriteImageSentinel(const fs::path& path) REQUIRE(input.is_open()); return {std::istreambuf_iterator(input), std::istreambuf_iterator()}; } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) template class WriteImageReadProbeStore : public ExtentCountingDataStore @@ -595,6 +599,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: wide and tiny interiors decode w requireWriteImageGray(scope, std::array{-0x1.8p+971, high * 0.5, high}, expected); } +// NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a distinct runtime witness", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -609,6 +614,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a REQUIRE(values.at(1) < values.at(2)); requireWriteImageGray(scope, values, std::array{0, 127, 255}); } +// NOLINTEND(bugprone-chained-comparison) TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite masked range decode literally", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { @@ -631,13 +637,16 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite const ScopedTempDir output(k_BinaryTestOutputDir); const WriteImageFilter filter; const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "mask.png", policy, "Grayscale", true)); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); const std::vector expected{10, 20, 30, 85, 85, 85, 170, 170, 170, 255, 255, 255, 10, 20, 30, 10, 20, 30, 10, 20, 30}; + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(readWriteImageNumericPng(output.path() / "mask.png", 7, 1) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: integer color reads masks only for slices", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -679,7 +688,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: integer color reads masks only f REQUIRE(result.result.warnings().empty()); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the first actual control color", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -707,7 +718,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the f REQUIRE(readWriteImageNumericPng(output.path() / "warn.png", 5, 1) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: late strict nonfinite preserves every existing slice byte", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -741,7 +754,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: late strict nonfinite preserves REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: no finite color range errors in either mode", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -761,6 +776,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: no finite color range errors in REQUIRE(fs::is_empty(output.path())); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("NXImageProcessing::WriteImageFilter: finite constants and all finite masked values stay valid", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { @@ -774,9 +790,11 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: finite constants and all finite const ScopedTempDir output(k_BinaryTestOutputDir); const WriteImageFilter filter; const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "constant.png", 0, "X Ray", true)); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().empty()); const std::vector expected = allMasked ? std::vector{10, 20, 30, 10, 20, 30, 10, 20, 30} : std::vector{255, 255, 255, 10, 20, 30, 255, 255, 255}; + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(readWriteImageNumericPng(output.path() / "constant.png", 3, 1) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -794,6 +812,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal la arguments.insertOrAssign(WriteImageFilter::k_Plane_Key, std::make_any(plane)); const WriteImageFilter filter; const auto result = scope.executeFilter(filter, dataStructure, arguments); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_VALID(result.result); const std::array, 3> expected{{{0, 85, 170, 255, 0, 85, 170, 255}, {0, 85, 0, 85, 170, 255, 170, 255}, {0, 170, 0, 170, 85, 255, 85, 255}}}; for(usize slice = 0; slice < 2; ++slice) @@ -803,6 +822,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal la { for(usize channel = 0; channel < 3; ++channel) { + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(bytes.at((pixel * 3) + channel) == expected.at(plane).at((slice * 4) + pixel)); } } @@ -810,6 +830,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal la UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: raw floating pixels ignore color policy and mask", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]", float32, float64) { UnitTest::LoadPlugins(); @@ -871,6 +892,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: raw floating pixels ign REQUIRE(std::signbit(actual.at(4))); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan opens no slice", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { @@ -890,19 +912,22 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan o const WriteImageFilter filter; const auto result = scope.executeFilter(filter, dataStructure, writeImageNumericArguments(output.path() / "cancel.png"), nullptr, handler, cancel); REQUIRE(cancel.load()); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == -1); REQUIRE(fs::is_empty(output.path())); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards use actionable results", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); - const auto empty = WriteImageColor::ValidateSize(0, 1, 1, false); + const auto empty = WriteImageColor::ValidateSize({.Width = 0, .Height = 1, .Count = 1}, false); SIMPLNX_RESULT_REQUIRE_INVALID(empty); REQUIRE(empty.errors().front().code == -27030); - const auto large = WriteImageColor::ValidateSize(std::numeric_limits::max(), 1, 1, true); + const auto large = WriteImageColor::ValidateSize({.Width = std::numeric_limits::max(), .Height = 1, .Count = 1}, true); SIMPLNX_RESULT_REQUIRE_INVALID(large); REQUIRE(large.errors().front().code == -27032); const auto invalidPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {1, 0}); @@ -911,7 +936,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards us const auto validPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {0, 1}); SIMPLNX_RESULT_REQUIRE_VALID(validPalette); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: page and slice IO failures preserve diagnostics and warnings", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -952,7 +979,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: page and slice IO failures prese REQUIRE(fs::is_empty(output.path())); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: late nonfinite scan uses bounded scalar and mask pages", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -999,7 +1028,9 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: late nonfinite scan uses bounded REQUIRE(fs::is_empty(output.path())); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: later commit failure keeps earlier slice and policy warning", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); @@ -1032,6 +1063,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: later commit failure keeps earli REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) TEST_CASE("NXImageProcessing::WriteImageFilter: Write Stack", "[NXImageProcessing][WriteImageFilter]") { @@ -2844,6 +2876,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: rejects a source-group grant sma } } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[NXImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") { UnitTest::LoadPlugins(); @@ -2952,4 +2985,5 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); UnitTest::CheckArraysInheritTupleDims(source); UnitTest::CheckArraysInheritTupleDims(reloaded); -} \ No newline at end of file +} +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index d7c9c685d1..38215f23d7 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -480,7 +480,9 @@ class ConnectedComponentLabelState m_MessageHandler.sendWarningMessage(m_Warnings.warnings().front().message); } } - return {static_cast(candidate)}; + Result result; + result.m_Expected = static_cast(candidate); + return result; } // Called once by the outer execution wrapper, including after cancellation, retry, or an independent error. @@ -509,22 +511,26 @@ struct CcUnionFind CcUnionFind& operator=(const CcUnionFind&) = delete; CcUnionFind& operator=(CcUnionFind&&) = delete; - std::vector parent{0u}; // zero is background; explicit legacy wrap can link foreground to it - Result makeLabel() // check the actual next table index before conversion, append, or assignment + const std::vector& parentTable() const noexcept { - auto labelResult = m_LabelState.labelFor(parent.size()); + return m_Parent; + } + + Result makeLabel() // check the actual next table index before conversion, append, or assignment + { + auto labelResult = m_LabelState.labelFor(m_Parent.size()); if(labelResult.invalid()) { return labelResult; } - parent.push_back(labelResult.value()); + m_Parent.push_back(labelResult.value()); return labelResult; } uint32 find(uint32 l) const noexcept // LookupSet: follow parents to the root { - while(l != parent[l]) + while(l != m_Parent[l]) { - l = parent[l]; + l = m_Parent[l]; } return l; } @@ -534,31 +540,31 @@ struct CcUnionFind const uint32 e2 = find(b); if(e1 < e2) { - parent[e2] = e1; + m_Parent[e2] = e1; } else if(e2 < e1) { - parent[e1] = e2; + m_Parent[e1] = e2; } } - // CreateConsecutive follows ITK's ascending root traversal: roots (parent[i]==i) get consecutive labels 1,2,...; bg=0. + // CreateConsecutive follows ITK's ascending root traversal: roots (m_Parent[i]==i) get consecutive labels 1,2,...; bg=0. // Returns {consecutive[], numberOfObjects}. std::pair, uint32> createConsecutive(const std::atomic_bool& shouldCancel, const ProgressCallback& reportProgress) const { - const usize n = parent.size(); - std::vector consecutive(n, 0u); + const usize labelCount = m_Parent.size(); + std::vector consecutive(labelCount, 0u); uint32 consecutiveLabel = 0; uint32 count = 0; - for(usize blockBegin = 1; blockBegin < n; blockBegin += k_ConnectedComponentResolutionBlockSize) + for(usize blockBegin = 1; blockBegin < labelCount; blockBegin += k_ConnectedComponentResolutionBlockSize) { if(shouldCancel) { return {std::move(consecutive), count}; } - const usize blockEnd = std::min(n, blockBegin + k_ConnectedComponentResolutionBlockSize); + const usize blockEnd = std::min(labelCount, blockBegin + k_ConnectedComponentResolutionBlockSize); for(usize i = blockBegin; i < blockEnd; ++i) { - if(parent[i] == i) + if(m_Parent[i] == i) { ++consecutiveLabel; // skips 0 (background) consecutive[i] = consecutiveLabel; @@ -574,6 +580,7 @@ struct CcUnionFind } private: + std::vector m_Parent{0u}; // zero is background; explicit legacy wrap can link foreground to it ConnectedComponentLabelState& m_LabelState; }; @@ -604,12 +611,12 @@ inline void EncodeLineGeometryInto(nonstd::span row, int64 nX, const Pr // Encode one gathered x-row and assign each run a fresh provisional label. template -inline Result<> EncodeLineInto(nonstd::span row, int64 nX, const Pred& pred, CcUnionFind& uf, CcLine& line) +inline Result<> EncodeLineInto(nonstd::span row, int64 width, const Pred& pred, CcUnionFind& unionFind, CcLine& line) { - EncodeLineGeometryInto(row, nX, pred, line); + EncodeLineGeometryInto(row, width, pred, line); for(CcRun& run : line) { - auto labelResult = uf.makeLabel(); + auto labelResult = unionFind.makeLabel(); if(labelResult.invalid()) { return ConvertResult(std::move(labelResult)); @@ -1465,10 +1472,10 @@ Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, Abstr provisional = std::make_unique(*provisionalDataStore); } - detail::CcUnionFind uf(labelState); + detail::CcUnionFind unionFind(labelState); beginProgressPhase("Labeling Connected Components", nZ == 1 ? vol : static_cast(nZ)); - Result<> pass1Result = nZ == 1 ? detail::Pass1TwoDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, uf, nullptr, target2DBytes, reportPercent) : - detail::Pass1ThreeDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, uf, nullptr, reportCount); + Result<> pass1Result = nZ == 1 ? detail::Pass1TwoDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, unionFind, nullptr, target2DBytes, reportPercent) : + detail::Pass1ThreeDimensional(inStore, provisional.get(), dims, pred, fullyConnected, shouldCancel, unionFind, nullptr, reportCount); if(pass1Result.invalid()) { return pass1Result; @@ -1479,25 +1486,25 @@ Result<> LabelConnectedComponentsImpl(const AbstractDataStore& inStore, Abstr } // ---- Resolve: CreateConsecutive, then a direct per-provisional-label lookup so pass 2 is O(1) per voxel. ---- - const usize provisionalLabelCount = uf.parent.size() - 1; + const usize provisionalLabelCount = unionFind.parentTable().size() - 1; beginProgressPhase("Resolving Connected Component Equivalences", 2 * provisionalLabelCount); - const std::pair, uint32> resolved = uf.createConsecutive(shouldCancel, reportPercent); + const std::pair, uint32> resolved = unionFind.createConsecutive(shouldCancel, reportPercent); if(shouldCancel) { return {}; } const std::vector& consecutive = resolved.first; - std::vector finalLabel(uf.parent.size(), 0u); - for(usize blockBegin = 1; blockBegin < uf.parent.size(); blockBegin += k_ConnectedComponentResolutionBlockSize) + std::vector finalLabel(unionFind.parentTable().size(), 0u); + for(usize blockBegin = 1; blockBegin < unionFind.parentTable().size(); blockBegin += k_ConnectedComponentResolutionBlockSize) { if(shouldCancel) { return {}; } - const usize blockEnd = std::min(uf.parent.size(), blockBegin + k_ConnectedComponentResolutionBlockSize); + const usize blockEnd = std::min(unionFind.parentTable().size(), blockBegin + k_ConnectedComponentResolutionBlockSize); for(usize i = blockBegin; i < blockEnd; ++i) { - finalLabel[i] = consecutive[uf.find(static_cast(i))]; + finalLabel[i] = consecutive[unionFind.find(static_cast(i))]; } reportPercent(provisionalLabelCount + blockEnd - 1); } @@ -1638,41 +1645,43 @@ Result CountConnectedComponents(const AbstractDataStore& inStore, con } if(volumeValues == 0) { - return Result{0u}; + return {}; } const IFilter::MessageHandler messageHandler; detail::ConnectedComponentLabelState labelState(ConnectedComponentLabelLimitHandling::Error, messageHandler); - detail::CcUnionFind uf(labelState); + detail::CcUnionFind unionFind(labelState); std::vector sizeByLabel; // sizeByLabel[provisional label] = total run length for that (pre-union) label Result<> pass1Result = dims[2] == 1 ? - detail::Pass1TwoDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, target2DBytes, detail::ProgressCallback{}) : - detail::Pass1ThreeDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, uf, &sizeByLabel, detail::ProgressCallback{}); + detail::Pass1TwoDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, unionFind, &sizeByLabel, target2DBytes, detail::ProgressCallback{}) : + detail::Pass1ThreeDimensional(inStore, nullptr, dims, pred, fullyConnected, shouldCancel, unionFind, &sizeByLabel, detail::ProgressCallback{}); if(pass1Result.invalid()) { return ConvertResultTo(std::move(pass1Result), uint32{0}); } if(shouldCancel) { - return Result{0u}; + return {}; } // Resolve per-root sizes now that the union-find is final (roots can still change during Pass1, so this can only be // done AFTER Pass1 completes -- summing by raw provisional label first, as Pass1 does, is what makes that safe). - std::vector rootSize(uf.parent.size(), 0u); + std::vector rootSize(unionFind.parentTable().size(), 0u); for(usize label = 1; label < sizeByLabel.size(); ++label) { - rootSize[uf.find(static_cast(label))] += sizeByLabel[label]; + rootSize[unionFind.find(static_cast(label))] += sizeByLabel[label]; } uint32 numComponents = 0; - for(usize i = 1; i < uf.parent.size(); ++i) + for(usize i = 1; i < unionFind.parentTable().size(); ++i) { - if(uf.parent[i] == i && rootSize[i] >= minSize) // i is a root (one entry per component) + if(unionFind.parentTable()[i] == i && rootSize[i] >= minSize) // i is a root (one entry per component) { ++numComponents; } } - return Result{numComponents}; + Result result; + result.m_Expected = numComponents; + return result; } catch(const std::bad_alloc&) { return MakeErrorResult(-8774, "Connected-component counting could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory."); diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 0f59e35a65..4691d8ca1b 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -2136,6 +2136,7 @@ Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, Abstra // CheckedOutput opts into the selected Curvature Flow controls, exact no-ops, and terminal conversion only. // Other finite-difference filters retain the default behavior and their own precision models. template +// NOLINTNEXTLINE(misc-no-recursion) -- The staging call has two resident endpoints, so it cannot re-enter the out-of-core staging branch. Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, FloatVec3 spacing, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit2D = detail::k_FiniteDifference2DResidentLimit) { diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 211299bc39..9095d9757c 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -401,8 +401,15 @@ inline bool FloatAlmostEqual(float64 x1, float64 x2, int64 maxUlps = 1) */ namespace detail { -inline bool OtsuObjectiveIsUsable(float64 globalMean, const std::vector& classMean, const std::vector& classFrequency, float64 objective, bool allowZero = false) +struct OtsuClassStatistics { + std::vector Mean; + std::vector Frequency; +}; + +inline bool OtsuObjectiveIsUsable(float64 globalMean, const OtsuClassStatistics& classes, float64 objective, bool allowZero = false) +{ + const auto& [classMean, classFrequency] = classes; if(!std::isfinite(globalMean) || !std::isfinite(objective) || objective < 0.0) { return false; @@ -422,18 +429,18 @@ inline bool OtsuObjectiveIsUsable(float64 globalMean, const std::vector } template -Result> ComputeOtsuThresholds(const Histogram& h, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint, const std::atomic_bool* shouldCancelPtr) +Result> ComputeOtsuThresholds(const Histogram& histogram, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint, const std::atomic_bool* shouldCancelPtr) { - const usize numBins = h.counts.size(); + const usize numBins = histogram.counts.size(); std::vector thresholds; if(numBins == 0 || numThresholds == 0) { - return {thresholds}; + return {}; } if(numThresholds >= numBins) { // Not enough bins to place this many thresholds; the init loop below would index past the end. - return {thresholds}; + return {}; } float64 totalCount = 0.0; for(usize bin = 0; bin < numBins; ++bin) @@ -442,19 +449,19 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if((bin % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) { - return {std::vector{}}; + return {}; } } - totalCount += static_cast(h.counts[bin]); + totalCount += static_cast(histogram.counts[bin]); } if(totalCount <= 0.0) { - return {thresholds}; + return {}; } // Per-bin measurement value == bin midpoint (ITK Histogram::GetMeasurementVector(bin)[0]). - auto measurement = [&](usize bin) -> float64 { return h.min + (static_cast(bin) + 0.5) * h.binWidth; }; - auto frequency = [&](usize bin) -> float64 { return static_cast(h.counts[bin]); }; + auto measurement = [&](usize bin) -> float64 { return histogram.min + ((static_cast(bin) + 0.5) * histogram.binWidth); }; + auto frequency = [&](usize bin) -> float64 { return static_cast(histogram.counts[bin]); }; const usize numberOfClasses = numThresholds + 1; @@ -466,7 +473,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if((b % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) { - return {std::vector{}}; + return {}; } } globalMean += measurement(b) * frequency(b); @@ -482,7 +489,9 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num std::vector maxVarThresholdIndexes = thresholdIndexes; // --- Frequency of the initial classes --- - std::vector classFrequency(numberOfClasses, 0.0); + OtsuClassStatistics classes; + auto& classFrequency = classes.Frequency; + classFrequency.resize(numberOfClasses); float64 freqSum = 0.0; for(usize j = 0; j < numberOfClasses - 1; ++j) { @@ -499,14 +508,15 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if((b % 65536) == 0 && shouldCancelPtr != nullptr && shouldCancelPtr->load()) { - return {std::vector{}}; + return {}; } } imgPDF[b] = frequency(b) / totalCount; } // --- Mean of the initial classes --- - std::vector classMean(numberOfClasses, 0.0); + auto& classMean = classes.Mean; + classMean.resize(numberOfClasses); float64 meanSum = 0.0; for(usize j = 0; j < numberOfClasses - 1; ++j) { @@ -526,7 +536,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num // Opt-in checks leave the established arithmetic and finite tie rule unchanged. if constexpr(Checked) { - if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween)) + if(!OtsuObjectiveIsUsable(globalMean, classes, maxVarBetween)) { return MakeErrorResult>(-28650, "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); } @@ -546,7 +556,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num maxVarBetween = maxVarBetween * valleyEmphasisFactor; if constexpr(Checked) { - if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, maxVarBetween, valleyEmphasisFactor == 0.0)) + if(!OtsuObjectiveIsUsable(globalMean, classes, maxVarBetween, valleyEmphasisFactor == 0.0)) { return MakeErrorResult>(-28651, "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } @@ -622,7 +632,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num { if(shouldCancelPtr != nullptr && shouldCancelPtr->load()) { - return {std::vector{}}; + return {}; } } float64 varBetween = 0.0; @@ -635,7 +645,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num varBetween /= totalCount; if constexpr(Checked) { - if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween)) + if(!OtsuObjectiveIsUsable(globalMean, classes, varBetween)) { return MakeErrorResult>(-28652, "Otsu candidate class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); @@ -653,7 +663,7 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num varBetween = varBetween * valleyEmphasisFactor; if constexpr(Checked) { - if(!OtsuObjectiveIsUsable(globalMean, classMean, classFrequency, varBetween, valleyEmphasisFactor == 0.0)) + if(!OtsuObjectiveIsUsable(globalMean, classes, varBetween, valleyEmphasisFactor == 0.0)) { return MakeErrorResult>(-28653, "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } @@ -672,16 +682,18 @@ Result> ComputeOtsuThresholds(const Histogram& h, usize num for(usize t = 0; t < numThresholds; ++t) { const float64 offset = returnBinMidpoint ? 0.5 : 1.0; - thresholds[t] = h.min + (static_cast(maxVarThresholdIndexes[t]) + offset) * h.binWidth; + thresholds[t] = histogram.min + (static_cast(maxVarThresholdIndexes[t]) + offset) * histogram.binWidth; } - return {std::move(thresholds)}; + Result> result; + result.m_Expected = std::move(thresholds); + return result; } } // namespace detail /** @brief Retains the established unchecked solver contract for existing direct consumers. */ -inline std::vector OtsuMultipleThresholds(const Histogram& h, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint) +inline std::vector OtsuMultipleThresholds(const Histogram& histogram, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint) { - auto result = detail::ComputeOtsuThresholds(h, numThresholds, valleyEmphasis, returnBinMidpoint, nullptr); + auto result = detail::ComputeOtsuThresholds(histogram, numThresholds, valleyEmphasis, returnBinMidpoint, nullptr); return std::move(result.value()); } } // namespace nx::core::ImageProcessing diff --git a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp index 25c5283b0f..d84c77fc85 100644 --- a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp @@ -65,7 +65,7 @@ T SubtractExactIntegerReconstructionHeight(T value, uint64 heightUnits) const uint64 distanceToMinimum = static_cast(value - std::numeric_limits::lowest()); return heightUnits >= distanceToMinimum ? std::numeric_limits::lowest() : static_cast(value - static_cast(heightUnits)); } - const uint64 positiveValue = static_cast(value); + const uint64 positiveValue = static_cast(+value); if(heightUnits <= positiveValue) { return static_cast(positiveValue - heightUnits); diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index c0df789ff3..090fe449ca 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -184,11 +184,20 @@ struct OtsuBufferPlan usize Values = 1; }; -inline OtsuBufferPlan ReserveOtsuBuffers(usize total, usize bytesPerValue, usize targetBytes, bool usesExternalStore, usize summaryBytesPerBlock = 0) +struct OtsuBufferRequirements { + usize TotalValues = 0; + usize BytesPerValue = 1; + bool UsesExternalStore = false; + usize SummaryBytesPerBlock = 0; +}; + +inline OtsuBufferPlan ReserveOtsuBuffers(const OtsuImageOptions& options, const OtsuBufferRequirements& requirements) +{ + const auto& [total, bytesPerValue, usesExternalStore, summaryBytesPerBlock] = requirements; OtsuBufferPlan plan; const usize usefulBytes = total > std::numeric_limits::max() / bytesPerValue ? std::numeric_limits::max() : total * bytesPerValue; - usize bytes = std::min({targetBytes, k_OtsuMaximumBatchBytes, usefulBytes}); + usize bytes = std::min({options.TargetBytes, k_OtsuMaximumBatchBytes, usefulBytes}); if(usesExternalStore) { plan.Reservation = ReserveWorkingMemory(bytes, usefulBytes); @@ -240,9 +249,9 @@ Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataSt return status; } - // T may be bool. A raw array provides the contiguous storage that vector cannot provide. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full transfer overwrites this contiguous buffer, including for bool. auto valuesPtr = std::make_unique_for_overwrite(batchValues); - // MaskT may be bool and must provide contiguous values for store transfers. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full mask transfer overwrites the contiguous bool or uint8 buffer. auto maskValuesPtr = maskPtr == nullptr ? nullptr : std::make_unique_for_overwrite(batchValues); for(usize start = 0; start < total;) { @@ -328,6 +337,14 @@ struct OtsuFrequencyEntry usize Count = 0; }; +template +struct OtsuFrequencyRing +{ + CacheMemoryBudgetManager::WorkingMemoryReservation Reservation; + std::unique_ptr>> Frequencies; + bool Compact = false; +}; + template Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T maxValue, usize frequency = 1) { @@ -381,35 +398,35 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { return MakeErrorResult(-8329, "Otsu input and optional mask must be scalar and contain the same number of tuples. Select a scalar mask with one value per input tuple."); } - OtsuHistogramResult result; if(shouldCancel) { - return {std::move(result)}; + return {}; } + OtsuHistogramResult result; result.HistogramData.counts.assign(options.NumberOfBins, 0); T minValue{}; T maxValue{}; const bool external = input.getStoreType() == IDataStore::StoreType::OutOfCore || (maskPtr != nullptr && maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore); const usize total = input.getSize(); const usize bytesPerValue = sizeof(T) + (maskPtr == nullptr ? 0 : sizeof(MaskT)); - auto plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external, std::is_floating_point_v ? sizeof(detail::OtsuFloatingRangeSummary) : 0); + auto plan = detail::ReserveOtsuBuffers( + options, + {.TotalValues = total, .BytesPerValue = bytesPerValue, .UsesExternalStore = external, .SummaryBytesPerBlock = std::is_floating_point_v ? sizeof(detail::OtsuFloatingRangeSummary) : 0}); constexpr usize ringSize = 65536; - CacheMemoryBudgetManager::WorkingMemoryReservation ringReservation; - std::unique_ptr>> frequenciesPtr; - using CompactRangeType = std::conditional_t, bool, const bool>; - CompactRangeType compactRange = false; + using FrequencyRingType = std::conditional_t, detail::OtsuFrequencyRing, const detail::OtsuFrequencyRing>; + FrequencyRingType ring{}; if constexpr(std::is_integral_v) { const usize ringBytes = ringSize * sizeof(detail::OtsuFrequencyEntry); // Admit the optional ring in full only after the required payload, without evicting cache entries. if(external && ResolvePreferredWorkingMemoryBytes(ringBytes) >= ringBytes) { - ringReservation = CacheMemoryBudgetManager::instance().tryReserveWorkingMemoryWithoutEviction(ringBytes); + ring.Reservation = CacheMemoryBudgetManager::instance().tryReserveWorkingMemoryWithoutEviction(ringBytes); } - compactRange = !external || ringReservation.sizeBytes() == ringBytes; - if(compactRange) + ring.Compact = !external || ring.Reservation.sizeBytes() == ringBytes; + if(ring.Compact) { - frequenciesPtr = std::make_unique>>(ringSize); + ring.Frequencies = std::make_unique>>(ringSize); } } Result<> rangeResult = detail::VisitOtsuValues( @@ -471,17 +488,17 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp ++result.FiniteCount; if constexpr(std::is_integral_v) { - if(compactRange) + if(ring.Compact) { using KeyType = detail::UnsignedIntegralType; const KeyType key = detail::IntegralMonotonicKey(value); - compactRange = (detail::IntegralMonotonicKey(maxValue) - detail::IntegralMonotonicKey(minValue)) <= static_cast(ringSize - 1); - if(compactRange) + ring.Compact = (detail::IntegralMonotonicKey(maxValue) - detail::IntegralMonotonicKey(minValue)) <= static_cast(ringSize - 1); + if(ring.Compact) { - auto& entry = (*frequenciesPtr)[static_cast(key & static_cast(ringSize - 1))]; + auto& entry = (*ring.Frequencies)[static_cast(key & static_cast(ringSize - 1))]; if(entry.Count != 0 && entry.Value != value) { - compactRange = false; + ring.Compact = false; } else { @@ -489,10 +506,10 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp ++entry.Count; } } - if(!compactRange) + if(!ring.Compact) { - frequenciesPtr.reset(); - ringReservation = {}; + ring.Frequencies.reset(); + ring.Reservation = {}; } } } @@ -501,7 +518,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } }, std::is_floating_point_v); - if(std::is_floating_point_v || compactRange || minValue == maxValue) + if(std::is_floating_point_v || ring.Compact || minValue == maxValue) { plan.Reservation = {}; } @@ -535,7 +552,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { histogram.counts.front() = result.FiniteCount; } - else if(compactRange) + else if(ring.Compact) { for(usize i = 0; i < ringSize; ++i) { @@ -543,7 +560,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { return ConvertResultTo(std::move(rangeResult), std::move(result)); } - const auto& entry = (*frequenciesPtr)[i]; + const auto& entry = (*ring.Frequencies)[i]; if(entry.Count != 0) { Result<> addResult = detail::AccumulateCheckedOtsuValue(histogram, entry.Value, minValue, maxValue, entry.Count); @@ -559,7 +576,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp if constexpr(std::is_floating_point_v) { // The floating-point histogram pass needs no range summaries. - plan = detail::ReserveOtsuBuffers(total, bytesPerValue, options.TargetBytes, external); + plan = detail::ReserveOtsuBuffers(options, {.TotalValues = total, .BytesPerValue = bytesPerValue, .UsesExternalStore = external}); } Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, plan.Values, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { for(usize i = 0; i < values.size(); ++i) @@ -605,7 +622,7 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram { if(shouldCancel) { - return {std::vector{}}; + return {}; } if(options.NumberOfThresholds == 0 || options.NumberOfThresholds >= histogram.counts.size()) { @@ -622,7 +639,9 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram } if(histogram.binWidth == 0.0 && histogram.min == histogram.max) { - return {std::vector(options.NumberOfThresholds, histogram.min)}; + Result> result; + result.m_Expected = std::vector(options.NumberOfThresholds, histogram.min); + return result; } return detail::ComputeOtsuThresholds(histogram, options.NumberOfThresholds, options.ValleyEmphasis, options.ReturnBinMidpoint, &shouldCancel); } @@ -714,12 +733,12 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStoregetStoreType() == IDataStore::StoreType::OutOfCore || - output.getStoreType() == IDataStore::StoreType::OutOfCore); - // T may be bool. A raw array provides the contiguous storage that vector cannot provide. + const bool external = + input.getStoreType() == IDataStore::StoreType::OutOfCore || maskPtr->getStoreType() == IDataStore::StoreType::OutOfCore || output.getStoreType() == IDataStore::StoreType::OutOfCore; + auto plan = detail::ReserveOtsuBuffers(options, {.TotalValues = input.getSize(), .BytesPerValue = sizeof(T) + sizeof(MaskT) + sizeof(uint8), .UsesExternalStore = external}); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full transfer overwrites this contiguous buffer, including for bool. auto valuesPtr = std::make_unique_for_overwrite(plan.Values); - // MaskT may be bool and must provide contiguous values for store transfers. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full mask transfer overwrites the contiguous bool or uint8 buffer. auto maskValuesPtr = std::make_unique_for_overwrite(plan.Values); auto labelsPtr = std::make_unique>(plan.Values); messageHandler.sendInfoMessage("Applying masked Otsu labels in bounded batches"); diff --git a/src/simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp b/src/simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp index 4639a934b0..09ff16a065 100644 --- a/src/simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp @@ -189,6 +189,7 @@ struct PointwiseMapBody { const T* inputBuffer; U* outputBuffer; + // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members) -- Synchronous workers borrow one callable without copying its state. const MapOpT& mapOp; const std::atomic_bool& shouldCancel; diff --git a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp index 3956ef9ddf..d3d97ed1ca 100644 --- a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp @@ -1336,10 +1336,10 @@ Result<> RecursiveGaussian2DYAxisToSink(const SrcStoreT& sourceStore, const Size } if(haloRows > 0) { - accumulated = detail::MergeRecursiveGaussianResults(pass.CheckedSmoothing, std::move(accumulated), - detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yEnd, haloRows, xBegin, columnCount, - nonstd::span(source.data() + (rowCount * columnCount), haloRows * columnCount), - pass.CheckedSmoothing)); + accumulated = detail::MergeRecursiveGaussianResults( + pass.CheckedSmoothing, std::move(accumulated), + detail::ReadRecursiveGaussian2DBlock(sourceStore, dims[0], yEnd, haloRows, xBegin, columnCount, + nonstd::span(source.data(), source.size()).subspan(rowCount * columnCount, haloRows * columnCount), pass.CheckedSmoothing)); if(accumulated.invalid()) { return accumulated; @@ -2040,7 +2040,7 @@ Result<> RecursiveGaussianAxisPass(const SrcStoreT& src, DstStoreT& dst, const S } const usize blockOffset = (z * dims[1] + yBegin) * dims[0]; accumulated = detail::MergeRecursiveGaussianResults(options.CheckedSmoothing, std::move(accumulated), - src.copyIntoBuffer(blockOffset, nonstd::span(srcSlab.data() + (z * valuesPerPlaneBlock), valuesPerPlaneBlock))); + src.copyIntoBuffer(blockOffset, nonstd::span(srcSlab.data(), srcSlab.size()).subspan(z * valuesPerPlaneBlock, valuesPerPlaneBlock))); if(accumulated.invalid()) { return accumulated; diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index dd8f33ec34..6dbff228c2 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -148,7 +148,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& const usize total = input.getSize(); if(total == 0) { - return {summary}; + return {{summary}}; } constexpr usize blockValues = 65536; constexpr usize residentBatchBytes = 32ULL * 1024ULL * 1024ULL; @@ -188,7 +188,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& } } std::vector> blocks(blockCapacity); - // T may be bool. A raw array provides the contiguous storage that vector cannot provide. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full transfer overwrites this contiguous buffer, including for bool. auto valuesPtr = direct ? nullptr : std::make_unique_for_overwrite(batchValues); for(usize start = 0; start < total;) { @@ -236,7 +236,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& } start += count; } - return {summary, std::move(status.warnings())}; + return {{summary}, std::move(status.warnings())}; } catch(const std::bad_alloc&) { return ConvertResultTo( @@ -318,8 +318,18 @@ bool ThresholdMaxCCSpanExceedsTwo(T low, T high) } template -Result SearchThresholdMaxCC(const AbstractDataStore& input, const SizeVec3& dims, T low, T high, T fixedCap, const ThresholdMaxCCOptions& options, const std::atomic_bool& shouldCancel) +struct ThresholdMaxCCSearchInterval { + T Low; + T High; + T FixedCap; +}; + +template +Result SearchThresholdMaxCC(const AbstractDataStore& input, const SizeVec3& dims, ThresholdMaxCCSearchInterval interval, const ThresholdMaxCCOptions& options, + const std::atomic_bool& shouldCancel) +{ + auto [low, high, fixedCap] = interval; T midpoint = std::midpoint(low, high); T left = std::midpoint(low, midpoint); T right = std::midpoint(high, midpoint); @@ -394,7 +404,7 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor auto reservation = ReserveWorkingMemory(preferredBytes, usefulBytes); const usize chunkValues = std::max(minimumValues, std::min(total, static_cast(std::min(reservation.sizeBytes() / valueBytes, std::numeric_limits::max())))); reservation.shrinkTo(static_cast(std::min(chunkValues, total)) * valueBytes); - // T may be bool and must provide contiguous values for store transfers. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) -- A full transfer overwrites this contiguous buffer, including for bool. auto inputPtr = fillOutside ? nullptr : std::make_unique_for_overwrite(std::min(chunkValues, total)); auto outputPtr = std::make_unique>(std::min(chunkValues, total)); for(usize start = 0; start < total;) @@ -511,7 +521,7 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in auto writeResult = detail::WriteThresholdMaxCC(input, output, true, T{}, T{}, options, shouldCancel); return MergeResults(std::move(status), std::move(writeResult)); } - auto thresholdResult = detail::SearchThresholdMaxCC(input, dims, summary.Minimum, std::min(summary.Maximum, cap.Value), cap.Value, options, shouldCancel); + auto thresholdResult = detail::SearchThresholdMaxCC(input, dims, {.Low = summary.Minimum, .High = std::min(summary.Maximum, cap.Value), .FixedCap = cap.Value}, options, shouldCancel); if(thresholdResult.invalid()) { return MergeResults(std::move(status), ConvertResult(std::move(thresholdResult))); From f9418c0493fe5c2a56d0ef649ce1e9f0699d8d02 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 11:33:36 -0500 Subject: [PATCH 38/98] COMP: Match C15 sources to NXImageProcessing Update plugin includes and test labels after the plugin rename. Keep the core utility namespace and numeric contracts unchanged. Signed-off-by: Jessica Marquis --- .../Filters/ExpImageFilter.cpp | 2 +- .../Filters/ExpNegativeImageFilter.cpp | 2 +- .../Filters/Log10ImageFilter.cpp | 2 +- .../Filters/LogImageFilter.cpp | 2 +- .../Filters/SqrtImageFilter.cpp | 2 +- .../Filters/SquareImageFilter.cpp | 2 +- .../Filters/TanImageFilter.cpp | 2 +- .../test/AcosImageFilterTest.cpp | 15 ++--- .../test/AsinImageFilterTest.cpp | 13 +++-- .../test/C15CompatibilityTestUtils.hpp | 10 ++-- .../test/ConnectedComponentEngineTest.cpp | 22 +++---- .../ConnectedComponentImageFilterTest.cpp | 8 +-- .../test/CurvatureFlowImageFilterTest.cpp | 42 +++++++------- .../test/HConvexImageFilterTest.cpp | 29 +++++----- .../test/HMinimaImageFilterTest.cpp | 26 ++++----- ...calWatershedFromMarkersImageFilterTest.cpp | 53 ++++++++--------- .../OtsuMultipleThresholdsImageFilterTest.cpp | 58 +++++++++---------- ...othingRecursiveGaussianImageFilterTest.cpp | 50 ++++++++-------- 18 files changed, 173 insertions(+), 167 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp index 5c6b68f3b2..81809ccd8c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp index e38e35d246..c66cbb075e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ExpNegativeImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp index 6d9fa80ae8..59ae63a981 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Log10ImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp index 03f959f887..0b6c0f0422 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/LogImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp index cc1e988acf..b7e0e04360 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SqrtImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp index 0b6854f3fc..0889f7a48c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp index 6e16def947..72779788f1 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/TanImageFilter.cpp @@ -2,7 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" -#include "ImageProcessing/utils/MathOutputUtilities.hpp" +#include "NXImageProcessing/utils/MathOutputUtilities.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" diff --git a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp index 2613f4e487..e88504a190 100644 --- a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp @@ -122,7 +122,8 @@ class AcosTransferStore : public DataStore // An angle at zero is pi/2 radians, independently of the input's integer width or signedness. // Keeping the input at zero makes the unfixed integer-output implementation safe to execute. // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer zero produces float32 radians", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer zero produces float32 radians", "[NXImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, + uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -161,7 +162,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer zero produces floa // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: floating input retains type and propagates domain NaN", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: floating input retains type and propagates domain NaN", "[NXImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -192,8 +193,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: floating input retains typ // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, - uint32, int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer endpoints and invalid values produce floating angles", "[NXImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -233,7 +234,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: integer endpoints and inva // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AcosImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: adjacent domain boundaries remain distinct", "[NXImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -262,7 +263,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AcosImageFilter: adjacent domain boundaries // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::AcosImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AcosImageFilter]") +TEST_CASE("NXImageProcessing::AcosImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][AcosImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -299,7 +300,7 @@ TEST_CASE("ImageProcessing::AcosImageFilter: mapped multi-component output prese // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::AcosImageFilter: mapped buffered engine preserves cancellation and I/O failures", "[ImageProcessing][Issue1874][AcosImageFilter]") +TEST_CASE("NXImageProcessing::AcosImageFilter: mapped buffered engine preserves cancellation and I/O failures", "[NXImageProcessing][Issue1874][AcosImageFilter]") { UnitTest::LoadPlugins(); using OutputType = ImageProcessing::IntegerToFloat32Type; diff --git a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp index 167a811fc4..6c0e28b872 100644 --- a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp @@ -62,7 +62,8 @@ DataPath runMappedIntegerAsin(DataStructure& dataStructure, const DataPath& inpu // The principal angle at integer one is pi/2 radians for every integer width and signedness. // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer one produces float32 radians", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer one produces float32 radians", "[NXImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, + uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -82,8 +83,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer one produces float // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer endpoints and invalid values produce floating angles", "[ImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, - uint32, int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer endpoints and invalid values produce floating angles", "[NXImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -122,7 +123,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: integer endpoints and inva // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: floating input retains type and principal angles", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: floating input retains type and principal angles", "[NXImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -156,7 +157,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: floating input retains typ // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: adjacent domain boundaries remain distinct", "[ImageProcessing][Issue1874][AsinImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: adjacent domain boundaries remain distinct", "[NXImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); @@ -184,7 +185,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::AsinImageFilter: adjacent domain boundaries // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::AsinImageFilter: mapped multi-component output preserves tuple shape", "[ImageProcessing][Issue1874][AsinImageFilter]") +TEST_CASE("NXImageProcessing::AsinImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][AsinImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); diff --git a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp index 8f79fa19c9..8cd852533e 100644 --- a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp +++ b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp @@ -3,11 +3,11 @@ #include "ReconstructionFilterTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" -#include "ImageProcessing/Filters/BinaryThresholdImageFilter.hpp" -#include "ImageProcessing/Filters/ConnectedComponentImageFilter.hpp" -#include "ImageProcessing/Filters/ReadMhaFileFilter.hpp" -#include "ImageProcessing/Filters/WriteImageFilter.hpp" -#include "ImageProcessing/ImageProcessing_test_dirs.hpp" +#include "NXImageProcessing/Filters/BinaryThresholdImageFilter.hpp" +#include "NXImageProcessing/Filters/ConnectedComponentImageFilter.hpp" +#include "NXImageProcessing/Filters/ReadMhaFileFilter.hpp" +#include "NXImageProcessing/Filters/WriteImageFilter.hpp" +#include "NXImageProcessing/NXImageProcessing_test_dirs.hpp" #include "SimplnxCore/Filters/ConvertDataFilter.hpp" #include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Core/Application.hpp" diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp index 8adf6067ae..a6310c0e84 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp @@ -1129,8 +1129,8 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: bounded true-2-D cancell } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: native provisional capacity decisions require no large forest", - "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: native provisional capacity decisions require no large forest", + "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) @@ -1183,7 +1183,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: native provisional capacit // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reuse consequence model", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: small legacy zero and reuse consequence model", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); // Literal wrapped IDs in a tiny real uint32 union-find. This is not a 2^32-entry forest execution. @@ -1212,8 +1212,8 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: small legacy zero and reus // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor consequence model preserves a foreground root", - "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: tiled zero-neighbor consequence model preserves a foreground root", + "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); const bool fullyConnected = GENERATE(false, true); @@ -1269,8 +1269,8 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: tiled zero-neighbor conseq // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: capacity warning context retains independent delivered diagnostics", - "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: capacity warning context retains independent delivered diagnostics", + "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) @@ -1294,8 +1294,8 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: capacity warning context r // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: seeded warning context survives resident retry copy error and cancellation", - "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: seeded warning context survives resident retry copy error and cancellation", + "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) @@ -1354,8 +1354,8 @@ TEST_CASE("ImageProcessing::ConnectedComponentEngine: seeded warning context sur // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentEngine: allocation addressability and I/O failures remain errors under both policies", - "[ImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") +TEST_CASE("NXImageProcessing::ConnectedComponentEngine: allocation addressability and I/O failures remain errors under both policies", + "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { UnitTest::LoadPlugins(); const auto handling = GENERATE(ConnectedComponentLabelLimitHandling::Error, ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy); diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp index a46f5d9392..6abcf5b5e6 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp @@ -94,7 +94,7 @@ std::vector MakeBlobField(usize dimX, usize dimY, usize dimZ) } // namespace // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: provisional label limit policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: provisional label limit policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); const ConnectedComponentImageFilter filter; @@ -126,7 +126,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: provisional label lim // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: both policies preserve literal connectivity labels and uint32 shape", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: both policies preserve literal connectivity labels and uint32 shape", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -179,7 +179,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: both policies preserv // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: many provisional runs can form one exact component", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: many provisional runs can form one exact component", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -213,7 +213,7 @@ TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: many provisional runs // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::ConnectedComponentImageFilter: execution cancellation keeps the filter cancellation result", "[ImageProcessing][Issue1874][ConnectedComponentImageFilter]") +TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: execution cancellation keeps the filter cancellation result", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index aa26cfa14a..8084e58e49 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -1,6 +1,6 @@ #include "C15CompatibilityTestUtils.hpp" -#include "ImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" +#include "NXImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" #include "NXImageProcessing/Filters/CurvatureFlowImageFilter.hpp" @@ -405,7 +405,7 @@ void requireCurvatureFlowConstant(DataStructure& dataStructure, const DataPath& } } // namespace -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid time step is rejected before execution", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid time step is rejected before execution", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -423,7 +423,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid time step is rejec UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid active spacing is rejected for no-op requests", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid active spacing is rejected for no-op requests", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -455,7 +455,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: invalid active spacing is } } -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: finite negative spacing and unused Z remain accepted", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: finite negative spacing and unused Z remain accepted", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -472,8 +472,8 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: finite negative spacing an UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: constant finite input preserves type and value", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, - int32, uint32, int64, uint64, float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: constant finite input preserves type and value", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, + uint16, int32, uint32, int64, uint64, float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -483,7 +483,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: constant finite i requireCurvatureFlowConstant(dataStructure, inputPath, TestType{7}, {.TimeStep = 0.05, .Iterations = 2}); } -TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: zero operation copies int64 values exactly", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero operation copies int64 values exactly", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -497,7 +497,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: zero operation co } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: terminal integer conversion has exact bounds", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, +TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: terminal integer conversion has exact bounds", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -539,7 +539,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: terminal integer } // NOLINTEND(bugprone-chained-comparison) -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating narrowing retains IEEE values", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating narrowing retains IEEE values", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); using ImageProcessing::detail::NarrowFloatingImageValue; @@ -556,7 +556,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating narrowing retains } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stencil truncates then clips once", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: independent one-step stencil truncates then clips once", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -593,7 +593,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: independent one-step stenc } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op preserves integer endpoints", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op preserves integer endpoints", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -605,7 +605,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op pres } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer result errors", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer result errors", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -621,7 +621,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong terminal routes honor the stencil", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong terminal routes honor the stencil", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -669,7 +669,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong ter // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op propagates transfer failures and cancellation", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op propagates transfer failures and cancellation", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -699,7 +699,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: native no-op propagates tr // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: resident wrapper merges clipping warning with copy failure", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: resident wrapper merges clipping warning with copy failure", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -723,7 +723,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: resident wrapper merges cl // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output retains nonfinite and overflow behavior", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating output retains nonfinite and overflow behavior", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -762,7 +762,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: floating output r // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded native transfers", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded native transfers", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -789,7 +789,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded n // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits retain clipping and transfer warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits retain clipping and transfer warnings", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -850,7 +850,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D terminal exits retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D terminal exits retain warnings", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -904,7 +904,7 @@ TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D termi // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain warnings", "[ImageProcessing][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain warnings", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { enum class Exit : uint8 { @@ -1086,7 +1086,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero iterations is a no- } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[ImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[NXImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 75de54d5a7..9199c7ffff 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -107,7 +107,7 @@ class HConvexTransferStore : public DataStore }; } // namespace -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: integer fractional Height fails preflight", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: integer fractional Height fails preflight", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -128,8 +128,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: integer fractional Heig } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast with unchanged type", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, - uint32, int64, uint64, float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zero contrast with unchanged type", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, + int32, uint32, int64, uint64, float32, float64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -172,7 +172,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: floating fractional Height retains fractional contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: floating fractional Height retains fractional contrast", "[NXImageProcessing][Issue1874][HConvexImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -209,7 +209,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: floating fractional Hei // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: native 64-bit unit contrast above double precision", "[ImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit unit contrast above double precision", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -241,7 +241,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: native 64-bit unit cont // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::HConvexImageFilter: exact signed 64-bit marker boundaries in both directions", "[ImageProcessing][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: exact signed 64-bit marker boundaries in both directions", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); constexpr int64 minimum = std::numeric_limits::lowest(); @@ -273,7 +273,7 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact signed 64-bit marker bound // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: exact integer marker zero unit and huge Heights", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: exact integer marker zero unit and huge Heights", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -310,7 +310,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: exact integer marker ze // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker arithmetic requires explicit opt-in", "[ImageProcessing][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker arithmetic requires explicit opt-in", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); constexpr int64 inputValue = (int64{1} << 54) + 1; @@ -331,7 +331,7 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker arithmetic requires // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker Height immediately below 2^64", "[ImageProcessing][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker Height immediately below 2^64", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); ImageProcessing::ReconstructionMarkerOptions options; @@ -352,7 +352,8 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: exact marker Height immediately // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: signed contrast overflow and adjacent representable contrast", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, int16, int32, int64) +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: signed contrast overflow and adjacent representable contrast", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, int16, int32, + int64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -401,7 +402,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: signed contrast overflo // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero contrast at native integer bounds", "[ImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zero contrast at native integer bounds", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -431,7 +432,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HConvexImageFilter: zero Height gives zero // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::HConvexImageFilter: whole Height contrast is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: whole Height contrast is stable under a signed baseline shift", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -457,7 +458,7 @@ TEST_CASE("ImageProcessing::HConvexImageFilter: whole Height contrast is stable // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::HConvexImageFilter: chunked contrast preserves transfer Results and cancellation", "[ImageProcessing][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: chunked contrast preserves transfer Results and cancellation", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); HConvexTransferStore input(2); @@ -769,7 +770,7 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: ITK real-image golden (HConvex } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::C15Compatibility resident exact widening supports fractional HConvex height", "[ImageProcessing][C15Compatibility][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident exact widening supports fractional HConvex height", "[NXImageProcessing][C15Compatibility][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index 7dc82260f1..191a8e00c1 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -1,6 +1,6 @@ #include "C15CompatibilityTestUtils.hpp" -#include "ImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" -#include "ImageProcessing/Filters/RegionalMinimaImageFilter.hpp" +#include "NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" +#include "NXImageProcessing/Filters/RegionalMinimaImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" @@ -56,7 +56,7 @@ Arguments makeHMinimaArguments(const DataPath& inputPath, float64 height, bool f } // namespace // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: integer fractional Height fails preflight", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: integer fractional Height fails preflight", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -80,8 +80,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: integer fractional Heig // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves values and type", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, - uint64, float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves values and type", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, + int64, uint64, float32, float64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -128,7 +128,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves v // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: floating fractional Height retains fractional minima", "[ImageProcessing][Issue1874][HMinimaImageFilter]", float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: floating fractional Height retains fractional minima", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -168,7 +168,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: floating fractional Hei // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: native 64-bit marker preserves units above double precision", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: native 64-bit marker preserves units above double precision", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -203,8 +203,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: native 64-bit marker pr // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves native integer bounds", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, - int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves native integer bounds", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, + uint32, int64, uint64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -241,7 +241,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: zero Height preserves n // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height raises or saturates native integer markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: whole Height raises or saturates native integer markers", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); @@ -279,7 +279,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height raises or // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: large whole Height retains exact native 64-bit markers", "[ImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: large whole Height retains exact native 64-bit markers", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -318,7 +318,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::HMinimaImageFilter: large whole Height reta // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::HMinimaImageFilter: whole Height is stable under a signed baseline shift", "[ImageProcessing][Issue1874][HMinimaImageFilter]") +TEST_CASE("NXImageProcessing::HMinimaImageFilter: whole Height is stable under a signed baseline shift", "[NXImageProcessing][Issue1874][HMinimaImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -589,7 +589,7 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::C15Compatibility resident fractional minima produce watershed markers", "[ImageProcessing][C15Compatibility][Issue1874][HMinimaImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident fractional minima produce watershed markers", "[NXImageProcessing][C15Compatibility][Issue1874][HMinimaImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index 207999e62b..e43bfe679c 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -444,7 +444,7 @@ std::vector MakeNegativeMarkerField(usize dx, usize dy, usize dz) } // namespace // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker conversion policy defaults to Error", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker conversion policy defaults to Error", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const WSFilter filter; @@ -472,8 +472,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: default Error rejects a changed uint64 marker before flooding", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: default Error rejects a changed uint64 marker before flooding", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); @@ -504,8 +504,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: defaul } // NOLINTEND(bugprone-chained-comparison) -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32_MAX marker remains valid with uint64 output", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32_MAX marker remains valid with uint64 output", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); @@ -528,8 +528,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32 REQUIRE(outputArrayPtr->getDataStoreRef().getValue(0) == uint64{4294967295ULL}); } -TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: native lossless seeds propagate exactly", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: native lossless seeds propagate exactly", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -557,8 +557,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilte } } -TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: wide marker bounds have literal legacy results", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: wide marker bounds have literal legacy results", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -576,8 +576,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilte } } -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias and lost seed choices have exact flood consequences", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias and lost seed choices have exact flood consequences", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -590,8 +590,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker copy checks chunk boundaries before writing", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker copy checks chunk boundaries before writing", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -633,7 +633,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invalid policy is rejected before execution", "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invalid policy is rejected before execution", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -651,8 +652,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invali // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: diagnostic context keeps exact 64-bit extrema", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: diagnostic context keeps exact 64-bit extrema", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -686,8 +687,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilte // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy cancellation retains observed warning only", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy cancellation retains observed warning only", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -715,8 +716,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy c // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy warning survives transfer failure or allocation failure", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy warning survives transfer failure or allocation failure", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -760,8 +761,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy w // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conversion warning survives later facade outcomes", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conversion warning survives later facade outcomes", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -822,8 +823,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conver // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: successful transfer warnings survive later marker-copy failure", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: successful transfer warnings survive later marker-copy failure", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -853,8 +854,8 @@ TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: succes // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restoration retains transfer warnings through a short final chunk", - "[ImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") +TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restoration retains transfer warnings through a short final chunk", + "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 7c85f49ce2..ab9c878672 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -126,7 +126,7 @@ void RunCompactInt16Otsu(DataStructure& ds, const DataPath& inputPath) } // namespace // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy is explicit and defaults to Error", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; @@ -146,7 +146,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask selection is optional and linked", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask selection is optional and linked", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; @@ -180,7 +180,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask sel // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has a separate uint8 control", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has a separate uint8 control", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; @@ -198,8 +198,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: preflight checks the complete configured uint8 class interval", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: preflight checks the complete configured uint8 class interval", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); @@ -393,7 +393,7 @@ class OtsuTransferStore : public DataStore } // namespace // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains literal endpoint frequencies and labels", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains literal endpoint frequencies and labels", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -429,7 +429,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains lite } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -446,8 +446,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects eac } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", bool, uint8) +TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", bool, uint8) { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -480,7 +480,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask pre } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite data is required in both modes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite data is required in both modes", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -501,7 +501,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite d UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask collision and tuple validation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask collision and tuple validation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -549,7 +549,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask c } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", +TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", int64, uint64) { UnitTest::LoadPlugins(); @@ -575,7 +575,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: full nat // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants and detectable numeric collapse are distinct", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: true constants and detectable numeric collapse are distinct", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; @@ -645,8 +645,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: true constants an // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer ring preserves full payload batches at its grant boundary", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuRingGrant]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer ring preserves full payload batches at its grant boundary", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuRingGrant]") { // The counting store exercises charged scan buffers and records caller transfers. It has no disk backing. constexpr usize valueCount = 16; @@ -688,7 +688,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: optional integer // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); std::atomic_bool shouldCancel{false}; @@ -1042,7 +1042,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: ITK real-image } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeline imports receive intentional defaults", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeline imports receive intentional defaults", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; @@ -1072,8 +1072,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeli // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -1113,8 +1113,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection va // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep explicit Warn endpoints and empty selected data errors", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep explicit Warn endpoints and empty selected data errors", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; @@ -1146,7 +1146,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep ex // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; @@ -1216,7 +1216,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper co } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal class labels coexist with masked label zero", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal class labels coexist with masked label zero", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -1237,7 +1237,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal cla } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds keep ordinary Warn endpoint classes", "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds keep ordinary Warn endpoint classes", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -1265,8 +1265,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds ke // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid may equal minimum with usable two-bin separation", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuCentroidSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid may equal minimum with usable two-bin separation", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuCentroidSafeRed]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -1313,8 +1313,8 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid ma // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summaries preserve literal counts across resident and buffered batches", - "[ImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summaries preserve literal counts across resident and buffered batches", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); @@ -1370,7 +1370,7 @@ TEST_CASE("ImageProcessing::OtsuMultipleThresholdsImageFilter: floating summarie // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::C15Compatibility resident validity masked Otsu feeds selected components", "[ImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident validity masked Otsu feeds selected components", "[NXImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 8784361705..4a97ba7d69 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -1,6 +1,6 @@ #include "C15CompatibilityTestUtils.hpp" -#include "ImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ItkGoldenTestUtils.hpp" +#include "NXImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" #include "NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp" @@ -205,7 +205,7 @@ std::vector ReadArray(const DataStructure& ds, const DataPath& path) } } // namespace -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active sigma is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active sigma is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -226,7 +226,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite act UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -249,7 +249,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite act UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected before execution", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected before execution", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -275,7 +276,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effec UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negative spacing and unused Z remain accepted", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negative spacing and unused Z remain accepted", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -295,7 +296,7 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negati } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected validation branch has a diagnostic", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected validation branch has a diagnostic", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -340,8 +341,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: constants retain type shape and selected endpoint behavior", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int8, int16, int32, int64, float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: constants retain type shape and selected endpoint behavior", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int8, int16, int32, int64, float32, float64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -393,7 +394,7 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: cons // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 output retains float32 interpass precision", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 output retains float32 interpass precision", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -418,8 +419,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 outpu // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and buffered terminal conversion follow literal expectations", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and buffered terminal conversion follow literal expectations", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); @@ -440,8 +441,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and bu // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: floating terminal values retain nonfinite behavior", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", - float32, float64) +TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: floating terminal values retain nonfinite behavior", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", float32, float64) { UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); @@ -467,8 +468,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: floa // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: terminal nonfinite integer errors and binary64 boundaries", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int32, int64) +TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: terminal nonfinite integer errors and binary64 boundaries", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int32, int64) { UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); @@ -500,8 +501,8 @@ TEMPLATE_TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: term // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warning survives later transfer failure and cancellation", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warning survives later transfer failure and cancellation", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const usize outcome = GENERATE(usize{0}, usize{1}, usize{2}); @@ -530,8 +531,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warn // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plane sinks retain successful warnings before error or cancellation", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plane sinks retain successful warnings before error or cancellation", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const SmoothingTransferStore source(64, 0.0F); @@ -569,7 +570,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plan // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tiled transfers keep prior row warnings on failure", "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tiled transfers keep prior row warnings on failure", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const bool write = GENERATE(false, true); @@ -583,8 +585,8 @@ TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tile } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -TEST_CASE("ImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis materializations narrow finite overflow explicitly", - "[ImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis materializations narrow finite overflow explicitly", + "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const uint32 axis = GENERATE(uint32{0}, uint32{1}, uint32{2}); @@ -1011,8 +1013,8 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: ITK real-im } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("ImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", - "[ImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", + "[NXImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); From 8b9c650e746a89b0a114dcef723632690b5ebad3 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 16:12:08 -0500 Subject: [PATCH 39/98] STY: Resolve C15 image-writer clang-tidy diagnostics Use the required helper namespace and C++20 ranges calls. Keep palette arguments distinct and make index grouping explicit. Record the existing contiguous-bool-buffer and namespace exceptions at the lines that require them. Preserve numeric and storage behavior. Signed-off-by: Jessica Marquis --- .../Algorithms/WriteImageColorUtilities.hpp | 29 ++++++++++------- .../Filters/Algorithms/WriteImageDirect.cpp | 32 +++++++++---------- .../Filters/Algorithms/WriteImageScanline.cpp | 32 +++++++++---------- .../utils/MathOutputUtilities.hpp | 2 ++ .../NXImageProcessing/test/WriteImageTest.cpp | 8 ++--- 5 files changed, 56 insertions(+), 47 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 5cae16c59d..5d96b70c89 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -20,10 +20,10 @@ #include #include -/** @namespace nx::core::WriteImageColor +/** @namespace nx::core::write_image_color * @brief Contains color safety operations used only by the image writer. */ -namespace nx::core::WriteImageColor +namespace nx::core::write_image_color { struct SliceExtents { @@ -97,19 +97,26 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, { return MakeErrorResult>(-27020, "Input array is empty."); } - constexpr usize targetPageBytes = 1024 * 1024; + constexpr usize targetPageBytes = usize{1024} * usize{1024}; const bool scanMask = std::is_floating_point_v && maskArray != nullptr; - const usize maskBytes = scanMask ? (maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8)) : 0; + usize maskBytes = 0; + if(scanMask) + { + maskBytes = maskArray->getDataType() == DataType::boolean ? sizeof(bool) : sizeof(uint8); + } const usize pageCount = std::min(count, std::max(1, targetPageBytes / (sizeof(T) + maskBytes))); // Scalar input can use bool. The store needs a contiguous bool buffer, which std::vector cannot provide. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) auto values = std::make_unique(pageCount); // The Boolean mask store also needs a contiguous bool buffer. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) std::unique_ptr boolMask; std::unique_ptr> byteMask; if(scanMask) { if(maskArray->getDataType() == DataType::boolean) { + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) std::vector cannot provide a contiguous bool buffer. boolMask = std::make_unique(pageCount); } else @@ -275,8 +282,8 @@ float32 Normalize(T value, T minimum, T maximum) */ inline Result<> ValidatePalette(const std::vector& points, const std::vector& bins) { - if(points.size() < 8 || points.size() % 4 != 0 || bins.size() != points.size() / 4 || !std::all_of(points.begin(), points.end(), [](float32 value) { return std::isfinite(value); }) || - !std::all_of(bins.begin(), bins.end(), [](float32 value) { return std::isfinite(value); }) || !std::is_sorted(bins.begin(), bins.end())) + if(points.size() < 8 || points.size() % 4 != 0 || bins.size() != points.size() / 4 || !std::ranges::all_of(points, [](float32 value) { return std::isfinite(value); }) || + !std::ranges::all_of(bins, [](float32 value) { return std::isfinite(value); }) || !std::ranges::is_sorted(bins)) { return MakeErrorResult(-27029, "Write Image requires at least two finite color controls in position order. Select a valid color preset."); } @@ -287,12 +294,12 @@ inline Result<> ValidatePalette(const std::vector& points, const std::v * @brief Uses the existing linear palette interpolation and truncating RGB bytes. * Checked endpoints and usize indexes avoid invalid segment/channel casts. * The caller validates the palette once before entering the pixel loop. - * @param value Finite normalized coordinate in [0, 1]. * @param bins Validated normalized control positions. + * @param value Finite normalized coordinate in [0, 1]. * @param points Validated packed position/RGB controls. * @return Three uint8 channels, with the existing truncation rule. */ -inline std::array ComputeRgb(float32 value, const std::vector& bins, const std::vector& points) +inline std::array ComputeRgb(const std::vector& bins, float32 value, const std::vector& points) { const usize last = bins.size() - 1; usize left = 0; @@ -305,7 +312,7 @@ inline std::array ComputeRgb(float32 value, const std::vector } else if(value > 0) { - right = static_cast(std::lower_bound(bins.begin(), bins.end(), value) - bins.begin()); + right = static_cast(std::ranges::lower_bound(bins, value) - bins.begin()); right = std::min(last, std::max(1, right)); left = right - 1; const float32 width = bins[right] - bins[left]; @@ -317,9 +324,9 @@ inline std::array ComputeRgb(float32 value, const std::vector std::array rgb{}; for(usize channel = 0; channel < rgb.size(); ++channel) { - const float64 scaled = (points[left * 4 + channel + 1] * (1.0 - fraction) + points[right * 4 + channel + 1] * fraction) * 255; + const float64 scaled = (points[(left * 4) + channel + 1] * (1.0 - fraction) + points[(right * 4) + channel + 1] * fraction) * 255; rgb.at(channel) = static_cast(std::clamp(scaled, 0.0, 255.0)); } return rgb; } -} // namespace nx::core::WriteImageColor +} // namespace nx::core::write_image_color diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 9c7786dd4b..275ae91992 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -197,16 +197,16 @@ struct ColorizeVolumeFunctor { // The operation calculates the range in pages and then colorizes bounded slices. template - Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const WriteImageColor::SliceExtents& sliceExtents, const std::vector& binPoints, + Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const write_image_color::SliceExtents& sliceExtents, const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, const std::atomic_bool& shouldCancel, - const std::function(std::vector&, usize)>& writeSlice, const WriteImageColor::ProgressCallbacks& progress) + const std::function(std::vector&, usize)>& writeSlice, const write_image_color::ProgressCallbacks& progress) { const auto& [sliceW, sliceH, sliceCount] = sliceExtents; - if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) + if(Result<> sizeResult = write_image_color::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) { return sizeResult; } - auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); + auto rangeResult = write_image_color::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); if(rangeResult.invalid()) { return ConvertResult(std::move(rangeResult)); @@ -272,8 +272,8 @@ struct ColorizeVolumeFunctor } const T sourceValue = sliceValues[dstPixel]; - const float32 nValue = WriteImageColor::Normalize(sourceValue, range.Minimum, range.Maximum); - const std::array rgb = WriteImageColor::ComputeRgb(nValue, binPoints, controlPoints); + const float32 nValue = write_image_color::Normalize(sourceValue, range.Minimum, range.Maximum); + const std::array rgb = write_image_color::ComputeRgb(binPoints, nValue, controlPoints); sliceBuffer[dst + 0] = rgb[0]; sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; @@ -413,15 +413,15 @@ Result<> WriteImageDirect::operator()() ThrottledMessageHandler progressThrottle(m_MessageHandler); progressThrottle.reset(sliceCount, "Writing image slices"); m_MessageHandler.sendInfoMessage("Preparing and writing image slices"); - const WriteImageColor::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, - .Rows = - [&](usize current, usize total) { - if(sliceCount == 1) - { - progressThrottle.updatePercent("Preparing image rows", current, total); - } - }}; - const WriteImageColor::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; + const write_image_color::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, + .Rows = + [&](usize current, usize total) { + if(sliceCount == 1) + { + progressThrottle.updatePercent("Preparing image rows", current, total); + } + }}; + const write_image_color::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; // Encode and atomically commit one independently named slice file. auto writeSlice = [&](std::vector& sliceBuffer, usize slice) -> Result<> { @@ -511,7 +511,7 @@ Result<> WriteImageDirect::operator()() return MakeErrorResult(-27022, fmt::format("Preset '{}' must define at least 2 control colors", m_InputValues.presetName)); } std::vector binPoints = ColorTableUtilities::NormalizeBinPoints(controlPoints); - if(Result<> paletteResult = WriteImageColor::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) + if(Result<> paletteResult = write_image_color::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) { return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(paletteResult)); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index c20c9d3174..e05d15cf17 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -380,16 +380,16 @@ struct ColorizeVolumeFunctor * @return Error from source I/O or the image writer. */ template - Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const WriteImageColor::SliceExtents& sliceExtents, const DataPath& dataArrayPath, + Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const write_image_color::SliceExtents& sliceExtents, const DataPath& dataArrayPath, const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, - const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const WriteImageColor::ProgressCallbacks& progress) + const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const write_image_color::ProgressCallbacks& progress) { const auto& [sliceW, sliceH, sliceCount] = sliceExtents; - if(Result<> sizeResult = WriteImageColor::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) + if(Result<> sizeResult = write_image_color::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) { return sizeResult; } - auto rangeResult = WriteImageColor::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); + auto rangeResult = write_image_color::ScanRange(dataArrayRef, maskArray, nonfinitePolicy, shouldCancel, progress.Range); if(rangeResult.invalid()) { return ConvertResult(std::move(rangeResult)); @@ -499,8 +499,8 @@ struct ColorizeVolumeFunctor } const T sourceValue = groupValues[groupValueIndex]; - const float32 nValue = WriteImageColor::Normalize(sourceValue, range.Minimum, range.Maximum); - const std::array rgb = WriteImageColor::ComputeRgb(nValue, binPoints, controlPoints); + const float32 nValue = write_image_color::Normalize(sourceValue, range.Minimum, range.Maximum); + const std::array rgb = write_image_color::ComputeRgb(binPoints, nValue, controlPoints); sliceBuffer[dst + 0] = rgb[0]; sliceBuffer[dst + 1] = rgb[1]; sliceBuffer[dst + 2] = rgb[2]; @@ -642,15 +642,15 @@ Result<> WriteImageScanline::operator()() ThrottledMessageHandler progressThrottle(m_MessageHandler); progressThrottle.reset(sliceCount, "Writing image slices"); m_MessageHandler.sendInfoMessage("Preparing and writing image slices"); - const WriteImageColor::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, - .Rows = - [&](usize current, usize total) { - if(sliceCount == 1) - { - progressThrottle.updatePercent("Preparing image rows", current, total); - } - }}; - const WriteImageColor::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; + const write_image_color::ProgressCallbacks progress{.Range = [&](usize current, usize total) { progressThrottle.updatePercent("Computing color range", current, total); }, + .Rows = + [&](usize current, usize total) { + if(sliceCount == 1) + { + progressThrottle.updatePercent("Preparing image rows", current, total); + } + }}; + const write_image_color::SliceExtents sliceExtents{.Width = sliceW, .Height = sliceH, .Count = sliceCount}; // Shared per-slice writer: names the file, writes via the ImageIO layer, commits atomically. auto writeSlice = [&](std::vector& sliceBuffer, usize slice) -> Result<> { @@ -734,7 +734,7 @@ Result<> WriteImageScanline::operator()() return MakeErrorResult(-27022, fmt::format("Preset '{}' must define at least 2 control colors", m_InputValues.presetName)); } std::vector binPoints = ColorTableUtilities::NormalizeBinPoints(controlPoints); - if(Result<> paletteResult = WriteImageColor::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) + if(Result<> paletteResult = write_image_color::ValidatePalette(controlPoints, binPoints); paletteResult.invalid()) { return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(paletteResult)); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp index b9faff33fd..b9ad6cbbea 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/MathOutputUtilities.hpp @@ -5,6 +5,8 @@ #include #include +// This header extends the established public ImageProcessing namespace. +// NOLINTNEXTLINE(readability-identifier-naming) namespace nx::core::ImageProcessing { /** diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 0988cc089c..1814b7c692 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -924,16 +924,16 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan o TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards use actionable results", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); - const auto empty = WriteImageColor::ValidateSize({.Width = 0, .Height = 1, .Count = 1}, false); + const auto empty = write_image_color::ValidateSize({.Width = 0, .Height = 1, .Count = 1}, false); SIMPLNX_RESULT_REQUIRE_INVALID(empty); REQUIRE(empty.errors().front().code == -27030); - const auto large = WriteImageColor::ValidateSize({.Width = std::numeric_limits::max(), .Height = 1, .Count = 1}, true); + const auto large = write_image_color::ValidateSize({.Width = std::numeric_limits::max(), .Height = 1, .Count = 1}, true); SIMPLNX_RESULT_REQUIRE_INVALID(large); REQUIRE(large.errors().front().code == -27032); - const auto invalidPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {1, 0}); + const auto invalidPalette = write_image_color::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {1, 0}); SIMPLNX_RESULT_REQUIRE_INVALID(invalidPalette); REQUIRE(invalidPalette.errors().front().code == -27029); - const auto validPalette = WriteImageColor::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {0, 1}); + const auto validPalette = write_image_color::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {0, 1}); SIMPLNX_RESULT_REQUIRE_VALID(validPalette); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) From 9318456f18cfe3a9bdbb51aa79a147a254a345ff Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 18:06:47 -0500 Subject: [PATCH 40/98] BUG: Reject nonnumeric Otsu masks in preflight Check the active mask array kind before typed access. Return a clear error for unsupported array kinds and preserve disabled-mask behavior. Cover active and inactive StringArray masks with a preflight regression. Signed-off-by: Jessica Marquis --- .../OtsuMultipleThresholdsImageFilter.cpp | 13 +++++-- .../OtsuMultipleThresholdsImageFilterTest.cpp | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 4282f2448c..0dbf02abb7 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -135,12 +135,19 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const labelOffset, lastLabel))}; } const auto maskPath = filterArgs.value(k_MaskArrayPath_Key); - const auto& mask = dataStructure.getDataRefAs(maskPath); + // ArraySelectionParameter also accepts nonnumeric array kinds. + const auto* maskPtr = dataStructure.getDataAs(maskPath); + if(maskPtr == nullptr) + { + constexpr int32 k_InvalidMaskArrayKind = -83100; + return {.outputActions = + MakeErrorResult(k_InvalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; + } const auto& input = dataStructure.getDataRefAs(selectedInputArray); - if(mask.getNumberOfTuples() != input.getNumberOfTuples()) + if(maskPtr->getNumberOfTuples() != input.getNumberOfTuples()) { return {.outputActions = MakeErrorResult(-8314, fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", - maskPath.toString(), mask.getNumberOfTuples(), selectedInputArray.toString(), input.getNumberOfTuples()))}; + maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), input.getNumberOfTuples()))}; } } diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index ab9c878672..ebbe7ff1ba 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -9,6 +9,7 @@ #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" @@ -19,6 +20,7 @@ #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/DataStructure/IDataStore.hpp" +#include "simplnx/DataStructure/StringArray.hpp" #include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IParameter.hpp" #include "simplnx/Filter/Parameters.hpp" @@ -1072,6 +1074,39 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipe // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: StringArray masks return a preflight error only when enabled", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + const bool useMask = GENERATE(true, false); + CAPTURE(useMask); + const auto application = Application::GetOrCreateInstance(); + REQUIRE(application != nullptr); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 4}); + const DataPath maskPath({"String Mask"}); + const auto* maskPtr = StringArray::CreateWithValues(dataStructure, "String Mask", ShapeType{2}, {"1", "1"}); + REQUIRE(maskPtr != nullptr); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(useMask)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.preflight(dataStructure, args); + if(useMask) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); + const auto& error = result.outputActions.errors().front(); + REQUIRE(error.code == -83100); + REQUIRE(error.message.find(maskPath.toString()) != std::string::npos); + REQUIRE(error.message.find("scalar bool or uint8") != std::string::npos); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions); + } + REQUIRE(dataStructure.getData(inputPath.replaceName("Output")) == nullptr); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { From 2996fb2ff76e9b9a77020896ef46bd5c16430e66 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 18:06:48 -0500 Subject: [PATCH 41/98] REV: Report image dimensions in size diagnostics Include slice extents and the index limit in color-size errors. Keep the existing guards and codes. Check the reported values and recovery advice without allocating an oversized image. Signed-off-by: Jessica Marquis --- .../Algorithms/WriteImageColorUtilities.hpp | 9 +++++++-- .../NXImageProcessing/test/WriteImageTest.cpp | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 5d96b70c89..005896e1db 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -53,12 +53,17 @@ Result<> ValidateSize(const SliceExtents& extents, bool hasMask) const usize bytesPerValue = sizeof(T) + (hasMask ? std::max(sizeof(bool), sizeof(uint8)) : 0); if(width == 0 || height == 0 || slices == 0 || width > maximum / height) { - return MakeErrorResult(-27030, "Write Image color slice dimensions are empty or too large. Reduce the image dimensions."); + return MakeErrorResult( + -27030, + fmt::format( + "Write Image color slice dimensions are empty or too large (width {}, height {}, slice count {}, index limit {}). Use positive dimensions whose width times height fits this limit.", width, + height, slices, maximum)); } const usize pixels = width * height; if(pixels > maximum / 3 || pixels > maximum / bytesPerValue || slices > maximum / (pixels * bytesPerValue)) { - return MakeErrorResult(-27032, "Write Image color buffer sizes exceed the supported index range. Reduce the image dimensions."); + return MakeErrorResult(-27032, fmt::format("Write Image color buffer sizes exceed the supported index range (width {}, height {}, slice count {}, index limit {}). Reduce the image dimensions.", + width, height, slices, maximum)); } return {}; } diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 1814b7c692..4a00ebadde 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -924,12 +924,25 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan o TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards use actionable results", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { UnitTest::LoadPlugins(); - const auto empty = write_image_color::ValidateSize({.Width = 0, .Height = 1, .Count = 1}, false); + constexpr usize maximum = std::numeric_limits::max(); + const auto empty = write_image_color::ValidateSize({.Width = 0, .Height = 2, .Count = 3}, false); SIMPLNX_RESULT_REQUIRE_INVALID(empty); REQUIRE(empty.errors().front().code == -27030); - const auto large = write_image_color::ValidateSize({.Width = std::numeric_limits::max(), .Height = 1, .Count = 1}, true); + const auto& emptyMessage = empty.errors().front().message; + CHECK(emptyMessage.find("width 0") != std::string::npos); + CHECK(emptyMessage.find("height 2") != std::string::npos); + CHECK(emptyMessage.find("slice count 3") != std::string::npos); + CHECK(emptyMessage.find("index limit " + std::to_string(maximum)) != std::string::npos); + CHECK(emptyMessage.find("positive dimensions") != std::string::npos); + const auto large = write_image_color::ValidateSize({.Width = maximum, .Height = 1, .Count = 2}, true); SIMPLNX_RESULT_REQUIRE_INVALID(large); REQUIRE(large.errors().front().code == -27032); + const auto& largeMessage = large.errors().front().message; + CHECK(largeMessage.find("width " + std::to_string(maximum)) != std::string::npos); + CHECK(largeMessage.find("height 1") != std::string::npos); + CHECK(largeMessage.find("slice count 2") != std::string::npos); + CHECK(largeMessage.find("index limit " + std::to_string(maximum)) != std::string::npos); + CHECK(largeMessage.find("Reduce the image dimensions") != std::string::npos); const auto invalidPalette = write_image_color::ValidatePalette({0, 0, 0, 0, 1, 1, 1, 1}, {1, 0}); SIMPLNX_RESULT_REQUIRE_INVALID(invalidPalette); REQUIRE(invalidPalette.errors().front().code == -27029); From 1367f8347c2511dbafc33e8debf3ce59a7e18f88 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 18:06:48 -0500 Subject: [PATCH 42/98] DOC: Correct Write Image input type guidance Describe the supported numeric image input types. Keep the existing Boolean mask guidance. Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/docs/WriteImageFilter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md index 84992545c5..d22c1ca758 100644 --- a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md @@ -66,7 +66,7 @@ Enabling *Create Color Table* converts a single-component **Data Array** to an R - *Select Preset...* chooses the color preset (for example grayscale, rainbow, or jet) that is applied to the array's values. - When *Create Color Table* is enabled, the **Input Image Data Array** must be a single-component numeric array, but it may be **any** numeric type: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, or float64. This is wider than the type restrictions that apply when writing an array's raw pixel values directly (see *Pixel Data Type Support By Format* above), since color-table mode always produces a uint8 RGB image regardless of the input array's type. If the selected array has more than one component, the filter will fail during preflight. -- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar pages. For floating-point input, it also reads the selected mask to check nonfinite values. Integer and Boolean inputs use the mask when rendering each slice. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. +- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar pages. For floating-point input, it also reads the selected mask to check nonfinite values. Integer inputs use the mask when rendering each slice. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. - If the finite range is constant, each unmasked voxel uses the preset's first control color. Masked voxels use the mask color. #### Nonfinite Input Handling From 2ea4885648c3e6c504c2cc44917dda1b4838fded Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Thu, 12 Nov 2026 18:26:20 -0500 Subject: [PATCH 43/98] STY: Use local naming for the Otsu mask error constant Keep the error code and behavior unchanged. Signed-off-by: Jessica Marquis --- .../Filters/OtsuMultipleThresholdsImageFilter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 0dbf02abb7..4dead11ade 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -139,9 +139,9 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const const auto* maskPtr = dataStructure.getDataAs(maskPath); if(maskPtr == nullptr) { - constexpr int32 k_InvalidMaskArrayKind = -83100; + constexpr int32 invalidMaskArrayKind = -83100; return {.outputActions = - MakeErrorResult(k_InvalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; + MakeErrorResult(invalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; } const auto& input = dataStructure.getDataRefAs(selectedInputArray); if(maskPtr->getNumberOfTuples() != input.getNumberOfTuples()) From 51b14ccef18286b3ff8638a44b28c941df29d57e Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 16:58:19 -0500 Subject: [PATCH 44/98] DOC: Distinguish image conversion contracts Document when to report conversion status and when to saturate a value. Explain how callers handle nonfinite output. Addresses review item 11. Signed-off-by: Jessica Marquis --- docs/Coding_Knowledge_Base.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/Coding_Knowledge_Base.md b/docs/Coding_Knowledge_Base.md index 3174a7a8ac..701759557b 100644 --- a/docs/Coding_Knowledge_Base.md +++ b/docs/Coding_Knowledge_Base.md @@ -746,11 +746,18 @@ Reset a prior fixed notation explicitly with `std::defaultfloat`. For printf, us ### Write floating-point output through a saturating cast -**Rule:** a filter whose output type can be an integer type (for example an output type that follows the input type) -writes each computed finite floating-point value through `ImageProcessing::detail::SaturateCastFromDouble()`, which -clamps it to the range of `U`, and reports one warning with the number of clipped values. A NaN or an infinity computed -for an integer output is an error that names the value's index and the output type; it is never written as 0. The -general rule is in the numeric-correctness reference of `cpp-best-practices`. +**Rule:** select the conversion helper that matches the algorithm's output and diagnostic requirements. + +- Use `ImageProcessing::detail::ConvertTerminalImageValue` when a filter must report clipping or reject nonfinite + integer output. Finite integer conversions truncate toward zero, then clip if the integer is out of range. The + result status identifies clipping. A nonfinite value for an integer output returns `NonFinite` and leaves the + destination untouched. A floating output can retain NaN and infinity. The caller combines the clipping + information into one filter warning and identifies a rejected value by its flat index. +- Use `ImageProcessing::detail::SaturateCastFromDouble()` for intentional clamping when only the converted value + is needed. For an integer destination, NaN maps to zero and the infinities map to the native bounds. Apply these + mappings only when the caller's nonfinite policy permits them. A converted value alone cannot identify clipping. + +See the numeric-correctness reference of `cpp-best-practices`. **Example of the broken rule:** `ExpImageFilter` casts `exp(double(value))` straight to the integer output, and `LogImageFilter`, `SquareImageFilter` and `TanImageFilter` do the same: `log(0)`, the `uint8` square of 16 and the @@ -760,9 +767,9 @@ general rule is in the numeric-correctness reference of `cpp-best-practices`. **What to look for:** `static_cast(floatingExpression)` where `U` can be integral. -**Pattern to use:** `SaturateCastFromDouble()` in -`simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp` for finite values; the nonfinite integer-output -error and the clipping warning of `SmoothingRecursiveGaussianImageFilter` and `CurvatureFlowImageFilter` (PR #1888). +**Pattern to use:** Curvature Flow and Smoothing Recursive Gaussian use `ConvertTerminalImageValue` for the +final conversion (PR #1888). They report one warning if clipping occurred and return an error for a nonfinite +integer result. Intensity Windowing uses `SaturateCastFromDouble` to clamp values to the destination type. ### Decide NaN and infinity handling by where the value comes from From 336b7221feb044d763931c9f0d901bbeb6b05bf5 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 16:58:19 -0500 Subject: [PATCH 45/98] DOC: Restore spacing in threshold examples Separate parameter names and numeric values in the examples. Addresses review item 14. Signed-off-by: Jessica Marquis --- .../ThresholdMaximumConnectedComponentsImageFilter.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md b/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md index 873316892c..1d4d92764c 100644 --- a/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ThresholdMaximumConnectedComponentsImageFilter.md @@ -14,9 +14,9 @@ A pixel is inside when its native intensity is at or above the selected lower th The score counts connected components of the would-be **nonzero output**, including components whose size is exactly **Minimum Object Size In Pixels**. This parameter is a pixel/voxel count and defaults to *0*. It changes the search score; it does not remove small objects from the final output. -**Inside Value** defaults to *1* and **Outside Value** defaults to *0*. Both are dimensionless uint8 values. With these defaults, the score counts in-band regions. Inside0/Outside1 scores the complement. If both values are nonzero, all pixels participate as foreground. If both are zero, none participate. A nonzero Outside Value can connect regions that would otherwise be separate. +**Inside Value** defaults to *1* and **Outside Value** defaults to *0*. Both are dimensionless uint8 values. With these defaults, the score counts in-band regions. Inside 0/Outside 1 scores the complement. If both values are nonzero, all pixels participate as foreground. If both are zero, none participate. A nonzero Outside Value can connect regions that would otherwise be separate. -The search stops when the interval is at most **2 input-intensity units** wide. This is an absolute rule for integers and floating inputs. It is not a relative tolerance or a measure of segmentation accuracy. For example, normalized values spanning 0 to 1 skip the search. Binary input `[0,1,0,1]` selects threshold0 and gives `[1,1,1,1]`, although another threshold could give more regions. Scaling the intensities can change the selected mask. The right candidate wins only if its score is strictly greater; ties select the left candidate. +The search stops when the interval is at most **2 input-intensity units** wide. This is an absolute rule for integers and floating inputs. It is not a relative tolerance or a measure of segmentation accuracy. For example, normalized values spanning 0 to 1 skip the search. Binary input `[0,1,0,1]` selects threshold 0 and gives `[1,1,1,1]`, although another threshold could give more regions. Scaling the intensities can change the selected mask. The right candidate wins only if its score is strictly greater; ties select the left candidate. For floating input, native rounding can repeat the complete search state while the interval is still wider than 2. The filter stops, retains the candidate selected by the normal comparison, and issues one precision warning. It does not choose a different endpoint or perform an exhaustive search. @@ -36,11 +36,11 @@ This single choice applies to image NaN, positive infinity and negative infinity - *Warn and Continue* uses only finite image values to establish native search bounds. Ordinary inclusive comparisons assign every nonfinite pixel Outside Value. One aggregate warning reports separate NaN and infinity counts. A positive-infinity parameter cap selects a finite native maximum; it does not include positive-infinity image pixels. - If a nonempty Warn input has no finite pixels, the filter skips all threshold/component searches and fills Outside Value. One aggregate warning states the counts and that no threshold was calculated. This is a defined fallback, not a fitted segmentation. No additional UI choice is required. -Outside0 gives no nonzero foreground in an all-Outside fallback. A nonzero Outside Value can make the entire image foreground downstream, or bridge finite regions in a mixed image. These changes affect object counts, fractions, boundaries and measurements. Later filters that require objects can fail on an empty mask or report different results on a whole-image mask. Warn does not waive cancellation, I/O, memory, addressability or strict component-capacity failures. +Outside 0 gives no nonzero foreground in an all-Outside fallback. A nonzero Outside Value can make the entire image foreground downstream, or bridge finite regions in a mixed image. These changes affect object counts, fractions, boundaries and measurements. Later filters that require objects can fail on an empty mask or report different results on a whole-image mask. Warn does not waive cancellation, I/O, memory, addressability or strict component-capacity failures. ### Numeric migration -The starting midpoint now includes the image minimum, and interval arithmetic avoids native integer overflow. This corrects offset, negative and wide-range cases. For input `[100,102]`, a nonbinding cap and Inside1/Outside0, the old starting threshold1 gave mask `[1,1]`; the corrected midpoint101 gives `[0,1]`. The output type is unchanged, but segmentation and downstream measurements can change. Constant images use their actual constant as the threshold. Old pipelines that omit the new nonfinite choice use Error and can now stop on questionable data that previously completed. +The starting midpoint now includes the image minimum, and interval arithmetic avoids native integer overflow. This corrects offset, negative and wide-range cases. For input `[100,102]`, a nonbinding cap and Inside1/Outside 0, the old starting threshold 1 gave mask `[1,1]`; the corrected midpoint 101 gives `[0,1]`. The output type is unchanged, but segmentation and downstream measurements can change. Constant images use their actual constant as the threshold. Old pipelines that omit the new nonfinite choice use Error and can now stop on questionable data that previously completed. The finite float32 `RA-Float.nrrd` regression fixture demonstrates this migration on a real image. Its range is -1146 to 32767. With default controls, the corrected initial midpoint changes from 16956.5 to 15810.5, and the final threshold changes from 24164.05859375 to 24166.107421875. Exactly 159 pixels change: foreground decreases from 99487 to 99328 pixels, and face-connected components decrease from 3715 to 3712. An independent six-neighbor flood fill confirms these counts; correcting only the initial seed reproduces the new mask. The retained heuristic can select fewer components after the arithmetic correction. Neither the count nor this regression is a segmentation-accuracy score or a global-maximum guarantee. From dfb6894972b79b31a08715e07f099714e2585d0e Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 17:07:03 -0500 Subject: [PATCH 46/98] DOC: Simplify image filter help Describe the choices, defaults, and saved pipeline behavior. Retain the limits that affect use and interpretation. Addresses review item 13. Signed-off-by: Jessica Marquis --- .../docs/ConnectedComponentImageFilter.md | 52 ++++++------ ...ologicalWatershedFromMarkersImageFilter.md | 69 +++++++-------- .../SmoothingRecursiveGaussianImageFilter.md | 56 ++++++------- .../docs/WriteImageFilter.md | 84 +++++++++---------- 4 files changed, 123 insertions(+), 138 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md b/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md index 5e892791dc..ec06c0c5f1 100644 --- a/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ConnectedComponentImageFilter.md @@ -8,48 +8,48 @@ NXImageProcessing (Segmentation) ## Description -Labels the objects in the input image: nonzero input voxels are foreground, and zero-valued voxels are background. Below the provisional ID limit, each distinct foreground object gets a unique label. Objects reached earlier by a raster-order scan (X fastest, then Y, then Z) get a lower label. Labels start at 1 and are consecutive; background is 0. +Labels connected foreground objects. Nonzero input voxels are foreground. Input voxels with value 0 are background. +The input must have one component and can use any signed or unsigned integer type. -The input array must be single-component (scalar) and may use **any integer type** (signed or unsigned). The filter tests each native input value only as zero or nonzero. A large uint64 value is valid foreground. Output is always **uint32**, with 4 bytes per value; input values are not converted to output label values. Below the provisional ID limit, the filter preserves the legacy ITK label values. +The output uses uint32 labels (4 bytes per voxel). Below the run ID limit, background has label 0 and each object has +a unique label. The filter assigns labels in scan order, starting at 1 and stepping along X first, then Y, then Z. +These labels match the legacy ITK filter below the run ID limit. ### Fully Connected -Set/Get whether the connected components are defined strictly by face connectivity or by face+edge+vertex connectivity. Default is FullyConnectedOff. For objects that are 1 pixel wide, use FullyConnectedOn. +**Fully Connected** is off by default. The filter joins voxels that share a face. Enable it to also join voxels that +touch at an edge or vertex. ### Provisional Label Limit Handling -A **provisional run ID** identifies one uninterrupted foreground run along X before connected runs are joined. Several provisional runs can form one final component. For example, stripes joined by a bridge can use many run IDs but produce one component. The limit therefore applies to provisional runs, not to the voxel count or the final component count. +The filter assigns one provisional ID to each uninterrupted foreground run along X. Multiple runs can join to form one +object. For example, a bridge can connect several stripes. The limit counts runs. -- **Error (default):** execution accepts provisional ID 4,294,967,295 and stops before creating ID 4,294,967,296. The check occurs when a new run needs an ID, so a large image can fail after a long first pass. New pipelines and existing pipelines without an explicit choice use Error. -- **Warn and Continue (Legacy Behavior):** execution sends one warning immediately when it first exceeds the limit. Normal completion and later processing or I/O errors retain that warning. The application's general cancellation handler can replace a cancelled result and its retained diagnostics; the immediate warning has already been sent. This choice keeps the old uint32 wrap behavior: ID 4,294,967,296 becomes background zero, the next ID reuses label 1, and later IDs reuse earlier labels. Foreground can disappear and distinct components can merge, including components already joined to other runs. Segmentation, feature counts, component sizes, and downstream per-feature statistics can be incorrect. The scanline route can join zero-labelled foreground runs to other runs; the tiled route skips zero labels in its previous-row neighbor checks. Different routes can therefore give different incorrect results after overflow. +- **Error** (default) accepts provisional ID 4,294,967,295. The filter stops if another run needs an ID. +- **Warn and Continue (Legacy Behavior)** reports one warning at the first overflow and continues. The first overflowing + ID becomes background 0. Later IDs reuse existing labels, starting at 1. -To run an old pipeline with intentional overflow compatibility, explicitly select *Warn and Continue (Legacy Behavior)*. Use this choice only if you accept the scientific consequences. It does not add capacity, preserve valid label identities after overflow, or guarantee completion. Correct support for more provisional IDs requires a separate algorithm or internal representation change. Shared component-counting and composite filters retain Error; they do not inherit this compatibility choice. +In Warn mode, a wrapped 0 in the previous row can cause scanline processing to label an adjacent object as background. +Tiled processing skips that previous-row 0, so the two routes can give different labels. In either route, foreground can +disappear and distinct objects can merge. These changes can affect object counts, sizes, and measurements. -Both choices retain independent allocation, addressability, I/O, and cancellation handling. Memory or I/O failures can still stop the filter. The policy does not add an all-or-nothing guarantee for an unrelated late output-write failure. +Saved pipelines without this choice use **Error**. Select **Warn and Continue (Legacy Behavior)** only if you need the +old overflow behavior and can accept its effects. A memory or file error can stop either choice. ## Algorithm -The filter scans the image in X-fastest order. It encodes each foreground run along X. A union-find table joins runs that have the selected connectivity. +The filter finds runs along X and joins neighboring runs with an equivalence table. A final pass assigns consecutive +labels. -After the first pass, the filter maps each provisional label to a consecutive label. The first output label is one. Each table entry represents a run, not a voxel. +For out-of-core 3D data, the filter keeps as much working data in memory as the available budget allows. If it can +keep the complete state in memory, it reads the input once. With less memory it can keep provisional labels in memory. +If the labels also do not fit, it reads the input twice and writes the output once in bounded groups. -For OOC 3D data, the filter first requests memory for the complete working state. A complete grant uses one input read and one output write. +For 2D data, wide rows can be processed in tiles. Temporary files can hold the previous row's labels. The final pass +reads input tiles and writes output tiles in bounded groups. -If this request is not complete, the filter requests memory for the uint32 provisional-label volume. A complete second grant keeps that volume in memory. - -If the second grant is not complete, the filter uses bounded replay. Pass 1 reads each input plane once. It encodes independent rows in parallel and keeps only adjacent-plane run data. - -After union resolution, pass 2 reads adjacent plane groups. It counts and maps independent rows in parallel. The group size uses at most one-twentieth of the complete replay working set, with a one-plane minimum. The shared cache budget can reduce this size. - -The bounded 3D route uses two full input reads and one full output write. Grouping changes the number of transfers, not the logical I/O. The route does not store a complete provisional-label volume. - -For true 2D data (`Z=1`), a checked scheduler uses at most 64 MiB for blocks. An extremely wide row uses X tiles. - -The tiled first pass can use temporary records for previous-row labels. The final pass rereads bounded input tiles and writes bounded output tiles. - -Below the provisional ID limit, all routes create labels at the same raster run starts and preserve the same union and consecutive-label order. Explicit legacy overflow retains each route's existing zero/reuse behavior. - -The equivalence tables remain resident memory proportional to the number of provisional runs, even when image transfers use bounded buffers or disk storage. Three uint32 tables with 2^32 entries alone need at least 48 GiB, before line records, image buffers, and other state. Allocation can fail before the ID limit is reached. This policy does not make the union-find tables bounded in size. +All routes keep equivalence tables in memory. Memory use grows with the number of provisional runs. Three uint32 tables +at the ID limit need at least 48 GiB, before other working data. A memory allocation can fail before the ID limit is reached. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md b/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md index 9bb22752e2..6875e1f227 100644 --- a/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/MorphologicalWatershedFromMarkersImageFilter.md @@ -8,62 +8,51 @@ NXImageProcessing (Watersheds / Segmentation) ## Description -Floods a grayscale image from user-supplied **markers**, which are integer seed labels. Each marked region grows over ascending gray levels until regions meet. The output labels identify the resulting basins. The markers control the initial regions; select them to represent the regions you want to separate. Marker IDs are dimensionless identities, not gray levels or array indexes. +Floods a grayscale image from integer seed labels called **markers**. Seed regions grow as the gray level rises. +The output labels identify the regions, or **basins**, formed when they meet. -Two flooding variants are provided via **Mark Watershed Line**: - -- **On (Meyer's algorithm, the default):** watershed-line voxels where distinct basins meet are left as label 0, separating the basins. -- **Off (Beucher's algorithm):** reached voxels are assigned to a basin without a separating line. An image with no surviving nonzero seed remains unmarked. - -**Fully Connected** selects the neighborhood used for flooding: face connectivity (6-connected in 3D, 4-connected in 2D) when off, or face+edge+vertex connectivity (26-connected in 3D, 8-connected in 2D) when on. +- **Mark Watershed Line** is on by default. The filter assigns zero where different basins meet. + With this choice off, the filter assigns reached pixels to basins without a separating line. +- **Fully Connected** is off by default. The filter uses face neighbors (4 in 2D, 6 in 3D). + Enable it to include diagonal neighbors (8 total in 2D, 26 in 3D). ### Input Guidance -- **Input Cell Data (grayscale):** a single-component scalar image of any numeric type. The flood ascends this image's gray levels. -- **Marker Cell Data:** a single-component **integer** scalar image with the same number of tuples as the grayscale input. Nonzero voxels are markers/seeds; a value of 0 means "unmarked". Voxels sharing the same nonzero label belong to the same seed region, so two disjoint blobs with the same label flood into one basin. - -### Output Interpretation +Select a single-component grayscale image and a single-component integer marker image with the same tuple count. Marker +ID 0 means unmarked. Pixels that use the same nonzero ID are one seed region, even if they are in separate locations. +If no nonzero seed remains, the output stays unmarked. -- A label image with the **same element type as the marker image**, the grayscale input's tuple and scalar component shapes, and the grayscale input's storage format. Watershed-line voxels (when Mark Watershed Line is on) are 0. Warn mode can change the original marker identities as described below. +The output has the marker array's element type and the grayscale image's shape and storage format. ### Marker ID Conversion Handling -The flood uses internal `uint32` labels. A **lossless round trip** means a native marker ID has the same value after conversion to `uint32` and back to its native type. - -- All `int8`, `uint8`, `int16`, `uint16`, `int32`, and `uint32` values round-trip, including negative signed values and their minimum values. -- For `int64` and `uint64`, the lossless direct range is **0 through 4294967295, inclusive**. Negative `int64` IDs do not round-trip. -- Zero means unmarked. `UINT32_MAX`, or 4294967295, is a valid in-bounds seed. It is not a reserved marker identity. - -**Marker ID Conversion Handling** has exactly two choices: - -- **Error (default):** check values during the existing bounded marker-copy pass. If an ID changes, stop before writing its affected scratch chunk and before flooding starts. The error identifies the marker array, flat index, native ID, and restored value. Previously copied scratch chunks or allocated output storage can remain; this is not a general rollback guarantee. -- **Warn and Continue (Legacy Behavior):** keep the old conversion, flood, and conversion back. If any marker changes, return one warning with the number of changed occurrences observed and the first changed marker's context. Valid round-tripping markers do not warn. This choice does not preserve unsupported native IDs or suppress independent allocation and I/O errors. +The filter converts marker IDs to `uint32` for flooding, then restores the marker type. All 8-, 16-, and 32-bit +integer values keep their original IDs, including signed negatives. For `int64` and `uint64`, the values that keep +their IDs range from 0 through 4,294,967,295, inclusive. -For example, in Warn mode: +- **Error** (default) stops before flooding if an ID changes during conversion. The error identifies the marker array, + flat index, original ID, and ID after conversion back to the marker type. +- **Warn and Continue (Legacy Behavior)** uses the old conversion and continues flooding. It reports how many marker + values changed and identifies the first change. -| Native marker | Internal and restored value | Consequence | +| Original marker in Warn mode | ID after conversion | Effect | | --- | --- | --- | -| `uint64` 4294967296 | 0 | The seed disappears into background. | -| `uint64` 4294967297 | 1 | It can merge with an existing seed 1. | -| `int64` -1 | 4294967295 | The seed is renamed in the `int64` output. | +| `uint64` 4,294,967,296 | 0 | The seed disappears. | +| `uint64` 4,294,967,297 | 1 | The seed can merge with seed 1. | +| `int64` −1 | 4,294,967,295 | The seed gets a different label. | -Seed loss, merging, or renaming can change basin boundaries and region sizes. Downstream feature counts and measurements can be incorrect for the original markers. Choose Warn only when you intentionally accept these consequences. - -Existing pipelines that omit the new choice use Error. To retain old lossy behavior, explicitly select Warn. Converting the marker array first preserves IDs only when every value fits the destination type; arbitrary narrowing is not a lossless repair. Lossless remapping is future work and is not an available choice. - -Copy, flood, and restore Results retain the conversion warning on normal completion and delivered later errors or cancellation. The generic filter wrapper can replace a successful cancelled execution Result and lose its warnings; warning retention at that inherited cancellation boundary is not guaranteed. Cancellation does not make a partial output a completed segmentation. - -This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK Morphological Watershed From Markers Image Filter. +Seed changes can alter basin boundaries, feature counts, and measurements. Saved pipelines without the choice use +**Error**. Select **Warn and Continue (Legacy Behavior)** only when you intend to keep the old conversion and can accept +those effects. ## Algorithm -Resident input uses an in-memory hierarchical queue. The filter processes the lowest gray level first. It uses FIFO order for voxels at the same level. - -The initial marker-copy pass checks the selected conversion policy using the same native values it copies. Transfers use at most 65536 marker values per chunk, with one native buffer and one `uint32` buffer. The check adds constant work per value and constant diagnostic state. It adds no extra image scan, remapping table, or resident copy of the full native marker volume. Internal marker/output scratch stores remain `uint32` and follow the grayscale storage format. - -An out-of-core endpoint keeps the same processing order with bounded, file-backed state. The engine gets an adaptive working-memory grant from the shared cache budget. It uses only the useful part of that grant and keeps a disk-backed fallback. If a complete state fits, the engine can keep it resident. Otherwise, it keeps fixed record pages in memory. Large partial 3-D state uses 32 x 32 x 8 voxel tiles so face neighbors usually share one page. +Flooding processes lower gray levels first and preserves arrival order at the same gray level. The filter checks +marker IDs as it copies them in groups of up to 65,536 values. -Eight- and sixteen-bit gray types use ordered gray-level buckets. Each bucket stores 64-bit voxel indexes in file-backed FIFO blocks. Wider integers and floating-point types use a file-backed minimum heap keyed by flood level and insertion order. Marker values are streamed during initialization and are not kept in each voxel record. Input and output transfers use bounded bulk I/O. +The in-memory path holds the flood state in memory. The out-of-core path can also use memory when the state fits. +For larger data it keeps the flood state on disk and reads and writes data in bounded groups. Both paths use the same +flood order. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md index feff6acd8c..2e2d5bf35e 100644 --- a/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/SmoothingRecursiveGaussianImageFilter.md @@ -8,53 +8,53 @@ NXImageProcessing (Smoothing) ## Description -Convolves the input image with a Gaussian kernel, computed with the Deriche 4th-order recursive IIR approximation (a fast, image-size-independent approximation to a true Gaussian convolution). One separable 0th-order pass is run along each filtered axis and the passes are cascaded, so the overall effect is a full N-dimensional Gaussian blur. This suppresses noise and fine detail while preserving the overall structure of the image. +Smooths an image with a recursive approximation of a Gaussian filter. Smoothing reduces fine detail and noise. -**Sigma** is specified per-axis (X, Y, Z), in the units of image spacing, so anisotropic smoothing and anisotropic spacing are both supported. Larger sigma produces stronger blurring along that axis. A `Z` size of 1 is treated as a 2D image and only the X and Y axes are filtered. - -The input array must have one component (scalar) and use `int8`, `int16`, `int32`, `int64`, `float32`, or `float64`. Unsigned types are not supported. The output has the **same type, tuple shape, and component shape as the input**. This is an ITK-free implementation of the legacy filter's recursive approximation. Exact agreement for all data and scales is not guaranteed. - -The filter requires at least 4 pixels along every filtered axis. +Select a single-component array of type int8, int16, int32, int64, float32, or float64. The output has the input type and +array shape. Each filtered axis needs at least four pixels. For a single Z slice, the filter uses only X and Y. ### Sigma -The standard deviation along each axis (X, Y, Z), in the units of image spacing. Default is 1 along every axis. For each active axis, Sigma must be finite and positive. Image spacing must be finite with an absolute value greater than `1e-8`. Finite negative spacing is accepted. The effective scale is Sigma divided by the absolute spacing; this quotient must be finite and positive, and the formed recurrence coefficients must be finite. +*Sigma* is the Gaussian standard deviation in image-spacing units. It defaults to 1 along each axis. A larger value +produces more smoothing along that axis. -These checks reject invalid coefficient formation. They do not impose a maximum Sigma or guarantee accuracy for extreme scales. When Z has size 1, Z Sigma and Z spacing are unused and are not checked. +For each filtered axis, Sigma must be finite and positive. The spacing magnitude must be finite and greater than +`1e-8` in image-spacing units. The filter checks that Sigma divided by the spacing magnitude gives a finite positive +scale and that the calculation can form finite filter coefficients. Accuracy can decrease at extreme scales. ### Normalize Across Scale -Whether to normalize the Gaussian over scale-space (Lindeberg's scale normalization). This affects derivative magnitudes but does not change 0th-order smoothing. Default is Off. +This choice defaults to Off. It affects Gaussian derivatives. Smoothing uses order zero and has the same result with +either choice. ## Algorithm -The filter applies one separable zero-order recursive-Gaussian pass along each active image axis. Resident **Data Arrays** retain the direct parallel cascade. For a three-dimensional disk-backed image, the Z pass first batches consecutive Y rows across the volume; the following X and Y passes are fused within each Z plane and cast directly to the output. A two-dimensional image fuses its Y and X passes directly from input to output and needs no full-image working array. Store reads and writes remain serial and contiguous; only staged in-memory lines are filtered in parallel. +The filter smooths one axis at a time. It calculates in double precision and stores intermediate values as +**float32** between axes. An output of type float64 cannot recover detail lost in these intermediate values. +Large int64 values can also lose detail. -Each recurrence uses double coefficients and arithmetic, then stores its result as **Float32 before the next axis**. This rounding is part of the filter's contract. Float64 output does not restore detail lost in those Float32 values. In particular, this filter does not preserve arbitrary Int64 intensity detail. - -Resident execution retains its Float32 working image and direct parallel path. Three-dimensional disk execution uses a file-backed Float32 working image, a fixed number of plane buffers, and a Z slab whose buffers target at most 64 MiB; one XZ row is the minimum transfer unit. Two-dimensional disk execution uses bounded row or column blocks and file-backed recurrence checkpoints. Split-column plans use a file-backed Float32 working image. Scratch files consume disk space without requiring a resident array for the whole image. Store transfers remain serial. +The in-memory path filters lines in parallel and holds a float32 working image. The three-dimensional out-of-core path +uses a disk-backed working image and processes bounded groups of rows and planes. The two-dimensional out-of-core path +works in bounded blocks and may use disk-backed intermediate data. Both out-of-core paths read and write stores +serially. ### Final values and diagnostics -For integer output, finite final values **truncate toward zero, then clip to the native output range only if needed**. For example, an Int8 result of `127.75` becomes `127` without clipping, while `128` clips to `127`. One warning reports any clipping during a filter run. A constant Int32 maximum can round to `2147483648` between passes; its final output clips back to `2147483647` and reports the warning. - -A nonfinite final value (`NaN` or either infinity) cannot be stored as an integer. The filter returns an error with the flat value index and output type. Floating output retains floating nonfinite behavior. Finite overflow when narrowing a double recurrence result to Float32 is explicitly mapped to infinity with the same sign; later arithmetic can produce NaN. No integer clipping occurs between axes. +For integer output, the filter truncates each finite final value toward zero. It clips the result to the output type's +range if needed. For example, an int8 result of 127.75 becomes 127 without clipping, while 128 clips to 127. The filter +reports one warning if any values clip. -The same terminal policy applies to all five output routes: - -| Output route | Final conversion | -| --- | --- | -| Direct resident output | Checked parallel conversion of Float32 values | -| Buffered store output | Checked conversion in bounded transfer buffers | -| Two-dimensional plane cascade | Checked conversion before a plane is written | -| Three-dimensional plane cascade | Checked conversion after the Z, X, and Y passes | -| Bounded two-dimensional disk cascade | Checked conversion before each output block is written | - -The smoothing helpers retain warnings delivered by completed transfers on their own success, error, and cancellation returns. However, the public filter wrapper can replace a successful execution result with a cancellation error and discard its execution warnings. Warning retention after cancellation is therefore not guaranteed at the public filter boundary. Cancellation and later I/O failures can leave completed output blocks in place; this filter does not promise an all-or-nothing output update. +If the final value is NaN or infinity, integer output fails with an error that identifies the value's index and output +type. Floating-point output retains NaN and infinity. A double value too large for an intermediate float32 becomes +infinity with the same sign. ### Migration and downstream use -Earlier versions could use an unchecked integer conversion, including an invalid result at the Int32 maximum. Review clipping warnings when updating an existing pipeline. Clipping and recursive overshoot can change the result of later thresholding or segmentation. The array size stays unchanged. Use floating input when you need to retain fractional or nonfinite output, while allowing for the unchanged Float32 inter-pass precision. +Review clipping warnings after updating a saved pipeline. Earlier versions could produce an invalid integer result +when the smoothed value exceeded the output type's range. Clipping can change later thresholding and segmentation. +Use floating-point input if you need fractional or nonfinite output. Intermediate values still have float32 precision. + +Cancellation or a later file error can leave completed blocks in the output. % Auto generated parameter table will be inserted here diff --git a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md index d22c1ca758..0bd64dc5bc 100644 --- a/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/WriteImageFilter.md @@ -6,43 +6,39 @@ NXImageProcessing (IO) ## Description -Writes a 2D image or a series of 2D slices from a 3D **Image Geometry**. This **Filter** does not depend on ITK. It uses stb_image_write for PNG/JPEG/BMP files, libtiff for TIFF files, and zlib for compressed MHA files. - -The following output file formats are supported: - -- PNG (via stb) -- JPEG / JPG (via stb) -- BMP (via stb) -- TIFF / TIF (via libtiff) -- MHA / MetaImage (compressed, attached pixel data) +Writes a 2D image or a series of 2D slices from a 3D **Image Geometry**. +The filter supports PNG, JPEG, BMP, TIFF, and MHA. MHA stores compressed pixels in the same file. ### Pixel Data Type Support By Format -When *Create Color Table* is **disabled**, the filter writes the **Data Array**'s own pixel values directly, and the chosen output file format constrains which data types can be written. This is validated during preflight, so an unsupported combination is reported as an error before the filter runs rather than failing partway through writing: - -| Output Format | Backend | Supported Data Types | -|----------------------------|---------|----------------------------| -| PNG / JPG / JPEG / BMP | stb | uint8 | -| TIFF / TIF | libtiff | uint8, uint16, float32 | -| MHA | zlib | int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64 | +With *Create Color Table* off, the file format must support the input array's type and number of components. The filter +checks both during preflight. -For example, writing a float32 array directly to a `.png` file fails preflight, since PNG supports only uint8 pixel data. The same array can be written to TIFF or MHA. If PNG is required, enable *Create Color Table* to write a uint8 RGB image. +| Output format | Supported pixel types | +| --- | --- | +| PNG, JPG, JPEG, BMP | uint8 | +| TIF, TIFF | uint8, uint16, float32 | +| MHA | int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64 | -MHA output supports the component counts accepted by the ITK writer: 1, 2, 3, 4, 10, 11, and 36. MHA files also store the origin and spacing for the selected output plane. Other formats use only the component counts supported by their backend. +For example, write raw float32 data to TIFF or MHA. To write single-component float32 data as PNG, enable *Create Color +Table* to produce a uint8 RGB image. -If an existing pipeline contains the ITK Write Image filter UUID, the NXImageProcessing plugin maps that UUID to this **Filter** when ITKImageProcessing is not available. When ITKImageProcessing is available, the original ITK **Filter** remains registered. +MHA supports 1, 2, 3, 4, 10, 11, or 36 components per pixel. It stores the origin and spacing for the selected plane. +Other formats have their own component limits. -If the input array represents a 3D volume, the filter will output a series of slices along one of the orthogonal axes. The options are to produce XY slices along the Z axis, XZ slices along the Y axis, or YZ slices along the X axis. The output files will be numbered sequentially starting at the *Index Offset* and ending at *Index Offset + (dim - 1)* for the chosen axis. For example, if the Z axis has 117 dimensions and *Index Offset* is 0, 117 XY image files will be produced and numbered 000 through 116. If the volume produces only a single slice, no index is appended and the file is written with exactly the user-specified name. +A saved pipeline can refer to the original ITK Write Image filter. The pipeline uses that filter when ITKImageProcessing +is available. Otherwise, NXImageProcessing uses this filter in its place. -The filter encodes each slice in a temporary file, then commits that slice through `AtomicFile`. This is a separate operation for each slice. An unrelated failure in a later slice can leave earlier completed slices in the output folder. File replacement can also fail if the operating system cannot rename the temporary file. +Each slice is written to a temporary file before it replaces the destination. An export that stops partway through can +leave earlier slices in the output folder. ### Plane -The *Plane* parameter controls which orthogonal plane is used when writing a 3D volume as a series of 2D image slices: +For a 3D volume, *Plane* selects the direction of the slice series. **XY** steps along Z, **XZ** along Y, and **YZ** +along X. -- **XY [0]**: Write image slices along the XY plane (normal to Z axis). -- **XZ [1]**: Write image slices along the XZ plane (normal to Y axis). -- **YZ [2]**: Write image slices along the YZ plane (normal to X axis). +The first slice uses *Index Offset*. For example, 117 XY slices with an offset of 0 are numbered 000 to 116. A single +slice uses the specified filename. ### Index Formatting @@ -62,31 +58,32 @@ This option only affects the written image files. The input Image Geometry and i ### Inline Color Table (Optional) -Enabling *Create Color Table* converts a single-component **Data Array** to an RGB image using the selected color preset. The filter checks the color range before it writes the first slice. The nonfinite policy below applies to this inline color conversion. +*Create Color Table* is off by default. When enabled, the filter uses *Select Preset...* to map any single-component +numeric input to uint8 RGB. An input with more than one component causes a preflight error. -- *Select Preset...* chooses the color preset (for example grayscale, rainbow, or jet) that is applied to the array's values. -- When *Create Color Table* is enabled, the **Input Image Data Array** must be a single-component numeric array, but it may be **any** numeric type: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, or float64. This is wider than the type restrictions that apply when writing an array's raw pixel values directly (see *Pixel Data Type Support By Format* above), since color-table mode always produces a uint8 RGB image regardless of the input array's type. If the selected array has more than one component, the filter will fail during preflight. -- Colorization uses bounded source groups and writes one RGB slice at a time. The initial scan reads bounded scalar pages. For floating-point input, it also reads the selected mask to check nonfinite values. Integer inputs use the mask when rendering each slice. Every finite scalar value participates in the minimum and maximum, including finite masked values. NaN and infinity do not participate in this range. -- If the finite range is constant, each unmasked voxel uses the preset's first control color. Masked voxels use the mask color. +The filter uses all finite values for the color range, including finite values excluded by a mask. For a constant finite +range, each unmasked pixel uses the first color in the preset. Different input values can map to the same RGB values. +Extreme-range colors can differ from older exports. #### Nonfinite Input Handling -This choice is active only when *Create Color Table* is enabled: +This choice applies to color-table output. Raw floating-point TIFF and MHA exports keep the input values. -- **Error** (default) reports an execution error for an unmasked NaN, positive infinity, or negative infinity. The check runs during the initial scan, before any slice file is written or replaced. This also protects existing files when the invalid value occurs in a later slice. -- **Warn and Continue** uses the preset's first control color for each unmasked nonfinite value. The filter returns one warning with the number of these values. It uses the finite values for the color range. The fallback can look identical to a valid finite minimum; it is not the preset's separate NaN color. +- **Error** (default) stops on an unmasked NaN or infinity before the filter writes or replaces any slice. +- **Warn and Continue** uses the preset's first color for unmasked NaN or infinity. The warning counts those pixels. + They can look the same as valid pixels at the minimum of the finite range. -The mask is applied before this policy. A masked nonfinite value uses *Masked Color (RGB)* and does not trigger the policy or its count. Finite masked values still participate in the range. Both choices report an error if no finite values remain, even if all nonfinite values are masked. A finite constant range remains valid. An entirely masked finite array also remains valid and uses the mask color. +Both choices require at least one finite input value. Masking nonfinite pixels cannot supply a range. An entirely masked +array with at least one finite value can still be written in the mask color. -Older pipelines now use **Error** unless they explicitly select **Warn and Continue**. To export data with unmasked NaN or infinity, repair the data, mask those pixels, or select the warning choice deliberately. Raw floating TIFF and MHA exports do not use this policy. +Saved pipelines without this choice use **Error** for color-table output. To export unmasked NaN or infinity, correct the +data, mask those pixels, or select **Warn and Continue**. -The writer keeps native integer endpoints and forms unsigned distances before floating conversion. This preserves adjacent int64 and uint64 ranges and avoids signed overflow. Floating normalization avoids overflowing differences across finite extrema. The normalized coordinate is float32, so interior colors still have finite precision. The existing linear interpolation and truncating conversion to uint8 channels remain in use. Different values can produce the same RGB bytes. Corrected extreme-range colors can differ from older exports. +#### Mask Array -*Use Mask Array* is a separate, top-level toggle (it is not nested under *Create Color Table*) that assigns a fixed color to "bad" voxels instead of coloring them from the preset: - -- *Mask Array* selects a boolean or uint8, single-component array that marks each **Voxel** as good (*true* / non-zero) or bad (*false* / zero). -- *Masked Color (RGB)* is the RGB triplet written for any voxel marked bad by the mask. -- The mask is only applied when *Create Color Table* is also enabled. If *Create Color Table* is disabled, *Use Mask Array* and *Masked Color (RGB)* have no effect, since the filter writes the raw pixel data unchanged. +Enable *Use Mask Array* with *Create Color Table* to give excluded pixels the *Masked Color (RGB)*. Select a single-component +boolean or uint8 mask with the same tuple count as the image data. A false or zero value excludes a pixel. Masked NaN +and infinity receive the mask color and are excluded from the warning count. ### Add Physical Scale Bar (Optional) @@ -112,7 +109,8 @@ the padded size. ### Required Input Sources -- **Image Data** -- a `uint8` (or `uint16`/`float32` for TIFF) **Cell Data** array on an **Image Geometry**, produced by an image reader such as [Read Image](ReadImageFilter.md) / [Read Images [3D Stack]](ReadImageStackFilter.md), or by any upstream image-processing filter. +Select a **Cell Data** array on an **Image Geometry**. You can use an image reader such as [Read Image](ReadImageFilter.md) +or [Read Images [3D Stack]](ReadImageStackFilter.md), or an array from an upstream image-processing filter. ## Algorithm @@ -134,8 +132,6 @@ The algorithm keeps the grouped source slab and one output slice in memory. A co The filter does not allocate a complete RGB volume. It still requires a complete selected 2D RGB slice. A scale bar can require a second padded slice and a separate band. The image codec can allocate more memory for that slice. The source-buffer reservation is not a limit on total process memory. -Cancellation can leave slices that were committed earlier. Execution errors and warnings are returned through the filter result. The shared filter wrapper can replace a valid execution result with cancellation and lose its execution warnings; this inherited limit does not change the initial nonfinite-error file-preservation guarantee. - % Auto generated parameter table will be inserted here ## Example Pipelines From b0c77e63b5a820709c36850fdc1884ae6762e3b1 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:31:58 -0500 Subject: [PATCH 47/98] REV: Preserve warnings from raw image exports Preserve read, creation, encode, and commit diagnostics through the first failure. Append each warning once in the raw writer. Addresses review item 8. Signed-off-by: Jessica Marquis --- .../Filters/Algorithms/WriteImageDirect.cpp | 40 ++++----- .../Filters/Algorithms/WriteImageScanline.cpp | 35 ++++---- .../NXImageProcessing/test/WriteImageTest.cpp | 87 ++++++++++++++++++- 3 files changed, 118 insertions(+), 44 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 275ae91992..0133afa201 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -130,9 +131,11 @@ struct ExtractSliceFunctor } const std::vector& sliceValues = sliceResult.value(); const usize expectedElements = static_cast(sliceExtent.totalElements()) * nComp; - if(sliceValues.size() != expectedElements) + const usize actualElements = sliceValues.size(); + if(actualElements != expectedElements) { - return MakeErrorResult(-27023, fmt::format("Could not bulk-read image slice {}. Expected {} values but received {}.", sliceIndex, expectedElements, sliceValues.size())); + return MergeResults(ConvertResult(std::move(sliceResult)), + MakeErrorResult(-27023, fmt::format("Could not bulk-read image slice {}. Expected {} values but received {}.", sliceIndex, expectedElements, actualElements))); } const usize rowElements = (planeIndex == 2 ? dimY : dimX) * nComp; @@ -157,7 +160,7 @@ struct ExtractSliceFunctor } } - return {}; + return ConvertResult(std::move(sliceResult)); } }; @@ -458,29 +461,16 @@ Result<> WriteImageDirect::operator()() m_MessageHandler.sendInfoMessage("Encoding image file"); } auto writeResult = imageIO->writePixelData(atomicFile.tempFilePath(), *writeBufferPtr, metadata); - if(m_InputValues.createColorTable) - { - Result<> fileStatus; - fileStatus.warnings() = std::move(atomicFileResult.warnings()); - writeResult = MergeResults(std::move(fileStatus), std::move(writeResult)); - } + writeResult = MergeResults(ConvertResult(std::move(atomicFileResult)), std::move(writeResult)); if(writeResult.invalid()) { return writeResult; } if(m_ShouldCancel) { - if(m_InputValues.createColorTable) - { - return MergeResults(std::move(writeResult), MakeErrorResult(-1, "Filter cancelled")); - } - return MakeErrorResult(-1, "Filter cancelled"); - } - auto commitResult = atomicFile.commit(); - if(m_InputValues.createColorTable) - { - commitResult = MergeResults(std::move(writeResult), std::move(commitResult)); + return MergeResults(std::move(writeResult), MakeErrorResult(-1, "Filter cancelled")); } + auto commitResult = MergeResults(std::move(writeResult), atomicFile.commit()); if(commitResult.valid()) { progressThrottle.updateCount(slice + 1); @@ -530,22 +520,26 @@ Result<> WriteImageDirect::operator()() // The non-color path preserves typed bytes for one slice at a time. usize sliceBufferSize = sliceW * sliceH * nComp * bytesPerComponent; std::vector sliceBuffer(sliceBufferSize); + Result<> status; for(usize slice = 0; slice < sliceCount; ++slice) { if(m_ShouldCancel) { - return {}; + return status; } auto extractResult = ExecuteDataFunction(ExtractSliceFunctor{}, dataType, imageArray, sliceBuffer, slice, m_InputValues.planeIndex, dimX, dimY, dimZ, nComp, progress.Rows); if(extractResult.invalid()) { - return extractResult; + return MergeResults(std::move(status), std::move(extractResult)); } + std::ranges::move(extractResult.warnings(), std::back_inserter(status.warnings())); + auto sliceResult = writeSlice(sliceBuffer, slice); if(sliceResult.invalid()) { - return sliceResult; + return MergeResults(std::move(status), std::move(sliceResult)); } + std::ranges::move(sliceResult.warnings(), std::back_inserter(status.warnings())); } - return {}; + return status; } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index e05d15cf17..da70c29af5 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -270,7 +271,7 @@ struct WriteDirectVolumeFunctor * @param shouldCancel Stops processing between output slices. * @param writeSlice Writes one packed output slice. * @param reportRows Optional synchronous callback for completed output rows. - * @return Error from the image writer. + * @return The accumulated warnings and the first transfer or writer error. */ template Result<> operator()(const IDataArray& dataArray, usize planeIndex, usize dimX, usize dimY, usize dimZ, usize sliceCount, usize sliceW, usize sliceH, usize numComponents, @@ -294,11 +295,12 @@ struct WriteDirectVolumeFunctor std::unique_ptr groupValues = std::make_unique(slicesPerGroup * sliceValueCount); std::vector sliceBuffer(sliceValueCount * (std::is_same_v ? 1 : sizeof(T))); + Result<> status; for(usize firstSlice = 0; firstSlice < sliceCount; firstSlice += slicesPerGroup) { if(shouldCancel) { - return {}; + return status; } const usize groupSliceCount = std::min(slicesPerGroup, sliceCount - firstSlice); @@ -306,25 +308,29 @@ struct WriteDirectVolumeFunctor const Extent groupExtent = CreateSliceGroupExtent(planeIndex, firstSlice, groupSliceCount, dimX, dimY, dimZ); // A failed read leaves the group buffer holding stale or uninitialized values. // Stop here so the writer never commits those values as image pixels. - if(Result<> result = dataStore.readExtentIntoBuffer(groupExtent, nonstd::span(groupValues.get(), groupValueCount)); result.invalid()) + auto readResult = dataStore.readExtentIntoBuffer(groupExtent, nonstd::span(groupValues.get(), groupValueCount)); + if(readResult.invalid()) { - return result; + return MergeResults(std::move(status), std::move(readResult)); } + std::ranges::move(readResult.warnings(), std::back_inserter(status.warnings())); for(usize localSliceIndex = 0; localSliceIndex < groupSliceCount; ++localSliceIndex) { if(shouldCancel) { - return {}; + return status; } CopySliceFromGroup(nonstd::span(groupValues.get(), groupValueCount), sliceBuffer, localSliceIndex, groupSliceCount, planeIndex, dimX, dimY, dimZ, numComponents, reportRows); - if(Result<> result = writeSlice(sliceBuffer, firstSlice + localSliceIndex); result.invalid()) + auto sliceResult = writeSlice(sliceBuffer, firstSlice + localSliceIndex); + if(sliceResult.invalid()) { - return result; + return MergeResults(std::move(status), std::move(sliceResult)); } + std::ranges::move(sliceResult.warnings(), std::back_inserter(status.warnings())); } } - return {}; + return status; } }; @@ -689,21 +695,12 @@ Result<> WriteImageScanline::operator()() m_MessageHandler.sendInfoMessage("Encoding image file"); } auto writeResult = imageIO->writePixelData(atomicFile.tempFilePath(), *writeBufferPtr, metadata); - if(m_InputValues.createColorTable) - { - Result<> fileStatus; - fileStatus.warnings() = std::move(atomicFileResult.warnings()); - writeResult = MergeResults(std::move(fileStatus), std::move(writeResult)); - } + writeResult = MergeResults(ConvertResult(std::move(atomicFileResult)), std::move(writeResult)); if(writeResult.invalid()) { return writeResult; } - auto commitResult = atomicFile.commit(); - if(m_InputValues.createColorTable) - { - commitResult = MergeResults(std::move(writeResult), std::move(commitResult)); - } + auto commitResult = MergeResults(std::move(writeResult), atomicFile.commit()); if(commitResult.valid()) { progressThrottle.updateCount(slice + 1); diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 4a00ebadde..5a231e3905 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -540,7 +540,9 @@ class WriteImageReadProbeStore : public ExtentCountingDataStore { return MakeErrorResult>(-27902, "Controlled Write Image slice read failure."); } - return ExtentCountingDataStore::readExtent(extent); + auto result = ExtentCountingDataStore::readExtent(extent); + addExtentWarning(extent, result); + return result; } Result<> readExtentIntoBuffer(const Extent& extent, nonstd::span values) const override { @@ -548,7 +550,13 @@ class WriteImageReadProbeStore : public ExtentCountingDataStore { return MakeErrorResult(-27902, "Controlled Write Image slice read failure."); } - return ExtentCountingDataStore::readExtentIntoBuffer(extent, values); + auto result = ExtentCountingDataStore::readExtentIntoBuffer(extent, values); + addExtentWarning(extent, result); + return result; + } + void enableExtentWarnings() noexcept + { + m_WarnOnExtentRead = true; } usize flatReadCount() const { @@ -560,8 +568,19 @@ class WriteImageReadProbeStore : public ExtentCountingDataStore } private: + // The returning read can call the buffer read. Add only one warning for that extent. + template + void addExtentWarning(const Extent& extent, Result& result) const + { + if(m_WarnOnExtentRead && result.valid() && result.warnings().empty()) + { + result.warnings().push_back({-27904, "Controlled Write Image read warning for XY slice " + std::to_string(extent.min.at(0)) + "."}); + } + } + bool m_FailFlat = false; bool m_FailExtent = false; + bool m_WarnOnExtentRead = false; mutable usize m_FlatReadCount = 0; mutable usize m_MaximumReadBytes = 0; }; @@ -1043,6 +1062,70 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: late nonfinite scan uses bounded } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. +TEST_CASE("NXImageProcessing::WriteImageFilter: raw export retains warnings from each slice", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + const bool failSecondCommit = GENERATE(false, true); + CAPTURE(scenario, failSecondCommit); + UnitTest::AlgorithmTestScope scope(scenario); + const ShapeType shape{2, 1, 2}; + const std::array values{7, 11, 13, 17}; + DataStructure dataStructure; + const DataPath inputPath = createWriteImageNumericInput(dataStructure, values, shape); + auto store = std::make_shared>(shape, false, false); + store->enableExtentWarnings(); + for(usize index = 0; index < values.size(); ++index) + { + store->setValue(index, values.at(index)); + } + auto* input = dataStructure.getDataAs(inputPath); + REQUIRE(input != nullptr); + const auto storeResult = input->setDataStore(store); + SIMPLNX_RESULT_REQUIRE_VALID(storeResult); + + const ScopedTempDir output(k_BinaryTestOutputDir); + const fs::path firstSlice = output.path() / "raw_000.png"; + const fs::path secondSlice = output.path() / "raw_001.png"; + if(failSecondCommit) + { + REQUIRE(fs::create_directory(secondSlice)); + std::ofstream sentinel(secondSlice / "keep.txt", std::ios::binary); + REQUIRE(sentinel.is_open()); + sentinel << "keep"; + } + auto arguments = writeImageNumericArguments(output.path() / "raw.png"); + arguments.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(false)); + arguments.insertOrAssign(WriteImageFilter::k_Plane_Key, std::make_any(k_XYPlane)); + arguments.insertOrAssign(WriteImageFilter::k_TotalIndexDigits_Key, std::make_any(3)); + + const WriteImageFilter filter; + const auto result = scope.executeFilter(filter, dataStructure, arguments); + if(failSecondCommit) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().size() == 1); + REQUIRE(result.result.errors().front().code == -15782); + const auto sentinel = readWriteImageSentinel(secondSlice / "keep.txt"); + REQUIRE(std::string(sentinel.begin(), sentinel.end()) == "keep"); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(c15_test::Decode(secondSlice, 2, 1, 1) == std::vector{13, 17}); + } + const auto& warnings = result.result.warnings(); + REQUIRE(warnings.size() == 2); + REQUIRE(warnings.at(0).code == -27904); + REQUIRE(warnings.at(0).message == "Controlled Write Image read warning for XY slice 0."); + REQUIRE(warnings.at(1).code == -27904); + REQUIRE(warnings.at(1).message == "Controlled Write Image read warning for XY slice 1."); + REQUIRE(c15_test::Decode(firstSlice, 2, 1, 1) == std::vector{7, 11}); + REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) + // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: later commit failure keeps earlier slice and policy warning", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { From 1197742af12a5b2df78edc604921b4e841f7213a Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:31:59 -0500 Subject: [PATCH 48/98] REV: Pass typed integer Heights to reconstruction Prepare one checked integer offset for both marker paths. Keep the floating-point entry point and validate Height before conversion. Addresses review item 7. Signed-off-by: Jessica Marquis --- .../Filters/HConvexImageFilter.cpp | 9 +-- .../Filters/HMinimaImageFilter.cpp | 9 +-- .../utils/ImageFilterExecutorDetail.hpp | 28 ++----- .../utils/ImageFilterExecutors.hpp | 30 +++++-- .../ReconstructionImageFilterExecutors.cpp | 41 +++++++--- .../test/HConvexImageFilterTest.cpp | 72 ++++++++++++----- .../test/HMinimaImageFilterTest.cpp | 29 +++++-- .../MorphologicalReconstructionEngine.hpp | 81 +++++++++++++------ 8 files changed, 201 insertions(+), 98 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp index 3b3d359ebb..874c6d1608 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp @@ -99,8 +99,7 @@ IFilter::PreflightResult HConvexImageFilter::preflightImpl(const DataStructure& auto height = filterArgs.value(k_Height_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - // The marker offsets every voxel by -Height through a saturating cast, which is undefined for a non-finite - // value; reject it here so the offset is always well-defined. + // The marker offset must be finite for reconstruction. if(!std::isfinite(height)) { return {MakeErrorResult(k_NonFiniteHeight, fmt::format("H Convex requires a finite Height, but the supplied value was {}.", height))}; @@ -140,9 +139,9 @@ Result<> HConvexImageFilter::executeImpl(DataStructure& dataStructure, const Arg // HConvex == input - HMaxima(input, Height): reconstruct-by-dilation of the saturating (input - Height) marker, // then subtract that h-maxima result from the input (the h-dome). FullyConnected selects the reconstruction // connectivity. - return ImageProcessing::ExecuteMorphologicalReconstructionImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Dilation, fullyConnected, - ImageProcessing::detail::ReconMarker::MinusHeight, height, shouldCancel, messageHandler, /*differenceFromInput=*/true, - /*exactIntegerHeight=*/true); + return ImageProcessing::detail::ExecuteMorphologicalReconstructionWithValidatedHeight(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Dilation, + fullyConnected, ImageProcessing::detail::ReconMarker::MinusHeight, height, shouldCancel, messageHandler, + /*differenceFromInput=*/true); } namespace diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp index c906ef5557..dd48acfc0a 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp @@ -100,8 +100,7 @@ IFilter::PreflightResult HMinimaImageFilter::preflightImpl(const DataStructure& auto height = filterArgs.value(k_Height_Key); const DataPath outputArrayPath = selectedInputArray.replaceName(outputArrayName); - // The marker offsets every voxel by +Height through a saturating cast, which is undefined for a non-finite - // value; reject it here so the offset is always well-defined. + // The marker offset must be finite for reconstruction. if(!std::isfinite(height)) { return {MakeErrorResult(k_NonFiniteHeight, fmt::format("H Minima requires a finite Height, but the supplied value was {}.", height))}; @@ -140,9 +139,9 @@ Result<> HMinimaImageFilter::executeImpl(DataStructure& dataStructure, const Arg // HMinima == reconstruct-by-erosion of the saturating (input + Height) marker under the input mask; unlike // HMaxima the legacy ITK filter exposes a FullyConnected option, so honor it here. - return ImageProcessing::ExecuteMorphologicalReconstructionImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Erosion, fullyConnected, - ImageProcessing::detail::ReconMarker::PlusHeight, height, shouldCancel, messageHandler, /*differenceFromInput=*/false, - /*exactIntegerHeight=*/true); + return ImageProcessing::detail::ExecuteMorphologicalReconstructionWithValidatedHeight(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::ReconstructOp::Erosion, + fullyConnected, ImageProcessing::detail::ReconMarker::PlusHeight, height, shouldCancel, messageHandler, + /*differenceFromInput=*/false); } namespace diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index 8323a49d8b..01772ddf55 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -610,8 +610,8 @@ Result<> CropSubtractFromStore(const AbstractDataStore& padded, const Abstrac * stays bounded (a fixed chunk for the height markers, one z-plane for Fillhole), so an out-of-core input * yields an out-of-core-bounded marker. @p dims is (X,Y,Z); @p height feeds MinusHeight/PlusHeight only. * - * MinusHeight/PlusHeight use @ref SaturateCastFromDouble by default. The exactIntegerHeight opt-in preserves native - * integer units with saturating addition or subtraction. Floating values retain the default arithmetic. + * For integer pixels an exact offset preserves native units, including values beyond float64 precision. + * A floating offset uses floating-point arithmetic. Both paths saturate integer marker values to native bounds. * * The Fillhole/GrindPeak marker border is the boundary in ITK's EFFECTIVE dimensionality: the legacy filter * processes a (W,H,1) volume as a 2D ITK image (ITKArrayHelper selects Dimension 2 when Z==1), so X and Y are always @@ -626,19 +626,18 @@ Result<> CropSubtractFromStore(const AbstractDataStore& padded, const Abstrac * @param markerStore Receives marker values; has the input's tuple and element layout. * @param dims Image dimensions in X, Y, Z order. * @param kind Selects a height or border marker. - * @param height Finite offset in intensity units; nonnegative and whole for the integer opt-in. + * @param height Prepared offset in intensity units. * @param shouldCancel Requests cancellation between transfers. - * @param exactIntegerHeight Opt-in to exact, saturating integer offsets for a validated whole Height. * @return The first transfer or dimension error, or a valid result. * @pre @p markerStore has the same tuple/element layout as @p inStore (X fastest). */ template -Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, AbstractDataStore& markerStore, const SizeVec3& dims, ReconMarker kind, float64 height, const std::atomic_bool& shouldCancel, - bool exactIntegerHeight = false) +Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, AbstractDataStore& markerStore, const SizeVec3& dims, ReconMarker kind, const ReconstructionHeight& height, + const std::atomic_bool& shouldCancel) { if(kind == ReconMarker::MinusHeight || kind == ReconMarker::PlusHeight) { - const float64 signedHeight = (kind == ReconMarker::MinusHeight) ? -height : height; + const bool lower = kind == ReconMarker::MinusHeight; const usize total = inStore.getSize(); const usize chunkValues = inStore.getStoreType() == IDataStore::StoreType::OutOfCore || markerStore.getStoreType() == IDataStore::StoreType::OutOfCore ? std::max(1, (64ULL * 1024ULL * 1024ULL) / sizeof(T)) : @@ -655,20 +654,7 @@ Result<> BuildReconstructionMarker(const AbstractDataStore& inStore, Abstract { return r; } - if(exactIntegerHeight && std::is_integral_v) - { - if constexpr(std::is_integral_v) - { - ApplyExactIntegerReconstructionHeight(nonstd::span(buffer.get(), count), height, kind == ReconMarker::MinusHeight); - } - } - else - { - for(usize i = 0; i < count; ++i) - { - buffer[i] = SaturateCastFromDouble(static_cast(buffer[i]) + signedHeight); - } - } + ApplyReconstructionHeight(nonstd::span(buffer.get(), count), height, lower); if(Result<> r = markerStore.copyFromBuffer(start, nonstd::span(buffer.get(), count)); r.invalid()) { return r; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp index 53ff0feeda..fd041607bd 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutors.hpp @@ -206,8 +206,7 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteBinaryMorphologyCompositeImageFilter(Da * reconstruction result is subtracted from the input in place after reconstruction (output = input - * reconstruction). Output type == input type (SameAsInput). The caller's preflight must have enforced a * scalar input (@ref PreflightImageFilter requireScalar=true) and, for the height markers, a finite - * @p height. The opt-in @p exactIntegerHeight also requires a nonnegative whole Height for integer images; - * it preserves native integer units and saturates markers at the type bounds. Other callers default to their existing arithmetic. + * @p height. The marker offset uses floating-point arithmetic. * @param dataStructure Owns the selected arrays and geometry. * @param imageGeomPath Image geometry used for dimensions. * @param inputArrayPath Scalar input image and reconstruction mask. @@ -219,13 +218,34 @@ NXIMAGEPROCESSING_EXPORT Result<> ExecuteBinaryMorphologyCompositeImageFilter(Da * @param shouldCancel Requests cancellation during reconstruction and between transfers. * @param messageHandler Receives progress messages. * @param differenceFromInput Selects HConvex contrast with checked signed subtraction. - * @param exactIntegerHeight Selects exact native integer markers for a validated whole, nonnegative Height. * @return A reconstruction, contrast or transfer error, or a valid result. */ NXIMAGEPROCESSING_EXPORT Result<> ExecuteMorphologicalReconstructionImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, ReconstructOp op, bool fullyConnected, detail::ReconMarker markerKind, float64 height, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, bool differenceFromInput = false, - bool exactIntegerHeight = false); + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, bool differenceFromInput = false); + +namespace detail +{ +/** + * @brief Reconstructs with a validated height. Integer images use an exact native offset. + * @param dataStructure Owns the selected arrays and geometry. + * @param imageGeomPath Image geometry used for dimensions. + * @param inputArrayPath Scalar input image and reconstruction mask. + * @param outputArrayPath Output image of the same type and shape as the input. + * @param op Reconstruction direction. + * @param fullyConnected Selects full connectivity when true. + * @param markerKind Selects the height marker. + * @param height Validated offset in intensity units. + * @param shouldCancel Requests cancellation during reconstruction and between transfers. + * @param messageHandler Receives progress messages. + * @param differenceFromInput Selects checked contrast subtraction when true. + * @return A reconstruction, contrast or transfer error, or a valid result. + * @pre Height is finite and nonnegative. Integer input requires a whole Height. + */ +Result<> ExecuteMorphologicalReconstructionWithValidatedHeight(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, + ReconstructOp op, bool fullyConnected, ReconMarker markerKind, float64 height, const std::atomic_bool& shouldCancel, + const IFilter::MessageHandler& messageHandler, bool differenceFromInput); +} // namespace detail /** * @brief Execute opening-by-reconstruction (@p firstOp == Erode) or closing-by-reconstruction (@p firstOp == diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp index 5c9d25b4e6..a59b455f64 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ReconstructionImageFilterExecutors.cpp @@ -38,11 +38,10 @@ struct MorphologicalReconstructionExecuteFn ReconstructOp op; bool fullyConnected; ReconMarker markerKind; - float64 height; + ReconstructionHeight height; const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; bool differenceFromInput; // HConvex: after reconstruction, output = input - reconstruction (the h-dome) - bool ExactIntegerHeight = false; template Result<> operator()() const @@ -55,7 +54,6 @@ struct MorphologicalReconstructionExecuteFn std::shared_ptr> markerStore; const AbstractDataStore* marker = &inStore; ReconstructionMarkerOptions markerOptions; - markerOptions.ExactIntegerHeight = ExactIntegerHeight; const bool isHeightMarker = markerKind == ReconMarker::MinusHeight || markerKind == ReconMarker::PlusHeight; const bool isBorderMarker = markerKind == ReconMarker::FillholeMaxBorder || markerKind == ReconMarker::GrindPeakMinBorder; const bool useDerivedMarker = isBorderMarker || (inStore.getStoreType() == IDataStore::StoreType::OutOfCore && isHeightMarker); @@ -76,7 +74,7 @@ struct MorphologicalReconstructionExecuteFn // Marker scratch inherits the input's storage format (out-of-core input -> out-of-core scratch), keeping the // whole filter memory-bounded through the streamed sweep path. markerStore = DataStoreUtilities::CreateDataStoreWithFormat(inArray.getDataFormat(), inArray.getTupleShape(), std::vector{1}, IDataAction::Mode::Execute); - if(auto markerResult = BuildReconstructionMarker(inStore, *markerStore, dims, markerKind, height, shouldCancel, ExactIntegerHeight); markerResult.invalid()) + if(auto markerResult = BuildReconstructionMarker(inStore, *markerStore, dims, markerKind, height, shouldCancel); markerResult.invalid()) { return markerResult; } @@ -100,6 +98,16 @@ struct MorphologicalReconstructionExecuteFn } }; +Result<> ExecutePreparedMorphologicalReconstruction(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, DataType inputType, + ReconstructOp op, bool fullyConnected, ReconMarker markerKind, ReconstructionHeight height, const std::atomic_bool& shouldCancel, + const IFilter::MessageHandler& messageHandler, bool differenceFromInput) +{ + const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); + const MorphologicalReconstructionExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, imageGeom.getDimensions(), op, fullyConnected, markerKind, + height, shouldCancel, messageHandler, differenceFromInput}; + return AllNumeric::dispatch(inputType, executeFn); +} + /** * @brief Streamed compare-select for the PreserveIntensities pass of opening/closing-by-reconstruction: for each * voxel, out[i] = (morph[i] == recon[i]) ? input[i] : unmarkedFill. Where the first reconstruction left the @@ -274,16 +282,23 @@ struct BinaryReconstructionOpeningExecuteFn // ----------------------------------------------------------------------------- Result<> ExecuteMorphologicalReconstructionImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, ReconstructOp op, bool fullyConnected, detail::ReconMarker markerKind, float64 height, const std::atomic_bool& shouldCancel, - const IFilter::MessageHandler& messageHandler, bool differenceFromInput, bool exactIntegerHeight) + const IFilter::MessageHandler& messageHandler, bool differenceFromInput) { - using TypeSetPolicy = AllNumeric; - const auto& inputArray = dataStructure.getDataRefAs(inputArrayPath); - const DataType inputType = inputArray.getDataType(); - const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); - const SizeVec3 dims = imageGeom.getDimensions(); - detail::MorphologicalReconstructionExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, op, fullyConnected, markerKind, height, shouldCancel, - messageHandler, differenceFromInput, exactIntegerHeight}; - return TypeSetPolicy::dispatch(inputType, executeFn); + const auto inputType = dataStructure.getDataRefAs(inputArrayPath).getDataType(); + return detail::ExecutePreparedMorphologicalReconstruction(dataStructure, imageGeomPath, inputArrayPath, outputArrayPath, inputType, op, fullyConnected, markerKind, height, shouldCancel, + messageHandler, differenceFromInput); +} + +// ----------------------------------------------------------------------------- +Result<> detail::ExecuteMorphologicalReconstructionWithValidatedHeight(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, + ReconstructOp op, bool fullyConnected, ReconMarker markerKind, float64 height, const std::atomic_bool& shouldCancel, + const IFilter::MessageHandler& messageHandler, bool differenceFromInput) +{ + const auto inputType = dataStructure.getDataRefAs(inputArrayPath).getDataType(); + const ReconstructionHeight offset = + GetIntegerScalarTypes().contains(inputType) ? ReconstructionHeight{ExactReconstructionHeight{SaturateCastFromDouble(height)}} : ReconstructionHeight{height}; + return ExecutePreparedMorphologicalReconstruction(dataStructure, imageGeomPath, inputArrayPath, outputArrayPath, inputType, op, fullyConnected, markerKind, offset, shouldCancel, messageHandler, + differenceFromInput); } // ----------------------------------------------------------------------------- diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 9199c7ffff..3607ea96d1 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -39,6 +39,13 @@ using namespace nx::core; namespace rt = recon_test; +static_assert(std::is_constructible_v); +static_assert(!std::is_constructible_v); + +using ReconstructionExecutor = Result<> (*)(DataStructure&, const DataPath&, const DataPath&, const DataPath&, ImageProcessing::ReconstructOp, bool, ImageProcessing::detail::ReconMarker, float64, + const std::atomic_bool&, const IFilter::MessageHandler&, bool); +static_assert(std::is_same_v); + namespace { // Filter-local error codes for the Height guards (kept in sync with HConvexImageFilter.cpp). @@ -118,13 +125,17 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: integer fractional He const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 2, 0}); const HConvexImageFilter filter; - // Integer images require a whole Height. Stop at preflight: the baseline must never execute this request. - const auto preflightResult = filter.preflight(dataStructure, makeHConvexArguments(inputPath, height)); + const Arguments args = makeHConvexArguments(inputPath, height); + const auto preflightResult = filter.preflight(dataStructure, args); REQUIRE(preflightResult.outputActions.invalid()); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().size() == 1); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().front().code == -1874101); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == preflightResult.outputActions.errors().front().code); + REQUIRE(dataStructure.getData(inputPath.replaceName("Output")) == nullptr); } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. @@ -209,17 +220,24 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: floating fractional H // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit unit contrast above double precision", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) +TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit contrast for unit and large Heights above double precision", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int64, + uint64) { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); - constexpr TestType inputValue = TestType{1} << 54; + constexpr float64 belowUint64Limit = 0x1.fffffffffffffp63; + const float64 height = GENERATE(1.0, 0x1.fffffffffffffp63, std::ldexp(1.0, 64), std::numeric_limits::max()); + CAPTURE(height); + constexpr TestType maximum = std::numeric_limits::max(); + // A signed input of -1 gives a representable contrast even if the marker reaches the native minimum. + constexpr TestType largeHeightInput = std::is_signed_v ? static_cast(-1) : maximum; + const TestType inputValue = height == 1.0 ? (TestType{1} << 54) : largeHeightInput; DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {inputValue}); const HConvexImageFilter filter; - const Arguments args = makeHConvexArguments(inputPath, 1.0); + const Arguments args = makeHConvexArguments(inputPath, height); const auto preflightResult = filter.preflight(dataStructure, args); SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); const auto executeResult = algorithmTestScope.executeFilter(filter, dataStructure, args); @@ -233,9 +251,9 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit unit co REQUIRE_NOTHROW(dataStructure.getDataRefAs>(outputPath)); const auto& outputStore = dataStructure.getDataRefAs>(outputPath).getDataStoreRef(); REQUIRE(outputStore.getSize() == 1); - // A one-pixel marker has no neighbor that can raise it: reconstruction is inputValue - 1, so contrast is 1. - // The baseline double conversion rounds the marker back to inputValue. Both values are safely inside T's range. - REQUIRE(outputStore.getValue(0) == TestType{1}); + // With one pixel the reconstruction equals the marker. Below 2^64 the unsigned marker stays 2047 above zero. + const TestType expected = height == 1.0 ? TestType{1} : (!std::is_signed_v && height == belowUint64Limit ? maximum - TestType{2047} : maximum); + REQUIRE(outputStore.getValue(0) == expected); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -251,8 +269,7 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact signed 64-bit marker bou const std::array original = {minimum, minimum + 1, -1, 0, maximum}; ImageProcessing::ReconstructionMarkerOptions options; options.source = lower ? ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight : ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; - options.height = std::ldexp(1.0, 63); - options.ExactIntegerHeight = true; + options.height = ImageProcessing::ExactReconstructionHeight{uint64{1} << 63}; ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(values.data(), values.size()), 0, {5, 1, 1}, options); // MIN + 2^63 = 0; MAX - 2^63 = -1. Other values saturate at the corresponding native bound. const std::array expected = lower ? std::array{minimum, minimum, minimum, minimum, -1} : std::array{0, 1, maximum, maximum, maximum}; @@ -263,7 +280,7 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact signed 64-bit marker bou SIMPLNX_RESULT_REQUIRE_VALID(inputResult); const std::atomic_bool shouldCancel = false; const auto markerResult = ImageProcessing::detail::BuildReconstructionMarker( - input, marker, {5, 1, 1}, lower ? ImageProcessing::detail::ReconMarker::MinusHeight : ImageProcessing::detail::ReconMarker::PlusHeight, options.height, shouldCancel, true); + input, marker, {5, 1, 1}, lower ? ImageProcessing::detail::ReconMarker::MinusHeight : ImageProcessing::detail::ReconMarker::PlusHeight, options.height, shouldCancel); SIMPLNX_RESULT_REQUIRE_VALID(markerResult); for(usize index = 0; index < expected.size(); ++index) { @@ -280,20 +297,19 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: exact integer marker constexpr TestType minimum = std::numeric_limits::lowest(); constexpr TestType maximum = std::numeric_limits::max(); const bool lower = GENERATE(false, true); - const float64 height = GENERATE(0.0, 1.0, std::ldexp(1.0, 64), std::numeric_limits::max()); + const uint64 height = GENERATE(uint64{0}, uint64{1}, std::numeric_limits::max()); CAPTURE(lower, height); std::array values = {minimum, 0, maximum}; ImageProcessing::ReconstructionMarkerOptions options; options.source = lower ? ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight : ImageProcessing::ReconstructionMarkerSource::MaskPlusHeight; - options.height = height; - options.ExactIntegerHeight = true; + options.height = ImageProcessing::ExactReconstructionHeight{height}; ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(values.data(), values.size()), 0, {3, 1, 1}, options); - if(height == 0.0) + if(height == 0) { const std::array expected = {minimum, 0, maximum}; REQUIRE(values == expected); } - else if(height == 1.0) + else if(height == 1) { const TestType zeroLowered = std::is_signed_v ? static_cast(-1) : TestType{0}; const std::array expected = @@ -336,8 +352,7 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker Height immediatel UnitTest::LoadPlugins(); ImageProcessing::ReconstructionMarkerOptions options; options.source = ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight; - options.height = 0x1.fffffffffffffp63; - options.ExactIntegerHeight = true; + options.height = ImageProcessing::ExactReconstructionHeight{std::numeric_limits::max() - uint64{2047}}; std::array unsignedValues = {std::numeric_limits::max()}; ImageProcessing::detail::ApplyReconstructionMarkerOptions(nonstd::span(unsignedValues.data(), unsignedValues.size()), 0, {1, 1, 1}, options); REQUIRE(unsignedValues.at(0) == uint64{2047}); @@ -722,23 +737,38 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: Preflight non-finite Height gu SECTION("NaN Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, std::numeric_limits::quiet_NaN())); + const Arguments args = makeArgs(inputPath, std::numeric_limits::quiet_NaN()); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NonFiniteHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NonFiniteHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } SECTION("Inf Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, std::numeric_limits::infinity())); + const Arguments args = makeArgs(inputPath, std::numeric_limits::infinity()); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NonFiniteHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NonFiniteHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } SECTION("Negative Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, -1.0)); + const Arguments args = makeArgs(inputPath, -1.0); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NegativeHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NegativeHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } } diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index 191a8e00c1..a751ecd7fc 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -69,13 +69,17 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: integer fractional He const auto wholeHeightPreflightResult = filter.preflight(dataStructure, makeHMinimaArguments(inputPath, 2.0)); SIMPLNX_RESULT_REQUIRE_VALID(wholeHeightPreflightResult.outputActions); - // Integer images require a whole Height. Stop at preflight: the baseline must never execute this request. - const auto preflightResult = filter.preflight(dataStructure, makeHMinimaArguments(inputPath, height)); + const Arguments args = makeHMinimaArguments(inputPath, height); + const auto preflightResult = filter.preflight(dataStructure, args); REQUIRE(preflightResult.outputActions.invalid()); REQUIRE(preflightResult.outputActions.errors().size() == 1); REQUIRE(preflightResult.outputActions.errors().front().code == -1874111); REQUIRE(preflightResult.outputActions.errors().front().message.find(inputPath.toString()) != std::string::npos); REQUIRE(preflightResult.outputActions.errors().front().message.find(height == 0.5 ? "0.5" : "2.5") != std::string::npos); + const auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == preflightResult.outputActions.errors().front().code); + REQUIRE(dataStructure.getData(inputPath.replaceName("Output")) == nullptr); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -539,23 +543,38 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: Preflight non-finite Height gu SECTION("NaN Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, std::numeric_limits::quiet_NaN())); + const Arguments args = makeArgs(inputPath, std::numeric_limits::quiet_NaN()); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NonFiniteHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NonFiniteHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } SECTION("Inf Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, std::numeric_limits::infinity())); + const Arguments args = makeArgs(inputPath, std::numeric_limits::infinity()); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NonFiniteHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NonFiniteHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } SECTION("Negative Height is rejected") { - auto result = filter.preflight(ds, makeArgs(inputPath, -1.0)); + const Arguments args = makeArgs(inputPath, -1.0); + const auto result = filter.preflight(ds, args); REQUIRE(result.outputActions.invalid()); REQUIRE(result.outputActions.errors().front().code == k_NegativeHeight); + const auto executeResult = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + REQUIRE(executeResult.result.errors().front().code == k_NegativeHeight); + REQUIRE(ds.getData(inputPath.replaceName("Output")) == nullptr); } } diff --git a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp index d84c77fc85..0e4e19cb8f 100644 --- a/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/MorphologicalReconstructionEngine.hpp @@ -19,10 +19,12 @@ #include #include #include +#include #include #include #include #include +#include #include namespace nx::core::ImageProcessing @@ -36,12 +38,31 @@ enum class ReconstructionMarkerSource MaskWithMaximumInterior }; +/** @brief A nonnegative marker offset in native integer intensity units. */ +class ExactReconstructionHeight +{ +public: + template T> + explicit constexpr ExactReconstructionHeight(T units) noexcept + : m_Units(units) + { + } + + constexpr uint64 units() const noexcept + { + return m_Units; + } + +private: + uint64 m_Units; +}; + +using ReconstructionHeight = std::variant; + struct ReconstructionMarkerOptions { ReconstructionMarkerSource source = ReconstructionMarkerSource::Provided; - float64 height = 0.0; - // Callers opt in only after validating a finite, nonnegative whole Height for integer images. - bool ExactIntegerHeight = false; + ReconstructionHeight height = 0.0; }; namespace detail @@ -120,22 +141,21 @@ T AddExactIntegerReconstructionHeight(T value, uint64 heightUnits) /** * @brief Offsets integer marker values exactly, saturating at the native type bounds. * @tparam T Integer scalar type. - * @param values Owned marker buffer to update. - * @param height Finite, nonnegative, whole Height, validated by the opting-in caller. + * @param values Marker buffer to update. + * @param height Prepared integer offset in intensity units. * @param lower Selects subtraction when true and addition when false. - * - * The Height conversion is once per buffer. Values never pass through floating point, and signed minima are never negated. */ template -void ApplyExactIntegerReconstructionHeight(nonstd::span values, float64 height, bool lower) +void ApplyExactIntegerReconstructionHeight(nonstd::span values, ExactReconstructionHeight height, bool lower) { static_assert(std::is_integral_v); - if(height >= std::ldexp(1.0, 64)) + const uint64 heightUnits = height.units(); + // The largest uint64 offset spans the full range of each supported integer pixel type. + if(heightUnits == std::numeric_limits::max()) { std::fill(values.begin(), values.end(), lower ? std::numeric_limits::lowest() : std::numeric_limits::max()); return; } - const uint64 heightUnits = static_cast(height); for(T& value : values) { value = lower ? SubtractExactIntegerReconstructionHeight(value, heightUnits) : AddExactIntegerReconstructionHeight(value, heightUnits); @@ -170,6 +190,33 @@ T SaturateReconstructionMarkerValue(float64 value) } } +/** + * @brief Applies an offset to the marker buffer and saturates integer results at the native type bounds. + * @tparam T Marker pixel type. + * @param values Marker buffer to update. + * @param height Prepared offset in intensity units. A floating offset uses floating-point arithmetic. + * @param lower Selects subtraction when true and addition when false. + */ +template +void ApplyReconstructionHeight(nonstd::span values, const ReconstructionHeight& height, bool lower) +{ + const auto* exactHeight = std::get_if(&height); + if constexpr(std::is_integral_v) + { + if(exactHeight != nullptr) + { + ApplyExactIntegerReconstructionHeight(values, *exactHeight, lower); + return; + } + } + const float64 offset = exactHeight != nullptr ? static_cast(exactHeight->units()) : std::get(height); + const float64 signedHeight = lower ? -offset : offset; + for(T& value : values) + { + value = SaturateReconstructionMarkerValue(static_cast(value) + signedHeight); + } +} + template void ApplyReconstructionMarkerOptions(nonstd::span values, usize start, const SizeVec3& dims, const ReconstructionMarkerOptions& options) { @@ -179,19 +226,7 @@ void ApplyReconstructionMarkerOptions(nonstd::span values, usize start, const } if(options.source == ReconstructionMarkerSource::MaskMinusHeight || options.source == ReconstructionMarkerSource::MaskPlusHeight) { - if constexpr(std::is_integral_v) - { - if(options.ExactIntegerHeight) - { - ApplyExactIntegerReconstructionHeight(values, options.height, options.source == ReconstructionMarkerSource::MaskMinusHeight); - return; - } - } - const float64 signedHeight = options.source == ReconstructionMarkerSource::MaskMinusHeight ? -options.height : options.height; - for(T& value : values) - { - value = SaturateReconstructionMarkerValue(static_cast(value) + signedHeight); - } + ApplyReconstructionHeight(values, options.height, options.source == ReconstructionMarkerSource::MaskMinusHeight); return; } From 425abc5a19574933e0858ece3478dff657caac01 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:31:59 -0500 Subject: [PATCH 49/98] REV: Share image conversion state and caller diagnostics Share the terminal conversion state. Keep the diagnostic ranges and recovery messages with the calling filters. Addresses review item 12. Signed-off-by: Jessica Marquis --- .../Filters/CurvatureFlowImageFilter.cpp | 3 +- .../utils/GaussianImageFilterExecutors.cpp | 41 ++------ .../utils/ImageFilterTemplateExecutors.hpp | 12 ++- .../utils/SmoothingRecursiveGaussian2D.hpp | 41 ++++++++ .../utils/TerminalConversionDiagnostics.hpp | 81 ++++++++++++++++ .../test/CurvatureFlowImageFilterTest.cpp | 34 +++---- .../test/RecursiveGaussianEngineTest.cpp | 6 +- ...othingRecursiveGaussianImageFilterTest.cpp | 10 +- .../FiniteDifferenceEngine.hpp | 96 ++++--------------- .../RecursiveGaussianEngine.hpp | 96 ++++--------------- .../TerminalImageConversion.hpp | 57 +++++++++++ 11 files changed, 264 insertions(+), 213 deletions(-) create mode 100644 src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/SmoothingRecursiveGaussian2D.hpp create mode 100644 src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/TerminalConversionDiagnostics.hpp diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp index 198afb367d..f3d61a8b30 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/CurvatureFlowImageFilter.cpp @@ -1,6 +1,7 @@ #include "CurvatureFlowImageFilter.hpp" #include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" +#include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" @@ -106,7 +107,7 @@ Result<> CurvatureFlowImageFilter::executeImpl(DataStructure& dataStructure, con auto timeStep = filterArgs.value(k_TimeStep_Key); auto numberOfIterations = filterArgs.value(k_NumberOfIterations_Key); - return ImageProcessing::ExecuteFiniteDifferenceImageFilter( + return ImageProcessing::ExecuteFiniteDifferenceImageFilter( dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, ImageProcessing::detail::CurvatureFlowFn{}, timeStep, numberOfIterations, shouldCancel, messageHandler); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp index 9dc57e10c2..5eb8d6e572 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/GaussianImageFilterExecutors.cpp @@ -1,4 +1,7 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "NXImageProcessing/utils/SmoothingRecursiveGaussian2D.hpp" + +#include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" @@ -221,7 +224,7 @@ template Result<> ApplySmoothingRecursiveGaussian3D(const AbstractDataStore& input, AbstractDataStore& output, WorkStoreT& work, const SizeVec3& dims, const FloatVec3& spacing, const std::vector& sigma, bool normalizeAcrossScale, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { - detail::SmoothingGaussianConversionState conversionState; + detail::TerminalConversionState conversionState; RecursiveGaussianPassOptions options; options.CheckedSmoothing = true; Result<> accumulated = RecursiveGaussianAxisPass(input, work, dims, 2, sigma[2], static_cast(spacing[2]), 0, normalizeAcrossScale, shouldCancel, messageHandler, options); @@ -241,7 +244,7 @@ Result<> ApplySmoothingRecursiveGaussian3D(const AbstractDataStore& input, Ab const RecursiveGaussianPlaneAxis first{.axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; const RecursiveGaussianPlaneAxis second{.axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; accumulated = MergeResults(std::move(accumulated), RecursiveGaussianXYPlaneCascadeToSink(work, dims, first, second, shouldCancel, messageHandler, outputSink)); - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return detail::ReportSmoothingGaussianConversion(conversionState, std::move(accumulated)); } template @@ -339,34 +342,6 @@ Result<> ApplyLaplacianRecursiveGaussian2D(const AbstractDataStore& input, Ab return {}; } -template -Result<> ApplySmoothingRecursiveGaussian2D(const AbstractDataStore& input, AbstractDataStore& output, WorkStoreT& work, CheckpointStoreT& checkpoints, const SizeVec3& dims, - const FloatVec3& spacing, const std::vector& sigma, bool normalizeAcrossScale, const detail::RecursiveGaussian2DBufferPlan& plan, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) -{ - detail::SmoothingGaussianConversionState conversionState; - std::vector outputBuffer; - auto terminalSink = [&](usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span filtered, nonstd::span) -> Result<> { - if constexpr(std::is_same_v) - { - return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size()), true); - } - outputBuffer.resize(filtered.size()); - if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filtered.data(), filtered.size()), nonstd::span(outputBuffer.data(), outputBuffer.size()), (yBegin * dims[0]) + xBegin, - conversionState, shouldCancel, columnCount, dims[0])) - { - return detail::SmoothingGaussianNonFiniteResult(conversionState); - } - return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(outputBuffer.data(), outputBuffer.size()), true); - }; - const RecursiveGaussianPlaneAxis smoothY{ - .axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; - const RecursiveGaussianPlaneAxis smoothX{ - .axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; - Result<> result = RecursiveGaussian2DYXCascadeToSink(input, work, checkpoints, dims, smoothY, smoothX, plan, shouldCancel, messageHandler, terminalSink); - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(result)); -} - /// @brief Dispatched execute body for the GradientMagnitudeRecursiveGaussian filter. /// Input is a T-typed scalar image and output is a fixed float32 gradient-magnitude image. For each filtered axis, it /// differentiates along that axis, smooths along every other axis, and accumulates the squared, spacing-scaled result. @@ -902,7 +877,7 @@ struct SmoothingRecursiveGaussianExecuteFn if(usePlaneCascade) { std::vector outputPlane(dims[0] * dims[1]); - detail::SmoothingGaussianConversionState conversionState; + detail::TerminalConversionState conversionState; auto outputSink = [&](usize planeOffset, nonstd::span filteredPlane) -> Result<> { if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filteredPlane.data(), filteredPlane.size()), nonstd::span(outputPlane.data(), filteredPlane.size()), planeOffset, conversionState, shouldCancel)) @@ -917,7 +892,7 @@ struct SmoothingRecursiveGaussianExecuteFn const RecursiveGaussianPlaneAxis second{ .axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; Result<> result = RecursiveGaussianXYPlaneCascadeToSink(inStore, dims, first, second, shouldCancel, messageHandler, outputSink); - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(result)); + return detail::ReportSmoothingGaussianConversion(conversionState, std::move(result)); } const std::string format = detail::SelectRecursiveGaussianWorkingDataFormat(inStore.getStoreType(), inStore.getDataFormat(), outStore.getStoreType(), outStore.getDataFormat()); @@ -959,7 +934,7 @@ struct SmoothingRecursiveGaussianExecuteFn } } } - return MergeResults(std::move(accumulated), CastFloat32StoreTo(work, outStore, shouldCancel)); + return MergeResults(std::move(accumulated), CastFloat32StoreTo(work, outStore, shouldCancel, detail::ReportSmoothingGaussianConversion)); } }; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp index a5edfb1fca..170f23d3f0 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp @@ -218,7 +218,7 @@ struct AxisProjectionExecuteFn * ConductanceParameter, ...) are supplied by the caller; this functor only resolves the T-typed input/output * stores and forwards to the streaming @ref ApplyFiniteDifference driver. */ -template +template struct FiniteDifferenceExecuteFn { DataStructure& dataStructure; @@ -237,7 +237,8 @@ struct FiniteDifferenceExecuteFn { const auto& inArray = dataStructure.getDataRefAs>(inputArrayPath); auto& outArray = dataStructure.getDataRefAs>(outputArrayPath); - return ApplyFiniteDifference(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); + return ApplyFiniteDifference(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, + messageHandler); } }; @@ -382,9 +383,9 @@ Result<> ExecuteAxisProjectionImageFilter(DataStructure& dataStructure, const Da * (CurvatureFlowFn etc.), which may itself hold extra parameters set by the caller. @p TypeSetPolicy * selects which scalar types the filter accepts (AllNumeric for CurvatureFlow; FloatingScalar for the * other three). The caller's preflight must have enforced a scalar input. - * CheckedOutput is an explicit Curvature Flow opt-in; other finite-difference callers keep the default. + * CheckedOutput selects the checked Curvature Flow path. ReportConversion supplies its diagnostics. */ -template +template Result<> ExecuteFiniteDifferenceImageFilter(DataStructure& dataStructure, const DataPath& imageGeomPath, const DataPath& inputArrayPath, const DataPath& outputArrayPath, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { @@ -393,7 +394,8 @@ Result<> ExecuteFiniteDifferenceImageFilter(DataStructure& dataStructure, const const auto& imageGeom = dataStructure.getDataRefAs(imageGeomPath); const SizeVec3 dims = imageGeom.getDimensions(); const FloatVec3 spacing = imageGeom.getSpacing(); - const detail::FiniteDifferenceExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, spacing, fn, timeStep, numberOfIterations, shouldCancel, messageHandler}; + const detail::FiniteDifferenceExecuteFn executeFn{dataStructure, inputArrayPath, outputArrayPath, dims, spacing, fn, + timeStep, numberOfIterations, shouldCancel, messageHandler}; return TypeSetPolicy::dispatch(inputType, executeFn); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/SmoothingRecursiveGaussian2D.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/SmoothingRecursiveGaussian2D.hpp new file mode 100644 index 0000000000..6ff6a246e2 --- /dev/null +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/SmoothingRecursiveGaussian2D.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "TerminalConversionDiagnostics.hpp" + +#include "simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp" + +#include +#include +#include + +namespace nx::core::ImageProcessing::detail +{ +/** @brief Smooths a 2D image with the provided work and checkpoint stores, then converts the output. */ +template +Result<> ApplySmoothingRecursiveGaussian2D(const AbstractDataStore& input, AbstractDataStore& output, WorkStoreT& work, CheckpointStoreT& checkpoints, const SizeVec3& dims, + const FloatVec3& spacing, const std::vector& sigma, bool normalizeAcrossScale, const detail::RecursiveGaussian2DBufferPlan& plan, + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) +{ + detail::TerminalConversionState conversionState; + std::vector outputBuffer; + auto terminalSink = [&](usize yBegin, usize xBegin, usize rowCount, usize columnCount, nonstd::span filtered, nonstd::span) -> Result<> { + if constexpr(std::is_same_v) + { + return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(filtered.data(), filtered.size()), true); + } + outputBuffer.resize(filtered.size()); + if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(filtered.data(), filtered.size()), nonstd::span(outputBuffer.data(), outputBuffer.size()), (yBegin * dims[0]) + xBegin, + conversionState, shouldCancel, columnCount, dims[0])) + { + return detail::SmoothingGaussianNonFiniteResult(conversionState); + } + return ImageProcessing::WriteRecursiveGaussian2DBlock(output, dims[0], yBegin, xBegin, rowCount, columnCount, nonstd::span(outputBuffer.data(), outputBuffer.size()), true); + }; + const RecursiveGaussianPlaneAxis smoothY{ + .axis = 1, .sigma = sigma[1], .spacing = static_cast(spacing[1]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + const RecursiveGaussianPlaneAxis smoothX{ + .axis = 0, .sigma = sigma[0], .spacing = static_cast(spacing[0]), .order = 0, .normalizeAcrossScale = normalizeAcrossScale, .CheckedSmoothing = true}; + Result<> result = RecursiveGaussian2DYXCascadeToSink(input, work, checkpoints, dims, smoothY, smoothX, plan, shouldCancel, messageHandler, terminalSink); + return detail::ReportSmoothingGaussianConversion(conversionState, std::move(result)); +} +} // namespace nx::core::ImageProcessing::detail diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/TerminalConversionDiagnostics.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/TerminalConversionDiagnostics.hpp new file mode 100644 index 0000000000..e8eef82d27 --- /dev/null +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/TerminalConversionDiagnostics.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp" + +#include +#include +#include + +namespace nx::core::ImageProcessing::detail +{ +/** + * @brief Adds Curvature Flow conversion diagnostics after the transfer diagnostics. + * @param state Conversion outcomes from the completed buffers. + * @param result Diagnostics received before finalization. + * @return The transfer and conversion diagnostics in their original order. + */ +inline Result<> ReportCurvatureFlowConversion(const TerminalConversionState& state, Result<> result) +{ + Result<> conversion; + const usize failedIndex = state.nonFiniteIndex(); + if(failedIndex != std::numeric_limits::max()) + { + conversion = MakeErrorResult( + k_CurvatureFlowNonFiniteIntegerOutput, + fmt::format("Curvature Flow produced a nonfinite integer output at flat value index {}. Reduce Time Step or use a floating-point input array to retain nonfinite output.", failedIndex)); + } + if(state.clipped()) + { + conversion.warnings().push_back({.code = k_CurvatureFlowClippedIntegerOutput, + .message = "Curvature Flow clipped final integer values to the output type's range after truncation. The evolution may be unstable. " + "Reduce Time Step and check downstream masks and segmentation for clipping plateaus."}); + } + return MergeResults(std::move(result), std::move(conversion)); +} + +/** + * @brief Reports a conversion failure to stop the active smoothing pass. + * @tparam T Output pixel type used in the error message. + * @param state Conversion outcomes from the completed buffers. + * @return An error at the lowest rejected index, or a valid result when there is no rejection. + */ +template +Result<> SmoothingGaussianNonFiniteResult(const TerminalConversionState& state) +{ + if(state.nonFiniteIndex() == std::numeric_limits::max()) + { + return {}; + } + return MakeErrorResult(k_SmoothingGaussianNonFiniteIntegerOutput, fmt::format("Smoothing Recursive Gaussian produced a nonfinite value at flat value index {} for output type '{}'. Use a " + "floating-point input array to retain nonfinite output, or correct nonfinite input values.", + state.nonFiniteIndex(), DataTypeToString(GetDataType()))); +} + +/** + * @brief Adds Smoothing Recursive Gaussian conversion diagnostics while retaining the first error. + * @tparam T Output pixel type used in the diagnostics. + * @param state Conversion outcomes from the completed buffers. + * @param result Diagnostics received before finalization. + * @return The first error and all diagnostics collected by the smoothing pass. + */ +template +Result<> ReportSmoothingGaussianConversion(const TerminalConversionState& state, Result<> result) +{ + if(result.valid()) + { + result = MergeResults(std::move(result), SmoothingGaussianNonFiniteResult(state)); + } + if(state.clipped()) + { + result.warnings().push_back({.code = k_SmoothingGaussianClippedIntegerOutput, + .message = fmt::format("Smoothing Recursive Gaussian clipped finite final integer values to the '{}' range after truncation. Review the " + "output before thresholding or segmentation; use a floating-point input to retain these values.", + DataTypeToString(GetDataType()))}); + } + return result; +} +} // namespace nx::core::ImageProcessing::detail diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 8084e58e49..20ab92c9ab 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -5,6 +5,8 @@ #include "NXImageProcessing/Filters/CurvatureFlowImageFilter.hpp" +#include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" + #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Array.hpp" #include "simplnx/Common/Extent.hpp" @@ -641,13 +643,13 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong t Result<> result; if(pingPong) { - result = ImageProcessing::ApplyFiniteDifference3DPingPong(input, output, first, second, dims, {1.0, 1.0, 1.0}, - ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, {}); + result = ImageProcessing::ApplyFiniteDifference3DPingPong( + input, output, first, second, dims, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, {}); } else { - result = ImageProcessing::ApplyFiniteDifference(input, output, dims, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, - shouldCancel, {}); + result = ImageProcessing::ApplyFiniteDifference( + input, output, dims, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, {}); } if(nonFinite) { @@ -680,8 +682,8 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op propagates CurvatureFlowForwardingStore output(shape, false, scenario == 1); input.setValue(0, std::numeric_limits::max()); output.setValue(0, 7); - const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {1, 1, 1}, {1.0F, 1.0F, 1.0F}, - ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); + const auto result = ImageProcessing::ApplyFiniteDifference( + input, output, {1, 1, 1}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); if(scenario >= 2) { SIMPLNX_RESULT_REQUIRE_VALID(result); @@ -712,8 +714,8 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: resident wrapper merges // This endpoint selects the wrapper and injects its final-copy fault. Real backing is checked in the paired tests. CurvatureFlowForwardingStore output(shape, false, true, true); const std::atomic_bool shouldCancel{false}; - const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {3, 3, 2}, {1.0F, 1.0F, 1.0F}, - ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); + const auto result = ImageProcessing::ApplyFiniteDifference( + input, output, {3, 3, 2}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().front().code == -28891); REQUIRE(result.warnings().size() == 1); @@ -774,8 +776,8 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded input.setValue(0, std::numeric_limits::max()); input.setValue(count - 1, (uint64{1} << 53) + 1); const std::atomic_bool shouldCancel{false}; - const auto result = ImageProcessing::ApplyFiniteDifference(input, output, {count, 1, 1}, {1.0F, 1.0F, 1.0F}, - ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); + const auto result = ImageProcessing::ApplyFiniteDifference( + input, output, {count, 1, 1}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, 0.0, 1, shouldCancel, {}); SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(result.warnings().empty()); REQUIRE(input.readCount() == 3); @@ -816,15 +818,15 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exi Result<> result; if(route == 2) { - result = ImageProcessing::ApplyFiniteDifference(input, output, {3, 3, 3}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, - 10.0, 1, shouldCancel, {}); + result = ImageProcessing::ApplyFiniteDifference( + input, output, {3, 3, 3}, {1.0F, 1.0F, 1.0F}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); } else { DataStore first(shape, ShapeType{1}, 0.0); DataStore second(shape, ShapeType{1}, 0.0); - result = ImageProcessing::ApplyFiniteDifference3DPingPong(input, output, first, second, {3, 3, 3}, {1.0, 1.0, 1.0}, - ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); + result = ImageProcessing::ApplyFiniteDifference3DPingPong( + input, output, first, second, {3, 3, 3}, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}); } if(route == 0) { @@ -878,7 +880,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D ter const auto plan = ImageProcessing::detail::BuildFiniteDifference2DBufferPlan(3, 3, 1, sizeof(float64), limit); REQUIRE(plan.coreCols == 3); REQUIRE(plan.coreRows == 1); - const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( + const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( input, output, first, second, 3, 3, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); if(cancel) { @@ -953,7 +955,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: X-tiled transfers retain REQUIRE(plan.valid); REQUIRE(plan.coreCols == 2); REQUIRE(plan.coreRows == 1); - const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( + const auto result = ImageProcessing::ApplyFiniteDifference2DBounded( input, output, first, second, width, height, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, 10.0, 1, shouldCancel, {}, limit); if(failRead || failWrite) { diff --git a/src/Plugins/NXImageProcessing/test/RecursiveGaussianEngineTest.cpp b/src/Plugins/NXImageProcessing/test/RecursiveGaussianEngineTest.cpp index dca1604259..79c99ee97d 100644 --- a/src/Plugins/NXImageProcessing/test/RecursiveGaussianEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/RecursiveGaussianEngineTest.cpp @@ -1,5 +1,7 @@ -#include "simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp" +#include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" + #include "simplnx/Utilities/ImageProcessing/GaussianTemporaryStore.hpp" +#include "simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp" #include "simplnx/Common/Array.hpp" #include "simplnx/Common/Types.hpp" @@ -654,7 +656,7 @@ TEST_CASE("NXImageProcessing::RecursiveGaussianEngine: forced buffered postproce REQUIRE(SqrtStoreInPlace(squared, shouldCancel, options).valid()); DataStore cast(tupleShape, ShapeType{1}, int16{}); - REQUIRE(CastFloat32StoreTo(scaled, cast, shouldCancel, options).valid()); + REQUIRE(CastFloat32StoreTo(scaled, cast, shouldCancel, ImageProcessing::detail::ReportSmoothingGaussianConversion, options).valid()); for(usize i = 0; i < k_TotalValues; ++i) { const double value = static_cast(source.getValue(i)) / 1.25; diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 4a97ba7d69..5f13bdb9d2 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -5,6 +5,8 @@ #include "NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.hpp" +#include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" + #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" @@ -433,7 +435,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and options.forceBuffered = buffered; options.targetScratchBytes = 5; const std::atomic_bool shouldCancel{false}; - const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, ImageProcessing::detail::ReportSmoothingGaussianConversion, options); SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == (fixture == 2 || fixture == 4 ? 1 : 0)); REQUIRE(output.getValue(0) == expected.at(fixture)); @@ -455,7 +457,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: fl options.forceBuffered = buffered; options.targetScratchBytes = 5; const std::atomic_bool shouldCancel{false}; - const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, ImageProcessing::detail::ReportSmoothingGaussianConversion, options); SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(result.warnings().empty()); REQUIRE(output.getValue(0) == TestType{93.75}); @@ -483,7 +485,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: te options.forceBuffered = buffered; options.targetScratchBytes = 1; const std::atomic_bool shouldCancel{false}; - const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, ImageProcessing::detail::ReportSmoothingGaussianConversion, options); REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == 1); if(nonfinite) { @@ -512,7 +514,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping wa ImageProcessing::detail::PointwiseExecutionOptions options; options.forceBuffered = true; options.targetScratchBytes = 5; - const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, options); + const Result<> result = ImageProcessing::CastFloat32StoreTo(source, output, shouldCancel, ImageProcessing::detail::ReportSmoothingGaussianConversion, options); REQUIRE(countSmoothingWarning(result, ImageProcessing::k_SmoothingGaussianClippedIntegerOutput) == 1); REQUIRE(countSmoothingWarning(result, -23692) >= 1); REQUIRE(countSmoothingWarning(result, -23693) >= 1); diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 4691d8ca1b..2ba79780ae 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -59,43 +59,8 @@ inline Result<> ValidateCurvatureFlowControls(float64 timeStep, const FloatVec3& return {}; } -class CurvatureFlowConversionState -{ -public: - CurvatureFlowConversionState() = default; - ~CurvatureFlowConversionState() = default; - CurvatureFlowConversionState(const CurvatureFlowConversionState&) = delete; - CurvatureFlowConversionState(CurvatureFlowConversionState&&) = delete; - CurvatureFlowConversionState& operator=(const CurvatureFlowConversionState&) = delete; - CurvatureFlowConversionState& operator=(CurvatureFlowConversionState&&) = delete; - - void recordClipping() noexcept - { - m_Clipped.store(true, std::memory_order_relaxed); - } - bool clipped() const noexcept - { - return m_Clipped.load(std::memory_order_relaxed); - } - void recordNonFinite(usize index) noexcept - { - usize previous = m_NonFiniteIndex.load(std::memory_order_relaxed); - while(index < previous && !m_NonFiniteIndex.compare_exchange_weak(previous, index, std::memory_order_relaxed)) - { - } - } - usize nonFiniteIndex() const noexcept - { - return m_NonFiniteIndex.load(std::memory_order_relaxed); - } - -private: - std::atomic m_Clipped{false}; - std::atomic m_NonFiniteIndex{std::numeric_limits::max()}; -}; - template -bool ConvertFiniteDifferenceOutputBuffer(nonstd::span input, nonstd::span output, usize offset, CurvatureFlowConversionState& state, const std::atomic_bool& shouldCancel, +bool ConvertFiniteDifferenceOutputBuffer(nonstd::span input, nonstd::span output, usize offset, TerminalConversionState& state, const std::atomic_bool& shouldCancel, usize valuesPerRow = 0, usize rowStride = 0) { bool clipped = false; @@ -136,29 +101,6 @@ bool ConvertFiniteDifferenceOutputBuffer(nonstd::span input, nonstd: return true; } -template -Result<> FinishFiniteDifferenceConversion(const CurvatureFlowConversionState& state) -{ - Result<> result; - if constexpr(CheckedOutput) - { - const usize failedIndex = state.nonFiniteIndex(); - if(failedIndex != std::numeric_limits::max()) - { - result = MakeErrorResult( - k_CurvatureFlowNonFiniteIntegerOutput, - fmt::format("Curvature Flow produced a nonfinite integer output at flat value index {}. Reduce Time Step or use a floating-point input array to retain nonfinite output.", failedIndex)); - } - if(state.clipped()) - { - result.warnings().push_back({.code = k_CurvatureFlowClippedIntegerOutput, - .message = "Curvature Flow clipped final integer values to the output type's range after truncation. The evolution may be unstable. " - "Reduce Time Step and check downstream masks and segmentation for clipping plateaus."}); - } - } - return result; -} - template Result<> CopyCurvatureFlowInput(const AbstractDataStore& input, AbstractDataStore& output, const std::atomic_bool& shouldCancel) { @@ -1513,17 +1455,17 @@ Result<> UpdateGlobalConductance2DBounded(F& fn, const StoreT& currentStore, usi return {}; } -template +template Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, AbstractDataStore& outStore, WorkingStoreT& firstStore, WorkingStoreT& secondStore, usize nx, usize ny, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit) { - detail::CurvatureFlowConversionState conversionState; + detail::TerminalConversionState conversionState; Result<> ioResult; const auto finish = [&](Result<> result = {}) { if constexpr(CheckedOutput) { - return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + return ReportConversion(conversionState, MergeResults(std::move(ioResult), std::move(result))); } return result; }; @@ -1728,17 +1670,17 @@ Result<> ApplyFiniteDifference2DBounded(const AbstractDataStore& inStore, Abs return finish(); } -template +template Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, AbstractDataStore& outStore, WorkingStoreT& firstStore, WorkingStoreT& secondStore, const SizeVec3& dims, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) { - detail::CurvatureFlowConversionState conversionState; + detail::TerminalConversionState conversionState; Result<> ioResult; const auto finish = [&](Result<> result = {}) { if constexpr(CheckedOutput) { - return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + return ReportConversion(conversionState, MergeResults(std::move(ioResult), std::move(result))); } return result; }; @@ -2039,7 +1981,7 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab return finish(); } -template +template Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, const std::array& sc, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit2D) { @@ -2105,10 +2047,11 @@ Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, Abstra TemporaryFiniteDifferenceStore secondStore(*secondRecords, shouldCancel); if(nz == 1) { - return ApplyFiniteDifference2DBounded(inStore, outStore, firstStore, secondStore, nx, ny, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, - residentLimit2D); + return ApplyFiniteDifference2DBounded(inStore, outStore, firstStore, secondStore, nx, ny, sc, fn, timeStep, numberOfIterations, shouldCancel, + messageHandler, residentLimit2D); } - return ApplyFiniteDifference3DPingPong(inStore, outStore, firstStore, secondStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler); + return ApplyFiniteDifference3DPingPong(inStore, outStore, firstStore, secondStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, + messageHandler); } // Full ITK-free FiniteDifference driver. Resident DataStore execution retains the original full-volume @@ -2134,19 +2077,19 @@ Result<> ApplyFiniteDifferenceOocRaw(const AbstractDataStore& inStore, Abstra // hardcoded to float64, the float32-input live-ITK grid diverged from ITK by ~1 ULP at essentially every voxel -- // a deterministic precision-MODEL mismatch, not thread-order FP noise -- and resolved once Real became T here). // CheckedOutput opts into the selected Curvature Flow controls, exact no-ops, and terminal conversion only. -// Other finite-difference filters retain the default behavior and their own precision models. -template +// The checked caller supplies ReportConversion to add its diagnostics at the end of conversion. +template // NOLINTNEXTLINE(misc-no-recursion) -- The staging call has two resident endpoints, so it cannot re-enter the out-of-core staging branch. Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractDataStore& outStore, const SizeVec3& dims, FloatVec3 spacing, F fn, float64 timeStep, uint32 numberOfIterations, const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, usize residentLimit2D = detail::k_FiniteDifference2DResidentLimit) { using Real = std::conditional_t; - detail::CurvatureFlowConversionState conversionState; + detail::TerminalConversionState conversionState; Result<> ioResult; const auto finish = [&](Result<> result = {}) { if constexpr(CheckedOutput) { - return MergeResults(MergeResults(std::move(ioResult), std::move(result)), detail::FinishFiniteDifferenceConversion(conversionState)); + return ReportConversion(conversionState, MergeResults(std::move(ioResult), std::move(result))); } return result; }; @@ -2222,8 +2165,9 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData { return finish(std::move(inputResult)); } - Result<> evolutionResult = MergeResults(std::move(inputResult), ApplyFiniteDifference(residentInput, residentOutput, dims, spacing, fn, timeStep, numberOfIterations, - shouldCancel, messageHandler, residentLimit2D)); + Result<> evolutionResult = + MergeResults(std::move(inputResult), ApplyFiniteDifference(residentInput, residentOutput, dims, spacing, fn, timeStep, numberOfIterations, + shouldCancel, messageHandler, residentLimit2D)); if(evolutionResult.invalid()) { return finish(std::move(evolutionResult)); @@ -2258,7 +2202,7 @@ Result<> ApplyFiniteDifference(const AbstractDataStore& inStore, AbstractData if(hasOutOfCoreEndpoint) { - return ApplyFiniteDifferenceOocRaw(inStore, outStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); + return ApplyFiniteDifferenceOocRaw(inStore, outStore, dims, sc, fn, timeStep, numberOfIterations, shouldCancel, messageHandler, residentLimit2D); } // Resident/fallback stores: accum (Real) = cast(in), streamed plane by plane -- ITK's IntermediateType diff --git a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp index d3d97ed1ca..bb55c37f20 100644 --- a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp @@ -3,7 +3,6 @@ #include "simplnx/Common/Array.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" -#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/Filter/IFilter.hpp" @@ -79,43 +78,8 @@ inline Result<> MergeRecursiveGaussianResults(bool checkedSmoothing, Result<> pr return current.invalid() ? std::move(current) : Result<>{}; } -class SmoothingGaussianConversionState -{ -public: - SmoothingGaussianConversionState() = default; - ~SmoothingGaussianConversionState() = default; - SmoothingGaussianConversionState(const SmoothingGaussianConversionState&) = delete; - SmoothingGaussianConversionState(SmoothingGaussianConversionState&&) = delete; - SmoothingGaussianConversionState& operator=(const SmoothingGaussianConversionState&) = delete; - SmoothingGaussianConversionState& operator=(SmoothingGaussianConversionState&&) = delete; - - void recordClipping() noexcept - { - m_Clipped.store(true, std::memory_order_relaxed); - } - bool clipped() const noexcept - { - return m_Clipped.load(std::memory_order_relaxed); - } - void recordNonFinite(usize index) noexcept - { - usize previous = m_NonFiniteIndex.load(std::memory_order_relaxed); - while(index < previous && !m_NonFiniteIndex.compare_exchange_weak(previous, index, std::memory_order_relaxed)) - { - } - } - usize nonFiniteIndex() const noexcept - { - return m_NonFiniteIndex.load(std::memory_order_relaxed); - } - -private: - std::atomic m_Clipped{false}; - std::atomic m_NonFiniteIndex{std::numeric_limits::max()}; -}; - template -bool ConvertSmoothingGaussianBuffer(nonstd::span input, nonstd::span output, usize offset, SmoothingGaussianConversionState& state, const std::atomic_bool& shouldCancel, +bool ConvertSmoothingGaussianBuffer(nonstd::span input, nonstd::span output, usize offset, TerminalConversionState& state, const std::atomic_bool& shouldCancel, usize valuesPerRow = 0, usize rowStride = 0) { bool clipped = false; @@ -149,36 +113,6 @@ bool ConvertSmoothingGaussianBuffer(nonstd::span input, nonstd::s return true; } -template -Result<> SmoothingGaussianNonFiniteResult(const SmoothingGaussianConversionState& state) -{ - if(state.nonFiniteIndex() == std::numeric_limits::max()) - { - return {}; - } - return MakeErrorResult(k_SmoothingGaussianNonFiniteIntegerOutput, fmt::format("Smoothing Recursive Gaussian produced a nonfinite value at flat value index {} for output type '{}'. Use a " - "floating-point input array to retain nonfinite output, or correct nonfinite input values.", - state.nonFiniteIndex(), DataTypeToString(GetDataType()))); -} - -/** @brief Finalizes the one owner warning on success, cancellation, or a later transfer/conversion failure. */ -template -Result<> FinishSmoothingGaussianConversion(const SmoothingGaussianConversionState& state, Result<> result) -{ - if(result.valid()) - { - result = MergeResults(std::move(result), SmoothingGaussianNonFiniteResult(state)); - } - if(state.clipped()) - { - result.warnings().push_back({.code = k_SmoothingGaussianClippedIntegerOutput, - .message = fmt::format("Smoothing Recursive Gaussian clipped finite final integer values to the '{}' range after truncation. Review the " - "output before thresholding or segmentation; use a floating-point input to retain these values.", - DataTypeToString(GetDataType()))}); - } - return result; -} - // Four-line batches are the computational unit, not the reporting unit. Group them so a worker // enters the shared progress seam once per this many batches. constexpr usize k_ProgressBatchGroup = 64; @@ -2597,11 +2531,21 @@ inline Result<> SqrtStoreInPlace(AbstractDataStore& store, const std::a return {}; } -// Smoothing final conversion retains float32 pass values and checks only the terminal integer conversion. +/** + * @brief Writes the final float32 working values in the destination type. + * @tparam T Destination pixel type. + * @param src Completed Gaussian pass values. + * @param dst Destination with the same value count as the source. + * @param shouldCancel Request to stop conversion between batches. + * @param reportConversion Adds conversion diagnostics to the result on each executed path. + * @param options Controls the direct or buffered route and the scratch memory budget. + * @return The first error and the collected transfer and conversion warnings. + */ template -Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataStore& dst, const std::atomic_bool& shouldCancel, const detail::PointwiseExecutionOptions& options = {}) +Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataStore& dst, const std::atomic_bool& shouldCancel, detail::TerminalConversionReporter& reportConversion, + const detail::PointwiseExecutionOptions& options = {}) { - detail::SmoothingGaussianConversionState conversionState; + detail::TerminalConversionState conversionState; Result<> accumulated; const usize total = src.getSize(); if(dst.getSize() != total) @@ -2620,7 +2564,7 @@ Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataS detail::ConvertSmoothingGaussianBuffer(srcValues.subspan(range.min(), range.max() - range.min()), dstValues.subspan(range.min(), range.max() - range.min()), range.min(), conversionState, shouldCancel); }); - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } Result planResult = detail::MakeRecursiveGaussianPostprocessingPlan( total, sizeof(float32) + sizeof(T), options.targetScratchBytes, @@ -2637,25 +2581,25 @@ Result<> CastFloat32StoreTo(const AbstractDataStore& src, AbstractDataS { if(shouldCancel) { - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } const usize n = std::min(batchValues, total - start); accumulated = MergeResults(std::move(accumulated), src.copyIntoBuffer(start, nonstd::span(s.data(), n))); if(accumulated.invalid()) { - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } if(!detail::ConvertSmoothingGaussianBuffer(nonstd::span(s.data(), n), nonstd::span(d.data(), n), start, conversionState, shouldCancel)) { - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } accumulated = MergeResults(std::move(accumulated), dst.copyFromBuffer(start, nonstd::span(d.data(), n))); if(accumulated.invalid()) { - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } start += n; } - return detail::FinishSmoothingGaussianConversion(conversionState, std::move(accumulated)); + return reportConversion(conversionState, std::move(accumulated)); } } // namespace nx::core::ImageProcessing diff --git a/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp index 77be4ddf96..d7231c196f 100644 --- a/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp +++ b/src/simplnx/Utilities/ImageProcessing/TerminalImageConversion.hpp @@ -1,5 +1,9 @@ #pragma once +#include "simplnx/Common/Result.hpp" +#include "simplnx/Common/Types.hpp" + +#include #include #include #include @@ -18,6 +22,59 @@ enum class TerminalImageConversionStatus : std::uint8_t NonFinite ///< A nonfinite integer result was rejected; output is unchanged. }; +/** + * @class TerminalConversionState + * @brief Collects clipping and the lowest rejected index for one terminal conversion. + * @details Workers can record outcomes concurrently. Read the state after the workers join. + */ +class TerminalConversionState +{ +public: + TerminalConversionState() = default; + ~TerminalConversionState() = default; + TerminalConversionState(const TerminalConversionState&) = delete; + TerminalConversionState(TerminalConversionState&&) = delete; + TerminalConversionState& operator=(const TerminalConversionState&) = delete; + TerminalConversionState& operator=(TerminalConversionState&&) = delete; + + void recordClipping() noexcept + { + m_Clipped.store(true, std::memory_order_relaxed); + } + bool clipped() const noexcept + { + return m_Clipped.load(std::memory_order_relaxed); + } + /** + * @brief Records the smallest flat index of a rejected value. + * @param index Flat index in the output array. + */ + void recordNonFinite(usize index) noexcept + { + usize previous = m_NonFiniteIndex.load(std::memory_order_relaxed); + while(index < previous && !m_NonFiniteIndex.compare_exchange_weak(previous, index, std::memory_order_relaxed)) + { + } + } + /** + * @brief Returns the lowest index observed after the workers join. + * @return The flat index or the largest usize value if there was no rejected value. + */ + usize nonFiniteIndex() const noexcept + { + return m_NonFiniteIndex.load(std::memory_order_relaxed); + } + +private: + std::atomic m_Clipped{false}; + std::atomic m_NonFiniteIndex{std::numeric_limits::max()}; +}; + +/** + * @brief Adds caller-owned diagnostics to the result at the end of terminal conversion. + */ +using TerminalConversionReporter = Result<>(const TerminalConversionState&, Result<>); + /** * @brief Converts floating image values and maps finite narrowing overflow to signed infinity. * @tparam OutputType float or double destination type. From 93ea0545a0316d3002a7c969b11b117956a2674a Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:31:59 -0500 Subject: [PATCH 50/98] DOC: Explain Connected Component wrap in the tooltip Explain the scanline and tiled behavior when a provisional label wraps to zero in legacy mode. Addresses review item 10. Signed-off-by: Jessica Marquis --- .../Filters/ConnectedComponentImageFilter.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp index 690d4fc016..82aabfa63f 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp @@ -49,12 +49,14 @@ Parameters ConnectedComponentImageFilter::parameters() const "Set/Get whether the connected components are defined strictly by face connectivity or by face+edge+vertex connectivity. Default is FullyConnectedOff. " "For objects that are 1 pixel wide, use FullyConnectedOn.", false)); - params.insert(std::make_unique(k_ProvisionalLabelLimitHandling_Key, "Provisional Label Limit Handling", - "Controls what happens if more than 4,294,967,295 provisional run IDs are needed. Error (default) stops before IDs wrap. " - "Warn and Continue (Legacy Behavior) keeps uint32 wrap to background zero and reused IDs. Foreground can disappear, components can merge, " - "and segmentation, counts, and downstream statistics can be incorrect. Choose Warn only if you intentionally accept these consequences. " - "It does not increase label capacity or prevent memory or I/O errors.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); + params.insert( + std::make_unique(k_ProvisionalLabelLimitHandling_Key, "Provisional Label Limit Handling", + "Controls what happens if more than 4,294,967,295 provisional run IDs are needed. Error (default) stops before IDs wrap. " + "Warn and Continue (Legacy Behavior) keeps uint32 wrap to background zero and reused IDs. Foreground can disappear, components can merge, " + "and counts and downstream statistics can be incorrect. A wrapped zero from a previous row can erase a neighboring component in scanline processing. " + "Tiled processing skips that zero and can give different labels. Choose Warn only if you accept these consequences. " + "It does not increase label capacity or prevent memory or I/O errors.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), From 4e9fc6aee2df8d83aca56ceef6eaf0535ea3c2f2 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:31:59 -0500 Subject: [PATCH 51/98] REV: Fit Otsu thresholds with finite samples only Fit only selected finite pixels. Assign one excluded label to masked and skipped pixels. Check that component labels and phase fractions remain usable after selection. Addresses review item 1. Signed-off-by: Jessica Marquis --- .../docs/OtsuMultipleThresholdsImageFilter.md | 16 +- .../OtsuMultipleThresholdsImageFilter.cpp | 33 ++-- .../test/C15CompatibilityTestUtils.hpp | 19 +++ .../OtsuMultipleThresholdsImageFilterTest.cpp | 149 +++++++++++------- .../ImageProcessing/OtsuImageEngine.hpp | 31 ++-- 5 files changed, 159 insertions(+), 89 deletions(-) diff --git a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md index 0ceb97c227..a4d5ab43a2 100644 --- a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md @@ -19,19 +19,25 @@ The configured class interval is **Label Offset** through **Label Offset + Numbe **Nonfinite Input Handling** has two choices: - **Error** is the default. A selected NaN, negative infinity, or positive infinity stops the filter. Choose a deliberate handling method before rerunning the pipeline. -- **Warn and Continue** retains these selected samples in the histogram. Only selected finite values set the histogram bounds. NaN and negative infinity contribute to the first bin and receive the ordinary lowest class label. Positive infinity contributes to the last bin and receives the ordinary highest class label. These samples affect threshold fitting; they are not treated as excluded pixels. The filter returns one warning with counts for the three nonfinite kinds. Classification can be unreliable or unpredictable. +- **Warn and Continue** fits thresholds using selected finite pixels. Selected NaN and infinity pixels receive **Excluded Label**. One warning counts the nonfinite pixels still selected after any mask. -Both choices require at least one selected finite sample. Empty data, an all-false mask, or data with no selected finite value cannot define a histogram range and cause an error. Warn does not silently remove nonfinite samples. +Both choices require at least one selected finite sample. Empty data, an all-false mask, or data with no selected finite value cannot define a histogram range and cause an error. ### Explicit mask and excluded labels Enable **Use Mask Array** to select a **Mask Array**. The mask must be scalar `bool` or `uint8` and have the same tuple count as the input. Tuples are paired in flat order; the mask can have a different tuple shape or parent group. False/zero excludes a pixel; true or any nonzero `uint8` value selects it. The filter applies this selection before range, histogram, and nonfinite checks. An excluded NaN or infinity therefore causes no nonfinite error or warning. -Excluded pixels receive **Masked Label**, which defaults to 255. This label must lie outside the entire configured class interval, even if some configured classes have no pixels. With a mask, at most 255 ordinary classes and one distinct excluded label fit in `uint8`. An interval that uses all labels 0 through 255 cannot leave a distinct Masked Label. The filter reports a collision and requires a control change. +Pixels excluded by the mask and nonfinite pixels skipped in Warn mode use the same **Excluded Label**. It defaults to 255 and must be outside the configured class interval when a mask is enabled or Warn is used with floating-point input. This leaves up to 255 ordinary classes. Choose an unused label or reduce the class interval if the label collides. A distinct label does not make downstream filters ignore excluded pixels. Supply the mask or an explicit background/exclusion rule to consumers that must ignore them. Changing histogram participation can change phase fractions, feature counts, and connectivity. -For example, with input `[0, 0, 4, 4, NaN, -inf, +inf]`, two bins, one threshold, and Label Offset 7, Warn produces histogram counts `[4, 3]` and labels `[7, 7, 8, 8, 7, 7, 8]`. The finite bounds remain 0 and 4. An explicit mask `[1, 1, 1, 1, 0, 0, 0]` instead produces counts `[2, 2]`, no nonfinite warning, and labels `[7, 7, 8, 8, 255, 255, 255]` with Masked Label 255. This mask also permits Error mode because it excludes all nonfinite values. +For input [0, 0, 4, 4, NaN, negative infinity, positive infinity], Warn uses histogram counts [2, 2] with two bins. +With Label Offset 7 and Excluded Label 255, it writes [7, 7, 8, 8, 255, 255, 255]. A mask that excludes the three +nonfinite pixels gives the same labels with either choice and produces no nonfinite warning. + +For component labeling, first select the ordinary class of interest into a binary mask. For fractions measured over valid +pixels, exclude Excluded Label from both the selection and the denominator. Removing nonfinite samples from the histogram +can move the fitted thresholds and change the labels of finite pixels too. ### Precision and pipeline migration @@ -39,7 +45,7 @@ Histogram bounds, bin measurements, and threshold fitting use `float64`. The fil The finite tie tolerance, ordinary threshold search, and Valley Emphasis behavior are retained. **ReturnBinMidpoint** selects a bin midpoint instead of its maximum for the returned threshold. These checks add no automatic normalization or new scientific fitting rule. -Existing pipelines receive Error, Use Mask Array off, and Masked Label 255 as defaults. Ordinary finite pipelines with valid label intervals and usable histogram and solver arithmetic retain their labels. A finite pipeline that previously completed can now stop with a numeric error if the new checks detect an unusable calculation. Pipelines that previously contained NaN/infinity now stop by default. Choose Warn to keep the stated endpoint-bin participation, or supply an explicit mask to exclude samples. Replacing nonfinite values upstream adds replacement intensities to the histogram and can bias thresholds; it is not statistical exclusion. +Existing pipelines receive Error, Use Mask Array off, and Excluded Label 255 as defaults. Ordinary finite pipelines with valid label intervals and usable histogram and solver arithmetic retain their labels. A finite pipeline that previously completed can now stop with a numeric error if the new checks detect an unusable calculation. Pipelines that previously contained NaN/infinity now stop by default. Choose Warn to skip selected nonfinite pixels and assign Excluded Label. Replacing nonfinite values upstream adds replacement intensities to the histogram and can bias thresholds; it is not statistical exclusion. This filter also includes an option to use the valley emphasis algorithm from H.F. Ng, "Automatic thresholding for defect detection", Pattern Recognition Letters, (27): 1644-1649, 2006. The valley emphasis algorithm is particularly effective when the object to be thresholded is small. It is turned off by default; enable it with **Valley Emphasis**. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 4dead11ade..d5fa1401ef 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -67,18 +67,18 @@ Parameters OtsuMultipleThresholdsImageFilter::parameters() const params.insert(std::make_unique(k_ValleyEmphasis_Key, "Valley Emphasis", "Set/Get the use of valley emphasis. Default is false.", false)); params.insert( std::make_unique(k_ReturnBinMidpoint_Key, "ReturnBinMidpoint", "Should the threshold value be mid-point of the bin or the maximum? Default is to return bin maximum.", false)); - params.insert(std::make_unique( - k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", - "Error stops on selected NaN or infinity. Warn and Continue retains NaN/negative infinity in the first histogram bin and positive infinity in the last; classification may be unreliable.", 0, - ChoicesParameter::Choices{"Error", "Warn and Continue"})); + params.insertLinkableParameter(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", + "Error stops on selected NaN or infinity. Warn and Continue skips these pixels when fitting thresholds and assigns Excluded Label.", + 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertLinkableParameter( std::make_unique(k_UseMaskArray_Key, "Use Mask Array", "Explicitly exclude pixels with a zero/false mask value before range and nonfinite checks.", false)); params.insert(std::make_unique(k_MaskArrayPath_Key, "Mask Array", "Select a scalar bool or uint8 mask with one value per input tuple. Nonzero values select pixels.", DataPath{}, ArraySelectionParameter::AllowedTypes{DataType::boolean, DataType::uint8}, ArraySelectionParameter::AllowedComponentShapes{{1}})); - params.insert(std::make_unique(k_MaskedLabel_Key, "Masked Label", - "Label for excluded pixels. It must be outside the entire configured class interval; downstream filters must explicitly honor it.", uint8{255})); + params.insert(std::make_unique(k_MaskedLabel_Key, "Excluded Label", + "Label for pixels excluded by the input mask or skipped by Warn and Continue. Choose a value outside the ordinary class interval.", uint8{255})); params.linkParameters(k_UseMaskArray_Key, k_MaskArrayPath_Key, true); params.linkParameters(k_UseMaskArray_Key, k_MaskedLabel_Key, true); + params.linkParameters(k_NonfiniteInputHandling_Key, k_MaskedLabel_Key, ChoicesParameter::ValueType{1}); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -125,15 +125,22 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const MakeErrorResult(-8312, fmt::format("Label Offset ({}) plus Number Of Thresholds ({}) exceeds 255. Reduce either value to keep every class label within uint8.", labelOffset, static_cast(numberOfThresholds)))}; } - if(filterArgs.value(k_UseMaskArray_Key)) + const bool useMask = filterArgs.value(k_UseMaskArray_Key); + const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); + const auto inputType = inputArray.getDataType(); + const bool warnOnNonfinite = filterArgs.value(k_NonfiniteInputHandling_Key) == 1; + if(useMask || (warnOnNonfinite && (inputType == DataType::float32 || inputType == DataType::float64))) { const uint32 maskedLabel = filterArgs.value(k_MaskedLabel_Key); if(maskedLabel >= labelOffset && maskedLabel <= lastLabel) { return {.outputActions = MakeErrorResult( - -8313, fmt::format("Masked Label ({}) collides with the configured class interval [{}, {}]. Select a Masked Label outside this interval or reduce the class interval.", maskedLabel, - labelOffset, lastLabel))}; + -8313, fmt::format("Excluded Label ({}) collides with the configured class interval [{}, {}]. Select an Excluded Label outside this interval or reduce the class interval.", + maskedLabel, labelOffset, lastLabel))}; } + } + if(useMask) + { const auto maskPath = filterArgs.value(k_MaskArrayPath_Key); // ArraySelectionParameter also accepts nonnumeric array kinds. const auto* maskPtr = dataStructure.getDataAs(maskPath); @@ -143,15 +150,13 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const return {.outputActions = MakeErrorResult(invalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; } - const auto& input = dataStructure.getDataRefAs(selectedInputArray); - if(maskPtr->getNumberOfTuples() != input.getNumberOfTuples()) + if(maskPtr->getNumberOfTuples() != inputArray.getNumberOfTuples()) { return {.outputActions = MakeErrorResult(-8314, fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", - maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), input.getNumberOfTuples()))}; + maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), inputArray.getNumberOfTuples()))}; } } - const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); if(inputArray.getNumberOfComponents() != 1) { return {MakeErrorResult(-8310, fmt::format("Otsu Multiple Thresholds requires a single-component (scalar) input array, but '{}' has {} components.", selectedInputArray.toString(), @@ -173,7 +178,7 @@ Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStruc const ImageProcessing::OtsuImageOptions options{.NumberOfBins = filterArgs.value(k_NumberOfHistogramBins_Key), .NumberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key), .LabelOffset = filterArgs.value(k_LabelOffset_Key), - .MaskedLabel = filterArgs.value(k_MaskedLabel_Key), + .ExcludedLabel = filterArgs.value(k_MaskedLabel_Key), .WarnOnNonfinite = filterArgs.value(k_NonfiniteInputHandling_Key) == 1, .ValleyEmphasis = filterArgs.value(k_ValleyEmphasis_Key), .ReturnBinMidpoint = filterArgs.value(k_ReturnBinMidpoint_Key)}; diff --git a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp index 8cd852533e..729075b795 100644 --- a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp +++ b/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp @@ -8,6 +8,7 @@ #include "NXImageProcessing/Filters/ReadMhaFileFilter.hpp" #include "NXImageProcessing/Filters/WriteImageFilter.hpp" #include "NXImageProcessing/NXImageProcessing_test_dirs.hpp" +#include "SimplnxCore/Filters/ComputeArrayStatisticsFilter.hpp" #include "SimplnxCore/Filters/ConvertDataFilter.hpp" #include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Core/Application.hpp" @@ -191,6 +192,24 @@ inline DataPath Components(DataStructure& dataStructure, const DataPath& input, return input.replaceName(name); } +inline float32 MaskedMean(DataStructure& dataStructure, const DataPath& input, const DataPath& mask) +{ + const ComputeArrayStatisticsFilter filter; + const DataPath stats({"Phase Statistics"}); + auto args = filter.getDefaultArguments(); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_FindMean_Key, std::make_any(true)); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_UseMask_Key, std::make_any(true)); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_SelectedArrayPath_Key, std::make_any(input)); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_MaskArrayPath_Key, std::make_any(mask)); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_DestinationAttributeMatrixPath_Key, std::make_any(stats)); + args.insertOrAssign(ComputeArrayStatisticsFilter::k_MeanArrayName_Key, std::make_any("Phase Fraction")); + ExecuteChecked(filter, dataStructure, args); + const Float32Array* result = nullptr; + REQUIRE_NOTHROW(result = &dataStructure.getDataRefAs(stats.createChildPath("Phase Fraction"))); + REQUIRE(result->getNumberOfTuples() == 1); + return result->getValue(0); +} + inline void SetGeometry(DataStructure& dataStructure) { ImageGeom* geometryPtr = nullptr; diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index ebbe7ff1ba..2edc72cc6a 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -182,7 +182,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask s // NOLINTEND(bugprone-chained-comparison) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: masked label has a separate uint8 control", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: excluded label is active for mask or Warn", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; @@ -192,10 +192,16 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: masked label ha REQUIRE(parameters.contains(maskedLabelKey)); const auto* maskedLabelPtr = dynamic_cast(parameters.at(maskedLabelKey).get()); REQUIRE(maskedLabelPtr != nullptr); - REQUIRE(maskedLabelPtr->humanName() == "Masked Label"); + REQUIRE(maskedLabelPtr->humanName() == "Excluded Label"); REQUIRE(std::any_cast(maskedLabelPtr->defaultValue()) == uint8{255}); const Arguments defaultArguments = filter.getDefaultArguments(); REQUIRE(defaultArguments.value(maskedLabelKey) == uint8{255}); + const bool masked = GENERATE(false, true); + const bool warn = GENERATE(false, true); + Arguments args = defaultArguments; + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(masked)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(warn ? 1 : 0)); + REQUIRE(parameters.isParameterActive(maskedLabelKey, args) == (masked || warn)); } // NOLINTEND(bugprone-chained-comparison) @@ -395,13 +401,42 @@ class OtsuTransferStore : public DataStore } // namespace // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains literal endpoint frequencies and labels", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn reserves a label only when exclusion is possible", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", + float64, int32) +{ + (void)Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 1, 4}); + const OtsuMultipleThresholdsImageFilter filter; + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(256)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(255)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(0)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + const auto preflight = filter.preflight(dataStructure, args); + if constexpr(std::is_floating_point_v) + { + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + REQUIRE(preflight.outputActions.errors().front().code == -8313); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + } +} + +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn fits finite-only thresholds and emits the saved excluded label", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize rotation = GENERATE(usize{0}, usize{1}, usize{4}); - std::vector values{0, 0, 4, 4, std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}; - std::vector expected{7, 7, 8, 8, 7, 7, 8}; + const bool useMask = GENERATE(false, true); + const float64 infinity = std::numeric_limits::infinity(); + std::vector values{0, 0, 1, 1, 2, infinity, infinity, infinity}; + // The finite histogram [2, 2, 1] splits after bin 0. Including the infinities would move the split to bin 1. + std::vector expected{7, 7, 8, 8, 8, 42, 42, 42}; std::ranges::rotate(values, values.begin() + static_cast::difference_type>(rotation)); std::ranges::rotate(expected, expected.begin() + static_cast::difference_type>(rotation)); DataStructure dataStructure; @@ -409,24 +444,36 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn retains li const Float64Array* inputPtr = nullptr; REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); const std::atomic_bool shouldCancel{false}; - const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; + const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 3, .LabelOffset = 7, .ExcludedLabel = 42, .WarnOnNonfinite = true}; const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); REQUIRE(histogramResult.value().HistogramData.min == 0); - REQUIRE(histogramResult.value().HistogramData.max == 4); - REQUIRE(histogramResult.value().HistogramData.counts == std::vector{4, 3}); - REQUIRE(histogramResult.value().NaNCount == 1); - REQUIRE(histogramResult.value().NegativeInfinityCount == 1); - REQUIRE(histogramResult.value().PositiveInfinityCount == 1); + REQUIRE(histogramResult.value().HistogramData.max == 2); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{2, 2, 1}); + REQUIRE(histogramResult.value().NaNCount == 0); + REQUIRE(histogramResult.value().NegativeInfinityCount == 0); + REQUIRE(histogramResult.value().PositiveInfinityCount == 3); auto args = otsuArguments(inputPath); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(42)); const OtsuMultipleThresholdsImageFilter filter; - const auto result = filter.execute(dataStructure, args); + if(useMask) + { + std::array inputMask{1, 1, 1, 1, 1, 1, 1, 1}; + const auto infinityIndex = std::distance(values.begin(), std::ranges::find(values, infinity)); + inputMask.at(static_cast(infinityIndex)) = 0; + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, inputMask))); + } + const auto restored = filter.fromJson(filter.toJson(args)); + SIMPLNX_RESULT_REQUIRE_VALID(restored); + const auto result = filter.execute(dataStructure, restored.value()); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == 1); REQUIRE(result.result.warnings().front().code == -8319); - REQUIRE(result.result.warnings().front().message.find("1 NaN, 1 negative infinity and 1 positive infinity") != std::string::npos); - REQUIRE(result.result.warnings().front().message.find("unreliable") != std::string::npos); + REQUIRE(result.result.warnings().front().message.find(useMask ? "skipped 2 nonfinite pixels still selected after applying the input mask" : "skipped 3 nonfinite pixels") != std::string::npos); + REQUIRE(result.result.warnings().front().message.find("Excluded Label for these pixels is 42") != std::string::npos); requireOtsuLabels(dataStructure, inputPath, expected); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -1148,7 +1195,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep explicit Warn endpoints and empty selected data errors", +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants exclude nonfinite pixels and empty selected data errors", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -1160,15 +1207,15 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants keep input.setValue(3, std::numeric_limits::infinity()); const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); - REQUIRE(histogramResult.value().HistogramData.counts == std::vector{3, 1}); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{1, 0}); DataStore output({4}, {1}, uint8{99}); const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(result.warnings().size() == 1); REQUIRE(output.getValue(0) == 7); - REQUIRE(output.getValue(1) == 7); - REQUIRE(output.getValue(2) == 7); - REQUIRE(output.getValue(3) == 8); + REQUIRE(output.getValue(1) == 255); + REQUIRE(output.getValue(2) == 255); + REQUIRE(output.getValue(3) == 255); input.setValue(0, std::numeric_limits::quiet_NaN()); const auto noFiniteResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(noFiniteResult); @@ -1272,7 +1319,8 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal c } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds keep ordinary Warn endpoint classes", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds preserve finite classes and exclude nonfinite pixels", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -1285,7 +1333,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 3, .NumberOfThresholds = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; const auto histogramResult = ImageProcessing::ComputeOtsuHistogram(inputPtr->getDataStoreRef(), static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_VALID(histogramResult); - REQUIRE(histogramResult.value().HistogramData.counts == std::vector{3, 1, 2}); + REQUIRE(histogramResult.value().HistogramData.counts == std::vector{1, 1, 1}); // Three bins and two thresholds have exactly one possible split: each finite intensity gets its own class. auto args = otsuArguments(inputPath); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); @@ -1295,7 +1343,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds const auto result = filter.execute(dataStructure, args); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == 1); - requireOtsuLabels(dataStructure, inputPath, {7, 8, 9, 7, 7, 9}); + requireOtsuLabels(dataStructure, inputPath, {7, 8, 9, 255, 255, 255}); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -1390,8 +1438,8 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summar REQUIRE(result.value().NaNCount == (excludeNonfinite ? 0 : 65537)); REQUIRE(result.value().NegativeInfinityCount == (excludeNonfinite ? 0 : 65537)); REQUIRE(result.value().PositiveInfinityCount == (excludeNonfinite ? 0 : 65537)); - // Per period, Warn contributes [0,NaN,-inf] to bin 0 and [4,+inf] to bin 1. Excluded values, including 99, contribute nothing. - const std::vector expected = excludeNonfinite ? std::vector{65537, 65537} : std::vector{196611, 131074}; + // Per period only 0 and 4 contribute to the histogram, regardless of selected nonfinite values. + const std::vector expected{65537, 65537}; REQUIRE(result.value().HistogramData.counts == expected); if(buffered) { @@ -1405,54 +1453,45 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summar // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident validity masked Otsu feeds selected components", "[NXImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels preserve class measurements", "[NXImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; - const bool includeNonfinite = GENERATE(false, true); + const bool useMask = GENERATE(false, true); const auto nan = std::numeric_limits::quiet_NaN(); const auto inf = std::numeric_limits::infinity(); - const std::vector original = includeNonfinite ? std::vector{0, 0, 4, 4, nan, -inf, inf} : std::vector{0, 0, 4, 4}; + const std::vector original{0, 4, inf, 4, nan, -inf, 0}; const ShapeType shape{1, 1, original.size()}; DataStructure dataStructure; const auto input = ip_test::BuildScalarValuesImage(dataStructure, original); - const auto validity = c15_test::Select(dataStructure, input, 0, 4, "Validity"); - const std::vector expectedValidity = includeNonfinite ? std::vector{1, 1, 1, 1, 0, 0, 0} : std::vector{1, 1, 1, 1}; - REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == expectedValidity); const OtsuMultipleThresholdsImageFilter filter; auto args = c15_test::ImageArguments(filter, input, "Classes"); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(2)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(1)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(7)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(255)); - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(validity)); - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(0)); - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ValleyEmphasis_Key, std::make_any(false)); - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_ReturnBinMidpoint_Key, std::make_any(false)); - c15_test::ExecuteChecked(filter, dataStructure, args); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NonfiniteInputHandling_Key, std::make_any(1)); + if(useMask) + { + const auto mask = createOtsuMask(dataStructure, std::array{1, 1, 1, 1, 0, 1, 1}); + REQUIRE(c15_test::Read(dataStructure, mask, ShapeType{7}, expectedStore) == std::vector{1, 1, 1, 1, 0, 1, 1}); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(mask)); + } + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE(result.result.warnings().size() == 1); + REQUIRE(result.result.warnings().front().message.find(useMask ? "skipped 2 nonfinite pixels still selected after applying the input mask" : "skipped 3 nonfinite pixels") != std::string::npos); const auto classes = input.replaceName("Classes"); - const std::vector expectedClasses = includeNonfinite ? std::vector{7, 7, 8, 8, 255, 255, 255} : std::vector{7, 7, 8, 8}; - REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == expectedClasses); + REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == std::vector{7, 8, 255, 8, 255, 255, 7}); const auto selected = c15_test::Select(dataStructure, classes, 8, 8, "DesiredClass"); - const std::vector expectedSelected = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; - REQUIRE(c15_test::Read(dataStructure, selected, shape, expectedStore) == expectedSelected); - const auto labels = c15_test::Components(dataStructure, selected, "Components"); - const std::vector expectedLabels = includeNonfinite ? std::vector{0, 0, 1, 1, 0, 0, 0} : std::vector{0, 0, 1, 1}; - const auto actualLabels = c15_test::Read(dataStructure, labels, shape, expectedStore); - REQUIRE(actualLabels == expectedLabels); - REQUIRE(std::count(actualLabels.begin(), actualLabels.end(), uint32{1}) == 2); - REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == expectedClasses); - REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == expectedValidity); - const auto values = c15_test::Read(dataStructure, input, shape, expectedStore); - REQUIRE(std::vector(values.begin(), values.begin() + 4) == std::vector{0, 0, 4, 4}); - if(includeNonfinite) - { - REQUIRE(std::isnan(values[4])); - REQUIRE(values[5] == -inf); - REQUIRE(values[6] == inf); - } + REQUIRE(c15_test::Read(dataStructure, selected, shape, expectedStore) == std::vector{0, 1, 0, 1, 0, 0, 0}); + const auto components = c15_test::Components(dataStructure, selected, "Components"); + REQUIRE(c15_test::Read(dataStructure, components, shape, expectedStore) == std::vector{0, 1, 0, 2, 0, 0, 0}); + const auto validity = c15_test::Select(dataStructure, classes, 7, 8, "Validity"); + REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == std::vector{1, 1, 0, 1, 0, 0, 1}); + REQUIRE(c15_test::MaskedMean(dataStructure, selected, validity) == 0.5F); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 090fe449ca..d818b0b288 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -37,7 +37,7 @@ struct OtsuImageOptions uint32 NumberOfBins = 128; uint8 NumberOfThresholds = 1; uint8 LabelOffset = 0; - uint8 MaskedLabel = 255; + uint8 ExcludedLabel = 255; bool WarnOnNonfinite = false; bool ValleyEmphasis = false; bool ReturnBinMidpoint = false; @@ -441,7 +441,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } if(!options.WarnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) { - return MakeErrorResult(-28654, "Otsu found a selected NaN or infinity. Choose Warn and Continue to retain endpoint-bin participation, or explicitly exclude it with Use Mask Array."); + return MakeErrorResult(-28654, "Otsu found a selected NaN or infinity. Choose Warn and Continue to skip nonfinite pixels and assign Excluded Label, or exclude them with Use Mask Array."); } if(summary.FiniteCount != 0) { @@ -603,8 +603,6 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp return ConvertInvalidResult(std::move(rangeResult)); } } - histogram.counts.front() += result.NaNCount + result.NegativeInfinityCount; - histogram.counts.back() += result.PositiveInfinityCount; return ConvertResultTo(std::move(rangeResult), std::move(result)); } @@ -662,7 +660,7 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O { if(!std::isfinite(value)) { - return std::isinf(value) && value > 0 ? static_cast(static_cast(options.LabelOffset) + options.NumberOfThresholds) : options.LabelOffset; + return options.ExcludedLabel; } } const usize index = static_cast(std::ranges::lower_bound(thresholds, static_cast(value)) - thresholds.begin()); @@ -670,7 +668,7 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O } /** - * @brief Fits an Otsu histogram and writes selected classes and optional masked labels. + * @brief Fits an Otsu histogram and writes selected classes and an excluded label. * @tparam T Numeric input value type. * @tparam MaskT Boolean or uint8 selection-mask type. * @param input Scalar source image. @@ -679,7 +677,7 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O * @param options Histogram, class-label, nonfinite, and transfer controls. * @param shouldCancel Requests cancellation during scans, search, and output work. * @param messageHandler Receives output-phase progress messages. - * @return Collected diagnostics, including the nonfinite participation warning, or a validation, numeric, or transfer error. + * @return Collected diagnostics, including the nonfinite exclusion warning, or a validation, numeric, or transfer error. * Cancellation retains the valid engine-result convention. * * Mask selection precedes histogram and nonfinite checks. The optional mask is read in bounded owned chunks. @@ -694,9 +692,10 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore(options.LabelOffset) + options.NumberOfThresholds > 255 || - (maskPtr != nullptr && options.MaskedLabel >= options.LabelOffset && static_cast(options.MaskedLabel) <= static_cast(options.LabelOffset) + options.NumberOfThresholds)) + ((maskPtr != nullptr || (std::is_floating_point_v && options.WarnOnNonfinite)) && options.ExcludedLabel >= options.LabelOffset && + static_cast(options.ExcludedLabel) <= static_cast(options.LabelOffset) + options.NumberOfThresholds)) { - return MakeErrorResult(-28603, "Otsu labels do not fit a distinct uint8 class interval. Reduce Label Offset/Number Of Thresholds or select a Masked Label outside that interval."); + return MakeErrorResult(-28603, "Otsu labels do not fit a distinct uint8 class interval. Reduce Label Offset/Number Of Thresholds or select an Excluded Label outside that interval."); } auto histogramResult = ComputeOtsuHistogram(input, maskPtr, options, shouldCancel); if(histogramResult.invalid()) @@ -710,12 +709,14 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore result; result.warnings() = std::move(histogramResult.warnings()); const auto& counts = histogramResult.value(); - if(counts.NaNCount != 0 || counts.NegativeInfinityCount != 0 || counts.PositiveInfinityCount != 0) + const usize excludedCount = counts.NaNCount + counts.NegativeInfinityCount + counts.PositiveInfinityCount; + if(excludedCount != 0) { - result.warnings().push_back(Warning{.code = -8319, - .message = fmt::format("Otsu retained {} NaN, {} negative infinity and {} positive infinity samples in the endpoint histogram bins. Classification can be " - "unreliable or unpredictable. Use an explicit mask to exclude these samples.", - counts.NaNCount, counts.NegativeInfinityCount, counts.PositiveInfinityCount)}); + const auto maskContext = maskPtr == nullptr ? "" : " still selected after applying the input mask"; + result.warnings().push_back( + Warning{.code = -8319, + .message = fmt::format("Otsu skipped {} nonfinite pixels{} when fitting thresholds ({} NaN, {} negative infinity and {} positive infinity). The Excluded Label for these pixels is {}.", + excludedCount, maskContext, counts.NaNCount, counts.NegativeInfinityCount, counts.PositiveInfinityCount, options.ExcludedLabel)}); } auto thresholdsResult = ComputeCheckedOtsuThresholds(histogramResult.value().HistogramData, options, shouldCancel); if(thresholdsResult.invalid()) @@ -780,7 +781,7 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore Date: Mon, 16 Nov 2026 18:31:59 -0500 Subject: [PATCH 52/98] REV: Report the first nonfinite threshold sample Report the selected sample's kind, array path, and flat tuple index. Derive the index during the existing data scans. Addresses review item 2. Signed-off-by: Jessica Marquis --- .../OtsuMultipleThresholdsImageFilter.cpp | 4 +- .../utils/ComponentImageFilterExecutors.cpp | 2 +- .../OtsuMultipleThresholdsImageFilterTest.cpp | 44 ++++++++++++++++++- ...imumConnectedComponentsImageFilterTest.cpp | 28 ++++++++++++ .../ImageProcessing/OtsuImageEngine.hpp | 39 +++++++++++----- ...esholdMaximumConnectedComponentsEngine.hpp | 31 ++++++++----- 6 files changed, 121 insertions(+), 27 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index d5fa1401ef..ab925532eb 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -189,10 +189,10 @@ Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStruc auto& output = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); if(maskPtr != nullptr && maskPtr->getDataType() == DataType::boolean) { - return ImageProcessing::ApplyOtsuImage(input, output, &maskPtr->getIDataStoreRefAs>(), options, shouldCancel, messageHandler); + return ImageProcessing::ApplyOtsuImage(input, output, &maskPtr->getIDataStoreRefAs>(), options, shouldCancel, messageHandler, selectedInputArray); } const auto* maskStorePtr = maskPtr == nullptr ? nullptr : &maskPtr->getIDataStoreRefAs>(); - return ImageProcessing::ApplyOtsuImage(input, output, maskStorePtr, options, shouldCancel, messageHandler); + return ImageProcessing::ApplyOtsuImage(input, output, maskStorePtr, options, shouldCancel, messageHandler, selectedInputArray); }); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp index 0dbc0afb4d..4bb93284cc 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp @@ -694,7 +694,7 @@ struct ThresholdMaxCCExecuteFn { const auto& input = dataStructure.getDataRefAs>(inputArrayPath).getDataStoreRef(); auto& output = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); - return ApplyThresholdMaximumConnectedComponents(input, output, dims, Options, shouldCancel, messageHandler); + return ApplyThresholdMaximumConnectedComponents(input, output, dims, Options, shouldCancel, messageHandler, inputArrayPath); } }; } // namespace diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 2edc72cc6a..543092cc12 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -34,6 +34,7 @@ #include "simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp" #include +#include #include #include #include @@ -478,6 +479,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn fits finit } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { UnitTest::LoadPlugins(); @@ -487,12 +489,50 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects e const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, value, 4}); const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, otsuArguments(inputPath)); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); - // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == -28654); + const auto& message = result.result.errors().front().message; + const auto* kind = std::isnan(value) ? "NaN" : (value < 0 ? "negative infinity" : "positive infinity"); + REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); + REQUIRE(message.find(fmt::format("{} at zero-based flat tuple index 1", kind)) != std::string::npos); + REQUIRE(message.find("skip nonfinite pixels and assign Excluded Label") != std::string::npos); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error finds the first selected nonfinite tuple across scan blocks", + "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + (void)Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize firstSelectedIndex = 262145; + const auto infinity = std::numeric_limits::infinity(); + std::vector values(firstSelectedIndex + 2, 4.0); + values[0] = std::numeric_limits::quiet_NaN(); + values[firstSelectedIndex] = -infinity; + values[firstSelectedIndex + 1] = infinity; + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); + const DataPath maskPath({"Mask"}); + auto maskStore = DataStoreUtilities::CreateDataStore(dataStructure, maskPath, {values.size()}, {1}); + REQUIRE(maskStore != nullptr); + std::vector mask(values.size(), uint8{1}); + mask[0] = 0; + const auto maskWrite = maskStore->copyFromBuffer(0, nonstd::span(mask.data(), mask.size())); + SIMPLNX_RESULT_REQUIRE_VALID(maskWrite); + REQUIRE(UInt8Array::Create(dataStructure, maskPath.getTargetName(), maskStore) != nullptr); + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + const OtsuMultipleThresholdsImageFilter filter; + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -28654); + const auto& message = result.result.errors().front().message; + REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); + REQUIRE(message.find("negative infinity at zero-based flat tuple index 262145") != std::string::npos); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index c64fd5e895..ed0fec5b52 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -9,6 +9,7 @@ #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -28,6 +29,7 @@ #include "simplnx/Utilities/MD5.hpp" #include +#include #include #include @@ -436,6 +438,10 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: on { SIMPLNX_RESULT_REQUIRE_INVALID(result.result); REQUIRE(result.result.errors().front().code == -28611); + const auto& message = result.result.errors().front().message; + REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); + REQUIRE(message.find(fmt::format("NaN at zero-based flat tuple index {}", placement)) != std::string::npos); + REQUIRE(message.find("assign Outside Value") != std::string::npos); UnitTest::CheckArraysInheritTupleDims(dataStructure); } else @@ -450,6 +456,28 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: on // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: Error finds the first nonfinite tuple across scan blocks", + "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +{ + (void)Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr usize firstNonfiniteIndex = 65537; + const auto infinity = std::numeric_limits::infinity(); + std::vector values(firstNonfiniteIndex + 2, 4.0); + values[firstNonfiniteIndex] = infinity; + values[firstNonfiniteIndex + 1] = -infinity; + DataStructure dataStructure; + const auto inputPath = rt::BuildImageFromPattern(dataStructure, values.size(), 1, 1, values); + const ThresholdMaximumConnectedComponentsImageFilter filter; + const auto result = filter.execute(dataStructure, thresholdArguments(inputPath)); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -28611); + const auto& message = result.result.errors().front().message; + REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); + REQUIRE(message.find("positive infinity at zero-based flat tuple index 65537") != std::string::npos); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: all nonfinite Warn skips search and calculates no threshold", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index d818b0b288..2894252fbf 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -4,6 +4,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" +#include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/DataStructure/IDataStore.hpp" #include "simplnx/Filter/IFilter.hpp" @@ -72,10 +73,11 @@ struct OtsuFloatingRangeSummary usize NaNCount = 0; usize NegativeInfinityCount = 0; usize PositiveInfinityCount = 0; + usize FirstNonfiniteIndex = std::numeric_limits::max(); // Index within the current batch. }; template -OtsuFloatingRangeSummary ReduceOtsuFloatingBlock(nonstd::span values, nonstd::span maskValues, const std::atomic_bool& shouldCancel) +OtsuFloatingRangeSummary ReduceOtsuFloatingBlock(nonstd::span values, nonstd::span maskValues, const std::atomic_bool& shouldCancel, usize blockStart) { OtsuFloatingRangeSummary summary; for(usize index = 0; index < values.size(); ++index) @@ -91,6 +93,10 @@ OtsuFloatingRangeSummary ReduceOtsuFloatingBlock(nonstd::span values const T value = values[index]; if(!std::isfinite(value)) { + if(summary.FirstNonfiniteIndex == std::numeric_limits::max()) + { + summary.FirstNonfiniteIndex = blockStart + index; + } if(std::isnan(value)) { ++summary.NaNCount; @@ -140,6 +146,7 @@ void MergeOtsuFloatingSummary(OtsuFloatingRangeSummary& summary, const OtsuFl summary.NaNCount += block.NaNCount; summary.NegativeInfinityCount += block.NegativeInfinityCount; summary.PositiveInfinityCount += block.PositiveInfinityCount; + summary.FirstNonfiniteIndex = std::min(summary.FirstNonfiniteIndex, block.FirstNonfiniteIndex); } template @@ -147,7 +154,7 @@ OtsuFloatingRangeSummary ReduceOtsuFloatingRange(nonstd::span values { if(values.size() <= k_OtsuRangeBlockValues) { - return ReduceOtsuFloatingBlock(values, maskValues, shouldCancel); + return ReduceOtsuFloatingBlock(values, maskValues, shouldCancel, 0); } // The caller caps each resident view/owned batch. This table never has one entry per full-image block. const usize blockCount = 1 + ((values.size() - 1) / k_OtsuRangeBlockValues); @@ -163,7 +170,7 @@ OtsuFloatingRangeSummary ReduceOtsuFloatingRange(nonstd::span values } const usize start = block * k_OtsuRangeBlockValues; const usize count = std::min(k_OtsuRangeBlockValues, values.size() - start); - summaries[block] = ReduceOtsuFloatingBlock(values.subspan(start, count), maskValues.empty() ? nonstd::span{} : maskValues.subspan(start, count), shouldCancel); + summaries[block] = ReduceOtsuFloatingBlock(values.subspan(start, count), maskValues.empty() ? nonstd::span{} : maskValues.subspan(start, count), shouldCancel, start); } }); OtsuFloatingRangeSummary summary; @@ -239,7 +246,7 @@ Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataSt return status; } const usize count = std::min(blockValues, total - start); - status = MergeResults(std::move(status), visitor(values.subspan(start, count), maskPtr == nullptr ? nonstd::span{} : maskValues.subspan(start, count))); + status = MergeResults(std::move(status), visitor(values.subspan(start, count), maskPtr == nullptr ? nonstd::span{} : maskValues.subspan(start, count), start)); if(status.invalid()) { return status; @@ -284,7 +291,7 @@ Result<> VisitOtsuValues(const AbstractDataStore& input, const AbstractDataSt return status; } status = MergeResults(std::move(status), - visitor(nonstd::span(valuesPtr.get(), count), maskPtr == nullptr ? nonstd::span{} : nonstd::span(maskValuesPtr.get(), count))); + visitor(nonstd::span(valuesPtr.get(), count), maskPtr == nullptr ? nonstd::span{} : nonstd::span(maskValuesPtr.get(), count), start)); if(status.invalid()) { return status; @@ -377,6 +384,7 @@ Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T * @param maskPtr Optional scalar mask with the input tuple count, paired in flat order. Null selects every input value. * @param options Histogram-bin count, nonfinite policy, and transfer-memory target. * @param shouldCancel Requests cancellation during scans and histogram construction. + * @param inputArrayPath Identifies the source array in diagnostics when called from a filter. * @return Histogram and selected finite and nonfinite sample counts, or an input, numeric, or transfer error. * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. * @@ -384,7 +392,8 @@ Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T * The ring uses capacity left after the input and mask buffers. A scan without usable ring frequencies uses a second bounded pass for nonconstant input. */ template -Result ComputeOtsuHistogram(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel) +Result ComputeOtsuHistogram(const AbstractDataStore& input, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, const std::atomic_bool& shouldCancel, + const DataPath& inputArrayPath = {}) { if(options.NumberOfBins < 2) { @@ -431,7 +440,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp } Result<> rangeResult = detail::VisitOtsuValues( input, maskPtr, options, shouldCancel, plan.Values, - [&](nonstd::span values, nonstd::span maskValues) -> Result<> { + [&](nonstd::span values, nonstd::span maskValues, usize batchStart) -> Result<> { if constexpr(std::is_floating_point_v) { const auto summary = detail::ReduceOtsuFloatingRange(values, maskValues, shouldCancel); @@ -439,9 +448,14 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { return {}; } - if(!options.WarnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) + if(!options.WarnOnNonfinite && summary.FirstNonfiniteIndex != std::numeric_limits::max()) { - return MakeErrorResult(-28654, "Otsu found a selected NaN or infinity. Choose Warn and Continue to skip nonfinite pixels and assign Excluded Label, or exclude them with Use Mask Array."); + const T value = values[summary.FirstNonfiniteIndex]; + const auto* kind = std::isnan(value) ? "NaN" : (std::signbit(value) ? "negative infinity" : "positive infinity"); + const auto inputName = inputArrayPath.empty() ? "the input array" : fmt::format("input array '{}'", inputArrayPath.toString()); + return MakeErrorResult(-28654, fmt::format("Otsu found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to skip nonfinite pixels and assign Excluded Label, or " + "select a mask that excludes these pixels.", + kind, batchStart + summary.FirstNonfiniteIndex, inputName)); } if(summary.FiniteCount != 0) { @@ -578,7 +592,7 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp // The floating-point histogram pass needs no range summaries. plan = detail::ReserveOtsuBuffers(options, {.TotalValues = total, .BytesPerValue = bytesPerValue, .UsesExternalStore = external}); } - Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, plan.Values, [&](nonstd::span values, nonstd::span maskValues) -> Result<> { + Result<> histogramResult = detail::VisitOtsuValues(input, maskPtr, options, shouldCancel, plan.Values, [&](nonstd::span values, nonstd::span maskValues, usize) -> Result<> { for(usize i = 0; i < values.size(); ++i) { if((i % 65536) == 0 && shouldCancel) @@ -677,6 +691,7 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O * @param options Histogram, class-label, nonfinite, and transfer controls. * @param shouldCancel Requests cancellation during scans, search, and output work. * @param messageHandler Receives output-phase progress messages. + * @param inputArrayPath Identifies the source array in diagnostics when called from a filter. * @return Collected diagnostics, including the nonfinite exclusion warning, or a validation, numeric, or transfer error. * Cancellation retains the valid engine-result convention. * @@ -685,7 +700,7 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O */ template Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore& output, const AbstractDataStore* maskPtr, const OtsuImageOptions& options, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler) + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler, const DataPath& inputArrayPath = {}) { if(input.getSize() != output.getSize() || output.getNumberOfComponents() != 1) { @@ -697,7 +712,7 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore::max(); // Index within the current batch. }; template @@ -78,10 +80,11 @@ void MergeThresholdMaxCCSummary(ThresholdMaxCCSummary& summary, const Thresho summary.NaNCount += block.NaNCount; summary.NegativeInfinityCount += block.NegativeInfinityCount; summary.PositiveInfinityCount += block.PositiveInfinityCount; + summary.FirstNonfiniteIndex = std::min(summary.FirstNonfiniteIndex, block.FirstNonfiniteIndex); } template -ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, bool warnOnNonfinite, const std::atomic_bool& shouldCancel) +ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, bool warnOnNonfinite, const std::atomic_bool& shouldCancel, usize blockStart) { ThresholdMaxCCSummary summary; for(usize index = 0; index < values.size(); ++index) @@ -109,6 +112,7 @@ ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, } if(!warnOnNonfinite) { + summary.FirstNonfiniteIndex = blockStart + index; break; } continue; @@ -135,11 +139,12 @@ ThresholdMaxCCSummary ReduceThresholdMaxCCBlock(nonstd::span values, * @param input Source values to scan. * @param warnOnNonfinite If true, counts nonfinite samples and continues. If false, a nonfinite sample causes an error. * @param shouldCancel Requests cancellation during the scan. + * @param inputArrayPath Identifies the source array in diagnostics when called from a filter. * @return Native finite bounds and sample counts, or a transfer or allocation error, or an error for disallowed nonfinite samples. * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. */ template -Result> ScanThresholdMaxCC(const AbstractDataStore& input, bool warnOnNonfinite, const std::atomic_bool& shouldCancel) +Result> ScanThresholdMaxCC(const AbstractDataStore& input, bool warnOnNonfinite, const std::atomic_bool& shouldCancel, const DataPath& inputArrayPath = {}) { ThresholdMaxCCSummary summary; Result<> status; @@ -209,7 +214,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& const nonstd::span values = direct ? residentPtr->createSpan().subspan(start, count) : nonstd::span(valuesPtr.get(), count); if(blockCapacity == 0) { - MergeThresholdMaxCCSummary(summary, ReduceThresholdMaxCCBlock(values, warnOnNonfinite, shouldCancel)); + MergeThresholdMaxCCSummary(summary, ReduceThresholdMaxCCBlock(values, warnOnNonfinite, shouldCancel, 0)); } else { @@ -220,7 +225,7 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& for(usize block = range.min(); block < range.max(); ++block) { const usize offset = block * blockValues; - blocks[block] = ReduceThresholdMaxCCBlock(values.subspan(offset, std::min(blockValues, count - offset)), warnOnNonfinite, shouldCancel); + blocks[block] = ReduceThresholdMaxCCBlock(values.subspan(offset, std::min(blockValues, count - offset)), warnOnNonfinite, shouldCancel, offset); } }); for(usize block = 0; block < blockCount; ++block) @@ -228,11 +233,16 @@ Result> ScanThresholdMaxCC(const AbstractDataStore& MergeThresholdMaxCCSummary(summary, blocks[block]); } } - if(!warnOnNonfinite && (summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0)) + if(summary.FirstNonfiniteIndex != std::numeric_limits::max()) { - return ConvertResultTo(MergeResults(std::move(status), MakeErrorResult(-28611, "Threshold Maximum Connected Components found image NaN or infinity during the initial scan. Select Warn and " - "Continue under Nonfinite Input Handling only if Outside Value participation is intended.")), - std::move(summary)); + const T value = values[summary.FirstNonfiniteIndex]; + const auto* kind = std::isnan(value) ? "NaN" : (std::signbit(value) ? "negative infinity" : "positive infinity"); + const auto inputName = inputArrayPath.empty() ? "the input array" : fmt::format("input array '{}'", inputArrayPath.toString()); + return ConvertInvalidResult>( + MergeResults(std::move(status), + MakeErrorResult(-28611, fmt::format("Threshold Maximum Connected Components found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to assign Outside " + "Value to nonfinite pixels.", + kind, start + summary.FirstNonfiniteIndex, inputName)))); } start += count; } @@ -455,6 +465,7 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor * @param options Component-size, cutoff, output-value, nonfinite, and staging controls. * @param shouldCancel Requests cancellation during scan, search, and output work. * @param messageHandler Receives output-phase progress messages. + * @param inputArrayPath Identifies the source array in diagnostics when called from a filter. * @return Collected warnings, or a parameter, shape, numeric-capacity, allocation, or transfer error. * Cancellation retains the valid engine-result convention. * @@ -465,7 +476,7 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor */ template Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& input, AbstractDataStore& output, const SizeVec3& dims, const ThresholdMaxCCOptions& options, - const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler = {}) + const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler = {}, const DataPath& inputArrayPath = {}) { Result<> status; if(std::isnan(options.UpperBoundary)) @@ -484,7 +495,7 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in } try { - auto summaryResult = detail::ScanThresholdMaxCC(input, options.WarnOnNonfinite, shouldCancel); + auto summaryResult = detail::ScanThresholdMaxCC(input, options.WarnOnNonfinite, shouldCancel, inputArrayPath); if(summaryResult.invalid()) { return ConvertResult(std::move(summaryResult)); From c3c9eeabb6dddc3edaefe57c178dc15e9c58abad Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 53/98] REV: Name nonfinite input handling choices Use shared enum values at the filter boundary and in the writer. Preserve the saved choice values and default. Addresses review item 4. Signed-off-by: Jessica Marquis --- .../Filters/Algorithms/WriteImage.hpp | 3 ++- .../Algorithms/WriteImageColorUtilities.hpp | 8 +++++--- .../Filters/Algorithms/WriteImageDirect.cpp | 4 ++-- .../Filters/Algorithms/WriteImageScanline.cpp | 7 ++++--- .../Filters/OtsuMultipleThresholdsImageFilter.cpp | 14 ++++++++------ ...esholdMaximumConnectedComponentsImageFilter.cpp | 8 ++++---- .../NXImageProcessing/Filters/WriteImageFilter.cpp | 6 ++++-- .../ImageProcessing/ImageProcessingConstants.hpp | 7 +++++++ 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp index 752c803fa1..2b5589f5b5 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp @@ -6,6 +6,7 @@ #include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Utilities/ImageIO/ImageIOEnums.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include #include @@ -28,7 +29,7 @@ struct NXIMAGEPROCESSING_EXPORT WriteImageInputValues DataPath imageGeometryPath; DataPath imageDataArrayPath; bool createColorTable = false; - uint64 NonfiniteInputHandling = 0; + ImageProcessing::NonfiniteInputHandling NonfiniteInputHandling = ImageProcessing::NonfiniteInputHandling::Error; std::string presetName; bool useMask = false; DataPath maskArrayPath; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp index 005896e1db..9890920327 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageColorUtilities.hpp @@ -4,6 +4,7 @@ #include "simplnx/Common/Types.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/IDataArray.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include #include @@ -88,13 +89,14 @@ struct Range * @tparam T Scalar input type. * @param array Single-component source array. * @param maskArray Optional single-component bool or uint8 mask. - * @param policy Error (0) or Warn and Continue (1). + * @param policy Action for unmasked nonfinite values. * @param shouldCancel Stops the scan between pages. * @param reportRange Synchronous callback after each complete page. * @return Native range and warnings, cancellation state, or the first scan error. */ template -Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, uint64 policy, const std::atomic_bool& shouldCancel, const std::function& reportRange) +Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, ImageProcessing::NonfiniteInputHandling policy, const std::atomic_bool& shouldCancel, + const std::function& reportRange) { const auto& store = array.getIDataStoreRefAs>(); const usize count = store.getNumberOfTuples(); @@ -168,7 +170,7 @@ Result> ScanRange(const IDataArray& array, const IDataArray* maskArray, const bool selected = boolMask != nullptr ? boolMask[index] : (byteMask == nullptr || (*byteMask)[index] != 0); if(selected) { - if(policy == 0) + if(policy == ImageProcessing::NonfiniteInputHandling::Error) { return ConvertResultTo>( MergeResults(std::move(status), MakeErrorResult(-27027, fmt::format("Write Image array '{}' has an unmasked nonfinite value at tuple {}. Repair or mask this value, or select 'Warn " diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 0133afa201..6ee99ab982 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -201,8 +201,8 @@ struct ColorizeVolumeFunctor // The operation calculates the range in pages and then colorizes bounded slices. template Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const write_image_color::SliceExtents& sliceExtents, const std::vector& binPoints, - const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, const std::atomic_bool& shouldCancel, - const std::function(std::vector&, usize)>& writeSlice, const write_image_color::ProgressCallbacks& progress) + const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, ImageProcessing::NonfiniteInputHandling nonfinitePolicy, + const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const write_image_color::ProgressCallbacks& progress) { const auto& [sliceW, sliceH, sliceCount] = sliceExtents; if(Result<> sizeResult = write_image_color::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index da70c29af5..fdb7093e99 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -379,7 +379,7 @@ struct ColorizeVolumeFunctor * @param controlPoints Color-control values. * @param maskArray Optional Boolean or uint8 mask array. * @param invalidColor RGB value for masked pixels. - * @param nonfinitePolicy Error (0) or Warn and Continue (1) for unmasked nonfinite input. + * @param nonfinitePolicy Action for unmasked nonfinite input. * @param shouldCancel Stops processing between output slices. * @param writeSlice Writes one packed RGB output slice. * @param progress Synchronous callbacks for range-scan values and output rows. @@ -387,8 +387,9 @@ struct ColorizeVolumeFunctor */ template Result<> operator()(const IDataArray& dataArrayRef, usize planeIndex, usize dimX, usize dimY, usize dimZ, const write_image_color::SliceExtents& sliceExtents, const DataPath& dataArrayPath, - const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, uint64 nonfinitePolicy, - const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, const write_image_color::ProgressCallbacks& progress) + const std::vector& binPoints, const std::vector& controlPoints, const IDataArray* maskArray, const std::vector& invalidColor, + ImageProcessing::NonfiniteInputHandling nonfinitePolicy, const std::atomic_bool& shouldCancel, const std::function(std::vector&, usize)>& writeSlice, + const write_image_color::ProgressCallbacks& progress) { const auto& [sliceW, sliceH, sliceCount] = sliceExtents; if(Result<> sizeResult = write_image_color::ValidateSize(sliceExtents, maskArray != nullptr); sizeResult.invalid()) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index ab925532eb..374100e72e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -67,9 +67,9 @@ Parameters OtsuMultipleThresholdsImageFilter::parameters() const params.insert(std::make_unique(k_ValleyEmphasis_Key, "Valley Emphasis", "Set/Get the use of valley emphasis. Default is false.", false)); params.insert( std::make_unique(k_ReturnBinMidpoint_Key, "ReturnBinMidpoint", "Should the threshold value be mid-point of the bin or the maximum? Default is to return bin maximum.", false)); - params.insertLinkableParameter(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", - "Error stops on selected NaN or infinity. Warn and Continue skips these pixels when fitting thresholds and assigns Excluded Label.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); + params.insertLinkableParameter(std::make_unique( + k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", "Error stops on selected NaN or infinity. Warn and Continue skips these pixels when fitting thresholds and assigns Excluded Label.", + static_cast(ImageProcessing::NonfiniteInputHandling::Error), ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertLinkableParameter( std::make_unique(k_UseMaskArray_Key, "Use Mask Array", "Explicitly exclude pixels with a zero/false mask value before range and nonfinite checks.", false)); params.insert(std::make_unique(k_MaskArrayPath_Key, "Mask Array", "Select a scalar bool or uint8 mask with one value per input tuple. Nonzero values select pixels.", @@ -78,7 +78,7 @@ Parameters OtsuMultipleThresholdsImageFilter::parameters() const "Label for pixels excluded by the input mask or skipped by Warn and Continue. Choose a value outside the ordinary class interval.", uint8{255})); params.linkParameters(k_UseMaskArray_Key, k_MaskArrayPath_Key, true); params.linkParameters(k_UseMaskArray_Key, k_MaskedLabel_Key, true); - params.linkParameters(k_NonfiniteInputHandling_Key, k_MaskedLabel_Key, ChoicesParameter::ValueType{1}); + params.linkParameters(k_NonfiniteInputHandling_Key, k_MaskedLabel_Key, static_cast(ImageProcessing::NonfiniteInputHandling::WarnAndContinue)); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -128,7 +128,8 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const const bool useMask = filterArgs.value(k_UseMaskArray_Key); const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); const auto inputType = inputArray.getDataType(); - const bool warnOnNonfinite = filterArgs.value(k_NonfiniteInputHandling_Key) == 1; + const bool warnOnNonfinite = + static_cast(filterArgs.value(k_NonfiniteInputHandling_Key)) == ImageProcessing::NonfiniteInputHandling::WarnAndContinue; if(useMask || (warnOnNonfinite && (inputType == DataType::float32 || inputType == DataType::float64))) { const uint32 maskedLabel = filterArgs.value(k_MaskedLabel_Key); @@ -179,7 +180,8 @@ Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStruc .NumberOfThresholds = filterArgs.value(k_NumberOfThresholds_Key), .LabelOffset = filterArgs.value(k_LabelOffset_Key), .ExcludedLabel = filterArgs.value(k_MaskedLabel_Key), - .WarnOnNonfinite = filterArgs.value(k_NonfiniteInputHandling_Key) == 1, + .WarnOnNonfinite = static_cast(filterArgs.value( + k_NonfiniteInputHandling_Key)) == ImageProcessing::NonfiniteInputHandling::WarnAndContinue, .ValleyEmphasis = filterArgs.value(k_ValleyEmphasis_Key), .ReturnBinMidpoint = filterArgs.value(k_ReturnBinMidpoint_Key)}; const IDataArray* maskPtr = filterArgs.value(k_UseMaskArray_Key) ? &dataStructure.getDataRefAs(filterArgs.value(k_MaskArrayPath_Key)) : nullptr; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp index ecd76a7ec2..9906f5fcf2 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp @@ -69,7 +69,7 @@ Parameters ThresholdMaximumConnectedComponentsImageFilter::parameters() const params.insert(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", "Error stops on image NaN or either infinity before search/output. Warn and Continue uses finite bounds and assigns nonfinite pixels Outside Value. " "With no finite pixels, it skips search, fills Outside Value and warns that no threshold was calculated.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); + static_cast(ImageProcessing::NonfiniteInputHandling::Error), ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), @@ -126,8 +126,8 @@ Result<> ThresholdMaximumConnectedComponentsImageFilter::executeImpl(DataStructu auto insideValue = filterArgs.value(k_InsideValue_Key); auto outsideValue = filterArgs.value(k_OutsideValue_Key); - return ImageProcessing::ExecuteThresholdMaximumConnectedComponentsImageFilter(dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, minimumObjectSizeInPixels, upperBoundary, - insideValue, outsideValue, shouldCancel, messageHandler, - filterArgs.value(k_NonfiniteInputHandling_Key) == 1); + return ImageProcessing::ExecuteThresholdMaximumConnectedComponentsImageFilter( + dataStructure, imageGeomPath, selectedInputArray, outputArrayPath, minimumObjectSizeInPixels, upperBoundary, insideValue, outsideValue, shouldCancel, messageHandler, + static_cast(filterArgs.value(k_NonfiniteInputHandling_Key)) == ImageProcessing::NonfiniteInputHandling::WarnAndContinue); } } // namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp index 8bce53b253..adf0131b6a 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp @@ -117,7 +117,7 @@ Parameters WriteImageFilter::parameters() const params.insert(std::make_unique(k_NonfiniteInputHandling_Key, "Nonfinite Input Handling", "Error stops before any slice is written if an unmasked value is NaN or infinity. Warn and Continue uses the first control color for those values " "and reports their count. Masked finite values still participate in the finite color range.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue"})); + static_cast(ImageProcessing::NonfiniteInputHandling::Error), ChoicesParameter::Choices{"Error", "Warn and Continue"})); params.insertSeparator(Parameters::Separator{"Scale Bar (Optional)"}); params.insert(std::make_unique(k_AddScaleBar_Key, "Add Physical Scale Bar", @@ -358,7 +358,9 @@ Result<> WriteImageFilter::executeImpl(DataStructure& dataStructure, const Argum inputValues.imageGeometryPath = filterArgs.value(k_ImageGeomPath_Key); inputValues.imageDataArrayPath = filterArgs.value(k_ImageArrayPath_Key); inputValues.createColorTable = filterArgs.value(k_CreateColorTable_Key); - inputValues.NonfiniteInputHandling = inputValues.createColorTable ? filterArgs.value(k_NonfiniteInputHandling_Key) : 0; + inputValues.NonfiniteInputHandling = inputValues.createColorTable ? + static_cast(filterArgs.value(k_NonfiniteInputHandling_Key)) : + ImageProcessing::NonfiniteInputHandling::Error; inputValues.presetName = filterArgs.value(k_SelectedPreset_Key); inputValues.useMask = filterArgs.value(k_UseMask_Key); inputValues.maskArrayPath = filterArgs.value(k_MaskArrayPath_Key); diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp index aa4de3abc2..9cb63052bc 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp @@ -10,6 +10,13 @@ namespace nx::core::ImageProcessing { +/** @brief Saved choices for handling nonfinite input values. */ +enum class NonfiniteInputHandling : uint64 +{ + Error = 0, + WarnAndContinue = 1 +}; + // Error codes (offset chosen to avoid collision with core/ITK codes). inline constexpr int32 k_UnsupportedDataType = -8000; inline constexpr int32 k_GeometryMismatch = -8001; From 96e1d06126a5c2f3f0dcc5c10920aba759b40c33 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 54/98] REV: Keep Otsu and cutoff validation in preflight Keep parameter validation in preflight. Preserve runtime buffer and transfer checks. Addresses review item 5. Signed-off-by: Jessica Marquis --- .../OtsuMultipleThresholdsImageFilter.cpp | 23 +++++++++++++------ ...dMaximumConnectedComponentsImageFilter.cpp | 7 +++++- .../OtsuMultipleThresholdsImageFilterTest.cpp | 21 +++++------------ ...imumConnectedComponentsImageFilterTest.cpp | 13 +++++++---- .../ImageProcessing/OtsuImageEngine.hpp | 14 +++++------ ...esholdMaximumConnectedComponentsEngine.hpp | 8 +++---- 6 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 374100e72e..0d67eda94c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -37,6 +37,13 @@ using namespace nx::core; namespace nx::core { +namespace +{ +constexpr int32 k_OtsuClassIntervalOverflow = -8312; +constexpr int32 k_OtsuExcludedLabelCollision = -8313; +constexpr int32 k_OtsuMaskTupleMismatch = -8314; +} // namespace + std::string OtsuMultipleThresholdsImageFilter::name() const { return FilterTraits::name; @@ -121,9 +128,9 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const const uint32 lastLabel = labelOffset + static_cast(numberOfThresholds); if(lastLabel > 255) { - return {.outputActions = - MakeErrorResult(-8312, fmt::format("Label Offset ({}) plus Number Of Thresholds ({}) exceeds 255. Reduce either value to keep every class label within uint8.", - labelOffset, static_cast(numberOfThresholds)))}; + return {.outputActions = MakeErrorResult(k_OtsuClassIntervalOverflow, + fmt::format("Label Offset ({}) plus Number Of Thresholds ({}) exceeds 255. Reduce either value to keep every class label within uint8.", + labelOffset, static_cast(numberOfThresholds)))}; } const bool useMask = filterArgs.value(k_UseMaskArray_Key); const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); @@ -136,8 +143,9 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const if(maskedLabel >= labelOffset && maskedLabel <= lastLabel) { return {.outputActions = MakeErrorResult( - -8313, fmt::format("Excluded Label ({}) collides with the configured class interval [{}, {}]. Select an Excluded Label outside this interval or reduce the class interval.", - maskedLabel, labelOffset, lastLabel))}; + k_OtsuExcludedLabelCollision, + fmt::format("Excluded Label ({}) collides with the configured class interval [{}, {}]. Select an Excluded Label outside this interval or reduce the class interval.", maskedLabel, + labelOffset, lastLabel))}; } } if(useMask) @@ -153,8 +161,9 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const } if(maskPtr->getNumberOfTuples() != inputArray.getNumberOfTuples()) { - return {.outputActions = MakeErrorResult(-8314, fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", - maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), inputArray.getNumberOfTuples()))}; + return {.outputActions = MakeErrorResult(k_OtsuMaskTupleMismatch, + fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", + maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), inputArray.getNumberOfTuples()))}; } } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp index 9906f5fcf2..b0348d71a6 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ThresholdMaximumConnectedComponentsImageFilter.cpp @@ -26,6 +26,11 @@ using namespace nx::core; namespace nx::core { +namespace +{ +constexpr int32 k_ThresholdMaxCCNaNUpperBoundary = -28610; +} // namespace + std::string ThresholdMaximumConnectedComponentsImageFilter::name() const { return FilterTraits::name; @@ -98,7 +103,7 @@ IFilter::PreflightResult ThresholdMaximumConnectedComponentsImageFilter::preflig if(std::isnan(filterArgs.value(k_UpperBoundary_Key))) { return {.outputActions = MakeErrorResult( - -28610, "Upper Boundary is NaN. Enter a valid numeric cutoff; positive infinity selects the native maximum and negative infinity requests an empty band.")}; + k_ThresholdMaxCCNaNUpperBoundary, "Upper Boundary is NaN. Enter a valid numeric cutoff; positive infinity selects the native maximum and negative infinity requests an empty band.")}; } auto imageGeomPath = filterArgs.value(k_InputImageGeomPath_Key); auto selectedInputArray = filterArgs.value(k_InputImageDataPath_Key); diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 543092cc12..9e5669a8c9 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -260,6 +260,9 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: preflight check REQUIRE(preflightResult.outputActions.invalid()); REQUIRE_FALSE(preflightResult.outputActions.errors().empty()); REQUIRE(preflightResult.outputActions.errors().front().code == -8312); + const auto execution = filter.execute(dataStructure, arguments); + SIMPLNX_RESULT_REQUIRE_INVALID(execution.result); + REQUIRE(execution.result.errors().front().code == -8312); } UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -634,6 +637,9 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.outputActions.errors().front().code == expectedCode); + const auto execution = filter.execute(dataStructure, args); + REQUIRE(execution.result.invalid()); + REQUIRE(execution.result.errors().front().code == expectedCode); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -1282,13 +1288,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().front().code == -28600); } - SECTION("mask size") - { - DataStore mask({1}, {1}, uint8{1}); - const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(&mask), options, shouldCancel); - SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -8329); - } SECTION("output size") { DataStore output({1}, {1}, uint8{0}); @@ -1296,14 +1295,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().front().code == -28602); } - SECTION("label interval") - { - options.LabelOffset = 255; - DataStore output({2}, {1}, uint8{0}); - const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); - SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28603); - } SECTION("threshold count") { options.NumberOfThresholds = 0; diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index ed0fec5b52..a742ca714b 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -407,10 +407,14 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: Na DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {100, 102}); const ThresholdMaximumConnectedComponentsImageFilter filter; - const auto result = filter.preflight(dataStructure, thresholdArguments(inputPath, {.UpperBoundary = std::numeric_limits::quiet_NaN(), .WarnOnNonfinite = warn})); + const auto args = thresholdArguments(inputPath, {.UpperBoundary = std::numeric_limits::quiet_NaN(), .WarnOnNonfinite = warn}); + const auto result = filter.preflight(dataStructure, args); SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); REQUIRE(result.outputActions.errors().front().code == -28610); REQUIRE(result.outputActions.errors().front().message.find("Upper Boundary") != std::string::npos); + const auto execution = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(execution.result); + REQUIRE(execution.result.errors().front().code == -28610); REQUIRE(dataStructure.getDataAs(inputPath.replaceName("Output")) == nullptr); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -841,18 +845,17 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: en // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: direct engine validates hard parameters and output shape before transfer", +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: direct engine validates output shape before transfer", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { UnitTest::LoadPlugins(); - const bool nanParameter = GENERATE(false, true); const ThresholdTransferStore input({std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()}); ThresholdTransferStore output({99}); const std::atomic_bool shouldCancel{false}; - const ImageProcessing::ThresholdMaxCCOptions options{.UpperBoundary = nanParameter ? std::numeric_limits::quiet_NaN() : 65536, .WarnOnNonfinite = true}; + const ImageProcessing::ThresholdMaxCCOptions options{.UpperBoundary = 65536, .WarnOnNonfinite = true}; const auto result = ImageProcessing::ApplyThresholdMaximumConnectedComponents(input, output, SizeVec3{2, 1, 1}, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == (nanParameter ? -28610 : -28612)); + REQUIRE(result.errors().front().code == -28612); REQUIRE(input.reads() == 0); REQUIRE(output.writes() == 0); } diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 2894252fbf..e4b26c06b8 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -388,6 +388,8 @@ Result<> AccumulateCheckedOtsuValue(Histogram& histogram, T value, T minValue, T * @return Histogram and selected finite and nonfinite sample counts, or an input, numeric, or transfer error. * A valid cancellation result can be incomplete. The caller must check shouldCancel before use. * + * @pre An active mask has the same tuple count as the input. + * * Mask selection precedes nonfinite and range checks. Compact native integer ranges can retain exact frequencies in a fixed ring. * The ring uses capacity left after the input and mask buffers. A scan without usable ring frequencies uses a second bounded pass for nonconstant input. */ @@ -403,9 +405,9 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { return MakeErrorResult(-28604, "Otsu histogram bin storage exceeds the addressable allocation size. Reduce Number Of Histogram Bins."); } - if(input.getNumberOfComponents() != 1 || (maskPtr != nullptr && (maskPtr->getNumberOfComponents() != 1 || maskPtr->getSize() != input.getSize()))) + if(input.getNumberOfComponents() != 1 || (maskPtr != nullptr && maskPtr->getNumberOfComponents() != 1)) { - return MakeErrorResult(-8329, "Otsu input and optional mask must be scalar and contain the same number of tuples. Select a scalar mask with one value per input tuple."); + return MakeErrorResult(-8329, "Otsu input and optional mask must be scalar. Select arrays with one component per tuple."); } if(shouldCancel) { @@ -695,6 +697,8 @@ uint8 ClassifyOtsuValue(T value, const std::vector& thresholds, const O * @return Collected diagnostics, including the nonfinite exclusion warning, or a validation, numeric, or transfer error. * Cancellation retains the valid engine-result convention. * + * @pre The class labels fit in uint8, Excluded Label is distinct when used, and the mask has the input tuple count. + * * Mask selection precedes histogram and nonfinite checks. The optional mask is read in bounded owned chunks. * A later write failure or cancellation can leave completed output batches in place. */ @@ -706,12 +710,6 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore(options.LabelOffset) + options.NumberOfThresholds > 255 || - ((maskPtr != nullptr || (std::is_floating_point_v && options.WarnOnNonfinite)) && options.ExcludedLabel >= options.LabelOffset && - static_cast(options.ExcludedLabel) <= static_cast(options.LabelOffset) + options.NumberOfThresholds)) - { - return MakeErrorResult(-28603, "Otsu labels do not fit a distinct uint8 class interval. Reduce Label Offset/Number Of Thresholds or select an Excluded Label outside that interval."); - } auto histogramResult = ComputeOtsuHistogram(input, maskPtr, options, shouldCancel, inputArrayPath); if(histogramResult.invalid()) { diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index a9dab126eb..83f583040f 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -469,7 +469,9 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor * @return Collected warnings, or a parameter, shape, numeric-capacity, allocation, or transfer error. * Cancellation retains the valid engine-result convention. * - * NaN Upper Boundary always fails. Image Error fails in the initial scan, including for an empty requested band. + * @pre The caller has rejected a NaN Upper Boundary. + * + * Image Error fails in the initial scan, including for an empty requested band. * Warn retains ordinary inclusive comparisons. With no finite data, the engine fills Outside Value and reports that no threshold was calculated. * Count capacity remains strict. Owned scan and output buffers are bounded; component-run tables depend on image topology. * Completed output is not rolled back after a late failure or cancellation. @@ -479,10 +481,6 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in const std::atomic_bool& shouldCancel, const IFilter::MessageHandler& messageHandler = {}, const DataPath& inputArrayPath = {}) { Result<> status; - if(std::isnan(options.UpperBoundary)) - { - return MakeErrorResult(-28610, "Upper Boundary is NaN. Enter a valid numeric cutoff; positive infinity selects the native maximum and negative infinity requests an empty band."); - } if(input.getNumberOfComponents() != 1 || output.getNumberOfComponents() != 1 || output.getSize() != input.getSize()) { return MakeErrorResult(-28612, "Threshold Maximum Connected Components requires scalar input and uint8 output with matching tuple counts. Recreate the output for the selected image."); From e877af212884a31c09b53e5bd8d802ded3e2b53d Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 55/98] REV: Construct image results through the Result API Replace writes to Result internals with value construction. Addresses review item 16. Signed-off-by: Jessica Marquis --- .../ImageProcessing/ConnectedComponentEngine.hpp | 8 ++------ src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp | 4 +--- src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp | 4 +--- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index 38215f23d7..29d0bc2f7c 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -480,9 +480,7 @@ class ConnectedComponentLabelState m_MessageHandler.sendWarningMessage(m_Warnings.warnings().front().message); } } - Result result; - result.m_Expected = static_cast(candidate); - return result; + return {{static_cast(candidate)}}; } // Called once by the outer execution wrapper, including after cancellation, retry, or an independent error. @@ -1679,9 +1677,7 @@ Result CountConnectedComponents(const AbstractDataStore& inStore, con ++numComponents; } } - Result result; - result.m_Expected = numComponents; - return result; + return {{numComponents}}; } catch(const std::bad_alloc&) { return MakeErrorResult(-8774, "Connected-component counting could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory."); diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 9095d9757c..1438c40f4f 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -684,9 +684,7 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u const float64 offset = returnBinMidpoint ? 0.5 : 1.0; thresholds[t] = histogram.min + (static_cast(maxVarThresholdIndexes[t]) + offset) * histogram.binWidth; } - Result> result; - result.m_Expected = std::move(thresholds); - return result; + return {{std::move(thresholds)}}; } } // namespace detail diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index e4b26c06b8..2df0c1fb2e 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -653,9 +653,7 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram } if(histogram.binWidth == 0.0 && histogram.min == histogram.max) { - Result> result; - result.m_Expected = std::vector(options.NumberOfThresholds, histogram.min); - return result; + return {{std::vector(options.NumberOfThresholds, histogram.min)}}; } return detail::ComputeOtsuThresholds(histogram, options.NumberOfThresholds, options.ValleyEmphasis, options.ReturnBinMidpoint, &shouldCancel); } From f0a1176ff38ade38a6eef34445b97dd52f6b113e Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 56/98] REV: Assign and name image filter diagnostic codes Use the agreed filter-specific ranges for new diagnostics. Keep the inherited codes and name the affected diagnostic sites. Addresses review item 6. Signed-off-by: Jessica Marquis --- .../Filters/HConvexImageFilter.cpp | 2 +- .../Filters/HMinimaImageFilter.cpp | 2 +- .../OtsuMultipleThresholdsImageFilter.cpp | 8 +-- .../Filters/SquareImageFilter.cpp | 22 ++++---- .../utils/ImageFilterExecutorDetail.hpp | 8 +-- .../test/HConvexImageFilterTest.cpp | 4 +- .../test/HMinimaImageFilterTest.cpp | 2 +- .../OtsuMultipleThresholdsImageFilterTest.cpp | 44 +++++++-------- .../test/SquareImageFilterTest.cpp | 12 ++--- .../ImageProcessing/HistogramEngine.hpp | 16 ++++-- .../ImageProcessing/OtsuImageEngine.hpp | 44 ++++++++++----- ...esholdMaximumConnectedComponentsEngine.hpp | 54 ++++++++++++------- 12 files changed, 132 insertions(+), 86 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp index 874c6d1608..e78c5ba974 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.cpp @@ -33,7 +33,7 @@ namespace // its Height guards: -8553 (non-finite) and -8556 (negative). constexpr int32 k_NonFiniteHeight = -8553; constexpr int32 k_NegativeHeight = -8556; -constexpr int32 k_FractionalIntegerHeight = -1874101; +constexpr int32 k_FractionalIntegerHeight = -29101; } // namespace namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp index dd48acfc0a..172f7c9968 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HMinimaImageFilter.cpp @@ -33,7 +33,7 @@ namespace // the -8552 (non-finite) and -8555 (negative) slots for its Height guards (HMaxima owns -8551/-8554). constexpr int32 k_NonFiniteHeight = -8552; constexpr int32 k_NegativeHeight = -8555; -constexpr int32 k_FractionalIntegerHeight = -1874111; +constexpr int32 k_FractionalIntegerHeight = -29201; } // namespace namespace nx::core diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 0d67eda94c..f4a1149961 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -39,9 +39,9 @@ namespace nx::core { namespace { -constexpr int32 k_OtsuClassIntervalOverflow = -8312; -constexpr int32 k_OtsuExcludedLabelCollision = -8313; -constexpr int32 k_OtsuMaskTupleMismatch = -8314; +constexpr int32 k_OtsuClassIntervalOverflow = -28903; +constexpr int32 k_OtsuExcludedLabelCollision = -28907; +constexpr int32 k_OtsuMaskTupleMismatch = -28908; } // namespace std::string OtsuMultipleThresholdsImageFilter::name() const @@ -155,7 +155,7 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const const auto* maskPtr = dataStructure.getDataAs(maskPath); if(maskPtr == nullptr) { - constexpr int32 invalidMaskArrayKind = -83100; + constexpr int32 invalidMaskArrayKind = -28909; return {.outputActions = MakeErrorResult(invalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp index 0889f7a48c..63f82e6e27 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SquareImageFilter.cpp @@ -32,6 +32,9 @@ using namespace nx::core; namespace { +constexpr int32 k_SquareBelowSignedLimit = -29001; +constexpr int32 k_SquareAboveSignedLimit = -29002; +constexpr int32 k_SquareAboveUnsignedLimit = -29003; template using SquareOutputType = std::conditional_t< std::is_same_v, int16, @@ -64,24 +67,25 @@ Result<> validateSquareRange(const AbstractDataStore& inputStore, const DataP constexpr int64 limit = 3037000499LL; if(extrema.min < -limit) { - auto errorResult = MakeErrorResult(-1874091, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at least -3037000499. Convert the input to " - "float64 first if approximate larger squares are acceptable.", - extrema.min, inputPath.toString())); + auto errorResult = + MakeErrorResult(k_SquareBelowSignedLimit, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at least -3037000499. Convert the input to " + "float64 first if approximate larger squares are acceptable.", + extrema.min, inputPath.toString())); return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); } if(extrema.max > limit) { - auto errorResult = MakeErrorResult(-1874092, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at most " - "3037000499. Convert the input to float64 first if approximate larger squares are acceptable.", - extrema.max, inputPath.toString())); + auto errorResult = MakeErrorResult(k_SquareAboveSignedLimit, fmt::format("Square Image Filter cannot square value {} in input array '{}': int64 inputs must be at most " + "3037000499. Convert the input to float64 first if approximate larger squares are acceptable.", + extrema.max, inputPath.toString())); return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); } } else if(extrema.max > 4294967295ULL) { - auto errorResult = MakeErrorResult(-1874093, fmt::format("Square Image Filter cannot square value {} in input array '{}': uint64 inputs must be at most " - "4294967295. Convert the input to float64 first if approximate larger squares are acceptable.", - extrema.max, inputPath.toString())); + auto errorResult = MakeErrorResult(k_SquareAboveUnsignedLimit, fmt::format("Square Image Filter cannot square value {} in input array '{}': uint64 inputs must be at most " + "4294967295. Convert the input to float64 first if approximate larger squares are acceptable.", + extrema.max, inputPath.toString())); return MergeResults(ConvertResult(std::move(extremaResult)), std::move(errorResult)); } return ConvertResult(std::move(extremaResult)); diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index 01772ddf55..b3b0efa80e 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -909,9 +909,11 @@ Result<> SubtractHConvexContrastRange(nonstd::span inputValues, nonstd: const usize invalid = firstInvalid.load(std::memory_order_relaxed); if(invalid != count) { - return MakeErrorResult(-1874102, fmt::format("H Convex contrast at tuple {} of input array '{}' exceeds the {} output maximum {}. Reduce Height or explicitly convert the input to a wider or " - "floating-point type before this filter.", - start + invalid, inputPath.toString(), DataTypeToString(GetDataType()), std::numeric_limits::max())); + constexpr int32 k_HConvexContrastOverflow = -29102; + return MakeErrorResult(k_HConvexContrastOverflow, + fmt::format("H Convex contrast at tuple {} of input array '{}' exceeds the {} output maximum {}. Reduce Height or explicitly convert the input to a wider or " + "floating-point type before this filter.", + start + invalid, inputPath.toString(), DataTypeToString(GetDataType()), std::numeric_limits::max())); } return {}; } diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 3607ea96d1..11ab66eb57 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -131,7 +131,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: integer fractional He // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().size() == 1); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. - REQUIRE(preflightResult.outputActions.errors().front().code == -1874101); + REQUIRE(preflightResult.outputActions.errors().front().code == -29101); const auto executeResult = filter.execute(dataStructure, args); SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); REQUIRE(executeResult.result.errors().front().code == preflightResult.outputActions.errors().front().code); @@ -392,7 +392,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: signed contrast overf { REQUIRE(executeResult.result.invalid()); REQUIRE(executeResult.result.errors().size() == 1); - REQUIRE(executeResult.result.errors().front().code == -1874102); + REQUIRE(executeResult.result.errors().front().code == -29102); REQUIRE(executeResult.result.errors().front().message.find("tuple 1") != std::string::npos); REQUIRE(executeResult.result.errors().front().message.find(inputPath.toString()) != std::string::npos); } diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index a751ecd7fc..ce3a7b5fc8 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -73,7 +73,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: integer fractional He const auto preflightResult = filter.preflight(dataStructure, args); REQUIRE(preflightResult.outputActions.invalid()); REQUIRE(preflightResult.outputActions.errors().size() == 1); - REQUIRE(preflightResult.outputActions.errors().front().code == -1874111); + REQUIRE(preflightResult.outputActions.errors().front().code == -29201); REQUIRE(preflightResult.outputActions.errors().front().message.find(inputPath.toString()) != std::string::npos); REQUIRE(preflightResult.outputActions.errors().front().message.find(height == 0.5 ? "0.5" : "2.5") != std::string::npos); const auto executeResult = filter.execute(dataStructure, args); diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 9e5669a8c9..5b4d135746 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -259,10 +259,10 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: preflight check { REQUIRE(preflightResult.outputActions.invalid()); REQUIRE_FALSE(preflightResult.outputActions.errors().empty()); - REQUIRE(preflightResult.outputActions.errors().front().code == -8312); + REQUIRE(preflightResult.outputActions.errors().front().code == -28903); const auto execution = filter.execute(dataStructure, arguments); SIMPLNX_RESULT_REQUIRE_INVALID(execution.result); - REQUIRE(execution.result.errors().front().code == -8312); + REQUIRE(execution.result.errors().front().code == -28903); } UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -422,7 +422,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn r if constexpr(std::is_floating_point_v) { SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); - REQUIRE(preflight.outputActions.errors().front().code == -8313); + REQUIRE(preflight.outputActions.errors().front().code == -28907); } else { @@ -475,7 +475,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn fits finit const auto result = filter.execute(dataStructure, restored.value()); SIMPLNX_RESULT_REQUIRE_VALID(result.result); REQUIRE(result.result.warnings().size() == 1); - REQUIRE(result.result.warnings().front().code == -8319); + REQUIRE(result.result.warnings().front().code == -28959); REQUIRE(result.result.warnings().front().message.find(useMask ? "skipped 2 nonfinite pixels still selected after applying the input mask" : "skipped 3 nonfinite pixels") != std::string::npos); REQUIRE(result.result.warnings().front().message.find("Excluded Label for these pixels is 42") != std::string::npos); requireOtsuLabels(dataStructure, inputPath, expected); @@ -493,7 +493,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects e const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, otsuArguments(inputPath)); SIMPLNX_RESULT_REQUIRE_INVALID(result.result); - REQUIRE(result.result.errors().front().code == -28654); + REQUIRE(result.result.errors().front().code == -28954); const auto& message = result.result.errors().front().message; const auto* kind = std::isnan(value) ? "NaN" : (value < 0 ? "negative infinity" : "positive infinity"); REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); @@ -529,7 +529,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error finds the const OtsuMultipleThresholdsImageFilter filter; const auto result = filter.execute(dataStructure, args); SIMPLNX_RESULT_REQUIRE_INVALID(result.result); - REQUIRE(result.result.errors().front().code == -28654); + REQUIRE(result.result.errors().front().code == -28954); const auto& message = result.result.errors().front().message; REQUIRE(message.find(fmt::format("'{}'", inputPath.toString())) != std::string::npos); REQUIRE(message.find("negative infinity at zero-based flat tuple index 262145") != std::string::npos); @@ -589,7 +589,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. - REQUIRE(result.result.errors().front().code == -28655); + REQUIRE(result.result.errors().front().code == -28955); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -601,7 +601,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 0, 4, 4}); auto args = otsuArguments(inputPath); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); - int32 expectedCode = -8313; + int32 expectedCode = -28907; SECTION("unused interior class is still reserved") { args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); @@ -621,7 +621,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask } SECTION("different tuple count") { - expectedCode = -8314; + expectedCode = -28908; args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1}))); } SECTION("full unmasked interval leaves no masked label") @@ -703,7 +703,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: true constants input.setValue(0, std::numeric_limits::max() - 1); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28656); + REQUIRE(result.errors().front().code == -28956); } SECTION("opposite float64 extrema overflow the range") { @@ -711,21 +711,21 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: true constants input.setValue(0, -std::numeric_limits::max()); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28656); + REQUIRE(result.errors().front().code == -28956); } SECTION("nonzero means square to zero") { const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 0, .max = 3e-200, .binWidth = 1e-200}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28650); + REQUIRE(result.errors().front().code == -28950); } SECTION("finite means square to infinity") { const ImageProcessing::Histogram histogram{.counts = {1, 1, 100}, .min = 1e200, .max = 4e200, .binWidth = 1e200}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28650); + REQUIRE(result.errors().front().code == -28950); } SECTION("unique rational optimum stays unchanged at ordinary scale") { @@ -1188,7 +1188,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: StringArray mas { SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); const auto& error = result.outputActions.errors().front(); - REQUIRE(error.code == -83100); + REQUIRE(error.code == -28909); REQUIRE(error.message.find(maskPath.toString()) != std::string::npos); REQUIRE(error.message.find("scalar bool or uint8") != std::string::npos); } @@ -1265,11 +1265,11 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants exclu input.setValue(0, std::numeric_limits::quiet_NaN()); const auto noFiniteResult = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(noFiniteResult); - REQUIRE(noFiniteResult.errors().front().code == -28655); + REQUIRE(noFiniteResult.errors().front().code == -28955); const DataStore empty({0}, {1}, 0.0); const auto emptyResult = ImageProcessing::ComputeOtsuHistogram(empty, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(emptyResult); - REQUIRE(emptyResult.errors().front().code == -28655); + REQUIRE(emptyResult.errors().front().code == -28955); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -1286,14 +1286,14 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper options.NumberOfBins = 1; const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28600); + REQUIRE(result.errors().front().code == -28900); } SECTION("output size") { DataStore output({1}, {1}, uint8{0}); const auto result = ImageProcessing::ApplyOtsuImage(input, output, static_cast*>(nullptr), options, shouldCancel, {}); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28602); + REQUIRE(result.errors().front().code == -28902); } SECTION("threshold count") { @@ -1301,21 +1301,21 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper const ImageProcessing::Histogram histogram{.counts = {1, 1}, .min = 0, .max = 4, .binWidth = 2}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28601); + REQUIRE(result.errors().front().code == -28901); } SECTION("empty histogram") { const ImageProcessing::Histogram histogram{.counts = {0, 0}, .min = 0, .max = 4, .binWidth = 2}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28605); + REQUIRE(result.errors().front().code == -28905); } SECTION("nonfinite histogram metadata") { const ImageProcessing::Histogram histogram{.counts = {1, 1}, .min = std::numeric_limits::infinity(), .max = std::numeric_limits::infinity(), .binWidth = 0}; const auto result = ImageProcessing::ComputeCheckedOtsuThresholds(histogram, options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28606); + REQUIRE(result.errors().front().code == -28906); } SECTION("collapsed required bin boundaries") { @@ -1324,7 +1324,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper input.setValue(1, 1e20 + 32768.0); const auto result = ImageProcessing::ComputeOtsuHistogram(input, static_cast*>(nullptr), options, shouldCancel); SIMPLNX_RESULT_REQUIRE_INVALID(result); - REQUIRE(result.errors().front().code == -28657); + REQUIRE(result.errors().front().code == -28957); } } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 8ab932972d..1202c8daef 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -173,27 +173,27 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: 64-bit overflow inputs return a const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); SECTION("Signed minimum") { - requireSquareOverflow(std::numeric_limits::min(), -1874091); + requireSquareOverflow(std::numeric_limits::min(), -29001); } SECTION("Signed negative adjacent overflow") { - requireSquareOverflow(-3037000500LL, -1874091); + requireSquareOverflow(-3037000500LL, -29001); } SECTION("Signed positive adjacent overflow") { - requireSquareOverflow(3037000500LL, -1874092); + requireSquareOverflow(3037000500LL, -29002); } SECTION("Signed maximum") { - requireSquareOverflow(std::numeric_limits::max(), -1874092); + requireSquareOverflow(std::numeric_limits::max(), -29002); } SECTION("Unsigned adjacent overflow") { - requireSquareOverflow(4294967296ULL, -1874093); + requireSquareOverflow(4294967296ULL, -29003); } SECTION("Unsigned maximum") { - requireSquareOverflow(std::numeric_limits::max(), -1874093); + requireSquareOverflow(std::numeric_limits::max(), -29003); } } diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 1438c40f4f..311c76073b 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -19,6 +19,11 @@ namespace nx::core::ImageProcessing { +inline constexpr int32 k_OtsuInvalidClassVariance = -28950; +inline constexpr int32 k_OtsuInvalidInitialValleyObjective = -28951; +inline constexpr int32 k_OtsuInvalidCandidateObjective = -28952; +inline constexpr int32 k_OtsuInvalidCandidateValleyObjective = -28953; + struct Histogram { std::vector counts; @@ -538,7 +543,8 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u { if(!OtsuObjectiveIsUsable(globalMean, classes, maxVarBetween)) { - return MakeErrorResult>(-28650, "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); + return MakeErrorResult>(k_OtsuInvalidClassVariance, + "Otsu class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); } } @@ -558,7 +564,8 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u { if(!OtsuObjectiveIsUsable(globalMean, classes, maxVarBetween, valleyEmphasisFactor == 0.0)) { - return MakeErrorResult>(-28651, "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + return MakeErrorResult>(k_OtsuInvalidInitialValleyObjective, + "Otsu valley-weighted initial objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } } } @@ -647,7 +654,7 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u { if(!OtsuObjectiveIsUsable(globalMean, classes, varBetween)) { - return MakeErrorResult>(-28652, + return MakeErrorResult>(k_OtsuInvalidCandidateObjective, "Otsu candidate class means or variance overflowed or collapsed through underflow. Rescale the selected finite input before thresholding."); } } @@ -665,7 +672,8 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u { if(!OtsuObjectiveIsUsable(globalMean, classes, varBetween, valleyEmphasisFactor == 0.0)) { - return MakeErrorResult>(-28653, "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); + return MakeErrorResult>(k_OtsuInvalidCandidateValleyObjective, + "Otsu valley-weighted candidate objective is not finite or usable. Rescale the selected finite input or disable Valley Emphasis."); } } } diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 2df0c1fb2e..1784dc888c 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -29,6 +29,19 @@ namespace nx::core::ImageProcessing { +inline constexpr int32 k_OtsuTooFewBins = -28900; +inline constexpr int32 k_OtsuInvalidThresholdCount = -28901; +inline constexpr int32 k_OtsuInvalidOutputShape = -28902; +inline constexpr int32 k_OtsuBinAllocationTooLarge = -28904; +inline constexpr int32 k_OtsuEmptyHistogram = -28905; +inline constexpr int32 k_OtsuInvalidHistogram = -28906; +inline constexpr int32 k_OtsuNonScalarStore = -28910; +inline constexpr int32 k_OtsuNonfiniteInput = -28954; +inline constexpr int32 k_OtsuNoFiniteSamples = -28955; +inline constexpr int32 k_OtsuInvalidBinWidth = -28956; +inline constexpr int32 k_OtsuCollapsedBins = -28957; +inline constexpr int32 k_OtsuSkippedNonfinite = -28959; + /** * @struct OtsuImageOptions * @brief Holds Otsu controls for fixed uint8 output labels. @@ -315,7 +328,8 @@ inline Result<> ConfigureCheckedOtsuBins(Histogram& histogram, bool nativeConsta if(!std::isfinite(range) || range <= 0.0 || !std::isfinite(upper) || upper <= histogram.min || !std::isfinite(histogram.binWidth) || histogram.binWidth <= 0.0 || !std::isfinite(1.0 / histogram.binWidth)) { - return MakeErrorResult(-28656, "Otsu cannot form a finite positive histogram range/bin width from the selected native values. Rescale the finite input or use fewer histogram bins."); + return MakeErrorResult(k_OtsuInvalidBinWidth, + "Otsu cannot form a finite positive histogram range/bin width from the selected native values. Rescale the finite input or use fewer histogram bins."); } float64 previousBoundary = histogram.min; float64 previousMeasurement = histogram.min; @@ -329,7 +343,7 @@ inline Result<> ConfigureCheckedOtsuBins(Histogram& histogram, bool nativeConsta const float64 measurement = histogram.min + ((static_cast(bin) + 0.5) * histogram.binWidth); if(!std::isfinite(boundary) || !std::isfinite(measurement) || boundary <= previousBoundary || (bin != 0 && measurement <= previousMeasurement)) { - return MakeErrorResult(-28657, "Otsu histogram boundaries or measurements collapse at the selected precision. Rescale the finite input or use fewer histogram bins."); + return MakeErrorResult(k_OtsuCollapsedBins, "Otsu histogram boundaries or measurements collapse at the selected precision. Rescale the finite input or use fewer histogram bins."); } previousBoundary = boundary; previousMeasurement = measurement; @@ -399,15 +413,15 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp { if(options.NumberOfBins < 2) { - return MakeErrorResult(-28600, "Otsu requires at least two histogram bins. Increase Number Of Histogram Bins."); + return MakeErrorResult(k_OtsuTooFewBins, "Otsu requires at least two histogram bins. Increase Number Of Histogram Bins."); } if(static_cast(options.NumberOfBins) > std::vector{}.max_size()) { - return MakeErrorResult(-28604, "Otsu histogram bin storage exceeds the addressable allocation size. Reduce Number Of Histogram Bins."); + return MakeErrorResult(k_OtsuBinAllocationTooLarge, "Otsu histogram bin storage exceeds the addressable allocation size. Reduce Number Of Histogram Bins."); } if(input.getNumberOfComponents() != 1 || (maskPtr != nullptr && maskPtr->getNumberOfComponents() != 1)) { - return MakeErrorResult(-8329, "Otsu input and optional mask must be scalar. Select arrays with one component per tuple."); + return MakeErrorResult(k_OtsuNonScalarStore, "Otsu input and optional mask must be scalar. Select arrays with one component per tuple."); } if(shouldCancel) { @@ -455,9 +469,10 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp const T value = values[summary.FirstNonfiniteIndex]; const auto* kind = std::isnan(value) ? "NaN" : (std::signbit(value) ? "negative infinity" : "positive infinity"); const auto inputName = inputArrayPath.empty() ? "the input array" : fmt::format("input array '{}'", inputArrayPath.toString()); - return MakeErrorResult(-28654, fmt::format("Otsu found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to skip nonfinite pixels and assign Excluded Label, or " - "select a mask that excludes these pixels.", - kind, batchStart + summary.FirstNonfiniteIndex, inputName)); + return MakeErrorResult(k_OtsuNonfiniteInput, + fmt::format("Otsu found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to skip nonfinite pixels and assign Excluded Label, or " + "select a mask that excludes these pixels.", + kind, batchStart + summary.FirstNonfiniteIndex, inputName)); } if(summary.FiniteCount != 0) { @@ -549,7 +564,8 @@ Result ComputeOtsuHistogram(const AbstractDataStore& inp if(result.FiniteCount == 0) { return ConvertResultTo( - MergeResults(std::move(rangeResult), MakeErrorResult(-28655, "Otsu has no selected finite samples. Select at least one finite input value with the optional mask before thresholding.")), + MergeResults(std::move(rangeResult), + MakeErrorResult(k_OtsuNoFiniteSamples, "Otsu has no selected finite samples. Select at least one finite input value with the optional mask before thresholding.")), std::move(result)); } auto& histogram = result.HistogramData; @@ -640,15 +656,15 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram } if(options.NumberOfThresholds == 0 || options.NumberOfThresholds >= histogram.counts.size()) { - return MakeErrorResult>(-28601, "Otsu Number Of Thresholds must be positive and less than the histogram bin count."); + return MakeErrorResult>(k_OtsuInvalidThresholdCount, "Otsu Number Of Thresholds must be positive and less than the histogram bin count."); } if(std::ranges::all_of(histogram.counts, [](usize count) { return count == 0; })) { - return MakeErrorResult>(-28605, "Otsu threshold calculation requires at least one histogram sample."); + return MakeErrorResult>(k_OtsuEmptyHistogram, "Otsu threshold calculation requires at least one histogram sample."); } if(!std::isfinite(histogram.min) || !std::isfinite(histogram.max) || !std::isfinite(histogram.binWidth) || histogram.binWidth < 0.0) { - return MakeErrorResult>(-28606, + return MakeErrorResult>(k_OtsuInvalidHistogram, "Otsu threshold calculation requires finite histogram bounds and a nonnegative finite bin width. Rebuild the histogram from selected finite data."); } if(histogram.binWidth == 0.0 && histogram.min == histogram.max) @@ -706,7 +722,7 @@ Result<> ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore ApplyOtsuImage(const AbstractDataStore& input, AbstractDataStore> ScanThresholdMaxCC(const AbstractDataStore& const T value = values[summary.FirstNonfiniteIndex]; const auto* kind = std::isnan(value) ? "NaN" : (std::signbit(value) ? "negative infinity" : "positive infinity"); const auto inputName = inputArrayPath.empty() ? "the input array" : fmt::format("input array '{}'", inputArrayPath.toString()); - return ConvertInvalidResult>( - MergeResults(std::move(status), - MakeErrorResult(-28611, fmt::format("Threshold Maximum Connected Components found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to assign Outside " - "Value to nonfinite pixels.", - kind, start + summary.FirstNonfiniteIndex, inputName)))); + return ConvertInvalidResult>(MergeResults( + std::move(status), MakeErrorResult(k_ThresholdMaxCCNonfiniteInput, + fmt::format("Threshold Maximum Connected Components found {} at zero-based flat tuple index {} in {}. Select Warn and Continue to assign Outside " + "Value to nonfinite pixels.", + kind, start + summary.FirstNonfiniteIndex, inputName)))); } start += count; } return {{summary}, std::move(status.warnings())}; } catch(const std::bad_alloc&) { - return ConvertResultTo( - MergeResults(std::move(status), MakeErrorResult(-28617, "Threshold Maximum Connected Components could not allocate its scan buffers. Reduce the data size or provide more memory.")), - std::move(summary)); + return ConvertResultTo(MergeResults(std::move(status), MakeErrorResult(k_ThresholdMaxCCScanAllocationFailed, + "Threshold Maximum Connected Components could not allocate its scan buffers. Reduce the data size or provide more memory.")), + std::move(summary)); } catch(const std::length_error&) { - return ConvertResultTo(MergeResults(std::move(status), MakeErrorResult(-28618, "Threshold Maximum Connected Components exceeded an addressable scan buffer size. Reduce the image dimensions.")), - std::move(summary)); + return ConvertResultTo( + MergeResults(std::move(status), MakeErrorResult(k_ThresholdMaxCCScanTooLarge, "Threshold Maximum Connected Components exceeded an addressable scan buffer size. Reduce the image dimensions.")), + std::move(summary)); } } @@ -389,7 +402,7 @@ Result SearchThresholdMaxCC(const AbstractDataStore& input, const SizeVec3 { if(low == oldLow && high == oldHigh && midpoint == oldMidpoint && ThresholdMaxCCSpanExceedsTwo(low, high)) { - status.warnings().push_back(Warning{.code = -28616, + status.warnings().push_back(Warning{.code = k_ThresholdMaxCCSearchStalled, .message = "Threshold search stopped because native floating precision repeated the complete search state while the interval was wider than " "2 intensity units. The candidate selected by the normal component-count comparison was retained; segmentation is heuristic."}); break; @@ -448,10 +461,12 @@ Result<> WriteThresholdMaxCC(const AbstractDataStore& input, AbstractDataStor return status; } catch(const std::bad_alloc&) { - return MergeResults(std::move(status), MakeErrorResult(-28634, "Threshold Maximum Connected Components could not allocate its output buffers. Reduce the data size or provide more memory.")); + return MergeResults(std::move(status), MakeErrorResult(k_ThresholdMaxCCOutputAllocationFailed, + "Threshold Maximum Connected Components could not allocate its output buffers. Reduce the data size or provide more memory.")); } catch(const std::length_error&) { - return MergeResults(std::move(status), MakeErrorResult(-28635, "Threshold Maximum Connected Components exceeded an addressable output buffer size. Reduce the image dimensions.")); + return MergeResults(std::move(status), + MakeErrorResult(k_ThresholdMaxCCOutputTooLarge, "Threshold Maximum Connected Components exceeded an addressable output buffer size. Reduce the image dimensions.")); } } } // namespace detail @@ -483,7 +498,8 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in Result<> status; if(input.getNumberOfComponents() != 1 || output.getNumberOfComponents() != 1 || output.getSize() != input.getSize()) { - return MakeErrorResult(-28612, "Threshold Maximum Connected Components requires scalar input and uint8 output with matching tuple counts. Recreate the output for the selected image."); + return MakeErrorResult(k_ThresholdMaxCCMismatchedOutput, + "Threshold Maximum Connected Components requires scalar input and uint8 output with matching tuple counts. Recreate the output for the selected image."); } usize sliceValues = 0; usize volumeValues = 0; @@ -510,7 +526,7 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in if(summary.NaNCount != 0 || summary.NegativeInfinityCount != 0 || summary.PositiveInfinityCount != 0) { status.warnings().push_back(Warning{ - .code = -28614, + .code = k_ThresholdMaxCCNonfiniteInputWarning, .message = fmt::format("Input contains {} NaN, {} negative infinity and {} positive infinity pixels. {} Outside Value is {}. Nonzero Outside pixels remain foreground and can connect regions.", summary.NaNCount, summary.NegativeInfinityCount, summary.PositiveInfinityCount, @@ -520,7 +536,7 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in } if(emptyBand && !noFinite) { - status.warnings().push_back(Warning{.code = -28615, + status.warnings().push_back(Warning{.code = k_ThresholdMaxCCEmptyBand, .message = fmt::format("Upper Boundary requests an empty native intensity band. Threshold search was skipped; every output pixel is set to Outside Value " "({}). A nonzero Outside Value can make the whole image foreground downstream.", options.OutsideValue)}); @@ -546,11 +562,11 @@ Result<> ApplyThresholdMaximumConnectedComponents(const AbstractDataStore& in return MergeResults(std::move(status), std::move(writeResult)); } catch(const std::bad_alloc&) { - return MergeResults(std::move(status), - MakeErrorResult(-28636, "Threshold Maximum Connected Components could not allocate its scan or output buffers. Reduce the data size or provide more memory.")); + return MergeResults(std::move(status), MakeErrorResult(k_ThresholdMaxCCAllocationFailed, + "Threshold Maximum Connected Components could not allocate its scan or output buffers. Reduce the data size or provide more memory.")); } catch(const std::length_error&) { - return MergeResults(std::move(status), MakeErrorResult(-28637, "Threshold Maximum Connected Components exceeded an addressable buffer size. Reduce the image dimensions.")); + return MergeResults(std::move(status), MakeErrorResult(k_ThresholdMaxCCBufferTooLarge, "Threshold Maximum Connected Components exceeded an addressable buffer size. Reduce the image dimensions.")); } } } // namespace nx::core::ImageProcessing From a57a27f072e9deb873003b58aeda2d631c332095 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 57/98] STY: Match existing image member naming Use the surrounding naming style for new members. Addresses review item 15. Signed-off-by: Jessica Marquis --- .../src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp | 2 +- .../NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp | 2 +- .../Filters/Algorithms/WriteImageScanline.cpp | 2 +- .../src/NXImageProcessing/Filters/WriteImageFilter.cpp | 2 +- .../NXImageProcessing/utils/ComponentImageFilterExecutors.cpp | 4 ++-- .../NXImageProcessing/utils/WatershedImageFilterExecutors.cpp | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp index 2b5589f5b5..8443ce195c 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImage.hpp @@ -29,7 +29,7 @@ struct NXIMAGEPROCESSING_EXPORT WriteImageInputValues DataPath imageGeometryPath; DataPath imageDataArrayPath; bool createColorTable = false; - ImageProcessing::NonfiniteInputHandling NonfiniteInputHandling = ImageProcessing::NonfiniteInputHandling::Error; + ImageProcessing::NonfiniteInputHandling nonfiniteInputHandling = ImageProcessing::NonfiniteInputHandling::Error; std::string presetName; bool useMask = false; DataPath maskArrayPath; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp index 6ee99ab982..b89cf6d50d 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageDirect.cpp @@ -513,7 +513,7 @@ Result<> WriteImageDirect::operator()() } auto colorResult = ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceExtents, binPoints, controlPoints, maskArrayPtr, - m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); + m_InputValues.invalidColor, m_InputValues.nonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp index fdb7093e99..07472e9dc1 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/WriteImageScanline.cpp @@ -745,7 +745,7 @@ Result<> WriteImageScanline::operator()() auto colorResult = ExecuteDataFunction(ColorizeVolumeFunctor{}, dataType, imageArray, m_InputValues.planeIndex, dimX, dimY, dimZ, sliceExtents, m_InputValues.imageDataArrayPath, binPoints, controlPoints, - maskArrayPtr, m_InputValues.invalidColor, m_InputValues.NonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); + maskArrayPtr, m_InputValues.invalidColor, m_InputValues.nonfiniteInputHandling, m_ShouldCancel, std::function(std::vector&, usize)>(writeSlice), progress); return MergeResults(ConvertResult(std::move(controlPointsResult)), std::move(colorResult)); } diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp index adf0131b6a..ab9c63e311 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/WriteImageFilter.cpp @@ -358,7 +358,7 @@ Result<> WriteImageFilter::executeImpl(DataStructure& dataStructure, const Argum inputValues.imageGeometryPath = filterArgs.value(k_ImageGeomPath_Key); inputValues.imageDataArrayPath = filterArgs.value(k_ImageArrayPath_Key); inputValues.createColorTable = filterArgs.value(k_CreateColorTable_Key); - inputValues.NonfiniteInputHandling = inputValues.createColorTable ? + inputValues.nonfiniteInputHandling = inputValues.createColorTable ? static_cast(filterArgs.value(k_NonfiniteInputHandling_Key)) : ImageProcessing::NonfiniteInputHandling::Error; inputValues.presetName = filterArgs.value(k_SelectedPreset_Key); diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp index 4bb93284cc..1deac6b339 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ComponentImageFilterExecutors.cpp @@ -53,7 +53,7 @@ struct ConnectedComponentExecuteFn const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; - ConnectedComponentLabelLimitHandling LabelLimitHandling; + ConnectedComponentLabelLimitHandling labelLimitHandling; template Result<> operator()() const @@ -62,7 +62,7 @@ struct ConnectedComponentExecuteFn auto& outArray = dataStructure.getDataRefAs>(outputArrayPath); const auto pred = [](T v) { return v != T{}; }; // compile-time predicate (Pred deduced by LabelConnectedComponents) return LabelConnectedComponents(inArray.getDataStoreRef(), outArray.getDataStoreRef(), dims, pred, fullyConnected, shouldCancel, messageHandler, k_ConnectedComponent2DTargetBytes, - LabelLimitHandling); + labelLimitHandling); } }; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp index 60a778bdef..ffa37d1698 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/WatershedImageFilterExecutors.cpp @@ -44,7 +44,7 @@ struct MorphologicalWatershedFromMarkersExecuteFn bool fullyConnected; const std::atomic_bool& shouldCancel; const IFilter::MessageHandler& messageHandler; - MarkerIdConversionHandling ConversionHandling = MarkerIdConversionHandling::Error; + MarkerIdConversionHandling conversionHandling = MarkerIdConversionHandling::Error; template // T = grayscale type Result<> operator()() const @@ -68,7 +68,7 @@ struct MorphologicalWatershedFromMarkersExecuteFn return MakeErrorResult(-79010, "MorphologicalWatershedFromMarkers: failed to allocate a uint32 scratch store."); } - accumulated = WidenIntegerArrayToUInt32(dataStructure, markerArrayPath, *markerU32, shouldCancel, ConversionHandling); + accumulated = WidenIntegerArrayToUInt32(dataStructure, markerArrayPath, *markerU32, shouldCancel, conversionHandling); if(accumulated.invalid()) { return accumulated; From 2569379252bddf8b05428858a7e45f3fefcb1246 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Mon, 16 Nov 2026 18:32:00 -0500 Subject: [PATCH 58/98] TEST: Load plugins only when image tests need them Keep plugin loads where a test needs the registry. Initialize the application where a preferences fixture requires it. Addresses review item 9. Signed-off-by: Jessica Marquis --- .../test/AcosImageFilterTest.cpp | 12 +++--- .../test/AsinImageFilterTest.cpp | 11 +++--- .../test/ConnectedComponentEngineTest.cpp | 6 --- .../ConnectedComponentImageFilterTest.cpp | 8 ++-- .../test/CurvatureFlowImageFilterTest.cpp | 34 ++++++++-------- .../test/ExpImageFilterTest.cpp | 10 ++--- .../test/ExpNegativeImageFilterTest.cpp | 11 +++--- .../test/HConvexImageFilterTest.cpp | 14 +------ .../test/HMinimaImageFilterTest.cpp | 12 +----- .../test/Log10ImageFilterTest.cpp | 9 +++-- .../test/LogImageFilterTest.cpp | 9 +++-- ...calWatershedFromMarkersImageFilterTest.cpp | 27 +++++++------ .../OtsuMultipleThresholdsImageFilterTest.cpp | 33 ++++++---------- ...othingRecursiveGaussianImageFilterTest.cpp | 22 +++-------- .../test/SqrtImageFilterTest.cpp | 9 +++-- .../test/SquareImageFilterTest.cpp | 16 ++++---- .../test/TanImageFilterTest.cpp | 5 ++- ...imumConnectedComponentsImageFilterTest.cpp | 39 +++++++------------ .../NXImageProcessing/test/WriteImageTest.cpp | 19 --------- 19 files changed, 117 insertions(+), 189 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp index e88504a190..7e2f170f43 100644 --- a/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AcosImageFilterTest.cpp @@ -6,6 +6,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -125,7 +126,7 @@ class AcosTransferStore : public DataStore TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer zero produces float32 radians", "[NXImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; @@ -164,7 +165,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer zero produces fl // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: floating input retains type and propagates domain NaN", "[NXImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; @@ -196,7 +197,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: floating input retains t TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer endpoints and invalid values produce floating angles", "[NXImageProcessing][Issue1874][AcosImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const auto values = [] { if constexpr(std::is_signed_v) @@ -236,7 +237,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: integer endpoints and in // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: adjacent domain boundaries remain distinct", "[NXImageProcessing][Issue1874][AcosImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const TestType inside = std::nextafter(TestType{1}, TestType{0}); const TestType outside = std::nextafter(TestType{1}, std::numeric_limits::infinity()); @@ -265,7 +266,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AcosImageFilter: adjacent domain boundari // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::AcosImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][AcosImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); @@ -302,7 +303,6 @@ TEST_CASE("NXImageProcessing::AcosImageFilter: mapped multi-component output pre // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::AcosImageFilter: mapped buffered engine preserves cancellation and I/O failures", "[NXImageProcessing][Issue1874][AcosImageFilter]") { - UnitTest::LoadPlugins(); using OutputType = ImageProcessing::IntegerToFloat32Type; AcosTransferStore input(0); AcosTransferStore output(-7.0f); diff --git a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp index 6c0e28b872..a04db217f5 100644 --- a/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/AsinImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -65,7 +66,7 @@ DataPath runMappedIntegerAsin(DataStructure& dataStructure, const DataPath& inpu TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer one produces float32 radians", "[NXImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {1}); @@ -86,7 +87,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer one produces flo TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer endpoints and invalid values produce floating angles", "[NXImageProcessing][Issue1874][AsinImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const auto inputValues = [] { if constexpr(std::is_signed_v) @@ -125,7 +126,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: integer endpoints and in // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: floating input retains type and principal angles", "[NXImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {-1, -0.5, -TestType{0}, TestType{0}, 0.5, 1, 2, -2, std::numeric_limits::quiet_NaN(), @@ -159,7 +160,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: floating input retains t // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: adjacent domain boundaries remain distinct", "[NXImageProcessing][Issue1874][AsinImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const TestType insideValue = std::nextafter(TestType{1}, TestType{0}); const TestType outsideValue = std::nextafter(TestType{1}, std::numeric_limits::infinity()); @@ -187,7 +188,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::AsinImageFilter: adjacent domain boundari // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::AsinImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][AsinImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildRampImage(dataStructure, 2, 0, 0); diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp index a6310c0e84..5943f7e107 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp @@ -1132,7 +1132,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: bounded true-2-D cancell TEST_CASE("NXImageProcessing::ConnectedComponentEngine: native provisional capacity decisions require no large forest", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) { SUCCEED("A native index on this platform cannot exceed uint32 capacity; no overflowing candidate was executed."); @@ -1185,7 +1184,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: native provisional capac // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ConnectedComponentEngine: small legacy zero and reuse consequence model", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); // Literal wrapped IDs in a tiny real uint32 union-find. This is not a 2^32-entry forest execution. const IFilter::MessageHandler handler; ImageProcessing::detail::ConnectedComponentLabelState state(ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy, handler); @@ -1215,7 +1213,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: small legacy zero and re TEST_CASE("NXImageProcessing::ConnectedComponentEngine: tiled zero-neighbor consequence model preserves a foreground root", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); const bool fullyConnected = GENERATE(false, true); DataStore inputStore(ShapeType{1, 2, 3}, ShapeType{1}, 0); inputStore.setValue(1, 1); @@ -1272,7 +1269,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: tiled zero-neighbor cons TEST_CASE("NXImageProcessing::ConnectedComponentEngine: capacity warning context retains independent delivered diagnostics", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) { SUCCEED("No native overflowing candidate is available on this platform."); @@ -1297,7 +1293,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: capacity warning context TEST_CASE("NXImageProcessing::ConnectedComponentEngine: seeded warning context survives resident retry copy error and cancellation", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); if constexpr(std::numeric_limits::max() <= std::numeric_limits::max()) { SUCCEED("No native overflowing candidate is available on this platform."); @@ -1357,7 +1352,6 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: seeded warning context s TEST_CASE("NXImageProcessing::ConnectedComponentEngine: allocation addressability and I/O failures remain errors under both policies", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter][ConnectedComponentEngine]") { - UnitTest::LoadPlugins(); const auto handling = GENERATE(ConnectedComponentLabelLimitHandling::Error, ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy); const auto fault = GENERATE(ConnectedComponentReadFaultType::Error, ConnectedComponentReadFaultType::BadAllocOnce, ConnectedComponentReadFaultType::LengthError); std::atomic_bool shouldCancel{false}; diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp index 6abcf5b5e6..b4dbcba4d2 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp @@ -6,6 +6,7 @@ #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -96,7 +97,6 @@ std::vector MakeBlobField(usize dimX, usize dimY, usize dimZ) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: provisional label limit policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { - UnitTest::LoadPlugins(); const ConnectedComponentImageFilter filter; REQUIRE(filter.parametersVersion() == 2); const Parameters parameters = filter.parameters(); @@ -128,7 +128,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: provisional label l // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: both policies preserve literal connectivity labels and uint32 shape", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); const bool fullyConnected = GENERATE(false, true); @@ -181,7 +181,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: both policies prese // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: many provisional runs can form one exact component", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); const std::vector input{1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1}; @@ -215,7 +215,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: many provisional ru // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: execution cancellation keeps the filter cancellation result", "[NXImageProcessing][Issue1874][ConnectedComponentImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); DataStructure dataStructure; diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 20ab92c9ab..88a687bf1a 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -13,6 +13,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" @@ -409,7 +410,7 @@ void requireCurvatureFlowConstant(DataStructure& dataStructure, const DataPath& TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid time step is rejected before execution", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const float64 timeStep = GENERATE(-0.05, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()); const uint32 iterations = GENERATE(0u, 1u); @@ -427,7 +428,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid time step is rej TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid active spacing is rejected for no-op requests", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool is3D = GENERATE(false, true); const uint32 iterations = GENERATE(0u, 1u); @@ -459,7 +460,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: invalid active spacing i TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: finite negative spacing and unused Z remain accepted", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const float32 unusedSpacing = GENERATE(0.0F, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()); DataStructure dataStructure; @@ -477,7 +478,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: finite negative spacing TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: constant finite input preserves type and value", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 1, 1, 1, {TestType{7}}); @@ -487,7 +488,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: constant finite TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero operation copies int64 values exactly", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool zeroIterations = GENERATE(false, true); constexpr TestType inputValue = (TestType{1} << 53) + TestType{1}; @@ -502,7 +503,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero operation TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: terminal integer conversion has exact bounds", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); using ImageProcessing::detail::ConvertTerminalImageValue; using ImageProcessing::detail::TerminalImageConversionStatus; TestType output = 7; @@ -543,7 +543,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: terminal intege TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating narrowing retains IEEE values", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); using ImageProcessing::detail::NarrowFloatingImageValue; const auto positive = NarrowFloatingImageValue(std::numeric_limits::max()); const auto negative = NarrowFloatingImageValue(-std::numeric_limits::max()); @@ -560,7 +559,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating narrowing retai // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: independent one-step stencil truncates then clips once", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool clip = GENERATE(false, true); const bool negativeSpacing = GENERATE(false, true); @@ -597,7 +596,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: independent one-step ste TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op preserves integer endpoints", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const TestType value = GENERATE(std::numeric_limits::lowest(), std::numeric_limits::max()); const bool zeroIterations = GENERATE(false, true); @@ -609,7 +608,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op pr // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integer result errors", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 3, 3, 1, {0, 0, 0, 0, 100, 100, 0, 100, 100}); @@ -625,7 +624,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: nonfinite terminal integ // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong terminal routes honor the stencil", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool pingPong = GENERATE(false, true); const bool nonFinite = GENERATE(false, true); @@ -673,7 +672,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong t // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op propagates transfer failures and cancellation", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const int scenario = GENERATE(0, 1, 2, 3); const ShapeType shape{1, 1, 1}; @@ -703,7 +702,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: native no-op propagates // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: resident wrapper merges clipping warning with copy failure", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const ImageProcessing::ScopedWorkingMemoryTuningOverride tuning(uint64{1024} * 1024); const ShapeType shape{2, 3, 3}; @@ -727,7 +726,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: resident wrapper merges // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating output retains nonfinite and overflow behavior", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool nanInput = GENERATE(false, true); DataStructure dataStructure; @@ -766,7 +765,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: floating output // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded native transfers", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); constexpr usize valuesPerMiB = (usize{1024} * 1024) / sizeof(uint64); constexpr usize count = (2 * valuesPerMiB) + 1; @@ -793,7 +792,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: exact no-op uses bounded // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exits retain clipping and transfer warnings", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const int route = GENERATE(0, 1, 2); // ping-pong delayed read, ping-pong cancellation, resident fallback cancellation const ShapeType shape{3, 3, 3}; @@ -854,7 +853,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 3D exi // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: synthetic delayed 2D terminal exits retain warnings", "[NXImageProcessing][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool cancel = GENERATE(false, true); const ShapeType shape{1, 3, 3}; @@ -1090,7 +1089,6 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero iterations is a no- // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[NXImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const bool step = GENERATE(false, true); diff --git a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp index 9ef813927b..31daf0a95f 100644 --- a/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -33,7 +34,7 @@ using namespace nx::core; TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: integer input produces float64 growth and decay", "[NXImageProcessing][Issue1874][ExpImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); std::vector values = {0, 1, 100}; if constexpr(std::is_signed_v) @@ -108,7 +109,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: integer input produces fl // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values retain growth decay and range behavior", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), @@ -196,7 +197,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating IEEE values reta // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ExpImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); @@ -249,7 +250,6 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: mapped multi-component output pres // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::ExpImageFilter: floating result conversion handles finite narrowing boundaries", "[NXImageProcessing][Issue1874][ExpImageFilter]") { - UnitTest::LoadPlugins(); constexpr float64 finiteLimit = std::numeric_limits::max(); const float64 outside = std::nextafter(finiteLimit, std::numeric_limits::infinity()); const float64 inside = std::nextafter(finiteLimit, float64{0}); @@ -275,7 +275,7 @@ TEST_CASE("NXImageProcessing::ExpImageFilter: floating result conversion handles // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {-1, 0, 1}); diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index 312c71a286..3ca07af775 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -12,6 +12,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -40,7 +41,7 @@ using namespace nx::core; TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input produces float64 decay", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const auto values = [] { if constexpr(std::is_signed_v) @@ -117,7 +118,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: integer input pro // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE values retain sign reversed range behavior", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), @@ -209,7 +210,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating IEEE val // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: mapped multi component output preserves tuple shape", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); @@ -261,7 +262,7 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: mapped multi component out // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: floating input retains type for safe finite values", "[NXImageProcessing][Issue1874][ExpNegativeImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {-1, 0, 1}); @@ -360,7 +361,7 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: ITK real-image golden (def // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain masks feed components", "[NXImageProcessing][C15Compatibility][Issue1874][ExpNegativeImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}, usize{5}, usize{6}); diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index 11ab66eb57..db1910e281 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -9,6 +9,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -117,7 +118,7 @@ class HConvexTransferStore : public DataStore TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: integer fractional Height fails preflight", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const float64 height = GENERATE(0.5, 2.5); CAPTURE(height); @@ -142,7 +143,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: integer fractional He TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zero contrast with unchanged type", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -185,7 +185,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zer // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: floating fractional Height retains fractional contrast", "[NXImageProcessing][Issue1874][HConvexImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -223,7 +222,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: floating fractional H TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit contrast for unit and large Heights above double precision", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -261,7 +259,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: native 64-bit contras // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::HConvexImageFilter: exact signed 64-bit marker boundaries in both directions", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { - UnitTest::LoadPlugins(); constexpr int64 minimum = std::numeric_limits::lowest(); constexpr int64 maximum = std::numeric_limits::max(); const bool lower = GENERATE(false, true); @@ -293,7 +290,6 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact signed 64-bit marker bou TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: exact integer marker zero unit and huge Heights", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); constexpr TestType minimum = std::numeric_limits::lowest(); constexpr TestType maximum = std::numeric_limits::max(); const bool lower = GENERATE(false, true); @@ -328,7 +324,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: exact integer marker // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker arithmetic requires explicit opt-in", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { - UnitTest::LoadPlugins(); constexpr int64 inputValue = (int64{1} << 54) + 1; constexpr int64 expectedExistingValue = int64{1} << 54; std::array values = {inputValue}; @@ -349,7 +344,6 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker arithmetic requir // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker Height immediately below 2^64", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { - UnitTest::LoadPlugins(); ImageProcessing::ReconstructionMarkerOptions options; options.source = ImageProcessing::ReconstructionMarkerSource::MaskMinusHeight; options.height = ImageProcessing::ExactReconstructionHeight{std::numeric_limits::max() - uint64{2047}}; @@ -370,7 +364,6 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: exact marker Height immediatel TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: signed contrast overflow and adjacent representable contrast", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, int16, int32, int64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -420,7 +413,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: signed contrast overf TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zero contrast at native integer bounds", "[NXImageProcessing][Issue1874][HConvexImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -449,7 +441,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HConvexImageFilter: zero Height gives zer // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::HConvexImageFilter: whole Height contrast is stable under a signed baseline shift", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -475,7 +466,6 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: whole Height contrast is stabl // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::HConvexImageFilter: chunked contrast preserves transfer Results and cancellation", "[NXImageProcessing][Issue1874][HConvexImageFilter]") { - UnitTest::LoadPlugins(); HConvexTransferStore input(2); HConvexTransferStore reconstruction(1); HConvexTransferStore output(-1); diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index ce3a7b5fc8..1da89d6004 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -10,6 +10,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -59,7 +60,7 @@ Arguments makeHMinimaArguments(const DataPath& inputPath, float64 height, bool f TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: integer fractional Height fails preflight", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const float64 height = GENERATE(0.5, 2.5); CAPTURE(height); @@ -87,7 +88,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: integer fractional He TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves values and type", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -134,7 +134,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: floating fractional Height retains fractional minima", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -174,7 +173,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: floating fractional H // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: native 64-bit marker preserves units above double precision", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope algorithmTestScope(scenario); @@ -210,7 +208,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: native 64-bit marker TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves native integer bounds", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool fullyConnected = GENERATE(false, true); CAPTURE(scenario, fullyConnected); @@ -248,7 +245,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: zero Height preserves TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: whole Height raises or saturates native integer markers", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool fullyConnected = GENERATE(false, true); const bool atMaximum = GENERATE(false, true); @@ -285,7 +281,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: whole Height raises o // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: large whole Height retains exact native 64-bit markers", "[NXImageProcessing][Issue1874][HMinimaImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool fullyConnected = GENERATE(false, true); const float64 height = GENERATE(std::ldexp(1.0, 63), 0x1.fffffffffffffp63); @@ -324,7 +319,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::HMinimaImageFilter: large whole Height re // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::HMinimaImageFilter: whole Height is stable under a signed baseline shift", "[NXImageProcessing][Issue1874][HMinimaImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool fullyConnected = GENERATE(false, true); const bool positiveBaseline = GENERATE(false, true); @@ -580,7 +574,6 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: Preflight non-finite Height gu TEST_CASE("NXImageProcessing::HMinimaImageFilter: FromSIMPLJson", "[NXImageProcessing][HMinimaImageFilter]") { - UnitTest::LoadPlugins(); const nlohmann::json json = {{"Height", 3.5}, {"FullyConnected", true}, {"SelectedCellArrayPath", {{"Data Container Name", "Image Geometry"}, {"Attribute Matrix Name", "CellData"}, {"Data Array Name", "Input"}}}, @@ -602,7 +595,6 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: FromSIMPLJson", "[NXImageProce // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima)", "[NXImageProcessing][ItkGolden][HMinimaImageFilter]") { - UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("RA-Short.nrrd", "7778067eeb752b6ac396dd9e362e8346", [](Arguments& args) { args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(2000.0)); }); } diff --git a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp index 212c8b55ad..902518ac29 100644 --- a/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/Log10ImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" @@ -79,7 +80,7 @@ constexpr float64 maximumIntegerLog10() TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: integer values produce float64 logarithms and domain results", "[NXImageProcessing][Issue1874][Log10ImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const auto values = [] { if constexpr(std::is_signed_v) @@ -139,7 +140,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: integer values produce // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: floating input retains type and IEEE domain behavior", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {1, 10, 100, 2, TestType{0.0}, -TestType{0.0}, -1, std::numeric_limits::quiet_NaN(), @@ -191,7 +192,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: floating input retains // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating boundaries have finite ordered logarithms", "[NXImageProcessing][Issue1874][Log10ImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = @@ -233,7 +234,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::Log10ImageFilter: positive floating bound // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::Log10ImageFilter: promoted Float64 output preserves tuple and component shapes", "[NXImageProcessing][Issue1874][Log10ImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; auto* imageGeomPtr = ImageGeom::Create(dataStructure, "Image Geometry"); diff --git a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp index 103b3af799..af9df39fd6 100644 --- a/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/LogImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -66,7 +67,7 @@ DataPath runMappedIntegerLog(DataStructure& dataStructure, const DataPath& input TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce float64 natural logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); constexpr int32 highestPower = std::numeric_limits::digits - 1; constexpr TestType highestPowerOfTwo = static_cast(TestType{1} << highestPower); @@ -116,7 +117,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: integer values produce fl // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = @@ -169,7 +170,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: floating input retains ty // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundaries retain finite logarithms", "[NXImageProcessing][Issue1874][LogImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::min(), std::numeric_limits::denorm_min(), @@ -201,7 +202,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::LogImageFilter: positive floating boundar // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::LogImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][LogImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 1, 0); diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index e43bfe679c..e441e4b3f0 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -446,7 +446,6 @@ std::vector MakeNegativeMarkerField(usize dx, usize dy, usize dz) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker conversion policy defaults to Error", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); const WSFilter filter; REQUIRE(filter.parametersVersion() == 2); const Parameters parameters = filter.parameters(); @@ -475,7 +474,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: mark TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: default Error rejects a changed uint64 marker before flooding", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const bool markWatershedLine = GENERATE(true, false); const bool fullyConnected = GENERATE(false, true); @@ -507,7 +506,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: defa TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT32_MAX marker remains valid with uint64 output", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const bool markWatershedLine = GENERATE(true, false); const bool fullyConnected = GENERATE(false, true); @@ -531,7 +530,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: UINT TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: native lossless seeds propagate exactly", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); const bool markWatershedLine = GENERATE(true, false); @@ -560,7 +559,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFil TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: wide marker bounds have literal legacy results", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); const bool markWatershedLine = GENERATE(true, false); @@ -579,7 +578,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFil TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alias and lost seed choices have exact flood consequences", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto handling = GENERATE(ChoicesParameter::ValueType{0}, ChoicesParameter::ValueType{1}); const bool markWatershedLine = GENERATE(true, false); @@ -593,7 +592,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: alia TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: marker copy checks chunk boundaries before writing", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize changedIndex = GENERATE(usize{65535}, usize{65536}, usize{65537}); const auto handling = GENERATE(ImageProcessing::MarkerIdConversionHandling::Error, ImageProcessing::MarkerIdConversionHandling::WarnAndContinueLegacy); @@ -636,7 +635,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: mark TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: invalid policy is rejected before execution", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; auto args = createSinglePixelMarkerArguments(dataStructure, 1, true, false); @@ -655,7 +654,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: inva TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: diagnostic context keeps exact 64-bit extrema", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; createMarkerLineArguments(dataStructure, {0, std::numeric_limits::max(), std::numeric_limits::max()}, true, false); @@ -690,7 +689,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFil TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy cancellation retains observed warning only", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); constexpr usize valueCount = 65537; const auto markerStorePtr = std::make_shared>(valueCount); @@ -719,7 +718,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy warning survives transfer failure or allocation failure", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); constexpr usize valueCount = 65537; const auto markerStorePtr = std::make_shared>(valueCount); @@ -764,7 +763,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conversion warning survives later facade outcomes", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const auto inputStorePtr = std::make_shared>(3); DataStructure dataStructure; @@ -826,7 +825,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conv TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: successful transfer warnings survive later marker-copy failure", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); constexpr usize valueCount = 65537; const auto markerStorePtr = std::make_shared>(valueCount); @@ -857,7 +856,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: succ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: restoration retains transfer warnings through a short final chunk", "[NXImageProcessing][Issue1874][MorphologicalWatershedFromMarkersImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); constexpr usize valueCount = 65537; DataStructure dataStructure; diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 5b4d135746..6e95b8af70 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -131,7 +131,6 @@ void RunCompactInt16Otsu(DataStructure& ds, const DataPath& inputPath) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { - UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; const Parameters parameters = filter.parameters(); const std::string policyKey = "nonfinite_input_handling_index"; @@ -151,7 +150,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: nonfinite polic // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask selection is optional and linked", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { - UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; const Parameters parameters = filter.parameters(); const std::string useMaskKey = "use_mask_array"; @@ -185,7 +183,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: explicit mask s // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: excluded label is active for mask or Warn", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { - UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; const Parameters parameters = filter.parameters(); const std::string maskedLabelKey = "masked_label"; @@ -210,7 +207,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: excluded label TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: preflight checks the complete configured uint8 class interval", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuSafeRed]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = BuildMultimodalImage(dataStructure, 2); @@ -433,7 +430,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn r TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn fits finite-only thresholds and emits the saved excluded label", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize rotation = GENERATE(usize{0}, usize{1}, usize{4}); const bool useMask = GENERATE(false, true); @@ -485,7 +482,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Warn fits finit // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error rejects each selected nonfinite kind", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const float64 value = GENERATE(std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()); DataStructure dataStructure; @@ -541,7 +538,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: Error finds the TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask precedes Error and accepts flat tuple count across parents", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", bool, uint8) { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = recon_test::BuildImageFromPattern( @@ -574,7 +571,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask p TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite data is required in both modes", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const uint64 policy = GENERATE(uint64{0}, uint64{1}); DataStructure dataStructure; @@ -595,7 +592,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask collision and tuple validation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 0, 4, 4}); @@ -647,7 +644,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool midpoint = GENERATE(false, true); DataStructure dataStructure; @@ -672,7 +669,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: full n // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: true constants and detectable numeric collapse are distinct", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; SECTION("true tiny constant is valid") @@ -785,7 +781,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: optional intege // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: bounded transfers preserve failures and cancellation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); std::atomic_bool shouldCancel{false}; OtsuTransferStore input(8); OtsuTransferStore mask(8); @@ -1139,7 +1134,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: ITK real-image // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: compatible pipeline imports receive intentional defaults", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); const OtsuMultipleThresholdsImageFilter filter; auto json = filter.toJson(filter.getDefaultArguments()); json.erase("nonfinite_input_handling_index"); @@ -1203,7 +1197,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: StringArray mas TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection validates scalar bool or uint8 and ignores disabled paths", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 2, 1, 1, {0, 4}); @@ -1244,7 +1238,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: mask selection TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants exclude nonfinite pixels and empty selected data errors", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; const ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2, .LabelOffset = 7, .WarnOnNonfinite = true}; DataStore input({4}, {1}, 42.0); @@ -1276,7 +1269,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: constants exclu // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper configuration errors are actionable", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; ImageProcessing::OtsuImageOptions options{.NumberOfBins = 2}; DataStore input({2}, {1}, 0.0); @@ -1331,7 +1323,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: checked helper TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal class labels coexist with masked label zero", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 4, std::numeric_limits::quiet_NaN()}); @@ -1353,7 +1345,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: highest legal c TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds preserve finite classes and exclude nonfinite pixels", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = recon_test::BuildImageFromPattern( @@ -1382,7 +1374,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: two thresholds TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid may equal minimum with usable two-bin separation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter][OtsuCentroidSafeRed]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool midpoint = GENERATE(false, true); REQUIRE(std::numeric_limits::is_iec559); @@ -1430,7 +1422,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: first centroid TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summaries preserve literal counts across resident and buffered batches", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); const bool excludeNonfinite = GENERATE(false, true); const usize targetBytes = GENERATE(usize{4096}, usize{64} * 1024 * 1024); @@ -1486,7 +1477,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summar // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels preserve class measurements", "[NXImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const bool useMask = GENERATE(false, true); diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 5f13bdb9d2..1fe85579bd 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -11,6 +11,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -209,7 +210,7 @@ std::vector ReadArray(const DataStructure& ds, const DataPath& path) TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active sigma is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize dimZ = GENERATE(usize{1}, usize{4}); const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; @@ -230,7 +231,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite a TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize dimZ = GENERATE(usize{1}, usize{4}); const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; @@ -254,7 +255,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite a TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize dimZ = GENERATE(usize{1}, usize{4}); const std::vector activeAxes = (dimZ == 1) ? std::vector{0, 1} : std::vector{0, 1, 2}; @@ -280,7 +281,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid eff TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: finite negative spacing and unused Z remain accepted", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const float64 unusedSigma = GENERATE(0.0, -1.0, std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); const float32 unusedSpacing = GENERATE(0.0F, -1.0F, std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); @@ -300,7 +301,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: finite nega // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: each selected validation branch has a diagnostic", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize failure = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); const std::array expectedCodes{ImageProcessing::k_SmoothingGaussianInvalidSigma, ImageProcessing::k_SmoothingGaussianInvalidSpacing, ImageProcessing::k_SmoothingGaussianInvalidScale, @@ -346,7 +347,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: each select TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: constants retain type shape and selected endpoint behavior", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int8, int16, int32, int64, float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope algorithmScope(scenario); const usize dimZ = GENERATE(usize{1}, usize{4}); @@ -398,7 +398,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: co // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 output retains float32 interpass precision", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope algorithmScope(scenario); DataStructure dataStructure; @@ -424,7 +423,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: float64 out TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and buffered terminal conversion follow literal expectations", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); const std::array input{93.75F, 127.75F, 128.0F, -128.75F, -129.0F}; const std::array expected{93, 127, 127, -128, -128}; @@ -446,7 +444,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: direct and TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: floating terminal values retain nonfinite behavior", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); DataStore source(ShapeType{4}, ShapeType{1}, 93.75F); source.setValue(1, std::numeric_limits::infinity()); @@ -473,7 +470,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: fl TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: terminal nonfinite integer errors and binary64 boundaries", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]", int32, int64) { - UnitTest::LoadPlugins(); const bool buffered = GENERATE(false, true); const bool nonfinite = GENERATE(false, true); const float32 invalidValue = GENERATE(std::numeric_limits::infinity(), -std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN()); @@ -506,7 +502,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: te TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping warning survives later transfer failure and cancellation", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const usize outcome = GENERATE(usize{0}, usize{1}, usize{2}); std::atomic_bool shouldCancel{false}; const SmoothingTransferStore source(8, 128.0F, {.ReadAt = outcome == 0 ? usize{2} : usize{0}}); @@ -536,7 +531,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: clipping wa TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected plane sinks retain successful warnings before error or cancellation", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const SmoothingTransferStore source(64, 0.0F); std::atomic_bool shouldCancel{false}; const bool cancel = GENERATE(false, true); @@ -575,7 +569,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected pl TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected tiled transfers keep prior row warnings on failure", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const bool write = GENERATE(false, true); SmoothingTransferStore store(8, 0.0F, {.ReadAt = write ? usize{0} : usize{2}, .WriteAt = write ? usize{2} : usize{0}}); std::array values{1.0F, 2.0F}; @@ -590,7 +583,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: selected ti TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: checked axis materializations narrow finite overflow explicitly", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const uint32 axis = GENERATE(uint32{0}, uint32{1}, uint32{2}); const bool fast = GENERATE(false, true); const DataStore source(ShapeType{5, 5, 5}, ShapeType{1}, 2.0 * static_cast(std::numeric_limits::max())); @@ -799,7 +791,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: FromSIMPLJson", "[NXImageProcessing][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const nlohmann::json json = { {"Sigma", {{"x", 2.0}, {"y", 3.0}, {"z", 4.0}}}, {"NormalizeAcrossScale", true}, @@ -1018,7 +1009,6 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: ITK real-im TEST_CASE("NXImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", "[NXImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const bool step = GENERATE(false, true); diff --git a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp index 05f4285ca8..de20faca47 100644 --- a/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SqrtImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AbstractDataStore.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -65,7 +66,7 @@ DataPath runMappedIntegerSqrt(DataStructure& dataStructure, const DataPath& inpu TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: integer values produce float64 square roots", "[NXImageProcessing][Issue1874][SqrtImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); const auto values = [] { if constexpr(std::is_signed_v) @@ -141,7 +142,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: integer values produce f // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating input retains type and IEEE domain results", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildScalarValuesImage( @@ -197,7 +198,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating input retains t // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating boundary roots match independent binary values", "[NXImageProcessing][Issue1874][SqrtImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); constexpr TestType smallestNormal = std::numeric_limits::min(); constexpr TestType infinity = std::numeric_limits::infinity(); @@ -253,7 +254,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SqrtImageFilter: floating boundary roots // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SqrtImageFilter: mapped multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SqrtImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath input = ip_test::BuildRampImage(dataStructure, 2, 0, 0); diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 1202c8daef..6e54dbde69 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -10,6 +10,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" @@ -153,7 +154,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: smaller integer types widen for TEST_CASE("NXImageProcessing::SquareImageFilter: retained 64-bit integers preserve low bits at safe bounds", "[NXImageProcessing][Issue1874][SquareImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); SECTION("int64 signed bounds") { @@ -169,7 +170,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: retained 64-bit integers preser TEST_CASE("NXImageProcessing::SquareImageFilter: 64-bit overflow inputs return actionable errors", "[NXImageProcessing][Issue1874][SquareImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); SECTION("Signed minimum") { @@ -199,7 +200,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: 64-bit overflow inputs return a TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: empty 64-bit inputs retain empty output", "[NXImageProcessing][Issue1874][SquareImageFilter]", int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); requireSquareValues({}, {}); } @@ -207,7 +208,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: empty 64-bit inputs re // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SquareImageFilter: cancellation preserves the filter cancellation result", "[NXImageProcessing][Issue1874][SquareImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::min()}); @@ -228,7 +229,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: cancellation preserves the filt // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::SquareImageFilter: widened multi-component output preserves tuple shape", "[NXImageProcessing][Issue1874][SquareImageFilter]") { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = ip_test::BuildRampImage(dataStructure, 2, 0, 0); @@ -259,7 +260,7 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: widened multi-component output // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating inputs retain type and safe IEEE values", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); const TestType infinity = std::numeric_limits::infinity(); DataStructure dataStructure; @@ -284,7 +285,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating inputs retain // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEMPLATE_TEST_CASE("NXImageProcessing::SquareImageFilter: floating overflow is positive infinity and underflow is positive zero", "[NXImageProcessing][Issue1874][SquareImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const TestType maximum = std::numeric_limits::max(); @@ -342,7 +343,6 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: ITK real-image golden (defaults // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::C15Compatibility resident widened Square persists and feeds integer components", "[NXImageProcessing][C15Compatibility][Issue1874][SquareImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const c15_test::Directory output; diff --git a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp index 3e6a69de51..447d486d6a 100644 --- a/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/TanImageFilterTest.cpp @@ -7,6 +7,7 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" +#include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" @@ -35,7 +36,7 @@ using namespace nx::core; TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: integer radians produce signed fractional float32 responses", "[NXImageProcessing][Issue1874][TanImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); std::vector values = {0, 1, 3}; @@ -108,7 +109,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: integer radians produce s // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::TanImageFilter: floating responses retain type signed zero and finite near-pole values", "[NXImageProcessing][Issue1874][TanImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(nx::core::DataStorageMode::ForceInCore, 0); DataStructure dataStructure; diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index a742ca714b..e6f708704f 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -234,7 +234,6 @@ class ThresholdTransferStore : public DataStore TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") { - UnitTest::LoadPlugins(); const ThresholdMaximumConnectedComponentsImageFilter filter; const Parameters parameters = filter.parameters(); const std::string policyKey = "nonfinite_input_handling_index"; @@ -256,7 +255,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: initial midpoint includes the finite image minimum", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][ThresholdSafeRed]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel prefsSentinel(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, std::vector{100, 102}); @@ -295,7 +294,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: in TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native integer endpoints retain exact width-two masks", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool nearMaximum = GENERATE(false, true); const TestType low = nearMaximum ? static_cast(std::numeric_limits::max() - TestType{2}) : std::numeric_limits::lowest(); @@ -314,7 +313,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: full native integer spans keep the absolute-two heuristic", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", int8, uint8, int16, uint16, int32, uint32, int64, uint64) { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {std::numeric_limits::lowest(), std::numeric_limits::max()}); @@ -331,7 +330,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: caps clamp before 64-bit conversion and preserve lower truncation", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); SECTION("signed exclusive high and finite overflow") { @@ -382,7 +381,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ca TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: low and empty bands return warned Outside values", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const float64 cap = GENERATE(-1.0, -std::numeric_limits::infinity(), 99.0); const uint8 outside = GENERATE(uint8{0}, uint8{7}); @@ -401,7 +400,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: lo TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: NaN Upper Boundary is a hard parameter error in both image modes", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool warn = GENERATE(false, true); DataStructure dataStructure; @@ -424,7 +423,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: Na TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: one image policy covers every nonfinite class and order", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const usize placement = GENERATE(usize{0}, usize{1}, usize{2}); const bool warn = GENERATE(false, true); @@ -485,7 +484,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: Er TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: all nonfinite Warn skips search and calculates no threshold", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const int32 kind = GENERATE(0, 1, 2, 3); const uint8 outside = GENERATE(uint8{0}, uint8{7}); @@ -509,7 +508,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: al TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: floating rounding distinguishes convergence from a repeated wide state", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const bool roundUp = GENERATE(false, true); DataStructure dataStructure; @@ -531,7 +530,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: fl TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: opposite finite floating extrema keep finite search candidates", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]", float32, float64) { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const auto inputPath = rt::BuildImageFromPattern(dataStructure, 2, 1, 1, {-std::numeric_limits::max(), std::numeric_limits::max()}); @@ -549,7 +548,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageF TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: nonzero scoring minimum sizes and the fixed scale stop are preserved", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const ThresholdMaximumConnectedComponentsImageFilter filter; @@ -617,7 +616,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: old JSON receives Error and version two preserves explicit Warn", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const ThresholdMaximumConnectedComponentsImageFilter filter; REQUIRE(filter.parametersVersion() == 2); auto json = filter.toJson(filter.getDefaultArguments()); @@ -640,7 +638,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ol TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine rejects image nonfinites before empty-cap output and distinguishes empty input", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const std::atomic_bool shouldCancel{false}; const float64 nonfinite = GENERATE(std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()); const ThresholdTransferStore input({0, nonfinite, 2}); @@ -666,7 +663,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: en TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no-finite fallback avoids count and reread passes and retains warnings on write failure", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const bool failWrite = GENERATE(false, true); const ThresholdTransferStore input({std::numeric_limits::quiet_NaN(), -std::numeric_limits::infinity(), std::numeric_limits::infinity()}); ThresholdTransferStore output({99, 99, 99}); @@ -700,7 +696,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: no TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: count failures remain strict under image Warn", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const auto fault = GENERATE(ThresholdFaultType::ReadError, ThresholdFaultType::BadAllocation, ThresholdFaultType::LengthError); ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 4}); input.setReadFault(2, fault); // Initial summary succeeds; the first component-count read fails. @@ -729,7 +724,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: summary and final transfers preserve failures and collected diagnostics", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const int32 stage = GENERATE(0, 1, 2, 3, 4, 5, 6); ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); ThresholdTransferStore output({99, 99, 99}); @@ -792,7 +786,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: su TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: outer exceptions preserve collected warnings before output", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const bool allocationFailure = GENERATE(false, true); const ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); ThresholdTransferStore output({99, 99, 99}); @@ -824,7 +817,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ou TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: engine cancellation keeps warning Results without inventing a local cancellation error", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const bool cancelBefore = GENERATE(false, true); std::atomic_bool shouldCancel{cancelBefore}; const ThresholdTransferStore input({0, std::numeric_limits::quiet_NaN(), 2}); @@ -848,7 +840,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: en TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: direct engine validates output shape before transfer", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); const ThresholdTransferStore input({std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()}); ThresholdTransferStore output({99}); const std::atomic_bool shouldCancel{false}; @@ -865,7 +856,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: di TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: resident and owned multi-block summaries use finite native extrema", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); constexpr usize count = 131073; std::vector values(count, 0); values.at(1) = 2; @@ -903,7 +893,6 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: re TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: native span and cap math retain exact bounds and ordinary floating rounding", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(std::numeric_limits::lowest(), std::numeric_limits::max())); REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(std::numeric_limits::lowest(), std::numeric_limits::max())); REQUIRE(ImageProcessing::detail::ThresholdMaxCCSpanExceedsTwo(-std::numeric_limits::max(), std::numeric_limits::max())); @@ -975,13 +964,11 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: pr // ----------------------------------------------------------------------------- TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ITK real-image golden (default)", "[NXImageProcessing][ItkGolden][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("cthead1.png", "c84b75c78c33844251a1095d9cbcffb9"); } TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: ITK real-image golden (parameters)", "[NXImageProcessing][ItkGolden][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); rt::RunReconstructionMd5ItkGolden("cthead1.png", "27c6cf8494fcc4e414f1c420e7a9ca6f", [](Arguments& args) { args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(40u)); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(150.0)); @@ -992,7 +979,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: IT TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: corrected-midpoint real-image regression (float)", "[NXImageProcessing][Issue1874][ThresholdMaximumConnectedComponentsImageFilter][MidpointMigration]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); DataStructure dataStructure; const DataPath geometryPath({"Image Geometry"}); @@ -1069,7 +1056,7 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", "[NXImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { - UnitTest::LoadPlugins(); + (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 5a231e3905..588114bb88 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -364,7 +364,6 @@ IFilter::PreflightResult RunFormatPreflightForType(const std::string& fileName, // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is explicit and defaults to Error", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") { - UnitTest::LoadPlugins(); const WriteImageFilter filter; const Parameters parameters = filter.parameters(); const std::string policyKey = "nonfinite_input_handling_index"; @@ -384,7 +383,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is explicit and // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: nonfinite policy is active only for color-table output", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageSafeRed]") { - UnitTest::LoadPlugins(); const WriteImageFilter filter; const Parameters parameters = filter.parameters(); const std::string policyKey = "nonfinite_input_handling_index"; @@ -589,7 +587,6 @@ class WriteImageReadProbeStore : public ExtentCountingDataStore TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: native endpoints decode to literal RGB", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]", int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); requireWriteImageGray(scope, std::array{std::numeric_limits::lowest(), std::numeric_limits::max()}, std::array{0, 255}); @@ -597,7 +594,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: native endpoints decode TEST_CASE("NXImageProcessing::WriteImageFilter: wide and tiny interiors decode without native range collapse", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const std::array expected{0, 127, 255}; @@ -621,7 +617,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: wide and tiny interiors decode w // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a distinct runtime witness", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const float64 tiny = std::numeric_limits::denorm_min(); @@ -637,7 +632,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: subnormal double interior has a TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite masked range decode literally", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool booleanMask = GENERATE(false, true); const uint64 policy = GENERATE(uint64{0}, uint64{1}); @@ -668,7 +662,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: mask first nonfinite and finite // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: integer color reads masks only for slices", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); CAPTURE(scenario); UnitTest::AlgorithmTestScope scope(scenario); @@ -712,7 +705,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: integer color reads masks only f // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the first actual control color", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool useMask = GENERATE(false, true); UnitTest::AlgorithmTestScope scope(scenario); @@ -742,7 +734,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: Warn returns one count and the f // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: late strict nonfinite preserves every existing slice byte", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const usize invalidKind = GENERATE(usize{0}, usize{1}, usize{2}); UnitTest::AlgorithmTestScope scope(scenario); @@ -778,7 +769,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: late strict nonfinite preserves // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: no finite color range errors in either mode", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const uint64 policy = GENERATE(uint64{0}, uint64{1}); const bool allMasked = GENERATE(false, true); @@ -799,7 +789,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: no finite color range errors in TEST_CASE("NXImageProcessing::WriteImageFilter: finite constants and all finite masked values stay valid", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool allMasked = GENERATE(false, true); UnitTest::AlgorithmTestScope scope(scenario); @@ -820,7 +809,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: finite constants and all finite TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal layouts in every plane", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const uint64 plane = GENERATE(uint64{0}, uint64{1}, uint64{2}); UnitTest::AlgorithmTestScope scope(scenario); @@ -852,7 +840,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: policy output retains literal la // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: raw floating pixels ignore color policy and mask", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]", float32, float64) { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const uint64 policy = GENERATE(uint64{0}, uint64{1}); const bool useMask = GENERATE(false, true); @@ -915,7 +902,6 @@ TEMPLATE_TEST_CASE("NXImageProcessing::WriteImageFilter: raw floating pixels ign TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan opens no slice", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); DataStructure dataStructure; @@ -942,7 +928,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: cancellation during color scan o // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards use actionable results", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); constexpr usize maximum = std::numeric_limits::max(); const auto empty = write_image_color::ValidateSize({.Width = 0, .Height = 2, .Count = 3}, false); SIMPLNX_RESULT_REQUIRE_INVALID(empty); @@ -973,7 +958,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: color size and palette guards us // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: page and slice IO failures preserve diagnostics and warnings", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const usize failure = GENERATE(usize{0}, usize{1}, usize{2}); UnitTest::AlgorithmTestScope scope(scenario); @@ -1016,7 +1000,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: page and slice IO failures prese // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: late nonfinite scan uses bounded scalar and mask pages", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); const bool useMask = GENERATE(false, true); UnitTest::AlgorithmTestScope scope(scenario); @@ -1129,7 +1112,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: raw export retains warnings from // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::WriteImageFilter: later commit failure keeps earlier slice and policy warning", "[NXImageProcessing][Issue1874][WriteImageFilter][WriteImageNumeric]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); DataStructure dataStructure; @@ -2975,7 +2957,6 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: rejects a source-group grant sma // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[NXImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") { - UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const usize operation = GENERATE(usize{0}, usize{1}, usize{2}); From 4dc3727fc977b564b626ed9998709246c8f68deb Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 17 Nov 2026 14:33:38 -0500 Subject: [PATCH 59/98] REV: Validate the final Otsu mask size and install the operation types Allow upstream filters to resize an Otsu mask before its final count is checked. Cover pipeline success and reject a final mismatch before store reads. Install the threshold engine dependency and document the Otsu solver return contract. Signed-off-by: Jessica Marquis --- CMakeLists.txt | 1 + .../docs/OtsuMultipleThresholdsImageFilter.md | 2 +- .../OtsuMultipleThresholdsImageFilter.cpp | 24 +++-- .../OtsuMultipleThresholdsImageFilterTest.cpp | 93 +++++++++++++++++-- .../ImageProcessing/HistogramEngine.hpp | 19 ++-- 5 files changed, 111 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fcfe7d456..3942d9ee9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -611,6 +611,7 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/OtsuImageEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ConnectedComponentEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/StreamingStatistics.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/SweepTemporaryStore.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/HistogramEngine.hpp diff --git a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md index a4d5ab43a2..4dd0bcac31 100644 --- a/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OtsuMultipleThresholdsImageFilter.md @@ -25,7 +25,7 @@ Both choices require at least one selected finite sample. Empty data, an all-fal ### Explicit mask and excluded labels -Enable **Use Mask Array** to select a **Mask Array**. The mask must be scalar `bool` or `uint8` and have the same tuple count as the input. Tuples are paired in flat order; the mask can have a different tuple shape or parent group. False/zero excludes a pixel; true or any nonzero `uint8` value selects it. The filter applies this selection before range, histogram, and nonfinite checks. An excluded NaN or infinity therefore causes no nonfinite error or warning. +Enable **Use Mask Array** to select a **Mask Array**. The mask must be scalar `bool` or `uint8`. Its final tuple count must match the input when Otsu runs, after any upstream filters have resized their arrays. Tuples are paired in flat order, so the mask can have a different tuple shape or parent group. False/zero excludes a pixel; true or any nonzero `uint8` value selects it. The filter applies this selection before range, histogram, and nonfinite checks. An excluded NaN or infinity therefore causes no nonfinite error or warning. Pixels excluded by the mask and nonfinite pixels skipped in Warn mode use the same **Excluded Label**. It defaults to 255 and must be outside the configured class interval when a mask is enabled or Warn is used with floating-point input. This leaves up to 255 ordinary classes. Choose an unused label or reduce the class interval if the label collides. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index f4a1149961..313f42e665 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -159,12 +159,6 @@ IFilter::PreflightResult OtsuMultipleThresholdsImageFilter::preflightImpl(const return {.outputActions = MakeErrorResult(invalidMaskArrayKind, fmt::format("Mask Array '{}' is not a numeric data array. Select a scalar bool or uint8 mask array.", maskPath.toString()))}; } - if(maskPtr->getNumberOfTuples() != inputArray.getNumberOfTuples()) - { - return {.outputActions = MakeErrorResult(k_OtsuMaskTupleMismatch, - fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", - maskPath.toString(), maskPtr->getNumberOfTuples(), selectedInputArray.toString(), inputArray.getNumberOfTuples()))}; - } } if(inputArray.getNumberOfComponents() != 1) @@ -193,9 +187,23 @@ Result<> OtsuMultipleThresholdsImageFilter::executeImpl(DataStructure& dataStruc k_NonfiniteInputHandling_Key)) == ImageProcessing::NonfiniteInputHandling::WarnAndContinue, .ValleyEmphasis = filterArgs.value(k_ValleyEmphasis_Key), .ReturnBinMidpoint = filterArgs.value(k_ReturnBinMidpoint_Key)}; - const IDataArray* maskPtr = filterArgs.value(k_UseMaskArray_Key) ? &dataStructure.getDataRefAs(filterArgs.value(k_MaskArrayPath_Key)) : nullptr; + const auto& inputArray = dataStructure.getDataRefAs(selectedInputArray); + const IDataArray* maskPtr = nullptr; + if(filterArgs.value(k_UseMaskArray_Key)) + { + const auto maskPath = filterArgs.value(k_MaskArrayPath_Key); + maskPtr = &dataStructure.getDataRefAs(maskPath); + // Upstream filters can resize arrays during execution. Compare their final counts before reading either store. + const auto maskTuples = maskPtr->getNumberOfTuples(); + const auto inputTuples = inputArray.getNumberOfTuples(); + if(maskTuples != inputTuples) + { + return MakeErrorResult(k_OtsuMaskTupleMismatch, fmt::format("Mask Array '{}' has {} tuples, but input '{}' has {}. Select a scalar mask with one value per input tuple.", maskPath.toString(), + maskTuples, selectedInputArray.toString(), inputTuples)); + } + } // Dispatch is synchronous, so the filter arguments and local paths remain live. - return ImageProcessing::AllNumeric::dispatch(dataStructure.getDataRefAs(selectedInputArray).getDataType(), [&]() -> Result<> { + return ImageProcessing::AllNumeric::dispatch(inputArray.getDataType(), [&]() -> Result<> { const auto& input = dataStructure.getDataRefAs>(selectedInputArray).getDataStoreRef(); auto& output = dataStructure.getDataRefAs>(outputArrayPath).getDataStoreRef(); if(maskPtr != nullptr && maskPtr->getDataType() == DataType::boolean) diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index 6e95b8af70..adbc0829c1 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -6,6 +6,8 @@ #include "NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.hpp" +#include "SimplnxCore/Filters/ScalarSegmentFeaturesFilter.hpp" + #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" @@ -28,6 +30,7 @@ #include "simplnx/Parameters/BoolParameter.hpp" #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/NumberParameter.hpp" +#include "simplnx/Pipeline/Pipeline.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/CacheMemoryBudgetManager.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" @@ -48,6 +51,7 @@ #include #include #include +#include #include using namespace nx::core; @@ -590,7 +594,7 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: selected finite UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask collision and tuple validation", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask label collisions fail preflight", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") { (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -598,7 +602,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 0, 4, 4}); auto args = otsuArguments(inputPath); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); - int32 expectedCode = -28907; SECTION("unused interior class is still reserved") { args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(3)); @@ -616,11 +619,6 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskedLabel_Key, std::make_any(8)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1, 1}))); } - SECTION("different tuple count") - { - expectedCode = -28908; - args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(createOtsuMask(dataStructure, std::array{1, 1, 1}))); - } SECTION("full unmasked interval leaves no masked label") { args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(256)); @@ -633,13 +631,90 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: configured mask // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.outputActions); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. - REQUIRE(result.outputActions.errors().front().code == expectedCode); + REQUIRE(result.outputActions.errors().front().code == -28907); const auto execution = filter.execute(dataStructure, args); REQUIRE(execution.result.invalid()); - REQUIRE(execution.result.errors().front().code == expectedCode); + // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. + REQUIRE(execution.result.errors().front().code == -28907); UnitTest::CheckArraysInheritTupleDims(dataStructure); } +// NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: pipeline accepts a mask resized by an upstream filter", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + (void)Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 3, 1, 1, {0, 0, 4}); + const auto geometryPath = inputPath.getParent().getParent(); + const auto maskPath = geometryPath.createChildPath("Features").createChildPath("Active"); + auto segmenter = std::make_unique(); + auto segmentArgs = segmenter->getDefaultArguments(); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_GridGeomPath_Key, std::make_any(geometryPath)); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_InputArrayPathKey, std::make_any(inputPath)); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_ScalarToleranceKey, std::make_any(0)); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_UseMask_Key, std::make_any(false)); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_RandomizeFeatures_Key, std::make_any(false)); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_CellFeatureName_Key, std::make_any("Features")); + segmentArgs.insertOrAssign(ScalarSegmentFeaturesFilter::k_ActiveArrayName_Key, std::make_any("Active")); + + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + Pipeline pipeline; + REQUIRE(pipeline.push_back(std::move(segmenter), segmentArgs)); + REQUIRE(pipeline.push_back(std::make_unique(), args)); + const std::atomic_bool cancel = false; + DataStructure preflightData = dataStructure; + REQUIRE(pipeline.preflight(preflightData, cancel)); + const UInt8Array* placeholderPtr = nullptr; + REQUIRE_NOTHROW(placeholderPtr = &preflightData.getDataRefAs(maskPath)); + REQUIRE(placeholderPtr->getNumberOfTuples() == 1); + + REQUIRE(pipeline.execute(dataStructure, cancel)); + const UInt8Array* maskPtr = nullptr; + REQUIRE_NOTHROW(maskPtr = &dataStructure.getDataRefAs(maskPath)); + REQUIRE(maskPtr->getNumberOfTuples() == 3); + std::array actualMask{}; + const auto readMask = maskPtr->getDataStoreRef().copyIntoBuffer(0, actualMask); + SIMPLNX_RESULT_REQUIRE_VALID(readMask); + REQUIRE(actualMask == std::array{0, 1, 1}); + requireOtsuLabels(dataStructure, inputPath, {255, 7, 8}); +} + +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: final mask tuple mismatch fails before store reads", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]") +{ + (void)Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + DataStructure dataStructure; + const auto inputPath = recon_test::BuildImageFromPattern(dataStructure, 4, 1, 1, {0, 0, 4, 4}); + auto inputStorePtr = std::make_shared>(4, IDataStore::StoreType::OutOfCore); + Float32Array* inputPtr = nullptr; + REQUIRE_NOTHROW(inputPtr = &dataStructure.getDataRefAs(inputPath)); + const auto resize = inputStorePtr->resizeTuples({1, 1, 4}); + SIMPLNX_RESULT_REQUIRE_VALID(resize); + const auto setStore = inputPtr->setDataStore(inputStorePtr); + SIMPLNX_RESULT_REQUIRE_VALID(setStore); + auto maskStorePtr = std::make_shared>(3, IDataStore::StoreType::OutOfCore); + const DataPath maskPath({"Mask"}); + REQUIRE(UInt8Array::Create(dataStructure, maskPath.getTargetName(), maskStorePtr) != nullptr); + + auto args = otsuArguments(inputPath); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); + args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(maskPath)); + const OtsuMultipleThresholdsImageFilter filter; + const auto preflight = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == -28908); + REQUIRE(result.result.errors().front().message.find("has 3 tuples") != std::string::npos); + REQUIRE(inputStorePtr->reads() == 0); + REQUIRE(maskStorePtr->reads() == 0); + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +// NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) + // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. TEMPLATE_TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: full native integer spans retain a valid two-bin split", "[NXImageProcessing][Issue1874][OtsuMultipleThresholdsImageFilter]", int64, uint64) diff --git a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp index 311c76073b..6ee291cf3b 100644 --- a/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/HistogramEngine.hpp @@ -395,15 +395,6 @@ inline bool FloatAlmostEqual(float64 x1, float64 x2, int64 maxUlps = 1) } } // namespace detail -/** - * @brief Bin-space multi-Otsu thresholding, a direct port of itk::OtsuMultipleThresholdsCalculator::Compute() - * (itkOtsuMultipleThresholdsCalculator.hxx, ITK v5.4.4) adapted to operate on the Histogram struct. - * - * Per-bin measurement value is the bin centroid/midpoint (min + (bin+0.5)*binWidth), matching ITK's - * Histogram::GetMeasurementVector(bin)[0]. Returns @p numThresholds threshold values in input-intensity units. - * Bin->value conversion uses the bin maximum (min + (bin+1)*binWidth), matching ITK's GetMaxs(), unless - * @p returnBinMidpoint is set, in which case the bin midpoint (min + (bin+0.5)*binWidth) is used. - */ namespace detail { struct OtsuClassStatistics @@ -696,7 +687,15 @@ Result> ComputeOtsuThresholds(const Histogram& histogram, u } } // namespace detail -/** @brief Retains the established unchecked solver contract for existing direct consumers. */ +/** + * @brief Fits multiple Otsu thresholds using bin midpoints as measurements, as in ITK v5.4.4. + * @param histogram Binned counts and their intensity range. + * @param numThresholds Number of thresholds to fit. + * @param valleyEmphasis Give more weight to thresholds in bins with fewer samples. + * @param returnBinMidpoint Return each selected bin's midpoint instead of its upper edge. + * @return `numThresholds` values in input-intensity units. Returns an empty vector for zero thresholds, + * too few bins, or a histogram with no samples. + */ inline std::vector OtsuMultipleThresholds(const Histogram& histogram, usize numThresholds, bool valleyEmphasis, bool returnBinMidpoint) { auto result = detail::ComputeOtsuThresholds(histogram, numThresholds, valleyEmphasis, returnBinMidpoint, nullptr); From 28474442ed8eb8f8d0bf1dda9984d72fc59f2c6b Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Tue, 17 Nov 2026 16:50:28 -0500 Subject: [PATCH 60/98] REV: Address review findings for ImageProcessing numeric fixes * Validate Gaussian spacing and coefficients against the final geometry. * Make the installed ImageProcessing operation types header self-contained. * Report Curvature Flow completion after checked conversion succeeds. * Clarify Otsu and Threshold Max CC behavior and remove an unused Otsu include. Signed-off-by: Jessica Marquis --- .../OtsuMultipleThresholdsImageFilter.cpp | 2 - .../SmoothingRecursiveGaussianImageFilter.cpp | 3 +- .../test/CurvatureFlowImageFilterTest.cpp | 10 +- ...othingRecursiveGaussianImageFilterTest.cpp | 129 +++++++++++++++--- .../FiniteDifferenceEngine.hpp | 10 +- .../ImageProcessingOperationTypes.hpp | 2 + .../ImageProcessing/OtsuImageEngine.hpp | 8 +- .../RecursiveGaussianEngine.hpp | 23 +++- ...esholdMaximumConnectedComponentsEngine.hpp | 2 + 9 files changed, 159 insertions(+), 30 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp index 313f42e665..ce0e374aec 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/OtsuMultipleThresholdsImageFilter.cpp @@ -1,7 +1,5 @@ #include "OtsuMultipleThresholdsImageFilter.hpp" -#include "NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp" - #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp index 29db2e1b85..0682398c3b 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/SmoothingRecursiveGaussianImageFilter.cpp @@ -97,7 +97,8 @@ IFilter::PreflightResult SmoothingRecursiveGaussianImageFilter::preflightImpl(co } auto sigma = filterArgs.value(k_Sigma_Key); - if(Result<> controls = ImageProcessing::detail::ValidateSmoothingGaussianControls(sigma, imageGeom.getSpacing(), imageGeom.getDimensions()); controls.invalid()) + // The executor validates spacing and coefficients after upstream filters set the final spacing. + if(Result<> controls = ImageProcessing::detail::ValidateSmoothingGaussianSigma(sigma, imageGeom.getDimensions()); controls.invalid()) { controls.errors().front().message += fmt::format(" Image Geometry '{}'.", imageGeomPath.toString()); return {.outputActions = ConvertResultTo(std::move(controls), OutputActions{})}; diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 88a687bf1a..6a99d39b50 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -642,8 +642,16 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: streamed and ping-pong t Result<> result; if(pingPong) { + bool reportedCompletion = false; + const IFilter::MessageHandler messageHandler{[&reportedCompletion](const IFilter::Message& message) { + if(message.type == IFilter::Message::Type::Progress && message.progress == 100 && message.message.starts_with("Finite Difference: Completed Iterations")) + { + reportedCompletion = true; + } + }}; result = ImageProcessing::ApplyFiniteDifference3DPingPong( - input, output, first, second, dims, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, {}); + input, output, first, second, dims, {1.0, 1.0, 1.0}, ImageProcessing::detail::CurvatureFlowFn{}, step, 1, shouldCancel, messageHandler); + REQUIRE(reportedCompletion == !nonFinite); } else { diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 1fe85579bd..06943ecc4c 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -7,22 +7,28 @@ #include "NXImageProcessing/utils/TerminalConversionDiagnostics.hpp" +#include "SimplnxCore/Filters/ResampleRectGridToImageGeomFilter.hpp" + #include "simplnx/Common/Aliases.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/DataStructure/DataStore.hpp" #include "simplnx/DataStructure/DataStructure.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" +#include "simplnx/DataStructure/Geometry/RectGridGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Filter/Arguments.hpp" #include "simplnx/Filter/IFilter.hpp" #include "simplnx/Parameters/VectorParameter.hpp" +#include "simplnx/Pipeline/Pipeline.hpp" +#include "simplnx/Pipeline/PipelineFilter.hpp" #include "simplnx/UnitTest/AlgorithmTestScope.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" @@ -229,7 +235,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite a UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected before execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite active spacing is rejected during execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); @@ -246,13 +252,15 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: nonfinite a spacing.at(axis) = invalidSpacing; dataStructure.getDataRefAs(geometryPath).setSpacing({spacing.at(0), spacing.at(1), spacing.at(2)}); const SmoothFilter filter; - const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, 1.0})); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. - SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + const auto result = filter.execute(dataStructure, makeSmoothingArguments(inputPath, {1.0, 1.0, 1.0})); + // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == ImageProcessing::k_SmoothingGaussianInvalidSpacing); + // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) UnitTest::CheckArraysInheritTupleDims(dataStructure); } -TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected before execution", +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid effective active scale is rejected during execution", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") { Application::GetOrCreateInstance(); @@ -272,10 +280,12 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: invalid eff std::vector sigma{1.0, 1.0, 1.0}; sigma.at(axis) = underflow ? std::numeric_limits::denorm_min() : std::numeric_limits::max(); const SmoothFilter filter; - const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); + const auto result = filter.execute(dataStructure, makeSmoothingArguments(inputPath, sigma)); // Positive finite operands can produce zero or infinity for sigma / abs(spacing). No cutoff on finite scales is imposed. - // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. - SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == ImageProcessing::k_SmoothingGaussianInvalidScale); + // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -336,10 +346,97 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: each select sigma.at(0) = std::numeric_limits::denorm_min(); } const SmoothFilter filter; - const auto preflight = filter.preflight(dataStructure, makeSmoothingArguments(inputPath, sigma)); - SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); - REQUIRE(preflight.outputActions.errors().front().code == expectedCodes.at(failure)); - REQUIRE(preflight.outputActions.errors().front().message.find("Image Geometry") != std::string::npos); + const auto args = makeSmoothingArguments(inputPath, sigma); + const auto preflight = filter.preflight(dataStructure, args); + if(failure == 0) + { + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + } + else + { + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + } + const auto result = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); + REQUIRE(result.result.errors().front().code == expectedCodes.at(failure)); + REQUIRE(result.result.errors().front().message.find("Image Geometry") != std::string::npos); +} + +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: pipeline validates spacing after resampling", "[NXImageProcessing][Issue1874][SmoothingRecursiveGaussianImageFilter]") +{ + Application::GetOrCreateInstance(); + const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); + constexpr float32 largeSpacing = 1.0e20F; + constexpr float32 signalValue = 7.0F; + const bool usableFinalSpacing = GENERATE(false, true); + const float32 finalSpacing = usableFinalSpacing ? largeSpacing : 1.0F; + const DataPath rectPath({"Rect Grid"}); + const DataPath imagePath({"Resampled Image"}); + const DataPath inputPath = imagePath.createChildPath("Cell Data").createChildPath("Signal"); + const ShapeType cellShape{1, 4, 4}; + const SizeVec3 dimensions{4, 4, 1}; + DataStructure dataStructure; + + auto* rectGrid = RectGridGeom::Create(dataStructure, rectPath.getTargetName()); + REQUIRE(rectGrid != nullptr); + rectGrid->setDimensions(dimensions); + auto* cellData = AttributeMatrix::Create(dataStructure, "Cell Data", cellShape, rectGrid->getId()); + REQUIRE(cellData != nullptr); + rectGrid->setCellData(*cellData); + const std::array boundNames{"X Bounds", "Y Bounds", "Z Bounds"}; + std::array bounds{}; + for(usize axis = 0; axis < bounds.size(); ++axis) + { + bounds[axis] = Float32Array::Create(dataStructure, boundNames[axis], std::make_unique>(dimensions[axis] + 1, 0.0F), rectGrid->getId()); + REQUIRE(bounds[axis] != nullptr); + const float32 axisSpacing = axis < 2 ? finalSpacing : 1.0F; + for(usize index = 0; index <= dimensions[axis]; ++index) + { + bounds[axis]->setValue(index, static_cast(index) * axisSpacing); + } + } + const auto boundsResult = rectGrid->setBounds(bounds[0], bounds[1], bounds[2]); + SIMPLNX_RESULT_REQUIRE_VALID(boundsResult); + REQUIRE(Float32Array::Create(dataStructure, "Signal", std::make_unique>(cellShape, ShapeType{1}, signalValue), cellData->getId()) != nullptr); + + using ResampleFilter = ResampleRectGridToImageGeomFilter; + auto resample = std::make_unique(); + auto resampleArgs = resample->getDefaultArguments(); + resampleArgs.insertOrAssign(ResampleFilter::k_RectilinearGridPath_Key, rectPath); + resampleArgs.insertOrAssign(ResampleFilter::k_ImageGeometryPath_Key, imagePath); + resampleArgs.insertOrAssign(ResampleFilter::k_ImageGeomCellAttributeMatrixName_Key, std::string("Cell Data")); + resampleArgs.insertOrAssign(ResampleFilter::k_Dimensions_Key, std::vector{4, 4, 1}); + resampleArgs.insertOrAssign(ResampleFilter::k_SelectedDataArrayPaths_Key, std::vector{rectPath.createChildPath("Cell Data").createChildPath("Signal")}); + + Pipeline pipeline; + REQUIRE(pipeline.push_back(std::move(resample), resampleArgs)); + REQUIRE(pipeline.push_back(std::make_unique(), makeSmoothingArguments(inputPath, {largeSpacing, largeSpacing, 1.0}))); + const std::atomic_bool shouldCancel{false}; + auto preflightData = dataStructure; + REQUIRE(pipeline.preflight(preflightData, shouldCancel)); + REQUIRE(preflightData.getDataRefAs(imagePath).getSpacing() == FloatVec3{1.0F, 1.0F, 1.0F}); + + const bool executed = pipeline.execute(dataStructure, shouldCancel); + REQUIRE(dataStructure.getDataRefAs(imagePath).getSpacing() == FloatVec3{finalSpacing, finalSpacing, 1.0F}); + if(usableFinalSpacing) + { + REQUIRE(executed); + const auto& output = dataStructure.getDataRefAs(inputPath.replaceName("Output")); + REQUIRE(output.getSize() == 16); + for(usize index = 0; index < output.getSize(); ++index) + { + REQUIRE(output[index] == Approx(signalValue)); + } + } + else + { + REQUIRE_FALSE(executed); + const auto* gaussian = dynamic_cast(pipeline.at(1)); + REQUIRE(gaussian != nullptr); + const auto errors = gaussian->getErrors(); + REQUIRE(errors.size() == 1); + REQUIRE(errors.front().code == ImageProcessing::k_SmoothingGaussianInvalidCoefficients); + } } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -764,7 +861,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSigma); } - SECTION("degenerate spacing rejected") + SECTION("degenerate spacing rejected during execution") { DataStructure ds; const std::vector field(8 * 8 * 8, int16{1}); // valid dims/type/sigma; only the X spacing is degenerate @@ -778,10 +875,10 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_OutputImageArrayName_Key, std::make_any("Output")); args.insertOrAssign(SmoothFilter::k_Sigma_Key, std::make_any>(std::vector{1.0, 1.0, 1.0})); args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); - const auto preflightResult = filter.preflight(ds, args); - SIMPLNX_RESULT_REQUIRE_INVALID(preflightResult.outputActions); + const auto result = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. - REQUIRE(preflightResult.outputActions.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSpacing); + REQUIRE(result.result.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSpacing); } } diff --git a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp index 2ba79780ae..dd4367317f 100644 --- a/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp @@ -1940,11 +1940,12 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab } if(finalIteration) { - if(!shouldCancel) + auto result = finish(); + if(result.valid() && !shouldCancel) { throttleProgress(progressThrottle, iter, numberOfIterations); } - return finish(); + return result; } currentStore = &firstStore; nextStore = &secondStore; @@ -1966,11 +1967,12 @@ Result<> ApplyFiniteDifference3DPingPong(const AbstractDataStore& inStore, Ab } if(finalIteration) { - if(!shouldCancel) + auto result = finish(); + if(result.valid() && !shouldCancel) { throttleProgress(progressThrottle, iter, numberOfIterations); } - return finish(); + return result; } std::swap(currentStore, nextStore); if(!shouldCancel) diff --git a/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp b/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp index 0101d0f2ab..fa38d5d7ee 100644 --- a/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp @@ -1,5 +1,7 @@ #pragma once +#include "simplnx/Common/Types.hpp" + namespace nx::core::ImageProcessing { /** @brief Only Connected Component exposes the explicit legacy wrap policy. Other callers keep Error. */ diff --git a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp index 1784dc888c..465cc726b9 100644 --- a/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/OtsuImageEngine.hpp @@ -675,12 +675,12 @@ inline Result> ComputeCheckedOtsuThresholds(const Histogram } /** - * @brief Maps one selected value to an ordinary uint8 class; equality stays in the lower class. + * @brief Classifies a selected value. A finite value equal to a threshold stays in the lower class. * @tparam T Numeric input value type. - * @param value Native value to classify. NaN and negative infinity use the lowest class; positive infinity uses the highest class. + * @param value Native value to classify. * @param thresholds Finite thresholds in ascending order, with exactly NumberOfThresholds entries. - * @param options Class offset and threshold count. Their sum must not exceed 255. - * @return The ordinary class label. Masked-label selection belongs to the caller. + * @param options Class offset, threshold count, and excluded label. The offset plus the threshold count must not exceed 255. + * @return The ordinary class label for a finite value or Excluded Label for a nonfinite value. The caller handles the mask. * @pre The caller has validated the class interval and threshold vector and has enforced the selected nonfinite policy. */ template diff --git a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp index bb55c37f20..b3822ed11e 100644 --- a/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/RecursiveGaussianEngine.hpp @@ -399,8 +399,13 @@ inline RecursiveGaussianCoefficients ComputeRecursiveGaussianCoefficients(double return c; } -/** @brief Checks the selected smoothing controls and formed coefficients without imposing an accuracy cutoff. */ -inline Result<> ValidateSmoothingGaussianControls(const std::vector& sigma, const FloatVec3& spacing, const SizeVec3& dims) +/** + * @brief Checks that Sigma is finite and positive on the axes that are filtered. + * @param sigma The standard deviation for each axis, in image-spacing units. + * @param dims Image dimensions. A Z dimension of one selects 2D smoothing. + * @return An error for the first invalid active Sigma, or a valid result. + */ +inline Result<> ValidateSmoothingGaussianSigma(const std::vector& sigma, const SizeVec3& dims) { const usize activeAxes = dims[2] > 1 ? 3 : 2; for(usize axis = 0; axis < activeAxes; ++axis) @@ -411,6 +416,20 @@ inline Result<> ValidateSmoothingGaussianControls(const std::vector& si fmt::format("Smoothing Recursive Gaussian Sigma for active axis {} must be finite and positive; received {}. Correct Sigma before execution.", axis, axis < sigma.size() ? fmt::format("{}", sigma.at(axis)) : "no value")); } + } + return {}; +} + +/** @brief Checks the selected smoothing controls and formed coefficients without imposing an accuracy cutoff. */ +inline Result<> ValidateSmoothingGaussianControls(const std::vector& sigma, const FloatVec3& spacing, const SizeVec3& dims) +{ + if(auto result = ValidateSmoothingGaussianSigma(sigma, dims); result.invalid()) + { + return result; + } + const usize activeAxes = dims[2] > 1 ? 3 : 2; + for(usize axis = 0; axis < activeAxes; ++axis) + { const double spacingValue = static_cast(spacing[axis]); if(!std::isfinite(spacingValue) || !(std::abs(spacingValue) > 1e-8)) { diff --git a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp index 1baeffac4a..d033064dfc 100644 --- a/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp @@ -357,6 +357,8 @@ Result SearchThresholdMaxCC(const AbstractDataStore& input, const SizeVec3 T left = std::midpoint(low, midpoint); T right = std::midpoint(high, midpoint); Result<> status; + // Score each candidate using the nonzero pixels it would write. In Warn and Continue, nonfinite pixels receive + // Outside Value. They can connect foreground regions when Outside Value is nonzero. while(ThresholdMaxCCSpanExceedsTwo(low, high) && !shouldCancel) { const T oldLow = low; From d85bc54e9f9ca5441639f35fe7d0f30de0f32210 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 09:19:30 -0500 Subject: [PATCH 61/98] TEST: Fix lint findings in the Gaussian pipeline regression Signed-off-by: Jessica Marquis --- ...othingRecursiveGaussianImageFilterTest.cpp | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index 06943ecc4c..d278efed92 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -10,6 +10,7 @@ #include "SimplnxCore/Filters/ResampleRectGridToImageGeomFilter.hpp" #include "simplnx/Common/Aliases.hpp" +#include "simplnx/Common/Array.hpp" #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" #include "simplnx/Common/TypesUtility.hpp" @@ -48,6 +49,7 @@ #include #include #include +#include #include using namespace nx::core; @@ -377,36 +379,36 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: pipeline va const SizeVec3 dimensions{4, 4, 1}; DataStructure dataStructure; - auto* rectGrid = RectGridGeom::Create(dataStructure, rectPath.getTargetName()); - REQUIRE(rectGrid != nullptr); - rectGrid->setDimensions(dimensions); - auto* cellData = AttributeMatrix::Create(dataStructure, "Cell Data", cellShape, rectGrid->getId()); - REQUIRE(cellData != nullptr); - rectGrid->setCellData(*cellData); + auto* rectGridPtr = RectGridGeom::Create(dataStructure, rectPath.getTargetName()); + REQUIRE(rectGridPtr != nullptr); + rectGridPtr->setDimensions(dimensions); + auto* cellDataPtr = AttributeMatrix::Create(dataStructure, "Cell Data", cellShape, rectGridPtr->getId()); + REQUIRE(cellDataPtr != nullptr); + rectGridPtr->setCellData(*cellDataPtr); const std::array boundNames{"X Bounds", "Y Bounds", "Z Bounds"}; std::array bounds{}; for(usize axis = 0; axis < bounds.size(); ++axis) { - bounds[axis] = Float32Array::Create(dataStructure, boundNames[axis], std::make_unique>(dimensions[axis] + 1, 0.0F), rectGrid->getId()); - REQUIRE(bounds[axis] != nullptr); + bounds.at(axis) = Float32Array::Create(dataStructure, boundNames.at(axis), std::make_unique>(dimensions[axis] + 1, 0.0F), rectGridPtr->getId()); + REQUIRE(bounds.at(axis) != nullptr); const float32 axisSpacing = axis < 2 ? finalSpacing : 1.0F; for(usize index = 0; index <= dimensions[axis]; ++index) { - bounds[axis]->setValue(index, static_cast(index) * axisSpacing); + bounds.at(axis)->setValue(index, static_cast(index) * axisSpacing); } } - const auto boundsResult = rectGrid->setBounds(bounds[0], bounds[1], bounds[2]); + const auto boundsResult = rectGridPtr->setBounds(bounds[0], bounds[1], bounds[2]); SIMPLNX_RESULT_REQUIRE_VALID(boundsResult); - REQUIRE(Float32Array::Create(dataStructure, "Signal", std::make_unique>(cellShape, ShapeType{1}, signalValue), cellData->getId()) != nullptr); + REQUIRE(Float32Array::Create(dataStructure, "Signal", std::make_unique>(cellShape, ShapeType{1}, signalValue), cellDataPtr->getId()) != nullptr); - using ResampleFilter = ResampleRectGridToImageGeomFilter; - auto resample = std::make_unique(); + using ResampleFilterType = ResampleRectGridToImageGeomFilter; + auto resample = std::make_unique(); auto resampleArgs = resample->getDefaultArguments(); - resampleArgs.insertOrAssign(ResampleFilter::k_RectilinearGridPath_Key, rectPath); - resampleArgs.insertOrAssign(ResampleFilter::k_ImageGeometryPath_Key, imagePath); - resampleArgs.insertOrAssign(ResampleFilter::k_ImageGeomCellAttributeMatrixName_Key, std::string("Cell Data")); - resampleArgs.insertOrAssign(ResampleFilter::k_Dimensions_Key, std::vector{4, 4, 1}); - resampleArgs.insertOrAssign(ResampleFilter::k_SelectedDataArrayPaths_Key, std::vector{rectPath.createChildPath("Cell Data").createChildPath("Signal")}); + resampleArgs.insertOrAssign(ResampleFilterType::k_RectilinearGridPath_Key, rectPath); + resampleArgs.insertOrAssign(ResampleFilterType::k_ImageGeometryPath_Key, imagePath); + resampleArgs.insertOrAssign(ResampleFilterType::k_ImageGeomCellAttributeMatrixName_Key, std::string("Cell Data")); + resampleArgs.insertOrAssign(ResampleFilterType::k_Dimensions_Key, std::vector{4, 4, 1}); + resampleArgs.insertOrAssign(ResampleFilterType::k_SelectedDataArrayPaths_Key, std::vector{rectPath.createChildPath("Cell Data").createChildPath("Signal")}); Pipeline pipeline; REQUIRE(pipeline.push_back(std::move(resample), resampleArgs)); @@ -431,9 +433,9 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: pipeline va else { REQUIRE_FALSE(executed); - const auto* gaussian = dynamic_cast(pipeline.at(1)); - REQUIRE(gaussian != nullptr); - const auto errors = gaussian->getErrors(); + const auto* gaussianNodePtr = dynamic_cast(pipeline.at(1)); + REQUIRE(gaussianNodePtr != nullptr); + const auto errors = gaussianNodePtr->getErrors(); REQUIRE(errors.size() == 1); REQUIRE(errors.front().code == ImageProcessing::k_SmoothingGaussianInvalidCoefficients); } @@ -876,6 +878,7 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: preflight r args.insertOrAssign(SmoothFilter::k_Sigma_Key, std::make_any>(std::vector{1.0, 1.0, 1.0})); args.insertOrAssign(SmoothFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); const auto result = filter.execute(ds, args); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. SIMPLNX_RESULT_REQUIRE_INVALID(result.result); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.errors().front().code == nx::core::ImageProcessing::k_SmoothingGaussianInvalidSpacing); From a347f1014ac61b3237001ebbec6cd7b5bc325be1 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 17:02:39 -0500 Subject: [PATCH 62/98] DOC: Define handling of nonfinite input arrays Signed-off-by: Jessica Marquis --- docs/Coding_Knowledge_Base.md | 48 ++++++++++++++++------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/docs/Coding_Knowledge_Base.md b/docs/Coding_Knowledge_Base.md index 701759557b..995059c9c2 100644 --- a/docs/Coding_Knowledge_Base.md +++ b/docs/Coding_Knowledge_Base.md @@ -780,23 +780,13 @@ integer result. Intensity Windowing uses `SaturateCastFromDouble` to clamp value to store in a floating-point array (a fill value or a replacement value) may be NaN or infinity. - **Values read from a file:** the reader rejects a nonfinite value where the format requires a finite one, with an error that names the file and the field. -- **Input array values** that would change a result other than their own element's output, or would reach a - comparison, an ordering, a range, a histogram, a threshold, a color map, a search, an index or an integer conversion. - This covers reductions (sums, means, standard deviations, extrema and data ranges, histograms, medians, modes and - unique-value counts, per feature or per region), neighborhood operations on floating-point data (smoothing, median and - gradient filters, morphology) and decisions made from a value (thresholds, segmentation tolerances, bin and index - selection). The filter has a choice parameter **Nonfinite Input Handling** (key - `nonfinite_input_handling_index`) with two choices, **Error** (the default) and **Warn and Continue**. - - **Error** stops at the first nonfinite value, before any output is written. The message names the array and the - tuple and says that **Warn and Continue** handles these values instead. - - **Warn and Continue** computes ranges and bounds from finite values only, gives each nonfinite element the result - that the filter documentation states (for example "first bin" or "Outside Value"), and reports one warning with - the number of nonfinite values. For reductions and neighborhood operations, exclude nonfinite values from the - contributing samples and counts. - - The in-core and out-of-core algorithms use one shared test and rule. The parameter has no effect on integer arrays. -- **Element-wise floating-point math**, where one element in gives the same element out and no other element changes, - lets the IEEE result pass through. The filter documentation lists what NaN, infinity, zero and out-of-domain inputs - produce. When integer input can produce a fraction or NaN, the output type is floating point. +- **Floating-point input arrays.** The caller is responsible for supplying data suitable for the filter. Do not add a + parameter, validation pass, warning or error solely to detect or report NaN or infinity in the user's array. + Keep the checks required to compute valid indices and perform safe conversions. When an algorithm needs a finite + range, use finite samples in its existing range calculation and handle the case where none are available. + Apply the same arithmetic on in-memory and out-of-core paths. +- **Element-wise floating-point results.** Element-wise math can produce an IEEE result such as NaN from the square + root of a finite negative value. When integer input can produce a fraction or NaN, the output type is floating point. - **A computed value written to an integer output:** see [Write floating-point output through a saturating cast](#write-floating-point-output-through-a-saturating-cast). - **A result that is undefined for degenerate input** (a zero-length vector, an all-zero quaternion) is NaN in a @@ -813,23 +803,29 @@ Histogram** groups values with the same equality test, and the shared statistics and `HistogramUtilities.hpp` sort and key floating-point values with `operator<`. `ComputeArrayMinMax` is not NaN-aware (issue #1874). **Invert Intensity Image** accepts a NaN **Maximum** and writes 0 to every integer pixel (issue #1873). -**Why:** every comparison with NaN is false. `operator<` is then not a strict weak ordering, so `std::sort`, -`std::stable_sort` and `std::map` keys are undefined; a threshold or a bin search puts NaN on one side without saying -so; an equality test written as "neither is less than the other" groups NaN with every value; and a cast of NaN or an -infinity to an integer is undefined behavior. Whether a nonfinite input value means "bad data" or "no measurement" -depends on the user's data, so input array values get a choice; parameters and file values do not. +**Why:** ordered comparisons with NaN are false. A minimum and maximum seeded with NaN can therefore stay NaN +even when the rest of the image is finite. Sorting a mixture of NaNs and finite values with ordinary less-than +comparisons can violate the ordering the sort requires. Converting NaN or infinity to an integer is undefined +behavior. Guard these operations where they occur. **What to look for:** - `std::sort`, `std::stable_sort`, `std::map`, `std::set` or `std::unique` over floating-point input values; - an equivalence written as `!(a < b) && !(b < a)` on floating-point values; -- a running minimum, maximum or sum over input values with no finiteness test; +- a finite range seeded from the first pixel when that pixel can be NaN; - a threshold, tolerance or bin index computed from an input value with no finiteness test; - a parameter check written as `value < 0` or `value <= 0`, which NaN passes. -**Pattern to use:** **Nonfinite Input Handling** in `ThresholdMaximumConnectedComponentsImageFilter` and -`WriteImageFilter` (PR #1888); the Upper Boundary of `ThresholdMaximumConnectedComponentsImageFilter` for a documented -meaning of infinity (PR #1888). +**Pattern to use (issue #1874):** + +- Otsu Multiple Thresholds fits the histogram using finite, mask-selected samples. Test the labels of the finite + samples with a NaN first and with it later. The filter makes no guarantee about the label of a nonfinite input. +- Threshold Maximum Connected Components uses finite intensities for its search bounds and assigns Outside Value to + nonfinite pixels. If no finite samples exist, fill with Outside Value. Upper Boundary is a filter parameter with + documented meanings for positive and negative infinity. +- Write Image preserves raw floating-point pixels. In color-table mode, finite values establish the palette range. + Masked pixels use the mask color. Unmasked nonfinite pixels use the first palette color, including when there are + no finite samples. ### Copy every field that sets behavior when you clone an object From f208b80c5e27dab70e2437f62572f823fa8e145f Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 17:47:41 -0500 Subject: [PATCH 63/98] DOC: Explain integer precision in H Convex reconstruction Signed-off-by: Jessica Marquis --- src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md | 5 +++++ src/Plugins/NXImageProcessing/docs/HMaximaImageFilter.md | 4 ++++ .../src/NXImageProcessing/Filters/HConvexImageFilter.hpp | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md b/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md index 71d2249d4d..f6264122ea 100644 --- a/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/HConvexImageFilter.md @@ -10,6 +10,11 @@ NXImageProcessing (Morphology) Computes the *h-convex* transform of a grayscale image: **HConvex(f, h) = f − HMaxima(f, h)**. Each regional maximum of the input is replaced by its own height (its contrast to the surrounding level), **capped at the Height parameter**, while everything that is not part of a maximum becomes zero. A regional maximum of contrast `c` therefore maps to `min(c, h)`. +Here HMaxima denotes mathematical reconstruction by dilation. With 64-bit integer intensities above 2^53, +the separate H Maxima filter can round its marker through Float64. Subtracting that filter's output from the +input can therefore give a different value. For example, an isolated peak at 2^54 and Height 1 has exact +marker intensity 2^54 − 1 in H Convex. + This isolates bright "domes" and measures their prominence: peaks taller than `Height` all map to `Height`, while shallower peaks map to their true contrast. It is a common pre-step for peak detection and marker extraction. Internally the filter reconstructs the `input − Height` marker by dilation under the original input, then subtracts that reconstruction from the input. The reconstruction cannot exceed an integer input, so the integer contrast is nonnegative. A nonnegative contrast can still exceed a signed output type's maximum. The filter returns an execution error in that case. diff --git a/src/Plugins/NXImageProcessing/docs/HMaximaImageFilter.md b/src/Plugins/NXImageProcessing/docs/HMaximaImageFilter.md index 85a75d2576..2ed47c82c2 100644 --- a/src/Plugins/NXImageProcessing/docs/HMaximaImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/HMaximaImageFilter.md @@ -16,6 +16,10 @@ Internally the filter performs a **reconstruction-by-dilation** of a marker imag The input array must be single-component (scalar); the output element type matches the input element type. This is an ITK-free, out-of-core-capable reimplementation of the legacy ITK H Maxima Image Filter. When all data is resident it uses a fast in-core hybrid reconstruction; when the arrays are out-of-core it streams the computation so memory use stays bounded regardless of image size. Both paths produce bit-identical output, and the result matches the legacy ITK filter exactly. +For wide integer images, marker construction uses Float64 arithmetic. It can lose single intensity units +above 2^53. The [H Convex](HConvexImageFilter.md) filter uses exact integer marker arithmetic and should be +used directly for integer peak contrast. + ### Height The *Height* parameter sets the contrast threshold in the same units as the image intensities: diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp index d337c7126a..4d91211815 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/HConvexImageFilter.hpp @@ -12,7 +12,8 @@ namespace nx::core * @brief ITK-free, out-of-core-capable h-convex transform: each regional maximum is replaced by its own height, * capped at the Height parameter (its "dome"), and everything else becomes zero. * - * HConvex(f, h) = f - HMaxima(f, h). A regional maximum of contrast c maps to min(c, h); the background maps to 0. + * HMaxima in the mathematical identity HConvex(f, h) = f - HMaxima(f, h) uses exact integer markers here. + * The separate H Maxima filter can round 64-bit integer markers above 2^53. A regional maximum of contrast c maps to min(c, h); the background maps to 0. * It is implemented by reconstruction-by-dilation of the (input - Height) marker under the input mask (the HMaxima * result) followed by an elementwise subtraction of that result from the input. Output type == input type; the * input must be single-component (scalar). Height is finite and nonnegative; integer images require a whole Height. From 2b3b8f0821d45c1baeeac99eefdf15fc0f083632 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:30 -0500 Subject: [PATCH 64/98] TEST: Name image filter integration cases by behavior Signed-off-by: Jessica Marquis --- .../test/CurvatureFlowImageFilterTest.cpp | 26 +++++----- .../test/ExpNegativeImageFilterTest.cpp | 37 +++++++------- .../test/HConvexImageFilterTest.cpp | 22 ++++---- .../test/HMinimaImageFilterTest.cpp | 42 +++++++-------- ...pp => ImageFilterIntegrationTestUtils.hpp} | 6 +-- .../OtsuMultipleThresholdsImageFilterTest.cpp | 27 +++++----- ...othingRecursiveGaussianImageFilterTest.cpp | 28 +++++----- .../test/SquareImageFilterTest.cpp | 37 +++++++------- ...imumConnectedComponentsImageFilterTest.cpp | 30 +++++------ .../NXImageProcessing/test/WriteImageTest.cpp | 51 ++++++++++--------- 10 files changed, 155 insertions(+), 151 deletions(-) rename src/Plugins/NXImageProcessing/test/{C15CompatibilityTestUtils.hpp => ImageFilterIntegrationTestUtils.hpp} (98%) diff --git a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp index 6a99d39b50..95b91f72cb 100644 --- a/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/CurvatureFlowImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "NXImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -1095,7 +1095,7 @@ TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: zero iterations is a no- } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident CurvatureFlowImageFilter feeds quantitative gradient", "[NXImageProcessing][C15Compatibility][Issue1874][CurvatureFlowImageFilter]") +TEST_CASE("NXImageProcessing::CurvatureFlowImageFilter: resident integration feeds quantitative gradient", "[NXImageProcessing][ImageFilterIntegration][Issue1874][CurvatureFlowImageFilter]") { const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); @@ -1111,20 +1111,20 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident CurvatureFlowImageFilter } DataStructure dataStructure; const auto input = recon_test::BuildImageFromPattern(dataStructure, 16, 4, 1, original); - c15_test::SetGeometry(dataStructure); - REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); + image_integration::SetGeometry(dataStructure); + REQUIRE(image_integration::Read(dataStructure, input, {1, 4, 16}, scope) == original); const CurvatureFlowImageFilter filter; - auto args = c15_test::ImageArguments(filter, input, "Smoothed"); + auto args = image_integration::ImageArguments(filter, input, "Smoothed"); args.insertOrAssign(CurvatureFlowImageFilter::k_NumberOfIterations_Key, std::make_any(1)); args.insertOrAssign(CurvatureFlowImageFilter::k_TimeStep_Key, std::make_any(0.125)); - c15_test::ExecuteChecked(filter, dataStructure, args); + image_integration::ExecuteChecked(filter, dataStructure, args); const auto smoothed = input.replaceName("Smoothed"); - const auto values = c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope); + const auto values = image_integration::Read(dataStructure, smoothed, {1, 4, 16}, scope); const GradientMagnitudeImageFilter gradient; - args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); + args = image_integration::ImageArguments(gradient, smoothed, "Gradient"); args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); - c15_test::Execute(gradient, dataStructure, args, scope); - const auto edges = c15_test::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); + image_integration::Execute(gradient, dataStructure, args, scope); + const auto edges = image_integration::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); for(usize row = 0; row < 4; ++row) { for(usize column = 0; column < 16; ++column) @@ -1147,9 +1147,9 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident CurvatureFlowImageFilter } } } - REQUIRE(c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); - REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); - c15_test::CheckGeometry(dataStructure, {16, 4, 1}); + REQUIRE(image_integration::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(image_integration::Read(dataStructure, input, {1, 4, 16}, scope) == original); + image_integration::CheckGeometry(dataStructure, {16, 4, 1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp index 3ca07af775..1445119692 100644 --- a/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ExpNegativeImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "NXImageProcessing/Filters/ExpImageFilter.hpp" #include "NXImageProcessing/Filters/Log10ImageFilter.hpp" @@ -359,11 +359,12 @@ TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: ITK real-image golden (def } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain masks feed components", "[NXImageProcessing][C15Compatibility][Issue1874][ExpNegativeImageFilter]") +TEST_CASE("NXImageProcessing::ExpNegativeImageFilter: resident integration exponential and domain masks feed components", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][ExpNegativeImageFilter]") { Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); - const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const image_integration::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}, usize{5}, usize{6}); CAPTURE(variant); DataStructure dataStructure; @@ -380,33 +381,33 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain m { case 0: input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {1, 0.36787944117144232160, 0.13533528323661269189}; lower = 0.5; upper = 1; selected = {1, 0, 0}; components = {1, 0, 0}; - REQUIRE(c15_test::Read(dataStructure, input, {1, 1, 3}, expectedStore) == std::vector{0, 1, 2}); + REQUIRE(image_integration::Read(dataStructure, input, {1, 1, 3}, expectedStore) == std::vector{0, 1, 2}); break; case 1: input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {1, std::numbers::e}; lower = 1; selected = {1, 0}; components = {1, 0}; - REQUIRE(c15_test::Read(dataStructure, input, {1, 1, 2}, expectedStore) == std::vector{0, 1}); + REQUIRE(image_integration::Read(dataStructure, input, {1, 1, 2}, expectedStore) == std::vector{0, 1}); break; case 2: input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1000, nan, -inf, inf}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {1, inf, nan, 0, inf}; selected = {1, 0, 0, 1, 0}; components = {1, 0, 0, 2, 0}; break; case 3: input = ip_test::BuildScalarValuesImage(dataStructure, {1, 0, -1, inf, nan}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {0, -inf, nan, inf, nan}; lower = -1; upper = 1; @@ -415,7 +416,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain m break; case 4: input = ip_test::BuildScalarValuesImage(dataStructure, {1, 10, 0, -1}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {0, 1, -inf, nan}; upper = 1; selected = {1, 1, 0, 0}; @@ -423,14 +424,14 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain m break; case 5: input = ip_test::BuildScalarValuesImage(dataStructure, {0, 4, -1, inf, nan}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {0, 2, nan, inf, nan}; selected = {1, 1, 0, 0, 0}; components = {1, 1, 0, 0, 0}; break; default: input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2}); - quantitative = c15_test::Pointwise(dataStructure, input, "Quantitative"); + quantitative = image_integration::Pointwise(dataStructure, input, "Quantitative"); expected = {1, 0.36787944117144232160, 0.13533528323661269189}; lower = 0.5; upper = 1; @@ -440,7 +441,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain m } const ShapeType shape{1, 1, expected.size()}; auto checkQuantitative = [&] { - const auto values = c15_test::Read(dataStructure, quantitative, shape, expectedStore); + const auto values = image_integration::Read(dataStructure, quantitative, shape, expectedStore); for(usize index = 0; index < expected.size(); ++index) { CAPTURE(index); @@ -460,13 +461,13 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exponential and domain m }; if(variant >= 2) { - REQUIRE(c15_test::Read(dataStructure, input, shape, expectedStore).size() == expected.size()); + REQUIRE(image_integration::Read(dataStructure, input, shape, expectedStore).size() == expected.size()); } checkQuantitative(); - const auto mask = c15_test::Select(dataStructure, quantitative, lower, upper, "Selected"); - REQUIRE(c15_test::Read(dataStructure, mask, shape, expectedStore) == selected); - const auto labels = c15_test::Components(dataStructure, mask, "Components"); - REQUIRE(c15_test::Read(dataStructure, labels, shape, expectedStore) == components); + const auto mask = image_integration::Select(dataStructure, quantitative, lower, upper, "Selected"); + REQUIRE(image_integration::Read(dataStructure, mask, shape, expectedStore) == selected); + const auto labels = image_integration::Components(dataStructure, mask, "Components"); + REQUIRE(image_integration::Read(dataStructure, labels, shape, expectedStore) == components); checkQuantitative(); UnitTest::CheckArraysInheritTupleDims(dataStructure); } diff --git a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp index db1910e281..1101746657 100644 --- a/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HConvexImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -790,7 +790,7 @@ TEST_CASE("NXImageProcessing::HConvexImageFilter: ITK real-image golden (HConvex } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident exact widening supports fractional HConvex height", "[NXImageProcessing][C15Compatibility][Issue1874][HConvexImageFilter]") +TEST_CASE("NXImageProcessing::HConvexImageFilter: resident integration exact widening supports fractional HConvex height", "[NXImageProcessing][ImageFilterIntegration][Issue1874][HConvexImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); @@ -798,19 +798,19 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident exact widening supports DataStructure dataStructure; const std::vector original{2, 2, 2, 2, 4, 2, 2, 2, 2}; const auto input = recon_test::BuildImageFromPattern(dataStructure, 3, 3, 1, original); - c15_test::SetGeometry(dataStructure); - const auto converted = c15_test::ConvertToFloat32(dataStructure, input); + image_integration::SetGeometry(dataStructure); + const auto converted = image_integration::ConvertToFloat32(dataStructure, input); const std::vector exactConverted{2, 2, 2, 2, 4, 2, 2, 2, 2}; - REQUIRE(c15_test::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); + REQUIRE(image_integration::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); const HConvexImageFilter filter; - auto args = c15_test::ImageArguments(filter, converted, "Contrast"); + auto args = image_integration::ImageArguments(filter, converted, "Contrast"); args.insertOrAssign(HConvexImageFilter::k_Height_Key, std::make_any(0.5)); args.insertOrAssign(HConvexImageFilter::k_FullyConnected_Key, std::make_any(false)); - c15_test::Execute(filter, dataStructure, args, scope); - REQUIRE(c15_test::Read(dataStructure, input.replaceName("Contrast"), {1, 3, 3}, scope) == std::vector{0, 0, 0, 0, 0.5F, 0, 0, 0, 0}); - REQUIRE(c15_test::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); - REQUIRE(c15_test::Read(dataStructure, input, {1, 3, 3}, scope) == original); - c15_test::CheckGeometry(dataStructure, {3, 3, 1}); + image_integration::Execute(filter, dataStructure, args, scope); + REQUIRE(image_integration::Read(dataStructure, input.replaceName("Contrast"), {1, 3, 3}, scope) == std::vector{0, 0, 0, 0, 0.5F, 0, 0, 0, 0}); + REQUIRE(image_integration::Read(dataStructure, converted, {1, 3, 3}, scope) == exactConverted); + REQUIRE(image_integration::Read(dataStructure, input, {1, 3, 3}, scope) == original); + image_integration::CheckGeometry(dataStructure, {3, 3, 1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp index 1da89d6004..4a3d0c1587 100644 --- a/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/HMinimaImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.hpp" #include "NXImageProcessing/Filters/RegionalMinimaImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -600,54 +600,54 @@ TEST_CASE("NXImageProcessing::HMinimaImageFilter: ITK real-image golden (HMinima } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident fractional minima produce watershed markers", "[NXImageProcessing][C15Compatibility][Issue1874][HMinimaImageFilter]") +TEST_CASE("NXImageProcessing::HMinimaImageFilter: resident integration fractional minima produce watershed markers", "[NXImageProcessing][ImageFilterIntegration][Issue1874][HMinimaImageFilter]") { UnitTest::LoadPlugins(); const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); DataStructure dataStructure; const auto input = ip_test::BuildScalarValuesImage(dataStructure, {0, 1, 2, 3, 2, 1, 0}); - c15_test::SetGeometry(dataStructure); + image_integration::SetGeometry(dataStructure); const ShapeType shape{1, 1, 7}; - REQUIRE(c15_test::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); - const auto converted = c15_test::ConvertToFloat32(dataStructure, input); - REQUIRE(c15_test::Read(dataStructure, converted, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + REQUIRE(image_integration::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + const auto converted = image_integration::ConvertToFloat32(dataStructure, input); + REQUIRE(image_integration::Read(dataStructure, converted, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); const HMinimaImageFilter minimaFilter; - auto args = c15_test::ImageArguments(minimaFilter, converted, "RaisedMinima"); + auto args = image_integration::ImageArguments(minimaFilter, converted, "RaisedMinima"); args.insertOrAssign(HMinimaImageFilter::k_Height_Key, std::make_any(0.5)); args.insertOrAssign(HMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); - c15_test::Execute(minimaFilter, dataStructure, args, scope); + image_integration::Execute(minimaFilter, dataStructure, args, scope); const auto raised = input.replaceName("RaisedMinima"); const std::vector expectedRaised{0.5F, 1, 2, 3, 2, 1, 0.5F}; - REQUIRE(c15_test::Read(dataStructure, raised, shape, scope) == expectedRaised); + REQUIRE(image_integration::Read(dataStructure, raised, shape, scope) == expectedRaised); const RegionalMinimaImageFilter regional; - args = c15_test::ImageArguments(regional, raised, "RegionalMinima"); + args = image_integration::ImageArguments(regional, raised, "RegionalMinima"); args.insertOrAssign(RegionalMinimaImageFilter::k_ForegroundValue_Key, std::make_any(1)); args.insertOrAssign(RegionalMinimaImageFilter::k_BackgroundValue_Key, std::make_any(0)); args.insertOrAssign(RegionalMinimaImageFilter::k_FullyConnected_Key, std::make_any(false)); args.insertOrAssign(RegionalMinimaImageFilter::k_FlatIsMinima_Key, std::make_any(true)); - c15_test::Execute(regional, dataStructure, args, scope); + image_integration::Execute(regional, dataStructure, args, scope); const auto regionalPath = input.replaceName("RegionalMinima"); - REQUIRE(c15_test::Read(dataStructure, regionalPath, shape, scope) == std::vector{1, 0, 0, 0, 0, 0, 1}); - const auto markers = c15_test::Components(dataStructure, regionalPath, "Markers"); + REQUIRE(image_integration::Read(dataStructure, regionalPath, shape, scope) == std::vector{1, 0, 0, 0, 0, 0, 1}); + const auto markers = image_integration::Components(dataStructure, regionalPath, "Markers"); const std::vector expectedMarkers{1, 0, 0, 0, 0, 0, 2}; - REQUIRE(c15_test::Read(dataStructure, markers, shape, scope) == expectedMarkers); + REQUIRE(image_integration::Read(dataStructure, markers, shape, scope) == expectedMarkers); const MorphologicalWatershedFromMarkersImageFilter watershed; - args = c15_test::ImageArguments(watershed, raised, "Basins"); + args = image_integration::ImageArguments(watershed, raised, "Basins"); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerImageDataPath_Key, std::make_any(markers)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkWatershedLine_Key, std::make_any(true)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_FullyConnected_Key, std::make_any(false)); args.insertOrAssign(MorphologicalWatershedFromMarkersImageFilter::k_MarkerIdConversionHandling_Key, std::make_any(0)); - c15_test::ExecuteChecked(watershed, dataStructure, args); - const auto labels = c15_test::Read(dataStructure, input.replaceName("Basins"), shape, scope); + image_integration::ExecuteChecked(watershed, dataStructure, args); + const auto labels = image_integration::Read(dataStructure, input.replaceName("Basins"), shape, scope); // The unique central ridge separates three cells in each basin. Zero marks the watershed line. REQUIRE(labels == std::vector{1, 1, 1, 0, 2, 2, 2}); REQUIRE(std::count(labels.begin(), labels.end(), uint32{1}) == 3); REQUIRE(std::count(labels.begin(), labels.end(), uint32{2}) == 3); - REQUIRE(c15_test::Read(dataStructure, raised, shape, scope) == expectedRaised); - REQUIRE(c15_test::Read(dataStructure, markers, shape, scope) == expectedMarkers); - REQUIRE(c15_test::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); - c15_test::CheckGeometry(dataStructure, {7, 1, 1}); + REQUIRE(image_integration::Read(dataStructure, raised, shape, scope) == expectedRaised); + REQUIRE(image_integration::Read(dataStructure, markers, shape, scope) == expectedMarkers); + REQUIRE(image_integration::Read(dataStructure, input, shape, scope) == std::vector{0, 1, 2, 3, 2, 1, 0}); + image_integration::CheckGeometry(dataStructure, {7, 1, 1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp b/src/Plugins/NXImageProcessing/test/ImageFilterIntegrationTestUtils.hpp similarity index 98% rename from src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp rename to src/Plugins/NXImageProcessing/test/ImageFilterIntegrationTestUtils.hpp index 729075b795..563d74d426 100644 --- a/src/Plugins/NXImageProcessing/test/C15CompatibilityTestUtils.hpp +++ b/src/Plugins/NXImageProcessing/test/ImageFilterIntegrationTestUtils.hpp @@ -28,7 +28,7 @@ // Small transport helpers only. Each owning test states its independent numerical expectations. // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -namespace c15_test +namespace image_integration { using namespace nx::core; @@ -36,7 +36,7 @@ class Directory { public: Directory() - : m_Path(std::filesystem::path(unit_test::k_BinaryDir.view()) / "test_output" / ("C15Compatibility-" + std::to_string(std::random_device{}()))) + : m_Path(std::filesystem::path(unit_test::k_BinaryDir.view()) / "test_output" / ("ImageFilterIntegration-" + std::to_string(std::random_device{}()))) { REQUIRE(std::filesystem::create_directories(m_Path)); } @@ -283,4 +283,4 @@ inline DataStructure ReadMha(const std::filesystem::path& path) return result; } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) -} // namespace c15_test +} // namespace image_integration diff --git a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp index adbc0829c1..02bf2a7577 100644 --- a/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OtsuMultipleThresholdsImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -1550,11 +1550,12 @@ TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: floating summar // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels preserve class measurements", "[NXImageProcessing][C15Compatibility][Issue1874][OtsuMultipleThresholdsImageFilter]") +TEST_CASE("NXImageProcessing::OtsuMultipleThresholdsImageFilter: resident integration Otsu excluded labels preserve class measurements", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][OtsuMultipleThresholdsImageFilter]") { (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); - const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const image_integration::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const bool useMask = GENERATE(false, true); const auto nan = std::numeric_limits::quiet_NaN(); const auto inf = std::numeric_limits::infinity(); @@ -1563,7 +1564,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels pre DataStructure dataStructure; const auto input = ip_test::BuildScalarValuesImage(dataStructure, original); const OtsuMultipleThresholdsImageFilter filter; - auto args = c15_test::ImageArguments(filter, input, "Classes"); + auto args = image_integration::ImageArguments(filter, input, "Classes"); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfHistogramBins_Key, std::make_any(2)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_NumberOfThresholds_Key, std::make_any(1)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_LabelOffset_Key, std::make_any(7)); @@ -1572,7 +1573,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels pre if(useMask) { const auto mask = createOtsuMask(dataStructure, std::array{1, 1, 1, 1, 0, 1, 1}); - REQUIRE(c15_test::Read(dataStructure, mask, ShapeType{7}, expectedStore) == std::vector{1, 1, 1, 1, 0, 1, 1}); + REQUIRE(image_integration::Read(dataStructure, mask, ShapeType{7}, expectedStore) == std::vector{1, 1, 1, 1, 0, 1, 1}); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_UseMaskArray_Key, std::make_any(true)); args.insertOrAssign(OtsuMultipleThresholdsImageFilter::k_MaskArrayPath_Key, std::make_any(mask)); } @@ -1581,14 +1582,14 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident Otsu excluded labels pre REQUIRE(result.result.warnings().size() == 1); REQUIRE(result.result.warnings().front().message.find(useMask ? "skipped 2 nonfinite pixels still selected after applying the input mask" : "skipped 3 nonfinite pixels") != std::string::npos); const auto classes = input.replaceName("Classes"); - REQUIRE(c15_test::Read(dataStructure, classes, shape, expectedStore) == std::vector{7, 8, 255, 8, 255, 255, 7}); - const auto selected = c15_test::Select(dataStructure, classes, 8, 8, "DesiredClass"); - REQUIRE(c15_test::Read(dataStructure, selected, shape, expectedStore) == std::vector{0, 1, 0, 1, 0, 0, 0}); - const auto components = c15_test::Components(dataStructure, selected, "Components"); - REQUIRE(c15_test::Read(dataStructure, components, shape, expectedStore) == std::vector{0, 1, 0, 2, 0, 0, 0}); - const auto validity = c15_test::Select(dataStructure, classes, 7, 8, "Validity"); - REQUIRE(c15_test::Read(dataStructure, validity, shape, expectedStore) == std::vector{1, 1, 0, 1, 0, 0, 1}); - REQUIRE(c15_test::MaskedMean(dataStructure, selected, validity) == 0.5F); + REQUIRE(image_integration::Read(dataStructure, classes, shape, expectedStore) == std::vector{7, 8, 255, 8, 255, 255, 7}); + const auto selected = image_integration::Select(dataStructure, classes, 8, 8, "DesiredClass"); + REQUIRE(image_integration::Read(dataStructure, selected, shape, expectedStore) == std::vector{0, 1, 0, 1, 0, 0, 0}); + const auto components = image_integration::Components(dataStructure, selected, "Components"); + REQUIRE(image_integration::Read(dataStructure, components, shape, expectedStore) == std::vector{0, 1, 0, 2, 0, 0, 0}); + const auto validity = image_integration::Select(dataStructure, classes, 7, 8, "Validity"); + REQUIRE(image_integration::Read(dataStructure, validity, shape, expectedStore) == std::vector{1, 1, 0, 1, 0, 0, 1}); + REQUIRE(image_integration::MaskedMean(dataStructure, selected, validity) == 0.5F); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp index d278efed92..e48351d71f 100644 --- a/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SmoothingRecursiveGaussianImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "NXImageProcessing/Filters/GradientMagnitudeImageFilter.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -1106,8 +1106,8 @@ TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: ITK real-im } // NOLINTBEGIN(bugprone-chained-comparison) Catch2 comparison decomposition triggers this check. -TEST_CASE("NXImageProcessing::C15Compatibility resident SmoothingRecursiveGaussianImageFilter feeds quantitative gradient", - "[NXImageProcessing][C15Compatibility][Issue1874][SmoothingRecursiveGaussianImageFilter]") +TEST_CASE("NXImageProcessing::SmoothingRecursiveGaussianImageFilter: resident integration feeds quantitative gradient", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][SmoothingRecursiveGaussianImageFilter]") { const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); @@ -1123,20 +1123,20 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident SmoothingRecursiveGaussi } DataStructure dataStructure; const auto input = recon_test::BuildImageFromPattern(dataStructure, 16, 4, 1, original); - c15_test::SetGeometry(dataStructure); - REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); + image_integration::SetGeometry(dataStructure); + REQUIRE(image_integration::Read(dataStructure, input, {1, 4, 16}, scope) == original); const SmoothingRecursiveGaussianImageFilter filter; - auto args = c15_test::ImageArguments(filter, input, "Smoothed"); + auto args = image_integration::ImageArguments(filter, input, "Smoothed"); args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_Sigma_Key, std::make_any>(std::vector{1, 1, 1})); args.insertOrAssign(SmoothingRecursiveGaussianImageFilter::k_NormalizeAcrossScale_Key, std::make_any(false)); - c15_test::Execute(filter, dataStructure, args, scope); + image_integration::Execute(filter, dataStructure, args, scope); const auto smoothed = input.replaceName("Smoothed"); - const auto values = c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope); + const auto values = image_integration::Read(dataStructure, smoothed, {1, 4, 16}, scope); const GradientMagnitudeImageFilter gradient; - args = c15_test::ImageArguments(gradient, smoothed, "Gradient"); + args = image_integration::ImageArguments(gradient, smoothed, "Gradient"); args.insertOrAssign(GradientMagnitudeImageFilter::k_UseImageSpacing_Key, std::make_any(true)); - c15_test::Execute(gradient, dataStructure, args, scope); - const auto edges = c15_test::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); + image_integration::Execute(gradient, dataStructure, args, scope); + const auto edges = image_integration::Read(dataStructure, input.replaceName("Gradient"), {1, 4, 16}, scope); for(usize row = 0; row < 4; ++row) { const usize rowOffset = row * 16; @@ -1165,9 +1165,9 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident SmoothingRecursiveGaussi } } } - REQUIRE(c15_test::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); - REQUIRE(c15_test::Read(dataStructure, input, {1, 4, 16}, scope) == original); - c15_test::CheckGeometry(dataStructure, {16, 4, 1}); + REQUIRE(image_integration::Read(dataStructure, smoothed, {1, 4, 16}, scope) == values); + REQUIRE(image_integration::Read(dataStructure, input, {1, 4, 16}, scope) == original); + image_integration::CheckGeometry(dataStructure, {16, 4, 1}); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison) diff --git a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp index 6e54dbde69..880acb6470 100644 --- a/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/SquareImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "UnaryFilterTestUtils.hpp" @@ -341,42 +341,43 @@ TEST_CASE("NXImageProcessing::SquareImageFilter: ITK real-image golden (defaults } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::C15Compatibility resident widened Square persists and feeds integer components", "[NXImageProcessing][C15Compatibility][Issue1874][SquareImageFilter]") +TEST_CASE("NXImageProcessing::SquareImageFilter: resident integration widened Square persists and feeds integer components", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][SquareImageFilter]") { const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); - const c15_test::Directory output; + const image_integration::Directory output; DataStructure source; const std::vector original{0, 2147483647, 0, 2}; const auto input = ip_test::BuildScalarValuesImage(source, original); - c15_test::SetGeometry(source); - REQUIRE(c15_test::Read(source, input, {1, 1, 4}, scope) == original); - const auto square = c15_test::Pointwise(source, input, "ExactSquare"); + image_integration::SetGeometry(source); + REQUIRE(image_integration::Read(source, input, {1, 1, 4}, scope) == original); + const auto square = image_integration::Pointwise(source, input, "ExactSquare"); // (2^31-1)^2 is exact in Int64 and cannot be transported exactly through Float64. const std::vector expected{0, 4611686014132420609LL, 0, 4}; - REQUIRE(c15_test::Read(source, square, {1, 1, 4}, scope) == expected); + REQUIRE(image_integration::Read(source, square, {1, 1, 4}, scope) == expected); const auto save = DREAM3D::WriteFile(output.path() / "square.dream3d", source); SIMPLNX_RESULT_REQUIRE_VALID(save); - c15_test::CheckSaved(output.path() / "square.dream3d", square, {1, 1, 4}, expected); + image_integration::CheckSaved(output.path() / "square.dream3d", square, {1, 1, 4}, expected); auto load = DREAM3D::LoadDataStructure(output.path() / "square.dream3d"); SIMPLNX_RESULT_REQUIRE_VALID(load); auto& reloaded = load.value(); - REQUIRE(c15_test::Read(reloaded, square, {1, 1, 4}, scope) == expected); - c15_test::CheckGeometry(reloaded, {4, 1, 1}); + REQUIRE(image_integration::Read(reloaded, square, {1, 1, 4}, scope) == expected); + image_integration::CheckGeometry(reloaded, {4, 1, 1}); const WriteImageFilter writer; - c15_test::Execute(writer, reloaded, c15_test::WriteArguments(square, output.path() / "square.mha"), scope); - auto raw = c15_test::ReadMha(output.path() / "square.mha"); - REQUIRE(c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 4}, scope) == expected); + image_integration::Execute(writer, reloaded, image_integration::WriteArguments(square, output.path() / "square.mha"), scope); + auto raw = image_integration::ReadMha(output.path() / "square.mha"); + REQUIRE(image_integration::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 4}, scope) == expected); const ImageGeom* rawGeometryPtr = nullptr; REQUIRE_NOTHROW(rawGeometryPtr = &raw.getDataRefAs(DataPath({"Image Geometry"}))); REQUIRE(rawGeometryPtr->getDimensions() == SizeVec3{4, 1, 1}); REQUIRE(rawGeometryPtr->getSpacing() == FloatVec3{1, 2, 1}); REQUIRE(rawGeometryPtr->getOrigin() == FloatVec3{4, 5, 0}); - const auto labels = c15_test::Components(reloaded, square, "Components"); - REQUIRE(c15_test::Read(reloaded, labels, {1, 1, 4}, scope) == std::vector{0, 1, 0, 2}); - REQUIRE(c15_test::Read(reloaded, square, {1, 1, 4}, scope) == expected); - REQUIRE(c15_test::Read(source, square, {1, 1, 4}, scope) == expected); - REQUIRE(c15_test::Read(source, input, {1, 1, 4}, scope) == original); + const auto labels = image_integration::Components(reloaded, square, "Components"); + REQUIRE(image_integration::Read(reloaded, labels, {1, 1, 4}, scope) == std::vector{0, 1, 0, 2}); + REQUIRE(image_integration::Read(reloaded, square, {1, 1, 4}, scope) == expected); + REQUIRE(image_integration::Read(source, square, {1, 1, 4}, scope) == expected); + REQUIRE(image_integration::Read(source, input, {1, 1, 4}, scope) == original); UnitTest::CheckArraysInheritTupleDims(source); UnitTest::CheckArraysInheritTupleDims(reloaded); } diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index e6f708704f..07188d79ac 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "ItkGoldenTestUtils.hpp" #include "ReconstructionFilterTestUtils.hpp" @@ -1053,12 +1053,12 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selects intended foreground", - "[NXImageProcessing][C15Compatibility][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") +TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: resident integration threshold fallback selects intended foreground", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][ThresholdMaximumConnectedComponentsImageFilter]") { (void)Application::GetOrCreateInstance(); const UnitTest::PreferencesSentinel preferences(DataStorageMode::ForceInCore, 0); - const c15_test::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; + const image_integration::StoreExpectation expectedStore{IDataStore::StoreType::InMemory}; const usize variant = GENERATE(usize{0}, usize{1}, usize{2}, usize{3}, usize{4}); const auto inf = std::numeric_limits::infinity(); DataStructure dataStructure; @@ -1068,7 +1068,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selec noFinite ? ip_test::BuildScalarValuesImage(dataStructure, {std::numeric_limits::quiet_NaN(), -inf, inf}) : ip_test::BuildScalarValuesImage(dataStructure, {100, 102}); const ShapeType shape{1, 1, noFinite ? usize{3} : usize{2}}; const ThresholdMaximumConnectedComponentsImageFilter filter; - auto args = c15_test::ImageArguments(filter, input, "Thresholded"); + auto args = image_integration::ImageArguments(filter, input, "Thresholded"); args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_MinimumObjectSizeInPixels_Key, std::make_any(0)); const std::array upperBounds{65536, 99, -inf, 65536, 65536}; args.insertOrAssign(ThresholdMaximumConnectedComponentsImageFilter::k_UpperBoundary_Key, std::make_any(upperBounds.at(variant))); @@ -1093,30 +1093,30 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident threshold fallback selec } const auto thresholded = input.replaceName("Thresholded"); const std::vector expected = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, outside); - REQUIRE(c15_test::Read(dataStructure, thresholded, shape, expectedStore) == expected); + REQUIRE(image_integration::Read(dataStructure, thresholded, shape, expectedStore) == expected); if(outside == 7) { // CC treats all nonzero values as foreground, including the documented expert Outside Value. - const auto direct = c15_test::Components(dataStructure, thresholded, "DirectComponents"); - REQUIRE(c15_test::Read(dataStructure, direct, shape, expectedStore) == std::vector{1, 1, 1}); + const auto direct = image_integration::Components(dataStructure, thresholded, "DirectComponents"); + REQUIRE(image_integration::Read(dataStructure, direct, shape, expectedStore) == std::vector{1, 1, 1}); } - const auto desired = c15_test::Select(dataStructure, thresholded, 1, 1, "DesiredInside"); + const auto desired = image_integration::Select(dataStructure, thresholded, 1, 1, "DesiredInside"); const std::vector expectedDesired = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); - REQUIRE(c15_test::Read(dataStructure, desired, shape, expectedStore) == expectedDesired); - const auto labels = c15_test::Components(dataStructure, desired, "Components"); + REQUIRE(image_integration::Read(dataStructure, desired, shape, expectedStore) == expectedDesired); + const auto labels = image_integration::Components(dataStructure, desired, "Components"); const std::vector expectedLabels = variant == 0 ? std::vector{0, 1} : std::vector(noFinite ? 3 : 2, 0); - REQUIRE(c15_test::Read(dataStructure, labels, shape, expectedStore) == expectedLabels); - REQUIRE(c15_test::Read(dataStructure, thresholded, shape, expectedStore) == expected); + REQUIRE(image_integration::Read(dataStructure, labels, shape, expectedStore) == expectedLabels); + REQUIRE(image_integration::Read(dataStructure, thresholded, shape, expectedStore) == expected); if(noFinite) { - const auto sourceValues = c15_test::Read(dataStructure, input, shape, expectedStore); + const auto sourceValues = image_integration::Read(dataStructure, input, shape, expectedStore); REQUIRE(std::isnan(sourceValues[0])); REQUIRE(sourceValues[1] == -inf); REQUIRE(sourceValues[2] == inf); } else { - REQUIRE(c15_test::Read(dataStructure, input, shape, expectedStore) == std::vector{100, 102}); + REQUIRE(image_integration::Read(dataStructure, input, shape, expectedStore) == std::vector{100, 102}); } UnitTest::CheckArraysInheritTupleDims(dataStructure); } diff --git a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp index 588114bb88..946eb21371 100644 --- a/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/WriteImageTest.cpp @@ -1,4 +1,4 @@ -#include "C15CompatibilityTestUtils.hpp" +#include "ImageFilterIntegrationTestUtils.hpp" #include "NXImageProcessing/Filters/AcosImageFilter.hpp" #include "NXImageProcessing/Filters/AsinImageFilter.hpp" #include "NXImageProcessing/Filters/TanImageFilter.hpp" @@ -1095,7 +1095,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: raw export retains warnings from else { SIMPLNX_RESULT_REQUIRE_VALID(result.result); - REQUIRE(c15_test::Decode(secondSlice, 2, 1, 1) == std::vector{13, 17}); + REQUIRE(image_integration::Decode(secondSlice, 2, 1, 1) == std::vector{13, 17}); } const auto& warnings = result.result.warnings(); REQUIRE(warnings.size() == 2); @@ -1103,7 +1103,7 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: raw export retains warnings from REQUIRE(warnings.at(0).message == "Controlled Write Image read warning for XY slice 0."); REQUIRE(warnings.at(1).code == -27904); REQUIRE(warnings.at(1).message == "Controlled Write Image read warning for XY slice 1."); - REQUIRE(c15_test::Decode(firstSlice, 2, 1, 1) == std::vector{7, 11}); + REQUIRE(image_integration::Decode(firstSlice, 2, 1, 1) == std::vector{7, 11}); REQUIRE(std::distance(fs::directory_iterator(output.path()), fs::directory_iterator{}) == 2); UnitTest::CheckArraysInheritTupleDims(dataStructure); } @@ -2955,13 +2955,14 @@ TEST_CASE("NXImageProcessing::WriteImageFilter: rejects a source-group grant sma } // NOLINTBEGIN(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) Catch2 and simplnx assertion macros trigger these checks. -TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tangent persist and export ordered fractions", "[NXImageProcessing][C15Compatibility][Issue1874][WriteImageFilter]") +TEST_CASE("NXImageProcessing::WriteImageFilter: resident integration principal angles and tangent persist and export ordered fractions", + "[NXImageProcessing][ImageFilterIntegration][Issue1874][WriteImageFilter]") { const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); UnitTest::AlgorithmTestScope scope(scenario); const usize operation = GENERATE(usize{0}, usize{1}, usize{2}); CAPTURE(operation, scenario); - const c15_test::Directory output; + const image_integration::Directory output; using InputSamplesType = std::array; using OutputSamplesType = std::array; constexpr auto piRadians = std::numbers::pi_v; @@ -2975,23 +2976,23 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan const auto& expected = outputSamples.at(operation); DataStructure source; const auto input = recon_test::BuildImageFromPattern(source, 3, 1, 2, original); - c15_test::SetGeometry(source); - REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); + image_integration::SetGeometry(source); + REQUIRE(image_integration::Read(source, input, {2, 1, 3}, scope) == original); const std::string outputName = operation == 2 ? "Tangent" : "Radians"; DataPath quantitative; switch(operation) { case 0: - quantitative = c15_test::Pointwise(source, input, outputName); + quantitative = image_integration::Pointwise(source, input, outputName); break; case 1: - quantitative = c15_test::Pointwise(source, input, outputName); + quantitative = image_integration::Pointwise(source, input, outputName); break; default: - quantitative = c15_test::Pointwise(source, input, outputName); + quantitative = image_integration::Pointwise(source, input, outputName); break; } - const auto values = c15_test::Read(source, quantitative, {2, 1, 3}, scope); + const auto values = image_integration::Read(source, quantitative, {2, 1, 3}, scope); for(usize index = 0; index < expected.size(); ++index) { CAPTURE(index); @@ -3008,27 +3009,27 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan // Geometry length units are a separate .dream3d field. const auto save = DREAM3D::WriteFile(output.path() / "quantitative.dream3d", source); SIMPLNX_RESULT_REQUIRE_VALID(save); - c15_test::CheckSaved(output.path() / "quantitative.dream3d", quantitative, {2, 1, 3}, values); + image_integration::CheckSaved(output.path() / "quantitative.dream3d", quantitative, {2, 1, 3}, values); auto load = DREAM3D::LoadDataStructure(output.path() / "quantitative.dream3d"); SIMPLNX_RESULT_REQUIRE_VALID(load); auto& reloaded = load.value(); - REQUIRE(c15_test::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); - c15_test::CheckGeometry(reloaded, {3, 1, 2}); + REQUIRE(image_integration::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); + image_integration::CheckGeometry(reloaded, {3, 1, 2}); const WriteImageFilter writer; - c15_test::Execute(writer, reloaded, c15_test::WriteArguments(quantitative, output.path() / "raw.mha"), scope); + image_integration::Execute(writer, reloaded, image_integration::WriteArguments(quantitative, output.path() / "raw.mha"), scope); if(operation == 0) { - c15_test::Execute(writer, reloaded, c15_test::WriteArguments(quantitative, output.path() / "raw.tif"), scope); - auto displayArgs = c15_test::WriteArguments(quantitative, output.path() / "display.png"); + image_integration::Execute(writer, reloaded, image_integration::WriteArguments(quantitative, output.path() / "raw.tif"), scope); + auto displayArgs = image_integration::WriteArguments(quantitative, output.path() / "display.png"); displayArgs.insertOrAssign(WriteImageFilter::k_CreateColorTable_Key, std::make_any(true)); displayArgs.insertOrAssign(WriteImageFilter::k_SelectedPreset_Key, std::make_any("Grayscale")); - c15_test::Execute(writer, reloaded, displayArgs, scope); + image_integration::Execute(writer, reloaded, displayArgs, scope); } for(usize slice = 0; slice < 2; ++slice) { const std::string suffix = slice == 0 ? "_000" : "_001"; - auto raw = c15_test::ReadMha(output.path() / (std::string("raw") + suffix + ".mha")); - const auto pixels = c15_test::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 3}, scope); + auto raw = image_integration::ReadMha(output.path() / (std::string("raw") + suffix + ".mha")); + const auto pixels = image_integration::Read(raw, DataPath({"Image Geometry", "CellData", "Pixels"}), {1, 1, 3}, scope); const ImageGeom* rawGeometryPtr = nullptr; REQUIRE_NOTHROW(rawGeometryPtr = &raw.getDataRefAs(DataPath({"Image Geometry"}))); REQUIRE(rawGeometryPtr->getDimensions() == SizeVec3{3, 1, 1}); @@ -3042,7 +3043,7 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan if(operation == 0) { const auto tiffPath = output.path() / (std::string("raw") + suffix + ".tif"); - const auto tiff = c15_test::Decode(tiffPath, 3, 1); + const auto tiff = image_integration::Decode(tiffPath, 3, 1); REQUIRE(tiff == pixels); const auto imageIO = CreateImageIO(tiffPath); SIMPLNX_RESULT_REQUIRE_VALID(imageIO); @@ -3052,14 +3053,14 @@ TEST_CASE("NXImageProcessing::C15Compatibility resident principal angles and tan REQUIRE(metadata.value().origin.has_value()); REQUIRE(metadata.value().spacing == FloatVec3{1, 2, 1}); REQUIRE(metadata.value().origin == FloatVec3{4, 5, 0}); - const auto display = c15_test::Decode(output.path() / (std::string("display") + suffix + ".png"), 3, 1, 3); + const auto display = image_integration::Decode(output.path() / (std::string("display") + suffix + ".png"), 3, 1, 3); const std::vector expectedDisplay = slice == 0 ? std::vector{0, 0, 0, 127, 127, 127, 255, 255, 255} : std::vector{255, 255, 255, 127, 127, 127, 0, 0, 0}; REQUIRE(display == expectedDisplay); } } - REQUIRE(c15_test::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); - REQUIRE(c15_test::Read(source, quantitative, {2, 1, 3}, scope) == values); - REQUIRE(c15_test::Read(source, input, {2, 1, 3}, scope) == original); + REQUIRE(image_integration::Read(reloaded, quantitative, {2, 1, 3}, scope) == values); + REQUIRE(image_integration::Read(source, quantitative, {2, 1, 3}, scope) == values); + REQUIRE(image_integration::Read(source, input, {2, 1, 3}, scope) == original); UnitTest::CheckArraysInheritTupleDims(source); UnitTest::CheckArraysInheritTupleDims(reloaded); } From 3414c842b31c96b2007e48700438b4de09eb425f Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:31 -0500 Subject: [PATCH 65/98] TEST: Cover allocation failure during watershed marker restoration Signed-off-by: Jessica Marquis --- ...calWatershedFromMarkersImageFilterTest.cpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index e441e4b3f0..b244e50ee5 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -870,18 +870,32 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: rest source.setValue(valueCount - 1, 4294967295U); source.warnOnRead(); outputStorePtr->warnOnWrite(); - bool expectFailure = false; + bool failFinalWrite = false; + bool failRestoreAllocation = false; SECTION("all delivered warnings survive success") { } SECTION("earlier warnings survive final write failure") { outputStorePtr->failWrite(2); - expectFailure = true; + failFinalWrite = true; + } + SECTION("allocation failure during the last restoration read") + { + source.failRead(2, true); + failRestoreAllocation = true; } const std::atomic_bool shouldCancel = false; const auto result = ImageProcessing::detail::NarrowUInt32StoreToIntegerArray(source, dataStructure, DataPath({"Image Geometry", "CellData", "Output"}), shouldCancel); - if(expectFailure) + if(failRestoreAllocation) + { + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().size() == 1); + REQUIRE(result.errors().front().code == -79045); + REQUIRE(result.errors().front().message.find("while restoring marker IDs") != std::string::npos); + REQUIRE(outputStorePtr->getValue(valueCount - 1) == 0); + } + else if(failFinalWrite) { SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().front().code == -1874152); @@ -892,10 +906,10 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: rest REQUIRE(outputStorePtr->getValue(valueCount - 1) == 4294967295ULL); } REQUIRE(outputStorePtr->getValue(0) == 1); - REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874153; }) == 2); - REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874154; }) == (expectFailure ? 1 : 2)); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874153; }) == (failRestoreAllocation ? 1 : 2)); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874154; }) == (failFinalWrite || failRestoreAllocation ? 1 : 2)); REQUIRE(source.readCount() == 2); - REQUIRE(outputStorePtr->writeCount() == 2); + REQUIRE(outputStorePtr->writeCount() == (failRestoreAllocation ? 1 : 2)); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) From 28a1ded0ef5e298d30e9fb7a6b71e9f959dad7a7 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:32 -0500 Subject: [PATCH 66/98] REV: Give label and marker failures distinct diagnostics Signed-off-by: Jessica Marquis --- .../utils/ImageFilterExecutorDetail.hpp | 24 +++++++---- .../test/ConnectedComponentEngineTest.cpp | 16 ++++---- ...calWatershedFromMarkersImageFilterTest.cpp | 21 +++++----- ...imumConnectedComponentsImageFilterTest.cpp | 4 +- .../ConnectedComponentEngine.hpp | 41 ++++++++++++------- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp index b3b0efa80e..62e8975175 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterExecutorDetail.hpp @@ -1015,6 +1015,11 @@ Result<> SubtractHConvexContrast(const AbstractDataStore& input, const Abstra } } +inline constexpr int32 k_MarkerIdConversionError = -79073; +inline constexpr int32 k_MarkerIdConversionWarning = -79074; +inline constexpr int32 k_MarkerIdCopyAllocationFailed = -79075; +inline constexpr int32 k_MarkerIdRestoreAllocationFailed = -79076; + /** @brief Constant-size native context for the existing bounded marker-copy pass. */ template class MarkerIdConversionState @@ -1055,13 +1060,13 @@ class MarkerIdConversionState if(handling == MarkerIdConversionHandling::Error) { return MergeResults(std::move(result), - MakeErrorResult(-79073, context + - "Execution stopped before flooding. Use IDs that round-trip, or select 'Warn and Continue (Legacy Behavior)' under " - "'Marker ID Conversion Handling' only if you intentionally accept seed loss, renamed IDs, merged basins, and incorrect region measurements.")); + MakeErrorResult(k_MarkerIdConversionError, + context + "Execution stopped before flooding. Use IDs that round-trip, or select 'Warn and Continue (Legacy Behavior)' under " + "'Marker ID Conversion Handling' only if you intentionally accept seed loss, renamed IDs, merged basins, and incorrect region measurements.")); } - return MergeResults(std::move(result), - MakeWarningVoidResult(79074, context + "'Warn and Continue (Legacy Behavior)' retains the old conversion and flood. Seeds can disappear or merge, IDs can be " - "renamed, and basin boundaries, region sizes, and downstream feature measurements can be incorrect.")); + return MergeResults(std::move(result), MakeWarningVoidResult(k_MarkerIdConversionWarning, + context + "'Warn and Continue (Legacy Behavior)' retains the old conversion and flood. Seeds can disappear or merge, IDs can be " + "renamed, and basin boundaries, region sizes, and downstream feature measurements can be incorrect.")); } private: @@ -1135,8 +1140,9 @@ struct WidenIntegerArrayToUInt32Fn return conversionState.finish(srcPath, ConversionHandling, std::move(accumulated)); } catch(const std::bad_alloc&) { - return conversionState.finish(srcPath, ConversionHandling, - MergeResults(std::move(accumulated), MakeErrorResult(-79045, "Morphological watershed could not allocate working storage while copying marker IDs."))); + return conversionState.finish( + srcPath, ConversionHandling, + MergeResults(std::move(accumulated), MakeErrorResult(k_MarkerIdCopyAllocationFailed, "Morphological watershed could not allocate working storage while copying marker IDs."))); } } }; @@ -1209,7 +1215,7 @@ struct NarrowUInt32StoreToIntegerArrayFn return accumulated; } catch(const std::bad_alloc&) { - return MergeResults(std::move(accumulated), MakeErrorResult(-79045, "Morphological watershed could not allocate working storage while restoring marker IDs.")); + return MergeResults(std::move(accumulated), MakeErrorResult(k_MarkerIdRestoreAllocationFailed, "Morphological watershed could not allocate working storage while restoring marker IDs.")); } } }; diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp index 5943f7e107..b2969caba6 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentEngineTest.cpp @@ -1161,7 +1161,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: native provisional capac else { SIMPLNX_RESULT_REQUIRE_INVALID(strictResult); - REQUIRE(strictResult.errors().front().code == -8771); + REQUIRE(strictResult.errors().front().code == -29301); REQUIRE(strictResult.errors().front().message.find(std::to_string(candidate)) != std::string::npos); REQUIRE(strictResult.errors().front().message.find("Warn and Continue (Legacy Behavior)") != std::string::npos); } @@ -1177,7 +1177,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: native provisional capac const auto legacyFinished = legacyState.finish({}); SIMPLNX_RESULT_REQUIRE_VALID(legacyFinished); REQUIRE(legacyFinished.warnings().size() == 1); - REQUIRE(legacyFinished.warnings().front().code == 8772); + REQUIRE(legacyFinished.warnings().front().code == -29302); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -1285,7 +1285,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: capacity warning context REQUIRE(result.errors().front().code == -8794); REQUIRE(result.warnings().size() == 2); REQUIRE(result.warnings().at(0).code == 8793); - REQUIRE(result.warnings().at(1).code == 8772); + REQUIRE(result.warnings().at(1).code == -29302); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -1325,7 +1325,7 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: seeded warning context s result = state.finish(std::move(result)); REQUIRE(warnings == 1); REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 8772); + REQUIRE(result.warnings().front().code == -29302); if(fault == ConnectedComponentReadFaultType::Cancel) { SIMPLNX_RESULT_REQUIRE_VALID(result); @@ -1365,13 +1365,13 @@ TEST_CASE("NXImageProcessing::ConnectedComponentEngine: allocation addressabilit int32 countCode = -8791; if(fault == ConnectedComponentReadFaultType::BadAllocOnce) { - labelCode = -8773; - countCode = -8774; + labelCode = -29303; + countCode = -29304; } else if(fault == ConnectedComponentReadFaultType::LengthError) { - labelCode = -8775; - countCode = -8776; + labelCode = -29305; + countCode = -29306; } REQUIRE(labelResult.errors().front().code == labelCode); REQUIRE(labelResult.warnings().empty()); diff --git a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp index b244e50ee5..36bf1dd795 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologicalWatershedFromMarkersImageFilterTest.cpp @@ -210,7 +210,7 @@ void requireMarkerLine(const std::vector& markers, const std::vector& expe if(changedCount != 0) { // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. - REQUIRE(result.result.warnings().front().code == 79074); + REQUIRE(result.result.warnings().front().code == -79074); // NOLINTNEXTLINE(bugprone-chained-comparison) Catch2 comparison decomposition. REQUIRE(result.result.warnings().front().message.find(std::to_string(changedCount) + " changed marker occurrence(s)") != std::string::npos); } @@ -494,7 +494,8 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: defa // Omit the new choice deliberately: existing pipelines must receive the safe default. const auto result = filter.execute(dataStructure, args, nullptr, messageHandler); requireSinglePixelMarkerOutput(dataStructure); - REQUIRE(result.result.invalid()); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while) simplnx assertion macro. + SIMPLNX_RESULT_REQUIRE_INVALID(result.result); REQUIRE(result.result.errors().size() == 1); REQUIRE(result.result.errors().front().code == -79073); REQUIRE(result.result.errors().front().message.find("Marker") != std::string::npos); @@ -617,7 +618,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: mark { SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().code == -79074); REQUIRE(destination.writeCount() == 2); REQUIRE(destination.getValue(changedIndex) == 1); messagePtr = &result.warnings().front().message; @@ -674,7 +675,7 @@ TEMPLATE_TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFil { SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().code == -79074); REQUIRE(result.warnings().front().message.find("2 changed marker occurrence(s)") != std::string::npos); messagePtr = &result.warnings().front().message; } @@ -706,7 +707,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy SIMPLNX_RESULT_REQUIRE_VALID(result); REQUIRE(shouldCancel.load()); REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().code == -79074); REQUIRE(result.warnings().front().message.find("1 changed marker occurrence(s)") != std::string::npos); REQUIRE(markerStorePtr->readCount() == 1); REQUIRE(destination.writeCount() == 1); @@ -742,7 +743,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy SECTION("later marker read throws allocation failure") { markerStorePtr->failRead(2, true); - expectedCode = -79045; + expectedCode = -79075; } const std::atomic_bool shouldCancel = false; const auto result = ImageProcessing::detail::WidenIntegerArrayToUInt32(dataStructure, DataPath({"Image Geometry", "CellData", "Marker"}), destination, shouldCancel, @@ -751,7 +752,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: copy REQUIRE(result.errors().size() == 1); REQUIRE(result.errors().front().code == expectedCode); REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().code == -79074); REQUIRE(result.warnings().front().message.find("1 changed marker occurrence(s)") != std::string::npos); REQUIRE(markerStorePtr->readCount() <= 2); REQUIRE(destination.writeCount() == 1); @@ -816,7 +817,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: conv REQUIRE(result.errors().front().code == expectedCode); } REQUIRE(result.warnings().size() == 1); - REQUIRE(result.warnings().front().code == 79074); + REQUIRE(result.warnings().front().code == -79074); UnitTest::CheckArraysInheritTupleDims(dataStructure); } // NOLINTEND(bugprone-chained-comparison,cppcoreguidelines-avoid-do-while) @@ -843,7 +844,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: succ SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().front().code == -1874151); REQUIRE(result.warnings().size() == 3); - REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 79074; }) == 1); + REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == -79074; }) == 1); REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874153; }) == 1); REQUIRE(std::ranges::count_if(result.warnings(), [](const Warning& warning) { return warning.code == 1874154; }) == 1); REQUIRE(markerStorePtr->readCount() == 2); @@ -891,7 +892,7 @@ TEST_CASE("NXImageProcessing::MorphologicalWatershedFromMarkersImageFilter: rest { SIMPLNX_RESULT_REQUIRE_INVALID(result); REQUIRE(result.errors().size() == 1); - REQUIRE(result.errors().front().code == -79045); + REQUIRE(result.errors().front().code == -79076); REQUIRE(result.errors().front().message.find("while restoring marker IDs") != std::string::npos); REQUIRE(outputStorePtr->getValue(valueCount - 1) == 0); } diff --git a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp index 07188d79ac..30417721b6 100644 --- a/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ThresholdMaximumConnectedComponentsImageFilterTest.cpp @@ -707,11 +707,11 @@ TEST_CASE("NXImageProcessing::ThresholdMaximumConnectedComponentsImageFilter: co int32 expectedCode = -28631; if(fault == ThresholdFaultType::BadAllocation) { - expectedCode = -8774; + expectedCode = -29304; } else if(fault == ThresholdFaultType::LengthError) { - expectedCode = -8776; + expectedCode = -29306; } REQUIRE(result.errors().front().code == expectedCode); REQUIRE(result.warnings().size() == 1); diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index 29d0bc2f7c..69f7621caa 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -33,8 +33,16 @@ namespace nx::core::ImageProcessing { +inline constexpr int32 k_ProvisionalLabelOverflow = -29301; +inline constexpr int32 k_ProvisionalLabelWrapWarning = -29302; +inline constexpr int32 k_LabelAllocationFailed = -29303; +inline constexpr int32 k_CountAllocationFailed = -29304; +inline constexpr int32 k_LabelSizeExceeded = -29305; +inline constexpr int32 k_CountSizeExceeded = -29306; + namespace detail { + using ProgressCallback = std::function; using BeginProgressPhaseCallback = std::function; @@ -465,17 +473,19 @@ class ConnectedComponentLabelState if(m_Handling == ConnectedComponentLabelLimitHandling::Error) { return MakeErrorResult( - -8771, fmt::format("Connected-component provisional run ID {} exceeds the uint32 limit of 4,294,967,295. Execution stopped before this ID was created. " - "Reduce the number of foreground runs. In Connected Component only, select 'Warn and Continue (Legacy Behavior)' under 'Provisional Label Limit Handling' " - "only if you intentionally accept lost foreground, merged components, and incorrect counts and statistics.", - candidate)); + k_ProvisionalLabelOverflow, + fmt::format("Connected-component provisional run ID {} exceeds the uint32 limit of 4,294,967,295. Execution stopped before this ID was created. " + "Reduce the number of foreground runs. In Connected Component only, select 'Warn and Continue (Legacy Behavior)' under 'Provisional Label Limit Handling' " + "only if you intentionally accept lost foreground, merged components, and incorrect counts and statistics.", + candidate)); } if(!m_WarningIssued) { - m_Warnings = MakeWarningVoidResult(8772, fmt::format("Connected-component provisional run ID {} exceeds 4,294,967,295. 'Warn and Continue (Legacy Behavior)' retains uint32 wrap to " - "background zero and reused IDs. Foreground can disappear, components can merge, and counts and downstream statistics can be incorrect. " - "Execution routes can give different incorrect results. Memory and I/O failures still stop execution.", - candidate)); + m_Warnings = MakeWarningVoidResult(k_ProvisionalLabelWrapWarning, + fmt::format("Connected-component provisional run ID {} exceeds 4,294,967,295. 'Warn and Continue (Legacy Behavior)' retains uint32 wrap to " + "background zero and reused IDs. Foreground can disappear, components can merge, and counts and downstream statistics can be incorrect. " + "Execution routes can give different incorrect results. Memory and I/O failures still stop execution.", + candidate)); m_WarningIssued = true; m_MessageHandler.sendWarningMessage(m_Warnings.warnings().front().message); } @@ -1616,12 +1626,14 @@ Result<> LabelConnectedComponents(const AbstractDataStore& inStore, AbstractD detail::LabelConnectedComponentsImpl(inStore, outStore, dims, pred, fullyConnected, shouldCancel, target2DBytes, beginProgressPhase, reportCount, reportPercent, labelState)); } catch(const std::bad_alloc&) { - return labelState.finish(MakeErrorResult(-8773, "Connected-component labeling could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory. " - "Legacy label continuation does not waive memory failures.")); + return labelState.finish(MakeErrorResult(k_LabelAllocationFailed, + "Connected-component labeling could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory. " + "Legacy label continuation does not waive memory failures.")); } catch(const std::length_error&) { - return labelState.finish(MakeErrorResult(-8775, "Connected-component labeling exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs. " - "Legacy label continuation does not increase storage capacity.")); + return labelState.finish(MakeErrorResult(k_LabelSizeExceeded, + "Connected-component labeling exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs. " + "Legacy label continuation does not increase storage capacity.")); } } @@ -1680,10 +1692,11 @@ Result CountConnectedComponents(const AbstractDataStore& inStore, con return {{numComponents}}; } catch(const std::bad_alloc&) { - return MakeErrorResult(-8774, "Connected-component counting could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory."); + return MakeErrorResult(k_CountAllocationFailed, + "Connected-component counting could not allocate its run tables or working buffers. Reduce the number of foreground runs or provide more memory."); } catch(const std::length_error&) { - return MakeErrorResult(-8776, "Connected-component counting exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs."); + return MakeErrorResult(k_CountSizeExceeded, "Connected-component counting exceeded an addressable run-table or buffer size. Reduce the image dimensions or number of foreground runs."); } } } // namespace nx::core::ImageProcessing From b7636cc09e3d4b7cc8d26a704a1324448e353149 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:34 -0500 Subject: [PATCH 67/98] STY: Name the default label limit and marker conversion choices Signed-off-by: Jessica Marquis --- .../Filters/ConnectedComponentImageFilter.cpp | 5 +++-- .../Filters/MorphologicalWatershedFromMarkersImageFilter.cpp | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp index 82aabfa63f..e3670ce96f 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ConnectedComponentImageFilter.cpp @@ -3,15 +3,16 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" #include "simplnx/Common/Result.hpp" +#include "simplnx/Common/TypeTraits.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" #include "simplnx/Parameters/BoolParameter.hpp" #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/Parameters/DataObjectNameParameter.hpp" #include "simplnx/Parameters/GeometrySelectionParameter.hpp" -#include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp" #include #include @@ -56,7 +57,7 @@ Parameters ConnectedComponentImageFilter::parameters() const "and counts and downstream statistics can be incorrect. A wrapped zero from a previous row can erase a neighboring component in scanline processing. " "Tiled processing skips that zero and can give different labels. Choose Warn only if you accept these consequences. " "It does not increase label capacity or prevent memory or I/O errors.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); + to_underlying(ImageProcessing::ConnectedComponentLabelLimitHandling::Error), ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp index 68e9ba6932..44506bfbff 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/MorphologicalWatershedFromMarkersImageFilter.cpp @@ -2,6 +2,7 @@ #include "NXImageProcessing/utils/ImageFilterExecutors.hpp" +#include "simplnx/Common/TypeTraits.hpp" #include "simplnx/DataStructure/IDataArray.hpp" #include "simplnx/Filter/Actions/CreateArrayAction.hpp" #include "simplnx/Parameters/ArraySelectionParameter.hpp" @@ -61,7 +62,7 @@ Parameters MorphologicalWatershedFromMarkersImageFilter::parameters() const "and make downstream feature measurements incorrect. Choose Warn only if you intentionally accept these consequences; it does not preserve unsupported IDs. " "Output keeps the marker type. All 8/16/32-bit integer values, including signed negatives, are lossless. The int64/uint64 lossless range is 0 through 4294967295, inclusive. " "Zero is unmarked; UINT32_MAX is a valid seed. For example, uint64 4294967296 becomes 0, 4294967297 becomes 1, and int64 -1 becomes 4294967295.", - 0, ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); + to_underlying(ImageProcessing::MarkerIdConversionHandling::Error), ChoicesParameter::Choices{"Error", "Warn and Continue (Legacy Behavior)"})); params.insertSeparator(Parameters::Separator{"Input Cell Data"}); params.insert(std::make_unique(k_InputImageGeomPath_Key, "Image Geometry", "Select the Image Geometry Group from the DataStructure.", DataPath({"Image Geometry"}), From 02439adc275755cc84a6f95e87c4f61c4708eb1d Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:35 -0500 Subject: [PATCH 68/98] TEST: Observe the effective Connected Component label policy Signed-off-by: Jessica Marquis --- CMakeLists.txt | 1 + .../ConnectedComponentImageFilterTest.cpp | 8 ++++++ .../ConnectedComponentEngine.hpp | 15 +++++++++++ .../ConnectedComponentPolicyObserver.cpp | 27 +++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/simplnx/Utilities/ImageProcessing/ConnectedComponentPolicyObserver.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3942d9ee9b..1d20290ab3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1185,6 +1185,7 @@ if(SIMPLNX_ENABLE_PACKAGING) endif() if(SIMPLNX_BUILD_TESTS) + target_sources(simplnx PRIVATE "${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ConnectedComponentPolicyObserver.cpp") target_compile_definitions(simplnx PUBLIC "SIMPLNX_BUILD_TESTS=1") add_subdirectory(test) endif() diff --git a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp index b4dbcba4d2..d16e991ea2 100644 --- a/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ConnectedComponentImageFilterTest.cpp @@ -20,7 +20,9 @@ #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingConstants.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp" #include @@ -159,8 +161,14 @@ TEST_CASE("NXImageProcessing::ConnectedComponentImageFilter: both policies prese REQUIRE(actionPtr->type() == DataType::uint32); REQUIRE(actionPtr->dims() == ShapeType{dimZ, 2, 2}); REQUIRE(actionPtr->componentDims() == ShapeType{1}); + // Reset the observation before the filter constructs the labeling state on this thread. + (void)ImageProcessing::detail::TakeConnectedComponentPolicyForTesting(); const auto executeResult = filter.execute(dataStructure, arguments); SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + const auto observed = ImageProcessing::detail::TakeConnectedComponentPolicyForTesting(); + REQUIRE(observed.has_value()); + const auto expectedPolicy = handling == 0 ? ImageProcessing::ConnectedComponentLabelLimitHandling::Error : ImageProcessing::ConnectedComponentLabelLimitHandling::WarnAndContinueLegacy; + REQUIRE(*observed == expectedPolicy); REQUIRE(executeResult.result.warnings().empty()); const DataPath outputPath = inputPath.replaceName("Output"); const IDataArray* arrayPtr = nullptr; diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp index 69f7621caa..85db01888b 100644 --- a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp @@ -42,6 +42,18 @@ inline constexpr int32 k_CountSizeExceeded = -29306; namespace detail { +#if defined(SIMPLNX_BUILD_TESTS) && SIMPLNX_BUILD_TESTS +/** + * @brief Records the effective label policy in the calling test thread when the label state is constructed. + * @param handling Policy supplied to the newly constructed label state. + */ +SIMPLNX_EXPORT void RecordConnectedComponentPolicyForTesting(ConnectedComponentLabelLimitHandling handling) noexcept; +/** + * @brief Returns and clears the policy last recorded on this thread. An empty value means no state was constructed. + * @return The last observed policy, or an empty value when there was no observation. + */ +SIMPLNX_EXPORT std::optional TakeConnectedComponentPolicyForTesting() noexcept; +#endif using ProgressCallback = std::function; using BeginProgressPhaseCallback = std::function; @@ -459,6 +471,9 @@ class ConnectedComponentLabelState : m_Handling(handling) , m_MessageHandler(messageHandler) { +#if defined(SIMPLNX_BUILD_TESTS) && SIMPLNX_BUILD_TESTS + RecordConnectedComponentPolicyForTesting(m_Handling); +#endif } ~ConnectedComponentLabelState() noexcept = default; ConnectedComponentLabelState(const ConnectedComponentLabelState&) = delete; diff --git a/src/simplnx/Utilities/ImageProcessing/ConnectedComponentPolicyObserver.cpp b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentPolicyObserver.cpp new file mode 100644 index 0000000000..ed1b403d88 --- /dev/null +++ b/src/simplnx/Utilities/ImageProcessing/ConnectedComponentPolicyObserver.cpp @@ -0,0 +1,27 @@ +#include "simplnx/Utilities/ImageProcessing/ConnectedComponentEngine.hpp" +#include "simplnx/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp" + +#include +#include + +namespace nx::core::ImageProcessing::detail +{ +namespace +{ +std::optional& lastLabelPolicy() noexcept +{ + static thread_local std::optional lastPolicy; + return lastPolicy; +} +} // namespace + +void RecordConnectedComponentPolicyForTesting(ConnectedComponentLabelLimitHandling handling) noexcept +{ + lastLabelPolicy() = handling; +} + +std::optional TakeConnectedComponentPolicyForTesting() noexcept +{ + return std::exchange(lastLabelPolicy(), std::nullopt); +} +} // namespace nx::core::ImageProcessing::detail From 529e939b94aa78207daa9fa951dd74f73286d1af Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:36 -0500 Subject: [PATCH 69/98] STY: Order the installed image processing headers Signed-off-by: Jessica Marquis --- CMakeLists.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d20290ab3..3e780bbd09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -604,18 +604,18 @@ set(SIMPLNX_HDRS ${SIMPLNX_SOURCE_DIR}/Utilities/MemoryUtilities.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/CacheMemoryBudgetManager.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ColorToGrayScale.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ConnectedComponentEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/HistogramEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageGeometryCrop.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageGeometryResample.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/WorkingMemory.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/TerminalImageConversion.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/OtsuImageEngine.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ConnectedComponentEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ImageProcessingOperationTypes.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/OtsuImageEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/PointwiseEngine.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/StreamingStatistics.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/SweepTemporaryStore.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/HistogramEngine.hpp - ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/PointwiseEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/TerminalImageConversion.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/ThresholdMaximumConnectedComponentsEngine.hpp + ${SIMPLNX_SOURCE_DIR}/Utilities/ImageProcessing/WorkingMemory.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ProgressEstimator.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/ThrottledMessageHandler.hpp ${SIMPLNX_SOURCE_DIR}/Utilities/StringUtilities.hpp From 57d1fd2c0be905cc08e2f9f28dbfb9ee85d6d255 Mon Sep 17 00:00:00 2001 From: Jessica Marquis Date: Wed, 18 Nov 2026 18:20:37 -0500 Subject: [PATCH 70/98] REF: Remove the retired histogram executor templates Signed-off-by: Jessica Marquis --- .../utils/ImageFilterTemplateExecutors.hpp | 72 ------------------- 1 file changed, 72 deletions(-) diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp index 170f23d3f0..f0c8ab609b 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/utils/ImageFilterTemplateExecutors.hpp @@ -6,14 +6,12 @@ #include "simplnx/Utilities/ImageProcessing/BoxNeighborhoodEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ContourEngine.hpp" #include "simplnx/Utilities/ImageProcessing/FiniteDifferenceEngine.hpp" -#include "simplnx/Utilities/ImageProcessing/HistogramEngine.hpp" #include "simplnx/Utilities/ImageProcessing/ImageProcessingFilterUtilities.hpp" #include "simplnx/Utilities/ImageProcessing/PointwiseEngine.hpp" #include "simplnx/Utilities/ImageProcessing/StreamingStatistics.hpp" #include #include -#include #include namespace nx::core::ImageProcessing @@ -134,59 +132,6 @@ struct ContourExecuteFn } }; -template