Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test envs to support python 3.9-3.12, plus an env without xesmf #66

Merged
merged 6 commits into from
Feb 13, 2024
Merged
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: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.8", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.12.noxesmf"]
steps:
- uses: actions/checkout@v4
- name: Create conda environment
Expand Down
25 changes: 25 additions & 0 deletions ci/environment-py3.11.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test_env_xagg_38
channels:
- conda-forge
dependencies:
- python=3.11
############## These will have to be adjusted to your specific project
- numpy
- scipy
- xarray
- pandas
- netcdf4
- geopandas >= 0.12.0
- shapely
- xesmf >= 0.7.0 # These versions and explicit loads are to fix an issue in xesmf's call to cf_xarray (possibly through esmpy)
- cf_xarray >= 0.5.1
- esmf >= 8.1.0
- esmpy >= 8.1.0
- pytables
##############
- pytest
- pip:
- codecov
- pytest-cov
- coverage[toml]
# - tables >= 3.7.0 # For exporting hd5 files through wm.to_file() (3.6.0 may have issues)
22 changes: 22 additions & 0 deletions ci/environment-py3.12.noxesmf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test_env_xagg_noxe
channels:
- conda-forge
dependencies:
- python=3.12
############## Without xesmf / esmpy / esmf explicitly installed, to check optional dependency
- numpy
- scipy
- xarray
- pandas
- netcdf4
- geopandas >= 0.12.0
- shapely
- cf_xarray >= 0.5.1
- pytables
##############
- pytest
- pip:
- codecov
- pytest-cov
- coverage[toml]
# - tables >= 3.7.0 # For exporting hd5 files through wm.to_file() (3.6.0 may have issues)
25 changes: 25 additions & 0 deletions ci/environment-py3.12.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test_env_xagg_38
channels:
- conda-forge
dependencies:
- python=3.12
############## These will have to be adjusted to your specific project
- numpy
- scipy
- xarray
- pandas
- netcdf4
- geopandas >= 0.12.0
- shapely
- xesmf >= 0.7.0 # These versions and explicit loads are to fix an issue in xesmf's call to cf_xarray (possibly through esmpy)
- cf_xarray >= 0.5.1
- esmf >= 8.1.0
- esmpy >= 8.1.0
- pytables
##############
- pytest
- pip:
- codecov
- pytest-cov
- coverage[toml]
# - tables >= 3.7.0 # For exporting hd5 files through wm.to_file() (3.6.0 may have issues)
2 changes: 1 addition & 1 deletion ci/environment-py3.8.yml → ci/environment-py3.9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: test_env_xagg_38
channels:
- conda-forge
dependencies:
- python=3.8
- python=3.9
############## These will have to be adjusted to your specific project
- numpy
- scipy
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
'tables',
'cf_xarray>=0.5.1',
],
extras_require={
'regrid':['xesmf>=0.7.1','esmpy>=8.1.0'],
'plots':['matplotlib','cmocean','cartopy'],
}
)
54 changes: 35 additions & 19 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from geopandas import testing as gpdt
from unittest import TestCase
from shapely.geometry import Polygon
import xesmf as xe
try:
import xesmf as xe
_has_xesmf=True
except ImportError:
# To be able to test the rest with environments without xesmf
_has_xesmf=False

from xagg.core import (process_weights,create_raster_polygons,get_pixel_overlaps,aggregate,NoOverlapError)

Expand Down Expand Up @@ -46,6 +51,7 @@ def test_process_weights_basic():
xr.testing.assert_allclose(ds_compare,ds_t)
# (weights_info isn't currently used by anything)


def test_process_weights_regrid_weights():
# Now, test with a weights array twice the resolution as the
# ds, so it needs to be regridded
Expand All @@ -63,15 +69,20 @@ def test_process_weights_regrid_weights():
coords=[np.array([-0.25,0.25,0.75,1.25]),
np.array([-0.25,0.25,0.75,1.25])])

ds_t,weights_info = process_weights(ds,weights=weights)
if _has_xesmf:
ds_t,weights_info = process_weights(ds,weights=weights)

