Skip to content

Fix pattern-scaling cache-validator variable-suffix mismatch - #94

Merged
maritsandstad merged 2 commits into
basefrom
fix/pattern-scaling-cache-validator
Jul 31, 2026
Merged

Fix pattern-scaling cache-validator variable-suffix mismatch#94
maritsandstad merged 2 commits into
basefrom
fix/pattern-scaling-cache-validator

Conversation

@benmsanderson

Copy link
Copy Markdown
Owner

Summary

A latent cache-validation bug in Cmip6MeteorDataGetter.validate_pattern_scaling_cache was causing every call to MeteorInterface.train() — even with a fully-populated cache — to run prepare_pattern_scaling_training_data, which fetches and assembles CMIP6 data over the network, and then immediately discard the result.

Root cause

MeteorPatternScaling saves each variable's cache under the name cmip6-{model}-aer-{variable} (e.g. cmip6-NorESM2-MM-aer-tas). The outer validator, however, built its expected name as cmip6-{model}-aer — missing the variable suffix — and returned is_valid=False for a cache that then loaded successfully by filename inside MeteorPatternScaling.__init__.

The two disagreeing cache checks meant:

  1. Outer validator ➜ "invalid" ➜ _train_pattern_scaling enters the cache-miss branch, calls prepare_pattern_scaling_training_data(...), loads CICERO forcing data, etc.
  2. MeteorPatternScaling(cache_dir=...) ➜ finds the pkl by filename ➜ loads from cache and returns ➜ discards everything from step 1.

Fix

  • Add optional variable= argument to validate_pattern_scaling_cache; when supplied it's appended to the expected name.
  • Thread variable=variable through _train_pattern_scaling.
  • Legacy default (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 with gen_global_ts at N=100:

Wall (s)
Before 45.7
After 26.9

18.7 s saved per generation call (removes ~4 s of make_meteor_training_data_composite in _load_transform_reference-adjacent paths and ~15 s in prepare_pattern_scaling_training_data).

Correctness

  • New assertions in test_caching:
    • Legacy call (no variable=) still rejects per-variable caches — locking in the bug's symptom.
    • Call with variable="tas" accepts the same cache — proves the fix.
  • All 31 existing test_cmip6_meteor_data_getter.py + test_meteor_interface.py tests still pass.

Test plan

  • pytest tests/unit/test_cmip6_meteor_data_getter.py::test_caching
  • pytest tests/unit/test_meteor_interface.py
  • Manual: train an emulator, delete-and-restore its noise pkl only, re-run emulator.train() — should now log "Using cached pattern scaling model" without a CMIP6 data reload.

🤖 Generated with Claude Code

benmsanderson and others added 2 commits July 19, 2026 01:03
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 maritsandstad left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Agreed, I think this can also just go in.

@maritsandstad
maritsandstad merged commit d001ea0 into base Jul 31, 2026
32 checks passed
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.

2 participants