tatami_stats
Matrix statistics for tatami
Loading...
Searching...
No Matches
median.hpp
Go to the documentation of this file.
1#ifndef TATAMI_STATS_MEDIAN_HPP
2#define TATAMI_STATS_MEDIAN_HPP
3
4#include "utils.hpp"
5
6#include <cmath>
7#include <vector>
8#include <algorithm>
9#include <limits>
10
11#include "tatami/tatami.hpp"
12#include "sanisizer/sanisizer.hpp"
13#include "quickstats/quickstats.hpp"
14
21namespace tatami_stats {
22
31 bool skip_nan = false;
32
37 int num_threads = 1;
38};
39
43template<typename Output_ = double, typename Value_, typename Index_>
44Output_ median_direct(Value_* ptr, Index_ num, bool skip_nan) {
45 nanable_ifelse<Value_>(
46 skip_nan,
47 [&]() -> void {
48 num = shift_nans(ptr, num);
49 },
50 []() -> void {}
51 );
52
53 return quickstats::median<Output_>(num, ptr);
54}
55
56template<typename Output_ = double, typename Value_, typename Index_>
57Output_ median_direct(Value_* value, Index_ num_nonzero, Index_ num_all, bool skip_nan) {
58 nanable_ifelse<Value_>(
59 skip_nan,
60 [&]() -> void {
61 auto new_nonzero = shift_nans(value, num_nonzero);
62 num_all -= num_nonzero - new_nonzero;
63 num_nonzero = new_nonzero;
64 },
65 []() -> void {}
66 );
67
68 return quickstats::median<Output_>(num_all, num_nonzero, value);
69}
89template<typename Value_, typename Index_, typename Output_>
90void median(const bool row, const tatami::Matrix<Value_, Index_>& mat, Output_* const output, const MedianOptions& opt) {
91 const auto dim = (row ? mat.nrow() : mat.ncol());
92 const auto otherdim = (row ? mat.ncol() : mat.nrow());
93
94 if (mat.sparse()) {
95 tatami::Options topt;
96 topt.sparse_extract_index = false;
97 topt.sparse_ordered_index = false; // we'll be sorting by value anyway.
98
99 tatami::parallelize([&](int, Index_ s, Index_ l) -> void {
100 auto ext = tatami::consecutive_extractor<true>(mat, row, s, l, topt);
102 auto vbuffer = buffer.data();
103 for (Index_ x = 0; x < l; ++x) {
104 auto range = ext->fetch(vbuffer, NULL);
105 tatami::copy_n(range.value, range.number, vbuffer);
106 output[x + s] = median_direct<Output_>(vbuffer, range.number, otherdim, opt.skip_nan);
107 }
108 }, dim, opt.num_threads);
109
110 } else {
111 tatami::parallelize([&](int, Index_ s, Index_ l) -> void {
113 auto ext = tatami::consecutive_extractor<false>(mat, row, s, l);
114 for (Index_ x = 0; x < l; ++x) {
115 auto ptr = ext->fetch(buffer.data());
116 tatami::copy_n(ptr, otherdim, buffer.data());
117 output[x + s] = median_direct<Output_>(buffer.data(), otherdim, opt.skip_nan);
118 }
119 }, dim, opt.num_threads);
120 }
121}
122
139template<typename Output_ = double, typename Value_, typename Index_>
140std::vector<Output_> median(const bool row, const tatami::Matrix<Value_, Index_>& mat, const MedianOptions& opt) {
141 const auto dim = (row ? mat.nrow() : mat.ncol());
142 auto output = sanisizer::create<std::vector<Output_> >(dim
143#ifdef TATAMI_STATS_TEST_DIRTY
144 , -1
145#endif
146 );
147 median(row, mat, output.data(), opt);
148 return output;
149}
150
151}
152
153#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 median(const bool row, const tatami::Matrix< Value_, Index_ > &mat, Output_ *const output, const MedianOptions &opt)
Definition median.hpp:90
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 median().
Definition median.hpp:26
bool skip_nan
Definition median.hpp:31
int num_threads
Definition median.hpp:37