Fix pattern-scaling cache-validator variable-suffix mismatch - #94
Merged
Conversation
MeteorPatternScaling saves per-variable caches under
`cmip6-{model}-aer-{variable}` (e.g. `cmip6-NorESM2-MM-aer-tas`), but
validate_pattern_scaling_cache constructed its expected name without
the variable suffix (`cmip6-NorESM2-MM-aer`). The comparison therefore
returned "invalid" for a cache that then loaded successfully by
filename inside MeteorPatternScaling.__init__, and the intervening
"invalid" branch in _train_pattern_scaling ran
prepare_pattern_scaling_training_data(), which loaded CMIP6 data over
the network and did substantial data assembly whose result was then
discarded.
Adds an optional `variable=` argument to validate_pattern_scaling_cache
and threads it through _train_pattern_scaling. The legacy default
(variable=None) preserves the no-suffix behavior for callers that
don't use per-variable caches.
Regression test in test_caching pins that a per-variable cache round-trips
only when variable= is supplied.
Wins on gen_global_ts (N=100 for illustration): 45.7 s -> 26.9 s. Every
generation call previously incurred ~13-18 s of unused training-data
prep; this makes it a rounding error. Effect compounds across workloads.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…validation
Companion to the variable-suffix fix. Even with the name comparison
working, `validate_pattern_scaling_cache` still demanded that a
per-variable cache carry ALL of `self.flds` in its `patternflds` (e.g.
both tas and pr for a data getter configured with `flds=['tas', 'pr']`),
but the pkl `MeteorPatternScaling` saves only carries the one variable
that pattern was fitted for. The check rejected the cache with
"Missing required fields: {'pr'}" for a tas-only pkl.
When `variable=` is supplied, narrow `expected_vars` to just that
variable so single-variable caches round-trip. Legacy behavior
(`variable=None`) is unchanged.
Regression test in test_caching updated: per-variable cache pkl now
carries only its own variable in `patternflds` (matching real caches),
proving both halves of the validator's variable handling.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
maritsandstad
approved these changes
Jul 30, 2026
maritsandstad
left a comment
Collaborator
There was a problem hiding this comment.
Agreed, I think this can also just go in.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A latent cache-validation bug in
Cmip6MeteorDataGetter.validate_pattern_scaling_cachewas causing every call toMeteorInterface.train()— even with a fully-populated cache — to runprepare_pattern_scaling_training_data, which fetches and assembles CMIP6 data over the network, and then immediately discard the result.Root cause
MeteorPatternScalingsaves each variable's cache under the namecmip6-{model}-aer-{variable}(e.g.cmip6-NorESM2-MM-aer-tas). The outer validator, however, built its expected name ascmip6-{model}-aer— missing the variable suffix — and returnedis_valid=Falsefor a cache that then loaded successfully by filename insideMeteorPatternScaling.__init__.The two disagreeing cache checks meant:
_train_pattern_scalingenters the cache-miss branch, callsprepare_pattern_scaling_training_data(...), loads CICERO forcing data, etc.MeteorPatternScaling(cache_dir=...)➜ finds the pkl by filename ➜ loads from cache and returns ➜ discards everything from step 1.Fix
variable=argument tovalidate_pattern_scaling_cache; when supplied it's appended to the expected name.variable=variablethrough_train_pattern_scaling.variable=None) preserves the no-suffix behavior for callers that don't use per-variable caches.Impact
Effect scales with how often
train()is called — for our profiling workloads that's every generation. Concrete measurement on the NorESM2-MM cache-hit path withgen_global_tsat N=100:18.7 s saved per generation call (removes ~4 s of
make_meteor_training_data_compositein_load_transform_reference-adjacent paths and ~15 s inprepare_pattern_scaling_training_data).Correctness
test_caching:variable=) still rejects per-variable caches — locking in the bug's symptom.variable="tas"accepts the same cache — proves the fix.test_cmip6_meteor_data_getter.py+test_meteor_interface.pytests still pass.Test plan
pytest tests/unit/test_cmip6_meteor_data_getter.py::test_cachingpytest tests/unit/test_meteor_interface.pyemulator.train()— should now log "Using cached pattern scaling model" without a CMIP6 data reload.🤖 Generated with Claude Code