Fix: plotting bounds calculation for multidimensional metrics dataarr… - #575
Fix: plotting bounds calculation for multidimensional metrics dataarr…#575Debadri-das wants to merge 2 commits into
Conversation
…ays (fixes mllam#520) Handles cases where predictions have ensemble dimensions or >3 dims, preventing a crash when calculating plotting bounds. Co-authored-by: GitHub Copilot
|
It fixes only the per-variable plotting-bound reduction in ARModel.plot_examples() (var_vmin / var_vmax), where the current code assumes deterministic (pred_steps, grid_index, feature) slices and can produce incorrectly shaped extrema once an ensemble dimension is present. |
|
@sadamov @joeloskarsson would you please review this? |
There was a problem hiding this comment.
This will indeed become an issue once we move to probabilistic modelling. But this is currently not a bug in main, as main does not support prob. modelling at all.
The right time to land both the fix and its test is alongside your #521 PR, which updates unroll_prediction() to handle 4D init_states -- at that point the test is motivated by real code, and the fix can be verified end-to-end.
Describe your changes
This PR fixes an ensemble-handling bug in the plotting path of
ARModel.plot_examples()that becomes visible once probabilistic / ensemble predictions are passed through the model evaluation pipeline.Summary of the problem
plot_examples() currently computes per-variable plotting bounds using:
This logic is valid only when each example slice has deterministic shape:
pred_slice:(pred_steps, num_grid_nodes, d_f)target_slice:(pred_steps, num_grid_nodes, d_f)In that deterministic case,
flatten(0, 1)collapses(time, grid)into one axis, and the reduction overdim=0correctly returns a(d_f,)vector of per-variable extrema.However, after ensemble/probabilistic support is introduced, the prediction tensor can carry an additional ensemble axis. In that case the reduction logic is no longer shape-correct.
For example, if
pred_slicehas shape:(ensemble_member, pred_steps, num_grid_nodes, d_f)then:
produces shape:
(ensemble_member * pred_steps, num_grid_nodes, d_f)and:
returns shape:
(num_grid_nodes, d_f)instead of the expected:
(d_f,)This breaks the contract expected by the downstream plotting code, where
var_vmin/var_vmaxare zipped as one(vmin, vmax)pair per state variable. At best this produces semantically incorrect bounds; at worst it causes shape mismatch failures in the plotting routine.What this PR changes
This PR updates the resampling / plotting-range calculation in
ARModel.plot_examples()so that per-variable extrema are reduced across all non-feature dimensions, not only across the deterministic(time, grid)case.Concretely, the fix makes the vrange computation robust for both:
The intended invariant is:
var_vmin.shape == (d_f,)var_vmax.shape == (d_f,)This preserves the existing plotting interface and ensures
vis.plot_prediction(...)continues to receive one valid color range per state variable.Why this fix matters
This is a small but important follow-up for the ensemble-support work around issue
#520.The codebase is moving toward probabilistic verification workflows and ensemble-aware tensor/DataArray conversion. Once that support lands, the plotting path must also remain stable during validation/test-time visualization. Otherwise, users can successfully produce ensemble predictions but still hit failures when generating example plots or GIFs.
Dependencies
No new dependencies are required.
This change only uses existing project dependencies:
torchnumpymatplotlib(indirectly via the existing plotting path)Issue Link
closes #520
Type of change
Checklist before requesting a review
pullwith--rebaseoption if possible).Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
reflecting type of change (add section where missing):
Checklist for assignee