Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toolAggregate warning #203

Merged
merged 6 commits into from
Apr 23, 2024
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
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ValidationKey: '7518273'
ValidationKey: '61488500'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: unable to verify current time
AutocreateReadme: yes
allowLinterWarnings: no
enforceVersionUpdate: no
10 changes: 7 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -36,7 +35,7 @@ jobs:
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9

Expand All @@ -49,6 +48,11 @@ jobs:
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)

- name: Verify that lucode2::buildLibrary was successful
if: github.event_name == 'pull_request'
shell: Rscript {0}
run: lucode2:::isVersionUpdated()

- name: Checks
shell: Rscript {0}
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9025
rev: v0.4.0
hooks:
- id: parsable-R
- id: deps-in-desc
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.8.1
date-released: '2024-01-11'
version: 3.10.0
date-released: '2024-04-22'
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
8 changes: 4 additions & 4 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.8.1
Date: 2024-01-11
Version: 3.10.0
Date: 2024-04-22
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -33,7 +33,7 @@ Depends:
Imports:
callr,
digest,
igraph (>= 1.0.1),
igraph (>= 2.0.0),
Matrix,
methods,
pkgload,
Expand All @@ -57,4 +57,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: no
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,installed.packages)
importFrom(utils,modifyList)
importFrom(utils,object.size)
importFrom(utils,packageDescription)
importFrom(utils,person)
importFrom(utils,read.csv)
Expand Down
2 changes: 1 addition & 1 deletion R/getMadratInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ getMadratInfo <- function(graph = NULL, cutoff = 5, extended = FALSE, ...) {

message("\n.:: Check for sub-structures (tools ignored) ::.")
fullfunc <- grep("^full", attr(igraph::V(ggraphNoTools), "names"), value = TRUE)
greduced <- igraph::delete.vertices(ggraphNoTools, fullfunc)
greduced <- igraph::delete_vertices(ggraphNoTools, fullfunc)
comp <- igraph::components(greduced)
out$sub_structures <- writeCommunities(comp$membership, # nolint
what = "independent calculations cluster",
Expand Down
35 changes: 22 additions & 13 deletions R/toolAggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@
#' will lead to summation.
#' @param verbosity Verbosity level of messages coming from the function: -1 = error,
#' 0 = warning, 1 = note, 2 = additional information, >2 = no message
#' @param zeroWeight Describes how a weight sum of 0 for a category/aggregation target should be treated.
#' "allow" means it is accepted (dangerous), "warn" throws a warning, "stop" throws an error.
#' @return the aggregated data in magclass format
#' @author Jan Philipp Dietrich, Ulrich Kreidenweis
#' @export
#' @importFrom magclass wrap ndata fulldim clean_magpie mselect setCells getCells mbind
#' @importFrom magclass setComment getNames getNames<- as.array
#' @importFrom magclass is.magpie getComment getComment<- dimCode getYears getYears<-
#' @importFrom magclass getDim getSets getSets<- as.magpie getItems collapseNames
#' @importFrom utils object.size
#' @importFrom Matrix Matrix t rowSums
#' @importFrom withr local_options
#' @seealso \code{\link{calcOutput}}
Expand All @@ -79,9 +80,9 @@
#' # create example mapping
#' p <- magclass::maxample("pop")
#' mapping <- data.frame(from = magclass::getItems(p, dim = 1.1),
#' region = rep(c("REG1", "REG2"), 5),
#' global = "GLO")
#' mapping
#' region = rep(c("REG1", "REG2"), 5),
#' global = "GLO")
#' print(mapping)
#'
#' # run aggregation
#' toolAggregate(p, mapping)
Expand All @@ -91,7 +92,7 @@
#' toolAggregate(p, mapping, to = "region+global")
#'
toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1, wdim = NULL, partrel = FALSE, # nolint
negative_weight = "warn", mixed_aggregation = FALSE, verbosity = 1) { # nolint
negative_weight = "warn", mixed_aggregation = FALSE, verbosity = 1, zeroWeight = "warn") { # nolint

if (!is.magpie(x)) stop("Input is not a MAgPIE object, x has to be a MAgPIE object!")

Expand Down Expand Up @@ -167,7 +168,7 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
regions <- as.character(unique(rel[, to]))
countries <- as.character(unique(rel[, from]))
m <- Matrix(data = 0, nrow = length(regions), ncol = length(countries),
dimnames = list(regions = regions, countries = countries))
dimnames = list(regions = regions, countries = countries))
m[cbind(match(rel[, to], rownames(m)), match(rel[, from], colnames(m)))] <- 1
if (is.numeric(to)) to <- dimnames(rel)[[2]][to]
if (is.numeric(from)) from <- dimnames(rel)[[2]][from]
Expand Down Expand Up @@ -216,7 +217,7 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1

if (is.null(wdim)) {
wdim <- union(getDim(rownames(rel), weight, fullmatch = TRUE),
getDim(colnames(rel), weight, fullmatch = TRUE))
getDim(colnames(rel), weight, fullmatch = TRUE))
# wdim must be in same main dimension as dim
wdim <- wdim[floor(wdim) == floor(dim)]
}
Expand Down Expand Up @@ -249,6 +250,15 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
}
}
tmp <- toolAggregate(weight, rel, from = from, to = to, dim = wdim, partrel = partrel, verbosity = 10)
if (zeroWeight != "allow" && any(tmp == 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! ",
'If this is really intended set zeroWeight = "allow".')
} else {
stop("Weight sum is 0, so cannot normalize. This changes the total sum of the magpie object!")
}
}
weight2 <- 1 / (tmp + 10^-100)
if (mixed_aggregation) {
weight2[is.na(weight2)] <- 1
Expand Down Expand Up @@ -288,13 +298,13 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
maxdim <- nchar(gsub("[^\\.]", "", names[1])) + 1

search <- paste0("^(", paste(rep("[^\\.]*\\.", subdim - 1), collapse = ""),
")([^\\.]*)(", paste(rep("\\.[^\\.]*", maxdim - subdim), collapse = ""), ")$")
")([^\\.]*)(", paste(rep("\\.[^\\.]*", maxdim - subdim), collapse = ""), ")$")
onlynames <- unique(sub(search, "\\2", names))

