Skip to content

Commit

Permalink
update predictor, include tests for predictor
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia committed Oct 11, 2022
1 parent f4e91c5 commit b9b8902
Show file tree
Hide file tree
Showing 32 changed files with 9,885 additions and 56,170 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ add_library(

target_include_directories(
coraxlib_difficulty_prediction_lib PRIVATE ${CORAX_INCLUDE_DIRS}
)
)
8 changes: 5 additions & 3 deletions src/difficulty.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "difficulty.h"

#include "corax/util/compress.h"
#include "corax/util/msa.h"

CORAX_EXPORT corax_msa_features *corax_msa_compute_features(
corax_msa_t *msa, unsigned int states, const corax_state_t *tipmap)
{
Expand Down Expand Up @@ -94,5 +91,10 @@ corax_msa_predict_difficulty(const corax_msa_features *msa_features,
double prediction = predict(feat, prediction_margin);
free(feat);

// LightGBM may produce results lower than 0.0 or higher than 1.0
// -> clip the difficulty to [0.0, 1.0]
if (prediction < 0.0) prediction = 0.0;
if (prediction > 1.0) prediction = 1.0;

return prediction;
}
1 change: 1 addition & 0 deletions src/difficulty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C"
#endif

#include "corax/core/partition.h"
#include "corax/util/compress.h"
#include "corax/util/msa.h"
#include "difficulty/prediction.h"

Expand Down
4 changes: 2 additions & 2 deletions src/difficulty/prediction.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const char* get_leaf_output_type(void) {
return "float64";
}


static inline double pred_transform(double margin) {
return margin;
}

double predict(union Entry* data, int pred_margin) {
double sum = (double)0;
unsigned int tmp;
Expand All @@ -60,7 +60,7 @@ double predict(union Entry* data, int pred_margin) {
sum += predict_margin_unit13(data);
sum += predict_margin_unit14(data);

sum = sum / 100 + (double)(0);
sum = sum + (double)(0);
if (!pred_margin) {
return pred_transform(sum);
} else {
Expand Down
2,924 changes: 456 additions & 2,468 deletions src/difficulty/tu0.c

Large diffs are not rendered by default.

3,092 changes: 506 additions & 2,586 deletions src/difficulty/tu1.c

Large diffs are not rendered by default.

2,972 changes: 436 additions & 2,536 deletions src/difficulty/tu10.c

Large diffs are not rendered by default.

2,948 changes: 478 additions & 2,470 deletions src/difficulty/tu11.c

Large diffs are not rendered by default.

3,044 changes: 494 additions & 2,550 deletions src/difficulty/tu12.c

Large diffs are not rendered by default.

2,980 changes: 458 additions & 2,522 deletions src/difficulty/tu13.c

Large diffs are not rendered by default.

804 changes: 110 additions & 694 deletions src/difficulty/tu14.c

Large diffs are not rendered by default.

20,174 changes: 0 additions & 20,174 deletions src/difficulty/tu15.c

This file was deleted.

2,868 changes: 452 additions & 2,416 deletions src/difficulty/tu2.c

Large diffs are not rendered by default.

3,026 changes: 475 additions & 2,551 deletions src/difficulty/tu3.c

Large diffs are not rendered by default.

2,958 changes: 443 additions & 2,515 deletions src/difficulty/tu4.c

Large diffs are not rendered by default.

2,960 changes: 450 additions & 2,510 deletions src/difficulty/tu5.c

Large diffs are not rendered by default.

3,090 changes: 485 additions & 2,605 deletions src/difficulty/tu6.c

Large diffs are not rendered by default.

2,948 changes: 444 additions & 2,504 deletions src/difficulty/tu7.c

Large diffs are not rendered by default.

2,916 changes: 456 additions & 2,460 deletions src/difficulty/tu8.c

Large diffs are not rendered by default.

3,086 changes: 483 additions & 2,603 deletions src/difficulty/tu9.c

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Set the warining flags for the tests.
set(
CORAX_TEST_WARNING_FLAGS
"-Wall"
"-Wextra"
"-Wconversion"
"-Wnon-virtual-dtor"
"-Woverloaded-virtual"
"-Wshadow"
"-Wsign-conversion"
"-Wundef"
"-Wunreachable-code"
"-Wunused"
)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
list(
APPEND CORAX_TEST_WARNING_FLAGS
"-Wcast-align"
"-Wnull-dereference"
"-Wpedantic"
"-Wextra-semi"
"-Wno-gnu-zero-variadic-macro-arguments"
)
endif()

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
list(
APPEND CORAX_TEST_WARNING_FLAGS
"-Wcast-align"
"-Wnull-dereference"
"-Wpedantic"
"-Wnoexcept"
"-Wsuggest-attribute=const"
"-Wsuggest-attribute=noreturn"
"-Wsuggest-override"
)
endif()

# Intel supports the fewest warning flags.

# Make the cmake helpers findable.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

# Add the unit tests.
add_subdirectory(unit)
136 changes: 136 additions & 0 deletions test/cmake/DifficultyTestHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Adapted from https://github.com/kamping-site/kamping/blob/main/tests/cmake/KampingTestHelper.cmake
# and https://github.com/kamping-site/kamping/blob/main/tests/cmake/KaTestrophe.cmake
#
# I removed the MPI parts because coraxlib doesn't directly use MPI yet; parallelization managed by the caller.

include(GoogleTest)

# We're relying on GoogleTest already being installed on this system.
# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")

# keep the cache clean
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)


function(target_add_address_sanitizer TARGET)
if(${CMAKE_VERSION} VERSION_LESS "3.13")
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS -fsanitize=address)
set_target_properties(${TARGET} PROPERTIES LINK_FLAGS -fsanitize=address)
else()
target_compile_options(${TARGET} PRIVATE -fsanitize=address)
target_link_options(${TARGET} PRIVATE -fsanitize=address)
endif()
endfunction()

# Convenience wrapper for adding tests for Coraxlib
# This creates the target, links googletest and coraxlib, enables warnings, and registers the test.
#
# TARGET_NAME the target name
# FILES the files of the target
#
# example: corax_register_test(mytarget FILES mytarget.cpp)
function(corax_register_test CORAX_TARGET_NAME)
cmake_parse_arguments(
"CORAX" # prefix
"NO_EXCEPTION_MODE" # flags
"" # one-valued arguments
"FILES;DEFINES" # multi-valued arguments
${ARGN}
)

# Add our main.cpp to the list of source files and register the test target.
list(APPEND CORAX_FILES src/main.cpp)
add_executable(${CORAX_TARGET_NAME} ${CORAX_FILES})

# Link gtest, gmock, coraxlib and pthread to the test target; also add corax lib include directories.
target_include_directories(${CORAX_TARGET_NAME} PRIVATE ${CORAX_INCLUDE_DIRS})
target_link_libraries(${CORAX_TARGET_NAME} PRIVATE coraxlib_difficulty_prediction_lib gtest gmock ${CORAX_LIBRARIES} pthread)

# Enable lots of warnings, enable the adress sanitizer and pass on defines (e.g. HAVE_AVX=1 for
# some tests).
target_compile_options(${CORAX_TARGET_NAME} PRIVATE ${CORAX_TEST_WARNING_FLAGS})
target_compile_definitions(${CORAX_TARGET_NAME} PRIVATE ${CORAX_DEFINES})
target_add_address_sanitizer(${CORAX_TARGET_NAME})

# Let ctest discover all tests in the test target (-> pretty list when calling ctest).
gtest_discover_tests(${CORAX_TARGET_NAME} WORKING_DIRECTORY ${PROJECT_DIR})

endfunction()

# Registers a set of tests which should fail to compile.
#
# TARGET prefix for the targets to be built
# FILES the list of files to include in the target
# SECTIONS sections of the compilation test to build
# LIBRARIES libraries to link via target_link_libraries(...)
#
# Loosely based on: https://stackoverflow.com/questions/30155619/expected-build-failure-tests-in-cmake
function(katestrophe_add_compilation_failure_test)
cmake_parse_arguments(
"KATESTROPHE" # prefix
"" # options
"TARGET" # one value arguments
"FILES;SECTIONS;LIBRARIES" # multiple value arguments
${ARGN}
)

# the file should compile without any section enabled
add_executable(${KATESTROPHE_TARGET} ${KATESTROPHE_FILES})
target_link_libraries(${KATESTROPHE_TARGET} PUBLIC gtest ${KATESTROPHE_LIBRARIES})

# For each given section, add a target.
foreach(SECTION ${KATESTROPHE_SECTIONS})
string(TOLOWER ${SECTION} SECTION_LOWERCASE)
set(THIS_TARGETS_NAME "${KATESTROPHE_TARGET}.${SECTION_LOWERCASE}")

# Add the executable and link the libraries.
add_executable(${THIS_TARGETS_NAME} ${KATESTROPHE_FILES})
target_link_libraries(${THIS_TARGETS_NAME} PUBLIC gtest ${KATESTROPHE_LIBRARIES})

# Select the correct section of the target by setting the appropriate preprocessor define.
target_compile_definitions(${THIS_TARGETS_NAME} PRIVATE ${SECTION})

# Exclude the target fromn the "all" target.
set_target_properties(
${THIS_TARGETS_NAME} PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE
)

# Add a test invoking "cmake --build" to test if the target compiles.
add_test(
NAME "${THIS_TARGETS_NAME}"
COMMAND cmake --build . --target ${THIS_TARGETS_NAME} --config $<CONFIGURATION>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Specify, that the target should not compile.
set_tests_properties("${THIS_TARGETS_NAME}" PROPERTIES WILL_FAIL TRUE)
endforeach()
endfunction()

# Convenience wrapper for registering a set of tests that should fail to compile and require coraxlib to be linked.
#
# TARGET prefix for the targets to be built
# FILES the list of files to include in the target
# SECTIONS sections of the compilation test to build
#
function(corax_register_compilation_failure_test CORAX_TARGET_NAME)
cmake_parse_arguments(
"CORAX"
"NO_EXCEPTION_MODE"
""
"FILES;SECTIONS"
${ARGN}
)
katestrophe_add_compilation_failure_test(
TARGET ${CORAX_TARGET_NAME}
FILES ${CORAX_FILES}
SECTIONS ${CORAX_SECTIONS}
LIBRARIES ${CORAX_LIBRARIES} pthread
)
endfunction()
8 changes: 8 additions & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include(DifficultyTestHelper)
include(GoogleTest)

set(DIFFICULTY_TEST_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)

corax_register_test(test_difficulty FILES src/difficulty.cpp DEFINES DATAPATH=${DIFFICULTY_TEST_DATA_DIR})

enable_testing()
Loading

0 comments on commit b9b8902

Please sign in to comment.