tatami_stats
Matrix statistics for tatami
Loading...
Searching...
No Matches
group_median.hpp
Go to the documentation of this file.
1#ifndef TATAMI_STATS_GROUP_MEDIAN_HPP
2#define TATAMI_STATS_GROUP_MEDIAN_HPP
3
4#include "utils.hpp"
5#include "median.hpp"
6
7#include <vector>
8#include <algorithm>
9
10#include "tatami/tatami.hpp"
11#include "sanisizer/sanisizer.hpp"
12
19namespace tatami_stats {
20
29 bool skip_nan = false;
30
35 int num_threads = 1;
36};
37
59template<typename Value_, typename Index_, typename Group_, typename Output_>
61 bool row,
63 const Group_* const group,
64 const std::size_t num_groups,
65 std::vector<Output_*>& output,
66 const GroupMedianOptions& opt
67) {
68 const auto dim = (row ? mat.nrow() : mat.ncol());
69 const auto otherdim = (row ? mat.ncol() : mat.nrow());
70
71 auto group_sizes = sanisizer::create<std::vector<Index_> >(num_groups);
72 for (Index_ i = 0; i < otherdim; ++i) {
73 group_sizes[group[i]] += 1;
74 }
75
76 tatami::parallelize([&](int, Index_ start, Index_ len) -> void {
78 auto workspace = sanisizer::create<std::vector<std::vector<Value_> > >(num_groups);
79 for (I<decltype(num_groups)> g = 0; g < num_groups; ++g) {
80 sanisizer::reserve(workspace[g], group_sizes[g]);
81 }
82
83 if (mat.sparse()) {
84 tatami::Options topt;
85 topt.sparse_ordered_index = false;
86 auto ext = tatami::consecutive_extractor<true>(mat, row, start, len, topt);
88
89 for (Index_ i = 0; i < len; ++i) {
90 auto range = ext->fetch(xbuffer.data(), ibuffer.data());
91 for (Index_ j = 0; j < range.number; ++j) {
92 workspace[group[range.index[j]]].push_back(range.value[j]);
93 }
94
95 for (I<decltype(num_groups)> g = 0; g < num_groups; ++g) {
96 auto& w = workspace[g];
97 output[g][i + start] = median_direct<Output_, Value_, Index_>(w.data(), w.size(), group_sizes[g], opt.skip_nan);
98 w.clear();
99 }
100 }
101
102 } else {
103 auto ext = tatami::consecutive_extractor<false>(mat, row, start, len);
104 for (Index_ i = 0; i < len; ++i) {
105 auto ptr = ext->fetch(xbuffer.data());
106 for (Index_ j = 0; j < otherdim; ++j) {
107 workspace[group[j]].push_back(ptr[j]);
108 }
109
110 for (I<decltype(num_groups)> g = 0; g < num_groups; ++g) {
111 auto& w = workspace[g];
112 output[g][i + start] = median_direct<Output_, Value_, Index_>(w.data(), w.size(), opt.skip_nan);
113 w.clear();
114 }
115 }
116 }
117 }, dim, opt.num_threads);
118}
119
141template<typename Output_ = double, typename Value_, typename Index_, typename Group_>
142std::vector<std::vector<Output_> > group_median(
143 bool row,
145 const Group_* const group,
146 const std::size_t num_groups,
147 const GroupMedianOptions& opt
148) {
149 auto output = sanisizer::create<std::vector<std::vector<Output_> > >(num_groups);
150 auto outptrs = sanisizer::create<std::vector<Output_*> >(num_groups);
151 const auto dim = (row ? mat.nrow() : mat.ncol());
152 for (std::size_t g = 0; g < num_groups; ++g) {
154#ifdef TATAMI_STATS_TEST_DIRTY
155 , -1
156#endif
157 );
158 outptrs[g] = output[g].data();
159 }
160 group_median(row, mat, group, num_groups, outptrs, opt);
161 return output;
162}
163
164}
165
166#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
Compute row and column medians from a tatami::Matrix.
Functions to compute statistics from a tatami::Matrix.
Definition count.hpp:20
void range(bool row, const tatami::Matrix< Value_, Index_ > &mat, RangeBuffers< Output_ > &output, const RangeOptions &opt)
Definition range.hpp:400
void group_median(bool row, const tatami::Matrix< Value_, Index_ > &mat, const Group_ *const group, const std::size_t num_groups, std::vector< Output_ * > &output, const GroupMedianOptions &opt)
Definition group_median.hpp:60
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
int parallelize(Function_ fun, const Index_ tasks, const int workers)
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_ordered_index
Options for group_median().
Definition group_median.hpp:24
bool skip_nan
Definition group_median.hpp:29
int num_threads
Definition group_median.hpp:35