scran_blocks
Blocking utilities for libscran
Loading...
Searching...
No Matches
block_weights.hpp
Go to the documentation of this file.
1#ifndef SCRAN_BLOCKS_BLOCK_WEIGHTS_HPP
2#define SCRAN_BLOCKS_BLOCK_WEIGHTS_HPP
3
4#include <vector>
5#include <cstddef>
6#include <algorithm>
7#include <cassert>
8
9#include "sanisizer/sanisizer.hpp"
10
11#include "utils.hpp"
12
18namespace scran_blocks {
19
32enum class WeightPolicy : char { NONE, SIZE, VARIABLE, EQUAL };
33
42 double lower_bound = 0;
43
48 double upper_bound = 1000;
49};
50
68inline double compute_variable_weight(const double s, const VariableWeightParameters& params) {
69 assert(params.lower_bound >= 0);
70 assert(params.upper_bound >= params.lower_bound);
71 if (s <= params.lower_bound) {
72 return 0;
73 }
74 if (s >= params.upper_bound) {
75 return 1;
76 }
77 return (s - params.lower_bound) / (params.upper_bound - params.lower_bound);
78}
79
101template<typename GetBlockSize_, typename SetBlockWeight_>
103 const std::size_t num_blocks,
104 GetBlockSize_ get_block_size,
105 const WeightPolicy policy,
106 const VariableWeightParameters& variable,
107 SetBlockWeight_ set_block_weight
108) {
109 if (policy == WeightPolicy::NONE || policy == WeightPolicy::SIZE) {
110 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
111 set_block_weight(s, get_block_size(s));
112 }
113
114 } else if (policy == WeightPolicy::EQUAL) {
115 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
116 set_block_weight(s, get_block_size(s) > 0);
117 }
118
119 } else {
120 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
121 set_block_weight(s, compute_variable_weight(get_block_size(s), variable));
122 }
123 }
124}
125
140template<typename Size_, typename Weight_>
141void compute_weights(const std::size_t num_blocks, const Size_* const sizes, const WeightPolicy policy, const VariableWeightParameters& variable, Weight_* const weights) {
143 num_blocks,
144 [&](std::size_t s) -> Size_ { return sizes[s]; },
145 policy,
146 variable,
147 [&](std::size_t s, Weight_ w) -> void { weights[s] = w; }
148 );
149}
150
163template<typename Weight_ = double, typename Size_>
164std::vector<Weight_> compute_weights(const std::vector<Size_>& sizes, const WeightPolicy policy, const VariableWeightParameters& variable) {
165 auto output = sanisizer::create<std::vector<Weight_> >(sizes.size());
166 compute_weights(sizes.size(), sizes.data(), policy, variable, output.data());
167 return output;
168}
169
170}
171
172#endif
Blocking utilities for libscran.
Definition block_weights.hpp:18
double compute_variable_weight(const double s, const VariableWeightParameters &params)
Definition block_weights.hpp:68
void compute_weights(const std::size_t num_blocks, GetBlockSize_ get_block_size, const WeightPolicy policy, const VariableWeightParameters &variable, SetBlockWeight_ set_block_weight)
Definition block_weights.hpp:102
WeightPolicy
Definition block_weights.hpp:32
Parameters for compute_variable_weight().
Definition block_weights.hpp:37
double lower_bound
Definition block_weights.hpp:42
double upper_bound
Definition block_weights.hpp:48