gsdecon
C++ port of the GSDecon algorithm
Loading...
Searching...
No Matches
blocked.hpp
Go to the documentation of this file.
1#ifndef GSDECON_BLOCKED_HPP
2#define GSDECON_BLOCKED_HPP
3
4#include <vector>
5
6#include "Eigen/Dense"
7
8#include "tatami/tatami.hpp"
9#include "irlba/irlba.hpp"
11
12#include "Options.hpp"
13#include "Results.hpp"
14#include "utils.hpp"
15
21namespace gsdecon {
22
48template<typename Value_, typename Index_, typename Block_, typename Float_>
49void compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* block, const Options& options, const Buffers<Float_>& output) {
50 if (internal::check_edge_cases(matrix, options.rank, output)) {
51 return;
52 }
53
55 bopt.number = options.rank;
56 bopt.scale = options.scale;
59 bopt.realize_matrix = options.realize_matrix;
60 bopt.num_threads = options.num_threads;
61 bopt.irlba_options = options.irlba_options;
62 auto res = scran_pca::blocked_pca(matrix, block, bopt);
63
64 // Here, we restore the block-specific centers. Don't be tempted into
65 // using MultiBatchPca, as that doesn't yield a rank-1 approximation
66 // that preserves global shifts between blocks.
67 static_assert(!Eigen::MatrixXd::IsRowMajor); // just double-checking...
68 const double* cptr = res.center.data();
69 size_t nfeat = res.center.cols();
70 size_t nblocks = res.center.rows();
71 std::vector<double> block_means(nblocks);
72
73 for (size_t f = 0; f < nfeat; ++f, cptr += nblocks) {
74#ifdef _OPENMP
75 #pragma omp simd
76#endif
77 for (size_t b = 0; b < nblocks; ++b) {
78 block_means[b] += cptr[b];
79 }
80 }
81 double denom = nfeat;
82 for (auto& b : block_means) {
83 b /= denom;
84 }
85
86 size_t ncells = res.components.cols();
87#ifdef _OPENMP
88 #pragma omp simd
89#endif
90 for (size_t c = 0; c < ncells; ++c) {
91 output.scores[c] = block_means[block[c]];
92 }
93 internal::process_output(res.rotation, res.components, options.scale, res.scale, output);
94}
95
114template<typename Float_ = double, typename Value_, typename Index_, typename Block_>
115Results<Float_> compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* block, const Options& options) {
116 Results<Float_> output;
117 output.weights.resize(matrix.nrow());
118 output.scores.resize(matrix.ncol());
119
120 Buffers<Float_> buffers;
121 buffers.weights = output.weights.data();
122 buffers.scores = output.scores.data();
123
124 compute_blocked(matrix, block, options, buffers);
125 return output;
126}
127
128}
129
130#endif
Classes for storing the results.
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
Gene set scoring with gsdecon.
Definition blocked.hpp:21
void compute_blocked(const tatami::Matrix< Value_, Index_ > &matrix, const Block_ *block, const Options &options, const Buffers< Float_ > &output)
Definition blocked.hpp:49
void blocked_pca(const tatami::Matrix< Value_, Index_ > &mat, const Block_ *block, const BlockedPcaOptions &options, BlockedPcaResults< EigenMatrix_, EigenVector_ > &output)
Buffers for the results of compute() and compute_blocked().
Definition Results.hpp:18
Float_ * scores
Definition Results.hpp:23
Float_ * weights
Definition Results.hpp:29
Options for compute() and compute_blocked().
Definition Options.hpp:17
bool realize_matrix
Definition Options.hpp:61
int rank
Definition Options.hpp:31
scran_blocks::WeightPolicy block_weight_policy
Definition Options.hpp:42
scran_blocks::VariableWeightParameters variable_block_weight_parameters
Definition Options.hpp:48
int num_threads
Definition Options.hpp:55
irlba::Options irlba_options
Definition Options.hpp:66
bool scale
Definition Options.hpp:37
Results of compute() and compute_blocked().
Definition Results.hpp:37
std::vector< double > scores
Definition Results.hpp:42
std::vector< double > weights
Definition Results.hpp:49
scran_blocks::VariableWeightParameters variable_block_weight_parameters
scran_blocks::WeightPolicy block_weight_policy