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

Implement explicit scenario choice across package. #633

Merged
merged 3 commits into from
Feb 21, 2025

Conversation

johanneskoch94
Copy link
Member

@johanneskoch94 johanneskoch94 commented Feb 14, 2025

  • Introduce a scenario argument to calcCapital, calcFeDemand, calcFeDemandBuildings, calcFloorspace, calcTheil, and calcEmiTarget. The larger picture is the attempt to step away from a default set of scenarios. The choice of scenarios should be explicit. In the case of the input data pipeline, it is made in fullREMIND and then passed on to the different calcOutput functions. Hopefully this will help us control/drop/introduce new scenarios more easily in the future.
  • Move SSP2_NAV_all duplication to mrindustry.
  • Set NAs in CO2 prices from ExpertGuess to 0.
  • Drop unused sources argument in calcEmiTarget.
  • Drop unused scenario_proj argument in calcFE.
  • General refactoring.

!! Still waiting for my input data test to run through (/p/tmp/jokoch/pre-processing, output at /p/projects/rd3mod/inputdata/output_1.27/rev7.22johannes_test7_62eff8f7_remind) but tagged in the reviewers so that the revision can start.

Connected to this PR in mrindustry.


scenarios <- list(SSPs = paste0("SSP", 1:5),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you talk about read- and convertStationaryy with @fbenke-pik ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ist there a something I should know @fbenke-pik ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be removed at some point, but for now we need to keep it.

@johanneskoch94
Copy link
Member Author

Side note:
I see this warning popping up at in multiple calculations. It seems to come from something happening in calcOutput. How do I get rid of it?

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", or "setNA" to return NA.

R/calcFEShares.R Outdated
fe_demand <- calcOutput("FEdemand", aggregate = FALSE)[, , "SSP1"]
fe_demand <- calcOutput("FEdemand", scenario = "SSP1", aggregate = FALSE)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disables the madrat cache for this call. See pik-piam/mrindustry#30 (comment) and pik-piam/mrindustry#30 (comment). Should be fixed for the benefit of run times and disk space. And is probably an issue across related changes in other packages.

Copy link
Member Author

@johanneskoch94 johanneskoch94 Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean it doesn't use a potential cache, in which the SSP1 scenario could be available right? I don't see a way around this without keeping a "default" set of scenarios, which is exactly what we want to get rid of. (Do you have any ideas?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calcFEShares() is called by fullREMIND(), and that knows what scenarios are going to be calculated. Just pass the scenario [sic] parameter passed to e.g. calcFEdemand() to calcFEshares() too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Thanks, will implement this right away.

Copy link
Contributor

@fbenke-pik fbenke-pik Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out, Michaja.

I just tested and noticed that madrat even does not recognize when you pass the same scenarios but in different order and will recalculate (so my point is we need to be aware of these things when passing additional arguments to heavy calc functions).

Introduce a scenario argument to calcCapital, calcFeDemand, calcFeDemandBuildings, calcFloorspace, calcTheil, and calcEmiTarget. The larger picture is the attempt to step away from a default set of scenarios. The choice of scenarios should be explicit. In the case of the input data pipeline, it is made in fullREMIND and then passed on to the different calcOutput functions. Hopefully this will help us control/drop/introduce new scenarios more easily in the future.
Move SSP2_NAV_all duplication to mrindustry.
Set NAs in CO2 prices from ExpertGuess to 0.
Drop unused sources argument in calcEmiTarget.
Drop unused scenario_proj argument in calcFE.
General refactoring.
Copy link

@robertsalzwedel robertsalzwedel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johanneskoch94 asked me to approve this as a technicality.

@johanneskoch94 johanneskoch94 merged commit be1f255 into pik-piam:master Feb 21, 2025
1 check passed
#' @param x MAgPIE object returned from readStationary
#' @param subset A string, or vector of string designating the entries for the scenario dimension. If possible,
#' passed to [mrdrivers::calcGDP()]. Unspecified scenarios receive SSP2 data.
convertStationary <- function(x, subset) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johanneskoch94 moving read and convert into the same file is a big no for mrremind. Please don't do that.

@@ -9,7 +9,6 @@
#' pre final energy category \item \code{subsidies_bulk}: subsidy rate per final
#' energy category \item \code{energy}: final energy quantities per category }
#' @return magpie object of the IIASA_subs_taxes data
#' @author Christoph Bertram
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHy did you remove the Author here? This is useful and important info, even if the person is no longer at PIK.

ceds <- ceds[, pmin(getYears(x), max(getYears(ceds))), ]
getYears(ceds) <- getYears(x)

return(list(
Copy link
Contributor

@fbenke-pik fbenke-pik Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agreed as a team to not use that rule and switched off that linter. Therefore, I don't think it makes sense to go through legacy code to change it. Probably not worth changing it back now, either. So this is just a remark for future changes.

#' @description The capacity targets (GW) at regional level are produced from two different databases-
#' UNFCCC_NDC database, an update of the Rogelj 2017 paper (see readme in inputdata), and REN21 Global Renewables report
#' The UNFCCC_NDC capacity targets are further broken down to conditional and unconditional targets.
#' @author Aman Malik, Oliver Richters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please bring back all the @author tags. They are useful to evaluate who to ask for help and if the function is rather new or legacy code.

#' a <- convertIIASA_subs_taxes(x)
#' }
#'
convertIIASA_subs_taxes <- function(x, subtype) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be brought back as an individual file

@@ -9,59 +9,26 @@
#' described in the article. The country codes are the World Bank codes.
#'
#' @return magpie object of the Gini data
#' @author Bjoern Soergel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please bring back author

@fbenke-pik
Copy link
Contributor

fbenke-pik commented Feb 28, 2025

There are several files with only stylistic changes. In future PRs, please make a separate PR for these. When doing complex refactorings, as in calcCapTarget, readExpertGuess, readUNFCCC_NDC, run inputdata generation with those refactorings (and no expected changes in generated data) and ensure that all the inputdata is unchanged using madrat::compareData. I know you ran compareData for this PR as well, but the problem is that you mix actual data changes with refactorings that do not change data and therefore you cannot tell for sure if you did not break anything using compareData, unless you did advanced analysis per file.

@fbenke-pik
Copy link
Contributor

Side note: I see this warning popping up at in multiple calculations. It seems to come from something happening in calcOutput. How do I get rid of it?

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", or "setNA" to return NA.

This is a known issue, occurs when running some aggregations on magclass objects where NAs in weights (or possibly other reasons) cause the sum to be not equal to its parts. Needs to be addressed per case and is a task in the RSE backlog.

#' @importFrom readxl read_xlsx
#' @importFrom dplyr select
#' Emissions_2023_cond (or 2018/2021/2022 or uncond) for Emissions targets
#' @param subset A string (or vector of strings) designating the scenario(s) to be returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a remark that it is needed for the convert function, as it is not used directly in the read function

}


convertEdgeBuildings <- function(x, subtype, subset) {
Copy link
Contributor

@fbenke-pik fbenke-pik Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readEdgeBuildings does not have the param 'subset`. Does it work then? If so, this is inconsistent with readUNFCCCC_NDR.R where you add the param to the read function (probably because you want to use it in the convert function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants