scran_blocks
Blocking utilities for libscran
Loading...
Searching...
No Matches
parallel_quantiles.hpp
Go to the documentation of this file.
1#ifndef SCRAN_BLOCKS_PARALLEL_QUANTILES_HPP
2#define SCRAN_BLOCKS_PARALLEL_QUANTILES_HPP
3
4#include <vector>
5#include <limits>
6#include <algorithm>
7#include <vector>
8#include <cstddef>
9#include <cmath>
10#include <optional>
11#include <cassert>
12
13#include "utils.hpp"
14
15#include "sanisizer/sanisizer.hpp"
16#include "quickstats/quickstats.hpp"
17
23namespace scran_blocks {
24
42template<typename Stat_, typename Output_>
43void parallel_quantiles(const std::size_t n, const std::vector<Stat_*>& in, const double quantile, Output_* const out, const bool skip_nan) {
44 const auto nblocks = in.size();
45 if (nblocks == 0) {
46 std::fill_n(out, n, std::numeric_limits<Output_>::quiet_NaN());
47 return;
48 } else if (nblocks == 1) {
49 std::copy_n(in[0], n, out);
50 return;
51 }
52
53 std::vector<Stat_> tmp_buffer;
54 tmp_buffer.reserve(nblocks);
55
56 if (skip_nan) {
57 quickstats::SingleQuantileVariableNumber<Output_, I<decltype(nblocks)> > calcs(nblocks, quantile);
58 for (std::size_t g = 0; g < n; ++g) {
59 tmp_buffer.clear();
60 for (I<decltype(nblocks)> b = 0; b < nblocks; ++b) {
61 const auto val = in[b][g];
62 if (!std::isnan(val)) {
63 tmp_buffer.push_back(val);
64 }
65 }
66 out[g] = calcs(tmp_buffer.size(), tmp_buffer.data());
67 }
68
69 } else {
70 quickstats::SingleQuantileFixedNumber<Output_, I<decltype(nblocks)> > calc(nblocks, quantile);
71 for (std::size_t g = 0; g < n; ++g) {
72 tmp_buffer.clear();
73 for (I<decltype(nblocks)> b = 0; b < nblocks; ++b) {
74 tmp_buffer.push_back(in[b][g]);
75 }
76 out[g] = calc(tmp_buffer.data());
77 }
78 }
79}
80
96template<typename Output_ = double, typename Stat_>
97std::vector<Output_> parallel_quantiles(const std::size_t n, const std::vector<Stat_*>& in, const double quantile, const bool skip_nan) {
98 auto out = sanisizer::create<std::vector<Output_> >(n);
99 parallel_quantiles(n, in, quantile, out.data(), skip_nan);
100 return out;
101}
102
103}
104
105#endif
106
Blocking utilities for libscran.
Definition block_weights.hpp:17
void parallel_quantiles(const std::size_t n, const std::vector< Stat_ * > &in, const double quantile, Output_ *const out, const bool skip_nan)
Definition parallel_quantiles.hpp:43