Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '69584592'
ValidationKey: '69669726'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
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.37.2
date-released: '2026-07-02'
version: 3.37.4
date-released: '2026-07-15'
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.37.2
Date: 2026-07-02
Version: 3.37.4
Date: 2026-07-15
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre"),
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431")),
Expand Down
15 changes: 0 additions & 15 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,17 @@ import(magclass)
importFrom(callr,r)
importFrom(igraph,"V<-")
importFrom(igraph,V)
importFrom(igraph,as_undirected)
importFrom(igraph,cluster_edge_betweenness)
importFrom(igraph,components)
importFrom(igraph,ego)
importFrom(igraph,graph_from_data_frame)
importFrom(igraph,make_ego_graph)
importFrom(igraph,membership)
importFrom(igraph,subcomponent)
importFrom(igraph,union)
importFrom(magclass,"getCells<-")
importFrom(magclass,"getComment<-")
importFrom(magclass,"getYears<-")
importFrom(magclass,as.magpie)
importFrom(magclass,clean_magpie)
importFrom(magclass,collapseNames)
importFrom(magclass,dimExists)
importFrom(magclass,dimSums)
importFrom(magclass,getCells)
importFrom(magclass,getComment)
importFrom(magclass,getItems)
importFrom(magclass,getNames)
importFrom(magclass,getYears)
Expand All @@ -86,15 +78,11 @@ importFrom(magclass,magpie_expand)
importFrom(magclass,mbind)
importFrom(magclass,ncells)
importFrom(magclass,new.magpie)
importFrom(magclass,nregions)
importFrom(magclass,nyears)
importFrom(magclass,read.magpie)
importFrom(magclass,setCells)
importFrom(magclass,setYears)
importFrom(magclass,time_interpolate)
importFrom(magclass,where)
importFrom(magclass,write.magpie)
importFrom(magclass,write.report)
importFrom(methods,formalArgs)
importFrom(pkgload,is_dev_package)
importFrom(renv,activate)
Expand All @@ -115,10 +103,7 @@ importFrom(utils,capture.output)
importFrom(utils,download.file)
importFrom(utils,installed.packages)
importFrom(utils,modifyList)
importFrom(utils,packageDescription)
importFrom(utils,person)
importFrom(utils,read.csv)
importFrom(utils,read.csv2)
importFrom(utils,sessionInfo)
importFrom(utils,str)
importFrom(utils,tail)
Expand Down
10 changes: 3 additions & 7 deletions R/calcOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@
#' a <- calcOutput(type = "TauTotal")
#' }
#'
#' @importFrom magclass nyears nregions getComment<- getComment getYears clean_magpie write.report write.magpie
#' getCells getYears<- is.magpie dimSums
#' @importFrom utils packageDescription read.csv2 read.csv
#' @importFrom withr defer local_dir
#' @export
calcOutput <- function(type, aggregate = TRUE, file = NULL, years = NULL, # nolint
round = NULL, signif = NULL, supplementary = FALSE,
Expand Down Expand Up @@ -205,7 +201,7 @@ calcOutput <- function(type, aggregate = TRUE, file = NULL, years = NULL, # noli
if (x$isocountries) {
.countrycheck <- function(datacountries, name) {
datacountries <- robustSort(datacountries)
isoCountry <- read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry <- utils::read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry1 <- as.vector(isoCountry[, "x"])
names(isoCountry1) <- isoCountry[, "X"]
isocountries <- robustSort(isoCountry1)
Expand Down Expand Up @@ -268,11 +264,11 @@ calcOutput <- function(type, aggregate = TRUE, file = NULL, years = NULL, # noli
}

startinfo <- toolstartmessage(callString, "+")
defer({
withr::defer({
toolendmessage(startinfo, "-")
})

local_dir(getConfig("outputfolder"))
withr::local_dir(getConfig("outputfolder"))

functionname <- prepFunctionName(type = type, prefix = "calc", ignore = ifelse(is.null(years), "years", NA))
extraArgs <- sapply(attr(functionname, "formals"), function(x) return(eval(parse(text = x))), simplify = FALSE) # nolint
Expand Down
2 changes: 1 addition & 1 deletion R/getISOlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' @importFrom magclass ncells
#' @export
getISOlist <- function(type = "all", threshold = 1) {
isoCountry <- read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry <- utils::read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry1 <- as.vector(isoCountry[, "x"])
names(isoCountry1) <- isoCountry[, "X"]
ref <- robustSort(isoCountry1)
Expand Down
2 changes: 0 additions & 2 deletions R/getMadratInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#' @author Jan Philipp Dietrich
#' @seealso \code{\link{getCalculations}}, \code{\link{getMadratGraph}}
#' @family dependency graph
#' @importFrom igraph graph_from_data_frame components V
#' cluster_edge_betweenness as_undirected membership
#' @export

getMadratInfo <- function(graph = NULL, cutoff = 5, extended = FALSE, ...) {
Expand Down
2 changes: 1 addition & 1 deletion R/readSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ readSource <- function(type, subtype = NULL, subset = NULL, # nolint: cyclocomp_
}

.testISO <- function(x, functionname = "function") {
isoCountry <- read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry <- utils::read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry1 <- as.vector(isoCountry[, "x"])
names(isoCountry1) <- isoCountry[, "X"]
isocountries <- robustSort(isoCountry1)
Expand Down
2 changes: 1 addition & 1 deletion R/regionscode.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ regionscode <- function(mapping = NULL, label = FALSE, strict = TRUE) {
if (ncol(mapping) >= 3) mapping[[1]] <- NULL

# read list of ISO-countries
isCountry <- read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isCountry <- utils::read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isCountry1 <- as.vector(isCountry[, "x"])
names(isCountry1) <- isCountry[, "X"]
isocountries <- robustSort(isCountry1)
Expand Down
20 changes: 11 additions & 9 deletions R/toolAggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ toolAggregateWeighted <- function(x, rel, weight, from, to, dim, wdim, partrel,
}

weightAndWeightSum <- toolZeroWeight(weight, rel, from, to, dim, wdim, partrel, zeroWeight)
weight <- weightAndWeightSum$weight
weight2 <- 1 / (weightAndWeightSum$weightSum + 10^-100)
weight <- weightAndWeightSum$weight # fine resolution
weight2 <- 1 / (weightAndWeightSum$weightSum + 10^-100) # coarse resolution
stopifnot(length(weight) >= length(weight2))

if (mixedAggregation) {
weight2[is.na(weight2)] <- 1
Expand All @@ -233,15 +234,17 @@ toolAggregateWeighted <- function(x, rel, weight, from, to, dim, wdim, partrel,
}

if (setequal(getItems(weight, dim = wdim), getItems(x, dim = dim))) {
# weighted mean aggregation
out <- toolAggregate(x * weight, rel, from = from, to = to, dim = dim, partrel = partrel) * weight2
} else {
# weighted disaggregation
out <- toolAggregate(x * weight2, rel, from = from, to = to, dim = dim, partrel = partrel) * weight
if (zeroWeight == "fix" && !isTRUE(all.equal(sum(out), sum(x)))) {
warning("total sum is not the same after toolAggregate's weighted disaggregation ",
"despite zeroWeight = 'fix', please contact a madrat developer.")
}
}

if (zeroWeight == "fix" && !isTRUE(all.equal(sum(out), sum(x)))) {
warning("total sum is not the same after toolAggregate despite zeroWeight = 'fix', ",
"please contact a madrat developer.")
}

getComment(out) <- c(xComment, paste0("Data aggregated (toolAggregate): ", date()))
return(out)
Expand All @@ -251,8 +254,7 @@ toolZeroWeight <- function(weight, rel, from, to, dim, wdim, partrel, zeroWeight
weightSum <- toolAggregate(weight, rel, from = from, to = to, dim = wdim, partrel = partrel, verbosity = 10)
if (zeroWeight != "allow" && any(weightSum == 0, na.rm = TRUE)) {
if (zeroWeight == "warn") {
warning("Weight sum is 0, so cannot normalize and will return 0 for some ",
"aggregation targets. This changes the total sum of the magpie object! ",
warning("Weight sum is 0, so cannot normalize and will return 0 for some aggregation targets. ",
'If this is really intended set zeroWeight = "allow", or "setNA" to return NA.')
} else if (zeroWeight == "setNA") {
weightSum[weightSum == 0] <- NA
Expand All @@ -263,7 +265,7 @@ toolZeroWeight <- function(weight, rel, from, to, dim, wdim, partrel, zeroWeight
warning("Critical: toolAggregate zeroWeight = fix did not fix the weight! Please contact a madrat developer.")
}
} else {
stop("Weight sum is 0, so cannot normalize. This changes the total sum of the magpie object!")
stop("Weight sum is 0, so cannot normalize weights!")
}
}
return(list(weight = weight, weightSum = weightSum))
Expand Down
4 changes: 2 additions & 2 deletions R/toolCountry2isocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#' @export
toolCountry2isocode <- function(country, warn = TRUE, ignoreCountries = NULL, type = NULL, mapping = NULL) {
if (!is.null(type)) warning("\"type\" argument is deprecated and will be ignored! Don't use it!")
country2iso <- read.csv2(system.file("extdata", "country2iso.csv", package = "madrat"),
row.names = NULL, encoding = "UTF-8")
country2iso <- utils::read.csv2(system.file("extdata", "country2iso.csv", package = "madrat"),
row.names = NULL, encoding = "UTF-8")
country2iso1 <- as.vector(country2iso[, "x"])
names(country2iso1) <- country2iso[, "X"]
mapping <- c(country2iso1, mapping)
Expand Down
6 changes: 3 additions & 3 deletions R/toolCountryFill.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ toolCountryFill <- function(x, fill = NA, no_remove_warning = NULL, overwrite =
countrylist = NULL, ...) {
comment <- getComment(x)
if (is.null(countrylist)) {
isoCountry <- read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
isoCountry <- utils::read.csv2(system.file("extdata", "iso_country.csv", package = "madrat"), row.names = NULL)
countrylist <- as.vector(isoCountry[, "x"])
names(countrylist) <- isoCountry[, "X"]
}
Expand All @@ -57,8 +57,8 @@ toolCountryFill <- function(x, fill = NA, no_remove_warning = NULL, overwrite =
# warn only for countries which were not explicitly mentioned in argument "remove"
countries2warn <- setdiff(additionalCountries, no_remove_warning)
if (length(countries2warn) > 0) {
historicalCountries <- unique(read.csv2(system.file("extdata", "ISOhistorical.csv", package = "madrat"),
stringsAsFactors = FALSE)[["fromISO"]])
historicalCountries <- unique(utils::read.csv2(system.file("extdata", "ISOhistorical.csv", package = "madrat"),
stringsAsFactors = FALSE)[["fromISO"]])
removedHistoricalCountries <- intersect(countries2warn, historicalCountries)
historicalHint <- if (length(removedHistoricalCountries) > 0) {
paste(" - By using madrat::toolISOhistorical the data for the following countries can usually still be used:",
Expand Down
23 changes: 15 additions & 8 deletions R/toolFixWeight.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#' @param weight magclass object containing weights to be used for a weighted
#' (dis)aggregation. The provided weight does not need to be normalized, any
#' number >= 0 is allowed.
#' @param map a data frame where the first column contains coarse resolution items
#' and the second column contains fine resolution items; fine resolution items
#' @param map a 2-column data frame where one column contains coarse resolution items
#' and the other column contains fine resolution items; fine resolution items
#' must match items in weight
#' @param dim which dim to fix (e.g. 1 or 3.2 or "region")
#' @return weight, with weights set to 10^-30 only where otherwise the total
Expand Down Expand Up @@ -44,7 +44,11 @@ toolFixWeight <- function(weight, map, dim) {
dim <- dimCode(dim, weight)
stopifnot(length(dim) == 1,
weight >= 0,
setequal(map[[2]], getItems(weight, dim)))
ncol(map) == 2)
if (!setequal(map[[2]], getItems(weight, dim))) {
map <- map[, 2:1]
}
stopifnot(setequal(map[[2]], getItems(weight, dim)))
originalDimnames <- dimnames(weight)

extramap <- NULL
Expand All @@ -55,17 +59,20 @@ toolFixWeight <- function(weight, map, dim) {
} else {
# merge all subdims into one dim, and replace back at the end using extramap
originalMap2 <- map[[2]]
map[[1]] <- sub(".", "p", map[[1]], fixed = TRUE)
map[[2]] <- sub(".", "p", map[[2]], fixed = TRUE)
extramap <- stats::setNames(nm = map[[2]], originalMap2)
getItems(weight, dim, full = TRUE) <- sub(".", "p", getItems(weight, dim, full = TRUE), fixed = TRUE)
map[[1]] <- gsub(".", "p", map[[1]], fixed = TRUE)
map[[2]] <- gsub(".", "p", map[[2]], fixed = TRUE)
extramap <- originalMap2
names(extramap) <- map[[2]]
getItems(weight, dim, full = TRUE) <- gsub(".", "p", getItems(weight, dim, full = TRUE), fixed = TRUE)
}
dim <- dim + 0.1
stopifnot(dim %in% c(1.1, 2.1, 3.1))
}
stopifnot(dim >= 1, dim < 4)
mainDim <- floor(dim)
map <- stats::setNames(nm = map[[2]], object = map[[1]])
mapnames <- map[[2]]
map <- map[[1]]
names(map) <- mapnames

# append subdim for coarse items according to map
weight <- add_dimension(weight, dim + 0.1, "placeholder_dimname",
Expand Down
2 changes: 1 addition & 1 deletion R/toolGetMapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ toolGetMapping <- function(name, type = NULL, where = NULL,
filetype <- tolower(file_ext(fname))
if (filetype == "csv") {
sep <- if (grepl(pattern = ";", x = readLines(fname, 1))) ";" else ","
return(read.csv(fname, sep = sep, stringsAsFactors = FALSE, comment.char = "*"))
return(utils::read.csv(fname, sep = sep, stringsAsFactors = FALSE, comment.char = "*"))
} else if (filetype == "rda") {
data <- NULL
load(fname)
Expand Down
5 changes: 3 additions & 2 deletions R/toolISOhistorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ toolISOhistorical <- function(m, mapping = NULL, additional_mapping = NULL, over
# mapping of historical countries and regions to the standard ISO-Country-List
# and last year of existence of the historical countries
if (is.null(mapping)) {
mapping <- read.csv2(system.file("extdata", "ISOhistorical.csv", package = "madrat"), stringsAsFactors = FALSE)
mapping <- utils::read.csv2(system.file("extdata", "ISOhistorical.csv", package = "madrat"),
stringsAsFactors = FALSE)
} else if (is.character(mapping)) {
mapping <- read.csv(mapping, sep = ";", as.is = TRUE)
mapping <- utils::read.csv(mapping, sep = ";", as.is = TRUE)
}
# add additional mapping, if provided
if (!is.null(additional_mapping)) {
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.37.2**
R package **madrat**, version **3.37.4**

[![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, Sauer P, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Crawford M, Kreidenweis U, Klein D, Rein P (2026). "madrat: May All Data be Reproducible and Transparent (MADRaT) *." doi:10.5281/zenodo.1115490 <https://doi.org/10.5281/zenodo.1115490>, Version: 3.37.2, <https://github.com/pik-piam/madrat>.
Dietrich J, Sauer P, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Crawford M, Kreidenweis U, Klein D, Rein P (2026). "madrat: May All Data be Reproducible and Transparent (MADRaT) *." doi:10.5281/zenodo.1115490 <https://doi.org/10.5281/zenodo.1115490>. Version: 3.37.4, <https://github.com/pik-piam/madrat>.

A BibTeX entry for LaTeX users is

Expand All @@ -64,9 +64,9 @@ A BibTeX entry for LaTeX users is
title = {madrat: May All Data be Reproducible and Transparent (MADRaT) *},
author = {Jan Philipp Dietrich and Pascal Sauer and Lavinia Baumstark and Stephen Wirth and Anastasis Giannousakis and Renato Rodrigues and Benjamin Leon Bodirsky and Debbora Leip and Michael Crawford and Ulrich Kreidenweis and David Klein and Patrick Rein},
doi = {10.5281/zenodo.1115490},
date = {2026-07-02},
date = {2026-07-15},
year = {2026},
url = {https://github.com/pik-piam/madrat},
note = {Version: 3.37.2},
note = {Version: 3.37.4},
}
```
4 changes: 2 additions & 2 deletions man/toolFixWeight.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion tests/testthat/test-toolAggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ test_that("trivial renaming works with weight", {
expect_silent(toolAggregate(pm, mapping, from = "a", to = "b", dim = 3, weight = weight))
})

test_that("zeroWeight = fix works", {
test_that("zeroWeight = fix works for disaggregation", {
x <- new.magpie(c("A", "B"), fill = 100)
rel <- data.frame(c("A", "A", "B", "B"),
c("A1", "A2", "B1", "B2"))
Expand All @@ -283,6 +283,21 @@ test_that("zeroWeight = fix works", {
expect_true(as.vector(y["B2", , ]) == 0) # and this is also still 0
})

test_that("zeroWeight = fix works for weighted mean aggregation", {
rel <- data.frame(c("A", "A", "B", "B"),
c("A.A1", "A.A2", "B.B1", "B.B2"))
x <- new.magpie(rel[[2]], fill = 100)
weight <- new.magpie(rel[[2]], fill = 0)
weight["B1", , ] <- 1

expect_warning(y <- toolAggregate(x, rel, weight),
"Weight sum is 0")
expect_true(as.vector(y["A", , ]) == 0)

expect_silent(y <- toolAggregate(x, rel, weight, zeroWeight = "fix"))
expect_true(as.vector(y["A", , ]) == 100)
})

test_that("empty cells in a to column do not result in aggregated data", {

empty <- ""
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-toolFixWeight.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ test_that("toolFixWeight works with subdims", {
})

test_that("toolFixWeight works with multi subdims map", {
map <- data.frame(c("X.CC", "X.CC", "X.CC", "X.DD", "X.DD"),
c("X.CC1", "X.CC2", "X.CC3", "X.DD1", "X.DD2"))
map <- data.frame(c("X.CC.U", "X.CC.U", "X.CC.U", "X.DD.U", "X.DD.U"),
c("X.CC1.U", "X.CC2.U", "X.CC3.U", "X.DD1.U", "X.DD2.U"))
weight <- new.magpie(c("A", "B"), map[[2]], c("EEE", "FFF"), fill = 0)
weight["B", "CC3", "EEE"] <- 1

Expand All @@ -58,7 +58,6 @@ test_that("toolFixWeight works with multi subdims map", {
})

test_that("toolFixWeight can handle large objects", {
skip("to save time skip test whether toolFixWeight can handle large objects")
to <- Reduce(x = 1:26, init = NULL, f = function(total, i) {
return(c(total, paste0(LETTERS[i], seq_len(1000 * i))))
})
Expand Down
Loading
Loading