2a. Test FrEDI Package #151
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### https://github.com/actions/upload-artifact | |
### https://github.blog/changelog/2021-11-10-github-actions-input-types-for-manual-workflows/ | |
### https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows | |
### https://github.com/r-lib/actions/tree/v2/setup-r-dependencies | |
### https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution | |
### Single line conditional can use ${{}} formulation. Multi-line conditional does not. | |
### For uploading artifacts: | |
### "path:" is the output path where Pandoc will write the compiled PDF. | |
### Note, this should be the same directory as the input paper.md | |
name: 2a. Test FrEDI Package | |
on: | |
workflow_dispatch: | |
inputs: | |
do_compare: | |
type: choice | |
description: Do you want to compare FrEDI results with those from another branch? | |
required: true | |
options: | |
- no | |
- yes | |
ref_branch: | |
type: string | |
description: To which branch of FrEDI do you want to compare results)? | |
default: main | |
agg_types: | |
type: choice | |
description: Aggregate across impact types? | |
required: true | |
options: | |
- no | |
- yes | |
dow_results: | |
type: choice | |
description: Do you want to generate scenario results for report figures? | |
required: true | |
options: | |
- no | |
- yes | |
dow_totals: | |
type: choice | |
description: Do you want to create a plot summarizing scenario results for all sectors? | |
required: true | |
options: | |
- no | |
- yes | |
dow_appendix: | |
type: choice | |
description: Do you want to create sector-specific appendix figures? | |
required: true | |
options: | |
- no | |
- yes | |
sector: | |
type: string | |
description: Which sectors do you want to run? | |
default: "all" | |
workflow_id: | |
type: string | |
description: Enter the run ID for the workflow from which to retrieve the scenario results | |
default: 1 | |
jobs: | |
compile_data: | |
runs-on: ubuntu-latest | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
name: Load Package Code | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Send input status | |
run: | | |
echo "ref_name = ${{ github.ref_name }}" | |
echo "ref_branch = ${{ inputs.ref_branch }}" | |
echo "do_compare = ${{ inputs.do_compare }}" | |
echo "agg_types = ${{ inputs.agg_types }}" | |
echo "dow_results = ${{ inputs.dow_results }}" | |
echo "dow_totals = ${{ inputs.dow_totals }}" | |
echo "dow_appendix = ${{ inputs.dow_appendix }}" | |
echo "sector = ${{ inputs.sector }}" | |
echo "workflow_id = ${{ inputs.workflow_id }}" | |
- name: Setup R | |
uses: r-lib/actions/setup-r@v2 | |
- name: Setup R package dependencies | |
uses: r-lib/actions/setup-r-dependencies@v2 | |
with: | |
cache: true | |
cache-version: 1 | |
packages: | | |
any::tidyverse | |
any::devtools | |
any::openxlsx | |
any::ggpubr | |
### Download tmp_sysdata.rda from 1. Compile Main FrEDI Data run | |
- name: Download all artifacts | |
if: | | |
inputs.dow_totals == 'true' || | |
inputs.dow_appendix == 'true' | |
id: download-artifact | |
uses: actions/download-artifact@v4 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
run-id: ${{ inputs.workflow_id }} | |
path: . | |
- name: Check artifacts | |
# if: ${{ inputs.dow_totals == 'true' || inputs.dow_appendix == 'true' }} | |
if: | | |
inputs.dow_totals == 'true' || | |
inputs.dow_appendix == 'true' | |
run: | | |
echo "dow_totals = ${{ inputs.dow_totals }}" | |
echo "dow_appendix = ${{ inputs.dow_appendix }}" | |
echo "dow_totals | dow_appendix = ${{ inputs.dow_appendix == 'true' || inputs.dow_appendix == 'true' }}" | |
echo "dow_totals | dow_appendix is true. Proceeding with the task." | |
ls -R ./scenario_results | |
### Install FrEDI from new branch and get results | |
### Install FrEDI from ref branch and get results | |
- name: Test results | |
if: | | |
inputs.do_compare == 'true' || | |
inputs.dow_results == 'true' || | |
inputs.dow_totals == 'true' || | |
inputs.dow_appendix == 'true' | |
run: | | |
Rscript -e ' | |
### Libraries | |
require(tidyverse) | |
require(devtools) | |
require(openxlsx) | |
require(ggpubr) | |
### Arguments | |
doGenTests <- "${{ inputs.do_compare }}" %in% c("true", "yes") | |
doScenarios <- "${{ inputs.dow_results }}" %in% c("true", "yes") | |
doSumFig <- "${{ inputs.dow_totals }}" %in% c("true", "yes") | |
doAppFig <- "${{ inputs.dow_appendix }}" %in% c("true", "yes") | |
doFigures <- doSumFig | doAppFig | |
doTotals <- doSumFig | |
sectors0 <- "${{ inputs.sector }}" | |
### Which branches to install | |
installRef <- doGenTests | |
installNew <- doGenTests | doScenarios | doFigures | |
###### Paths ###### | |
### Main repo path, FrEDI project path, scripts path | |
rPath0 <- "."; | |
# pPath0 <- rPath0 |> file.path("FrEDI") | |
pPath0 <- rPath0 | |
### Load scripts and testing functions | |
sPath0 <- pPath0 |> file.path("scripts") | |
tPath0 <- pPath0 |> file.path("testing") | |
sFiles0 <- sPath0 |> list.files(full.names=TRUE) | |
tFiles0 <- tPath0 |> list.files(full.names=TRUE) | |
for(file_i in sFiles0){file_i |> source(); rm(file_i)} | |
for(file_i in tFiles0){file_i |> source(); rm(file_i)} | |
### Where to save results | |
oPath0 <- pPath0 |> file.path("data_tests") | |
oFileNew <- oPath0 |> file.path("newResults.rda") | |
oFileRef <- oPath0 |> file.path("refResults.rda") | |
### Check if path exists and, if not, create it | |
exists0 <- oPath0 |> dir.exists() | |
if(!exists0) oPath0 |> dir.create(recursive=TRUE) | |
###### Action Arguments ###### | |
urlRepo <- "https://github.com/USEPA/FrEDI" | |
newBranch <- "${{ github.ref_name }}" | |
refBranch <- "${{ inputs.ref_branch }}" | |
c(newBranch, refBranch) |> print() | |
aggTypes <- "${{ inputs.agg_types }}" %in% c("true", "yes") | |
if(aggTypes) { | |
cAggLvls <- "all" | |
} else { | |
cAggLvls <- c("national", "modelaverage", "impactyear") | |
} ### End if(aggTypes) | |
###### Compare Results ###### | |
### Compare results between branches | |
###### ** Install FrEDI from Reference Branch ###### | |
### Install FrEDI from ref branch | |
if(installRef) { | |
devtools::install_github( | |
repo=urlRepo, ref=refBranch, | |
# subdir="FrEDI", | |
dependencies=F, upgrade="never", force=T, type="source" | |
) ### End install_github | |
} ### End if(installRef) | |
###### ** Run FrEDI from Reference Branch ###### | |
### Load library, run FrEDI | |
if(doGenTests) { | |
library(FrEDI) | |
dfRef <- run_fredi(aggLevels=cAggLvls) | |
dfRef |> save(file=oFileRef) | |
"Finished running FrEDI on " |> paste0(refBranch, " branch.") |> print() | |
oPath0 |> list.files() |> print() | |
### Detach FrEDI package | |
package:FrEDI |> detach(unload=TRUE) | |
} ### End if(doGenTests) | |
###### ** Install FrEDI from New Branch ###### | |
### Install FrEDI from new branch | |
if(installNew) { | |
devtools::install_github( | |
repo=urlRepo, ref=newBranch, | |
dependencies=F, upgrade="never", force=T, type="source" | |
) ### End install_github | |
} ### End if(installNew) | |
###### ** Run FrEDI from New Branch ###### | |
### Load library, run FrEDI | |
if(doGenTests) { | |
library(FrEDI) | |
dfNew <- run_fredi(aggLevels=cAggLvls) | |
dfNew |> save(file=oFileNew) | |
"Finished running FrEDI on " |> paste0(newBranch, " branch.") |> print() | |
oPath0 |> list.files() |> print() | |
} ### End if(doGenTests) | |
###### ** Run General Tests ###### | |
### Get test results | |
if(doGenTests) { | |
dfTests <- general_fredi_test( | |
newOutputs = dfNew, | |
refOutputs = dfRef, | |
outPath = oPath0 | |
) ### End general_fredi_test | |
"Finished running general tests." |> print() | |
oPath0 |> list.files() |> print() | |
rm(dfTests) | |
} ### End if(doGenTests) | |
###### Sectors & Years ###### | |
if(doScenarios | doFigures) { | |
### Format sector names | |
sectors0 |> print() | |
sectors0 <- sectors0 |> | |
str_split(pattern=",") |> unlist() |> | |
trimws() | |
sectors0 |> print() | |
### Which years to report on for GCM, SLR sectors | |
gcmYears0 <- c(2010, 2090) | |
slrYears0 <- c(2050, 2090) | |
} ### End if(doScenarios | doFigures) | |
###### Run Scenarios ###### | |
###### ** Setup Scenarios ###### | |
if(doScenarios) { | |
### CONUS temps | |
conusTemps <- list( | |
temps = c(0:10), | |
tempLabels = c(0:10), | |
tempType = "conus", | |
prefix = "Other_Integer" | |
) ### End list | |
### Global temps | |
globalTemps <- list( | |
temps = c(1.487, 2.198), | |
tempLabels = c(1.5, 2), | |
tempType = "global", | |
prefix = "preI_global" | |
) ### End list | |
### Temps list | |
tempsList <- list(conus=conusTemps, global=globalTemps) | |
rm(conusTemps, globalTemps) | |
} ### End if(doScenarios) | |
###### ** Run Scenarios ###### | |
### aggOnly=Whether to only include sectors for which "includeaggregate==1" in Fig 7 plots | |
if(doScenarios) { | |
### Run scenarios | |
dfResults <- run_constantTempScenarios( | |
sectors = sectors0, | |
tempList = tempsList, | |
gcmYears = gcmYears0, | |
slrYears = slrYears0, | |
aggOnly = FALSE, | |
loadCode = "project", | |
silent = TRUE , | |
testing = FALSE, | |
fpath = pPath0, | |
saveFile = TRUE , | |
outPath = oPath0, | |
return = FALSE | |
) ### End run_constantTempScenarios | |
"Finished running scenarios..." |> print() | |
oPath0 |> list.files() |> print() | |
rm(dfResults) | |
} ### End if(doScenarios) | |
###### Create Figures ###### | |
### aggOnly=Whether to only include sectors for which "includeaggregate==1" in Fig 7 plots | |
if(doFigures) { | |
### Artifact paths | |
artDir <- oPath0 |> file.path("scenario_results") | |
gcmName <- "gcm_DOW_scenario_results" |> paste0(".", "rda") | |
slrName <- "slr_scenario_results" |> paste0(".", "rda") | |
gcmPath <- artDir |> file.path(gcmName) | |
slrPath <- artDir |> file.path(slrName) | |
### Initialize data | |
gcmData0 <- NULL | |
slrData0 <- NULL | |
### Check if files exist | |
gcmExists <- gcmPath |> file.exists() | |
slrExists <- slrPath |> file.exists() | |
anyExist <- c(gcmExists, slrExists) |> any() | |
oPath0 |> list.files() |> print(); | |
artDir |> list.files() |> print(); | |
c(gcmPath, slrPath) |> print() | |
### Load data | |
do_any <- FALSE | |
if(gcmExists) { | |
### Try to load data | |
# gcmPath |> load() | |
gcmPath |> load(gcm_env <- new.env()) | |
gcm_list <- gcm_env |> as.list() |> unlist(recursive=F) | |
gcmData0 <- gcm_list[["obj0"]] | |
do_gcm <- !(gcmData0 |> is.null()) | |
do_any <- do_any | do_gcm | |
gcmData0 |> glimpse() | |
rm(gcm_env, gcm_list) | |
} ### End if(gcmExists) | |
if(slrExists) { | |
# slrPath |> load() | |
slrPath |> load(slr_env <- new.env()) | |
slr_list <- slr_env |> as.list() | |
slrData0 <- slr_list[["obj0"]] | |
do_slr <- !(slrData0 |> is.null()) | |
do_any <- do_any | do_slr | |
slrData0 |> glimpse() | |
rm(slr_env, slr_list) | |
} ### End if(slrExists) | |
### Whether to run the test | |
makePlots <- doFigures & anyExist & do_any | |
makePlots |> print() | |
### gcmData=Dataframe with data for GCM sectors | |
### slrData=Dataframe with data for SLR sectors | |
if(makePlots) { | |
### Number of digits for formatting sector names | |
digits0 <- 16 | |
### Create plots | |
dfPlots <- create_DOW_plots( | |
sectors = sectors0, | |
gcmData = gcmData0, | |
slrData = slrData0, | |
gcmYears = gcmYears0, | |
slrYears = slrYears0, | |
totals = doTotals, | |
aggOnly = doTotals, | |
digits = digits, | |
silent = TRUE , | |
testing = FALSE, | |
loadCode = "project", | |
fpath = "." , | |
saveFile = TRUE , | |
outPath = oPath0, | |
img_dev = "pdf", | |
return = FALSE | |
) ### End create_DOW_plotsfunction | |
} ### End if(makePlots) | |
"Finished creating figures..." |> print() | |
oPath0 |> list.files() |> print() | |
} ### End if(doScenarios) | |
"got here" |> print() | |
' | |
- name: Upload General Tests | |
if: | | |
inputs.do_compare == 'true' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: general_tests | |
path: | | |
./data_tests/* | |
- name: Upload Scenario Results | |
if: | | |
inputs.dow_results == 'true' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: scenario_results | |
path: | | |
./data_tests/* | |
- name: Upload Report Figures | |
if: | | |
inputs.dow_totals == 'true' || | |
inputs.dow_appendix == 'true' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: report_figures | |
path: | | |
./data_tests/* | |