-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgeofence_utils.py
280 lines (250 loc) · 9.95 KB
/
geofence_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Geofence-related utility functions.
Provides functions for checking geofencing rules and rolling up
classification labels based on geographic restrictions and taxonomic levels.
"""
from typing import Optional
from speciesnet.constants import Classification
from speciesnet.taxonomy_utils import get_ancestor_at_level
from speciesnet.taxonomy_utils import get_full_class_string
# Handy type aliases.
PredictionLabelType = str
PredictionScoreType = float
PredictionSourceType = str
PredictionType = tuple[PredictionLabelType, PredictionScoreType, PredictionSourceType]
def should_geofence_animal_classification(
label: str,
country: Optional[str],
admin1_region: Optional[str],
geofence_map: dict,
enable_geofence: bool,
) -> bool:
"""Checks whether to geofence animal prediction in a country or admin1_region.
Args:
label:
Animal label to check geofence rules for.
country:
Country (in ISO 3166-1 alpha-3 format) to check geofence rules for.
Optional.
admin1_region:
First-level administrative division (in ISO 3166-2 format) to check
geofence rules for. Optional.
geofence_map:
Dictionary mapping full class strings to geofence rules.
enable_geofence:
Whether geofencing is enabled.
Returns:
A boolean indicating whether to geofence given animal prediction.
"""
# Do not geofence if geofencing is disabled.
if not enable_geofence:
return False
# Do not geofence if country was not provided.
if not country:
return False
# Do not geofence if full class string is missing from the geofence map.
full_class_string = get_full_class_string(label)
if full_class_string not in geofence_map:
return False
# Check if we need to geofence based on "allow" rules.
allow_countries = geofence_map[full_class_string].get("allow")
if allow_countries:
if country not in allow_countries:
# Geofence when country was not explicitly allowed.
return True
else:
allow_admin1_regions = allow_countries[country]
if (
admin1_region
and allow_admin1_regions
and admin1_region not in allow_admin1_regions
):
# Geofence when admin1_region was not explicitly allowed.
return True
# Check if we need to geofence based on "block" rules.
block_countries = geofence_map[full_class_string].get("block")
if block_countries:
if country in block_countries:
block_admin1_regions = block_countries[country]
if not block_admin1_regions:
# Geofence when entire country was blocked.
return True
elif admin1_region and admin1_region in block_admin1_regions:
# Geofence when admin1_region was blocked.
return True
# Do not geofence if no rule enforced that.
return False
def roll_up_labels_to_first_matching_level( # pylint: disable=too-many-positional-arguments
labels: list[str],
scores: list[float],
country: Optional[str],
admin1_region: Optional[str],
target_taxonomy_levels: list[str],
non_blank_threshold: float,
taxonomy_map: dict,
geofence_map: dict,
enable_geofence: bool,
) -> Optional[PredictionType]:
"""Rolls up prediction labels to the first taxonomy level above given threshold.
Args:
labels:
List of classification labels.
scores:
List of classification scores.
country:
Country (in ISO 3166-1 alpha-3 format) associated with prediction.
Optional.
admin1_region:
First-level administrative division (in ISO 3166-2 format) associated
with prediction. Optional.
target_taxonomy_levels:
Ordered list of taxonomy levels at which to roll up classification
labels and check if the cumulative score passes the given threshold.
Levels must be a subset of: "species", "genus", "family", "order",
"class", "kingdom".
non_blank_threshold:
Min threshold at which the cumulative score is good enough to consider
the rollup successful.
taxonomy_map:
Dictionary mapping taxa to labels.
geofence_map:
Dictionary mapping full class strings to geofence rules.
enable_geofence:
Whether geofencing is enabled.
Returns:
A tuple of <label, score, prediction_source> describing the first taxonomy
level at which the cumulative score passes the given threshold. If no such
level exists, return `None`.
Raises:
ValueError:
If the taxonomy level if not one of: "species", "genus", "family",
"order", "class", "kingdom".
"""
expected_target_taxonomy_levels = {
"species",
"genus",
"family",
"order",
"class",
"kingdom",
}
unknown_target_taxonomy_levels = set(target_taxonomy_levels).difference(
expected_target_taxonomy_levels
)
if unknown_target_taxonomy_levels:
raise ValueError(
"Unexpected target taxonomy level(s): "
f"{unknown_target_taxonomy_levels}. "
f"Expected only levels from the set: {expected_target_taxonomy_levels}."
)
# Accumulate scores at each taxonomy level and, if they pass the desired
# threshold, return that rollup label.
for taxonomy_level in target_taxonomy_levels:
accumulated_scores = {}
for label, score in zip(labels, scores):
rollup_label = get_ancestor_at_level(
label=label, taxonomy_level=taxonomy_level, taxonomy_map=taxonomy_map
)
if rollup_label:
new_score = accumulated_scores.get(rollup_label, 0.0) + score
accumulated_scores[rollup_label] = new_score
max_rollup_label = None
max_rollup_score = 0.0
for rollup_label, rollup_score in accumulated_scores.items():
if (
rollup_score > max_rollup_score
and not should_geofence_animal_classification(
rollup_label, country, admin1_region, geofence_map, enable_geofence
)
):
max_rollup_label = rollup_label
max_rollup_score = rollup_score
if max_rollup_score > non_blank_threshold and max_rollup_label:
return (
max_rollup_label,
max_rollup_score,
f"classifier+rollup_to_{taxonomy_level}",
)
return None
def geofence_animal_classification(
*,
labels: list[str],
scores: list[float],
country: Optional[str],
admin1_region: Optional[str],
taxonomy_map: dict,
geofence_map: dict,
enable_geofence: bool,
) -> PredictionType:
"""Geofences animal prediction in a country or admin1_region.
Under the hood, this also rolls up the labels every time it encounters a
geofenced label.
Args:
labels:
List of classification labels.
scores:
List of classification scores.
country:
Country (in ISO 3166-1 alpha-3 format) associated with prediction.
Optional.
admin1_region:
First-level administrative division (in ISO 3166-2 format) associated
with prediction. Optional.
taxonomy_map:
Dictionary mapping taxa to labels.
geofence_map:
Dictionary mapping full class strings to geofence rules.
enable_geofence:
Whether geofencing is enabled.
Returns:
A tuple of <label, score, prediction_source> describing the result of the
combined geofence and rollup operations.
"""
if should_geofence_animal_classification(
labels[0], country, admin1_region, geofence_map, enable_geofence
):
rollup = roll_up_labels_to_first_matching_level(
labels=labels,
scores=scores,
country=country,
admin1_region=admin1_region,
target_taxonomy_levels=["family", "order", "class", "kingdom"],
# Force the rollup to pass the top classification score.
non_blank_threshold=scores[0] - 1e-10,
taxonomy_map=taxonomy_map,
geofence_map=geofence_map,
enable_geofence=enable_geofence,
)
if rollup:
rollup_label, rollup_score, rollup_source = rollup
return (
rollup_label,
rollup_score,
f"classifier+geofence+{rollup_source[len('classifier+'):]}",
)
else:
# Normally, this return statement should never be reached since the
# animal rollup would eventually succeed (even though that may be at
# "kingdom" level, as a last resort). The only scenario when this could
# still be reached is if the method was incorrectly called with a list
# of non-animal labels (e.g. blanks, vehicles). In this case it's best
# to return an unknown classification, while propagating the top score.
return (
Classification.UNKNOWN,
scores[0],
"classifier+geofence+rollup_failed",
)
else:
return labels[0], scores[0], "classifier"