Skip to content

Commit

Permalink
ENH: First simple test for iteration reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
acoussat authored and Simon Rit committed Feb 13, 2020
1 parent 0965dfe commit b441fc7
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 70 deletions.
23 changes: 17 additions & 6 deletions applications/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ add_subdirectory(rtkspectraldenoiseprojections)
add_subdirectory(rtkprojectionmatrix)
add_subdirectory(rtkvectorconjugategradient)

add_subdirectory(rtkcheckimagequality)

#All the executables below are meant to create RTK ThreeDCircularProjectionGeometry files
add_subdirectory(rtkvarianobigeometry)
add_subdirectory(rtkvarianprobeamgeometry)
Expand All @@ -108,24 +110,33 @@ if(BUILD_TESTING)

add_test(rtkappsimulatedgeometrytest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtksimulatedgeometry -n 180 --sid 1000 --sdd 1500 -o geo)

add_test(rtkdrawshepploganphantomtest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkdrawshepploganphantom -o reference.mha --dimension 84 --phantomscale 40)

add_test(rtkappprojectshepploganphantomtest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkprojectshepploganphantom -o sheppy.mha -g geo --phantomscale 40 --dimension 128)
set_tests_properties(rtkappprojectshepploganphantomtest PROPERTIES DEPENDS rtkappsimulatedgeometrytest)

if(RTK_USE_CUDA AND CUDA_HAVE_GPU)
add_test(rtkappfdkcudatest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk_gpu.mha --hardware cuda)
add_test(rtkappfdkcudatest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk_gpu.mha --hardware cuda --dimension 84)
set_tests_properties(rtkappfdkcudatest PROPERTIES DEPENDS rtkappprojectshepploganphantomtest)

add_test(rtkappfdkchecktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkcheckimagequality fdk_gpu.mha)
set_tests_properties(rtkappfdkchecktest PROPERTIES DEPENDS rtkappfdkcudatest)
add_test(rtkappfdkchecktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkcheckimagequality -i reference.mha -j fdk_gpu.mha -t 7000)
set_tests_properties(rtkappfdkchecktest PROPERTIES DEPENDS "rtkdrawshepploganphantomtest,rtkappfdkcudatest")
else()
# Only run the software version if
if(NOT FAST_TESTS_NO_CHECKS)
add_test(rtkappfdktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk_gpu.mha)
add_test(rtkappfdktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk_gpu.mha --dimension 84)
set_tests_properties(rtkappfdktest PROPERTIES DEPENDS rtkappprojectshepploganphantomtest)

add_test(rtkappfdkchecktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkcheckimagequality fdk_gpu.mha)
set_tests_properties(rtkappfdkchecktest PROPERTIES DEPENDS rtkappfdktest)
add_test(rtkappfdkchecktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkcheckimagequality -i reference.mha -j fdk_gpu.mha -t 7000)
set_tests_properties(rtkappfdkchecktest PROPERTIES DEPENDS "rtkdrawshepploganphantomtest,rtkappfdktest")
endif()
endif()

# Iteration reporting testing
add_test(rtkapposemtest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkosem -g geo -p . -r sheppy.mha -o osem.mha -n 3 --dimension 84 --output-every 1 --nprojpersubset 45 --iteration-file-name osem%d.mha)
set_tests_properties(rtkapposemtest PROPERTIES DEPENDS rtkappprojectshepploganphantomtest)

add_test(rtkapposemchecktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkcheckimagequality -i reference.mha -j osem1.mha,osem2.mha,osem3.mha -t 35000,16000,10000)
set_tests_properties(rtkapposemchecktest PROPERTIES DEPENDS "rtkapposemtest,rtkdrawshepploganphantomtest")

endif()
14 changes: 14 additions & 0 deletions applications/rtkcheckimagequality/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
WRAP_GGO(rtkcheckimagequality_GGO_C rtkcheckimagequality.ggo ${RTK_BINARY_DIR}/rtkVersion.ggo)
add_executable(rtkcheckimagequality rtkcheckimagequality.cxx ${rtkcheckimagequality_GGO_C})
target_link_libraries(rtkcheckimagequality RTK)

