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
10 changes: 5 additions & 5 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- uses: actions/setup-python@v5
- uses: actions/setup-python@v7
with:
python-version: "3.x"

Expand All @@ -25,7 +25,7 @@ jobs:
python -m build

- name: Upload distributions
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: release-dists
path: dist/
Expand All @@ -48,7 +48,7 @@ jobs:

steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: release-dists
path: dist/
Expand All @@ -74,7 +74,7 @@ jobs:

steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: release-dists
path: dist/
Expand Down
18 changes: 10 additions & 8 deletions .github/workflows/run-pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Set up Python env with uv
uses: astral-sh/setup-uv@v4
- name: "Set up Python"
uses: actions/setup-python@v7
with:
version: "0.5.6"
enable-cache: true
python-version: ${{ matrix.python-version }}

- name: "Set up Python"
uses: actions/setup-python@v5
- name: Set up Python env with uv
uses: astral-sh/setup-uv@v9.0.0
with:
version: "0.12.3"
python-version: ${{ matrix.python-version }}
enable-cache: true
cache-suffix: ${{ matrix.python-version }}

- name: Install the project
run: uv sync --all-extras --all-groups
Expand Down
18 changes: 10 additions & 8 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Set up Python env with uv
uses: astral-sh/setup-uv@v4
- name: "Set up Python"
uses: actions/setup-python@v7
with:
version: "0.5.6"
enable-cache: true
python-version: ${{ matrix.python-version }}

- name: "Set up Python"
uses: actions/setup-python@v5
- name: Set up Python env with uv
uses: astral-sh/setup-uv@v9.0.0
with:
version: "0.12.3"
python-version: ${{ matrix.python-version }}
enable-cache: true
cache-suffix: ${{ matrix.python-version }}

- name: Install the project
run: uv sync --all-extras --all-groups
Expand Down
25 changes: 13 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,55 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Scientific/Engineering :: Atmospheric Science",
]
requires-python = ">=3.11,<3.14"
requires-python = ">=3.12,<3.15"
dependencies = [
"dacite>=1.8.1",
"gcsfs>=2024.12.0",
"geopandas>=1.0.1",
"joblib>=1.4.2",
"numpy>=2.2.0",
"pandas>=2.2.3",
"pandas>=2.2.3,<3",
"pooch>=1.8.2",
"pyarrow>=19.0.1",
"pyyaml>=6.0.2",
"regionmask>=0.13.0",
"s3fs>=2024.12.0",
"scores>=2.4.0",
"shapely>=2.0",
"tqdm>=4.67.1",
"xarray[io]>=2025.7.1",
"zarr>=3.0.9,<4",
"kerchunk[dev]",
"kerchunk>=0.2.10",
"fastparquet>=2024.11.0",
"flox>=0.10.0",
"click>=8.1.8",
"polars>=1.32.1",
"eccodes==2.42.0",
"frozenlist==1.5.0",
"pyogrio==0.10.0",
"scikit-image>=0.25.2",
"cartopy==0.24.1",
"numba>=0.63.1",
"numba>=0.66.0",
"sparse>=0.17.0",
"icechunk>=1.1.14",
"icechunk>=2.1.2",
"dask[complete]>=2025.1.0",
"distributed>=2025.1.0",
"lazy-loader>=0.4",
]

[project.optional-dependencies]
data-prep = [
# No cp314 wheels are published; installing on 3.14 would build from
# source and require system GEOS and PROJ.
"cartopy>=0.24.1; python_version < '3.14'",
"fsspec>=2024.12.0",
"matplotlib>=3.10.0",
"obstore>=0.8.2",
"scipy>=1.13",
"seaborn>=0.13.2",
"ujson>=5.10.0",
"virtualizarr==2.1.2",
"virtualizarr>=2.7.3",
"dask[complete]>=2025.1.0",
"distributed>=2025.1.0",
]
Expand Down Expand Up @@ -117,7 +119,6 @@ ewb = "extremeweatherbench.evaluate_cli:cli_runner"

[tool.uv.sources]
extremeweatherbench = { workspace = true }
kerchunk = { git = "https://github.com/fsspec/kerchunk" }

[tool.ruff]
line-length = 88
Expand Down
54 changes: 41 additions & 13 deletions src/extremeweatherbench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import pathlib
from typing import Any, Callable, Literal, Optional, Sequence, Union

import cartopy.io.shapereader as shpreader
import geopandas as gpd
import numpy as np
import numpy.typing as npt
import pandas as pd # type: ignore[import-untyped]
import pooch
import regionmask
import shapely
import sparse
Expand All @@ -24,6 +25,13 @@

logger = logging.getLogger(__name__)

# Natural Earth vector data has been hosted on S3 since 2021; see
# https://github.com/nvkelso/natural-earth-vector/issues/445
NATURAL_EARTH_URL = (
"https://naturalearth.s3.amazonaws.com/{resolution}_{category}/"
"ne_{resolution}_{name}.zip"
)

