Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions Common/GTesting/elxImpactMetricGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include <sstream>
#include "itkParameterMapInterface.h"

using elx::formatParameterStringByDimensionAndLevel;
using elx::GetBooleanVectorFromString;
using elx::GetVectorFromString;
using elx::groupStrByDimensions;

namespace
{
// Helper to format vector outputs
template <typename T>
std::string
Expand All @@ -25,6 +32,7 @@ ExpectVectorEqual(const std::vector<T> & actual, const std::vector<T> & expected
{
EXPECT_EQ(actual, expected) << "Expected: " << vecToStr(expected) << "\nActual: " << vecToStr(actual);
}
} // namespace


GTEST_TEST(GetVectorFromString, LimitsAndParsing)
Expand Down Expand Up @@ -95,18 +103,15 @@ GTEST_TEST(FormatParameterStringByDimensionAndLevel, Basic)
config->Initialize(argMap, parameterMap);

// Level 0: read 3 individual values → expect "5 5 5"
std::string resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 0, 3);
std::string resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 0, 3);
EXPECT_EQ(resultStr, "5 5 5");

// Level 1: one line with 5 values → "11 11 11 29 29"
resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 1, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 1, 3);
EXPECT_EQ(resultStr, "11 11 11 29 29");

// Level 2: only "13" twice → expect fallback to fill 3 values
resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 2, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 2, 3);
EXPECT_EQ(resultStr, "13 13");

// --- Second case: PatchSize as float ---
Expand All @@ -115,31 +120,26 @@ GTEST_TEST(FormatParameterStringByDimensionAndLevel, Basic)
config->Initialize(argMap, parameterMap);

// Level 0: two values → repeat to fill 3
resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 0, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 0, 3);
EXPECT_EQ(resultStr, "6.0 6.0");

// Level 1: only "1.0 1.0"
resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 1, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 1, 3);
EXPECT_EQ(resultStr, "1.0 1.0");

// --- Third case: all entries are "6.0", just checking fallback filling ---

parameterMap["ImpactPatchSize"] = { "6.0", "6.0", "6.0", "6.0", "3.0 3.0 1.5 1.5 1.5", "1.0", "1.0" };
config->Initialize(argMap, parameterMap);

resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 0, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 0, 3);
EXPECT_EQ(resultStr, "6.0 6.0 6.0");

// Check fallback with single value
resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 1, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 1, 3);
EXPECT_EQ(resultStr, "6.0");

resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 2, 3);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 2, 3);
EXPECT_EQ(resultStr, "1.0 1.0");

// --- Fourth case: test imageDimension auto-detection via ImpactDimension ---
Expand All @@ -148,16 +148,13 @@ GTEST_TEST(FormatParameterStringByDimensionAndLevel, Basic)
parameterMap["ImpactPatchSize"] = { "5", "5", "13", "13", "13", "11 11 11 29 29" };
config->Initialize(argMap, parameterMap);

resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 0);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 0);
EXPECT_EQ(resultStr, "5 5");

resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 1);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 1);
EXPECT_EQ(resultStr, "13 13 13");

resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 2);
resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 2);
EXPECT_EQ(resultStr, "11 11 11 29 29");
}

Expand All @@ -172,8 +169,7 @@ GTEST_TEST(FormatParameterStringByDimensionAndLevel, EarlyStopDueToMissingParam)
parameterMap["ImpactPatchSize"] = { "5", "5" };
config->Initialize(argMap, parameterMap);

std::string resultStr =
formatParameterStringByDimensionAndLevel<elastix::Configuration>(config.GetPointer(), "Impact", "PatchSize", 0, 3);
std::string resultStr = formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 0, 3);

// Should stop early and return "5 5" (not enough to fill 3)
EXPECT_EQ(resultStr, "5 5");
Expand All @@ -188,7 +184,7 @@ GTEST_TEST(FormatParameterStringByDimensionAndLevel, LevelBeyondAvailable)
parameterMap["ImpactPatchSize"] = { "5", "5" }; // Only 2 values
config->Initialize(argMap, parameterMap);

std::string resultStr = formatParameterStringByDimensionAndLevel<elastix::Configuration>(
config.GetPointer(), "Impact", "PatchSize", 5, 3); // ask level 5
std::string resultStr =
formatParameterStringByDimensionAndLevel(config.GetPointer(), "Impact", "PatchSize", 5, 3); // ask level 5
EXPECT_EQ(resultStr, "");
}
31 changes: 14 additions & 17 deletions Components/Metrics/Impact/elxImpactMetric.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define elxImpactMetric_h

#include "elxIncludes.h" // include first to avoid MSVS warning
#include "elxConfiguration.h"
#include "itkImpactImageToImageMetric.h"
#include "itkVector.h"

Expand Down Expand Up @@ -261,8 +262,6 @@ class ITK_TEMPLATE_EXPORT ImpactMetric
GenerateModelsConfiguration(unsigned int level, std::string type, std::string mode, unsigned int imageDimension);
};

} // end namespace elastix

