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

Benin Cashews: radiant mlhub -> source cooperative #2116

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions tests/data/technoserve-cashew-benin/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os

import numpy as np
import rasterio
from rasterio.crs import CRS
from rasterio.transform import Affine

DTYPE = np.uint16
SIZE = 2

np.random.seed(0)

dates = ('00_20191105',)
all_bands = (
'B01',
'B02',
'B03',
'B04',
'B05',
'B06',
'B07',
'B08',
'B8A',
'B09',
'B11',
'B12',
'CLD',
)
profile = {
'driver': 'GTiff',
'dtype': DTYPE,
'width': SIZE,
'height': SIZE,
'count': 1,
'crs': CRS.from_epsg(32631),
'transform': Affine(
10.002549584378608,
0.0,
440853.29890114715,
0.0,
-9.99842989423825,
1012804.082877621,
),
}

for date in dates:
os.makedirs(os.path.join('imagery', '00', date), exist_ok=True)
for band in all_bands:
Z = np.random.randint(np.iinfo(DTYPE).max, size=(SIZE, SIZE), dtype=DTYPE)
path = os.path.join('imagery', '00', date, f'{date}_{band}_10m.tif')
with rasterio.open(path, 'w', **profile) as src:
src.write(Z, 1)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/data/technoserve-cashew-benin/labels/00.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "FeatureCollection",
"name": "cashew_benin",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32631" } },
"features": [
{ "type": "Feature", "properties": { "OBJECTID": 1, "class": 1, "Shape_Leng": 367629.52331100003, "Shape_Area": 16997542.377500001, "class_name": "Well-managed planatation" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 447131.214800000190735, 1001286.6359 ], [ 447166.272199999541044, 1001285.31299999915 ], [ 447196.037899999879301, 1001285.31299999915 ], [ 447244.324400000274181, 1001283.3286 ], [ 447256.230700000189245, 1001282.00569999963 ], [ 447253.58490000013262, 1001248.9327 ], [ 447254.907800000160933, 1001228.4275 ], [ 447252.923700000159442, 1001212.05179999955 ], [ 447087.555899999978, 1001212.333799999207 ], [ 447082.266800000332296, 1001241.656599999988 ], [ 447076.97510000038892, 1001256.208799999207 ], [ 447074.329300000332296, 1001286.6359 ], [ 447131.214800000190735, 1001286.6359 ] ] ] } }
]
}
Binary file not shown.
Binary file not shown.
49 changes: 11 additions & 38 deletions tests/datasets/test_benin_cashews.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import glob
import os
import shutil
from pathlib import Path

import matplotlib.pyplot as plt
Expand All @@ -18,44 +16,22 @@
DatasetNotFoundError,
RGBBandsMissingError,
)


class Collection:
def download(self, output_dir: str, **kwargs: str) -> None:
glob_path = os.path.join('tests', 'data', 'ts_cashew_benin', '*.tar.gz')
for tarball in glob.iglob(glob_path):
shutil.copy(tarball, output_dir)


def fetch(dataset_id: str, **kwargs: str) -> Collection:
return Collection()
from torchgeo.datasets.utils import Executable


class TestBeninSmallHolderCashews:
@pytest.fixture
def dataset(
self, monkeypatch: MonkeyPatch, tmp_path: Path
self, azcopy: Executable, monkeypatch: MonkeyPatch, tmp_path: Path
) -> BeninSmallHolderCashews:
radiant_mlhub = pytest.importorskip('radiant_mlhub', minversion='0.3')
monkeypatch.setattr(radiant_mlhub.Collection, 'fetch', fetch)
source_md5 = '255efff0f03bc6322470949a09bc76db'
labels_md5 = 'ed2195d93ca6822d48eb02bc3e81c127'
monkeypatch.setitem(BeninSmallHolderCashews.image_meta, 'md5', source_md5)
monkeypatch.setitem(BeninSmallHolderCashews.target_meta, 'md5', labels_md5)
monkeypatch.setattr(BeninSmallHolderCashews, 'dates', ('2019_11_05',))
url = os.path.join('tests', 'data', 'technoserve-cashew-benin')
monkeypatch.setattr(BeninSmallHolderCashews, 'url', url)
monkeypatch.setattr(BeninSmallHolderCashews, 'dates', ('20191105',))
monkeypatch.setattr(BeninSmallHolderCashews, 'tile_height', 2)
monkeypatch.setattr(BeninSmallHolderCashews, 'tile_width', 2)
root = str(tmp_path)
transforms = nn.Identity()
bands = BeninSmallHolderCashews.all_bands

return BeninSmallHolderCashews(
root,
transforms=transforms,
bands=bands,
download=True,
api_key='',
checksum=True,
verbose=True,
)
return BeninSmallHolderCashews(root, transforms=transforms, download=True)

def test_getitem(self, dataset: BeninSmallHolderCashews) -> None:
x = dataset[0]
Expand All @@ -66,25 +42,22 @@ def test_getitem(self, dataset: BeninSmallHolderCashews) -> None:
assert isinstance(x['y'], torch.Tensor)

def test_len(self, dataset: BeninSmallHolderCashews) -> None:
assert len(dataset) == 72
assert len(dataset) == 1

def test_add(self, dataset: BeninSmallHolderCashews) -> None:
ds = dataset + dataset
assert isinstance(ds, ConcatDataset)
assert len(ds) == 144
assert len(ds) == 2

def test_already_downloaded(self, dataset: BeninSmallHolderCashews) -> None:
BeninSmallHolderCashews(root=dataset.root, download=True, api_key='')
BeninSmallHolderCashews(root=dataset.root, download=True)

def test_not_downloaded(self, tmp_path: Path) -> None:
with pytest.raises(DatasetNotFoundError, match='Dataset not found'):
BeninSmallHolderCashews(str(tmp_path))

def test_invalid_bands(self) -> None:
with pytest.raises(AssertionError):
BeninSmallHolderCashews(bands=['B01', 'B02']) # type: ignore[arg-type]

with pytest.raises(ValueError, match='is an invalid band name.'):
BeninSmallHolderCashews(bands=('foo', 'bar'))

def test_plot(self, dataset: BeninSmallHolderCashews) -> None:
Expand Down
Loading
Loading