Skip to content

Commit

Permalink
ENH: Increase itk::STAPLEImageFilter coverage
Browse files Browse the repository at this point in the history
Increase `itk::STAPLEImageFilter` coverage:
- Make the test accept additional arguments to test all class instance
  variable Set/Get methods.
- Exercise basic object methods using the
  `ITK_EXERCISE_BASIC_OBJECT_METHODS` macro.
- Test the Set/Get methods using the `ITK_TEST_SET_GET_VALUE` macro.
- Test other getter methods by serializing the data to the standard
  output.
- Test exceptions using the `ITK_TRY_EXPECT_EXCEPTION` macro.

Take advantage of the commit to conform to the ITK SW Guide test
argument variable naming in the argument checking message.
  • Loading branch information
jhlegarreta authored and hjmjohnson committed Sep 12, 2022
1 parent e99228e commit aa76dfb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Modules/Filtering/ImageCompare/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ itk_add_test(NAME itkConstrainedValueDifferenceImageFilterTest
itk_add_test(NAME itkSimilarityIndexImageFilterTest
COMMAND ITKImageCompareTestDriver itkSimilarityIndexImageFilterTest)
itk_add_test(NAME itkSTAPLEImageFilterTest
COMMAND ITKImageCompareTestDriver
--compare DATA{${ITK_DATA_ROOT}/Baseline/Algorithms/STAPLEImageFilterTest.mha}
${ITK_TEST_OUTPUT_DIR}/STAPLEImageFilterTest.mha
itkSTAPLEImageFilterTest 2 ${ITK_TEST_OUTPUT_DIR}/STAPLEImageFilterTest.mha 255 0.5 DATA{${ITK_DATA_ROOT}/Input/STAPLE1.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE2.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE3.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE4.png})
COMMAND ITKImageCompareTestDriver
--compare DATA{${ITK_DATA_ROOT}/Baseline/Algorithms/STAPLEImageFilterTest.mha}
${ITK_TEST_OUTPUT_DIR}/STAPLEImageFilterTest.mha
itkSTAPLEImageFilterTest 2 ${ITK_TEST_OUTPUT_DIR}/STAPLEImageFilterTest.mha 255 0.5 100 DATA{${ITK_DATA_ROOT}/Input/STAPLE1.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE2.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE3.png} DATA{${ITK_DATA_ROOT}/Input/STAPLE4.png})
itk_add_test(NAME itkTestingComparisonImageFilterTest
COMMAND ITKImageCompareTestDriver
--compare DATA{Input/itkTestingComparisonImageFilterTest.png}
Expand Down
95 changes: 85 additions & 10 deletions Modules/Filtering/ImageCompare/test/itkSTAPLEImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class StaplerBase
return static_cast<unsigned int>(m_Files.size());
}

virtual const std::vector<double> &
GetSensitivity() = 0;
virtual const std::vector<double> &
GetSpecificity() = 0;
virtual double
GetSensitivity(unsigned int) = 0;
virtual double
Expand All @@ -73,6 +77,10 @@ class StaplerBase
GetForeground() const = 0;
virtual void
SetForeground(unsigned short) = 0;
virtual unsigned int
GetMaximumIterations() const = 0;
virtual void
SetMaximumIterations(unsigned int) = 0;
virtual void
SetConfidenceWeight(double) = 0;
virtual double
Expand Down Expand Up @@ -104,6 +112,16 @@ class Stapler : public StaplerBase
}
~Stapler() override = default;

unsigned int
GetMaximumIterations() const override
{
return m_Stapler->GetMaximumIterations();
}
void
SetMaximumIterations(unsigned int maximumIterations) override
{
m_Stapler->SetMaximumIterations(maximumIterations);
}
double
GetConfidenceWeight() const override
{
Expand All @@ -115,6 +133,17 @@ class Stapler : public StaplerBase
m_Stapler->SetConfidenceWeight(w);
}