operators = {
">": operator.gt,
">=": operator.ge,
Expand Down Expand Up @@ -767,6 +775,35 @@ def reduce_dataarray(
raise TypeError(f"method must be str or callable, got {type(method)}")


def load_natural_earth_geometries(
name: str,
resolution: Literal["10m", "50m", "110m"] = "50m",
category: str = "physical",
) -> list[shapely.geometry.base.BaseGeometry]:
"""Download and read a Natural Earth vector layer.

The zipped shapefile is cached on disk by pooch, so repeat calls within and
across sessions do not re-download it.

Args:
name: Natural Earth layer name, e.g. 'land', 'lakes', or 'ocean'.
resolution: Natural Earth resolution ('10m', '50m', or '110m').
Defaults to '50m'.
category: Natural Earth category. Defaults to 'physical'.

Returns:
The layer's geometries in EPSG:4326.
"""
path = pooch.retrieve(
url=NATURAL_EARTH_URL.format(
resolution=resolution, category=category, name=name
),
known_hash=None,
path=pooch.os_cache("extremeweatherbench"),
)
return list(gpd.read_file(f"zip://{path}").geometry)


def load_land_geometry(
resolution: Literal["10m", "50m", "110m"] = "50m",
) -> shapely.geometry.Polygon:
Expand All @@ -780,18 +817,12 @@ def load_land_geometry(
The land geometry as a shapely Polygon with lakes and
ocean-connected water bodies (bays, estuaries, seas) excluded.
"""
land = shpreader.natural_earth(
category="physical", name="land", resolution=resolution
)
land_geoms = list(shpreader.Reader(land).geometries())
land_geoms = load_natural_earth_geometries("land", resolution=resolution)
land_union = shapely.ops.unary_union(land_geoms)

# Exclude lakes to avoid false landfall detections
try:
lakes = shpreader.natural_earth(
category="physical", name="lakes", resolution=resolution
)
lake_geoms = list(shpreader.Reader(lakes).geometries())
lake_geoms = load_natural_earth_geometries("lakes", resolution=resolution)
if lake_geoms:
lakes_union = shapely.ops.unary_union(lake_geoms)
land_union = land_union.difference(lakes_union)
Expand Down Expand Up @@ -820,10 +851,7 @@ def load_ocean_geometry(
Returns:
The ocean geometry as a unified shapely Polygon.
"""
ocean = shpreader.natural_earth(
category="physical", name="ocean", resolution=resolution
)
ocean_geoms = list(shpreader.Reader(ocean).geometries())
ocean_geoms = load_natural_earth_geometries("ocean", resolution=resolution)
return shapely.ops.unary_union(ocean_geoms)


Expand Down
14 changes: 10 additions & 4 deletions tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sparse
import xarray as xr

from extremeweatherbench import inputs
from extremeweatherbench import inputs, utils


class TestInputBase:
Expand Down Expand Up @@ -1252,7 +1252,9 @@ def test_ghcn_custom_convert_to_dataset_no_duplicates(self, sample_ghcn_datafram

# Should have no NaN values if no duplicates were dropped
original_count = len(clean_data)
result_count = result.surface_air_temperature.count().item()
result_count = utils.maybe_densify_dataarray(
result.surface_air_temperature.count()
).item()
assert result_count == original_count

def test_ghcn_custom_convert_to_dataset_single_duplicate(
Expand Down Expand Up @@ -1282,7 +1284,9 @@ def test_ghcn_custom_convert_to_dataset_single_duplicate(

# Should have dropped one duplicate, so count should equal original
original_count = len(clean_data)
result_count = result.surface_air_temperature.count().item()
result_count = utils.maybe_densify_dataarray(
result.surface_air_temperature.count()
).item()
assert result_count == original_count

def test_ghcn_custom_convert_to_dataset_many_duplicates(
Expand Down Expand Up @@ -1319,7 +1323,9 @@ def test_ghcn_custom_convert_to_dataset_many_duplicates(

# Should have dropped all duplicates, so count should equal original
original_count = len(clean_data)
result_count = result.surface_air_temperature.count().item()
result_count = utils.maybe_densify_dataarray(
result.surface_air_temperature.count()
).item()
assert result_count == original_count

def test_ghcn_custom_convert_to_dataset_exception_handling(self):
Expand Down
12 changes: 7 additions & 5 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2484,8 +2484,8 @@ def side_effect(track_data, **kw):
assert coord_name in result.coords, (
f"Missing metadata coord: {coord_name}"
)
assert abs(float(result.forecast_landfall_latitude) - 16.1) < 0.01
assert abs(float(result.target_landfall_latitude) - 16.0) < 0.01
assert abs(result.forecast_landfall_latitude.item() - 16.1) < 0.01
assert abs(result.target_landfall_latitude.item() - 16.0) < 0.01

def test_first_approach_skips_init_with_late_track_start(self):
"""Verify approach='first' filters out init_times whose
Expand Down Expand Up @@ -2757,7 +2757,7 @@ def side_effect(track_data, **kw):
f"Displacement {result.values[0]:.1f} km should be small (FL vs FL)"
)
# Verify metadata uses the Florida target
assert abs(float(result.target_landfall_latitude) - 29.5) < 0.01
assert abs(result.target_landfall_latitude.item() - 29.5) < 0.01

def test_multi_landfall_displacement_next_approach(self):
"""Verify approach='next' also uses first forecast landfall
Expand Down Expand Up @@ -4802,9 +4802,11 @@ def _to_backend(forecast, target, backend):

@staticmethod
def _resolve(result):
"""Compute dask results; pass through numpy/sparse."""
"""Compute dask results and densify sparse ones; pass through numpy."""
if hasattr(result, "compute"):
return result.compute()
result = result.compute()
if isinstance(result.data, sparse.COO):
result = result.copy(data=result.data.todense())
return result

@pytest.mark.parametrize("backend", ["numpy", "dask", "sparse"])
Expand Down
Loading
Loading