Skip to content

Commit 44d5b1d

Browse files
committed
seeds fix
1 parent f1f35fb commit 44d5b1d

12 files changed

Lines changed: 167 additions & 182 deletions

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Depends: R (>= 4.1.0)
2222
Encoding: UTF-8
2323
License: MIT + file LICENSE
2424
Roxygen: list(markdown = TRUE)
25-
RoxygenNote: 7.3.3
2625
LazyData: true
2726
LazyDataCompression: xz
2827
Config/testthat/edition: 3
@@ -40,7 +39,6 @@ Imports:
4039
FARDEEP,
4140
ComICS,
4241
lme4,
43-
withr,
4442
e1071,
4543
preprocessCore,
4644
parallel
@@ -50,3 +48,4 @@ Suggests:
5048
testthat (>= 3.0.0)
5149
URL: https://github.com/kalidouBA/dicepro
5250
BugReports: https://github.com/kalidouBA/dicepro/issues
51+
Config/roxygen2/version: 8.0.0

R/dicepro.R

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
)
155155

156156
if (methodDeconv == "CSx" &&
157-
(is.null(cibersortx_email) || is.null(cibersortx_token)))
157+
(is.null(cibersortx_email) || is.null(cibersortx_token)))
158158
stop("CIBERSORTx credentials required for CSx.", call. = FALSE)
159159

160160
out_Decon <- running_method(
@@ -229,8 +229,7 @@
229229
best_hyperParams(
230230
trials_df = res$trials,
231231
W = res$W,
232-
H = res$H,
233-
savePaths = output_dir
232+
H = res$H
234233
)
235234
}
236235

