ranking: Remove caching for optimization

This commit is contained in:
Elias Projahn 2022-02-24 14:51:15 +01:00
parent 57fc119993
commit 33cc33f81a

View file

@ -65,41 +65,36 @@ optimal_weights <- function(analysis, methods, reference_gene_ids,
stop("Invalid analyis. Use geposan::analyze().")
}
cached(
"optimization",
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") {
min(rank)
} else if (target == "max") {
max(rank)
} else if (target == "mean") {
mean(rank)
} else {
stats::median(rank)
}
]
# Compute the target rank of the reference genes when applying the
# weights.
target_rank <- function(factors) {
data <- ranking(analysis, as.list(factors))
if (result > 0) {
result
} else {
Inf
}
result <- data[
gene %chin% reference_gene_ids,
if (target == "min") {
min(rank)
} else if (target == "max") {
max(rank)
} else if (target == "mean") {
mean(rank)
} else {
stats::median(rank)
}
]
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)))
if (result > 0) {
result
} else {
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)))
}