mirror of
https://github.com/johrpan/geposan.git
synced 2025-10-26 02:37:25 +01:00
ranking: Remove caching for optimization
This commit is contained in:
parent
57fc119993
commit
33cc33f81a
1 changed files with 27 additions and 32 deletions
59
R/ranking.R
59
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)))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue