ranking: Cache optimizations

This commit is contained in:
Elias Projahn 2021-12-16 13:57:02 +01:00
parent e2b93babe5
commit 48ff555e88

View file

@ -64,31 +64,41 @@ optimal_weights <- function(analysis, methods, reference_gene_ids,
stop("Invalid analyis. Use geposan::analyze().") stop("Invalid analyis. Use geposan::analyze().")
} }
# Compute the target rank of the reference genes when applying the weights. cached(
target_rank <- function(factors) { "optimization",
data <- ranking(analysis, as.list(factors)) c(analysis$preset, methods, reference_gene_ids, target),
{ # nolint
# Compute the target rank of the reference genes when applying the
# weights.
target_rank <- function(factors) {
data <- ranking(analysis, as.list(factors))
result <- data[gene %chin% reference_gene_ids, if (target == "min") { result <- data[
min(rank) gene %chin% reference_gene_ids,
} else if (target == "max") { if (target == "min") {
max(rank) min(rank)
} else if (target == "mean") { } else if (target == "max") {
mean(rank) max(rank)
} else { } else if (target == "mean") {
stats::median(rank) mean(rank)
}] } else {
stats::median(rank)
}
]
if (result > 0) { if (result > 0) {
result result
} else { } else {
Inf Inf
}
}
initial_factors <- rep(1.0, length(methods))
names(initial_factors) <- methods
optimal_factors <- stats::optim(initial_factors, target_rank)$par
as.list(optimal_factors / max(abs(optimal_factors)))
} }
} )
initial_factors <- rep(1.0, length(methods))
names(initial_factors) <- methods
optimal_factors <- stats::optim(initial_factors, target_rank)$par
as.list(optimal_factors / max(abs(optimal_factors)))
} }