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

Add tests for Xarray io functions #30

Merged
merged 21 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
02c3108
removed (commented-out) CONUS/AK search constraint
Jack-Hayes Oct 14, 2024
5f2b7f0
removed CONUS/AK constraint (cryocloud dev instead of local)
Jack-Hayes Oct 15, 2024
e5a70a4
Merge conflict cryocloud
Jack-Hayes Oct 15, 2024
fe707f6
updated CONUS/AK (not sure why my branch didn't have this already) an…
Jack-Hayes Oct 16, 2024
acf9467
WSL build complete
Jack-Hayes Oct 17, 2024
f786a49
Deleted CONUS/AK test and box import
Jack-Hayes Oct 17, 2024
fa66db4
Merge remote-tracking branch 'origin/main' into hayes-dev
Jack-Hayes Nov 8, 2024
ec9a5b8
added shapely make_valid to cascading_search
Jack-Hayes Nov 13, 2024
b2f2e22
Merge branch 'main' of https://github.com/uw-cryo/coincident into hay…
Jack-Hayes Nov 27, 2024
24fdb5c
Return rasters for cop30 and ESA search
Jack-Hayes Nov 27, 2024
0fc08ba
Added ODC dependency and fixed formatting
Jack-Hayes Dec 5, 2024
4b836ec
Merge remote-tracking branch 'origin/hayes-dev' into hayes-dev
Jack-Hayes Dec 5, 2024
3c88b5e
Merge remote-tracking branch 'origin/main' into hayes-dev
Jack-Hayes Dec 10, 2024
2468431
Added test for .io.xarray and synced search main.py
Jack-Hayes Dec 10, 2024
999d960
Cleaned test_xarray and added aoi, large_aoi to init
Jack-Hayes Dec 11, 2024
6b46f48
test_xarray matplotlib import inside test func
Jack-Hayes Dec 12, 2024
3429121
removed depends_on_optional from xarray test
Jack-Hayes Dec 12, 2024
f799324
use conftest.py
scottyhq Dec 13, 2024
19918e8
add back unused import for auth test
scottyhq Dec 13, 2024
6ab0514
streamline environments
scottyhq Dec 14, 2024
b7f271e
streamline environments 2
scottyhq Dec 14, 2024
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
62 changes: 31 additions & 31 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/coincident/datasets/planetary_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

@dataclass
class COP30(Dataset):
"""Essential metadata for Copernicus DEM"""
"""Essential metadata and data access for Copernicus DEM"""

alias: str = "cop30"
has_stac_api: bool = True
collections: list[str] = field(default_factory=lambda: ["cop-dem-glo-30"])
search: str = STACAPI
start: str | None = None # NOTE: has 'representative' datetime of 2021-04-22
start: str | None = None # Copernicus DEM has 'representative' datetime: 2021-04-22
end: str | None = None
type: str = "sar"
type: str = "dem"
provider: str = "microsoft"


Expand Down
84 changes: 84 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import annotations

import pytest
import xarray as xr

import coincident
from coincident.io.xarray import plot_esa_worldcover, to_dataset

# These tests are based on the workflow from https://coincident.readthedocs.io/en/latest/examples/contextual_data.html
# I couldn't think of a way to generalized these tests since the
# xarray funcs are pretty tailored to COP30 and ESA WC

# mypy doesn't like the resolution argument in to_dataset
# Argument "resolution" to "to_dataset" has incompatible type "float"; expected "dict[str, Any]" [arg-type]
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved

@pytest.fixture
def search_aoi():
"""Fixture to load a simplified AOI from realistic lidar data."""
workunit = "CO_WestCentral_2019"
df_wesm = coincident.search.wesm.read_wesm_csv()
gf_lidar = coincident.search.wesm.load_by_fid(
df_wesm[df_wesm.workunit == workunit].index
)
return gf_lidar.simplify(0.01)
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.network
def test_to_dataset_with_cop30(search_aoi):
"""Test `to_dataset` functionality with COP30 dataset."""
gf_cop30 = coincident.search.search(
dataset="cop30",
intersects=search_aoi,
)
ds = to_dataset(
gf_cop30,
aoi=search_aoi,
resolution=0.00081, # ~90m
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved
mask=True,
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved
).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."


@pytest.mark.network
def test_to_dataset_with_worldcover(search_aoi):
"""Test `to_dataset` functionality with WorldCover dataset."""
gf_wc = coincident.search.search(
dataset="worldcover",
intersects=search_aoi,
datetime=["2020"],
)
ds = to_dataset(
gf_wc,
bands=["map"],
aoi=search_aoi,
resolution=0.00081, # ~90m
mask=True,
).compute()
ds = ds.rename(map="landcover")
assert isinstance(ds, xr.Dataset), "Expected output to be an xarray Dataset."
assert "landcover" in ds.data_vars, "Expected 'landcover' variable in the Dataset."
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved


# Tests for `plot_esa_worldcover`
@pytest.mark.network
def test_plot_esa_worldcover_valid(search_aoi):
"""Test `plot_esa_worldcover` with valid WorldCover dataset."""
gf_wc = coincident.search.search(
dataset="worldcover",
intersects=search_aoi,
datetime=["2020"],
)
ds = to_dataset(
gf_wc,
bands=["map"],
aoi=search_aoi,
resolution=0.00081, # ~90m
mask=True,
).compute()
ds = ds.rename(map="landcover")
ax = plot_esa_worldcover(ds)
assert ax is not None, "Expected a valid Matplotlib Axes object."
assert len(ax.images) > 0, "Expected at least one image in the plot."
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved
ax.set_title("ESA WorldCover")
Jack-Hayes marked this conversation as resolved.
Show resolved Hide resolved
Loading