Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix the shared colorbar in `plot_prediction` showing a 0-1 range instead of the data range; it was attached to the first collection on the axis, which is the cartopy coastline artist rather than the data mesh [\#903](https://github.com/mllam/neural-lam/pull/903) @nikhil3495

- Fix `InteractionNet` and `PropagationNet` dimension mismatches when `hidden_dim` differs from `input_dim` [\#836](https://github.com/mllam/neural-lam/pull/836) @Karvy-Singh

- Raise a `ValueError` for `--val_interval` below 1 in `train_model.fit`; `0` previously crashed with a `ZeroDivisionError` inside Lightning after the model was built, and negative values were silently accepted [\#847](https://github.com/mllam/neural-lam/pull/847) @nikhil3495
Expand Down
12 changes: 8 additions & 4 deletions neural_lam/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,7 @@ def plot_prediction(
subplot_kw={"projection": datastore.coords_projection},
)

for ax, da, subtitle in zip(
axes, (da_target, da_prediction), ("Ground Truth", "Prediction")
):
meshes = [
plot_on_axis(
ax=ax,
da=da,
Expand All @@ -696,12 +694,18 @@ def plot_prediction(
boundary_alpha=boundary_alpha,
crop_to_interior=crop_to_interior,
)
for ax, da, subtitle in zip(
axes, (da_target, da_prediction), ("Ground Truth", "Prediction")
)
]

if title:
fig.suptitle(title, size=_TITLE_SIZE)

# Not axes[0].collections[0]: cartopy adds coastlines and borders as
# collections before the mesh, and those have a default 0-1 norm
cbar = fig.colorbar(
axes[0].collections[0],
meshes[0],
ax=axes,
orientation="horizontal",
location="bottom",
Expand Down
49 changes: 45 additions & 4 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

# Third-party
import matplotlib
import matplotlib.collections
import matplotlib.figure
import matplotlib.pyplot as plt
import numpy as np
import pytest
import torch
import xarray as xr
from cartopy import crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes

# First-party
from neural_lam import config as nlconfig
Expand All @@ -33,6 +35,10 @@
TEST_OUTPUT_DIR = Path(__file__).parent / "test_outputs" / "plotting"
TEST_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

# Kept before mock_cartopy_downloads stubs them, for tests needing the real ones
_REAL_GEOAXES_COASTLINES = GeoAxes.coastlines
_REAL_GEOAXES_ADD_FEATURE = GeoAxes.add_feature


@pytest.fixture(scope="session", autouse=True)
def _set_agg_backend():
Expand Down Expand Up @@ -136,10 +142,45 @@ def test_plot_prediction() -> None:
assert len(ground_truth_ax.collections) == 1
assert len(prediction_ax.collections) == 1

assert ground_truth_ax.collections[0].norm.vmin == expected_vmin
assert ground_truth_ax.collections[0].norm.vmax == expected_vmax
assert prediction_ax.collections[0].norm.vmin == expected_vmin
assert prediction_ax.collections[0].norm.vmax == expected_vmax
ground_truth_mesh, prediction_mesh = (
next(
c
for c in ax.collections
if isinstance(c, matplotlib.collections.QuadMesh)
)
for ax in (ground_truth_ax, prediction_ax)
)
assert ground_truth_mesh.norm.vmin == expected_vmin
assert ground_truth_mesh.norm.vmax == expected_vmax
assert prediction_mesh.norm.vmin == expected_vmin
assert prediction_mesh.norm.vmax == expected_vmax


def test_plot_prediction_colorbar_uses_data_range(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The shared colorbar spans ``vrange`` even when coastlines and borders
are drawn, since cartopy adds those as collections before the mesh."""
# Undo the autouse stubs: creating feature artists needs no download,
# only drawing them does, and this test never draws.
monkeypatch.setattr(GeoAxes, "coastlines", _REAL_GEOAXES_COASTLINES)
monkeypatch.setattr(GeoAxes, "add_feature", _REAL_GEOAXES_ADD_FEATURE)

datastore = init_datastore_example("dummydata")
n_grid = datastore.num_grid_points
vmin, vmax = 250.0, 350.0

fig = vis.plot_prediction(
datastore=datastore,
da_prediction=xr.DataArray(np.linspace(vmin, vmax, n_grid)),
da_target=xr.DataArray(np.linspace(vmax, vmin, n_grid)),
vrange=(vmin, vmax),
boundary_alpha=None,
crop_to_interior=False,
)

*_, colorbar_ax = fig.axes
assert colorbar_ax.get_xlim() == pytest.approx((vmin, vmax))


def test_plot_error_map() -> None:
Expand Down
Loading