From 66e1a545735ee57c5d0fb4640e36a279066b1265 Mon Sep 17 00:00:00 2001 From: rfriedman22 Date: Thu, 3 Sep 2026 09:27:10 -0700 Subject: [PATCH 1/3] Fix crash issues from glasso --- R/PLNfamily-class.R | 30 ++++++++++++++++++++---------- R/PLNnetworkfamily-class.R | 25 +++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/R/PLNfamily-class.R b/R/PLNfamily-class.R index 1c043573..e5e49be4 100644 --- a/R/PLNfamily-class.R +++ b/R/PLNfamily-class.R @@ -72,16 +72,26 @@ PLNfamily <- } else { nullModel <- NULL } - for (model in self$models) - model$postTreatment( - self$responses, - self$covariates, - self$offsets, - self$weights, - config_post=config_post, - config_optim=config_optim, - nullModel = nullModel - ) + for (i in seq_along(self$models)) { + model <- self$models[[i]] + tryCatch( + { + model$postTreatment( + self$responses, + self$covariates, + self$offsets, + self$weights, + config_post = config_post, + config_optim = config_optim, + nullModel = nullModel + ) + }, + error = function(e) { + warning(paste("Post-treatment failed for model", i, ":", e$message, "\nTruncating model family to models 1 to", i - 1)) + self$models <- self$models[1:(i - 1)] + } + ) + } }, ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/R/PLNnetworkfamily-class.R b/R/PLNnetworkfamily-class.R index 44687aec..3ea537a4 100644 --- a/R/PLNnetworkfamily-class.R +++ b/R/PLNnetworkfamily-class.R @@ -119,6 +119,16 @@ Networkfamily <- R6Class( }, + ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ## Post treatment -------------------- + #' @description Update fields after optimization + #' @param config_post a list for controlling the post-treatments (optional bootstrap, jackknife, R2, etc.). + #' @param config_optim a list for controlling the optimization parameters used during post_treatments + postTreatment = function(config_post, config_optim) { + super$postTreatment(config_post, config_optim) + private$params <- self$penalties[seq_along(self$models)] + }, + ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ## Extractors ------------------------ #' @description Extract the regularization path of a [`Networkfamily`] @@ -432,7 +442,13 @@ PLNnetworkfamily <- R6Class( myPLN <- PLNnetworkfamily$new(self$penalties, data, control) myPLN$optimize(data, control$config_optim) nets <- do.call(cbind, lapply(myPLN$models, function(model) { - as.matrix(model$latent_network("support"))[upper.tri(diag(private$p))] + # If Omega is null, glasso diverged on the first iteration, so the network is completely unstable + if (is.null(model$model_par$Omega)) { + support <- matrix(0, nrow = private$p, ncol = private$p) + } else { + support <- as.matrix(model$latent_network("support")) + } + support[upper.tri(diag(private$p))] })) nets }, mc.cores = getOption("mc.cores", 1L)) @@ -595,7 +611,12 @@ ZIPLNnetworkfamily <- R6Class( myPLN$optimize(data, control$config_optim) nets <- do.call(cbind, lapply(myPLN$models, function(model) { - as.matrix(model$latent_network("support"))[upper.tri(diag(private$p))] + if (is.null(model$model_par$Omega)) { + support <- matrix(0, nrow = private$p, ncol = private$p) + } else { + support <- as.matrix(model$latent_network("support")) + } + support[upper.tri(diag(private$p))] })) nets }, mc.cores = getOption("mc.cores", 1L)) From aba988d783d87f9d39dcb4fccf0b9255be3b0ef1 Mon Sep 17 00:00:00 2001 From: rfriedman22 Date: Fri, 4 Sep 2026 11:23:16 -0700 Subject: [PATCH 2/3] Forgot to break the post-treatment loop after catching error --- R/PLNfamily-class.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/PLNfamily-class.R b/R/PLNfamily-class.R index e5e49be4..34cf2055 100644 --- a/R/PLNfamily-class.R +++ b/R/PLNfamily-class.R @@ -89,6 +89,7 @@ PLNfamily <- error = function(e) { warning(paste("Post-treatment failed for model", i, ":", e$message, "\nTruncating model family to models 1 to", i - 1)) self$models <- self$models[1:(i - 1)] + break } ) } From dcd44f8742b1b555cfb01785b32c52d2a85ca45f Mon Sep 17 00:00:00 2001 From: rfriedman22 Date: Fri, 4 Sep 2026 12:01:16 -0700 Subject: [PATCH 3/3] The break was misplaced, better way to handle the error is to wrap the entire loop in tryCatch, rather than each post-treatment --- R/PLNfamily-class.R | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/R/PLNfamily-class.R b/R/PLNfamily-class.R index 34cf2055..13df89b9 100644 --- a/R/PLNfamily-class.R +++ b/R/PLNfamily-class.R @@ -72,27 +72,24 @@ PLNfamily <- } else { nullModel <- NULL } - for (i in seq_along(self$models)) { - model <- self$models[[i]] - tryCatch( - { - model$postTreatment( - self$responses, - self$covariates, - self$offsets, - self$weights, - config_post = config_post, - config_optim = config_optim, - nullModel = nullModel - ) - }, - error = function(e) { - warning(paste("Post-treatment failed for model", i, ":", e$message, "\nTruncating model family to models 1 to", i - 1)) - self$models <- self$models[1:(i - 1)] - break - } - ) - } + tryCatch({ + for (i in seq_along(self$models)) { + model <- self$models[[i]] + model$postTreatment( + self$responses, + self$covariates, + self$offsets, + self$weights, + config_post = config_post, + config_optim = config_optim, + nullModel = nullModel + ) + } + }, + error = function(e) { + warning(paste("Post-treatment failed for model", i, ":", e$message, "\nTruncating model family to models 1 to", i - 1)) + self$models <- self$models[1:(i - 1)] + }) }, ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%