Skip to content

Commit

Permalink
Test for correct calculations when matrix type is not double/int. (#7)
Browse files Browse the repository at this point in the history
This requires some bugfixes to remove the default typing assumptions.
  • Loading branch information
LTLA authored Jun 28, 2024
1 parent fb5467e commit 726966a
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 12 deletions.
6 changes: 3 additions & 3 deletions include/tatami_stats/grouped_sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
runners.emplace_back(local_output.back().data(), sopt.skip_nan, start);
}

auto ext = tatami::consecutive_extractor<true>(p, !row, 0, otherdim, start, len, opt);
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, start, len, opt);
std::vector<Value_> xbuffer(len);
std::vector<Index_> ibuffer(len);

Expand Down Expand Up @@ -178,8 +178,8 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
runners.emplace_back(len, local_output.back().data(), sopt.skip_nan);
}

std::vector<double> xbuffer(len);
auto ext = tatami::consecutive_extractor<false>(p, !row, 0, otherdim, start, len);
std::vector<Value_> xbuffer(len);
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, start, len);

for (int i = 0; i < otherdim; ++i) {
auto ptr = ext->fetch(xbuffer.data());
Expand Down
2 changes: 1 addition & 1 deletion include/tatami_stats/grouped_variances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
runners.emplace_back(len, local_mean_output.back().data(), local_var_output.back().data(), sopt.skip_nan);
}

std::vector<double> xbuffer(len);
std::vector<Value_> xbuffer(len);
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, start, len);

for (Index_ i = 0; i < otherdim; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions include/tatami_stats/ranges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, 0, otherdim, s, l, opt);
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l, opt);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);

Expand Down Expand Up @@ -416,7 +416,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, 0, otherdim, s, l);
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

auto local_min = (store_min ? LocalOutputBuffer<Output_>(thread, s, l, min_out) : LocalOutputBuffer<Output_>());
Expand Down
4 changes: 2 additions & 2 deletions include/tatami_stats/sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
opt.sparse_ordered_index = false;

tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, 0, otherdim, s, l, opt);
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l, opt);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);

Expand Down Expand Up @@ -256,7 +256,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, 0, otherdim, s, l);
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

LocalOutputBuffer<Output_> local_output(thread, s, l, output);
Expand Down
9 changes: 5 additions & 4 deletions include/tatami_stats/variances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void add_welford_zeros(Output_& mean, Output_& sumsq, Index_ num_nonzero, Index_
template<typename Index_>
struct MockVector {
MockVector(size_t) {}
Index_ operator[](size_t) const { return 0; }
Index_& operator[](size_t) { return out; }
Index_ out = 0;
};

}
Expand Down Expand Up @@ -334,7 +335,7 @@ class RunningSparse {
for (Index_ i = 0; i < my_num; ++i) {
auto& curM = my_mean[i];
auto& curV = my_variance[i];
auto ct = my_count - my_nan[i];
Index_ ct = my_count - my_nan[i];

if (ct < 2) {
curV = std::numeric_limits<Output_>::quiet_NaN();
Expand Down Expand Up @@ -412,7 +413,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, 0, otherdim, s, l);
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);

Expand Down Expand Up @@ -443,7 +444,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, 0, otherdim, s, l);
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

std::vector<Output_> running_means(l);
Expand Down
36 changes: 36 additions & 0 deletions tests/src/grouped_medians.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,42 @@ TEST(GroupedMedians, EdgeCases) {
EXPECT_TRUE(tatami_stats::grouped_medians::by_column(&empty1, grouping.data()).empty());
}

TEST(GroupedMedians, NewType) {
size_t NR = 98, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));