const std::vector<double> &
GetSensitivity() override
{
return m_Stapler->GetSensitivity();
}
const std::vector<double> &
GetSpecificity() override
{
return m_Stapler->GetSpecificity();
}

double
GetSensitivity(unsigned int i) override
{
Expand Down Expand Up @@ -183,15 +212,27 @@ Stapler<VDimension>::Execute()
int
itkSTAPLEImageFilterTest(int argc, char * argv[])
{
if (argc < 5)
if (argc < 6)
{
std::cerr << "Use: " << itkNameOfTestExecutableMacro(argv)
<< " file_dimensionality output.mhd foreground_value confidence_weight "
"file1 file2 ... fileN"
<< std::endl;
std::cerr << "Missing parameters." << std::endl;
std::cerr
<< "Usage: " << itkNameOfTestExecutableMacro(argv)
<< " fileDimensionality outputFileName foregroundValue maximumIterations confidenceWeight file1 file2 ... fileN"
<< std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int Dimension = 2;

using InputImageType = itk::Image<unsigned short, Dimension>;
using OutputImageType = itk::Image<double, Dimension>;

using STAPLEImageFilterType = itk::STAPLEImageFilter<InputImageType, OutputImageType>;
auto stapleImageFilter = STAPLEImageFilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(stapleImageFilter, STAPLEImageFilter, ImageToImageFilter);


StaplerBase * stapler;

if (std::stoi(argv[1]) == 2)
Expand All @@ -208,14 +249,24 @@ itkSTAPLEImageFilterTest(int argc, char * argv[])
return EXIT_FAILURE;
}

for (int i = 0; i < argc - 5; ++i)
for (int i = 0; i < argc - 6; ++i)
{
stapler->AddFileName(argv[i + 5]);
stapler->AddFileName(argv[i + 6]);
}

stapler->SetConfidenceWeight(static_cast<double>(std::stod(argv[4])));
stapler->SetOutputFileName(argv[2]);
stapler->SetForeground(static_cast<unsigned short>(std::stoi(argv[3])));

auto foregroundValue = static_cast<unsigned short>(std::stoi(argv[3]));
stapler->SetForeground(foregroundValue);
ITK_TEST_SET_GET_VALUE(foregroundValue, stapler->GetForeground());

auto maximumIterations = static_cast<unsigned int>(std::stoi(argv[4]));
stapler->SetMaximumIterations(maximumIterations);
ITK_TEST_SET_GET_VALUE(maximumIterations, stapler->GetMaximumIterations());

auto confidenceWeight = static_cast<double>(std::stod(argv[5]));
stapler->SetConfidenceWeight(confidenceWeight);
ITK_TEST_SET_GET_VALUE(confidenceWeight, stapler->GetConfidenceWeight());

// Execute the stapler
int ret = stapler->Execute();
Expand Down Expand Up @@ -248,11 +299,35 @@ itkSTAPLEImageFilterTest(int argc, char * argv[])
<< stapler->GetSpecificity(i) << std::endl;
}

std::cout << "Mean:\t\t" << avgP << "\t\t" << avgQ << std::endl;
std::vector<double> specificity = stapler->GetSpecificity();
std::cout << "Specificity: ";
for (auto value : specificity)
{
std::cout << value << " ";
}
std::cout << std::endl;

std::vector<double> sensitivity = stapler->GetSensitivity();
std::cout << "Sensitivity: ";
for (auto value : specificity)
{
std::cout << value << " ";
}
std::cout << std::endl;

avgP /= static_cast<double>(stapler->GetNumberOfFiles());
avgQ /= static_cast<double>(stapler->GetNumberOfFiles());

std::cout << "Mean:\t\t" << avgP << "\t\t" << avgQ << std::endl;

// Test index exceptions
unsigned int i = stapler->GetNumberOfFiles() + 1;
ITK_TRY_EXPECT_EXCEPTION(stapler->GetSensitivity(i));
ITK_TRY_EXPECT_EXCEPTION(stapler->GetSpecificity(i));

delete stapler;


std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}

0 comments on commit aa76dfb

Please sign in to comment.