# Installation code
if(NOT RTK_INSTALL_NO_EXECUTABLES)
foreach(EXE_NAME rtkcheckimagequality)
install(TARGETS ${EXE_NAME}
RUNTIME DESTINATION ${RTK_INSTALL_RUNTIME_DIR} COMPONENT Runtime
LIBRARY DESTINATION ${RTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
ARCHIVE DESTINATION ${RTK_INSTALL_ARCHIVE_DIR} COMPONENT Development)
endforeach()
endif()

117 changes: 117 additions & 0 deletions applications/rtkcheckimagequality/rtkcheckimagequality.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "rtkcheckimagequality_ggo.h"
#include "rtkConfiguration.h"
#include "rtkMacro.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRegionConstIterator.h"

/**
* \file rtkcheckimagequality.cxx
*
* \brief Checks that an image has a satisfactory MSE against a reference.
*
* \author Aurélien Coussat
*/

template <class TImage>
double
MSE(typename TImage::Pointer reference, typename TImage::Pointer reconstruction)
{
using ImageIteratorType = itk::ImageRegionConstIterator<TImage>;
ImageIteratorType itTest(reconstruction, reconstruction->GetBufferedRegion());
ImageIteratorType itRef(reference, reference->GetBufferedRegion());

using ErrorType = double;
ErrorType EnerError = 0.;

itTest.GoToBegin();
itRef.GoToBegin();

while (!itRef.IsAtEnd())
{
typename TImage::PixelType TestVal = itTest.Get();
typename TImage::PixelType RefVal = itRef.Get();
EnerError += std::pow(ErrorType(RefVal - TestVal), 2.);
++itTest;
++itRef;
}

return EnerError;
}

int
main(int argc, char ** argv)
{

args_info_rtkcheckimagequality args_info;
cmdline_parser_rtkcheckimagequality_params args_params;
cmdline_parser_rtkcheckimagequality_params_init(&args_params);
args_params.print_errors = 1;
args_params.check_required = 1;
args_params.override = 1;
args_params.initialize = 1;
if (0 != cmdline_parser_rtkcheckimagequality_ext(argc, argv, &args_info, &args_params))
{
std::cerr << "Error in cmdline_parser_rtkcheckimagequality_ext" << std::endl;
exit(1);
}

constexpr unsigned int Dimension = 3;
using PixelType = float;
using ImageType = itk::Image<PixelType, Dimension>;

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader;

// Maximum number of comparisons to perform (depends on the number of inputs)
unsigned int n_max =
std::max({ args_info.reference_given, args_info.reconstruction_given, args_info.threshold_given });

for (unsigned int i = 0; i < n_max; i++)
{
unsigned int reference_index = std::min(args_info.reference_given - 1, i);
unsigned int reconstruction_index = std::min(args_info.reconstruction_given - 1, i);
unsigned int threshold_index = std::min(args_info.threshold_given - 1, i);

reader = ReaderType::New();
reader->SetFileName(args_info.reference_arg[reference_index]);

try
{
reader->Update();
}
catch (::itk::ExceptionObject & e)
{
std::cerr << e.GetDescription();
return EXIT_FAILURE;
}

ImageType::Pointer reference = reader->GetOutput();

reader = ReaderType::New();
reader->SetFileName(args_info.reconstruction_arg[reconstruction_index]);

try
{
reader->Update();
}
catch (::itk::ExceptionObject & e)
{
std::cerr << e.GetDescription();
return EXIT_FAILURE;
}

ImageType::Pointer reconstruction = reader->GetOutput();

double mse = MSE<ImageType>(reference, reconstruction);

if (mse > args_info.threshold_arg[threshold_index])
{
std::cerr << "Error comparing " << args_info.reference_arg[reference_index] << " and "
<< args_info.reconstruction_arg[reconstruction_index] << ":" << std::endl
<< "MSE " << mse << " above given threshold " << args_info.threshold_arg[threshold_index] << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions applications/rtkcheckimagequality/rtkcheckimagequality.ggo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package "rtkcheckimagequality"
purpose "Checks the MSE of a reconstructed image against a reference."

option "reference" i "Reference volume" string multiple yes
option "reconstruction" j "Reconstructed volume" string multiple yes
option "threshold" t "MSE threshold" int multiple yes
5 changes: 0 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ if(NOT ITK_SOURCE_DIR)
set(ExternalData_LINK_CONTENT SHA512)
endif()

#-----------------------------------------------------------------------------
# Executable to check the image quality
add_executable(rtkcheckimagequality rtkcheckimagequality.cxx)
target_link_libraries(rtkcheckimagequality ${ITK_LIBRARIES} ${RTK_LIBRARIES} ${RTK-Test_LIBRARIES})

rtk_add_test(rtkFDKTest rtkfdktest.cxx)
rtk_add_cuda_test(rtkFDKCudaTest rtkfdktest.cxx)

Expand Down
59 changes: 0 additions & 59 deletions test/rtkcheckimagequality.cxx

This file was deleted.

0 comments on commit b441fc7

Please sign in to comment.