scran
C++ library for basic single-cell RNA-seq analyses
Loading...
Searching...
No Matches
blocking.hpp
Go to the documentation of this file.
1#ifndef SCRAN_UTILS_BLOCKING_HPP
2#define SCRAN_UTILS_BLOCKING_HPP
3
4#include "macros.hpp"
5
6#include <vector>
7#include <algorithm>
8#include <stdexcept>
9
15namespace scran {
16
28template<typename Id_>
29size_t count_ids(size_t length, const Id_* ids) {
30 if (!length) {
31 return 0;
32 } else {
33 return static_cast<size_t>(*std::max_element(ids, ids + length)) + 1;
34 }
35}
36
51template<typename Output_ = int, bool allow_zeros_ = false, typename Id_>
52std::vector<Output_> tabulate_ids(size_t length, const Id_* ids, bool allow_zeros = false) {
53 size_t nids = count_ids(length, ids);
54
55 std::vector<Output_> ids_size(nids);
56 for (size_t j = 0; j < length; ++j) {
57 ++ids_size[ids[j]];
58 }
59
60 if (!allow_zeros) {
61 for (auto b : ids_size) {
62 if (b == 0) {
63 throw std::runtime_error("IDs must be 0-based and consecutive with no empty blocks");
64 }
65 }
66 }
67
68 return ids_size;
69}
70
82enum class WeightPolicy : char { NONE, VARIABLE, EQUAL };
83
93 constexpr VariableBlockWeightParameters(double l = 0, double u = 1000) : upper_bound(u), lower_bound(l) {}
94
99
104};
105
123inline double variable_block_weight(double s, const VariableBlockWeightParameters& params) {
124 if (s < params.lower_bound || s == 0) {
125 return 0;
126 }
127
128 if (s > params.upper_bound) {
129 return 1;
130 }
131
132 return (s - params.lower_bound) / (params.upper_bound - params.lower_bound);
133}
134
147template<typename Size_>
148std::vector<double> compute_block_weights(const std::vector<Size_>& sizes, WeightPolicy policy, const VariableBlockWeightParameters& param) {
149 size_t nblocks = sizes.size();
150 std::vector<double> weights;
151 weights.reserve(nblocks);
152
153 if (policy == WeightPolicy::NONE) {
154 weights.insert(weights.end(), sizes.begin(), sizes.end());
155 } else if (policy == WeightPolicy::EQUAL) {
156 for (auto s : sizes) {
157 weights.push_back(s > 0);
158 }
159 } else {
160 for (auto s : sizes) {
161 weights.push_back(variable_block_weight(s, param));
162 }
163 }
164
165 return weights;
166}
167
168}
169
170#endif
Set common macros used through libscran.
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
double variable_block_weight(double s, const VariableBlockWeightParameters &params)
Definition blocking.hpp:123
size_t count_ids(size_t length, const Id_ *ids)
Definition blocking.hpp:29
std::vector< Output_ > tabulate_ids(size_t length, const Id_ *ids, bool allow_zeros=false)
Definition blocking.hpp:52
std::vector< double > compute_block_weights(const std::vector< Size_ > &sizes, WeightPolicy policy, const VariableBlockWeightParameters &param)
Definition blocking.hpp:148
WeightPolicy
Definition blocking.hpp:82
Parameters for variable_block_weight().
Definition blocking.hpp:87
double upper_bound
Definition blocking.hpp:103
constexpr VariableBlockWeightParameters(double l=0, double u=1000)
Definition blocking.hpp:93
double lower_bound
Definition blocking.hpp:98