Skip to content

Commit

Permalink
Merge pull request #260 from USEPA/release-v1.4.0
Browse files Browse the repository at this point in the history
Release v1.4.0
  • Loading branch information
bl-young authored Jan 19, 2024
2 parents b8bc690 + 4d7dbc2 commit 387073c
Show file tree
Hide file tree
Showing 40 changed files with 795 additions and 1,064 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: R CI/CD test #R-CMD-check

on:
push:
branches: [main, master, develop]
# branches: [main, master, develop]
paths-ignore:
- '**.md' # prevent md files (e.g., README.md) in any repo dir from trigering workflow
- '**.bib'
Expand Down Expand Up @@ -73,7 +73,6 @@ jobs:
devtools::load_all()
m <- useeior::buildModel("USEEIOv2.0-411")
useeior::printValidationResults(m)
shell: Rscript {0}

- name: Build and validate models - USEEIOv2.0-i-411 detail industry model with waste disaggregation
Expand All @@ -83,7 +82,6 @@ jobs:
devtools::load_all()
m <- useeior::buildModel("USEEIOv2.0-i-411")
useeior::printValidationResults(m)
shell: Rscript {0}

- name: Build and validate models - USEEIOv2.0 industry model
Expand All @@ -93,7 +91,6 @@ jobs:
devtools::load_all()
m <- useeior::buildModel("USEEIOv2.0-i-GHG")
useeior::printValidationResults(m)
shell: Rscript {0}

- name: Build and validate models - USEEIOv2.0 summary model
Expand All @@ -103,7 +100,6 @@ jobs:
devtools::load_all()
m <- useeior::buildModel("USEEIOv2.0-s-GHG")
useeior::printValidationResults(m)
shell: Rscript {0}

- name: Build and validate models - USEEIOv2.0.1-411 detail model with waste disaggregation
Expand All @@ -122,7 +118,15 @@ jobs:
devtools::load_all()
m <- useeior::buildModel("USEEIOv2.0-GHG-NGCombustion")
useeior::printValidationResults(m)
shell: Rscript {0}

- name: Build and validate models - GAEEIOv1.0-s-WAT-12 summary two-region model
if: always()
run: |
require(devtools)
devtools::load_all()
m <- useeior::buildModel("GAEEIOv1.0-s-WAT-12")
useeior::print2RValidationResults(m)
shell: Rscript {0}

#- name: Show testthat output
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/generate-2R-model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
model:
description: "Model"
required: true
default: "GAEEIOv2.0-s-WAT"
default: "GAEEIOv1.0-s-WAT-12"