std::vector<int> cgrouping;
for (size_t c = 0; c < NC; ++c) {
cgrouping.push_back(c % 5);
}
std::vector<int> rgrouping;
for (size_t r = 0; r < NR; ++r) {
rgrouping.push_back(r % 7);
}
auto rexpected = tatami_stats::grouped_medians::by_row(ref.get(), cgrouping.data());
auto cexpected = tatami_stats::grouped_medians::by_column(ref.get(), rgrouping.data());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::grouped_medians::by_row(dense_row.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_row(dense_column.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_row(sparse_row.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_row(sparse_column.get(), cgrouping.data()), rexpected);

EXPECT_EQ(tatami_stats::grouped_medians::by_column(dense_row.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_column(dense_column.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_column(sparse_row.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_medians::by_column(sparse_column.get(), rgrouping.data()), cexpected);
}

TEST(GroupedMedians, DirtyOutputs) {
size_t NR = 56, NC = 179;

Expand Down
36 changes: 36 additions & 0 deletions tests/src/grouped_sums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ TEST(GroupedSums, EdgeCases) {
EXPECT_TRUE(tatami_stats::grouped_sums::by_column(&empty1, grouping.data()).empty());
}

TEST(GroupedSums, NewType) {
size_t NR = 98, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));

std::vector<int> cgrouping;
for (size_t c = 0; c < NC; ++c) {
cgrouping.push_back(c % 5);
}
std::vector<int> rgrouping;
for (size_t r = 0; r < NR; ++r) {
rgrouping.push_back(r % 7);
}
auto rexpected = tatami_stats::grouped_sums::by_row(ref.get(), cgrouping.data());
auto cexpected = tatami_stats::grouped_sums::by_column(ref.get(), rgrouping.data());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::grouped_sums::by_row(dense_row.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_row(dense_column.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_row(sparse_row.get(), cgrouping.data()), rexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_row(sparse_column.get(), cgrouping.data()), rexpected);

EXPECT_EQ(tatami_stats::grouped_sums::by_column(dense_row.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_column(dense_column.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_column(sparse_row.get(), rgrouping.data()), cexpected);
EXPECT_EQ(tatami_stats::grouped_sums::by_column(sparse_column.get(), rgrouping.data()), cexpected);
}

TEST(GroupedSums, DirtyOutputs) {
size_t NR = 56, NC = 179;

Expand Down
36 changes: 36 additions & 0 deletions tests/src/grouped_variances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,42 @@ TEST(GroupedVariances, EdgeCases) {
}
}

TEST(GroupedVariances, NewType) {
size_t NR = 198, NC = 52;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));

std::vector<int> cgrouping;
for (size_t c = 0; c < NC; ++c) {
cgrouping.push_back(c % 5);
}
std::vector<int> rgrouping;
for (size_t r = 0; r < NR; ++r) {
rgrouping.push_back(r % 7);
}
auto rexpected = tatami_stats::grouped_variances::by_row(ref.get(), cgrouping.data());
auto cexpected = tatami_stats::grouped_variances::by_column(ref.get(), rgrouping.data());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_row(dense_row.get(), cgrouping.data()), rexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_row(dense_column.get(), cgrouping.data()), rexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_row(sparse_row.get(), cgrouping.data()), rexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_row(sparse_column.get(), cgrouping.data()), rexpected);

compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_column(dense_row.get(), rgrouping.data()), cexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_column(dense_column.get(), rgrouping.data()), cexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_column(sparse_row.get(), rgrouping.data()), cexpected);
compare_double_vectors_of_vectors(tatami_stats::grouped_variances::by_column(sparse_column.get(), rgrouping.data()), cexpected);
}

TEST(GroupedVariances, DirtyOutputs) {
int NR = 56, NC = 179;

Expand Down
27 changes: 27 additions & 0 deletions tests/src/medians.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,33 @@ TEST(ComputingDimMedians, RowMediansNaN) {
EXPECT_TRUE(std::isnan(rref.back()));
}

TEST(ComputingDimMedians, NewType) {
size_t NR = 198, NC = 52;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto rexpected = tatami_stats::medians::by_row(ref.get());
auto cexpected = tatami_stats::medians::by_column(ref.get());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::medians::by_row(dense_row.get()), rexpected);
EXPECT_EQ(tatami_stats::medians::by_row(dense_column.get()), rexpected);
EXPECT_EQ(tatami_stats::medians::by_row(sparse_row.get()), rexpected);
EXPECT_EQ(tatami_stats::medians::by_row(sparse_column.get()), rexpected);

EXPECT_EQ(tatami_stats::medians::by_column(dense_row.get()), cexpected);
EXPECT_EQ(tatami_stats::medians::by_column(dense_column.get()), cexpected);
EXPECT_EQ(tatami_stats::medians::by_column(sparse_row.get()), cexpected);
EXPECT_EQ(tatami_stats::medians::by_column(sparse_column.get()), cexpected);
}

TEST(ComputingDimMedians, DirtyOutput) {
size_t NR = 99, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.5, 1, 10); // see comments above about why we use 0.5.
Expand Down
27 changes: 27 additions & 0 deletions tests/src/ranges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ TEST(ComputingDimExtremes, NoZeros) {
EXPECT_EQ(rref, tatami_stats::ranges::by_row(sparse_column.get()));
}

TEST(ComputingDimExtremes, NewType) {
size_t NR = 198, NC = 52;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto rexpected = tatami_stats::ranges::by_row(ref.get());
auto cexpected = tatami_stats::ranges::by_column(ref.get());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::ranges::by_row(dense_row.get()), rexpected);
EXPECT_EQ(tatami_stats::ranges::by_row(dense_column.get()), rexpected);
EXPECT_EQ(tatami_stats::ranges::by_row(sparse_row.get()), rexpected);
EXPECT_EQ(tatami_stats::ranges::by_row(sparse_column.get()), rexpected);

EXPECT_EQ(tatami_stats::ranges::by_column(dense_row.get()), cexpected);
EXPECT_EQ(tatami_stats::ranges::by_column(dense_column.get()), cexpected);
EXPECT_EQ(tatami_stats::ranges::by_column(sparse_row.get()), cexpected);
EXPECT_EQ(tatami_stats::ranges::by_column(sparse_column.get()), cexpected);
}

TEST(ComputingDimExtremes, Empty) {
auto dense_row = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(10, 0, std::vector<double>()));
auto cres = tatami_stats::ranges::by_column(dense_row.get());
Expand Down
27 changes: 27 additions & 0 deletions tests/src/sums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ TEST(ComputingDimSums, ColumnSumsWithNan) {
EXPECT_TRUE(is_all_nan(tatami_stats::sums::by_column(sparse_column.get())));
}

TEST(ComputingDimSums, NewType) {
size_t NR = 198, NC = 52;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto rexpected = tatami_stats::sums::by_row(ref.get());
auto cexpected = tatami_stats::sums::by_column(ref.get());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::sums::by_row(dense_row.get()), rexpected);
EXPECT_EQ(tatami_stats::sums::by_row(dense_column.get()), rexpected);
EXPECT_EQ(tatami_stats::sums::by_row(sparse_row.get()), rexpected);
EXPECT_EQ(tatami_stats::sums::by_row(sparse_column.get()), rexpected);

EXPECT_EQ(tatami_stats::sums::by_column(dense_row.get()), cexpected);
EXPECT_EQ(tatami_stats::sums::by_column(dense_column.get()), cexpected);
EXPECT_EQ(tatami_stats::sums::by_column(sparse_row.get()), cexpected);
EXPECT_EQ(tatami_stats::sums::by_column(sparse_column.get()), cexpected);
}

