Skip to content

Commit

Permalink
Merge pull request #93 from nasaharvest/start-date-less-restrictive
Browse files Browse the repository at this point in the history
[Fix] Double dates in export files
  • Loading branch information
ivanzvonkov authored Jul 7, 2022
2 parents 59595c9 + e95bcbe commit 549181d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
3 changes: 1 addition & 2 deletions cropharvest/eo/eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ def export_for_bbox(
raise ValueError(f"Start date {start_date} is after end date {end_date}")

ee_bbox = EEBoundingBox.from_bounding_box(bounding_box=bbox, padding_metres=0)
general_identifier = f"{bbox_name}_{str(start_date)}_{str(end_date)}"
if metres_per_polygon is not None:
regions = ee_bbox.to_polygons(metres_per_patch=metres_per_polygon)
ids = [f"batch_{i}/{i}" for i in range(len(regions))]
Expand All @@ -438,7 +437,7 @@ def export_for_bbox(
for identifier, region in zip(ids, regions):
return_obj[identifier] = self._export_for_polygon(
polygon=region,
polygon_identifier=f"{general_identifier}/{identifier}",
polygon_identifier=f"{bbox_name}/{identifier}",
start_date=start_date,
end_date=end_date,
file_dimensions=file_dimensions,
Expand Down
6 changes: 3 additions & 3 deletions cropharvest/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def __init__(
@staticmethod
def start_date_from_str(path: Union[Path, str]) -> datetime:
dates = re.findall(r"\d{4}-\d{2}-\d{2}", str(path))
if len(dates) != 2:
raise ValueError(f"{path} should have start and end date")
start_date_str, _ = dates
if len(dates) < 1:
raise ValueError(f"{path} should have at least one date")
start_date_str = dates[0]
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
return start_date

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
author="Gabriel Tseng",
author_email="[email protected]",
url="https://github.com/nasaharvest/cropharvest",
version="0.4.0",
version="0.4.1",
classifiers=[
"Programming Language :: Python :: 3",
"License :: Other/Proprietary License",
Expand Down
4 changes: 2 additions & 2 deletions test/cropharvest/eo/test_eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_export_for_bbox(mock_export_for_polygon, metres_per_polygon, mock_polyg
mock_export_for_polygon.assert_called_with(
end_date=end_date,
polygon=None,
polygon_identifier=f"Togo_{start_date}_{end_date}/batch/0",
polygon_identifier="Togo/batch/0",
start_date=start_date,
file_dimensions=None,
test=True,
Expand All @@ -143,7 +143,7 @@ def test_export_for_bbox(mock_export_for_polygon, metres_per_polygon, mock_polyg
call(
end_date=end_date,
polygon=None,
polygon_identifier=f"Togo_{start_date}_{end_date}/batch_{i}/{i}",
polygon_identifier=f"Togo/batch_{i}/{i}",
start_date=start_date,
file_dimensions=None,
test=True,
Expand Down
15 changes: 15 additions & 0 deletions test/cropharvest/test_inference.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from datetime import datetime
from pathlib import Path
import numpy as np
Expand All @@ -13,6 +15,19 @@ def test_start_date_from_str():
assert actual_start_date == expected_start_date


def test_start_date_from_str_none():
with pytest.raises(ValueError):
Inference.start_date_from_str("98-togo")


def test_start_date_from_str_more_than_2():
actual_start_date = Inference.start_date_from_str(
"98-togo_2019-02-06_2020-02-01_2019-02-06_2020-02-01"
)
expected_start_date = datetime(2019, 2, 6, 0, 0)
assert actual_start_date == expected_start_date


def test_combine_predictions():
flat_lat = np.array([14.95313164, 14.95313164, 14.95313164, 14.95313164, 14.95313164])
flat_lon = np.array([-86.25070894, -86.25061911, -86.25052928, -86.25043945, -86.25034962])
Expand Down

0 comments on commit 549181d

Please sign in to comment.