Skip to content

Commit

Permalink
allow empty category in stats (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Oct 21, 2021
1 parent d61ed0b commit 1e24c85
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.0.0a2 (2021-10-21)

* Allow `rio_tiler.utils.get_array_statistics` to return `0` for unfound category, instead of raising an error (https://github.com/cogeotiff/rio-tiler/pull/443)

# 3.0.0a1 (2021-10-20)

**breaking changes**
Expand Down
2 changes: 1 addition & 1 deletion rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_array_statistics(
numpy.array(categories).astype(keys.dtype) if categories else keys
).tolist()
histogram = [
[out_dict[x] for x in h_keys],
[out_dict.get(x, 0) for x in h_keys],
h_keys,
]
else:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ def test_get_array_statistics():
assert len(stats[0]["histogram"][0]) == stats[0]["unique"]
assert len(stats[0]["histogram"][1]) == stats[0]["unique"]

# histogram return only the categories passed
stats = utils.get_array_statistics(arr, categorical=True, categories=[1, 10, 12])
assert len(stats[0]["histogram"][0]) == 3
assert len(stats[0]["histogram"][1]) == 3

# test if providing a category not in the data (1000000)
stats = utils.get_array_statistics(
arr, categorical=True, categories=[1, 10, 12, 1000000]
)
assert len(stats[0]["histogram"][0]) == 4
assert len(stats[0]["histogram"][1]) == 4
assert stats[0]["histogram"][0][3] == 0.0 # there is no value 1000000

0 comments on commit 1e24c85

Please sign in to comment.