-
Notifications
You must be signed in to change notification settings - Fork 315
fix(uncertainty): resolve SA median manifest lookup (#3882) #3906
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
Open
man080107
wants to merge
8
commits into
PecanProject:develop
Choose a base branch
from
man080107:fix/3882-sa-median-manifest-lookup
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58855f5
fix(uncertainty): resolve median (q50) drop in manifest-based SA lookup
man080107 451019d
Update test-read_sa_output_median.R
dlebauer 29141f5
fix(tests): correct variable argument and add ncdf4 to Suggests
man080107 b5c859f
fix: use is.na() and which() for median manifest fallback lookup
man080107 eff4cc6
Merge branch 'develop' into fix/3882-sa-median-manifest-lookup
infotroph af5afba
Merge branch 'develop' into fix/3882-sa-median-manifest-lookup
infotroph 090dbec
Merge branch 'develop' into fix/3882-sa-median-manifest-lookup
infotroph e57fde7
Merge branch 'develop' into fix/3882-sa-median-manifest-lookup
infotroph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
modules/uncertainty/tests/testthat/test-read_sa_output_median.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Regression test for the shared-median manifest fallback in read.sa.output() | ||
| # | ||
| # Background: write.sa.configs() writes the median (q50) run as a single shared | ||
| # entry with pft_name = "NA" and trait = "NA". read.sa.output() previously did | ||
| # a strict per-trait/per-PFT lookup, so the median row always failed to match | ||
| # and the 50th-quantile cell was returned as NA. | ||
| # | ||
| # The fix adds a fallback: when quantile == "50" and no exact match is found, | ||
| # look for the shared median entry before giving up. | ||
| # | ||
| # Reference: https://github.com/pecanproject/pecan/issues/<issue-number> | ||
|
|
||
| test_that("read.sa.output resolves median (q50) via shared manifest fallback", { | ||
| withr::with_tempdir({ | ||
| pft <- "temperate.coniferous" | ||
| trait <- "growth_resp_factor" | ||
| yr <- 2004 | ||
|
|
||
| # ---- Build a manifest that matches what write.sa.configs() actually writes ---- | ||
| # One shared median row (pft_name = "NA", trait = "NA") + no trait-specific row | ||
| # for quantile 50. This is the exact situation that triggered the original bug. | ||
| median_run_id <- "SA-median--1" | ||
| manifest <- data.frame( | ||
| type = "Sensitivity", | ||
| pft_name = "NA", | ||
| trait = "NA", | ||
| quantile = "50", | ||
| run_id = median_run_id, | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| write.csv(manifest, "runs_manifest.csv", row.names = FALSE) | ||
|
|
||
| # ---- Stub out the model output directory so read.output() finds a file ---- | ||
| run_outdir <- file.path(getwd(), median_run_id) | ||
| dir.create(run_outdir, recursive = TRUE) | ||
|
|
||
| # Create a minimal NetCDF file with an NPP variable for the target year | ||
| nc_path <- file.path(run_outdir, paste0(yr, ".nc")) | ||
| nc_obj <- ncdf4::nc_create( | ||
| nc_path, | ||
| list(ncdf4::ncvar_def("NPP", "kg m-2 s-1", list(), missval = NA_real_)) | ||
| ) | ||
| ncdf4::ncvar_put(nc_obj, "NPP", 1.23) | ||
| ncdf4::nc_close(nc_obj) | ||
|
|
||
| # ---- Run read.sa.output() ---- | ||
| # Should NOT warn "Run ID invalid or missing" for quantile 50 any more. | ||
| expect_no_warning( | ||
| out <- PEcAn.uncertainty::read.sa.output( | ||
| traits = trait, | ||
| quantiles = "50", | ||
| pecandir = getwd(), | ||
| outdir = getwd(), | ||
| pft.name = pft, | ||
| start.year = yr, | ||
| end.year = yr, | ||
| variable = PEcAn.utils::convert.expr("NPP") | ||
| ), | ||
| regexp = "Run ID invalid or missing" | ||
| ) | ||
|
|
||
| # The median cell must not be NA | ||
| expect_false( | ||
| is.na(out[["50", trait]]), | ||
| label = "median (q50) output should not be NA" | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
|
|
||
| test_that("read.sa.output still warns when no median fallback row exists", { | ||
| withr::with_tempdir({ | ||
| # Manifest with quantile 50 row that has the wrong pft_name (not "NA"), | ||
| # meaning neither exact match nor shared-median fallback can be found. | ||
| manifest <- data.frame( | ||
| type = "Sensitivity", | ||
| pft_name = "some.other.pft", | ||
| trait = "NA", | ||
| quantile = "50", | ||
| run_id = "SA-median--1", | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| write.csv(manifest, "runs_manifest.csv", row.names = FALSE) | ||
|
|
||
| expect_warning( | ||
| PEcAn.uncertainty::read.sa.output( | ||
| traits = "growth_resp_factor", | ||
| quantiles = "50", | ||
| pecandir = getwd(), | ||
| outdir = getwd(), | ||
| pft.name = "temperate.coniferous", | ||
| start.year = 2004, | ||
| end.year = 2004, | ||
| variable = PEcAn.utils::convert.expr("NPP") | ||
| ), | ||
| regexp = "No run found in manifest" | ||
| ) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.