Skip to content

Commit

Permalink
adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaeifar committed Dec 22, 2024
1 parent a6bfc78 commit 52dbe29
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 27 deletions.
29 changes: 16 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.24)
message(STATUS "CMake version: ${CMAKE_VERSION}")
include(CheckLanguage)

set(project "SpinWalk")
set(project_lib "SpinWalkLib")
project(${project})
set(project_exe "SpinWalk_exe")
set(project_lib "SpinWalk_lib")
project(${project_exe})

# Find HDF5
find_package(HDF5 REQUIRED COMPONENTS CXX)
Expand All @@ -16,7 +16,7 @@ find_package(TBB REQUIRED)
if(CMAKE_VERSION VERSION_GREATER "3.30")
cmake_policy(SET CMP0167 NEW)
endif()
find_package(Boost COMPONENTS log log_setup REQUIRED)
find_package(Boost REQUIRED COMPONENTS log log_setup)

check_language(CUDA)
# Check for CUDA-capable GPU
Expand All @@ -43,11 +43,11 @@ endif()

# Add Boost and CUDA include directories
include_directories(${Boost_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ./include ./src)
# Add subfolders
add_subdirectory(./src/dwi)
add_subdirectory(./src/phantom)
add_subdirectory(./src/config)
add_subdirectory(./src/sim)
# Add modules directory
add_subdirectory(src/dwi)
add_subdirectory(src/phantom)
add_subdirectory(src/config)
add_subdirectory(src/sim)

# Add the sources files
set(SOURCE_MODULES ${SUBCOMMAND_PHANTOM} ${SUBCOMMAND_SIM} ${SUBCOMMAND_DWI} ${SUBCOMMAND_CONFIG})
Expand All @@ -74,7 +74,7 @@ endif()

# Add the executable
add_library(${project_lib} ${SOURCE_MODULES})
add_executable(${project} ${SOURCE_MAIN})
add_executable(${project_exe} ${SOURCE_MAIN})

# Add the libraries
if(CMAKE_CUDA_COMPILER)
Expand All @@ -87,18 +87,21 @@ else()
message(STATUS "OpenMP not found, skipping.")
endif()
target_link_libraries(${project_lib} ${Boost_LIBRARIES} ${HDF5_CXX_LIBRARIES} TBB::tbb)
target_link_libraries(${project} PRIVATE ${project_lib})
target_link_libraries(${project_exe} PRIVATE ${project_lib})

# Set the output name
set_target_properties(${project} PROPERTIES OUTPUT_NAME "spinwalk")
set_target_properties(${project_exe} PROPERTIES OUTPUT_NAME "spinwalk")
set_target_properties(${project_lib} PROPERTIES OUTPUT_NAME "spinwalk")


# Install the executable
if (UNIX)
install(TARGETS ${project} DESTINATION bin)
install(TARGETS ${project_exe} DESTINATION bin)
install(TARGETS ${project_lib} DESTINATION lib)
endif()

# Add the tests directory
add_subdirectory(tests)

# cmake ..
# cmake --build . --config Release
3 changes: 2 additions & 1 deletion containers/Dockerfile_CPU
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ RUN rm -rf ./build && cmake -B ./build && cmake --build ./build --config Release
# Stage 2: Runtime
FROM ubuntu:24.04
USER root
RUN apt-get update && apt install -y libboost-log1.83.0 libtbb-dev libhdf5-103-1 --no-install-recommends && apt-get clean && apt-get -y autoremove && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt install -y libboost-log1.83.0 libtbb-dev libhdf5-103-1 libomp5 --no-install-recommends && apt-get clean && apt-get -y autoremove && rm -rf /var/lib/apt/lists/*
COPY --from=builder /opt/SpinWalk/build/spinwalk /usr/local/bin/
COPY --from=builder /opt/SpinWalk/build/tests/spinwalk_test /usr/local/bin/
LABEL org.opencontainers.image.authors="Ali Aghaeifar"

# docker run --rm -it -v /DATA2:/mnt/DATA2 spinwalk_cpu:ubuntu24.04 bash
1 change: 1 addition & 0 deletions containers/create_dockers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
pushd "$SCRIPT_DIR" > /dev/null

# docker system prune -a --volumes --force
docker system prune --force

CUDA_VERSIONS=("12.0.0" "12.2.0" "12.6.3")

Expand Down
2 changes: 1 addition & 1 deletion src/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define VERSION_MAJOR 1
#define VERSION_MINOR 18
#define VERSION_PATCH 1
#define VERSION_PATCH 2

// Helper macros to stringify values
#define STRINGIFY_HELPER(x) #x
Expand Down
6 changes: 3 additions & 3 deletions src/phantom/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace phantom {
if (args.cylinder){
std::cout << "Generating cylinder phantom..." << std::endl;
cylinder cyl(args.fov, args.resolution, args.dchi, args.oxy_level, args.radius, args.volume_fraction, args.orientation, args.seed, args.output);
status = status && cyl.run();
status = status && cyl.run(true);
}
if (args.sphere){
std::cout << "Generating sphere phantom..." << std::endl;
sphere sph(args.fov, args.resolution, args.dchi, args.oxy_level, args.radius, args.volume_fraction, args.seed,args.output);
status = status && sph.run();
status = status && sph.run(true);
}
std::cout << "Done." << std::endl;
return true;
return status;
}
}
2 changes: 1 addition & 1 deletion src/phantom/phantom_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class phantom_base
void set_space(float fov_um, size_t resolution);
void set_parameters(float dChi, float Y, float volume_fraction = 10.0);
void set_filename(std::string filename = "shape.h5");
virtual bool run(){return true;};
virtual bool run(bool write_to_disk) = 0;
virtual bool save() const;
virtual bool create_grid();
virtual bool generate_shapes() = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/phantom/phantom_cylinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ std::ostream& operator<<(std::ostream& os, const cylinder& obj)

// -------------------------------------------------------------------------- //

bool cylinder::run()
bool cylinder::run(bool write_to_disk)
{
BOOST_LOG_TRIVIAL(info) << *this << std::endl;
if(create_grid() == false)
Expand All @@ -294,8 +294,9 @@ bool cylinder::run()
return false;
if(generate_mask_fieldmap() == false)
return false;
if(save() == false)
return false;
if(write_to_disk)
if(save() == false)
return false;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/phantom/phantom_cylinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class cylinder : public phantom_base
cylinder(float fov_um, size_t resolution, float dChi, float Y, float radius_um = 50, float volume_fraction = 10., float orientation = -1.0f, int32_t seed=-1, std::string filename = "shape.h5");
~cylinder();

virtual bool run() override;
virtual bool run(bool write_to_disk) override;
virtual void set_cylinder_parameters(float radius_um = 50, float orientation = -1.0f);
virtual bool generate_shapes();
virtual bool generate_mask_fieldmap();
Expand Down
7 changes: 4 additions & 3 deletions src/phantom/phantom_sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ std::ostream& operator<<(std::ostream& os, const sphere& obj)

// -------------------------------------------------------------------------- //

bool sphere::run()
bool sphere::run(bool write_to_disk)
{
BOOST_LOG_TRIVIAL(info) << *this << std::endl;
if(create_grid() == false)
Expand All @@ -217,8 +217,9 @@ bool sphere::run()
return false;
if(generate_mask_fieldmap() == false)
return false;
if(save() == false)
return false;
if(write_to_disk)
if(save() == false)
return false;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/phantom/phantom_sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class sphere : public phantom_base
sphere(float fov_um, size_t resolution, float dChi, float Y, float radius_um = 50, float volume_fraction = 10.0, int32_t seed = -1, std::string filename = "shape.h5");
virtual ~sphere();

virtual bool run() override;
virtual bool run(bool write_to_disk) override;
virtual void set_sphere_parameters(float radius_um = 50);
virtual bool generate_shapes();
virtual bool generate_mask_fieldmap();
Expand Down
36 changes: 36 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.24)
include(CTest)

set(project_test "SpinWalk_test")
project(${project_test})

# Boost
if(CMAKE_VERSION VERSION_GREATER "3.30")
cmake_policy(SET CMP0167 NEW)
endif()
find_package(Boost REQUIRED COMPONENTS unit_test_framework log)

if (NOT Boost_UNIT_TEST_FRAMEWORK_FOUND)
message("Boost Unit Test Framework not found. Not compiling tests")
return()
endif ()

if (NOT Boost_USE_STATIC_LIBS)
add_definitions(-DBOOST_TEST_DYN_LINK)
endif()


# include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/include ${Boost_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Boost_INCLUDE_DIRS})

add_executable(${project_test} test_phantom.cpp test_config.cpp)
target_link_libraries(${project_test} ${Boost_LIBRARIES} SpinWalk_lib)
set_target_properties(${project_test} PROPERTIES OUTPUT_NAME "spinwalk_test")

add_test(NAME check COMMAND spinwalk_test)

if (UNIX)
install(TARGETS ${project_test} DESTINATION bin)
endif()

# spinwalk_test --log_level=test_suite --report_level=short
34 changes: 34 additions & 0 deletions tests/test_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#define BOOST_TEST_MODULE "SpinWlkTest"
#include <boost/test/unit_test.hpp>
#include <boost/log/core.hpp>
#include <filesystem>

#include "../src/config/config_generator.h"
#include "../include/ini.h"


BOOST_AUTO_TEST_CASE(config_creation) {
boost::log::core::get()->set_logging_enabled(false);

std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
std::filesystem::path temp_file = temp_dir / "gre.ini";
std::filesystem::path temp_file_parent = temp_dir / "default_config.ini";
std::vector<std::string> phantoms = {"phantom1.h5", "phantom2.h5"};
uint32_t timestep_us = 25;
uint32_t TE = 12345;

BOOST_REQUIRE(config::generate_gre(TE, timestep_us, phantoms, temp_file.string()));
BOOST_REQUIRE(std::filesystem::exists(temp_file));
BOOST_REQUIRE(std::filesystem::exists(temp_file_parent));

mINI::INIFile file(temp_file.string());
mINI::INIStructure ini;
BOOST_REQUIRE(file.read(ini));
BOOST_CHECK_EQUAL(std::stoi(ini["SCAN_PARAMETERS"]["TE[0]"]), TE);
BOOST_CHECK_EQUAL(std::stoi(ini["SCAN_PARAMETERS"]["TIME_STEP"]), timestep_us);

std::filesystem::remove(temp_file);
std::filesystem::remove(temp_file_parent);
BOOST_CHECK(!std::filesystem::exists(temp_file));
BOOST_CHECK(!std::filesystem::exists(temp_file_parent));
}
42 changes: 42 additions & 0 deletions tests/test_phantom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <boost/test/unit_test.hpp>
#include <boost/log/core.hpp>

#include "../src/phantom/phantom_sphere.h"
#include "../src/phantom/phantom_cylinder.h"


BOOST_AUTO_TEST_SUITE(test_phantom_creation)

BOOST_AUTO_TEST_CASE(cylinder_creation) {
boost::log::core::get()->set_logging_enabled(false);
float diff_margin = 2.f;

std::vector<float> volume_fraction = {5.0, 10.0};
for(const auto &vf : volume_fraction){
phantom::cylinder cyl(600, 300, 0.11e-6, -1, -20, vf, 0, 0, "");
BOOST_TEST(cyl.run(false));
BOOST_TEST(std::abs(cyl.get_actual_volume_fraction() - vf) < diff_margin);
}

std::vector<float> angles = {5.0, 10.0};
float vf = 10.0;
for(const auto &a : angles){
phantom::cylinder cyl(600, 300, 0.11e-6, -1, -20, vf, 0, a, "");
BOOST_TEST(cyl.run(false));
BOOST_TEST(std::abs(cyl.get_actual_volume_fraction() - vf) < diff_margin);
}
}

BOOST_AUTO_TEST_CASE(sphere_creation) {
boost::log::core::get()->set_logging_enabled(false);
std::vector<float> volume_fraction = {5.0, 10.0};
float diff_margin = 2.f;
for(const auto &vf : volume_fraction){
phantom::sphere sph(600, 300, 0.11e-6, -1, -20, vf, 0, "");
BOOST_CHECK(sph.run(false));
BOOST_CHECK(std::abs(sph.get_actual_volume_fraction() - vf) < diff_margin);
}
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 52dbe29

Please sign in to comment.