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

Adding South America Soybean Dataset #1668

Merged
merged 82 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
e510a06
Created file for South America Soybean dataset and added it to __init…
cookie-kyu Oct 16, 2023
c67cae4
Updated south_america_soybean.py
cookie-kyu Oct 25, 2023
5153b0c
Merge branch 'microsoft:main' into main
cookie-kyu Nov 8, 2023
79e6970
Added tests
cookie-kyu Nov 8, 2023
5b0e053
Merge branch 'main' of https://github.com/cookie-kyu/torchgeo
cookie-kyu Nov 8, 2023
ac40ec1
Updated data.py
cookie-kyu Nov 14, 2023
7655805
Merge branch 'main' into main
cookie-kyu Nov 14, 2023
fd01011
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
46db62b
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
f53d822
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
1ed0c28
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
7a8e0cf
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
6b8b143
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
38bc7cb
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
95f5368
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
b6c9efc
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
5bf910b
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
2c2cc42
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
3eb2e33
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
355d8f1
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
430674a
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
5cc76e7
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
6cc7e7c
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
4f41e13
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
45b306e
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
558cf44
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
4c7a5aa
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
48f1b63
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
281fda2
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
12d1ea3
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
bed3b03
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
c66359b
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
3886f0b
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
824ce88
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
557b169
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
55c1720
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
6749974
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
d1e7a01
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
e788624
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
1580964
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
a5262ce
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
d8ca8d6
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
0a256a3
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
c0070ed
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
1fe3a16
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
8a6d0b0
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Nov 18, 2023
1385b4c
Merge branch 'main' into main
cookie-kyu Nov 18, 2023
74356ee
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
6b59cfd
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
61579dd
Update tests/datasets/test_south_america_soybean.py
cookie-kyu Nov 18, 2023
e67613f
Updated tests
cookie-kyu Dec 1, 2023
97ba0fc
fixed an error in init
cookie-kyu Jan 4, 2024
305ea44
fixed some path inconsistencies
cookie-kyu Jan 15, 2024
d3e82ed
fixed all errors
cookie-kyu Jan 22, 2024
4ddaf70
Fix comments
cookie-kyu Jan 23, 2024
ddecb3c
added dataset to datasets.rst
cookie-kyu Jan 26, 2024
7d8eefc
edit datasets.rst
cookie-kyu Jan 26, 2024
243d488
pushed again
cookie-kyu Jan 26, 2024
66d0e93
Delete tests/data/south_america_soybean/.DS_Store
cookie-kyu Jan 26, 2024
4278c23
Update docs/api/datasets.rst
cookie-kyu Jan 26, 2024
3b9d606
Edited datasets.rst
cookie-kyu Jan 26, 2024
83af882
Edited datasets.rst
cookie-kyu Jan 26, 2024
c900488
Fixed styling
cookie-kyu Jan 26, 2024
4a506ef
Merge branch 'main' into main
adamjstewart Jan 27, 2024
d147b7f
Fix docstring formatting
adamjstewart Jan 27, 2024
0326926
Fix whitespace
adamjstewart Jan 27, 2024
f739867
Add blank line
adamjstewart Jan 27, 2024
fb5ea8d
Merge branch 'main' into main
cookie-kyu Jan 27, 2024
abe019a
Fixed download urls
cookie-kyu Jan 28, 2024
ffa24d1
Merge branch 'main' of https://github.com/cookie-kyu/torchgeo
cookie-kyu Jan 28, 2024
f130c0c
Update geo_datasets.csv
cookie-kyu Jan 28, 2024
50c8460
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Jan 29, 2024
d265ea7
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Jan 29, 2024
96af9e5
Update torchgeo/datasets/south_america_soybean.py
cookie-kyu Jan 29, 2024
773bd41
Updated geo_datasets.csv and added years parameter to class
cookie-kyu Jan 31, 2024
7bc9ea9
Merge branch 'main' of https://github.com/cookie-kyu/torchgeo
cookie-kyu Jan 31, 2024
d84a52a
Delete tests/data/.DS_Store
cookie-kyu Jan 31, 2024
5e76506
Delete tests/.DS_Store
cookie-kyu Jan 31, 2024
f78d63d
Merge branch 'main' into main
cookie-kyu Jan 31, 2024
7dca57b
Update south_america_soybean.py
cookie-kyu Feb 6, 2024
c48bbad
Merge branch 'main' into main
cookie-kyu Feb 6, 2024
477ddd6
Fix docstring formatting
adamjstewart Feb 6, 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
1 change: 1 addition & 0 deletions docs/api/geo_datasets.csv
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Dataset,Type,Source,Size (px),Resolution (m)
`NLCD`_,Masks,Landsat,-,30
`Open Buildings`_,Geometries,"Maxar, CNES/Airbus",-,-
`Sentinel`_,Imagery,Sentinel,"10,000x10,000",10
`South America Soybean`_,Masks,Sentinel-2,-,10
Binary file added tests/.DS_Store
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file added tests/data/.DS_Store
Binary file not shown.
Binary file added tests/data/south_america_soybean/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
87 changes: 87 additions & 0 deletions tests/data/south_america_soybean/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#os.environ['PROJ_LIB'] = r'E:\Programs\anaconda3\envs\gis\Library\share\proj'
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
import hashlib
import os
import shutil

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

SIZE = 32
wkt = """
PROJCS["Albers Conical Equal Area",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Albers_Conic_Equal_Area"],
PARAMETER["latitude_of_center",23],
PARAMETER["longitude_of_center",-96],
PARAMETER["standard_parallel_1",29.5],
PARAMETER["standard_parallel_2",45.5],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["meters",1],
AXIS["Easting",EAST],
AXIS["Northing",NORTH]]
"""


