Skip to content

registered output filenames get renamed at the end of the simulation #319

@achubaty

Description

@achubaty

reprex modified from the existing test:

test_that("registerOutputs", {
  testInit()

  newModule("test", tmpdir, open = FALSE)
  cat(file = file.path(tmpdir, "test", "test.R"),'
      defineModule(sim, list(
      name = "test",
      description = "insert module description here",
      keywords = c("insert key words here"),
      authors = person(c("Eliot", "J", "B"), "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre")),
      childModules = character(0),
      version = list(SpaDES.core = "0.1.0", test = "0.0.1"),
      spatialExtent = terra::ext(rep(0, 4)),
      timeframe = as.POSIXlt(c(NA, NA)),
      timeunit = "year",
      citation = list("citation.bib"),
      documentation = list("README.md", "test.Rmd"),
      reqdPkgs = list(),
      parameters = rbind(
        defineParameter(".useCache", "character", ".inputObjects", NA, NA, "")
      ),
      inputObjects = bindrows(
        expectsInput("age", "numeric", ""),
        expectsInput("ageMap", "numeric", ""),
        expectsInput("worked", "numeric", ""),
        expectsInput("age2", "numeric", "") # need a dummy one that isn not supplied in simInit below
      ),
      outputObjects = bindrows(
      )
      ))

      doEvent.test = function(sim, eventTime, eventType, debug = FALSE) {
      switch(
      eventType,
      init = {

      tf <- tempfile2(tempdir = outputPath(sim), fileext = ".rds")
      tf2 <- tempfile2(tempdir = outputPath(sim), fileext = ".rds")
      tf3 <- tempfile2(tempdir = outputPath(sim), fileext = ".rds")

      sim <- saveRDS(sim$age, file = tf) |> registerOutputs(filename = tf)
      sim <- registerOutputs(filename = saveRDS(sim$age, file = tf2))
      sim <- saveRDS(sim$age, file = tf3) |> registerOutputs()

      sim <- scheduleEvent(sim, time(sim) + 1, "test", "event1")
      },
      event1 = {
      tf4 <- tempfile2(tempdir = outputPath(sim), fileext = ".qs2")
      sim <- qs2::qs_save(sim$age, file = tf4) |> registerOutputs()

      tf5 <- tempfile2(tempdir = outputPath(sim), fileext = ".png")
      png(tf5)
      plot(1:10, runif(10))
      dev.off()
      sim <- registerOutputs(tf5)

      sim <- scheduleEvent(sim, time(sim)+1, "test", "event1")
      })
      return(invisible(sim))
      }

      .inputObjects <- function(sim) {
        if (suppliedElsewhere("ageMap", sim)) {
          sim$worked <- TRUE
        }
        sim
      }
      ', fill = TRUE)
  modules <- "test"
  sim <- simInit(times = list(start = 0, end = 2, timeunit = "year"),
                 modules = modules,
                 objects = list(age = 1, vegMap = 2, studyArea = 3),
                 ## NOTE: added outputs data.frame
                 outputs = data.frame(
                   objectName = "vegMap",
                   saveTime = 2,
                   fun = "saveRDS",
                   package = "base",
                   file = "vegMap.rds",
                   stringsAsFactors = FALSE
                  ),
                 paths = list(modulePath = tmpdir))
  sim <- spades(sim)
  expect_equal(NROW(outputs(sim)$file), 8)
  expect_true(all(file.exists(outputs(sim)$file))) ## FAILS see below
})

Here's the outputs table during module run in the final year:

Browse[1]> outputs(sim)
   objectName saveTime     fun package
       <char>    <num>  <char>  <char>
1:     vegMap        2 saveRDS    base
2:       <NA>        0    <NA>    <NA>
3:       <NA>        0    <NA>    <NA>
4:       <NA>        0    <NA>    <NA>
5:       <NA>        1    <NA>    <NA>
6:       <NA>        1    <NA>    <NA>
7:       <NA>        2    <NA>    <NA>
8:       <NA>        2    <NA>    <NA>
                                                             file  saved arguments
                                                           <char> <lgcl>    <AsIs>
1:                /tmp/RtmpIg9zjV/SpaDES/outputs/vegMap_year2.rds     NA        NA
2: /tmp/RtmpIg9zjV/SpaDES/outputs/Br4oKAyD/file2a492332cb3782.rds   TRUE        NA
3: /tmp/RtmpIg9zjV/SpaDES/outputs/5bio97Lg/file2a49237d10ef7f.rds   TRUE        NA
4: /tmp/RtmpIg9zjV/SpaDES/outputs/ZigjRMcs/file2a492368917739.rds   TRUE        NA
5: /tmp/RtmpIg9zjV/SpaDES/outputs/1XmGXwjh/file2a492350c4d353.qs2   TRUE        NA
6: /tmp/RtmpIg9zjV/SpaDES/outputs/Hqmr2waX/file2a492360d23740.png   TRUE        NA
7: /tmp/RtmpIg9zjV/SpaDES/outputs/bmvtL8Wh/file2a4923271abd95.qs2   TRUE        NA
8: /tmp/RtmpIg9zjV/SpaDES/outputs/zThkPBs8/file2a492352428a21.png   TRUE        NA

And here's the outputs table after simulation wrapup:

>   outputs(sim)
   objectName saveTime     fun package
       <char>    <num>  <char>  <char>
1:     vegMap        2 saveRDS    base
2:       <NA>        0 saveRDS    base
3:       <NA>        0 saveRDS    base
4:       <NA>        0 saveRDS    base
5:       <NA>        1 saveRDS    base
6:       <NA>        1 saveRDS    base
7:       <NA>        2 saveRDS    base
8:       <NA>        2 saveRDS    base
                                                                   file  saved arguments
                                                                 <char> <lgcl>    <AsIs>
1:                      /tmp/RtmpIg9zjV/SpaDES/outputs/vegMap_year2.rds   TRUE        NA
2: /tmp/RtmpIg9zjV/SpaDES/outputs/Br4oKAyD/file2a492332cb3782_year0.rds   TRUE        NA
3: /tmp/RtmpIg9zjV/SpaDES/outputs/5bio97Lg/file2a49237d10ef7f_year0.rds   TRUE        NA
4: /tmp/RtmpIg9zjV/SpaDES/outputs/ZigjRMcs/file2a492368917739_year0.rds   TRUE        NA
5: /tmp/RtmpIg9zjV/SpaDES/outputs/1XmGXwjh/file2a492350c4d353_year1.qs2   TRUE        NA
6: /tmp/RtmpIg9zjV/SpaDES/outputs/Hqmr2waX/file2a492360d23740_year1.png   TRUE        NA
7: /tmp/RtmpIg9zjV/SpaDES/outputs/bmvtL8Wh/file2a4923271abd95_year2.qs2   TRUE        NA
8: /tmp/RtmpIg9zjV/SpaDES/outputs/zThkPBs8/file2a492352428a21_year2.png   TRUE        NA

Each of the filenames had _yearX appended to them, but those filenames are incorrect (they aren't where the files were saved during module execution).

Also note that this behaviour is NOT triggered in the original test, but only when passing an outputs data.frame to the simInit call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions