Skip to content

Commit 03d37c1

Browse files
committed
fix get_stats when an empty mask is passed
1 parent b4da49b commit 03d37c1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

geoutils/raster/raster.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ def _statistics(self, band: int = 1, counts: tuple[int, int] | None = None) -> d
19181918
"90th percentile": np.nanpercentile(mdata, 90),
19191919
"LE90": linear_error(mdata, interval=90),
19201920
"NMAD": nmad(data),
1921-
"RMSE": np.sqrt(np.ma.mean(np.square(data))),
1921+
"RMSE": np.sum(np.sqrt(np.ma.mean(np.square(data)))),
19221922
"Standard deviation": np.ma.std(data),
19231923
"Valid count": valid_count,
19241924
"Total count": data.size,
@@ -1932,7 +1932,7 @@ def _statistics(self, band: int = 1, counts: tuple[int, int] | None = None) -> d
19321932
"Valid inlier count": valid_inlier_count,
19331933
"Total inlier count": counts[1],
19341934
"Percentage inlier points": (valid_inlier_count / counts[0]) * 100,
1935-
"Percentage valid inlier points": (valid_inlier_count / counts[1]) * 100,
1935+
"Percentage valid inlier points": (valid_inlier_count / counts[1]) * 100 if counts[1] != 0 else 0,
19361936
}
19371937
)
19381938

tests/test_raster/test_raster.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -2002,11 +2002,13 @@ def test_stats(self, example: str, caplog) -> None:
20022002
"Percentage valid points",
20032003
]
20042004

2005+
stat_types = (int, float, np.integer, np.floating)
2006+
20052007
# Full stats
20062008
stats = raster.get_stats()
20072009
for name in expected_stats:
20082010
assert name in stats
2009-
assert stats.get(name) is not None
2011+
assert isinstance(stats.get(name), stat_types)
20102012

20112013
# With mask
20122014
inlier_mask = raster.get_mask()
@@ -2018,10 +2020,21 @@ def test_stats(self, example: str, caplog) -> None:
20182020
"Percentage valid inlier points",
20192021
]:
20202022
assert name in stats_masked
2021-
assert stats_masked.get(name) is not None
2023+
assert isinstance(stats_masked.get(name), stat_types)
20222024
stats_masked.pop(name)
20232025
assert stats_masked == stats
20242026

2027+
# Empty mask
2028+
empty_mask = np.ones_like(inlier_mask)
2029+
stats_masked = raster.get_stats(inlier_mask=empty_mask)
2030+
for name in [
2031+
"Valid inlier count",
2032+
"Total inlier count",
2033+
"Percentage inlier points",
2034+
"Percentage valid inlier points",
2035+
]:
2036+
assert stats_masked.get(name) == 0
2037+
20252038
# Single stat
20262039
for name in expected_stats:
20272040
stat = raster.get_stats(stats_name=name)

0 commit comments

Comments
 (0)