From ca68fb5767af8f6477714a2687b80feed91fe407 Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Mon, 21 Aug 2023 10:22:00 +0200 Subject: [PATCH 1/5] add io and extra to testing in ci again --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e5b776975..8195cea71 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,7 +44,7 @@ jobs: activate-environment: hydromt use-mamba: true - name: Generate env spec - run: pip install tomli && python make_env.py test + run: pip install tomli && python make_env.py test,io,extra - name: Set cache date From 721e9064ac923e33e6f913f8c8ed62638cabc112 Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Mon, 21 Aug 2023 10:23:51 +0200 Subject: [PATCH 2/5] make test run again --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b5314084f..6f2869a1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -137,7 +137,7 @@ include = ["hydromt"] exclude = ["docs", "examples", "envs", "tests", "binder", ".github"] [tool.pytest.ini_options] -addopts = "--ff --timeout=120" +addopts = "--ff --timeout=120 " testpaths = ["tests"] filterwarnings = [ From b6d4296a30f1a19ff014b5b5282ba61f117d0d4c Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Mon, 21 Aug 2023 10:52:29 +0200 Subject: [PATCH 3/5] add uxgrid to test deps --- pyproject.toml | 3 ++- tests/conftest.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6f2869a1a..f3dba74e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,7 +73,8 @@ test = [ "pytest-cov", # test coverage "pytest-mock", # mocking "pytest-timeout", # darn hanging tests -] + "xugrid" + ] doc = [ "nbsphinx", # build notebooks in docs "pydata-sphinx-theme", # theme diff --git a/tests/conftest.py b/tests/conftest.py index a5b21c8f8..3a42cc9ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ import pyflwdir import pytest import xarray as xr +from dask import config as dask_config from shapely.geometry import box from hydromt import ( @@ -18,6 +19,8 @@ ) from hydromt.data_catalog import DataCatalog +dask_config.set(scheduler="single-threaded") + @pytest.fixture() def rioda(): From 05769ca1050613056d7351745a5cb88e4e9416aa Mon Sep 17 00:00:00 2001 From: hboisgon Date: Mon, 21 Aug 2023 17:52:59 +0800 Subject: [PATCH 4/5] fix mesh_model tests --- hydromt/models/model_mesh.py | 7 ++++--- hydromt/workflows/basin_mask.py | 9 ++++++++- tests/conftest.py | 6 +++++- tests/test_model.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/hydromt/models/model_mesh.py b/hydromt/models/model_mesh.py index 9c7c27b9f..dbd5c1ab7 100644 --- a/hydromt/models/model_mesh.py +++ b/hydromt/models/model_mesh.py @@ -292,7 +292,7 @@ def set_mesh( raise ValueError( f"Cannot set mesh from {str(type(data).__name__)} without a name." ) - data = data.to_dataset(optional_attributes=True) + data = data.to_dataset() # Checks on grid topology # TODO: check if we support setting multiple grids at once. For now just one @@ -371,7 +371,8 @@ def set_mesh( # update related geoms if necessary: region if overwrite_grid or new_grid: # add / updates region - self._geoms.pop("region", None) + if "region" in self.geoms: + self._geoms.pop("region", None) self.region def get_mesh( @@ -437,7 +438,7 @@ def read_mesh( Additional keyword arguments to be passed to the `read_nc` method. """ self._assert_read_mode - ds = xr.merge(self._read_nc(fn, **kwargs).values()) + ds = xr.merge(self.read_nc(fn, **kwargs).values()) uds = xu.UgridDataset(ds) if ds.rio.crs is not None: # parse crs uds.ugrid.set_crs(ds.raster.crs) diff --git a/hydromt/workflows/basin_mask.py b/hydromt/workflows/basin_mask.py index 7d3f8ccd1..519375cd0 100644 --- a/hydromt/workflows/basin_mask.py +++ b/hydromt/workflows/basin_mask.py @@ -148,8 +148,15 @@ def parse_region(region, logger=logger): if _compat.HAS_XUGRID: if isinstance(value0, (str, Path)) and isfile(value0): kwarg = dict(mesh=xu.open_dataset(value0)) - elif isinstance(value0, xu.UgridDataset): + elif isinstance(value0, (xu.UgridDataset, xu.UgridDataArray)): kwarg = dict(mesh=value0) + elif isinstance(value0, (xu.Ugrid1d, xu.Ugrid2d)): + kwarg = dict(mesh=value0.to_dataset(optional_attributes=True)) + else: + raise ValueError( + f"Unrecognised type {type(value0)}." + "Should be a path, data catalog key or xugrid object." + ) kwargs.update(kwarg) else: raise ImportError("xugrid is required to read mesh files.") diff --git a/tests/conftest.py b/tests/conftest.py index 3a42cc9ce..1650c62be 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,5 @@ +from os.path import abspath, dirname, join + import geopandas as gpd import numpy as np import pandas as pd @@ -21,6 +23,8 @@ dask_config.set(scheduler="single-threaded") +DATADIR = join(dirname(abspath(__file__)), "data") + @pytest.fixture() def rioda(): @@ -83,7 +87,7 @@ def geodf(df): @pytest.fixture() def world(): - world = gpd.read_file("tests/data/naturalearth_lowres.geojson") + world = gpd.read_file(join(DATADIR, "naturalearth_lowres.geojson")) return world diff --git a/tests/test_model.py b/tests/test_model.py index fde9a5d7b..58fd233c1 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -641,7 +641,7 @@ def test_meshmodel_setup(griduda, world): mod.setup_mesh2d(region, res=10000, crs=3857, grid_name="mesh2d") mod.region - region = {"mesh": griduda.ugrid.to_dataset()} + region = {"mesh": griduda} mod1 = MeshModel(data_libs=["artifact_data", dc_param_fn]) mod1.setup_mesh2d(region, grid_name="mesh2d") mod1.setup_mesh2d_from_rasterdataset("vito", grid_name="mesh2d") From d0e76fcb079c2f9cb7cad1ae10b47294a636c626 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Mon, 21 Aug 2023 17:58:40 +0800 Subject: [PATCH 5/5] small fix for region of type Ugrid --- hydromt/workflows/basin_mask.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hydromt/workflows/basin_mask.py b/hydromt/workflows/basin_mask.py index 519375cd0..a1a64886b 100644 --- a/hydromt/workflows/basin_mask.py +++ b/hydromt/workflows/basin_mask.py @@ -151,7 +151,9 @@ def parse_region(region, logger=logger): elif isinstance(value0, (xu.UgridDataset, xu.UgridDataArray)): kwarg = dict(mesh=value0) elif isinstance(value0, (xu.Ugrid1d, xu.Ugrid2d)): - kwarg = dict(mesh=value0.to_dataset(optional_attributes=True)) + kwarg = dict( + mesh=xu.UgridDataset(value0.to_dataset(optional_attributes=True)) + ) else: raise ValueError( f"Unrecognised type {type(value0)}."