if (!setequal(colnames(rel), onlynames)) {
if (!setequal(rownames(rel), onlynames)) {
stop("The provided mapping contains entries which could not be found in the data: ",
paste(setdiff(colnames(rel), onlynames), collapse = ", "))
paste(setdiff(colnames(rel), onlynames), collapse = ", "))
} else {
rel <- t(rel)
}
Expand All @@ -312,8 +322,7 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
cnames <- .tmp(add, colnames(rel))
rnames <- .tmp(add, rownames(rel))

newRel <- Matrix(0, nrow = length(rnames), ncol = length(cnames),
dimnames = list(rnames, cnames))
newRel <- Matrix(0, nrow = length(rnames), ncol = length(cnames), dimnames = list(rnames, cnames))

for (i in seq_along(additions)) {
newRel[seq_len(nrow(rel)) + (i - 1) * nrow(rel), seq_len(ncol(rel)) + (i - 1) * ncol(rel)] <- rel
Expand All @@ -327,7 +336,7 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
if (dim(x)[dim] != dim(rel)[2]) {
if (dim(x)[dim] != dim(rel)[1]) {
stop("Relation matrix has in both dimensions a different number of entries (",
dim(rel)[1], ", ", dim(rel)[2], ") than x has cells (", dim(x)[dim], ")!")
dim(rel)[1], ", ", dim(rel)[2], ") than x has cells (", dim(x)[dim], ")!")
} else {
rel <- t(rel)
}
Expand Down Expand Up @@ -378,7 +387,7 @@ toolAggregate <- function(x, rel, weight = NULL, from = NULL, to = NULL, dim = 1
# Compute region vector for outputs after aggregation via sending
# factor values through the relation matrix
regOut <- factor(as.vector(round(rel %*% as.numeric(regionList) /
(rel %*% rep(1, dim(rel)[2])))))
(rel %*% rep(1, dim(rel)[2])))))
levels(regOut) <- levels(regionList)
} else {
stop("Missing dimnames for aggregated dimension")
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.8.1**
R package **madrat**, version **3.10.0**

[![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 (URL: https://doi.org/10.5281/zenodo.1115490), R package version 3.8.1, <URL: 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 <https://doi.org/10.5281/zenodo.1115490>, R package version 3.10.0, <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.8.1},
doi = {10.5281/zenodo.1115490},
note = {R package version 3.10.0},
url = {https://github.com/pik-piam/madrat},
doi = {10.5281/zenodo.1115490},
}
```
26 changes: 26 additions & 0 deletions man/madrat-package.Rd

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

12 changes: 8 additions & 4 deletions man/toolAggregate.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-toolAggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,11 @@ test_that("Edge cases work", {
expect_silent(b <- toolAggregate(a, rel, weight = a))
expect_setequal(getCells(b), c("NLD", "BEL", "LUX"))
})

test_that("columns with only zeros in weight produce a warning", {
weight <- pm
weight[, , ] <- 0
expect_warning(toolAggregate(pm, rel, weight = weight), "Weight sum is 0")
expect_error(toolAggregate(pm, rel, weight = weight, zeroWeight = "stop"), "Weight sum is 0")
expect_silent(toolAggregate(pm, rel, weight = weight, zeroWeight = "allow"))
})