From 12b7da214605ffc120ebab90e1f4a0f4e42d9c9e Mon Sep 17 00:00:00 2001 From: mdtanker Date: Wed, 9 Oct 2024 14:23:43 +0200 Subject: [PATCH] build: make xesmf optional dependency --- pyproject.toml | 8 +++++--- src/invert4geom/synthetic.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 457723de..2cf62a2a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,6 @@ dependencies = [ "deprecation", "pooch", "scikit-learn", - "xesmf", ### # optimization ### @@ -79,6 +78,9 @@ dependencies = [ ] [project.optional-dependencies] +synth = [ + "xesmf", +] test = [ "pytest >=6", "pytest-cov >=3", @@ -97,7 +99,7 @@ docs = [ "sphinxcontrib-bibtex", ] dev = [ - "invert4geom[test,docs]", + "invert4geom[test,docs, synth]", "ruff", "nox", "pre-commit", @@ -108,7 +110,7 @@ dev = [ "mypy", ] all = [ - "invert4geom[test,dev,docs]", + "invert4geom[test,dev,docs,synth]", ] [project.urls] diff --git a/src/invert4geom/synthetic.py b/src/invert4geom/synthetic.py index 8a530ec1..d5e4e037 100644 --- a/src/invert4geom/synthetic.py +++ b/src/invert4geom/synthetic.py @@ -9,13 +9,17 @@ import pooch import verde as vd import xarray as xr -import xesmf from numpy.typing import NDArray from polartoolkit import fetch, maps from polartoolkit import utils as polar_utils from invert4geom import cross_validation, log, utils +try: + import xesmf +except ImportError: + xesmf = None + def load_synthetic_model( spacing: float = 1e3, @@ -283,6 +287,12 @@ def contaminate_with_long_wavelength_noise( xarray.DataArray Contaminated grid """ + if xesmf is None: + msg = ( + "To use the `contaminate_with_long_wavelength_noise` function, you must " + "have the `xesmf` package installed." + ) + raise ImportError(msg) grid = copy.deepcopy(grid) @@ -310,6 +320,7 @@ def contaminate_with_long_wavelength_noise( data=None, data_names=None, ).rename({"northing": "lat", "easting": "lon"}) + low_res_grid = xesmf.Regridder( grid.rename({"northing": "lat", "easting": "lon"}), low_res_grid,