43template <
typename Sum_,
typename Detected_>
70template <
typename Sum_,
typename Detected_>
79 std::vector<std::vector<Sum_> >
sums;
96template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
97void compute_aggregate_by_row(
99 const Factor_* factor,
108 size_t nsums = buffers.
sums.size();
109 std::vector<Sum_> tmp_sums(nsums);
110 size_t ndetected = buffers.
detected.size();
111 std::vector<Detected_> tmp_detected(ndetected);
114 std::vector<Data_> vbuffer(NC);
115 typename std::conditional<sparse_, std::vector<Index_>, Index_>::type ibuffer(NC);
117 for (Index_ x = s, end = s + l; x < end; ++x) {
119 if constexpr(sparse_) {
120 return ext->fetch(vbuffer.data(), ibuffer.data());
122 return ext->fetch(vbuffer.data());
127 std::fill(tmp_sums.begin(), tmp_sums.end(), 0);
129 if constexpr(sparse_) {
130 for (Index_ j = 0; j < row.number; ++j) {
131 tmp_sums[factor[row.index[j]]] += row.value[j];
134 for (Index_ j = 0; j < NC; ++j) {
135 tmp_sums[factor[j]] += row[j];
140 for (
size_t l = 0; l < nsums; ++l) {
141 buffers.
sums[l][x] = tmp_sums[l];
146 std::fill(tmp_detected.begin(), tmp_detected.end(), 0);
148 if constexpr(sparse_) {
149 for (Index_ j = 0; j < row.number; ++j) {
150 tmp_detected[factor[row.index[j]]] += (row.value[j] > 0);
153 for (Index_ j = 0; j < NC; ++j) {
154 tmp_detected[factor[j]] += (row[j] > 0);
158 for (
size_t l = 0; l < ndetected; ++l) {
159 buffers.
detected[l][x] = tmp_detected[l];
166template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
167void compute_aggregate_by_column(
169 const Factor_* factor,
170 const AggregateAcrossCellsBuffers<Sum_, Detected_>& buffers,
171 const AggregateAcrossCellsOptions& options)
179 std::vector<Data_> vbuffer(l);
180 typename std::conditional<sparse_, std::vector<Index_>, Index_>::type ibuffer(l);
182 for (Index_ x = 0; x < NC; ++x) {
183 auto current = factor[x];
185 if constexpr(sparse_) {
186 auto col = ext->fetch(vbuffer.data(), ibuffer.data());
187 if (buffers.sums.size()) {
188 auto& cursum = buffers.sums[current];
189 for (Index_ i = 0; i < col.number; ++i) {
190 cursum[col.index[i]] += col.value[i];
194 if (buffers.detected.size()) {
195 auto& curdetected = buffers.detected[current];
196 for (Index_ i = 0; i < col.number; ++i) {
197 curdetected[col.index[i]] += (col.value[i] > 0);
202 auto col = ext->fetch(vbuffer.data());
204 if (buffers.sums.size()) {
205 auto cursum = buffers.sums[current] + s;
206 for (Index_ i = 0; i < l; ++i) {
211 if (buffers.detected.size()) {
212 auto curdetected = buffers.detected[current] + s;
213 for (Index_ i = 0; i < l; ++i) {
214 curdetected[i] += (col[i] > 0);
219 }, p.
nrow(), options.num_threads);
246template<
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
249 const Factor_* factor,
255 internal::compute_aggregate_by_row<true>(input, factor, buffers, options);
257 internal::compute_aggregate_by_row<false>(input, factor, buffers, options);
261 internal::compute_aggregate_by_column<true>(input, factor, buffers, options);
263 internal::compute_aggregate_by_column<false>(input, factor, buffers, options);
285template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Data_,
typename Index_,
typename Factor_>
288 const Factor_* factor,
291 size_t NC = input.
ncol();
292 size_t nlevels = (NC ? *std::max_element(factor, factor + NC) + 1 : 0);
293 size_t ngenes = input.
nrow();
299 output.
sums.resize(nlevels, std::vector<Sum_>(ngenes));
300 buffers.
sums.resize(nlevels);
301 for (
size_t l = 0; l < nlevels; ++l) {
302 buffers.
sums[l] = output.
sums[l].data();
307 output.
detected.resize(nlevels, std::vector<Detected_>(ngenes));
309 for (
size_t l = 0; l < nlevels; ++l) {
void aggregate_across_cells(const tatami::Matrix< Data_, Index_ > &input, const Factor_ *factor, const AggregateAcrossCellsBuffers< Sum_, Detected_ > &buffers, const AggregateAcrossCellsOptions &options)
Definition aggregate_across_cells.hpp:247
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)