From de56917f06d209ebda7f3d71c6c8cba20d4b35aa Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 11 Oct 2026 09:14:56 +0000 Subject: [PATCH] fix(PM_cycle): build gamlam cycle index from the cycle column A cycles = 0 run writes a single row with cycle 0, and rep(1:0, ...) gave two values, so creating the cycle object failed and $cycle$status was empty. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GTyUgJEd5uCEmv9qYrv8dC --- R/PM_cycle.R | 3 +-- tests/testthat/test-pm-cycle-posterior.R | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-pm-cycle-posterior.R diff --git a/R/PM_cycle.R b/R/PM_cycle.R index 86217bb80..969e44bb1 100755 --- a/R/PM_cycle.R +++ b/R/PM_cycle.R @@ -236,7 +236,6 @@ PM_cycle <- R6::R6Class( observed_outeq <- sort(unique(normalize_engine_index(op_raw$outeq))) n_out <- max(observed_outeq, na.rm = TRUE) - n_cyc <- max(cycle_data$cycle) # gamlam model_types <- decode_error_model_rows(config$errormodels$models, op_raw$outeq) |> @@ -261,7 +260,7 @@ PM_cycle <- R6::R6Class( values_to = "value", names_to = c("type", "outeq"), names_sep = "\\." ) |> - mutate(cycle = rep(1:n_cyc, each = n_out)) |> + mutate(cycle = rep(raw$cycle, each = n_out)) |> select(cycle, value, outeq) |> arrange(cycle, outeq) |> mutate(outeq = normalize_engine_index(as.numeric(outeq))) |> diff --git a/tests/testthat/test-pm-cycle-posterior.R b/tests/testthat/test-pm-cycle-posterior.R new file mode 100644 index 000000000..2f8fad306 --- /dev/null +++ b/tests/testthat/test-pm-cycle-posterior.R @@ -0,0 +1,21 @@ +library(Pmetrics) + +test_that("a cycles = 0 run creates its cycle object", { + skip_on_cran() + local_exa_tmp_cleanup() + run_root <- withr::local_tempdir(pattern = "pmetrics-cycle0-") + model <- PM_model$new(x = modEx, compile = FALSE) + + suppressMessages(model$fit( + data = dataEx, path = run_root, run = 1, cycles = 2, + overwrite = TRUE, report = "none", quiet = TRUE + )) + run2 <- suppressMessages(model$fit( + data = dataEx, path = run_root, run = 2, cycles = 0, prior = 1, + overwrite = TRUE, report = "none", quiet = TRUE + )) + + expect_length(run2$cycle$status, 1) + expect_equal(run2$cycle$data$objective$cycle, 0) + expect_true(all(run2$cycle$data$gamlam$cycle == 0)) +})