tatami_stats
Matrix statistics for tatami
Loading...
Searching...
No Matches
quantile.hpp
Go to the documentation of this file.
1#ifndef TATAMI_STATS_QUANTILE_HPP
2#define TATAMI_STATS_QUANTILE_HPP
3
4#include "utils.hpp"
5
6#include <cmath>
7#include <vector>
8#include <algorithm>
9#include <limits>
10#include <type_traits>
11
12#include "tatami/tatami.hpp"
13#include "sanisizer/sanisizer.hpp"
14#include "quickstats/quickstats.hpp"
15
22namespace tatami_stats {
23
32 bool skip_nan = false;
33
38 int num_threads = 1;
39};
40
58template<typename Value_, typename Index_, typename Output_>
60 const bool row,
62 const double prob,
63 Output_* const output,
64 const QuantileOptions& opt
65) {
66 const auto dim = (row ? mat.nrow() : mat.ncol());
67 const auto otherdim = (row ? mat.ncol() : mat.nrow());
68 if (otherdim == 0) {
69 // Prevent initalize() from constructing with a fixed instance with otherdim == 0.
70 std::fill_n(output, dim, std::numeric_limits<Output_>::quiet_NaN());
71 return;
72 }
73
74 tatami::parallelize([&](int, Index_ s, Index_ l) -> void {
75 std::optional<quickstats::SingleQuantileFixedNumber<Output_> > qcalcs_fixed;
76 std::optional<quickstats::SingleQuantileVariableNumber<Output_> > qcalcs_var;
77 // Index_ is safe to cast to std::size_t as that's part of the tatami contract.
78 nanable_ifelse<Value_>(
79 opt.skip_nan,
80 [&]() -> void {
81 qcalcs_var.emplace(otherdim, prob);
82 },
83 [&]() -> void {
84 qcalcs_fixed.emplace(otherdim, prob);
85 }
86 );
87
88 if (mat.sparse()) {
89 tatami::Options topt;
90 topt.sparse_extract_index = false;
91 topt.sparse_ordered_index = false; // we'll be sorting by value anyway.
92 auto ext = tatami::consecutive_extractor<true>(mat, row, s, l, topt);
94 auto vbuffer = buffer.data();
95
96 for (Index_ x = 0; x < l; ++x) {
97 auto range = ext->fetch(vbuffer, NULL);
98 tatami::copy_n(range.value, range.number, vbuffer);
99
100 nanable_ifelse<Value_>(
101 opt.skip_nan,
102 [&]() -> void {
103 const auto new_non_zeros = shift_nans(vbuffer, range.number);
104 output[x + s] = (*qcalcs_var)(otherdim - (range.number - new_non_zeros), new_non_zeros, vbuffer);
105 },
106 [&]() -> void {
107 output[x + s] = (*qcalcs_fixed)(range.number, vbuffer);
108 }
109 );
110 }
111
112 } else {
114 auto ext = tatami::consecutive_extractor<false>(mat, row, s, l);
115
116 for (Index_ x = 0; x < l; ++x) {
117 auto bufptr = buffer.data();
118 auto raw = ext->fetch(bufptr);
119 tatami::copy_n(raw, otherdim, bufptr);
120
121 nanable_ifelse<Value_>(
122 opt.skip_nan,
123 [&]() -> void {
124 const auto new_total = shift_nans(bufptr, otherdim);
125 output[x + s] = (*qcalcs_var)(new_total, bufptr);
126 },
127 [&]() -> void {
128 output[x + s] = (*qcalcs_fixed)(bufptr);
129 }
130 );
131 }
132 }
133 }, dim, opt.num_threads);
134}
135
154template<typename Output_ = double, typename Value_, typename Index_>
155std::vector<Output_> quantile(
156 const bool row,
158 const double prob,
159 const QuantileOptions& opt
160) {
161 const auto dim = (row ? mat.nrow() : mat.ncol());
162 auto output = sanisizer::create<std::vector<Output_> >(dim
163#ifdef TATAMI_STATS_TEST_DIRTY
164 , -1
165#endif
166 );
167 quantile(row, mat, prob, output.data(), opt);
168 return output;
169}
170
171}
172
173#endif
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
virtual std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const=0
Functions to compute statistics from a tatami::Matrix.
Definition count.hpp:20
void quantile(const bool row, const tatami::Matrix< Value_, Index_ > &mat, const double prob, Output_ *const output, const QuantileOptions &opt)
Definition quantile.hpp:59
void range(bool row, const tatami::Matrix< Value_, Index_ > &mat, RangeBuffers< Output_ > &output, const RangeOptions &opt)
Definition range.hpp:400
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Value_ * copy_n(const Value_ *const input, const Size_ n, Value_ *const output)
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
auto consecutive_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, const Index_ iter_start, const Index_ iter_length, Args_ &&... args)
bool sparse_extract_index
bool sparse_ordered_index
Options for quantile().
Definition quantile.hpp:27
int num_threads
Definition quantile.hpp:38
bool skip_nan
Definition quantile.hpp:32