Skip to content

Commit 874ffc7

Browse files
Merge branch 'PredictiveEcology:development' into development
2 parents 4d1b474 + 47dbc54 commit 874ffc7

File tree

5 files changed

+229
-2
lines changed

5 files changed

+229
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Type: Package
33
Title: Utilities for Working With the 'fireSense' Group of 'SpaDES' Modules
44
Description: Utilities for working with the 'fireSense' group of 'SpaDES' modules.
55
Date: 2026-02-18
6-
Version: 0.0.10
6+
Version: 0.0.12
77
Authors@R: c(
88
person("Jean", "Marchal", email = "jean.d.marchal@gmail.com",
99
role = c("aut")),

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export(removeBufferedFiresOutsideRTM)
7474
export(rescaleKnown2)
7575
export(rescaleVarsByMagnitude)
7676
export(runDEoptim)
77+
export(runELFs)
7778
export(spreadFitAdditionalColNames)
7879
export(spreadProbFromIntegerCovs)
7980
export(stackAndExtract)
@@ -98,6 +99,8 @@ importFrom(RhpcBLASctl,omp_set_num_threads)
9899
importFrom(SpaDES.core,P)
99100
importFrom(SpaDES.core,Plots)
100101
importFrom(SpaDES.core,anyPlotting)
102+
importFrom(SpaDES.core,simInitAndSpades2)
103+
importFrom(SpaDES.project,user)
101104
importFrom(SpaDES.tools,distanceFromEachPoint)
102105
importFrom(SpaDES.tools,rasterizeReduced)
103106
importFrom(SpaDES.tools,spread)
@@ -142,6 +145,7 @@ importFrom(ggplot2,ggtitle)
142145
importFrom(ggplot2,labs)
143146
importFrom(ggplot2,theme_minimal)
144147
importFrom(ggplot2,xlab)
148+
importFrom(googledrive,drive_update)
145149
importFrom(grDevices,dev.off)
146150
importFrom(grDevices,png)
147151
importFrom(graphics,abline)

R/ELFs.R

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,125 @@ moveSliversToOtherELFs <- function(lostPixels, lp, ca, i, r) {
514514
list(ca = ca, r = r)
515515
}
516516

517+
518+
519+
#' Run ELFs modules and optionally update googledrive png
520+
#'
521+
#' Extracting only the "ELF" module, runs the projectmodule
522+
#' via \code{SpaDES.core::simInitAndSpades2()} with caching (optionally
523+
#' updates a Google Drive file if the current user is \code{"emcintir"}), and
524+
#' returns the vector of polygon IDs from \code{spreadFitPreRun}.
525+
#'
526+
#' @details
527+
#' The function:
528+
#' \enumerate{
529+
#' \item Restricts \code{preRunSetupProject$modules} to entries matching \code{"ELFs"}.
530+
#' \item Collects all \code{.R} source files under each module directory (recursively),
531+
#' excluding any under \code{tests/}, and uses these as part of the cache key.
532+
#' \item Retrieves remote metadata (hash) from a Google Drive file URL and assigns
533+
#' the remote hash to \code{preRunSetupProject$params$fireSense_ELFs$hashSpreadFitRemoteFile}.
534+
#' \item Initializes and runs the simulation with \code{SpaDES.core::simInitAndSpades2()},
535+
#' wrapping the call in \code{Cache()} so that the run is skipped unless inputs
536+
#' (source files and remote hash) have changed.
537+
#' \item If the current \code{SpaDES.project::user()} is \code{"emcintir"}, updates
538+
#' the Google Drive file at the provided URL with any output files whose names
539+
#' contain \code{"ELF"}, caching the update by \code{cacheId(sim)}.
540+
#' \item Returns \code{sim$spreadFitPreRun$polygonID}.
541+
#' }
542+
#'
543+
#' Internal `Cache`ing relies on two extra keys:
544+
#' \itemize{
545+
#' \item \code{src}: paths to module \code{.R} files (excluding \code{tests/}).
546+
#' \item \code{remoteHash}: the remote metadata hash obtained from the URL.
547+
#' }
548+
#'
549+
#' The function expects \code{preRunSetupProject} to be a SpaDES project list with at least:
550+
#' \itemize{
551+
#' \item \code{$modules}: character vector of module names.
552+
#' \item \code{$paths$modulePath}: base directory containing module folders.
553+
#' \item \code{$params$fireSense_ELFs}: a list where \code{hashSpreadFitRemoteFile} can be set.
554+
#' }
555+
#'
556+
#' @param preRunSetupProject list. A SpaDES project object prepared for
557+
#' \code{SpaDES.core::simInitAndSpades2()}, containing \code{$modules},
558+
#' \code{$paths$modulePath}, and \code{$params$fireSense_ELFs}.
559+
#' @param urlELFresults A googledrive url where the ELF data.frame that contains
560+
#' geometries and fitted SpreadFit parameters and several other
561+
#' attributes.
562+
#' @param onlyFittedELFs logical. If `TRUE`, the default, then this will only
563+
#' return the ELF indices that have been successfully fitted via `fireSense_SpreadFit`.
564+
#' Whether individual ELFs have been fitted or not will be determined based on
565+
#' downloading the `urlELFresults`.
566+
#'
567+
#' @return
568+
#' A vector corresponding to \code{sim$spreadFitPreRun$polygonID}. Currently, no high arctic
569+
#' ELFs are returned (e.g., Ecoprovinces starting with either 1. or 2.)
570+
#'
571+
#' @section Side Effects:
572+
#' Updates a Google Drive file when the current user is \code{"emcintir"} and when
573+
#' the run produces outputs with names containing \code{"ELF"}.
574+
#'
575+
#' @section Notes:
576+
#' \itemize{
577+
#' \item The remote file URL is hard-coded as:
578+
#' \code{"https://drive.google.com/file/d/1tkC944mPzR9-y-qCMDB5o2cAR_1MoDz4/view?usp=drive_link"}.
579+
#' \item \code{Cache()} and \code{cacheId()} are assumed to be available (typically from
580+
#' \pkg{reproducible} in SpaDES workflows).
581+
#' \item \code{asPath()} is expected to be available in your environment (commonly from
582+
#' \pkg{reproducible}); adjust if your project defines it elsewhere.
583+
#' }
584+
#'
585+
#' @examples
586+
#' \dontrun{
587+
#' # Assuming 'prj' is a valid SpaDES project list:
588+
#' # prj <- somePreparationFunction(...)
589+
#' ids <- runELFs(prj)
590+
#' }
591+
#'
592+
#' @importFrom SpaDES.core simInitAndSpades2
593+
#' @importFrom reproducible Cache
594+
#' @importFrom googledrive drive_update
595+
#' @importFrom SpaDES.project user
596+
#' @export
597+
#'
598+
#' @seealso
599+
#' \code{\link[SpaDES.core]{simInitAndSpades2}},
600+
#' \code{\link[reproducible]{Cache}},
601+
#' \code{\link[googledrive]{drive_update}}
602+
#'
603+
#' @author
604+
#' Based on the provided code snippet.
605+
#'
606+
runELFs <- function(preRunSetupProject, onlyFittedELFs = TRUE,
607+
urlELFresults = "https://drive.google.com/file/d/1tkC944mPzR9-y-qCMDB5o2cAR_1MoDz4/view?usp=drive_link") {
608+
preRunSetupProject$modules <- grep("ELFs", preRunSetupProject$modules, value = TRUE)
609+
# For digesting; i.e., whether it needs to be re-run
610+
srcFiles <- asPath(dir(file.path(preRunSetupProject$paths$modulePath, preRunSetupProject$modules),
611+
pattern = "\\.R$", recursive = TRUE, full.names = TRUE) |>
612+
grep(pattern = "tests\\/", invert = TRUE, value = TRUE))
613+
md <- reproducible:::getRemoteMetadata(url = urlELFresults)
614+
preRunSetupProject$params$fireSense_ELFs$hashSpreadFitRemoteFile <- md$remoteHas
615+
616+
# Run the module
617+
sim <- SpaDES.core::simInitAndSpades2(preRunSetupProject) |>
618+
Cache(.cacheExtra = list(src = srcFiles, remoteHash = md$remoteHash),
619+
omitArgs = "l")
620+
621+
# Upload if it is emcintir; and if it has changed
622+
if (SpaDES.project::user() %in% "emcintir") {
623+
cid <- cacheId(sim)
624+
ll <- googledrive::drive_update(file = urlELFresults,
625+
media = grep("ELF", sim@outputs$file, value = TRUE)) |>
626+
Cache(omitArgs = c("file", "media"), .cacheExtra = list(cacheId = cid))
627+
}
628+
# })
629+
if (isTRUE(onlyFittedELFs)) {
630+
.ELFinds <- sim$spreadFitPreRun$polygonID
631+
} else {
632+
.ELFinds <- names(sim$ELFs$rasCentered)
633+
}
634+
#remove Arctic that is far from treeline
635+
.ELFinds <- grep("^1\\.|^2\\.", invert = TRUE, value = TRUE, .ELFinds)
636+
637+
.ELFinds
638+
}

man/runDEoptim.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/runELFs.Rd

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)