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
15namespace scran_blocks {
16
28enum class WeightPolicy : char { NONE, VARIABLE, EQUAL };
29
38 double lower_bound = 0;
39
44 double upper_bound = 1000;
45};
46
63inline double compute_variable_weight(double s, const VariableWeightParameters& params) {
64 if (s < params.lower_bound || s == 0) {
65 return 0;
66 }
67
68 if (s > params.upper_bound) {
69 return 1;
70 }
71
72 return (s - params.lower_bound) / (params.upper_bound - params.lower_bound);
73}
74
89template<typename Size_, typename Weight_>
90void compute_weights(std::size_t num_blocks, const Size_* sizes, WeightPolicy policy, const VariableWeightParameters& variable, Weight_* weights) {
91 if (policy == WeightPolicy::NONE) {
92 std::copy_n(sizes, num_blocks, weights);
93 } else if (policy == WeightPolicy::EQUAL) {
94 for (decltype(num_blocks) s = 0; s < num_blocks; ++s) {
95 weights[s] = sizes[s] > 0;
96 }
97 } else {
98 for (decltype(num_blocks) s = 0; s < num_blocks; ++s) {
99 weights[s] = compute_variable_weight(sizes[s], variable);
100 }
101 }
102}
103
116template<typename Weight_ = double, typename Size_>
117std::vector<Weight_> compute_weights(const std::vector<Size_>& sizes, WeightPolicy policy, const VariableWeightParameters& variable) {
118 auto output = sanisizer::create<std::vector<Weight_> >(sizes.size());
119 compute_weights(sizes.size(), sizes.data(), policy, variable, output.data());
120 return output;
121}
122
123}
124
125#endif
Blocking utilities for libscran.
Definition average_vectors.hpp:18
double compute_variable_weight(double s, const VariableWeightParameters &params)
Definition block_weights.hpp:63
void compute_weights(std::size_t num_blocks, const Size_ *sizes, WeightPolicy policy, const VariableWeightParameters &variable, Weight_ *weights)
Definition block_weights.hpp:90
WeightPolicy
Definition block_weights.hpp:28
Parameters for compute_variable_weight().
Definition block_weights.hpp:33
double lower_bound
Definition block_weights.hpp:38
double upper_bound
Definition block_weights.hpp:44