forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.py
More file actions
475 lines (390 loc) · 16.1 KB
/
filters.py
File metadata and controls
475 lines (390 loc) · 16.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""This module stores filters related to SageMaker JumpStart."""
from __future__ import absolute_import
from ast import literal_eval
from enum import Enum
from typing import Dict, List, Union, Any
from sagemaker.jumpstart.types import JumpStartDataHolderType
class BooleanValues(str, Enum):
"""Enum class for boolean values.
This is a status value that an ``Operand`` can resolve to.
"""
TRUE = "true"
FALSE = "false"
UNKNOWN = "unknown"
UNEVALUATED = "unevaluated"
class FilterOperators(str, Enum):
"""Enum class for filter operators for JumpStart models."""
EQUALS = "equals"
NOT_EQUALS = "not_equals"
IN = "in"
NOT_IN = "not_in"
class SpecialSupportedFilterKeys(str, Enum):
"""Enum class for special supported filter keys."""
TASK = "task"
FRAMEWORK = "framework"
FILTER_OPERATOR_STRING_MAPPINGS = {
FilterOperators.EQUALS: ["===", "==", "equals", "is"],
FilterOperators.NOT_EQUALS: ["!==", "!=", "not equals", "is not"],
FilterOperators.IN: ["in"],
FilterOperators.NOT_IN: ["not in"],
}
_PAD_ALPHABETIC_OPERATOR = (
lambda operator: f" {operator} "
if any(character.isalpha() for character in operator)
else operator
)
ACCEPTABLE_OPERATORS_IN_PARSE_ORDER = (
list(map(_PAD_ALPHABETIC_OPERATOR, FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.NOT_EQUALS]))
+ list(map(_PAD_ALPHABETIC_OPERATOR, FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.NOT_IN]))
+ list(map(_PAD_ALPHABETIC_OPERATOR, FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.EQUALS]))
+ list(map(_PAD_ALPHABETIC_OPERATOR, FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.IN]))
)
SPECIAL_SUPPORTED_FILTER_KEYS = set(
[
SpecialSupportedFilterKeys.TASK,
SpecialSupportedFilterKeys.FRAMEWORK,
]
)
class Operand:
"""Operand class for filtering JumpStart content."""
def __init__(
self, unresolved_value: Any, resolved_value: BooleanValues = BooleanValues.UNEVALUATED
):
self.unresolved_value: Any = unresolved_value
self._resolved_value: BooleanValues = resolved_value
def __iter__(self) -> Any:
"""Returns an iterator."""
yield self
def eval(self) -> None:
"""Evaluates operand."""
return
@property
def resolved_value(self) -> BooleanValues:
"""Getter method for resolved_value."""
return self._resolved_value
@resolved_value.setter
def resolved_value(self, new_resolved_value: Any) -> None:
"""Setter method for resolved_value. Resolved_value must be of type ``BooleanValues``."""
if isinstance(new_resolved_value, BooleanValues):
self._resolved_value = new_resolved_value
return
raise RuntimeError(
"Resolved value must be of type BooleanValues, "
f"but got type {type(new_resolved_value)}."
)
@staticmethod
def validate_operand(operand: Any) -> Any:
"""Validate operand and return ``Operand`` object.
Args:
operand (Any): The operand to validate.
Raises:
RuntimeError: If the operand is not of ``Operand`` or ``str`` type.
"""
if isinstance(operand, str):
if operand.lower() == BooleanValues.TRUE.lower():
operand = Operand(operand, resolved_value=BooleanValues.TRUE)
elif operand.lower() == BooleanValues.FALSE.lower():
operand = Operand(operand, resolved_value=BooleanValues.FALSE)
elif operand.lower() == BooleanValues.UNKNOWN.lower():
operand = Operand(operand, resolved_value=BooleanValues.UNKNOWN)
else:
operand = Operand(parse_filter_string(operand))
elif not issubclass(type(operand), Operand):
raise RuntimeError(f"Operand '{operand}' is not supported.")
return operand
class Operator(Operand):
"""Operator class for filtering JumpStart content.
An operator in this case corresponds to an operand that is also an operation.
For example, given the expression ``(True or True) and True``,
``(True or True)`` is an operand to an ``And`` expression, but is also itself an
operator. ``(True or True) and True`` would also be considered an operator.
"""
def __init__(
self,
resolved_value: BooleanValues = BooleanValues.UNEVALUATED,
unresolved_value: Any = None,
):
"""Initializes ``Operator`` instance.
Args:
resolved_value (BooleanValues): Optional. The resolved value of the operator.
(Default: BooleanValues.UNEVALUATED).
unresolved_value (Any): Optional. The unresolved value of the operator.
(Default: None).
"""
super().__init__(unresolved_value=unresolved_value, resolved_value=resolved_value)
def eval(self) -> None:
"""Evaluates operator."""
return
def __iter__(self) -> Any:
"""Returns an iterator."""
yield self
class And(Operator):
"""And operator class for filtering JumpStart content."""
def __init__(
self,
*operands: Union[Operand, str],
) -> None:
"""Instantiates And object.
Args:
operand (Operand): Operand for And-ing.
Raises:
RuntimeError: If the operands cannot be validated.
"""
self.operands: List[Operand] = list(operands) # type: ignore
for i in range(len(self.operands)):
self.operands[i] = Operand.validate_operand(self.operands[i])
super().__init__()
def eval(self) -> None:
"""Evaluates operator.
Raises:
RuntimeError: If the operands remain unevaluated after calling ``eval``,
or if the resolved value isn't a ``BooleanValues`` type.
"""
incomplete_expression = False
for operand in self.operands:
if not issubclass(type(operand), Operand):
raise RuntimeError(
f"Operand must be subclass of ``Operand``, but got {type(operand)}"
)
if operand.resolved_value == BooleanValues.UNEVALUATED:
operand.eval()
if operand.resolved_value == BooleanValues.UNEVALUATED:
raise RuntimeError(
"Operand remains unevaluated after calling ``eval`` function."
)
if operand.resolved_value == BooleanValues.FALSE:
self.resolved_value = BooleanValues.FALSE
return
if operand.resolved_value == BooleanValues.UNKNOWN:
incomplete_expression = True
if not incomplete_expression:
self.resolved_value = BooleanValues.TRUE
else:
self.resolved_value = BooleanValues.UNKNOWN
def __iter__(self) -> Any:
"""Returns an iterator."""
for operand in self.operands:
yield from operand
yield self
class Constant(Operator):
"""Constant operator class for filtering JumpStart content."""
def __init__(
self,
constant: BooleanValues,
):
"""Instantiates Constant operator object.
Args:
constant (BooleanValues): Value of constant.
"""
super().__init__(constant)
def eval(self) -> None:
"""Evaluates constant"""
return
def __iter__(self) -> Any:
"""Returns an iterator."""
yield self
class Identity(Operator):
"""Identity operator class for filtering JumpStart content."""
def __init__(
self,
operand: Union[Operand, str],
):
"""Instantiates Identity object.
Args:
operand (Union[Operand, str]): Operand for identity operation.
"""
super().__init__()
self.operand = Operand.validate_operand(operand)
def __iter__(self) -> Any:
"""Returns an iterator."""
yield self
yield from self.operand
def eval(self) -> None:
"""Evaluates operator.
Raises:
RuntimeError: If the operand remains unevaluated after calling ``eval``,
or if the resolved value isn't a ``BooleanValues`` type.
"""
if not issubclass(type(self.operand), Operand):
raise RuntimeError(
f"Operand must be subclass of ``Operand``, but got {type(self.operand)}"
)
if self.operand.resolved_value == BooleanValues.UNEVALUATED:
self.operand.eval()
if self.operand.resolved_value == BooleanValues.UNEVALUATED:
raise RuntimeError("Operand remains unevaluated after calling ``eval`` function.")
if not isinstance(self.operand.resolved_value, BooleanValues):
raise RuntimeError(self.operand.resolved_value)
self.resolved_value = self.operand.resolved_value
class Or(Operator):
"""Or operator class for filtering JumpStart content."""
def __init__(
self,
*operands: Union[Operand, str],
) -> None:
"""Instantiates Or object.
Args:
operands (Operand): Operand for Or-ing.
Raises:
RuntimeError: If the operands cannot be validated.
"""
self.operands: List[Operand] = list(operands) # type: ignore
for i in range(len(self.operands)):
self.operands[i] = Operand.validate_operand(self.operands[i])
super().__init__()
def eval(self) -> None:
"""Evaluates operator.
Raises:
RuntimeError: If the operands remain unevaluated after calling ``eval``,
or if the resolved value isn't a ``BooleanValues`` type.
"""
incomplete_expression = False
for operand in self.operands:
if not issubclass(type(operand), Operand):
raise RuntimeError(
f"Operand must be subclass of ``Operand``, but got {type(operand)}"
)
if operand.resolved_value == BooleanValues.UNEVALUATED:
operand.eval()
if operand.resolved_value == BooleanValues.UNEVALUATED:
raise RuntimeError(
"Operand remains unevaluated after calling ``eval`` function."
)
if operand.resolved_value == BooleanValues.TRUE:
self.resolved_value = BooleanValues.TRUE
return
if operand.resolved_value == BooleanValues.UNKNOWN:
incomplete_expression = True
if not incomplete_expression:
self.resolved_value = BooleanValues.FALSE
else:
self.resolved_value = BooleanValues.UNKNOWN
def __iter__(self) -> Any:
"""Returns an iterator."""
for operand in self.operands:
yield from operand
yield self
class Not(Operator):
"""Not operator class for filtering JumpStart content."""
def __init__(
self,
operand: Union[Operand, str],
) -> None:
"""Instantiates Not object.
Args:
operand (Operand): Operand for Not-ing.
"""
self.operand: Operand = Operand.validate_operand(operand)
super().__init__()
def eval(self) -> None:
"""Evaluates operator.
Raises:
RuntimeError: If the operand remains unevaluated after calling ``eval``,
or if the resolved value isn't a ``BooleanValues`` type.
"""
if not issubclass(type(self.operand), Operand):
raise RuntimeError(
f"Operand must be subclass of ``Operand``, but got {type(self.operand)}"
)
if self.operand.resolved_value == BooleanValues.UNEVALUATED:
self.operand.eval()
if self.operand.resolved_value == BooleanValues.UNEVALUATED:
raise RuntimeError("Operand remains unevaluated after calling ``eval`` function.")
if self.operand.resolved_value == BooleanValues.TRUE:
self.resolved_value = BooleanValues.FALSE
return
if self.operand.resolved_value == BooleanValues.FALSE:
self.resolved_value = BooleanValues.TRUE
return
self.resolved_value = BooleanValues.UNKNOWN
def __iter__(self) -> Any:
"""Returns an iterator."""
yield from self.operand
yield self
class ModelFilter(JumpStartDataHolderType):
"""Data holder class to store model filters.
For a given filter string "task == ic", the key corresponds to
"task" and the value corresponds to "ic", with the operation being
"==".
"""
__slots__ = ["key", "value", "operator"]
def __init__(self, key: str, value: str, operator: str):
"""Instantiates ``ModelFilter`` object.
Args:
key (str): The key in metadata for the model filter.
value (str): The value of the metadata for the model filter.
operator (str): The operator used in the model filter.
"""
self.key = key
self.value = value
self.operator = operator
def parse_filter_string(filter_string: str) -> ModelFilter:
"""Parse filter string and return a serialized ``ModelFilter`` object.
Args:
filter_string (str): The filter string to be serialized to an object.
"""
for operator in ACCEPTABLE_OPERATORS_IN_PARSE_ORDER:
split_filter_string = filter_string.split(operator)
if len(split_filter_string) == 2:
return ModelFilter(
key=split_filter_string[0].strip(),
value=split_filter_string[1].strip(),
operator=operator.strip(),
)
raise ValueError(f"Cannot parse filter string: {filter_string}")
def evaluate_filter_expression( # pylint: disable=too-many-return-statements
model_filter: ModelFilter,
cached_model_value: Union[str, bool, int, float, Dict[str, Any], List[Any]],
) -> BooleanValues:
"""Evaluates model filter with cached model spec value, returns boolean.
Args:
model_filter (ModelFilter): The model filter for evaluation.
cached_model_value (Any): The value in the model manifest/spec that should be used to
evaluate the filter.
"""
if model_filter.operator in FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.EQUALS]:
model_filter_value = model_filter.value
if isinstance(cached_model_value, bool):
cached_model_value = str(cached_model_value).lower()
model_filter_value = model_filter.value.lower()
if str(model_filter_value) == str(cached_model_value):
return BooleanValues.TRUE
return BooleanValues.FALSE
if model_filter.operator in FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.NOT_EQUALS]:
if isinstance(cached_model_value, bool):
cached_model_value = str(cached_model_value).lower()
model_filter.value = model_filter.value.lower()
if str(model_filter.value) == str(cached_model_value):
return BooleanValues.FALSE
return BooleanValues.TRUE
if model_filter.operator in FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.IN]:
py_obj = literal_eval(model_filter.value)
try:
iter(py_obj)
except TypeError:
return BooleanValues.FALSE
if cached_model_value in py_obj:
return BooleanValues.TRUE
return BooleanValues.FALSE
if model_filter.operator in FILTER_OPERATOR_STRING_MAPPINGS[FilterOperators.NOT_IN]:
py_obj = literal_eval(model_filter.value)
try:
iter(py_obj)
except TypeError:
return BooleanValues.TRUE
if cached_model_value in py_obj:
return BooleanValues.FALSE
return BooleanValues.TRUE
raise RuntimeError(f"Bad operator: {model_filter.operator}")