45template <
typename Sum_,
typename Detected_>
72template <
typename Sum_,
typename Detected_>
81 std::vector<std::vector<Sum_> >
sums;
98template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
99void compute_aggregate_by_row(
101 const Factor_* factor,
110 size_t nsums = buffers.
sums.size();
111 std::vector<Sum_> tmp_sums(nsums);
112 size_t ndetected = buffers.
detected.size();
113 std::vector<Detected_> tmp_detected(ndetected);
116 std::vector<Data_> vbuffer(NC);
117 typename std::conditional<sparse_, std::vector<Index_>, Index_>::type ibuffer(NC);
119 for (Index_ x = s, end = s + l; x < end; ++x) {
121 if constexpr(sparse_) {
122 return ext->fetch(vbuffer.data(), ibuffer.data());
124 return ext->fetch(vbuffer.data());
129 std::fill(tmp_sums.begin(), tmp_sums.end(), 0);
131 if constexpr(sparse_) {
132 for (Index_ j = 0; j < row.number; ++j) {
133 tmp_sums[factor[row.index[j]]] += row.value[j];
136 for (Index_ j = 0; j < NC; ++j) {
137 tmp_sums[factor[j]] += row[j];
142 for (
size_t l = 0; l < nsums; ++l) {
143 buffers.
sums[l][x] = tmp_sums[l];
148 std::fill(tmp_detected.begin(), tmp_detected.end(), 0);
150 if constexpr(sparse_) {
151 for (Index_ j = 0; j < row.number; ++j) {
152 tmp_detected[factor[row.index[j]]] += (row.value[j] > 0);
155 for (Index_ j = 0; j < NC; ++j) {
156 tmp_detected[factor[j]] += (row[j] > 0);
160 for (
size_t l = 0; l < ndetected; ++l) {
161 buffers.
detected[l][x] = tmp_detected[l];
168template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
169void compute_aggregate_by_column(
171 const Factor_* factor,
172 const AggregateAcrossCellsBuffers<Sum_, Detected_>& buffers,
173 const AggregateAcrossCellsOptions& options)
181 std::vector<Data_> vbuffer(length);
182 typename std::conditional<sparse_, std::vector<Index_>, Index_>::type ibuffer(length);
184 size_t num_sums = buffers.sums.size();
185 auto get_sum = [&](Index_ i) -> Sum_* {
return buffers.sums[i]; };
186 tatami_stats::LocalOutputBuffers<Sum_,
decltype(get_sum)> local_sums(t, num_sums, start, length, std::move(get_sum));
187 auto get_detected = [&](Index_ i) -> Detected_* {
return buffers.detected[i]; };
188 size_t num_detected = buffers.detected.size();
189 tatami_stats::LocalOutputBuffers<Detected_,
decltype(get_detected)> local_detected(t, num_detected, start, length, std::move(get_detected));
191 for (Index_ x = 0; x < NC; ++x) {
192 auto current = factor[x];
194 if constexpr(sparse_) {
195 auto col = ext->fetch(vbuffer.data(), ibuffer.data());
197 auto cursum = local_sums.data(current);
198 for (Index_ i = 0; i < col.number; ++i) {
199 cursum[col.index[i] - start] += col.value[i];
203 auto curdetected = local_detected.data(current);
204 for (Index_ i = 0; i < col.number; ++i) {
205 curdetected[col.index[i] - start] += (col.value[i] > 0);
210 auto col = ext->fetch(vbuffer.data());
212 auto cursum = local_sums.data(current);
213 for (Index_ i = 0; i < length; ++i) {
218 auto curdetected = local_detected.data(current);
219 for (Index_ i = 0; i < length; ++i) {
220 curdetected[i] += (col[i] > 0);
226 local_sums.transfer();
227 local_detected.transfer();
228 }, p.
nrow(), options.num_threads);
255template<
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
258 const Factor_* factor,
264 internal::compute_aggregate_by_row<true>(input, factor, buffers, options);
266 internal::compute_aggregate_by_row<false>(input, factor, buffers, options);
270 internal::compute_aggregate_by_column<true>(input, factor, buffers, options);
272 internal::compute_aggregate_by_column<false>(input, factor, buffers, options);
294template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Data_,
typename Index_,
typename Factor_>
297 const Factor_* factor,
300 size_t NC = input.
ncol();
301 size_t nlevels = (NC ? *std::max_element(factor, factor + NC) + 1 : 0);
302 size_t ngenes = input.
nrow();
308 output.
sums.resize(nlevels, std::vector<Sum_>(ngenes
309#ifdef SCRAN_AGGREGATE_TEST_INIT
310 , SCRAN_AGGREGATE_TEST_INIT
313 buffers.
sums.resize(nlevels);
314 for (
size_t l = 0; l < nlevels; ++l) {
315 buffers.
sums[l] = output.
sums[l].data();
320 output.
detected.resize(nlevels, std::vector<Detected_>(ngenes
321#ifdef SCRAN_AGGREGATE_TEST_INIT
322 , SCRAN_AGGREGATE_TEST_INIT
326 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:256