1#ifndef SCRAN_NORMALIZATION_SANITIZE_SIZE_FACTORS_HPP
2#define SCRAN_NORMALIZATION_SANITIZE_SIZE_FACTORS_HPP
49 for (
size_t i = 0; i < n; ++i) {
50 auto sf = size_factors[i];
55 }
else if (std::isnan(sf)) {
57 }
else if (std::isinf(sf)) {
208 static double find_smallest_valid_factor(
size_t n, T* size_factors) {
209 auto smallest = std::numeric_limits<double>::infinity();
210 for (
size_t i = 0; i < n; ++i) {
211 auto s = size_factors[i];
212 if (smallest > s && s > 0) {
217 if (std::isinf(smallest)) {
224 static double find_largest_valid_factor(
size_t n, T* size_factors) {
227 for (
size_t i = 0; i < n; ++i) {
228 auto s = size_factors[i];
229 if (std::isfinite(s) && largest < s) {
255 if (handle_negative == HandlerAction::ERROR) {
256 throw std::runtime_error(
"detected negative size factor");
257 }
else if (handle_negative == HandlerAction::SANITIZE) {
258 smallest = find_smallest_valid_factor(n, size_factors);
259 for (
size_t i = 0; i < n; ++i) {
260 auto& s = size_factors[i];
269 if (handle_zero == HandlerAction::ERROR) {
270 throw std::runtime_error(
"detected size factor of zero");
271 }
else if (handle_zero == HandlerAction::SANITIZE) {
273 smallest = find_smallest_valid_factor(n, size_factors);
275 for (
size_t i = 0; i < n; ++i) {
276 auto& s = size_factors[i];
285 if (handle_nan == HandlerAction::ERROR) {
286 throw std::runtime_error(
"detected NaN size factor");
287 }
else if (handle_nan == HandlerAction::SANITIZE) {
288 for (
size_t i = 0; i < n; ++i) {
289 auto& s = size_factors[i];
298 if (handle_infinite == HandlerAction::ERROR) {
299 throw std::runtime_error(
"detected infinite size factor");
300 }
else if (handle_infinite == HandlerAction::SANITIZE) {
301 auto largest = find_largest_valid_factor(n, size_factors);
302 for (
size_t i = 0; i < n; ++i) {
303 auto& s = size_factors[i];
321 void run(
size_t n, T* size_factors)
const {
323 run(n, size_factors, status);
Sanitize invalid size factors.
Definition SanitizeSizeFactors.hpp:75
SanitizeSizeFactors & set_handle_nan(HandlerAction h=Defaults::handle_nan)
Definition SanitizeSizeFactors.hpp:160
void run(size_t n, T *size_factors, const SizeFactorValidity &status) const
Definition SanitizeSizeFactors.hpp:251
HandlerAction
Definition SanitizeSizeFactors.hpp:84
void run(size_t n, T *size_factors) const
Definition SanitizeSizeFactors.hpp:321
SanitizeSizeFactors & set_handle_infinite(HandlerAction h=Defaults::handle_infinite)
Definition SanitizeSizeFactors.hpp:174
SanitizeSizeFactors & set_handle_negative(HandlerAction h=Defaults::handle_negative)
Definition SanitizeSizeFactors.hpp:147
SanitizeSizeFactors & set_handle_non_finite(HandlerAction h)
Definition SanitizeSizeFactors.hpp:200
SanitizeSizeFactors & set_handle_non_positive(HandlerAction h)
Definition SanitizeSizeFactors.hpp:187
SanitizeSizeFactors & set_handle_zero(HandlerAction h=Defaults::handle_zero)
Definition SanitizeSizeFactors.hpp:133
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
SizeFactorValidity validate_size_factors(size_t n, const T *size_factors)
Definition SanitizeSizeFactors.hpp:46
Default parameters.
Definition SanitizeSizeFactors.hpp:89
static constexpr HandlerAction handle_zero
Definition SanitizeSizeFactors.hpp:93
static constexpr HandlerAction handle_infinite
Definition SanitizeSizeFactors.hpp:108
static constexpr HandlerAction handle_negative
Definition SanitizeSizeFactors.hpp:98
static constexpr HandlerAction handle_nan
Definition SanitizeSizeFactors.hpp:103
Validity of size factors.
Definition SanitizeSizeFactors.hpp:12
bool has_nan
Definition SanitizeSizeFactors.hpp:26
bool has_zero
Definition SanitizeSizeFactors.hpp:21
bool has_infinite
Definition SanitizeSizeFactors.hpp:31
bool has_negative
Definition SanitizeSizeFactors.hpp:16