Debian Codegraph @

tests/src/algorithm/test_LSHIndex.cpp

raw
1 #include <pbcopper/algorithm/KMerIndex.h>
2 #include <pbcopper/algorithm/LSHIndex.h>
3 #include <pbcopper/utility/Random.h>
4 #include <pbcopper/utility/Ssize.h>
5
6 #include <gtest/gtest.h>
7
8 #include <algorithm>
9 #include <random>
10
11 using namespace PacBio;
12
13 // clang-format off
14
15 TEST(Algorithm_lsh_index, lsh_index_test_accuracy) {
16 static constexpr int NF = 100;
17 static constexpr int NL = 200;
18 std::vector<uint64_t> kmerFounders(NF);
19 uint64_t seed = 13;
20 auto sample = [&]() {return Utility::WyHash64Step(seed);};
21 for(auto& x: kmerFounders) {
22 x = sample() >> 4;
23 }
24 const int bk = 16;
25 std::vector<std::vector<uint64_t>> kmerSets(NL, kmerFounders);
26 for(int i = 0; i < NL; ++i) {
27 auto& ks =kmerSets[i];
28 for(int j = 0; j < i + 5; ++j) {
29 ks[sample() % ks.size()] = sample();
30 }
31 std::transform(ks.begin(), ks.end(), ks.begin(), Algorithm::SubMerSelection::WangHash);
32 std::sort(ks.begin(), ks.end());
33 ks.resize(bk);
34 }
35 // Test that building the index twice yields the same index
36 Algorithm::LSHIndex<uint64_t, uint32_t> bkidx = Algorithm::LSHIndex<uint64_t, uint32_t>::CreateBottomK(bk);
37 bkidx.Insert(kmerSets.begin(), kmerSets.end());
38 {
39 Algorithm::LSHIndex<uint64_t, uint32_t> bkidx2;
40 bkidx2.Insert(kmerSets.begin(), kmerSets.end());
41 EXPECT_EQ(bkidx, bkidx2);
42 }
43 // Test that each item is its own best hit.
44 for(int i = 0; i < NL; ++i) {
45 const auto [ids, counts, numberNonZero] = bkidx.Query(kmerSets[i]);
46 const auto findMyself = std::find_if(ids.begin(), ids.end(), [i](int x) {return x == i;});
47 EXPECT_NE(findMyself, ids.end());
48 const auto countit = counts.begin() + (findMyself - ids.begin());
49 EXPECT_EQ(*countit, *std::max_element(counts.begin(), counts.end()));
50 }
51 }
52
53 // clang-format on