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 CMS Global Mangrove Canopy dataset #391

Merged
merged 10 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions docs/api/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Chesapeake Bay High-Resolution Land Cover Project
.. autoclass:: ChesapeakeWV
.. autoclass:: ChesapeakeCVPR

CMS Global Mangrove Canopy Dataset
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: CMS_Global_Mangrove_Canopy

Cropland Data Layer (CDL)
^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
66 changes: 66 additions & 0 deletions tests/data/cms_mangrove_canopy/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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
calebrob6 marked this conversation as resolved.
Show resolved Hide resolved

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}")
89 changes: 89 additions & 0 deletions tests/datasets/test_cms_mangrove_canopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
# 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 (
CMS_Global_Mangrove_Canopy,
IntersectionDataset,
UnionDataset,
)


def download_url(url: str, root: str, *args: str, **kwargs: str) -> None:
shutil.copy(url, root)


class TestCMS_Global_Mangrove_Canopy:
@pytest.fixture
def dataset(
self, monkeypatch: Generator[MonkeyPatch, None, None], tmp_path: Path
) -> CMS_Global_Mangrove_Canopy:
zipfile = "CMS_Global_Map_Mangrove_Canopy_1665.zip"
monkeypatch.setattr( # type: ignore[attr-defined]
CMS_Global_Mangrove_Canopy, "zipfile", zipfile
)

md5 = "45d894d4516d91212b70fd9a803e28cd"
monkeypatch.setattr( # type: ignore[attr-defined]
CMS_Global_Mangrove_Canopy, "md5", md5
)

root = os.path.join("tests", "data", "cms_mangrove_canopy")
transforms = nn.Identity() # type: ignore[attr-defined]
country = "Angola"

return CMS_Global_Mangrove_Canopy(
root, country=country, transforms=transforms, checksum=True
)

def test_getitem(self, dataset: CMS_Global_Mangrove_Canopy) -> 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."):
CMS_Global_Mangrove_Canopy(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)
CMS_Global_Mangrove_Canopy(root, country="Angola")

def test_invalid_country(self) -> None:
with pytest.raises(AssertionError):
CMS_Global_Mangrove_Canopy(country="fakeCountry")

def test_invalid_measurement(self) -> None:
with pytest.raises(AssertionError):
CMS_Global_Mangrove_Canopy(measurement="wrongMeasurement")

def test_and(self, dataset: CMS_Global_Mangrove_Canopy) -> None:
ds = dataset & dataset
assert isinstance(ds, IntersectionDataset)

def test_or(self, dataset: CMS_Global_Mangrove_Canopy) -> None:
ds = dataset | dataset
assert isinstance(ds, UnionDataset)

def test_plot(self, dataset: CMS_Global_Mangrove_Canopy) -> None:
query = dataset.bounds
x = dataset[query]
dataset.plot(x["mask"])
2 changes: 2 additions & 0 deletions torchgeo/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ChesapeakeVA,
ChesapeakeWV,
)
from .cms_mangrove_canopy import CMS_Global_Mangrove_Canopy
from .cowc import COWC, COWCCounting, COWCDetection
from .cv4a_kenya_crop_type import CV4AKenyaCropType
from .cyclone import TropicalCycloneWindEstimation
Expand Down Expand Up @@ -96,6 +97,7 @@
"ChesapeakeVA",
"ChesapeakeWV",
"ChesapeakeCVPR",
"CMS_Global_Mangrove_Canopy",
"Landsat",
"Landsat1",
"Landsat2",
Expand Down
Loading