1#ifndef SCRAN_CHOOSE_OUTLIER_FILTERS_HPP
2#define SCRAN_CHOOSE_OUTLIER_FILTERS_HPP
4#include "../utils/macros.hpp"
39 static constexpr bool lower =
true;
44 static constexpr bool upper =
true;
110 double sanitize(
double proposed,
bool log)
const {
112 if (std::isinf(proposed)) {
117 proposed = std::exp(proposed);
164 template<
bool overwrite = true,
typename Input,
typename Output>
165 void filter(
size_t n, Input* input, Output* output)
const {
167 throw std::runtime_error(
"should use filter_blocked() for multiple batches");
169 filter_<overwrite>(n, input, output, [](
size_t i) ->
size_t {
return 0; });
182 template<
typename Output = u
int8_t,
typename Input>
183 std::vector<Output>
filter(
size_t n,
const Input* input)
const {
184 std::vector<Output> output(n);
185 filter(n, input, output.data());
204 template<
bool overwrite = true,
typename Block,
typename Input,
typename Output>
205 void filter_blocked(
size_t n,
const Block* block,
const Input* input, Output* output)
const {
207 filter_<overwrite>(n, input, output, [&](
size_t i) -> Block {
return block[i]; });
209 filter<overwrite>(n, input, output);
227 template<
typename Output = u
int8_t,
typename Block,
typename Input>
228 std::vector<Output>
filter_blocked(
size_t n,
const Block* block,
const Input* input)
const {
229 std::vector<Output> output(n);
235 template<
bool overwrite,
typename Input,
typename Output,
typename Function>
236 void filter_(
size_t n,
const Input* input, Output* output, Function indexer)
const {
237 for (
size_t i = 0; i < n; ++i) {
240 if (!
lower.empty()) {
241 if (quality_control::is_less_than(input[i],
lower[b])) {
247 if (!
upper.empty()) {
248 if (quality_control::is_greater_than(input[i],
upper[b])) {
254 if constexpr(overwrite) {
274 size_t nblocks = x.
medians.size();
276 for (
size_t b = 0; b < nblocks; ++b) {
278 auto mad = x.
mads[b];
280 double& lthresh = x.
medians[b];
281 double& uthresh = x.
mads[b];
283 if (std::isnan(med)) {
285 lthresh = std::numeric_limits<double>::quiet_NaN();
288 uthresh = std::numeric_limits<double>::quiet_NaN();
291 auto delta = std::max(min_diff, num_mads * mad);
293 lthresh = sanitize(med - delta, x.
log);
296 uthresh = sanitize(med + delta, x.
log);
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
Default parameters.
Definition ChooseOutlierFilters.hpp:35
static constexpr double num_mads
Definition ChooseOutlierFilters.hpp:49
static constexpr bool lower
Definition ChooseOutlierFilters.hpp:39
static constexpr bool upper
Definition ChooseOutlierFilters.hpp:44
static constexpr double min_diff
Definition ChooseOutlierFilters.hpp:54
Outlier thresholds for QC filtering.
Definition ChooseOutlierFilters.hpp:127
std::vector< Output > filter_blocked(size_t n, const Block *block, const Input *input) const
Definition ChooseOutlierFilters.hpp:228
std::vector< double > lower
Definition ChooseOutlierFilters.hpp:141
std::vector< double > upper
Definition ChooseOutlierFilters.hpp:148
void filter(size_t n, Input *input, Output *output) const
Definition ChooseOutlierFilters.hpp:165
std::vector< Output > filter(size_t n, const Input *input) const
Definition ChooseOutlierFilters.hpp:183
void filter_blocked(size_t n, const Block *block, const Input *input, Output *output) const
Definition ChooseOutlierFilters.hpp:205
Define outlier filters from the median and MAD.
Definition ChooseOutlierFilters.hpp:30
ChooseOutlierFilters & set_min_diff(double m=Defaults::min_diff)
Definition ChooseOutlierFilters.hpp:104
ChooseOutlierFilters & set_num_mads(double n=Defaults::num_mads)
Definition ChooseOutlierFilters.hpp:92
ChooseOutlierFilters & set_lower(bool l=Defaults::lower)
Definition ChooseOutlierFilters.hpp:70
Thresholds run(ComputeMedianMad::Results x) const
Definition ChooseOutlierFilters.hpp:273
ChooseOutlierFilters & set_upper(bool u=Defaults::upper)
Definition ChooseOutlierFilters.hpp:81