ds_compare = xr.Dataset({'weights':(('lat','lon'),np.array([[0,1],[2,3]]))},
coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1])),
})
ds_compare = xr.Dataset({'weights':(('lat','lon'),np.array([[0,1],[2,3]]))},
coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1])),
})

# Check if weights were correctly added to ds
xr.testing.assert_allclose(ds_compare,ds_t)
# Check if weights were correctly added to ds
xr.testing.assert_allclose(ds_compare,ds_t)
else:
# Should raise ImportError in the no-xesmf environment
with pytest.raises(ImportError):
ds_t,weights_info = process_weights(ds,weights=weights)

def test_process_weights_close_weights():
# Make sure weights that are within `np.allclose` but not exactly
Expand Down Expand Up @@ -136,7 +147,7 @@ def test_create_raster_polygons_with_weights():
'lon':(['lon'],np.array([0,1])),
'bnds':(['bnds'],np.array([0,1]))})

# Synethetic weights grid
# Synthetic weights grid that requires regridding
weights = xr.DataArray(data=np.array([[-0.5,0.5,0.5,1.5],
[0.5,-0.5,1.5,0.5],
[1.5,2.5,2.5,3.5],
Expand All @@ -145,19 +156,24 @@ def test_create_raster_polygons_with_weights():
coords=[np.array([-0.25,0.25,0.75,1.25]),
np.array([-0.25,0.25,0.75,1.25])])

pix_agg = create_raster_polygons(ds,weights=weights)
if _has_xesmf:
pix_agg = create_raster_polygons(ds,weights=weights)

compare_series = pd.Series(data=[np.array(v) for v in [0.,1.,2.,3.]],
index=[0,1,2,3],
name='weights')
compare_series = pd.Series(data=[np.array(v) for v in [0.,1.,2.,3.]],
index=[0,1,2,3],
name='weights')


# There's an issue here in pd.testing.assert_series_equal...
#pd.testing.assert_series_equal(pix_agg['gdf_pixels'].weights,
# compare_series)
#np.testing.assert_allclose(compare_series,pix_agg['gdf_pixels'].weights)
#assert np.allclose(compare_series,pix_agg['gdf_pixels'].weights)
assert np.allclose([float(v) for v in compare_series],[float(v) for v in pix_agg['gdf_pixels'].weights])
# There's an issue here in pd.testing.assert_series_equal...
#pd.testing.assert_series_equal(pix_agg['gdf_pixels'].weights,
# compare_series)
#np.testing.assert_allclose(compare_series,pix_agg['gdf_pixels'].weights)
#assert np.allclose(compare_series,pix_agg['gdf_pixels'].weights)
assert np.allclose([float(v) for v in compare_series],[float(v) for v in pix_agg['gdf_pixels'].weights])
else:
# Should raise ImportError in the no-xesmf environment
with pytest.raises(ImportError):
pix_agg = create_raster_polygons(ds,weights=weights)

def test_create_raster_polygons_at180():
# Make sure raster polygons are correctly built at the 180/-180
Expand Down
1 change: 0 additions & 1 deletion tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from geopandas import testing as gpdt
from unittest import TestCase
from shapely.geometry import Polygon
import xesmf as xe

from xagg.wrappers import (pixel_overlaps)

Expand Down
9 changes: 6 additions & 3 deletions xagg/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import warnings
import re
import os
try:
import xesmf as xe
_has_xesmf=True
except ImportError:
_has_xesmf=False

from . auxfuncs import (find_rel_area,normalize,fix_ds,get_bnds,subset_find,list_or_first)
from . classes import (weightmap,aggregated)
Expand Down Expand Up @@ -155,9 +160,7 @@ def process_weights(ds,weights=None,target='ds',silent=False):
# Import xesmf here to allow the code to work without it (it
# often has dependency issues and isn't necessary for many
# features of xagg)
try:
import xesmf as xe
except ImportError:
if not _has_xesmf:
raise ImportError('If the `weights` grid and the `ds` grid are different, '+
'`xesmf` is needed for `xagg` to regrid them to match; however, '+
'`xesmf` is not installed. Either install `xesmf` or '+
Expand Down
Loading