From d85d2d13e12f916a7e2881c8255c24b4b75701dc Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Thu, 25 Apr 2024 17:18:44 +0200 Subject: [PATCH] Optional dependencies and errors updated --- .github/workflows/build.yml | 11 +++++++---- environment.yml | 12 ++++++------ setup.cfg | 3 ++- src/pygeogrids/grids.py | 1 - src/pygeogrids/shapefile.py | 12 +++++------- tests/test_grid.py | 11 ++++------- tests/test_shapefile.py | 4 +++- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 317285a..a4e9670 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,8 +10,8 @@ on: push: pull_request: workflow_dispatch: - schedule: - - cron: '0 0 * * *' # daily + #schedule: + # - cron: '0 0 * * *' # daily jobs: build: @@ -19,8 +19,11 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: ['3.8', '3.9', '3.10'] - os: ["ubuntu-latest", "windows-latest"] + include: + - os: "ubuntu-latest" + python-version: '3.8' # first supported + - os: ["ubuntu-latest", "windows-latest", "macos-latest"] + python-version: '3.12' # latest supported steps: - uses: actions/checkout@v2 with: diff --git a/environment.yml b/environment.yml index bea4e26..5bd54af 100644 --- a/environment.yml +++ b/environment.yml @@ -2,17 +2,17 @@ name: pygeogrids channels: - conda-forge dependencies: - - numpy - - pandas - - netcdf4 - geos - gdal - - pyproj - - pykdtree - - scipy - ipykernel - pip - pip: + - numpy + - pandas + - netCDF4 + - pykdtree + - pyproj + - scipy - sphinx - nbsphinx - sphinx_rtd_theme diff --git a/setup.cfg b/setup.cfg index a8884a2..ac9f41f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,13 +32,14 @@ package_dir = install_requires = importlib-metadata; python_version<"3.8" numpy + pandas netCDF4 pyproj pykdtree # The usage of test_requires is discouraged, see `Dependency Management` docs tests_require = pytest; pytest-cov # Require a specific Python version, e.g. Python 2.7 or >= 3.4 -python_requires = >=3.6 +python_requires = >=3.8 [options.packages.find] where = src diff --git a/src/pygeogrids/grids.py b/src/pygeogrids/grids.py index 153b42c..cd5446c 100644 --- a/src/pygeogrids/grids.py +++ b/src/pygeogrids/grids.py @@ -35,7 +35,6 @@ try: from osgeo import ogr - ogr_installed = True except ImportError: ogr_installed = False diff --git a/src/pygeogrids/shapefile.py b/src/pygeogrids/shapefile.py index e1ad1a0..4ddacd1 100644 --- a/src/pygeogrids/shapefile.py +++ b/src/pygeogrids/shapefile.py @@ -36,7 +36,6 @@ try: from osgeo import ogr - ogr_installed = True except ImportError: ogr_installed = False @@ -97,9 +96,8 @@ def get_gad_grid_points(grid, gadm_shp_path, level, name=None, oid=None): raise ValueError("Requested Object not found in shapefile.") else: - raise ImportError( - "No supported implementation installed." "Please install gdal and osgeo." - ) + raise ImportError("Could not import ogr from osgeo. " + "Please install them via `conda install geos gdal` first.") class ShpReader: @@ -138,8 +136,8 @@ def __init__( to filter out the relevant polygons. """ if not ogr_installed: - raise ImportError("Could not import ogr/osr. " - "Please install `gdal` with conda first.") + raise ImportError("Could not import ogr from osgeo. " + "Please install them via `conda install geos gdal` first.") self.shp_path = shp_path self.driver = driver self._init_open_shp() @@ -213,7 +211,7 @@ def lookup_id(self, names: Union[str, list, np.ndarray]) -> np.ndarray: rows = np.unique(np.where(np.isin(self.features.values, names))[0]) return self.features.index.values[rows] - def geom(self, id) -> ogr.Geometry: + def geom(self, id) -> 'ogr.Geometry': """ Get geometry of feature with passed id """ diff --git a/tests/test_grid.py b/tests/test_grid.py index 46137f0..042d6f1 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -32,16 +32,11 @@ import unittest import numpy.testing as nptest import numpy as np -try: - from osgeo import ogr - ogr_installed = True -except ImportError: - ogr_installed = False - import pytest from pygeogrids.grids import lonlat2cell, BasicGrid import pygeogrids as grids +from pygeogrids.grids import ogr_installed class Test_lonlat2cell(unittest.TestCase): @@ -660,12 +655,15 @@ def test_reorder_to_cellsize(): np.array([14, 14, 14, 14])) +@pytest.mark.skipif(not ogr_installed, reason="OGR not installed.") class Test_ShpGrid(unittest.TestCase): def setUp(self): """ Setup grid and shp_file to check if grid points fall in shp. """ + from osgeo import ogr + lat = np.arange(-90, 90, 1) lon = np.arange(-180, 180, 1) self.lons, self.lats = np.meshgrid(lon, lat) @@ -682,7 +680,6 @@ def setUp(self): poly.AddGeometry(ring) self.shp = poly - def test_shpgrid(self): ''' Check if gridpoints fall in polygon. diff --git a/tests/test_shapefile.py b/tests/test_shapefile.py index b500085..c88f461 100644 --- a/tests/test_shapefile.py +++ b/tests/test_shapefile.py @@ -1,6 +1,8 @@ -from pygeogrids.shapefile import subgrid_for_shp +import pytest +from pygeogrids.shapefile import subgrid_for_shp, ogr_installed from pygeogrids.grids import genreg_grid +@pytest.mark.skipif(not ogr_installed, reason="OGR not installed.") def test_subgrid_from_shapefile(): grid = genreg_grid(1)