-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Xarray io functions (#30)
* removed (commented-out) CONUS/AK search constraint * removed CONUS/AK constraint (cryocloud dev instead of local) * updated CONUS/AK (not sure why my branch didn't have this already) and win-64 compat * WSL build complete * Deleted CONUS/AK test and box import * added shapely make_valid to cascading_search * Return rasters for cop30 and ESA search * Added ODC dependency and fixed formatting * Added test for .io.xarray and synced search main.py * Cleaned test_xarray and added aoi, large_aoi to init * test_xarray matplotlib import inside test func * removed depends_on_optional from xarray test * use conftest.py * add back unused import for auth test * streamline environments * streamline environments 2 --------- Co-authored-by: Scott Henderson <[email protected]>
- Loading branch information
1 parent
c5a1a85
commit f8b0973
Showing
8 changed files
with
1,833 additions
and
3,159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +0,0 @@ | ||
from __future__ import annotations | ||
|
||
# import os | ||
# if not os.environ.get('MAXAR_API_KEY'): | ||
# os.environ['MAXAR_API_KEY'] = 'fake-test-key' | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# ruff: noqa: ARG001 | ||
from __future__ import annotations | ||
|
||
import geopandas as gpd | ||
import pytest | ||
|
||
# import os | ||
# if not os.environ.get('MAXAR_API_KEY'): | ||
# os.environ['MAXAR_API_KEY'] = 'fake-test-key' | ||
|
||
|
||
@pytest.fixture(scope="package") | ||
def aoi(): | ||
# 11 vertices, 1,361km^2 | ||
aoi_url = "https://raw.githubusercontent.com/SlideRuleEarth/sliderule-python/main/data/grandmesa.geojson" | ||
return gpd.read_file(aoi_url) | ||
|
||
|
||
@pytest.fixture | ||
def large_aoi(scope="package"): | ||
# 260 vertices, large area 269,590 km^2 | ||
aoi_url = "https://raw.githubusercontent.com/unitedstates/districts/refs/heads/gh-pages/states/CO/shape.geojson" | ||
return gpd.read_file(aoi_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
import xarray as xr | ||
from matplotlib.collections import QuadMesh | ||
|
||
import coincident | ||
from coincident.io.xarray import plot_esa_worldcover, to_dataset | ||
|
||
# Decorate tests requiring internet (slow & flaky) | ||
network = pytest.mark.network | ||
|
||
|
||
@network | ||
def test_to_dataset_with_cop30(aoi): | ||
"""Test `to_dataset` functionality with COP30 dataset.""" | ||
gf_cop30 = coincident.search.search( | ||
dataset="cop30", | ||
intersects=aoi, | ||
) | ||
ds = to_dataset( | ||
gf_cop30, | ||
aoi=aoi, | ||
resolution=0.1, # ~1km | ||
).compute() | ||
assert isinstance(ds, xr.Dataset), "Expected output to be an xarray Dataset." | ||
assert "data" in ds.data_vars, "Expected 'data' variable in the Dataset." | ||
|
||
|
||
@network | ||
def test_to_dataset_with_worldcover(aoi): | ||
"""Test `to_dataset` functionality with WorldCover dataset.""" | ||
gf_wc = coincident.search.search( | ||
dataset="worldcover", | ||
intersects=aoi, | ||
datetime=["2020"], | ||
) | ||
ds = to_dataset( | ||
gf_wc, | ||
bands=["map"], | ||
aoi=aoi, | ||
resolution=0.1, # ~1km | ||
).compute() | ||
assert isinstance(ds, xr.Dataset), "Expected output to be an xarray Dataset." | ||
assert "map" in ds.data_vars, "Expected 'map' variable in the Dataset." | ||
|
||
|
||
@network | ||
def test_plot_esa_worldcover_valid(aoi): | ||
"""Test `plot_esa_worldcover` with valid WorldCover dataset.""" | ||
|
||
gf_wc = coincident.search.search( | ||
dataset="worldcover", | ||
intersects=aoi, | ||
datetime=["2021"], | ||
) | ||
ds = to_dataset( | ||
gf_wc, | ||
bands=["map"], | ||
aoi=aoi, | ||
resolution=0.1, # ~1km | ||
).compute() | ||
ds = ds.rename(map="landcover") | ||
ax = plot_esa_worldcover(ds) | ||
assert ax is not None, "Expected a valid Matplotlib Axes object." | ||
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.4.0.html | ||
# https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/tests/test_contour.py#L146 | ||
assert any( | ||
isinstance(c, QuadMesh) for c in ax.get_children() | ||
), "Expected at least one pcolormesh object in the plot." |