-
Notifications
You must be signed in to change notification settings - Fork 11
fix: trying to fix loading results from the old pmetrics (not to be merged) #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,10 @@ PM_result <- R6::R6Class( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' Use the `$save` method on the augmented `PM_result` object to save it with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' new optimal sampling results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opt_samp = NULL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @field NPdata List of NPAG run information | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NPdata = NULL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @field ITdata List of IT2B run information | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ITdata = NULL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' Create new object populated with data from previous run | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -64,21 +68,56 @@ PM_result <- R6::R6Class( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param path include `r template("path")`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param quiet Quietly validate. Default is `FALSE`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialize = function(out, path = ".", quiet = TRUE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(out$NPdata)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self$NPdata <- out$NPdata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(self$NPdata) <- c("NPAG", "list") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allData <- "NPdata" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self$NPdata <- NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(out$ITdata)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self$ITdata <- out$ITdata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(self$ITdata) <- c("IT2B", "list") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allData <- "ITdata" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self$ITdata <- NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (is.null(out$NPdata) & is.null(out$ITdata)) { # neither present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(out$op)) { # check if it is a rust object (op is always present) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!inherits(out$op, "R6")) { # it is a rust object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self$NPdata <- out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(self$NPdata) <- c("NPAG", "rust", "list") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allData <- "NPdata" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # the following were saved as R6 objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| purrr::walk( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c("pop", "post", "final", "cycle", "op", "cov", "data", "model", "valid"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| \(x){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self[[x]] <- NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(out[[x]])) { # if the object is loaded... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!inherits(out[[x]], "R6")) { # older save | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli::cli_abort(c("x" = "The object was saved in an older format. Please re-run the analysis.")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inherits(out[[x]], paste0("PM", x)) || inherits(out[[x]], "PMmatrix")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self[[x]] <- get(paste0("PM_", x))$new(out[[x]]) # ...make the R6 from old PMxxx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self[[x]] <- get(paste0("PM_", x))$new(out[[allData]]) # ...make the R6 from raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
102
to
+107
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(x == "model"){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+103
to
109
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inherits(out[[x]], paste0("PM", x)) || inherits(out[[x]], "PMmatrix")) { | |
| self[[x]] <- get(paste0("PM_", x))$new(out[[x]]) # ...make the R6 from old PMxxx | |
| } else { | |
| self[[x]] <- get(paste0("PM_", x))$new(out[[allData]]) # ...make the R6 from raw | |
| } | |
| } else { | |
| if(x == "model"){ | |
| if (x == "model") { | |
| # For legacy saves, upgrade the model from out$model itself. | |
| tryCatch({ | |
| args <- list(x = out[[x]], compile = FALSE) | |
| self[[x]] <- do.call(get(paste0("PM_", x))$new, args = args) | |
| }, error = function(e) { | |
| cli::cli_abort(c( | |
| "!" = "Failed to upgrade legacy {.field model} object from saved results.", | |
| "i" = "The saved 'model' field is not in a supported legacy format.", | |
| "x" = conditionMessage(e) | |
| )) | |
| }) | |
| } else if (inherits(out[[x]], paste0("PM", x)) || inherits(out[[x]], "PMmatrix")) { | |
| self[[x]] <- get(paste0("PM_", x))$new(out[[x]]) # ...make the R6 from old PMxxx | |
| } else { | |
| self[[x]] <- get(paste0("PM_", x))$new(out[[allData]]) # ...make the R6 from raw | |
| } | |
| } else { | |
| if (x == "model") { |
Copilot
AI
Mar 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
load() can return multiple object names from an .Rdata file; get(obj_names, envir = env) will error if obj_names has length > 1. Since this PR targets loading older Pmetrics outputs (which may plausibly save multiple objects), add handling for multiple loaded objects (e.g., select the expected one by name, or use mget() and validate) and emit a clear error message if the file format is unsupported.
| result <- output2List(Out = get(obj_names, envir = env)) | |
| if (length(obj_names) == 0L) { | |
| cli::cli_abort(c( | |
| "x" = "The file {.path {found}} did not contain any objects.", | |
| "i" = "This does not appear to be a valid Pmetrics output file." | |
| )) | |
| } | |
| if (length(obj_names) == 1L) { | |
| Out <- get(obj_names, envir = env) | |
| } else { | |
| # Multiple objects were loaded; try to select the expected one | |
| objs <- mget(obj_names, envir = env, inherits = FALSE) | |
| if ("Out" %in% obj_names && is.list(objs[["Out"]])) { | |
| Out <- objs[["Out"]] | |
| } else { | |
| cli::cli_abort(c( | |
| "x" = "The file {.path {found}} contains multiple objects and the expected Pmetrics output object could not be identified.", | |
| "i" = "Objects found: {.val {obj_names}}.", | |
| "i" = "This file format is not supported by {.fn PM_load}." | |
| )) | |
| } | |
| } | |
| result <- output2List(Out = Out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If both
out$NPdataandout$ITdataare present,allDatawill be overwritten by the second block and the upgrade path will use whichever one ran last. Since a run should be either NPAG or IT2B, consider detecting the “both present” case and failing fast (or selecting deterministically based on an explicit field) to avoid silently rebuilding objects from the wrong raw data.