Skip to content

run.sensitivity.analysis: hoist samples loading outside variables loop (#3859) - #4107

Merged
infotroph merged 4 commits into
PecanProject:developfrom
Anurag-M1:fix/3859-sa-samples-loop-hoist
Oct 26, 2026
Merged

infotroph merged 4 commits into
PecanProject:developfrom
Anurag-M1:fix/3859-sa-samples-loop-hoist

Conversation

@Anurag-M1

@Anurag-M1 Anurag-M1 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Hoists loading of samples.Rdata and sensitivity.samples out of the variables loop in run.sensitivity.analysis(), loading them once into samples before the loop. Also resolves loop variable confusion by renaming the inner parsed expression to parsed_var <- PEcAn.utils::convert.expr(variable).

Motivation and Context

Closes #3859.

Previously, run.sensitivity.analysis() executed load(fname, envir = samples) for both samples.Rdata and sensitivity.samples inside for (variable in variables). In multi-variable sensitivity analyses, this repeatedly re-read the same sample files from disk on every iteration.

Additionally, variables <- PEcAn.utils::convert.expr(variable) was executed inside the loop; this has been renamed to parsed_var <- PEcAn.utils::convert.expr(variable) to avoid confusion with the loop iterator.

Review Time Estimate

[✓] When possible

Types of changes

[✓] Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My change requires a change to the documentation.
  • My name is in the list of CITATION.cff
  • I agree that PEcAn Project may distribute my contribution under any or all of
    • the same license as the existing code,
    • and/or the BSD 3-clause license.
  • I have updated the CHANGELOG.md.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PecanProject#3859)

- In modules/uncertainty/R/run.sensitivity.analysis.R, load samples.Rdata and
  sensitivity.samples once before the variables loop into samples_base, avoiding
  redundant disk reads on every variable iteration.
- Use rlang::env_clone(samples_base) inside the loop to isolate per-variable
  modifications (e.g. Celsius unit conversions).
- Rename the inner variable expression parsing to parsed_var to prevent
  shadowing and overwriting the loop vector variables.
- Support extracting variables from settings$sensitivity.analysis when provided
  as a vector or multiple tags.
- Add unit tests in test-run.sensitivity.analysis.R covering single-load
  verification and multi-variable loop preservation.
- Update CHANGELOG.md and modules/uncertainty/NEWS.md.
Comment on lines +89 to +92
variable <- unlist(
settings$sensitivity.analysis[names(settings$sensitivity.analysis) == "variable"],
use.names = FALSE
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Revert, please. These approaches will diverge if any element has length > 1 or names of its own. It's worth asking whether or not length>1 variable specs make sense and if so whether to enforce that, but that's for a separate PR with its own tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted back to the original loop extraction.

infotroph and others added 2 commits October 21, 2026 09:55
- Revert unlist() variable extraction to original loop in run.sensitivity.analysis()
- Keep samples environment name instead of samples_base and drop env_clone
- Replace mock-based load counter test with multi-variable output verification test
- Update changelog and news entries
@Anurag-M1

Copy link
Copy Markdown
Contributor Author

Thanks for the review @infotroph! I've addressed all the feedback in the latest commit:

  • Reverted unlist() variable extraction back to the original loop.
  • Kept samples as the environment name throughout and dropped rlang::env_clone() since samples is purely read from.
  • Replaced the mock-based load counter test with an end-to-end multi-variable output verification test using write_sa_fixture.
  • Updated CHANGELOG.md and NEWS.md.

Ready for re-review whenever you have a moment!

@infotroph
infotroph enabled auto-merge October 26, 2026 09:02
@infotroph
infotroph added this pull request to the merge queue Oct 26, 2026
Merged via the queue into PecanProject:develop with commit 2b2dde4 Oct 26, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid loading samples for each iteration in variables loop inside run.sensitivity.analysis

2 participants