Skip to content

Commit

Permalink
add new python versions for tests, fix optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ks905383 committed Feb 13, 2024
1 parent 5d76da9 commit 2b2527e
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 28 deletions.
22 changes: 22 additions & 0 deletions ci/environment-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.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)
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
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@
'tables',
'cf_xarray>=0.5.1',
],
extras_require=[
'xesmf>=0.7.1',
'esmpy>=8.1.0',
'matplotlib',
'cmocean',
'cartopy',
]
)
54 changes: 30 additions & 24 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,32 +51,33 @@ 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
ds = xr.Dataset(coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1])),
})

# Synthetic weights grid, with double the resolution, and shifted
# by a half degree. Should regrid to the same weights grid as above
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],
[2.5,1.5,3.5,2.5]]),
dims=['lat','lon'],
coords=[np.array([-0.25,0.25,0.75,1.25]),
np.array([-0.25,0.25,0.75,1.25])])
if _has_xesmf:
def test_process_weights_regrid_weights():
# Now, test with a weights array twice the resolution as the
# ds, so it needs to be regridded
ds = xr.Dataset(coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1])),
})

# Synthetic weights grid, with double the resolution, and shifted
# by a half degree. Should regrid to the same weights grid as above
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],
[2.5,1.5,3.5,2.5]]),
dims=['lat','lon'],
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)
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)

def test_process_weights_close_weights():
# Make sure weights that are within `np.allclose` but not exactly
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

0 comments on commit 2b2527e

Please sign in to comment.