-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CMS Global Mangrove Canopy dataset (#391)
* CMS dataset * dynamically set filename * add warning in documentation * requested changes and data.py * single zip file and camel case * md5 check added * correct error messages * compression smaller test file Co-authored-by: Caleb Robinson <calebrob6@gmail.com>
- Loading branch information
Showing
9 changed files
with
419 additions
and
0 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
Binary file added
BIN
+62.3 KB
tests/data/cms_mangrove_canopy/CMS_Global_Map_Mangrove_Canopy_1665.zip
Binary file not shown.
Binary file added
BIN
+22.1 KB
...data/cms_mangrove_canopy/CMS_Global_Map_Mangrove_Canopy_1665/data/Mangrove_agb_Angola.tif
Binary file not shown.
Binary file added
BIN
+22.2 KB
...ta/cms_mangrove_canopy/CMS_Global_Map_Mangrove_Canopy_1665/data/Mangrove_hba95_Angola.tif
Binary file not shown.
Binary file added
BIN
+22.1 KB
...a/cms_mangrove_canopy/CMS_Global_Map_Mangrove_Canopy_1665/data/Mangrove_hmax95_Angola.tif
Binary file not shown.
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,68 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import hashlib | ||
import os | ||
import random | ||
import shutil | ||
|
||
import numpy as np | ||
import rasterio | ||
|
||
np.random.seed(0) | ||
random.seed(0) | ||
|
||
SIZE = 64 | ||
|
||
|
||
files = [ | ||
{"image": "Mangrove_agb_Angola.tif"}, | ||
{"image": "Mangrove_hba95_Angola.tif"}, | ||
{"image": "Mangrove_hmax95_Angola.tif"}, | ||
] | ||
|
||
|
||
def create_file(path: str, dtype: str, num_channels: int) -> None: | ||
profile = {} | ||
profile["driver"] = "GTiff" | ||
profile["dtype"] = dtype | ||
profile["count"] = num_channels | ||
profile["crs"] = "epsg:4326" | ||
profile["transform"] = rasterio.transform.from_bounds(0, 0, 1, 1, 1, 1) | ||
profile["height"] = SIZE | ||
profile["width"] = SIZE | ||
profile["compress"] = "lzw" | ||
profile["predictor"] = 2 | ||
|
||
Z = np.random.randint( | ||
np.iinfo(profile["dtype"]).max, size=(1, SIZE, SIZE), dtype=profile["dtype"] | ||
) | ||
src = rasterio.open(path, "w", **profile) | ||
src.write(Z) | ||
|
||
|
||
if __name__ == "__main__": | ||
directory = "CMS_Global_Map_Mangrove_Canopy_1665" | ||
|
||
# Remove old data | ||
if os.path.isdir(directory): | ||
shutil.rmtree(directory) | ||
|
||
os.makedirs(os.path.join(directory, "data"), exist_ok=True) | ||
|
||
for file_dict in files: | ||
# Create mask file | ||
path = file_dict["image"] | ||
create_file( | ||
os.path.join(directory, "data", path), dtype="int32", num_channels=1 | ||
) | ||
|
||
# Compress data | ||
shutil.make_archive(directory.replace(".zip", ""), "zip", ".", directory) | ||
|
||
# Compute checksums | ||
with open(directory + ".zip", "rb") as f: | ||
md5 = hashlib.md5(f.read()).hexdigest() | ||
print(f"{directory}: {md5}") |
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,93 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
import shutil | ||
from pathlib import Path | ||
from typing import Generator | ||
|
||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
from _pytest.monkeypatch import MonkeyPatch | ||
from rasterio.crs import CRS | ||
|
||
from torchgeo.datasets import CMSGlobalMangroveCanopy, IntersectionDataset, UnionDataset | ||
|
||
|
||
def download_url(url: str, root: str, *args: str, **kwargs: str) -> None: | ||
shutil.copy(url, root) | ||
|
||
|
||
class TestCMSGlobalMangroveCanopy: | ||
@pytest.fixture | ||
def dataset( | ||
self, monkeypatch: Generator[MonkeyPatch, None, None], tmp_path: Path | ||
) -> CMSGlobalMangroveCanopy: | ||
zipfile = "CMS_Global_Map_Mangrove_Canopy_1665.zip" | ||
monkeypatch.setattr( # type: ignore[attr-defined] | ||
CMSGlobalMangroveCanopy, "zipfile", zipfile | ||
) | ||
|
||
md5 = "d6894fa6293cc9c0f3f95a810e842de5" | ||
monkeypatch.setattr( # type: ignore[attr-defined] | ||
CMSGlobalMangroveCanopy, "md5", md5 | ||
) | ||
|
||
root = os.path.join("tests", "data", "cms_mangrove_canopy") | ||
transforms = nn.Identity() # type: ignore[attr-defined] | ||
country = "Angola" | ||
|
||
return CMSGlobalMangroveCanopy( | ||
root, country=country, transforms=transforms, checksum=True | ||
) | ||
|
||
def test_getitem(self, dataset: CMSGlobalMangroveCanopy) -> None: | ||
x = dataset[dataset.bounds] | ||
assert isinstance(x, dict) | ||
assert isinstance(x["crs"], CRS) | ||
assert isinstance(x["mask"], torch.Tensor) | ||
|
||
def test_no_dataset(self) -> None: | ||
with pytest.raises(RuntimeError, match="Dataset not found in."): | ||
CMSGlobalMangroveCanopy(root="/test") | ||
|
||
def test_already_downloaded(self, tmp_path: Path) -> None: | ||
pathname = os.path.join( | ||
"tests", | ||
"data", | ||
"cms_mangrove_canopy", | ||
"CMS_Global_Map_Mangrove_Canopy_1665.zip", | ||
) | ||
root = str(tmp_path) | ||
shutil.copy(pathname, root) | ||
CMSGlobalMangroveCanopy(root, country="Angola") | ||
|
||
def test_corrupted(self, tmp_path: Path) -> None: | ||
with open( | ||
os.path.join(tmp_path, "CMS_Global_Map_Mangrove_Canopy_1665.zip"), "w" | ||
) as f: | ||
f.write("bad") | ||
with pytest.raises(RuntimeError, match="Dataset found, but corrupted."): | ||
CMSGlobalMangroveCanopy(root=str(tmp_path), country="Angola", checksum=True) | ||
|
||
def test_invalid_country(self) -> None: | ||
with pytest.raises(AssertionError): | ||
CMSGlobalMangroveCanopy(country="fakeCountry") | ||
|
||
def test_invalid_measurement(self) -> None: | ||
with pytest.raises(AssertionError): | ||
CMSGlobalMangroveCanopy(measurement="wrongMeasurement") | ||
|
||
def test_and(self, dataset: CMSGlobalMangroveCanopy) -> None: | ||
ds = dataset & dataset | ||
assert isinstance(ds, IntersectionDataset) | ||
|
||
def test_or(self, dataset: CMSGlobalMangroveCanopy) -> None: | ||
ds = dataset | dataset | ||
assert isinstance(ds, UnionDataset) | ||
|
||
def test_plot(self, dataset: CMSGlobalMangroveCanopy) -> None: | ||
query = dataset.bounds | ||
x = dataset[query] | ||
dataset.plot(x["mask"]) |
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
Oops, something went wrong.