tatami_test
Utilities for testing tatami libraries
Loading...
Searching...
No Matches
define_non_target_part.hpp
Go to the documentation of this file.
1#ifndef TATAMI_TEST_DEFINE_DIMENSION_PART_HPP
2#define TATAMI_TEST_DEFINE_DIMENSION_PART_HPP
3
4#include <vector>
5#include <random>
6#include <optional>
7#include <algorithm>
8#include <cassert>
9
11#include "sanisizer/sanisizer.hpp"
12
13#include "create_seed.hpp"
14#include "utils.hpp"
15
21namespace tatami_test {
22
40template<typename Index_>
41std::pair<Index_, Index_> define_non_target_block(
42 const Index_ extent,
43 const double relative_start,
44 const double relative_length
45) {
46 if (!extent) {
47 return std::pair<Index_, Index_>(0, 0);
48 } else {
49 // Use rounding to avoid excessive truncation of near-integer values.
50 // Note that this requires some care to ensure that start + length does not exceed extent;
51 // instead, we compute the rounded endpoint, and then subtract the start to get the length.
52 Index_ actual_start = std::min(extent - 1, static_cast<Index_>(std::round(relative_start * extent)));
53 Index_ actual_end = std::min(extent, static_cast<Index_>(std::round((relative_start + relative_length) * extent)));
54 return std::pair<Index_, Index_>(actual_start, actual_end - actual_start);
55 }
56}
57
73template<typename Value_, typename Index_>
74std::pair<Index_, Index_> define_non_target_block(
76 bool use_row,
77 const double relative_start,
78 const double relative_length
79) {
80 const Index_ nsecondary = (use_row ? matrix.ncol() : matrix.nrow());
81 return define_non_target_block(nsecondary, relative_start, relative_length);
82}
83
84
101template<typename Index_>
102tatami::VectorPtr<Index_> define_non_target_indices(const Index_ start, const Index_ length, const double probability, const std::optional<SeedType>& seed) {
103 auto ptr = new std::vector<Index_>;
104 tatami::VectorPtr<Index_> output(ptr);
105 if (length == 0) {
106 return output;
107 }
108 const Index_ max = start + length;
109
110 SeedType seed0;
111 if (seed.has_value()) {
112 seed0 = *seed;
113 } else {
114 seed0 = create_seed(start, length, probability);
115 }
116
117 RngEngine rng(seed0);
118 ptr->push_back(start);
119 std::uniform_real_distribution udist;
120 for (Index_ i = start + 1; i < max; ++i) {
121 if (udist(rng) < probability) {
122 ptr->push_back(i);
123 }
124 }
125 if (ptr->back() != static_cast<Index_>(max - 1)) {
126 ptr->push_back(max - 1);
127 }
128
129 return output;
130}
131
148template<typename Index_>
150 const Index_ extent,
151 const double relative_start,
152 const double relative_length,
153 const double probability,
154 const std::optional<SeedType>& seed
155) {
156 auto converted = define_non_target_block(extent, relative_start, relative_length);
157 return define_non_target_indices(converted.first, converted.second, probability, seed);
158}
159
178template<typename Value_, typename Index_>
180 const tatami::Matrix<Value_, Index_>& matrix,
181 bool use_row,
182 const double relative_start,
183 const double relative_length,
184 const double probability,
185 const std::optional<SeedType>& seed
186) {
187 const Index_ nsecondary = (use_row ? matrix.ncol() : matrix.nrow());
188 return define_non_target_indices(nsecondary, relative_start, relative_length, probability, seed);
189}
190
194// For back-compatibility.
195template<typename Index_>
196tatami::VectorPtr<Index_> create_indexed_subset(const Index_ extent, const double relative_start, const double probability, const SeedType seed) {
197 return define_non_target_indices(extent, relative_start, 1 - relative_start, probability, seed);
198}
206template<typename Value_, typename Index_>
207bool check_block(const tatami::Matrix<Value_, Index_>& matrix, bool use_row, Index_ start, Index_ length) {
208 const Index_ nsecondary = (use_row ? matrix.ncol() : matrix.nrow());
209 return start >= 0 &&
210 (start < nsecondary || (nsecondary == 0 && start == 0)) &&
211 length >= 0
212 && sanisizer::is_less_than_or_equal(length, nsecondary - start);
213}
214
215template<typename Value_, typename Index_>
216bool check_indices(const tatami::Matrix<Value_, Index_>& matrix, bool use_row, const tatami::VectorPtr<Index_>& indices_ptr) {
217 if (!indices_ptr) {
218 return false;
219 }
220
221 const auto num_indices = indices_ptr->size();
222 if (num_indices == 0) {
223 return true;
224 }
225
226 auto previous = indices_ptr->front();
227 if (previous < 0) {
228 return false;
229 }
230
231 for (I<decltype(num_indices)> i = 1; i < num_indices; ++i) {
232 if ((*indices_ptr)[i] <= previous) {
233 return false;
234 }
235 previous = (*indices_ptr)[i];
236 }
237 const Index_ nsecondary = (use_row ? matrix.ncol() : matrix.nrow());
238 return indices_ptr->back() < nsecondary;
239}
244}
245
246#endif
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
create a seed.
Utilities for testing tatami libraries.
Definition create_seed.hpp:17
SeedType create_seed(Args_ &&... args)
Definition create_seed.hpp:93
std::mt19937_64 RngEngine
Definition utils.hpp:26
RngEngine::result_type SeedType
Definition utils.hpp:31
tatami::VectorPtr< Index_ > define_non_target_indices(const Index_ start, const Index_ length, const double probability, const std::optional< SeedType > &seed)
Definition define_non_target_part.hpp:102
std::pair< Index_, Index_ > define_non_target_block(const Index_ extent, const double relative_start, const double relative_length)
Definition define_non_target_part.hpp:41
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Miscellaneous utilities.