np.random.seed(0)
files = ["South_America_Soybean_2002.tif", "South_America_Soybean_2021.tif"]

def create_file(path: str, dtype: str):
"""Create the testing file."""
profile = {
"driver": "GTiff",
"dtype": dtype,
"count": 1,
#"crs": CRS.from_wkt(wkt),
"transform": Affine(
0.0002499999999999943131,
0.0,
-82.0005000000000024,
0.0,
-0.0002499999999999943131,
0.0005000000000000,
),
"height": SIZE,
"width": SIZE,
"compress": "lzw",
"predictor": 2,
}

allowed_values = [0, 1]

Z = np.random.choice(allowed_values, size=(SIZE, SIZE))

with rasterio.open(path, "w", **profile) as src:
src.write(Z, 1)

if __name__ == "__main__":
dir = os.path.join(os.getcwd(), "SouthAmericaSoybean")
print(dir)
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
if os.path.exists(dir) and os.path.isdir(dir):
shutil.rmtree(dir)

os.makedirs(dir, exist_ok=True)

for file in files:
create_file(os.path.join(dir, file), dtype="int8")

# Compress data
shutil.make_archive("SouthAmericaSoybean", "zip", ".", dir)

# Compute checksums
with open("SouthAmericaSoybean.zip", "rb") as f:
md5 = hashlib.md5(f.read()).hexdigest()
print(f"SouthAmericaSoybean.zip: {md5}")
110 changes: 110 additions & 0 deletions tests/datasets/test_south_america_soybean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
import shutil
from pathlib import Path

import matplotlib.pyplot as plt
import pytest
import torch
import torch.nn as nn
from pytest import MonkeyPatch
from rasterio.crs import CRS

import torchgeo.datasets.utils
from torchgeo.datasets import south_america_soybean, BoundingBox, IntersectionDataset, UnionDataset
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved


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


class TestSouthAmericaSoybean:
@pytest.fixture
def dataset(self, monkeypatch: MonkeyPatch, tmp_path: Path) -> south_america_soybean:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
monkeypatch.setattr(torchgeo.datasets.southamerica_soybean, "download_url", download_url)
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
transforms = nn.Identity()
md5s = {
2002: "8a4a9dcea54b3ec7de07657b9f2c0893",
2021: "edff3ada13a1a9910d1fe844d28ae4f",
}
monkeypatch.setattr(south_america_soybean, "md5s", md5s)

url = os.path.join("tests", "data", "south_america_soybean", "SouthAmerica_Soybean_{}.tif")
monkeypatch.setattr(south_america_soybean, "url", url)


return south_america_soybean(
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
transforms=transforms,
download=True,
checksum=True,
years=[2002, 2021],
)

def test_getitem(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
x = dataset[dataset.bounds]
assert isinstance(x, dict)
assert isinstance(x["crs"], CRS)
assert isinstance(x["mask"], torch.Tensor)

def test_classes(self) -> None:
root = os.path.join("tests", "data", "southamerica_soybean")
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
classes = list(south_america_soybean.cmap.keys())[0:2]
ds = south_america_soybean(root, years=[2021], classes=classes)
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
sample = ds[ds.bounds]
mask = sample["mask"]
assert mask.max() < len(classes)

def test_and(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
ds = dataset & dataset
assert isinstance(ds, IntersectionDataset)

def test_or(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
ds = dataset | dataset
assert isinstance(ds, UnionDataset)

def test_already_extracted(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
south_america_soybean(dataset.paths, download=True, years=[2021])
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved

def test_already_downloaded(self, tmp_path: Path) -> None:
pathname = os.path.join("tests", "data", "southamerica_soybean", "SouthAmerica_Soybean_2021.tif")
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
root = str(tmp_path)

shutil.copy(pathname, root)
south_america_soybean(root, years=[2021])
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved

def test_invalid_year(self, tmp_path: Path) -> None:
with pytest.raises(
AssertionError,
match="south_america_soybean data product only exists for the following years:",
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
):
south_america_soybean(str(tmp_path), years=[1996])

def test_invalid_classes(self) -> None:
with pytest.raises(AssertionError):
south_america_soybean(classes=[-1])
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved

with pytest.raises(AssertionError):
south_america_soybean(classes=[11])
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved

def test_plot(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
query = dataset.bounds
x = dataset[query]
dataset.plot(x, suptitle="Test")
plt.close()

def test_plot_prediction(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
query = dataset.bounds
x = dataset[query]
x["prediction"] = x["mask"].clone()
dataset.plot(x, suptitle="Prediction")
plt.close()

def test_not_downloaded(self, tmp_path: Path) -> None:
with pytest.raises(RuntimeError, match="Dataset not found"):
south_america_soybean(str(tmp_path))
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved

def test_invalid_query(self, dataset: south_america_soybean) -> None:
cookie-kyu marked this conversation as resolved.
Show resolved Hide resolved
query = BoundingBox(0, 0, 0, 0, 0, 0)
with pytest.raises(
IndexError, match="query: .* not found in index with bounds:"
):
dataset[query]
2 changes: 2 additions & 0 deletions torchgeo/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
from .sentinel import Sentinel, Sentinel1, Sentinel2
from .skippd import SKIPPD
from .so2sat import So2Sat
from .south_america_soybean import SouthAmericaSoybean
from .spacenet import (
SpaceNet,
SpaceNet1,
Expand Down Expand Up @@ -173,6 +174,7 @@
"Sentinel",
"Sentinel1",
"Sentinel2",
"SouthAmericaSoybean"
# NonGeoDataset
"ADVANCE",
"BeninSmallHolderCashews",
Expand Down
Loading
Loading