120template<
typename Stat_>
194template<
typename Stat_>
202 std::vector<std::vector<Stat_> >
mean;
254template<
typename Index_,
typename Stat_>
255void process_simple_pairwise_effects(
257 const std::size_t ngroups,
258 const std::size_t nblocks,
259 const std::size_t ncombos,
260 const std::vector<Stat_>& combo_means,
261 const std::vector<Stat_>& combo_vars,
262 const std::vector<Stat_>& combo_detected,
263 const double threshold,
264 const BlockAverageInfo<Stat_>& average_info,
266 const int num_threads
268 const Stat_* total_weights_ptr = NULL;
269 std::optional<std::vector<Stat_> > total_weights_per_group;
270 if (average_info.use_mean()) {
273 total_weights_per_group = compute_total_weight_per_group(ngroups, nblocks, average_info.combo_weights().data());
274 total_weights_ptr = total_weights_per_group->data();
276 total_weights_ptr = average_info.combo_weights().data();
281 std::optional<PrecomputedPairwiseWeights<Stat_> > preweights;
282 if (average_info.use_mean()) {
284 preweights.emplace(ngroups, nblocks, average_info.combo_weights().data());
289 std::optional<std::vector<Stat_> > qbuffer, qrevbuffer;
290 std::optional<scran_blocks::SingleQuantileVariable<Stat_, typename std::vector<Stat_>::iterator> > qcalc;
291 if (!average_info.use_mean()) {
293 qrevbuffer.emplace();
294 qcalc.emplace(nblocks, average_info.quantile());
297 for (Index_ gene = start, end = start + length; gene < end; ++gene) {
298 auto in_offset = sanisizer::product_unsafe<std::size_t>(gene, ncombos);
300 if (!output.
mean.empty()) {
301 const auto tmp_means = combo_means.data() + in_offset;
302 if (average_info.use_mean()) {
303 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_means, average_info.combo_weights().data(), total_weights_ptr, output.
mean);
305 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_means, *qbuffer, *qcalc, output.
mean);
310 const auto tmp_detected = combo_detected.data() + in_offset;
311 if (average_info.use_mean()) {
312 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_detected, average_info.combo_weights().data(), total_weights_ptr, output.
detected);
314 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_detected, *qbuffer, *qcalc, output.
detected);
319 const auto out_offset = sanisizer::product_unsafe<std::size_t>(gene, ngroups, ngroups);
322 const auto tmp_means = combo_means.data() + in_offset;
323 const auto tmp_variances = combo_vars.data() + in_offset;
324 const auto outptr = output.
cohens_d + out_offset;
325 if (average_info.use_mean()) {
326 compute_pairwise_cohens_d_blockmean(tmp_means, tmp_variances, ngroups, nblocks, threshold, *preweights, outptr);
328 compute_pairwise_cohens_d_blockquantile(tmp_means, tmp_variances, ngroups, nblocks, threshold, *qbuffer, *qrevbuffer, *qcalc, outptr);
333 const auto tmp_detected = combo_detected.data() + in_offset;
335 if (average_info.use_mean()) {
336 compute_pairwise_simple_diff_blockmean(tmp_detected, ngroups, nblocks, *preweights, outptr);
338 compute_pairwise_simple_diff_blockquantile(tmp_detected, ngroups, nblocks, *qbuffer, *qcalc, outptr);
343 const auto tmp_means = combo_means.data() + in_offset;
344 const auto outptr = output.
delta_mean + out_offset;
345 if (average_info.use_mean()) {
346 compute_pairwise_simple_diff_blockmean(tmp_means, ngroups, nblocks, *preweights, outptr);
348 compute_pairwise_simple_diff_blockquantile(tmp_means, ngroups, nblocks, *qbuffer, *qcalc, outptr);
352 }, ngenes, num_threads);
355template<
typename Index_,
typename Stat_>
356ScoreMarkersPairwiseBuffers<Stat_> preallocate_pairwise_results(
358 const std::size_t ngroups,
359 ScoreMarkersPairwiseResults<Stat_>& store,
360 const ScoreMarkersPairwiseOptions& opt
362 ScoreMarkersPairwiseBuffers<Stat_> output;
364 if (opt.compute_group_mean) {
365 preallocate_average_results(ngenes, ngroups, store.mean, output.mean);
367 if (opt.compute_group_detected) {
368 preallocate_average_results(ngenes, ngroups, store.detected, output.detected);
371 const auto num_effect_sizes = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ngroups, ngroups);
373 if (opt.compute_cohens_d) {
374 store.cohens_d.resize(num_effect_sizes
375#ifdef SCRAN_MARKERS_TEST_INIT
376 , SCRAN_MARKERS_TEST_INIT
379 output.cohens_d = store.cohens_d.data();
381 if (opt.compute_auc) {
382 store.auc.resize(num_effect_sizes
383#ifdef SCRAN_MARKERS_TEST_INIT
384 , SCRAN_MARKERS_TEST_INIT
387 output.auc = store.auc.data();
389 if (opt.compute_delta_mean) {
390 store.delta_mean.resize(num_effect_sizes
391#ifdef SCRAN_MARKERS_TEST_INIT
392 , SCRAN_MARKERS_TEST_INIT
395 output.delta_mean = store.delta_mean.data();
397 if (opt.compute_delta_detected) {
398 store.delta_detected.resize(num_effect_sizes
399#ifdef SCRAN_MARKERS_TEST_INIT
400 , SCRAN_MARKERS_TEST_INIT
403 output.delta_detected = store.delta_detected.data();
419 const std::size_t ngroups,
420 const Group_*
const group,
421 const std::size_t nblocks,
422 const Block_*
const block,
423 const std::size_t ncombos,
424 const std::size_t*
const combo,
425 const std::vector<Index_>& combo_sizes,
426 const ScoreMarkersPairwiseOptions& options,
427 const ScoreMarkersPairwiseBuffers<Stat_>& output
429 const auto ngenes = matrix.
nrow();
430 const auto payload_size = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ncombos);
431 std::vector<Stat_> combo_means, combo_vars, combo_detected;
432 if (!output.mean.empty() || output.cohens_d != NULL || output.delta_mean != NULL) {
433 combo_means.resize(payload_size);
435 if (output.cohens_d != NULL) {
436 combo_vars.resize(payload_size);
438 if (!output.detected.empty() || output.delta_detected != NULL) {
439 combo_detected.resize(payload_size);
444 BlockAverageInfo<Stat_> average_info;
445 if (options.block_average_policy == BlockAveragePolicy::MEAN) {
446 average_info = BlockAverageInfo<Stat_>(
449 options.block_weight_policy,
450 options.variable_block_weight_parameters
454 average_info = BlockAverageInfo<Stat_>(options.block_quantile);
458 scan_matrix_by_row_full_auc<single_block_>(
477 scan_matrix_by_column(
480 if constexpr(single_block_) {
487 if constexpr(single_block_) {
501 process_simple_pairwise_effects(
591template<
typename Value_,
typename Index_,
typename Group_,
typename Stat_>
594 const Group_*
const group,
598 const Index_ NC = matrix.
ncol();
599 const auto group_sizes = tatami_stats::tabulate_groups(group, NC);
600 const auto ngroups = sanisizer::cast<std::size_t>(group_sizes.size());
602 internal::score_markers_pairwise<true>(
607 static_cast<int*
>(NULL),
609 static_cast<std::size_t*
>(NULL),
655template<
typename Value_,
typename Index_,
typename Group_,
typename Block_,
typename Stat_>
658 const Group_*
const group,
659 const Block_*
const block,
663 const Index_ NC = matrix.
ncol();
664 const auto ngroups = output.
mean.size();
665 const auto nblocks = tatami_stats::total_groups(block, NC);
667 const auto combinations = internal::create_combinations(ngroups, group, block, NC);
668 const auto combo_sizes = internal::tabulate_combinations<Index_>(ngroups, nblocks, combinations);
669 const auto ncombos = combo_sizes.size();
671 internal::score_markers_pairwise<false>(
673 sanisizer::cast<std::size_t>(ngroups),
675 sanisizer::cast<std::size_t>(nblocks),
677 sanisizer::cast<std::size_t>(ncombos),
701template<
typename Stat_ =
double,
typename Value_,
typename Index_,
typename Group_>
703 const auto ngroups = tatami_stats::total_groups(group, matrix.
ncol());
705 auto buffers = internal::preallocate_pairwise_results(matrix.
nrow(), ngroups, res, options);
729template<
typename Stat_ =
double,
typename Value_,
typename Index_,
typename Group_,
typename Block_>
732 const Group_*
const group,
733 const Block_*
const block,
736 const auto ngroups = tatami_stats::total_groups(group, matrix.
ncol());
738 const auto buffers = internal::preallocate_pairwise_results(matrix.
nrow(), ngroups, res, options);