From 315a5f24a211960e937509d9bc0e8261b3f1c0c2 Mon Sep 17 00:00:00 2001 From: Falk Benke Date: Wed, 13 Mar 2024 10:50:13 +0100 Subject: [PATCH 1/3] add expandMagclass helper --- R/expandMagclass.R | 25 ++++++++++++++++++ R/reportEmi.R | 5 +++- tests/testthat/test-expandMagclass.R | 38 ++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 R/expandMagclass.R create mode 100644 tests/testthat/test-expandMagclass.R diff --git a/R/expandMagclass.R b/R/expandMagclass.R new file mode 100644 index 00000000..8ac2d1a8 --- /dev/null +++ b/R/expandMagclass.R @@ -0,0 +1,25 @@ +#' Expand Magclass Object +#' +#' A helper to that restricts and expands x to the size of ref. +#' +#' @param x a magclass object to be modified +#' @param ref a magclass object used as a reference for the modification +#' @param fill value to be set in new dimensions +#' +#' @export +expandMagclass <- function(x, ref, fill = 0) { + # extend the object to the union of the both objects + r <- new.magpie( + cells_and_regions = union(getRegions(x), getRegions(ref)), + years = union(getYears(x), getYears(ref)), + names = union(getNames(x), getNames(ref)), + fill = fill, + sets = names(dimnames(ref)) + ) + # copy over values from x + r[getRegions(x), getYears(x), getNames(x)] <- x + + # restrict object to dimensions of ref + r <- r[getRegions(ref), getYears(ref), getNames(ref)] + return(r) +} diff --git a/R/reportEmi.R b/R/reportEmi.R index 13e022a5..cc43cf44 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -143,8 +143,11 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, # FE non-energy use vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = F, react = "silent")[,t,] + if (length(vm_demFENonEnergySector) == 0) { vm_demFENonEnergySector <- NULL + } else { + vm_demFENonEnergySector <- expandMagclass(vm_demFENonEnergySector, vm_demFeSector) } # secondary energy production @@ -301,7 +304,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, # calculate FE without non-energy use vm_demFeSector_woNonEn <- vm_demFeSector - vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector + vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector)] - vm_demFENonEnergySector } # Read-in plastic-related variables diff --git a/tests/testthat/test-expandMagclass.R b/tests/testthat/test-expandMagclass.R new file mode 100644 index 00000000..2415b4b8 --- /dev/null +++ b/tests/testthat/test-expandMagclass.R @@ -0,0 +1,38 @@ +test_that("expandMagclass works", { + region <- c("AAA", "BBB", "CCC") + t <- paste0("y", c(2000, 2005, 2010)) + name <- c("foo", "bar", "bazz") + + A <- new.magpie( + cells_and_regions = region, years = t, names = name, fill = 1, + sets = c("region", "t", "name") + ) + + # spatial ---- + ## x smaller then ref ---- + expect_equal(expandMagclass(A[region[-2],,], A), + `mselect<-`(A, region = region[2], value = 0)) + + ## x larger then ref ---- + expect_equal(expandMagclass(A, A[region[-2],,]), + A[region[-2],,]) + + # temporal ---- + ## x smaller then ref ---- + expect_equal(expandMagclass(A[,t[-2],], A), + `mselect<-`(A, t = t[2], value = 0)) + + ## x larger then ref ---- + expect_equal(expandMagclass(x = A, ref = A[,t[-2],]), + A[,t[-2],]) + + # names ---- + ## x smaller then ref ---- + expect_equal(expandMagclass(A[,,name[-2]], A), + `mselect<-`(A, name = name[2], value = 0)) + + ## x larger then ref ---- + expect_equal(expandMagclass(A, A[,,name[-2]]), + A[,,name[-2]]) + +}) From 4bd17f08534b40e0d018496e41f3087fb12db79b Mon Sep 17 00:00:00 2001 From: Falk Benke Date: Wed, 13 Mar 2024 11:23:34 +0100 Subject: [PATCH 2/3] rename helper --- R/{expandMagclass.R => matchDim.R} | 7 ++++--- R/reportEmi.R | 2 +- .../{test-expandMagclass.R => test-matchDim.R} | 12 ++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) rename R/{expandMagclass.R => matchDim.R} (77%) rename tests/testthat/{test-expandMagclass.R => test-matchDim.R} (72%) diff --git a/R/expandMagclass.R b/R/matchDim.R similarity index 77% rename from R/expandMagclass.R rename to R/matchDim.R index 8ac2d1a8..62ed8d00 100644 --- a/R/expandMagclass.R +++ b/R/matchDim.R @@ -1,13 +1,14 @@ -#' Expand Magclass Object +#' match object dimensions of a magclass object #' -#' A helper to that restricts and expands x to the size of ref. +#' A helper that restricts and expands a magclass object x to the size of a +#' magclass object ref. #' #' @param x a magclass object to be modified #' @param ref a magclass object used as a reference for the modification #' @param fill value to be set in new dimensions #' #' @export -expandMagclass <- function(x, ref, fill = 0) { +matchDim <- function(x, ref, fill = 0) { # extend the object to the union of the both objects r <- new.magpie( cells_and_regions = union(getRegions(x), getRegions(ref)), diff --git a/R/reportEmi.R b/R/reportEmi.R index cc43cf44..3863cbd2 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -147,7 +147,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, if (length(vm_demFENonEnergySector) == 0) { vm_demFENonEnergySector <- NULL } else { - vm_demFENonEnergySector <- expandMagclass(vm_demFENonEnergySector, vm_demFeSector) + vm_demFENonEnergySector <- matchDim(vm_demFENonEnergySector, vm_demFeSector) } # secondary energy production diff --git a/tests/testthat/test-expandMagclass.R b/tests/testthat/test-matchDim.R similarity index 72% rename from tests/testthat/test-expandMagclass.R rename to tests/testthat/test-matchDim.R index 2415b4b8..f36de545 100644 --- a/tests/testthat/test-expandMagclass.R +++ b/tests/testthat/test-matchDim.R @@ -1,4 +1,4 @@ -test_that("expandMagclass works", { +test_that("matchDim works", { region <- c("AAA", "BBB", "CCC") t <- paste0("y", c(2000, 2005, 2010)) name <- c("foo", "bar", "bazz") @@ -10,25 +10,25 @@ test_that("expandMagclass works", { # spatial ---- ## x smaller then ref ---- - expect_equal(expandMagclass(A[region[-2],,], A), + expect_equal(matchDim(A[region[-2],,], A), `mselect<-`(A, region = region[2], value = 0)) ## x larger then ref ---- - expect_equal(expandMagclass(A, A[region[-2],,]), + expect_equal(matchDim(A, A[region[-2],,]), A[region[-2],,]) # temporal ---- ## x smaller then ref ---- - expect_equal(expandMagclass(A[,t[-2],], A), + expect_equal(matchDim(A[,t[-2],], A), `mselect<-`(A, t = t[2], value = 0)) ## x larger then ref ---- - expect_equal(expandMagclass(x = A, ref = A[,t[-2],]), + expect_equal(matchDim(x = A, ref = A[,t[-2],]), A[,t[-2],]) # names ---- ## x smaller then ref ---- - expect_equal(expandMagclass(A[,,name[-2]], A), + expect_equal(matchDim(A[,,name[-2]], A), `mselect<-`(A, name = name[2], value = 0)) ## x larger then ref ---- From af78e5dd9aed32f6cf5369cd9755f86b3d5eb2d2 Mon Sep 17 00:00:00 2001 From: Falk Benke Date: Wed, 13 Mar 2024 12:02:35 +0100 Subject: [PATCH 3/3] increment version --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- NAMESPACE | 1 + R/matchDim.R | 5 ++++- README.md | 6 +++--- man/matchDim.Rd | 19 +++++++++++++++++++ tests/testthat/test-matchDim.R | 2 +- 8 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 man/matchDim.Rd diff --git a/.buildlibrary b/.buildlibrary index 913bd9e5..3ef7620a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '2246856528' +ValidationKey: '2247009630' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 786ee459..2b5c18c8 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'remind2: The REMIND R package (2nd generation)' -version: 1.135.12 -date-released: '2024-03-12' +version: 1.135.14 +date-released: '2024-03-13' abstract: Contains the REMIND-specific routines for data and model output manipulation. authors: - family-names: Rodrigues diff --git a/DESCRIPTION b/DESCRIPTION index c16b571a..5a0c1e02 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: remind2 Title: The REMIND R package (2nd generation) -Version: 1.135.12 -Date: 2024-03-12 +Version: 1.135.14 +Date: 2024-03-13 Authors@R: c( person("Renato", "Rodrigues", , "renato.rodrigues@pik-potsdam.de", role = c("aut", "cre")), person("Lavinia", "Baumstark", role = "aut"), diff --git a/NAMESPACE b/NAMESPACE index fc67eee1..74bae3dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,6 +28,7 @@ export(getRunsMIFGDX) export(get_total_efficiencies) export(loadCs2Data) export(loadModeltest) +export(matchDim) export(nashAnalysis) export(plotCDR) export(plotLCOE) diff --git a/R/matchDim.R b/R/matchDim.R index 62ed8d00..1ef1f206 100644 --- a/R/matchDim.R +++ b/R/matchDim.R @@ -9,7 +9,8 @@ #' #' @export matchDim <- function(x, ref, fill = 0) { - # extend the object to the union of the both objects + + # extend the object to the union of both objects r <- new.magpie( cells_and_regions = union(getRegions(x), getRegions(ref)), years = union(getYears(x), getYears(ref)), @@ -17,10 +18,12 @@ matchDim <- function(x, ref, fill = 0) { fill = fill, sets = names(dimnames(ref)) ) + # copy over values from x r[getRegions(x), getYears(x), getNames(x)] <- x # restrict object to dimensions of ref r <- r[getRegions(ref), getYears(ref), getNames(ref)] + return(r) } diff --git a/README.md b/README.md index 5e165ea7..e4064757 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # The REMIND R package (2nd generation) -R package **remind2**, version **1.135.12** +R package **remind2**, version **1.135.14** [![CRAN status](https://www.r-pkg.org/badges/version/remind2)](https://cran.r-project.org/package=remind2) [![R build status](https://github.com/pik-piam/remind2/workflows/check/badge.svg)](https://github.com/pik-piam/remind2/actions) [![codecov](https://codecov.io/gh/pik-piam/remind2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/remind2) [![r-universe](https://pik-piam.r-universe.dev/badges/remind2)](https://pik-piam.r-universe.dev/builds) @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues . +Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Duerrwaechter J, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P, Weigmann P (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.135.14, . A BibTeX entry for LaTeX users is @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is title = {remind2: The REMIND R package (2nd generation)}, author = {Renato Rodrigues and Lavinia Baumstark and Falk Benke and Jan Philipp Dietrich and Alois Dirnaichner and Jakob Duerrwaechter and Pascal Führlich and Anastasis Giannousakis and Robin Hasse and Jérome Hilaire and David Klein and Johannes Koch and Katarzyna Kowalczyk and Antoine Levesque and Aman Malik and Anne Merfort and Leon Merfort and Simón Morena-Leiva and Michaja Pehl and Robert Pietzcker and Sebastian Rauner and Oliver Richters and Marianna Rottoli and Christof Schötz and Felix Schreyer and Kais Siala and Björn Sörgel and Mike Spahr and Jessica Strefler and Philipp Verpoort and Pascal Weigmann}, year = {2024}, - note = {R package version 1.135.12}, + note = {R package version 1.135.14}, url = {https://github.com/pik-piam/remind2}, } ``` diff --git a/man/matchDim.Rd b/man/matchDim.Rd new file mode 100644 index 00000000..d68a6fed --- /dev/null +++ b/man/matchDim.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/matchDim.R +\name{matchDim} +\alias{matchDim} +\title{match object dimensions of a magclass object} +\usage{ +matchDim(x, ref, fill = 0) +} +\arguments{ +\item{x}{a magclass object to be modified} + +\item{ref}{a magclass object used as a reference for the modification} + +\item{fill}{value to be set in new dimensions} +} +\description{ +A helper that restricts and expands a magclass object x to the size of a +magclass object ref. +} diff --git a/tests/testthat/test-matchDim.R b/tests/testthat/test-matchDim.R index f36de545..cc0b906c 100644 --- a/tests/testthat/test-matchDim.R +++ b/tests/testthat/test-matchDim.R @@ -32,7 +32,7 @@ test_that("matchDim works", { `mselect<-`(A, name = name[2], value = 0)) ## x larger then ref ---- - expect_equal(expandMagclass(A, A[,,name[-2]]), + expect_equal(matchDim(A, A[,,name[-2]]), A[,,name[-2]]) })