TEST(ComputingDimSums, DirtyOutput) {
size_t NR = 99, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
Expand Down
27 changes: 27 additions & 0 deletions tests/src/variances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ TEST(ComputingDimVariances, ColumnVariancesWithNan) {
EXPECT_TRUE(is_all_nan(tatami_stats::variances::by_column(sparse_column.get())));
}

TEST(ComputingDimVariances, NewType) {
size_t NR = 198, NC = 52;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1, /* lower = */ 1, /* upper = */ 100);
for (auto& d : dump) {
d = std::round(d);
}
auto ref = std::unique_ptr<tatami::NumericMatrix>(new tatami::DenseRowMatrix<double, int>(NR, NC, dump));
auto rexpected = tatami_stats::variances::by_row(ref.get());
auto cexpected = tatami_stats::variances::by_column(ref.get());

std::vector<int8_t> ivec(dump.begin(), dump.end());
auto dense_row = std::make_shared<tatami::DenseRowMatrix<int8_t, uint8_t> >(NR, NC, std::move(ivec));
auto dense_column = tatami::convert_to_dense(dense_row.get(), false);
auto sparse_row = tatami::convert_to_compressed_sparse(dense_row.get(), true);
auto sparse_column = tatami::convert_to_compressed_sparse(dense_row.get(), false);

EXPECT_EQ(tatami_stats::variances::by_row(dense_row.get()), rexpected);
compare_double_vectors(tatami_stats::variances::by_row(dense_column.get()), rexpected);
compare_double_vectors(tatami_stats::variances::by_row(sparse_row.get()), rexpected);
compare_double_vectors(tatami_stats::variances::by_row(sparse_column.get()), rexpected);

EXPECT_EQ(tatami_stats::variances::by_column(dense_row.get()), cexpected);
compare_double_vectors(tatami_stats::variances::by_column(dense_column.get()), cexpected);
compare_double_vectors(tatami_stats::variances::by_column(sparse_row.get()), cexpected);
compare_double_vectors(tatami_stats::variances::by_column(sparse_column.get()), cexpected);
}

TEST(ComputingDimVariances, DirtyOutput) {
size_t NR = 99, NC = 152;
auto dump = tatami_test::simulate_sparse_vector<double>(NR * NC, 0.1);
Expand Down

0 comments on commit 726966a

Please sign in to comment.