jobs:
build:
Expand Down
26 changes: 8 additions & 18 deletions .zenodo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
"title": "useeior",
"upload_type": "software",
"creators": [
{
"affiliation": "General Dynamics Information Technology, Inc.",
"name": "Mo Li"
"orcid": "https://orcid.org/0000-0002-3672-1622"
},
{
"affiliation": "US Environmental Protection Agency",
"name": "Wesley Ingwersen"
"orcid": "https://orcid.org/0000-0002-9614-701X"
},
{
"affiliation": "Eastern Research Group",
"name": "Ben Young"
Expand All @@ -23,16 +13,16 @@
"affiliation": "Eastern Research Group",
"name": "Jorge Vendries"
"orcid": "https://orcid.org/0000-0002-8452-229X"
},
{
"affiliation": "US Environmental Protection Agency",
"name": "Catherine Birney",
"orcid": "https://orcid.org/0000-0003-4467-9927"
},
{
"affiliation": "General Dynamics Information Technology, Inc.",
"name": "Mo Li"
"orcid": "https://orcid.org/0000-0002-3672-1622"
},
{
"affiliation": "Eastern Research Group",
"name": "Andrew Beck",
"orcid": "https://orcid.org/0000-0003-4051-6901"
"affiliation": "US Environmental Protection Agency",
"name": "Wesley Ingwersen"
"orcid": "https://orcid.org/0000-0002-9614-701X"
}
],
"access_right": "open"
Expand Down
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: useeior
Type: Package
Title: USEEIO R modeling software
Version: 1.3.0
Date: 2023-7-21
Version: 1.4.0
Date: 2024-1-19
Authors@R: c(
person("Mo","Li", email="[email protected]", role="aut"),
person("Wesley","Ingwersen", email="[email protected]", role= c("aut", "cre")),
person("Ben","Young", email="[email protected]", role="aut"),
person("Jorge","Vendries", email="[email protected]", role="aut"))
person("Jorge","Vendries", email="[email protected]", role="aut"),
person("Mo","Li", email="[email protected]", role="aut"),
person("Wesley","Ingwersen", email="[email protected]", role= c("aut", "cre")))
Description: The United States Environmentally-Extended Input-Output model
is a model used to estimate potential environmental and economic impacts
associated with the production and consumption of goods and services in the US.
Expand All @@ -23,6 +23,7 @@ Imports:
ggplot2 (>= 3.0),
jsonlite (>= 1.6),
logging (>= 0.10),
Matrix (>= 1.4),
rappdirs (>= 0.3),
readxl (>= 1.3.1),
reshape2 (>= 1.4.3),
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ export(aggregateResultMatrixbyRow)
export(barplotFloworImpactFractionbyRegion)
export(barplotIndicatorScoresbySector)
export(buildModel)
export(buildTwoRegionModels)
export(calculateEEIOModel)
export(calculateFlowContributiontoImpact)
export(calculateMarginSectorImpacts)
export(calculateSectorContributiontoImpact)
export(calculateSectorPurchasedbySectorSourcedImpact)
export(compare2RCommodityTotals)
export(compareCommodityOutputXMarketShareandIndustryOutputwithCPITransformation)
export(compareCommodityOutputandDomesticUseplusProductionDemand)
export(compareEandLCIResult)
export(compareFlowTotals)
export(compareOutputandLeontiefXDemand)
export(disaggregateTotalToDirectAndTier1)
export(extractAndFormatDemandVector)
export(formatDemandVector)
export(generateModelIdentifier)
export(heatmapSatelliteTableCoverage)
export(heatmapSectorRanking)
export(normalizeResultMatrixByTotalImpacts)
export(plotMatrixCoefficient)
export(print2RValidationResults)
export(printValidationResults)
export(seeAvailableModels)
export(validate2RCommodityTotals)
export(writeModelMatrices)
export(writeModelforAPI)
export(writeModeltoXLSX)
Expand Down
62 changes: 60 additions & 2 deletions R/BuildModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ constructEEIOMatrices <- function(model) {
colnames(model$U_d) <- colnames(model$U)
model[c("U", "U_d")] <- lapply(model[c("U", "U_d")],
function(x) ifelse(is.na(x), 0, x))
model$U_n <- generateDirectRequirementsfromUse(model, domestic = FALSE) #normalized Use
model$U_d_n <- generateDirectRequirementsfromUse(model, domestic = TRUE) #normalized DomesticUse

if (model$specs$IODataSource=="stateior") {
model$U_n <- generate2RDirectRequirementsfromUseWithTrade(model, domestic = FALSE)
model$U_d_n <- generate2RDirectRequirementsfromUseWithTrade(model, domestic = TRUE)
} else {
model$U_n <- generateDirectRequirementsfromUse(model, domestic = FALSE) #normalized Use
model$U_d_n <- generateDirectRequirementsfromUse(model, domestic = TRUE) #normalized DomesticUse
}

model$q <- model$CommodityOutput
model$x <- model$IndustryOutput
model$mu <- model$InternationalTradeAdjustment
Expand Down Expand Up @@ -227,6 +234,57 @@ createCfromFactorsandBflows <- function(factors,B_flows) {
return(C)
}

#' Build two-region models for all 50 states based on a single config reference file.
#' @param modelname Name of the model from a config file.
#' @param configpaths str vector, paths (including file name) of model configuration file
#' and optional agg/disagg configuration file(s). If NULL, built-in config files are used.
#' @param validate bool, if TRUE print validation results for each model
#' @param year int, indicating for which year to run the models
#' @return A list of EEIO models for each state with complete components and attributes
#' @export
buildTwoRegionModels <- function(modelname, configpaths = NULL, validate = FALSE, year = NULL) {
model_ls <- list()
q_comparison_failures_ls <- list()
basemodel <- initializeModel(modelname, configpaths)

if(!is.null(year)){
basemodel$specs$IOYear <- year
}

for (s in state.abb){
model <- basemodel
state <- paste("US", s, sep="-")
model$specs$ModelRegionAcronyms[1] <- state
cat("\n")
logging::loginfo(paste0("Building two-region model for ",
paste(state, model$specs$ModelRegionAcronyms[2], sep="/"),
"..."))
cat("\n")

model_ls[[state]] <- tryCatch(
{
model <- loadIOData(model, configpaths)
model <- loadandbuildSatelliteTables(model)
model <- loadandbuildIndicators(model)
model <- loadDemandVectors(model)
model <- constructEEIOMatrices(model)
if (validate) {
print2RValidationResults(model)
}
model_ls[[state]] <- model
},
error=function(e)
{
message(paste0("Error for ", state, " model."))
return(NA)
}
)# end of try catch


}
return(model_ls)
}

#' Build an EIO model with economic components only.
#' @param modelname Name of the model from a config file.
#' @param configpaths str vector, paths (including file name) of model configuration file
Expand Down
27 changes: 21 additions & 6 deletions R/CalculationFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
#' Calculate total emissions/resources (LCI) and total impacts (LCIA) for an EEIO model
#' for a given perspective and demand vector.
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param perspective Perspective of the model, can be "DIRECT", "INTERMEDIATE", or "FINAL".
#' @param demand A demand vector, can be name of a built-in model demand vector, e.g. "Production",
#' @param perspective Perspective of the model, can be "DIRECT" or "FINAL". "DIRECT" perspective
#' aligns results with the sectors in which they are produced, while "FINAL" perspective aligns
#' results with the sectors consumed by the final user.
#' @param demand A demand vector, can be name of a built-in model demand vector, e.g. "Production" or "Consumption",
#' or an actual demand vector with names as one or more model sectors and
#' numeric values in USD with the same dollar year as model.
#' @param location, str optional location code for demand vector, required for two-region models
#' @param use_domestic_requirements A logical value: if TRUE, use domestic demand and L_d matrix;
#' if FALSE, use complete demand and L matrix.
#' @export
#' @return A list with LCI and LCIA results (in data.frame format) of the EEIO model.
calculateEEIOModel <- function(model, perspective, demand = "Production", use_domestic_requirements = FALSE) {
calculateEEIOModel <- function(model, perspective, demand = "Production", location = NULL, use_domestic_requirements = FALSE) {
result <- list()
# Generate Total Requirements (L or L_d) matrix based on whether "use_domestic"
if (use_domestic_requirements) {
L <- model$L_d
M <- model$M_d
N <- model$N_d
} else {
L <- model$L
M <- model$M
N <- model$N
}

# Prepare demand vector
Expand All @@ -30,7 +37,15 @@ calculateEEIOModel <- function(model, perspective, demand = "Production", use_do
paste0("Domestic", demand),
paste0("Complete", demand))
# Get vector name (ID) from the meta table
id <- meta[which(meta$Name==demand_name),"ID"]
if(is.null(location)) {
id <- meta[which(meta$Name==demand_name),"ID"]
if(length(id)>1) {
stop("Unique demand vector not found, consider passing location")
}
} else {
id <- meta[which(meta$Name==demand_name &
meta$Location==location),"ID"]
}
d <- model$DemandVectors$vectors[[id]]
} else {
stop(paste0("'", demand, "' is not a valid demand vector name in model."))
Expand Down Expand Up @@ -58,10 +73,10 @@ calculateEEIOModel <- function(model, perspective, demand = "Production", use_do
} else if (perspective=="FINAL") {
# Calculate Final Perspective LCI (a matrix with total impacts in form of sector x flows)
logging::loginfo("Calculating Final Perspective LCI...")
result$LCI_f <- calculateFinalPerspectiveLCI(model$M, f)
result$LCI_f <- calculateFinalPerspectiveLCI(M, f)
# Calculate Final Perspective LCIA (matrix with total impacts in form of sector x impacts)
logging::loginfo("Calculating Final Perspective LCIA...")
result$LCIA_f <- calculateFinalPerspectiveLCIA(model$N, f)
result$LCIA_f <- calculateFinalPerspectiveLCIA(N, f)
}

logging::loginfo("Result calculation complete.")
Expand Down
5 changes: 3 additions & 2 deletions R/ConfigurationFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#' @param configtype str, configuration type, can be "model", "disagg", or "agg"
#' @param configpaths str vector, paths (including file name) of model configuration file
#' and optional agg/disagg configuration file(s). If NULL, built-in config files are used.
#' @param pkg str, indicate package for access to config, either "useeior" or "stateior"
#' @return A list of model specifications.
getConfiguration <- function(configname, configtype, configpaths = NULL) {
getConfiguration <- function(configname, configtype, configpaths = NULL, pkg="useeior") {
configfile <- paste0(configname, ".yml")
if (is.null(configpaths)) {
configpath <- system.file(paste0("extdata/", configtype, "specs/"), configfile, package = "useeior")
configpath <- system.file(paste0("extdata/", configtype, "specs/"), configfile, package = pkg)
} else {
configpath <- configpaths[endsWith(configpaths, configfile)]
if (length(configpath) == 0) {
Expand Down
Loading

0 comments on commit 387073c

Please sign in to comment.