Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEST] Add compute_ibf_size test. #224

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/util/display_layout/compute_ibf_size.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ---------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/chopper/blob/main/LICENSE.md
// ---------------------------------------------------------------------------------------------------

#pragma once

#include <cstdint>

#include <hibf/build/bin_size_in_bits.hpp>
#include <hibf/build/build_data.hpp>
#include <hibf/contrib/robin_hood.hpp>
#include <hibf/layout/graph.hpp>

void update_parent_kmers(robin_hood::unordered_flat_set<uint64_t> & parent_kmers,
robin_hood::unordered_flat_set<uint64_t> const & kmers)
{
parent_kmers.insert(kmers.begin(), kmers.end());
}

size_t compute_ibf_size(robin_hood::unordered_flat_set<uint64_t> & parent_kmers,
robin_hood::unordered_flat_set<uint64_t> & kmers,
size_t const number_of_bins,
seqan::hibf::layout::graph::node const & ibf_node,
seqan::hibf::build::build_data & data,
size_t const current_hibf_level)
{
size_t const kmers_per_bin = std::ceil(static_cast<double>(kmers.size()) / number_of_bins);
size_t const bin_size =
std::ceil(seqan::hibf::build::bin_size_in_bits({.fpr = data.config.maximum_false_positive_rate,
.hash_count = data.config.number_of_hash_functions,
.elements = kmers_per_bin})
* data.fpr_correction[number_of_bins]);

size_t const ibf_size = ibf_node.number_of_technical_bins * bin_size;

if (current_hibf_level > 0 /* not top level */)
update_parent_kmers(parent_kmers, kmers);

return ibf_size;
}
30 changes: 1 addition & 29 deletions src/util/display_layout/sizes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@

#include <chopper/layout/input.hpp>

#include <hibf/build/bin_size_in_bits.hpp>
#include <hibf/build/build_data.hpp>
#include <hibf/config.hpp>
#include <hibf/contrib/robin_hood.hpp>
#include <hibf/layout/compute_fpr_correction.hpp>
#include <hibf/layout/graph.hpp>
#include <hibf/sketch/hyperloglog.hpp>

#include "compute_ibf_size.hpp"
#include "shared.hpp"

struct ibf_stats
Expand Down Expand Up @@ -126,34 +126,6 @@ void compute_kmers(robin_hood::unordered_flat_set<uint64_t> & kmers,
data.config.input_fn(record.idx, seqan::hibf::insert_iterator{kmers});
}

void update_parent_kmers(robin_hood::unordered_flat_set<uint64_t> & parent_kmers,
robin_hood::unordered_flat_set<uint64_t> const & kmers)
{
parent_kmers.insert(kmers.begin(), kmers.end());
}

size_t compute_ibf_size(robin_hood::unordered_flat_set<uint64_t> & parent_kmers,
robin_hood::unordered_flat_set<uint64_t> & kmers,
size_t const number_of_bins,
seqan::hibf::layout::graph::node const & ibf_node,
seqan::hibf::build::build_data & data,
size_t const current_hibf_level)
{
size_t const kmers_per_bin = std::ceil(static_cast<double>(kmers.size()) / number_of_bins);
size_t const bin_size =
std::ceil(seqan::hibf::build::bin_size_in_bits({.fpr = data.config.maximum_false_positive_rate,
.hash_count = data.config.number_of_hash_functions,
.elements = kmers_per_bin})
* data.fpr_correction[number_of_bins]);

size_t const ibf_size = ibf_node.number_of_technical_bins * bin_size;

if (current_hibf_level > 0 /* not top level */)
update_parent_kmers(parent_kmers, kmers);

return ibf_size;
}

