Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstewart committed Sep 13, 2021
1 parent cb8381c commit 44a56ee
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
59 changes: 59 additions & 0 deletions tests/datasets/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import builtins
import glob
import math
import os
import pickle
import shutil
import sys
from datetime import datetime
from pathlib import Path
from typing import Any, Generator, Tuple

Expand All @@ -19,6 +21,7 @@
from torchgeo.datasets.utils import (
BoundingBox,
collate_dict,
disambiguate_timestamp,
download_and_extract_archive,
download_radiant_mlhub,
extract_archive,
Expand Down Expand Up @@ -206,6 +209,62 @@ def test_invalid_t(self) -> None:
BoundingBox(0, 1, 2, 3, 5, 4)


@pytest.mark.parametrize(
"date_string,format,min_datetime,max_datetime",
[
("", "", datetime(1970, 1, 1).timestamp(), datetime.max.timestamp()),
(
"2021",
"%Y",
datetime(2021, 1, 1, 0, 0, 0, 0).timestamp(),
datetime(2021, 12, 31, 23, 59, 59, 999999).timestamp(),
),
(
"2021-09",
"%Y-%m",
datetime(2021, 9, 1, 0, 0, 0, 0).timestamp(),
datetime(2021, 9, 30, 23, 59, 59, 999999).timestamp(),
),
(
"2021-09-13",
"%Y-%m-%d",
datetime(2021, 9, 13, 0, 0, 0, 0).timestamp(),
datetime(2021, 9, 13, 23, 59, 59, 999999).timestamp(),
),
(
"2021-09-13 17",
"%Y-%m-%d %H",
datetime(2021, 9, 13, 17, 0, 0, 0).timestamp(),
datetime(2021, 9, 13, 17, 59, 59, 999999).timestamp(),
),
(
"2021-09-13 17:21",
"%Y-%m-%d %H:%M",
datetime(2021, 9, 13, 17, 21, 0, 0).timestamp(),
datetime(2021, 9, 13, 17, 21, 59, 999999).timestamp(),
),
(
"2021-09-13 17:21:53",
"%Y-%m-%d %H:%M:%S",
datetime(2021, 9, 13, 17, 21, 53, 0).timestamp(),
datetime(2021, 9, 13, 17, 21, 53, 999999).timestamp(),
),
(
"2021-09-13 17:21:53:000123",
"%Y-%m-%d %H:%M:%S:%f",
datetime(2021, 9, 13, 17, 21, 53, 123).timestamp(),
datetime(2021, 9, 13, 17, 21, 53, 123).timestamp(),
),
],
)
def test_disambiguate_timestamp(
date_string: str, format: str, min_datetime: float, max_datetime: float
) -> None:
mint, maxt = disambiguate_timestamp(date_string, format)
assert math.isclose(mint, min_datetime)
assert math.isclose(maxt, max_datetime)


def test_collate_dict() -> None:
samples = [
{
Expand Down
6 changes: 3 additions & 3 deletions torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def __init__(
# Skip files that rasterio is unable to read
continue
else:
mint: float = datetime.min.timestamp()
maxt: float = datetime.max.timestamp()
mint = datetime(1970, 1, 1).timestamp()
maxt = datetime.max.timestamp()
if "date" in match.groupdict():
date = match.group("date")
mint, maxt = disambiguate_timestamp(date, self.date_format)
Expand Down Expand Up @@ -449,7 +449,7 @@ def __init__(
# Skip files that fiona is unable to read
continue
else:
mint = datetime.min.timestamp()
mint = datetime(1970, 1, 1).timestamp()
maxt = datetime.max.timestamp()
coords = (minx, maxx, miny, maxy, mint, maxt)
self.index.insert(i, coords, filepath)
Expand Down
20 changes: 11 additions & 9 deletions torchgeo/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import tarfile
import zipfile
from calendar import monthrange
from datetime import datetime
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

Expand Down Expand Up @@ -276,28 +277,29 @@ def disambiguate_timestamp(date_str: str, format: str) -> Tuple[float, float]:

# TODO: This is really tedious, is there a better way to do this?

if not any([f"{c}%" in format for c in "yYcxG"]):
if not any([f"%{c}" in format for c in "yYcxG"]):
# No temporal info
mint = datetime.min
mint = datetime(1970, 1, 1)
maxt = datetime.max
elif not any([f"{c}%" in format for c in "bBmjUWcxV"]):
elif not any([f"%{c}" in format for c in "bBmjUWcxV"]):
# Year resolution
maxt = datetime(mint.year, 12, 31, 23, 59, 59, 999999)
elif not any([f"{c}%" in format for c in "djcx"]):
elif not any([f"%{c}" in format for c in "djcx"]):
# Month resolution
maxt = datetime(mint.year, mint.month, 31, 23, 59, 59, 999999)
elif not any([f"{c}%" in format for c in "HIcX"]):
maxday = monthrange(mint.year, mint.month)[1]
maxt = datetime(mint.year, mint.month, maxday, 23, 59, 59, 999999)
elif not any([f"%{c}" in format for c in "HIcX"]):
# Day resolution
maxt = datetime(mint.year, mint.month, mint.day, 23, 59, 59, 999999)
elif not any([f"{c}%" in format for c in "McX"]):
elif not any([f"%{c}" in format for c in "McX"]):
# Hour resolution
maxt = datetime(mint.year, mint.month, mint.day, mint.hour, 59, 59, 999999)
elif not any([f"{c}%" in format for c in "ScX"]):
elif not any([f"%{c}" in format for c in "ScX"]):
# Minute resolution
maxt = datetime(
mint.year, mint.month, mint.day, mint.hour, mint.minute, 59, 999999
)
elif not any([f"{c}%" in format for c in "f"]):
elif not any([f"%{c}" in format for c in "f"]):
# Second resolution
maxt = datetime(
mint.year, mint.month, mint.day, mint.hour, mint.minute, mint.second, 999999
Expand Down

0 comments on commit 44a56ee

Please sign in to comment.