1#ifndef SCRAN_NORM_CHOOSE_PSEUDO_COUNT_HPP
2#define SCRAN_NORM_CHOOSE_PSEUDO_COUNT_HPP
42template<
typename Float_>
43Float_ find_quantile(Float_ quantile,
size_t n, Float_* ptr) {
44 double raw =
static_cast<double>(n - 1) * quantile;
45 size_t index = std::ceil(raw);
46 std::nth_element(ptr, ptr + index, ptr + n);
47 double upper = *(ptr + index);
48 std::nth_element(ptr, ptr + index - 1, ptr + index);
49 double lower = *(ptr + index - 1);
50 return lower * (index - raw) + upper * (raw - (index - 1));
86template<
typename Float_>
94 for (
size_t i = 0; i < num; ++i) {
95 auto val = size_factors[i];
96 if (std::isfinite(val) && val > 0) {
98 size_factors[counter] = val;
109 double lower_sf, upper_sf;
111 lower_sf = *std::min_element(size_factors, size_factors + num);
112 upper_sf = *std::max_element(size_factors, size_factors + num);
114 lower_sf = internal::find_quantile(options.
quantile, num, size_factors);
115 upper_sf = internal::find_quantile(1 - options.
quantile, num, size_factors);
119 Float_ pseudo_count = (1.0 / lower_sf - 1.0 / upper_sf) / (8 * options.
max_bias);
121 return std::max(options.
min_value, pseudo_count);
134template<
typename Float_>
136 std::vector<Float_> buffer(size_factors, size_factors + num);
Scaling normalization of single-cell data.
Definition center_size_factors.hpp:18
Float_ choose_pseudo_count(size_t num, const Float_ *size_factors, const ChoosePseudoCountOptions &options)
Definition choose_pseudo_count.hpp:135
Float_ choose_pseudo_count_raw(size_t num, Float_ *size_factors, const ChoosePseudoCountOptions &options)
Definition choose_pseudo_count.hpp:87
Options for choose_pseudo_count().
Definition choose_pseudo_count.hpp:17
double quantile
Definition choose_pseudo_count.hpp:23
double max_bias
Definition choose_pseudo_count.hpp:28
double min_value
Definition choose_pseudo_count.hpp:34