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
96template<typename Size_, typename Weight_>
97void compute_weights(const std::size_t num_blocks, const Size_* const sizes, const WeightPolicy policy, const VariableWeightParameters& variable, Weight_* const weights) {
98 if (policy == WeightPolicy::NONE || policy == WeightPolicy::SIZE) {
99 std::copy_n(sizes, num_blocks, weights);
100 } else if (policy == WeightPolicy::EQUAL) {
101 for (decltype(I(num_blocks)) s = 0; s < num_blocks; ++s) {
102 weights[s] = sizes[s] > 0;
103 }
104 } else {
105 for (decltype(I(num_blocks)) s = 0; s < num_blocks; ++s) {
106 weights[s] = compute_variable_weight(sizes[s], variable);
107 }
108 }
109}
110
123template<typename Weight_ = double, typename Size_>
124std::vector<Weight_> compute_weights(const std::vector<Size_>& sizes, const WeightPolicy policy, const VariableWeightParameters& variable) {
125 auto output = sanisizer::create<std::vector<Weight_> >(sizes.size());
126 compute_weights(sizes.size(), sizes.data(), policy, variable, output.data());
127 return output;
128}
129
130}
131
132#endif
Blocking utilities for libscran.
Definition average_vectors.hpp:20
void compute_weights(const std::size_t num_blocks, const Size_ *const sizes, const WeightPolicy policy, const VariableWeightParameters &variable, Weight_ *const weights)
Definition block_weights.hpp:97
double compute_variable_weight(const double s, const VariableWeightParameters &params)
Definition block_weights.hpp:67
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