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
8#include "sanisizer/sanisizer.hpp"
9
10#include "utils.hpp"
11
17namespace scran_blocks {
18
31enum class WeightPolicy : char { NONE, SIZE, VARIABLE, EQUAL };
32
41 double lower_bound = 0;
42
47 double upper_bound = 1000;
48};
49
67inline double compute_variable_weight(const double s, const VariableWeightParameters& params) {
68 if (s < params.lower_bound || s == 0) {
69 return 0;
70 }
71
72 if (s > params.upper_bound) {
73 return 1;
74 }
75
76 return (s - params.lower_bound) / (params.upper_bound - params.lower_bound);
77}
78
97template<typename GetBlockSize_, typename SetBlockWeight_>
99 const std::size_t num_blocks,
100 GetBlockSize_ get_block_size,
101 const WeightPolicy policy,
102 const VariableWeightParameters& variable,
103 SetBlockWeight_ set_block_weight
104) {
105 if (policy == WeightPolicy::NONE || policy == WeightPolicy::SIZE) {
106 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
107 set_block_weight(s, get_block_size(s));
108 }
109
110 } else if (policy == WeightPolicy::EQUAL) {
111 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
112 set_block_weight(s, get_block_size(s) > 0);
113 }
114
115 } else {
116 for (I<decltype(num_blocks)> s = 0; s < num_blocks; ++s) {
117 set_block_weight(s, compute_variable_weight(get_block_size(s), variable));
118 }
119 }
120}
121
136template<typename Size_, typename Weight_>
137void compute_weights(const std::size_t num_blocks, const Size_* const sizes, const WeightPolicy policy, const VariableWeightParameters& variable, Weight_* const weights) {
139 num_blocks,
140 [&](std::size_t s) -> Size_ { return sizes[s]; },
141 policy,
142 variable,
143 [&](std::size_t s, Weight_ w) -> void { weights[s] = w; }
144 );
145}
146
159template<typename Weight_ = double, typename Size_>
160std::vector<Weight_> compute_weights(const std::vector<Size_>& sizes, const WeightPolicy policy, const VariableWeightParameters& variable) {
161 auto output = sanisizer::create<std::vector<Weight_> >(sizes.size());
162 compute_weights(sizes.size(), sizes.data(), policy, variable, output.data());
163 return output;
164}
165
166}
167
168#endif
Blocking utilities for libscran.
Definition block_weights.hpp:17
double compute_variable_weight(const double s, const VariableWeightParameters &params)
Definition block_weights.hpp:67
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:98
WeightPolicy
Definition block_weights.hpp:31
Parameters for compute_variable_weight().
Definition block_weights.hpp:36
double lower_bound
Definition block_weights.hpp:41
double upper_bound
Definition block_weights.hpp:47