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

temp commit qa score #53

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion EO_Floods/providers/hydrafloods/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ def quality_score(self) -> list[float]:
"MODIS",
]: # these datasets consist of global images, need to be clipped first before reducing
self.obj.apply_func(func=lambda x: x.clip(self.region), inplace=True)
self.obj.apply_func(func=calc_quality_score, inplace=True, band=self.qa_band)
self.obj.apply_func(func=calc_quality_score, inplace=True, band=self.qa_band, geom=self.region)
qa_score = self.obj.collection.aggregate_array("qa_score").getInfo()
return [round(score, 2) for score in qa_score]
25 changes: 17 additions & 8 deletions EO_Floods/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

def coords_to_ee_geom(coords: list) -> ee.geometry.Geometry:
"""Convert a list of xmin, ymin, xmax, ymax coords to an ee.Geometry."""
if len(coords) == 4: # noqa:PLR2004
if len(coords) == 4: # noqa:PLR2004
xmin, ymin, xmax, ymax = coords
if not -180 <= xmin <= 180 or not -180 <= xmax <= 180: # noqa:PLR2004
if not -180 <= xmin <= 180 or not -180 <= xmax <= 180: # noqa:PLR2004
err_msg = "X values are not within the longitudinal range"
raise ValueError(err_msg)
if not -90 <= ymin <= 90 or not -90 <= ymax <= 90: # noqa:PLR2004
if not -90 <= ymin <= 90 or not -90 <= ymax <= 90: # noqa:PLR2004
err_msg = "Y values are not within the latitudinal range"
raise ValueError(err_msg)
return ee.Geometry.BBox(xmin, ymin, xmax, ymax)
Expand Down Expand Up @@ -118,8 +118,8 @@ def get_dates_in_time_range(start_date_str: str, end_date_str: str) -> list:

"""
# Convert start and end date strings to datetime objects
start_date = datetime.strptime(start_date_str, "%Y-%m-%d") # noqa:DTZ007
end_date = datetime.strptime(end_date_str, "%Y-%m-%d") # noqa:DTZ007
start_date = datetime.strptime(start_date_str, "%Y-%m-%d") # noqa:DTZ007
end_date = datetime.strptime(end_date_str, "%Y-%m-%d") # noqa:DTZ007

# Initialize an empty list to store the dates
date_list = []
Expand All @@ -134,7 +134,9 @@ def get_dates_in_time_range(start_date_str: str, end_date_str: str) -> list:


def calc_quality_score(
image: ee.Image, band: str, geom: ee.Geometry | None = None,
image: ee.Image,
band: str,
geom: ee.Geometry | None = None,
) -> ee.Image:
"""Calculate a quality score for an ee.Image.

Expand All @@ -158,15 +160,22 @@ def calc_quality_score(
masked_pixel_count = (
image.select(band)
.reduceRegion(
reducer=ee.Reducer.count(), geometry=geom, scale=30, maxPixels=1e10,
reducer=ee.Reducer.count(),
geometry=geom,
scale=30,
maxPixels=1e10,
)
.get(band)
)
# TODO rasterize geom and get total pixel count
total_pixel_count = (
image.select(band)
.unmask()
.reduceRegion(
reducer=ee.Reducer.count(), geometry=geom, scale=30, maxPixels=1e10,
reducer=ee.Reducer.count(),
geometry=geom,
scale=30,
maxPixels=1e10,
)
.get(band)
)
Expand Down
Loading