Skip to content

Commit

Permalink
⚗️ Benchmark cog3pio engine against rioxarray
Browse files Browse the repository at this point in the history
Parametrized test to compare loading a GeoTIFF using cog3pio vs rioxarray via their respective xarray BackendEntrypoints. Made a new extras dependency group in pyproject.toml called 'benchmark', and listed both `pytest-codspeed` and `rioxarray` under it.
  • Loading branch information
weiji14 committed Mar 26, 2024
1 parent 4946458 commit c87e196
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install the package
run: |
set -e
python -m pip install cog3pio[tests] --find-links dist --force-reinstall
python -m pip install cog3pio[benchmark,tests] --find-links dist --force-reinstall
python -m pip list
# Run the benchmark tests
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ dependencies = [
dynamic = ["version"]

[project.optional-dependencies]
benchmark = [
"pytest-codspeed",
"rioxarray",
]
tests = [
"pytest",
"pytest-codspeed",
]

[project.entry-points."xarray.backends"]
Expand Down
28 changes: 26 additions & 2 deletions python/tests/test_xarray_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
Tests for xarray 'cog3pio' backend engine.
"""

import numpy as np
import pytest
import xarray as xr

try:
import rioxarray

HAS_RIOXARRAY = True
except ImportError:
HAS_RIOXARRAY = False


# %%
def test_xarray_backend_cog3pio():
@pytest.mark.benchmark
@pytest.mark.parametrize(
"engine",
[
"cog3pio",
pytest.param(
"rasterio",
marks=pytest.mark.skipif(
condition=not HAS_RIOXARRAY, reason="Could not import 'rioxarray'"
),
),
],
)
def test_xarray_backend_open_dataarray(engine):
"""
Ensure that passing engine='cog3pio' to xarray.open_dataarray works.
Ensure that passing engine='cog3pio' to xarray.open_dataarray works, and benchmark
against engine="rasterio" (rioxarray).
"""
with xr.open_dataarray(
filename_or_obj="https://github.com/cogeotiff/rio-tiler/raw/6.4.1/tests/fixtures/cog_nodata_nan.tif",
Expand All @@ -20,3 +43,4 @@ def test_xarray_backend_cog3pio():
assert da.y.min() == 5190440.0
assert da.y.max() == 5300040.0
assert da.dtype == "float32"
np.testing.assert_allclose(actual=da.mean(), desired=0.08855476)

0 comments on commit c87e196

Please sign in to comment.