Skip to content

Commit

Permalink
Merge pull request #220 from caviddhen/master
Browse files Browse the repository at this point in the history
fix toolAggregate warning in toolISOHistorical
  • Loading branch information
caviddhen authored Oct 15, 2024
2 parents 6b1ea35 + b129a6b commit d664da2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '62608680'
ValidationKey: '62691330'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
lucode2
covr
madrat
magclass
citation
gms
goxygen
GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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: 'madrat: May All Data be Reproducible and Transparent (MADRaT) *'
version: 3.13.2
date-released: '2024-09-24'
version: 3.13.3
date-released: '2024-10-14'
abstract: Provides a framework which should improve reproducibility and transparency
in data processing. It provides functionality such as automatic meta data creation
and management, rudimentary quality management, data caching, work-flow management
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: madrat
Title: May All Data be Reproducible and Transparent (MADRaT) *
Version: 3.13.2
Date: 2024-09-24
Version: 3.13.3
Date: 2024-10-14
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre"),
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431")),
Expand Down
19 changes: 11 additions & 8 deletions R/toolISOhistorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ toolISOhistorical <- function(m, mapping = NULL, additional_mapping = NULL, over
}

mTr <- toolAggregate(m[a$fromISO, subTime, ],
mapping[is.element(mapping$toISO, a$toISO), c("fromISO", "toISO")], weight = weight,
mapping[is.element(mapping$toISO, a$toISO), c("fromISO", "toISO")],
weight = weight + 1e-12,
negative_weight = "allow")
## aggregation of countries
} else {
Expand Down Expand Up @@ -241,15 +242,17 @@ toolISOhistorical <- function(m, mapping = NULL, additional_mapping = NULL, over
# these countries are added below if missing, and given weight 0
if (mainDim == 1.1) {
addInd <- setdiff(as.vector(outer(
tr$toISO,
getItems(z[setNames(list(c(tr$fromISO)), mainDimName), subTime, ], dim = secdDim),
paste, sep = ".")),
getItems(weight, dim = 1))
tr$toISO,
getItems(z[setNames(list(c(tr$fromISO)), mainDimName), subTime, ],
dim = secdDim),
paste, sep = ".")),
getItems(weight, dim = 1))
} else if (mainDim == 1.2) {
addInd <- setdiff(as.vector(outer(
getItems(z[setNames(list(c(tr$fromISO)), mainDimName), subTime, ], dim = secdDim),
tr$toISO, paste, sep = ".")),
getItems(weight, dim = 1))
getItems(z[setNames(list(c(tr$fromISO)), mainDimName), subTime, ],
dim = secdDim),
tr$toISO, paste, sep = ".")),
getItems(weight, dim = 1))
}
if (length(addInd) > 0) {
addR <- unique(c(addR, addInd))
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# May All Data be Reproducible and Transparent (MADRaT) *

R package **madrat**, version **3.13.2**
R package **madrat**, version **3.13.3**

[![CRAN status](https://www.r-pkg.org/badges/version/madrat)](https://cran.r-project.org/package=madrat) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1115490.svg)](https://doi.org/10.5281/zenodo.1115490) [![R build status](https://github.com/pik-piam/madrat/workflows/check/badge.svg)](https://github.com/pik-piam/madrat/actions) [![codecov](https://codecov.io/gh/pik-piam/madrat/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/madrat) [![r-universe](https://pik-piam.r-universe.dev/badges/madrat)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -55,7 +55,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **madrat** in publications use:

Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Sauer P (2024). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi:10.5281/zenodo.1115490 <https://doi.org/10.5281/zenodo.1115490>, R package version 3.13.2, <https://github.com/pik-piam/madrat>.
Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Sauer P (2024). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi: 10.5281/zenodo.1115490 (URL: https://doi.org/10.5281/zenodo.1115490), R package version 3.13.3, <URL: https://github.com/pik-piam/madrat>.

A BibTeX entry for LaTeX users is

Expand All @@ -64,8 +64,8 @@ A BibTeX entry for LaTeX users is
title = {madrat: May All Data be Reproducible and Transparent (MADRaT)},
author = {Jan Philipp Dietrich and Lavinia Baumstark and Stephen Wirth and Anastasis Giannousakis and Renato Rodrigues and Benjamin Leon Bodirsky and Debbora Leip and Ulrich Kreidenweis and David Klein and Pascal Sauer},
year = {2024},
note = {R package version 3.13.2},
url = {https://github.com/pik-piam/madrat},
note = {R package version 3.13.3},
doi = {10.5281/zenodo.1115490},
url = {https://github.com/pik-piam/madrat},
}
```
44 changes: 25 additions & 19 deletions tests/testthat/test-toolISOhistorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("Historic data is properly translated", {
a["SUN", 1990, ] <- 240
a[-1, 1991:1992, ] <- rep(1:15, 2)
a <- toolISOhistorical(a)
expect_identical(as.vector(a[, 1990, ]), 2 * (1:15))
expect_equal(as.vector(a[, 1990, ]), 2 * (1:15))
expect_identical(magclass::getItems(a, dim = 1.1), newcountries)
})

Expand All @@ -23,8 +23,10 @@ test_that("Given mapping data is properly translated", {

ref <- new("magpie", .Data = structure(c(2, 1, 1, 1, 2, 1, 1, 1, 2,
1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1),
.Dim = c(4L, 5L, 1L), .Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"), data = NULL)))
.Dim = c(4L, 5L, 1L),
.Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"),
data = NULL)))
expect_identical(toolISOhistorical(a, mapping = m), ref)

mapfile <- tempfile()
Expand All @@ -41,9 +43,11 @@ test_that("Given mapping data is properly translated", {
b[is.na(b)] <- 0

ref2 <- new("magpie", .Data = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 2, 0, 0, 0, 2, 1, 1, 1), .Dim = c(4L, 5L, 1L),
.Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"), data = NULL)))
0, 0, 0, 2, 0, 0, 0, 2, 1, 1, 1),
.Dim = c(4L, 5L, 1L),
.Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"),
data = NULL)))

expect_identical(toolISOhistorical(b, mapping = m, overwrite = TRUE), ref)
expect_identical(toolISOhistorical(b, mapping = m, overwrite = FALSE), ref2)
Expand All @@ -52,15 +56,17 @@ test_that("Given mapping data is properly translated", {
expect_warning(o <- toolISOhistorical(a, mapping = m), "Weight in toolISOhistorical contained NAs")
ref3 <- new("magpie", .Data = structure(c(2, 0, 1.5, 1.5, 2, 0, 1.5,
1.5, 2, 0, 1.5, 1.5, 2, 0, 1.5, 1.5, 2, NA, 1, 1),
.Dim = c(4L, 5L, 1L), .Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"), data = NULL)))
expect_identical(o, ref3)
.Dim = c(4L, 5L, 1L),
.Dimnames = list(region = c("A", "B1", "B2", "B3"),
year = c("y9", "y10", "y11", "y12", "y13"),
data = NULL)))
expect_equal(o, ref3)
})

test_that("Bilateral data is properly translated", {
localConfig(.verbose = FALSE)
newcountriesA <- as.vector(outer(c("ARM", "AZE", "BLR", "EST", "GEO", "KAZ", "KGZ",
"LVA", "LTU", "MDA", "TJK", "TKM", "UKR", "UZB"), "USA", paste, sep = "."))
"LVA", "LTU", "MDA", "TJK", "TKM", "UKR", "UZB"), "USA", paste, sep = "."))
missing <- "RUS.USA"
a <- new.magpie(c("SUN.USA", newcountriesA), 1991:1993)
a["SUN.USA", 1991, ] <- 240
Expand All @@ -72,24 +78,24 @@ test_that("Bilateral data is properly translated", {
# when disagg in 2nd dim, and with all countries missing
b <- new.magpie("USA.SUN", 1991:1993)
b["USA.SUN", 1991, ] <- 240
for (ov in c(TRUE, FALSE)){
b1 <- toolISOhistorical(b, overwrite = ov)
expect_equal(as.vector(b1[, 1991, ]), rep(240 / 15, 15))
for (ov in c(TRUE, FALSE)) {
b1 <- toolISOhistorical(b, overwrite = ov)
expect_equal(as.vector(b1[, 1991, ]), rep(240 / 15, 15))
}
# when additional weighting is given
expect_error(o <- toolISOhistorical(b, additional_weight = "2"),
"Additional_weight is not supported for bilateral data.")

# when aggregating
mapping <- data.frame(fromISO = c("A1", "A2"),
toISO = c("A", "A"),
lastYear = c("y0010", "y0010"),
stringsAsFactors = FALSE)
toISO = c("A", "A"),
lastYear = c("y0010", "y0010"),
stringsAsFactors = FALSE)

d <- new.magpie(c("A1.Z", "A2.Z", "A.Z"), 9:11,)
d[c("A1.Z", "A2.Z"), c("y0009", "y0010") , ] <- 5
d <- new.magpie(c("A1.Z", "A2.Z", "A.Z"), 9:11, )
d[c("A1.Z", "A2.Z"), c("y0009", "y0010"), ] <- 5
d <- toolISOhistorical(d, mapping = mapping, overwrite = FALSE)
expect_equal(as.vector(d[,c("y0009", "y0010"), ]), rep(10, 2))
expect_equal(as.vector(d[, c("y0009", "y0010"), ]), rep(10, 2))



Expand Down

0 comments on commit d664da2

Please sign in to comment.