From b539bfc58bd6ffcad7cd0433cd115c48bb0a1f27 Mon Sep 17 00:00:00 2001 From: borishejblum Date: Tue, 20 May 2025 11:46:30 +0200 Subject: [PATCH 1/2] DCQ default + lbfgs3 --- DESCRIPTION | 1 + NAMESPACE | 1 + R/DICEPRO.R | 2 +- R/NMF_optim.R | 12 ++++++------ man/DICEPRO.Rd | 2 +- man/nmf_lbfgsb.Rd | 6 ++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cd9f378..32af9b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,6 +28,7 @@ LinkingTo: RcppEigen Imports: matrixStats, + lbfgsb3c, dplyr, rBeta2009, reshape2, diff --git a/NAMESPACE b/NAMESPACE index 3cc5c20..0ef2726 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ importFrom(dplyr,across) importFrom(dplyr,everything) importFrom(dplyr,group_by) importFrom(dplyr,summarise) +importFrom(lbfgsb3c,lbfgsb3) importFrom(matrixStats,rowVars) importFrom(parallel,detectCores) importFrom(pcaMethods,prep) diff --git a/R/DICEPRO.R b/R/DICEPRO.R index 2b081b0..70cb18c 100644 --- a/R/DICEPRO.R +++ b/R/DICEPRO.R @@ -61,7 +61,7 @@ #' #' result <- DICEPRO(reference = data_simulation$reference, #' bulk = data_simulation$bulk, -#' methodDeconv = "CSx", +#' methodDeconv = "DCQ", #' W_prime = 0, #' bulkName = "Bulk_Sample", #' refName = "Reference_Data", diff --git a/R/NMF_optim.R b/R/NMF_optim.R index dc11289..518f3cd 100644 --- a/R/NMF_optim.R +++ b/R/NMF_optim.R @@ -13,10 +13,8 @@ #' @param lambda_ Regularization parameter (optional, defaults to 10). It is a scalar. #' @param gamma_par Penalty factor for the sum of cell type proportions (optional, defaults to 100). It is a scalar. #' @param N_unknownCT Number of unknown cell types to model (optional, defaults to 1). It is a scalar. -#' @param con A list of control parameters for the optimization process passed to `optim` (optional). -#' - `fnscale`: Scaling factor for the objective function (optional, defaults to 1). +#' @param con A list of control parameters for the optimization process passed to `lbfgs3c` (optional). #' - `maxit`: Maximum number of iterations (optional, defaults to 5000). -#' - `tmax`: Maximum number of function evaluations (optional, defaults to 50). #' #' @return A list containing: #' - `w`: The optimized matrix W, a vector of length N_gene. @@ -30,11 +28,12 @@ #' #' @importFrom stats optim #' @importFrom stats sd +#' @importFrom lbfgsb3c lbfgsb3 #' #' @export nmf_lbfgsb <- function(r_dataset, W_prime = 0, p_prime = 0, lambda_ = 10, gamma_par = 100, - N_unknownCT = 1, con = list(fnscale = 1, maxit = 3e3, tmax = 30)) { + N_unknownCT = 1, con = list(maxit = 3e3)) { B <- as.matrix(r_dataset$B) p_cb <- r_dataset$P_cb W_cb <- r_dataset$W_cb @@ -83,7 +82,7 @@ nmf_lbfgsb <- function(r_dataset, W_prime = 0, p_prime = 0, lambda_ = 10, gamma_ return(grad) } - result <- try(optim(par = theta, fn = obj_fun, gr = grad_obj_fun, + result <- try(lbfgsb3c::lbfgsb3c(par = theta, fn = obj_fun, gr = grad_obj_fun, lower = c(rep(0, length(theta) - 1), 1e-6), upper = c(rep(Inf, N_gene * N_cellsType), rep(1, N_sample * N_cellsType), Inf), control = con, method = "L-BFGS-B"), silent=TRUE) @@ -120,7 +119,8 @@ nmf_lbfgsb <- function(r_dataset, W_prime = 0, p_prime = 0, lambda_ = 10, gamma_ frobNorm = obj_term1_opt, constNorm = obj_term2_opt, c1 = obj_term3_opt, c2 = obj_term4_opt, objectiveValue = obj_term1_opt+obj_term2_opt, penalty = obj_term3_opt + obj_term4_opt, cvrge = result$convergence, - constraint = abs(1 - constraints)) + constraint = abs(1 - constraints), + iternum <- result$counts[1]) return(results) } } diff --git a/man/DICEPRO.Rd b/man/DICEPRO.Rd index b077bde..2511c09 100644 --- a/man/DICEPRO.Rd +++ b/man/DICEPRO.Rd @@ -86,7 +86,7 @@ data_simulation <- simulation(loi = "gauss", scenario = " ", bias = TRUE, nSampl result <- DICEPRO(reference = data_simulation$reference, bulk = data_simulation$bulk, - methodDeconv = "CSx", + methodDeconv = "DCQ", W_prime = 0, bulkName = "Bulk_Sample", refName = "Reference_Data", diff --git a/man/nmf_lbfgsb.Rd b/man/nmf_lbfgsb.Rd index 54a5d9f..ba066db 100644 --- a/man/nmf_lbfgsb.Rd +++ b/man/nmf_lbfgsb.Rd @@ -11,7 +11,7 @@ nmf_lbfgsb( lambda_ = 10, gamma_par = 100, N_unknownCT = 1, - con = list(fnscale = 1, maxit = 3000, tmax = 30) + con = list(maxit = 3000) ) } \arguments{ @@ -32,11 +32,9 @@ nmf_lbfgsb( \item{N_unknownCT}{Number of unknown cell types to model (optional, defaults to 1). It is a scalar.} -\item{con}{A list of control parameters for the optimization process passed to \code{optim} (optional). +\item{con}{A list of control parameters for the optimization process passed to \code{lbfgs3c} (optional). \itemize{ -\item \code{fnscale}: Scaling factor for the objective function (optional, defaults to 1). \item \code{maxit}: Maximum number of iterations (optional, defaults to 5000). -\item \code{tmax}: Maximum number of function evaluations (optional, defaults to 50). }} } \value{ From 95604d14de7b35ba7f05a2311e4c4d35ab56c5bb Mon Sep 17 00:00:00 2001 From: borishejblum Date: Tue, 20 May 2025 11:46:53 +0200 Subject: [PATCH 2/2] trying to catch python errors... --- R/zzz.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/zzz.R b/R/zzz.R index 287cb1f..e203dad 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -3,10 +3,19 @@ reticulate::py_require("scikit-learn") reticulate::py_require("scipy") reticulate::py_require("pandas") + reticulate::py_require("deflate") reticulate::py_require("rpy2") reticulate::py_require("matplotlib") reticulate::py_require("lightgbm") reticulate::py_require("seaborn") reticulate::py_require("hyperopt") reticulate::py_require("reservoirpy") + + # rpy2_import <- try(reticulate::import("rpy2"), silent = TRUE) + # if(inherits(rpy2_import, "try-error")){ + # stop("Python module `rpy2` is missing. ", + # "Try installing it by running the following R command in the console:\n", + # "reticulate::py_install('rpy2')") + # } + }