From 44204b599f9e6d04540f76dd7abcfd3c187f93ec Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Sun, 5 Nov 2023 14:46:56 -0500 Subject: [PATCH] remove `xarray` as hard dependency (#629) * remove xarray as hard dep * refactor mixin * rm underscore * address martin comments --------- Co-authored-by: eli knaap --- ci/310-oldest.yaml | 2 +- ci/310.yaml | 2 +- ci/311.yaml | 2 +- ci/312-no-optional.yaml | 1 - ci/312.yaml | 2 +- environment.yml | 2 +- libpysal/weights/tests/test_contiguity.py | 18 ++++++++++++++---- libpysal/weights/tests/test_raster.py | 6 +++--- pyproject.toml | 2 +- 9 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ci/310-oldest.yaml b/ci/310-oldest.yaml index 60e5cc745..586cea8bb 100644 --- a/ci/310-oldest.yaml +++ b/ci/310-oldest.yaml @@ -12,7 +12,6 @@ dependencies: - requests=2.27 - scipy=1.8 - shapely=2.0.1 - - xarray=2022.3 # testing - codecov - matplotlib>=3.6 @@ -29,6 +28,7 @@ dependencies: - scikit-learn=1.1 - sqlalchemy=2.0 - zstd + - xarray=2022.3 - pip - pip: - platformdirs==2.0.2 diff --git a/ci/310.yaml b/ci/310.yaml index 702dd50ec..0440a62b2 100644 --- a/ci/310.yaml +++ b/ci/310.yaml @@ -12,7 +12,6 @@ dependencies: - requests - scipy - shapely - - xarray # testing - codecov - matplotlib @@ -28,4 +27,5 @@ dependencies: - pyarrow - scikit-learn - sqlalchemy + - xarray - zstd diff --git a/ci/311.yaml b/ci/311.yaml index 43a91a140..e56f11f42 100644 --- a/ci/311.yaml +++ b/ci/311.yaml @@ -12,7 +12,6 @@ dependencies: - requests - scipy - shapely - - xarray # testing - codecov - matplotlib @@ -28,4 +27,5 @@ dependencies: - pyarrow - scikit-learn - sqlalchemy + - xarray - zstd diff --git a/ci/312-no-optional.yaml b/ci/312-no-optional.yaml index 59e0a35cf..64cd90538 100644 --- a/ci/312-no-optional.yaml +++ b/ci/312-no-optional.yaml @@ -11,7 +11,6 @@ dependencies: - platformdirs - requests - scipy - - xarray # testing - codecov - matplotlib diff --git a/ci/312.yaml b/ci/312.yaml index 6063157bb..2389b4ff8 100644 --- a/ci/312.yaml +++ b/ci/312.yaml @@ -12,7 +12,6 @@ dependencies: - requests - scipy - shapely - - xarray # testing - codecov - matplotlib @@ -29,6 +28,7 @@ dependencies: - scikit-learn - sqlalchemy - zstd + - xarray # for docs build action (this env only) - mkdocs-jupyter - myst-parser diff --git a/environment.yml b/environment.yml index 70a37d268..50a5e9300 100644 --- a/environment.yml +++ b/environment.yml @@ -15,7 +15,6 @@ dependencies: - requests - scipy - shapely - - xarray # optional libpysal deps - geodatasets - joblib @@ -25,4 +24,5 @@ dependencies: - pyarrow - scikit-learn - sqlalchemy + - xarray - zstd diff --git a/libpysal/weights/tests/test_contiguity.py b/libpysal/weights/tests/test_contiguity.py index a390f1cb2..06132a91e 100644 --- a/libpysal/weights/tests/test_contiguity.py +++ b/libpysal/weights/tests/test_contiguity.py @@ -1,18 +1,20 @@ +# ruff: noqa: N815 + import numpy as np +import pytest from ... import examples as pysal_examples from ...io import geotable as pdio -from ...io.fileio import FileIO as ps_open +from ...io.fileio import FileIO from .. import contiguity as c -from .. import raster, util +from .. import util from ..weights import W class ContiguityMixin: polygon_path = pysal_examples.get_path("columbus.shp") point_path = pysal_examples.get_path("baltim.shp") - da = raster.testDataArray((1, 4, 4), missing_vals=False) - f = ps_open(polygon_path) # our file handler + f = FileIO(polygon_path) # our file handler polygons = f.read() # our iterable f.seek(0) # go back to head of file cls = object # class constructor @@ -25,6 +27,12 @@ class ContiguityMixin: known_wsp_da = dict() known_wi_da = None known_w_da = dict() + try: + from .. import raster + + da = raster.testDataArray((1, 4, 4), missing_vals=False) + except ImportError: + da = None def setup_method(self): self.__dict__.update( @@ -109,6 +117,8 @@ def test_from_geodataframe_order(self): assert w.id_order[:5] == expected def test_from_xarray(self): + pytest.importorskip("xarray") + w = self.cls.from_xarray(self.da, sparse=False, n_jobs=-1) assert w[self.known_wi_da] == self.known_w_da ws = self.cls.from_xarray(self.da) diff --git a/libpysal/weights/tests/test_raster.py b/libpysal/weights/tests/test_raster.py index 51685e075..4abd39765 100644 --- a/libpysal/weights/tests/test_raster.py +++ b/libpysal/weights/tests/test_raster.py @@ -2,13 +2,12 @@ import numpy as np import pandas as pd import pytest -from xarray import DataArray - from .. import raster class Testraster: def setup_method(self): + pytest.importorskip("xarray") self.da1 = raster.testDataArray() self.da2 = raster.testDataArray((1, 4, 4), missing_vals=False) self.da3 = self.da2.rename({"band": "layer", "x": "longitude", "y": "latitude"}) @@ -77,9 +76,10 @@ def test_da2_wsp(self): assert w2.index.tolist() == self.da2.to_series().index.tolist() def test_w2da(self): + xarray = pytest.importorskip("xarray") w2 = raster.da2W(self.da2, "rook", n_jobs=-1) da2 = raster.w2da(self.da2.data.flatten(), w2, self.da2.attrs, self.da2.coords) - da_compare = DataArray.equals(da2, self.da2) + da_compare = xarray.DataArray.equals(da2, self.da2) assert da_compare == True def test_wsp2da(self): diff --git a/pyproject.toml b/pyproject.toml index 8fee7b845..c9688bf6f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,6 @@ dependencies = [ "requests>=2.27", "scipy>=1.8", "shapely>=2.0.1", - "xarray>=2022.3", ] [project.urls] @@ -49,6 +48,7 @@ plus = [ "pyarrow>=7.0", "scikit-learn>=1.1", "sqlalchemy>=2.0", + "xarray>=2022.3", "zstd", ] dev = [