/**
* \brief Convert a string token to a typed value with bounds checking.
*
Expand Down Expand Up @@ -481,19 +480,17 @@ groupStrByDimensions(std::string valueStr, std::vector<unsigned int> dimensions)
* The output collects parameters for the requested level. If a value at that level
* contains multiple space-separated entries, it is returned directly.
*
* \tparam TConfiguration Type of the configuration (must provide ReadParameter).
* \param config Pointer to configuration object.
* \param prefix Parameter prefix (e.g., "Impact").
* \param parameterName Name of the parameter suffix (e.g., "PatchSize").
* \param level Level index to extract values from.
* \return Formatted string containing values.
*/
template <typename TConfiguration>
std::string
formatParameterStringByDimensionAndLevel(const TConfiguration * config,
const std::string & prefix,
const std::string & parameterName,
int level)
inline std::string
formatParameterStringByDimensionAndLevel(const Configuration * config,
const std::string & prefix,
const std::string & parameterName,
int level)
{
std::ostringstream paramStream;
std::ostringstream paramDefault;
Expand Down Expand Up @@ -547,21 +544,19 @@ formatParameterStringByDimensionAndLevel(const TConfiguration * config,
*
* Similar to the above function but uses a constant dimension instead of a per-level model dimension.
*
* \tparam TConfiguration Type of the configuration (must provide ReadParameter).
* \param config Pointer to configuration object.
* \param prefix Parameter prefix (e.g., "Impact").
* \param parameterName Name of the parameter (e.g., "PatchSize").
* \param level Level index to extract values from.
* \param dimension Fixed dimension used to select parameters.
* \return Formatted string containing values.
*/
template <typename TConfiguration>
std::string
formatParameterStringByDimensionAndLevel(const TConfiguration * config,
const std::string & prefix,
const std::string & parameterName,
int level,
unsigned int dimension)
inline std::string
formatParameterStringByDimensionAndLevel(const Configuration * config,
const std::string & prefix,
const std::string & parameterName,
int level,
unsigned int dimension)
{
std::ostringstream paramStream;
int paramIndex = 0;
Expand Down Expand Up @@ -606,6 +601,8 @@ formatParameterStringByDimensionAndLevel(const TConfiguration * config,
return paramStream.str();
} // end formatParameterStringByDimensionAndLevel

} // end namespace elastix


#ifndef ITK_MANUAL_INSTANTIATION
# include "elxImpactMetric.hxx"
Expand Down
21 changes: 9 additions & 12 deletions Components/Metrics/Impact/elxImpactMetric.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,23 @@ ImpactMetric<TElastix>::GenerateModelsConfiguration(unsigned int level,
std::vector<unsigned int> dimensions;
if (mode == "Static")
{
patchSizeStr = formatParameterStringByDimensionAndLevel<Configuration>(
this->GetConfiguration(), prefix, "PatchSize", level, imageDimension);
patchSizeStr =
formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "PatchSize", level, imageDimension);
for (int i = 0; i < modelsPathVec.size(); ++i)
dimensions.push_back(imageDimension);
if (patchSizeStr.empty() && level > 0)
{
patchSizeStr = formatParameterStringByDimensionAndLevel<Configuration>(
this->GetConfiguration(), prefix, "PatchSize", 0, imageDimension);
patchSizeStr =
formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "PatchSize", 0, imageDimension);
}
}
else
{
patchSizeStr =
formatParameterStringByDimensionAndLevel<Configuration>(this->GetConfiguration(), prefix, "PatchSize", level);
patchSizeStr = formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "PatchSize", level);
dimensions = modelsDimensionVec;
if (patchSizeStr.empty() && level > 0)
{
patchSizeStr =
formatParameterStringByDimensionAndLevel<Configuration>(this->GetConfiguration(), prefix, "PatchSize", 0);
patchSizeStr = formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "PatchSize", 0);
}
}
std::vector<std::string> patchSizeVec = groupStrByDimensions(patchSizeStr, dimensions);
Expand All @@ -138,17 +136,16 @@ ImpactMetric<TElastix>::GenerateModelsConfiguration(unsigned int level,
std::vector<std::string> voxelSizeVec;
if (mode == "Static")
{
voxelSizeStr = formatParameterStringByDimensionAndLevel<Configuration>(
this->GetConfiguration(), prefix, "VoxelSize", level, imageDimension);
voxelSizeStr =
formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "VoxelSize", level, imageDimension);
std::vector<unsigned int> imageDimensions;
for (int i = 0; i < modelsPathVec.size(); ++i)
imageDimensions.push_back(imageDimension);
voxelSizeVec = groupStrByDimensions(voxelSizeStr, imageDimensions);
}
else
{
voxelSizeStr =
formatParameterStringByDimensionAndLevel<Configuration>(this->GetConfiguration(), prefix, "VoxelSize", level);
voxelSizeStr = formatParameterStringByDimensionAndLevel(this->GetConfiguration(), prefix, "VoxelSize", level);
voxelSizeVec = groupStrByDimensions(voxelSizeStr, modelsDimensionVec);
}

Expand Down