From 33cc33f81ae0325f804d6c5c4125f3ee73e9370a Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Thu, 24 Feb 2022 14:51:15 +0100 Subject: [PATCH] ranking: Remove caching for optimization --- R/ranking.R | 59 ++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/R/ranking.R b/R/ranking.R index 233698b..47ec098 100644 --- a/R/ranking.R +++ b/R/ranking.R @@ -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))) }