size_t hierarchical_stats(std::vector<ibf_stats> & stats,
robin_hood::unordered_flat_set<uint64_t> & parent_kmers,
seqan::hibf::layout::graph::node const & current_node,
Expand Down
11 changes: 11 additions & 0 deletions test/api/display_layout/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ---------------------------------------------------------------------------------------------------
# Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
# Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
# This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
# shipped with this file and also available at: https://github.com/seqan/chopper/blob/main/LICENSE.md
# ---------------------------------------------------------------------------------------------------

cmake_minimum_required (VERSION 3.18)

add_api_test (compute_ibf_size_test.cpp)
target_include_directories (compute_ibf_size_test PUBLIC "${CMAKE_CURRENT_LIST_DIR}/../../../src/util/display_layout")
93 changes: 93 additions & 0 deletions test/api/display_layout/compute_ibf_size_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// ---------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/chopper/blob/main/LICENSE.md
// ---------------------------------------------------------------------------------------------------

#include <gtest/gtest.h>

#include <compute_ibf_size.hpp>

#include <hibf/build/construct_ibf.hpp> // for construct_ibf
#include <hibf/layout/compute_fpr_correction.hpp>

TEST(compute_ibf_size_test, merged_bin_is_max_bin)
{
size_t const number_of_bins = 1;
size_t const current_hibf_level = 0;

robin_hood::unordered_flat_set<uint64_t> parent_kmers;
robin_hood::unordered_flat_set<uint64_t> kmers;

for (size_t i = 0; i < 1000; ++i)
kmers.insert(i);

seqan::hibf::layout::graph::node ibf_node{
.children = {seqan::hibf::layout::graph::node{}}, // one merged bin
.parent_bin_index = 0,
.max_bin_index = 0,
.number_of_technical_bins = 64,
.favourite_child_idx = 0, // max bin is a merged bin and it is the first in `children`
.remaining_records = {} // not needed for compute_ibf_size
};

double fpr = 0.0001;
size_t hash = 2;
size_t tmax = 64;

seqan::hibf::build::build_data data{
.config = {.number_of_user_bins = 123,
.number_of_hash_functions = hash,
.maximum_false_positive_rate = fpr,
.threads = 1,
.tmax = tmax},
.fpr_correction = seqan::hibf::layout::compute_fpr_correction({.fpr = fpr, .hash_count = hash, .t_max = tmax})};

auto ibf = seqan::hibf::build::construct_ibf(parent_kmers, kmers, number_of_bins, ibf_node, data, true);

auto ibf_size = compute_ibf_size(parent_kmers, kmers, number_of_bins, ibf_node, data, current_hibf_level);

EXPECT_EQ(ibf_size, ibf.bit_size());
EXPECT_TRUE(parent_kmers.empty());
}

TEST(compute_ibf_size_test, split_bin_is_max_bin)
{
size_t const number_of_bins = 4;
size_t const current_hibf_level = 1;

robin_hood::unordered_flat_set<uint64_t> parent_kmers;
robin_hood::unordered_flat_set<uint64_t> kmers;

for (size_t i = 0; i < 1000; ++i)
kmers.insert(i);

seqan::hibf::layout::graph::node ibf_node{
.children = {seqan::hibf::layout::graph::node{}}, // one merged bin
.parent_bin_index = 0,
.max_bin_index = 0,
.number_of_technical_bins = 64,
.favourite_child_idx = std::nullopt, // max bin is a split bin
.remaining_records = {} // not needed for compute_ibf_size
};

double fpr = 0.0001;
size_t hash = 2;
size_t tmax = 64;

seqan::hibf::build::build_data data{
.config = {.number_of_user_bins = 123,
.number_of_hash_functions = hash,
.maximum_false_positive_rate = fpr,
.threads = 1,
.tmax = tmax},
.fpr_correction = seqan::hibf::layout::compute_fpr_correction({.fpr = fpr, .hash_count = hash, .t_max = tmax})};

auto ibf = seqan::hibf::build::construct_ibf(parent_kmers, kmers, number_of_bins, ibf_node, data, true);

auto ibf_size = compute_ibf_size(parent_kmers, kmers, number_of_bins, ibf_node, data, current_hibf_level);

EXPECT_EQ(ibf_size, ibf.bit_size());
EXPECT_FALSE(parent_kmers.empty());
}