Skip to content

Commit

Permalink
Use initial_value() to test that we don't assume zeroed buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Jan 11, 2025
1 parent ea882f3 commit 7774483
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 125 deletions.
14 changes: 13 additions & 1 deletion include/scran_markers/auc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ namespace internal {

template<typename Value_, typename Group_, typename Output_>
struct AucWorkspace {
AucWorkspace(size_t ngroups, Output_* buffer) : less_than(ngroups), equal(ngroups), outputs(ngroups) {
AucWorkspace(size_t ngroups, Output_* buffer) :
less_than(ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
),
equal(ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
),
outputs(ngroups)
{
output_start = buffer;
for (size_t i = 0; i < ngroups; ++i) {
outputs[i] = buffer;
Expand Down
12 changes: 10 additions & 2 deletions include/scran_markers/average_group_stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ void fill_average_results(
mean_ptrs.reserve(ngroups);
detected_ptrs.reserve(ngroups);
for (size_t g = 0; g < ngroups; ++g) {
mean_res.emplace_back(ngenes);
detected_res.emplace_back(ngenes);
mean_res.emplace_back(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
detected_res.emplace_back(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
mean_ptrs.emplace_back(mean_res.back().data());
detected_ptrs.emplace_back(detected_res.back().data());
}
Expand Down
18 changes: 15 additions & 3 deletions include/scran_markers/scan_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,28 @@ void initialize_auc_workspace(
const std::vector<Weight_>& combo_weight)
{
size_t ngroups2 = ngroups * ngroups;
work.common_buffer.resize(ngroups2);
work.common_buffer.resize(ngroups2
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);

work.block_workspaces.reserve(nblocks);
work.block_num_zeros.reserve(nblocks);
work.block_totals.reserve(nblocks);
for (size_t b = 0; b < nblocks; ++b) {
// All workspaces just re-use the same buffer for the AUCs, so make sure to run compute_pairwise_auc() for only one block at a time.
work.block_workspaces.emplace_back(ngroups, work.common_buffer.data());
work.block_num_zeros.emplace_back(ngroups);
work.block_totals.emplace_back(ngroups);
work.block_num_zeros.emplace_back(ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
work.block_totals.emplace_back(ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
}

auto lsIt = combo_size.begin();
Expand Down
24 changes: 20 additions & 4 deletions include/scran_markers/score_markers_pairwise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,35 @@ ScoreMarkersPairwiseBuffers<Stat_> fill_pairwise_results(size_t ngenes, size_t n
size_t num_effect_sizes = ngenes * ngroups * ngroups; // already size_t's, no need to cast.

if (opt.compute_cohens_d) {
store.cohens_d.resize(num_effect_sizes);
store.cohens_d.resize(num_effect_sizes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
output.cohens_d = store.cohens_d.data();
}
if (opt.compute_auc) {
store.auc.resize(num_effect_sizes);
store.auc.resize(num_effect_sizes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
output.auc = store.auc.data();
}
if (opt.compute_delta_mean) {
store.delta_mean.resize(num_effect_sizes);
store.delta_mean.resize(num_effect_sizes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
output.delta_mean = store.delta_mean.data();
}
if (opt.compute_delta_detected) {
store.delta_detected.resize(num_effect_sizes);
store.delta_detected.resize(num_effect_sizes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
output.delta_detected = store.delta_detected.data();
}

Expand Down
30 changes: 25 additions & 5 deletions include/scran_markers/summarize_comparisons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,43 @@ SummaryBuffers<Stat_, Rank_> fill_summary_results(
SummaryBuffers<Stat_, Rank_> ptr;

if (compute_min) {
out.min.resize(ngenes);
out.min.resize(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
ptr.min = out.min.data();
}
if (compute_mean) {
out.mean.resize(ngenes);
out.mean.resize(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
ptr.mean = out.mean.data();
}
if (compute_median) {
out.median.resize(ngenes);
out.median.resize(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
ptr.median = out.median.data();
}
if (compute_max) {
out.max.resize(ngenes);
out.max.resize(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
ptr.max = out.max.data();
}
if (compute_min_rank) {
out.min_rank.resize(ngenes);
out.min_rank.resize(ngenes
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
ptr.min_rank = out.min_rank.data();
}

Expand Down
48 changes: 31 additions & 17 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,32 @@ FetchContent_Declare(

FetchContent_MakeAvailable(scran_tests)

add_executable(
libtest
include(GoogleTest)

option(CODE_COVERAGE "Enable coverage testing" OFF)
set(DO_CODE_COVERAGE OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(DO_CODE_COVERAGE ON)
endif()

macro(decorate_test target)
target_link_libraries(
${target}
scran_markers
scran_tests
)

target_compile_options(${target} PRIVATE -Wall -Werror -Wpedantic -Wextra)

if(DO_CODE_COVERAGE)
target_compile_options(${target} PRIVATE -O0 -g --coverage)
target_link_options(${target} PRIVATE --coverage)
endif()

gtest_discover_tests(${target})
endmacro()

set(LIST_FILES
src/auc.cpp
src/cohens_d.cpp
src/simple_diff.cpp
Expand All @@ -21,19 +45,9 @@ add_executable(
src/average_group_stats.cpp
)

target_link_libraries(
libtest
scran_markers
scran_tests
)

target_compile_options(libtest PRIVATE -Wall -Werror -Wpedantic -Wextra)

option(CODE_COVERAGE "Enable coverage testing" OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(libtest PRIVATE -O0 -g --coverage)
target_link_options(libtest PRIVATE --coverage)
endif()
add_executable(libtest "${LIST_FILES}")
decorate_test(libtest)

include(GoogleTest)
gtest_discover_tests(libtest)
add_executable(dirtytest "${LIST_FILES}")
decorate_test(dirtytest)
target_compile_definitions(dirtytest PRIVATE "SCRAN_MARKERS_TEST_INIT=scran_tests::initial_value()")
38 changes: 31 additions & 7 deletions tests/src/auc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <gtest/gtest.h>
#include "scran_tests/scran_tests.hpp"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -57,7 +57,11 @@ TEST_F(AucTest, Self) {
std::vector<double> group1 { 0, -0.1, 1, 2.2, 3.5, 5 };

{
std::vector<double> output(4);
std::vector<double> output(4
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(2, output.data());

std::vector<int> num_zeros, totals;
Expand All @@ -73,7 +77,11 @@ TEST_F(AucTest, Self) {

// Trying again with 3 groups.
{
std::vector<double> output(9);
std::vector<double> output(9
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(3, output.data());

std::vector<int> num_zeros, totals;
Expand All @@ -97,7 +105,11 @@ TEST_F(AucTest, NoZeros) {
std::vector<double> group2 { 1, 5, 3.4, 5, -0.1, 5, -0.2, -5 };
std::vector<double> group3 { -0.12, 4, -0.1, 5, 2, -0.1, 3, 5, 6.2, 1.2, 1.11 };

std::vector<double> output(9);
std::vector<double> output(9
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(3, output.data());

std::vector<int> num_zeros, totals;
Expand All @@ -121,7 +133,11 @@ TEST_F(AucTest, Zeros) {
std::vector<double> group2 { 0, 1, 5, 0, 5, 0, 5, -0.2, -5 };
std::vector<double> group3 { -0.12, 4, 0, 5, 2, 0, 3, 5, 0, 1.2, 1.11 };

std::vector<double> output(9);
std::vector<double> output(9
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(3, output.data());

std::vector<int> num_zeros, totals;
Expand All @@ -139,7 +155,11 @@ TEST_F(AucTest, Zeros) {
TEST_F(AucTest, ThresholdSelf) {
std::vector<double> group { -1, 0, 1, 4, 3, 2, 5, 6, 7, 9 };

std::vector<double> output(4);
std::vector<double> output(4
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(2, output.data());

std::vector<int> num_zeros, totals;
Expand Down Expand Up @@ -192,7 +212,11 @@ TEST_F(AucTest, ThresholdZeros) {
std::vector<double> group2 { -0.5, 0, 1.5, 1.5, 0, 1.5, 2.5, 0, -0.5, -0.5 };
std::vector<double> group3 { -0.5, 6, 2, 0, -1.5, 0.5, 1.5, 1, 2, 0, 5 };

std::vector<double> output(9);
std::vector<double> output(9
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::AucWorkspace<double, int, double> input(3, output.data());

std::vector<int> num_zeros, totals;
Expand Down
2 changes: 0 additions & 2 deletions tests/src/average_group_stats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <gtest/gtest.h>

#include "scran_tests/scran_tests.hpp"
#include "scran_blocks/scran_blocks.hpp"

Expand Down
32 changes: 26 additions & 6 deletions tests/src/cohens_d.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <gtest/gtest.h>
#include "scran_tests/scran_tests.hpp"

#include "scran_markers/cohens_d.hpp"

Expand Down Expand Up @@ -27,7 +27,11 @@ TEST(CohensD, Unblocked) {
std::vector<int> group_sizes{ 10, 5, 12, 34 }; // don't really matter.

size_t ngroups = means.size();
std::vector<double> output(ngroups * ngroups);
std::vector<double> output(ngroups * ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::PrecomputedPairwiseWeights preweights(group_sizes.size(), 1, group_sizes.data());
scran_markers::internal::compute_pairwise_cohens_d(means.data(), variances.data(), ngroups, 1, preweights, 0.0, output.data());

Expand All @@ -44,7 +48,11 @@ TEST(CohensD, Thresholded) {
std::vector<int> group_sizes{ 10, 5, 12, 34 }; // don't really matter.

size_t ngroups = means.size();
std::vector<double> output(ngroups * ngroups);
std::vector<double> output(ngroups * ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::PrecomputedPairwiseWeights preweights(group_sizes.size(), 1, group_sizes.data());
scran_markers::internal::compute_pairwise_cohens_d(means.data(), variances.data(), ngroups, 1, preweights, 1.0, output.data());

Expand All @@ -64,7 +72,11 @@ TEST(CohensD, MissingValues) {
std::vector<int> group_sizes{ 0, 5, 1, 1, 5 };

size_t ngroups = means.size();
std::vector<double> output(means.size() * means.size());
std::vector<double> output(means.size() * means.size()
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::PrecomputedPairwiseWeights preweights(ngroups, 1, group_sizes.data());
scran_markers::internal::compute_pairwise_cohens_d(means.data(), variances.data(), ngroups, 1, preweights, 0.0, output.data());

Expand Down Expand Up @@ -97,7 +109,11 @@ TEST(CohensD, Blocked) {
std::vector<double> variances{1.5, 2.3, 0.5, 1.2, 0.1, 1.2, 0.4, 0.5 };
std::vector<int> combo_sizes{ 10, 5, 12, 34, 15, 2, 3, 6 };

std::vector<double> output(ngroups * ngroups);
std::vector<double> output(ngroups * ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::PrecomputedPairwiseWeights preweights(ngroups, nblocks, combo_sizes.data());
scran_markers::internal::compute_pairwise_cohens_d(means.data(), variances.data(), ngroups, nblocks, preweights, 0.0, output.data());

Expand Down Expand Up @@ -126,7 +142,11 @@ TEST(CohensD, BlockedMissing) {
std::vector<double> variances{nan, nan, nan, nan, 0.1, 1.2, 0.4, 0.5 };
std::vector<int> combo_sizes{ 0, 1, 0, 0, 15, 2, 3, 6 };

std::vector<double> output(ngroups * ngroups);
std::vector<double> output(ngroups * ngroups
#ifdef SCRAN_MARKERS_TEST_INIT
, SCRAN_MARKERS_TEST_INIT
#endif
);
scran_markers::internal::PrecomputedPairwiseWeights preweights(ngroups, nblocks, combo_sizes.data());
scran_markers::internal::compute_pairwise_cohens_d(means.data(), variances.data(), ngroups, nblocks, preweights, 0.0, output.data());

Expand Down
2 changes: 1 addition & 1 deletion tests/src/create_combinations.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <gtest/gtest.h>
#include "scran_tests/scran_tests.hpp"

#include "scran_markers/create_combinations.hpp"

Expand Down
Loading

0 comments on commit 7774483

Please sign in to comment.