@@ -321,9 +320,9 @@
321320
#' (samples × cell types).
322321
#' @param normalize Logical. Apply z-score normalisation per gene.
323322
#' @param seed Integer. Random seed used for full pipeline reproducibility.
324-
#' Defaults to \code{42L}. Ensures deterministic behaviour of the
323+
#' Defaults to \code{NULL}. If set, ensures deterministic behaviour of the
325324
#' hyperparameter optimisation and downstream stochastic components.
326-
#' Set to \code{NULL} if you explicitly want non-reproducible runs.
325+
#' Set to \code{NULL} if you explicitly want random (non-reproducible) runs.
327326
#'
328327
#' @return An object of class \code{"dicepro"} (a named list) containing:
329328
#' \itemize{
@@ -386,7 +385,6 @@ dicepro <- function(reference,
386385
out_Decon = NULL,
387386
normalize = TRUE,
388387
seed = NULL) {
389-
390388
# ---- Validation ----------------------------------------------------------
391389
args <- .validate_inputs(normalize, algo_select, hspaceTechniqueChoose)
392390
normalize <- args$normalize
@@ -423,8 +421,6 @@ dicepro <- function(reference,
423421
restrictionEspace = c("gamma", "lambda_factor", "p_prime")
424422
)
425423

426-
seed <- seed %||% 42L
427-
428424
# ---- Hyperparameter optimisation -----------------------------------------
429425
out <- .run_hyperopt(
430426
dataset = dataset,

R/hypersearch.R

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ contains_nan_or_inf <- function(value) {
8787
#' If \code{NULL}, defaults to a matrix of \code{0.1}.
8888
#' @param lambda_ Numeric. Regularization parameter \eqn{\lambda}.
8989
#' @param gamma_par Numeric. Regularization parameter \eqn{\gamma}.
90-
#' @param path2save Character. Path passed to \code{nmf_lbfgsb}.
9190
#'
9291
#' @return List output from \code{\link{nmf_lbfgsb}} with \code{H} and
9392
#' \code{W} cleaned by \code{.clean_nmf_matrix}.
@@ -97,8 +96,7 @@ nmf_lbfgsb_hyperOpt <- function(dataset,
9796
W_prime = NULL,
9897
p_prime = NULL,
9998
lambda_ = 10,
100-
gamma_par = 100,
101-
path2save = "") {
99+
gamma_par = 100) {
102100

103101
B <- as.data.frame(dataset$B)
104102
W_cb <- as.data.frame(dataset$W)
@@ -134,7 +132,6 @@ nmf_lbfgsb_hyperOpt <- function(dataset,
134132
p_prime = p_prime,
135133
lambda_ = lambda_,
136134
gamma_par = gamma_par,
137-
path2save = path2save,
138135
N_unknownCT = N_unknownCT
139136
)
140137

@@ -191,8 +188,7 @@ objective_opt <- function(dataset,
191188
W_prime = W_prime,
192189
p_prime = p_prime,
193190
lambda_ = lambda_,
194-
gamma_par = gamma,
195-
path2save = exp_dir
191+
gamma_par = gamma
196192
)
197193

198194
# --- Coerce result components to plain scalars/matrices -------------------
@@ -219,11 +215,11 @@ objective_opt <- function(dataset,
219215

220216
# --- Guard: discard trials with non-finite values -------------------------
221217
if (contains_nan_or_inf(.sc(result_dict$objectiveValue)) ||
222-
contains_nan_or_inf(.sc(result_dict$constraint)) ||
223-
any(is.na(c(
224-
.sc(result_dict$frob_H), .sc(result_dict$var_H),
225-
.sc(result_dict$frob_W), .sc(result_dict$var_W)
226-
)))) {
218+
contains_nan_or_inf(.sc(result_dict$constraint)) ||
219+
any(is.na(c(
220+
.sc(result_dict$frob_H), .sc(result_dict$var_H),
221+
.sc(result_dict$frob_W), .sc(result_dict$var_W)
222+
)))) {
227223
return(NULL)
228224
}
229225

@@ -311,7 +307,7 @@ objective_wrapper <- function(objective_opt, dataset, config, params,
311307
)
312308

313309
if (is.null(returned_dict) ||
314-
(!is.null(returned_dict$status) && returned_dict$status != "OK")) {
310+
(!is.null(returned_dict$status) && returned_dict$status != "OK")) {
315311
return(NULL)
316312
}
317313

@@ -406,9 +402,9 @@ objective_wrapper <- function(objective_opt, dataset, config, params,
406402
# Normalise every spec once, delegating entirely to the shared parser.
407403
space <- stats::setNames(
408404
mapply(.parse_hyperopt_searchspace,
409-
arg = space_names,
410-
specs = space,
411-
SIMPLIFY = FALSE),
405+
arg = space_names,
406+
specs = space,
407+
SIMPLIFY = FALSE),
412408
space_names
413409
)
414410

@@ -420,29 +416,29 @@ objective_wrapper <- function(objective_opt, dataset, config, params,
420416
for (param_name in space_names) {
421417
spec <- space[[param_name]]
422418
params[[param_name]] <- switch(spec$type,
423-
choice = sample(spec$choices, 1L)[[1L]],
424-
randint = sample(seq.int(spec$low, spec$high), 1L),
425-
uniform = stats::runif(1L, spec$low, spec$high),
426-
quniform = {
427-
v <- stats::runif(1L, spec$low, spec$high)
428-
round(v / spec$q) * spec$q
429-
},
430-
loguniform = exp(stats::runif(1L, log(spec$low), log(spec$high))),
431-
qloguniform = {
432-
v <- exp(stats::runif(1L, log(spec$low), log(spec$high)))
433-
round(v / spec$q) * spec$q
434-
},
435-
normal = stats::rnorm(1L, spec$mu, spec$sigma),
436-
qnormal = {
437-
v <- stats::rnorm(1L, spec$mu, spec$sigma)
438-
round(v / spec$q) * spec$q
439-
},
440-
lognormal = stats::rlnorm(1L, spec$mu, spec$sigma),
441-
qlognormal = {
442-
v <- stats::rlnorm(1L, spec$mu, spec$sigma)
443-
round(v / spec$q) * spec$q
444-
},
445-
stop(paste("Unknown search space type:", spec$type))
419+
choice = sample(spec$choices, 1L)[[1L]],
420+
randint = sample(seq.int(spec$low, spec$high), 1L),
421+
uniform = stats::runif(1L, spec$low, spec$high),
422+
quniform = {
423+
v <- stats::runif(1L, spec$low, spec$high)
424+
round(v / spec$q) * spec$q
425+
},
426+
loguniform = exp(stats::runif(1L, log(spec$low), log(spec$high))),
427+
qloguniform = {
428+
v <- exp(stats::runif(1L, log(spec$low), log(spec$high)))
429+
round(v / spec$q) * spec$q
430+
},
431+
normal = stats::rnorm(1L, spec$mu, spec$sigma),
432+
qnormal = {
433+
v <- stats::rnorm(1L, spec$mu, spec$sigma)
434+
round(v / spec$q) * spec$q
435+
},
436+
lognormal = stats::rlnorm(1L, spec$mu, spec$sigma),
437+
qlognormal = {
438+
v <- stats::rlnorm(1L, spec$mu, spec$sigma)
439+
round(v / spec$q) * spec$q
440+
},
441+
stop(paste("Unknown search space type:", spec$type))
446442
)
447443
}
448444

@@ -480,7 +476,7 @@ objective_wrapper <- function(objective_opt, dataset, config, params,
480476

481477
warning(sprintf(
482478
paste0(".sample_from_space: no candidate satisfying gamma > %.4g * lambda_ ",
483-
"found in %d attempts. The last draw is returned as-is."),
479+
"found in %d attempts. The last draw is returned as-is."),
484480
gamma_ratio_min, max_tries
485481
), call. = FALSE)
486482

@@ -557,7 +553,7 @@ research_hyperOpt <- function(objective_opt,
557553

558554
config <- .parse_config(config)
559555

560-
if(!is.null(seed)){
556+
if (!is.null(seed)) {
561557
set.seed(seed)
562558
}
563559

@@ -594,10 +590,10 @@ research_hyperOpt <- function(objective_opt,
594590

595591
params <- if (use_tpe) {
596592
.tpe_sample(search_space, tpe_history,
597-
gamma_ratio_min = gamma_ratio_min)
593+
gamma_ratio_min = gamma_ratio_min)
598594
} else {
599595
.sample_from_space(search_space,
600-
gamma_ratio_min = gamma_ratio_min)
596+
gamma_ratio_min = gamma_ratio_min)
601597
}
602598

603599
res <- tryCatch(
@@ -683,7 +679,7 @@ research_hyperOpt <- function(objective_opt,
683679
candidates <- lapply(
684680
seq_len(n_candidates),
685681
function(.) .sample_from_space(search_space,
686-
gamma_ratio_min = gamma_ratio_min)
682+
gamma_ratio_min = gamma_ratio_min)
687683
)
688684

689685
scores <- vapply(candidates, function(cand) {
@@ -694,11 +690,11 @@ research_hyperOpt <- function(objective_opt,
694690
if (is.null(x_val)) next
695691

696692
good_vals <- vapply(history[good_idx],
697-
function(h) h$params[[pname]] %||% NA_real_,
698-
numeric(1L))
693+
function(h) h$params[[pname]] %||% NA_real_,
694+
numeric(1L))
699695
bad_vals <- vapply(history[bad_idx],
700-
function(h) h$params[[pname]] %||% NA_real_,
701-
numeric(1L))
696+
function(h) h$params[[pname]] %||% NA_real_,
697+
numeric(1L))
702698

703699
good_vals <- good_vals[!is.na(good_vals)]
704700
bad_vals <- bad_vals[!is.na(bad_vals)]
@@ -768,22 +764,22 @@ research_hyperOpt <- function(objective_opt,
768764
for (arg in required_args) {
769765
if (is.null(config[[arg]]))
770766
stop(paste("No", arg, "argument found in configuration file."),
771-
call. = FALSE)
767+
call. = FALSE)
772768
}
773769

774770
valid_methods <- c("tpe", "random", "atpe", "anneal")
775771
if (!config$hp_method %in% valid_methods)
776772
stop(
777773
paste("Unknown hyperopt algorithm:", config$hp_method,
778-
"-- valid options:", paste(valid_methods, collapse = ", ")),
774+
"-- valid options:", paste(valid_methods, collapse = ", ")),
779775
call. = FALSE
780776
)
781777

782778
# Validation of the optional ratio (all_gamma_dominant mode)
783779
if (!is.null(config$gamma_ratio_min)) {
784780
if (!is.numeric(config$gamma_ratio_min) ||
785-
length(config$gamma_ratio_min) != 1L ||
786-
config$gamma_ratio_min <= 0) {
781+
length(config$gamma_ratio_min) != 1L ||
782+
config$gamma_ratio_min <= 0) {
787783
stop("'gamma_ratio_min' must be a single positive numeric.", call. = FALSE)
788784
}
789785
}
@@ -844,42 +840,42 @@ research_hyperOpt <- function(objective_opt,
844840
.n <- as.numeric
845841

846842
switch(type,
847-
choice = list(type = "choice",
848-
choices = specs[-1L]),
849-
randint = list(type = "randint",
850-
low = as.integer(specs[[2L]]),
851-
high = as.integer(specs[[3L]])),
852-
uniform = list(type = "uniform",
853-
low = .n(specs[[2L]]),
854-
high = .n(specs[[3L]])),
855-
quniform = list(type = "quniform",
856-
low = .n(specs[[2L]]),
857-
high = .n(specs[[3L]]),
858-
q = .n(specs[[4L]])),
859-
loguniform = list(type = "loguniform",
860-
low = .n(specs[[2L]]),
861-
high = .n(specs[[3L]])),
862-
qloguniform = list(type = "qloguniform",
863-
low = .n(specs[[2L]]),
864-
high = .n(specs[[3L]]),
865-
q = .n(specs[[4L]])),
866-
normal = list(type = "normal",
867-
mu = .n(specs[[2L]]),
868-
sigma = .n(specs[[3L]])),
869-
qnormal = list(type = "qnormal",
870-
mu = .n(specs[[2L]]),
871-
sigma = .n(specs[[3L]]),
872-
q = .n(specs[[4L]])),
873-
lognormal = list(type = "lognormal",
874-
mu = .n(specs[[2L]]),
875-
sigma = .n(specs[[3L]])),
876-
qlognormal = list(type = "qlognormal",
877-
mu = .n(specs[[2L]]),
878-
sigma = .n(specs[[3L]]),
879-
q = .n(specs[[4L]])),
880-
stop(sprintf(
881-
"Unknown search space type '%s' for parameter '%s'.", type, arg
882-
), call. = FALSE)
843+
choice = list(type = "choice",
844+
choices = specs[-1L]),
845+
randint = list(type = "randint",
846+
low = as.integer(specs[[2L]]),
847+
high = as.integer(specs[[3L]])),
848+
uniform = list(type = "uniform",
849+
low = .n(specs[[2L]]),
850+
high = .n(specs[[3L]])),
851+
quniform = list(type = "quniform",
852+
low = .n(specs[[2L]]),
853+
high = .n(specs[[3L]]),
854+
q = .n(specs[[4L]])),
855+
loguniform = list(type = "loguniform",
856+
low = .n(specs[[2L]]),
857+
high = .n(specs[[3L]])),
858+
qloguniform = list(type = "qloguniform",
859+
low = .n(specs[[2L]]),
860+
high = .n(specs[[3L]]),
861+
q = .n(specs[[4L]])),
862+
normal = list(type = "normal",
863+
mu = .n(specs[[2L]]),
864+
sigma = .n(specs[[3L]])),
865+
qnormal = list(type = "qnormal",
866+
mu = .n(specs[[2L]]),
867+
sigma = .n(specs[[3L]]),
868+
q = .n(specs[[4L]])),
869+
lognormal = list(type = "lognormal",
870+
mu = .n(specs[[2L]]),
871+
sigma = .n(specs[[3L]])),
872+
qlognormal = list(type = "qlognormal",
873+
mu = .n(specs[[2L]]),
874+
sigma = .n(specs[[3L]]),
875+
q = .n(specs[[4L]])),
876+
stop(sprintf(
877+
"Unknown search space type '%s' for parameter '%s'.", type, arg
878+
), call. = FALSE)
883879
)
884880
}
885881

0 commit comments

Comments
 (0)