Skip to content

Commit

Permalink
Update functions for the new tatami library interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed May 21, 2024
1 parent 0add14d commit f25b91d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
12 changes: 5 additions & 7 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ include(FetchContent)
FetchContent_Declare(
knncolle
GIT_REPOSITORY https://github.com/LTLA/knncolle
GIT_TAG c5a1776ebf10641d9bf8715e89cb2d965b06e899
GIT_TAG master
)

FetchContent_MakeAvailable(knncolle)

FetchContent_Declare(
tatami
GIT_REPOSITORY https://github.com/LTLA/tatami
GIT_TAG 09770b07db2c71bde6a515b47748bcf5d3557a4c
GIT_TAG master
)

FetchContent_MakeAvailable(tatami)

FetchContent_Declare(
byteme
GIT_REPOSITORY https://github.com/LTLA/byteme
GIT_TAG a270a9cc0431d3f22e378becdd0ed1a32b7a4812
GIT_TAG master
)

FetchContent_MakeAvailable(knncolle)
FetchContent_MakeAvailable(tatami)
FetchContent_MakeAvailable(byteme)
4 changes: 2 additions & 2 deletions include/singlepp/IntegratedBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class IntegratedBuilder {

// 'in_use' is guaranteed to be sorted and unique, see its derivation in finish().
// This means we can directly use it for indexed extraction.
auto wrk = tatami::consecutive_extractor<false, false>(curmat, start, len, in_use);
auto wrk = tatami::consecutive_extractor<false>(curmat, false, start, len, in_use);
std::vector<double> buffer(wrk->index_length);

for (int c = start, end = start + len; c < end; ++c) {
Expand Down Expand Up @@ -484,7 +484,7 @@ class IntegratedBuilder {
RankedVector<double, int> tmp_ranked;
tmp_ranked.reserve(remapped_in_use.size());
std::vector<double> buffer(remapped_in_use.size());
auto wrk = tatami::consecutive_extractor<false, false>(curmat, start, len, remapped_in_use);
auto wrk = tatami::consecutive_extractor<false>(curmat, false, start, len, remapped_in_use);

for (size_t c = start, end = start + len; c < end; ++c) {
auto ptr = wrk->fetch(c, buffer.data());
Expand Down
2 changes: 1 addition & 1 deletion include/singlepp/IntegratedScorer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class IntegratedScorer {
tatami::parallelize([&](int, int start, int len) -> void {
// We perform an indexed extraction, so all subsequent indices
// will refer to indices into this subset (i.e., 'built.universe').
auto wrk = tatami::consecutive_extractor<false, false>(mat, start, len, built.universe);
auto wrk = tatami::consecutive_extractor<false>(mat, false, start, len, built.universe);
std::vector<double> buffer(wrk->index_length);

RankedVector<double, int> data_ranked, data_ranked2;
Expand Down
2 changes: 1 addition & 1 deletion include/singlepp/annotate_cells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline void annotate_cells_simple(
SubsetSorter subsorted(subcopy);

tatami::parallelize([&](int, int start, int length) -> void {
auto wrk = tatami::consecutive_extractor<false, false>(mat, start, length, subsorted.extraction_subset());
auto wrk = tatami::consecutive_extractor<false>(mat, false, start, length, subsorted.extraction_subset());
RankedVector<double, int> vec(num_subset);
std::vector<double> buffer(num_subset);

Expand Down
2 changes: 1 addition & 1 deletion include/singlepp/build_indices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ std::vector<Reference> build_indices(const tatami::Matrix<double, int>* ref, con
tatami::parallelize([&](int, int start, int len) -> void {
RankedVector<double, int> ranked(NR);
std::vector<double> buffer(ref->nrow());
auto wrk = tatami::consecutive_extractor<false, false>(ref, start, len, subsorter.extraction_subset());
auto wrk = tatami::consecutive_extractor<false>(ref, false, start, len, subsorter.extraction_subset());

for (int c = start, end = start + len; c < end; ++c) {
auto ptr = wrk->fetch(c, buffer.data());
Expand Down
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ add_executable(
src/ChooseClassicMarkers.cpp
)


FetchContent_Declare(
tatami_stats
GIT_REPOSITORY https://github.com/tatami-inc/tatami_stats
GIT_TAG master
)

FetchContent_MakeAvailable(tatami_stats)

target_link_libraries(
libtest
gtest_main
singlepp
tatami_stats
)

set(USE_OPENMP OFF CACHE BOOL "Compile with OpenMP support")
Expand Down
5 changes: 3 additions & 2 deletions tests/src/fine_tune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ TEST(FineTuneTest, Basic) {

// Check early exit conditions.
{
auto vec = refs->dense_column()->fetch(0); // doesn't really matter what we pick here.
auto ranked = fill_ranks(vec.size(), vec.data());
std::vector<double> buffer(refs->ncol());
auto vec = refs->dense_column()->fetch(0, buffer.data()); // doesn't really matter what we pick here.
auto ranked = fill_ranks(refs->ncol(), vec);

std::vector<double> scores { 0.2, 0.5, 0.1 };
auto output = ft.run(ranked, references, markers, scores, 0.8, 0.05);
Expand Down
14 changes: 10 additions & 4 deletions tests/src/naive_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ template<class RefMatrix>
double naive_score(const std::vector<double>& scaled_test, const std::vector<int>& in_label, const RefMatrix& refs, const std::vector<int>& subset, double quantile) {
std::vector<double> correlations;
auto wrk = refs->dense_column(subset);
std::vector<typename RefMatrix::element_type::value_type> buffer(refs->nrow());

for (auto l : in_label) {
auto col = wrk->fetch(l);
const auto scaled_ref = quick_scaled_ranks(col);
auto col = wrk->fetch(l, buffer.data());
tatami::copy_n(col, buffer.size(), buffer.data());
const auto scaled_ref = quick_scaled_ranks(buffer);
correlations.push_back(singlepp::distance_to_correlation(scaled_test.size(), scaled_test.data(), scaled_ref.data()));
}

return singlepp::correlations_to_scores(correlations, quantile);
}

Expand All @@ -33,10 +37,12 @@ auto naive_method(size_t nlabels, const Labels& labels, const RefMatrix& refs, c

auto by_labels = split_by_label(nlabels, labels);
auto wrk = mat->dense_column(subset);
std::vector<typename Matrix::element_type::value_type> buffer(mat->nrow());

for (size_t c = 0; c < mat->ncol(); ++c) {
auto col = wrk->fetch(c);
auto scaled = quick_scaled_ranks(col);
auto col = wrk->fetch(c, buffer.data());
tatami::copy_n(col, buffer.size(), buffer.data());
auto scaled = quick_scaled_ranks(buffer);

std::vector<std::pair<double, size_t> > my_scores;
for (size_t r = 0; r < nlabels; ++r) {
Expand Down
10 changes: 5 additions & 5 deletions tests/src/scaled_ranks.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>

#include "singlepp/scaled_ranks.hpp"
#include "tatami/tatami.hpp"
#include "tatami_stats/tatami_stats.hpp"
#include "singlepp/compute_scores.hpp"

#include <algorithm>
Expand Down Expand Up @@ -98,7 +98,7 @@ TEST(ScaledRanks, Basic) {
singlepp::scaled_ranks(ranks, out.data());

// Mean should be zero, variance should be... something.
auto stats = tatami::stats::variances::compute_direct(out.data(), out.size());
auto stats = tatami_stats::variances::direct(out.data(), out.size(), false);
EXPECT_TRUE(std::abs(stats.first) < 1e-8);
EXPECT_FLOAT_EQ(stats.second, expected_variance(stuff.size()));

Expand Down Expand Up @@ -141,7 +141,7 @@ TEST(ScaledRanks, Ties) {
singlepp::scaled_ranks(ranks, ref.data());

// Checking values aren't NA or infinite.
auto stats = tatami::stats::variances::compute_direct(ref.data(), ref.size());
auto stats = tatami_stats::variances::direct(ref.data(), ref.size(), false);
EXPECT_TRUE(std::abs(stats.first) < 1e-8);
EXPECT_FLOAT_EQ(stats.second, expected_variance(original_size));

Expand All @@ -154,7 +154,7 @@ TEST(ScaledRanks, Ties) {
EXPECT_EQ(tied[0], tied.back()); // same rank
EXPECT_NE(tied[0], ref[0]); // changes the ranks; note that this doesn't work if the first element is right in the middle.

auto stats2 = tatami::stats::variances::compute_direct(tied.data(), tied.size()); // these properties still hold.
auto stats2 = tatami_stats::variances::direct(tied.data(), tied.size(), false); // these properties still hold.
EXPECT_TRUE(std::abs(stats2.first) < 1e-8);
EXPECT_FLOAT_EQ(stats2.second, expected_variance(tied.size()));

Expand All @@ -167,7 +167,7 @@ TEST(ScaledRanks, Ties) {
std::vector<double> dupped(stuff.size());
singlepp::scaled_ranks(ranks, dupped.data());

auto stats3 = tatami::stats::variances::compute_direct(dupped.data(), dupped.size());
auto stats3 = tatami_stats::variances::direct(dupped.data(), dupped.size(), false);
EXPECT_TRUE(std::abs(stats3.first) < 1e-8);
EXPECT_FLOAT_EQ(stats3.second, expected_variance(original_size * 2));

Expand Down

0 comments on commit f25b91d

Please sign in to comment.