|
| 1 | +"""Collections of Utils for HPO.""" |
| 2 | + |
| 3 | +# Copyright (C) 2022 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions |
| 15 | +# and limitations under the License. |
| 16 | + |
| 17 | +from typing import Literal, Optional |
| 18 | + |
| 19 | + |
| 20 | +def left_vlaue_is_better(val1, val2, mode: Literal["max", "min"]) -> bool: |
| 21 | + """Check left value is better than right value. |
| 22 | +
|
| 23 | + Whether check it's greather or lesser is changed depending on 'model'. |
| 24 | +
|
| 25 | + Args: |
| 26 | + val1 : value to check that it's bigger than other value. |
| 27 | + val2 : value to check that it's bigger than other value. |
| 28 | + mode (Literal['max', 'min']): value to decide whether better means greater or lesser. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + bool: whether val1 is better than val2. |
| 32 | + """ |
| 33 | + check_mode_input(mode) |
| 34 | + if mode == "max": |
| 35 | + return val1 > val2 |
| 36 | + return val1 < val2 |
| 37 | + |
| 38 | + |
| 39 | +def check_positive(value, variable_name: Optional[str] = None, error_message: Optional[str] = None): |
| 40 | + """Validate that value is positivle. |
| 41 | +
|
| 42 | + Args: |
| 43 | + value (Any): value to validate. |
| 44 | + variable_name (Optional[str], optional): name of value. It's used for error message. Defaults to None. |
| 45 | + error_message (Optional[str], optional): Error message to use when type is different. Defaults to None. |
| 46 | +
|
| 47 | + Raises: |
| 48 | + ValueError: If value isn't positive, the error is raised. |
| 49 | + """ |
| 50 | + if value <= 0: |
| 51 | + if error_message is not None: |
| 52 | + message = error_message |
| 53 | + elif variable_name: |
| 54 | + message = f"{variable_name} should be positive.\n" f"your value : {value}" |
| 55 | + else: |
| 56 | + raise ValueError |
| 57 | + raise ValueError(message) |
| 58 | + |
| 59 | + |
| 60 | +def check_not_negative(value, variable_name: Optional[str] = None, error_message: Optional[str] = None): |
| 61 | + """Validate that value isn't negative. |
| 62 | +
|
| 63 | + Args: |
| 64 | + value (Any): value to validate. |
| 65 | + variable_name (Optional[str], optional): name of value. It's used for error message. Defaults to None. |
| 66 | + error_message (Optional[str], optional): Error message to use when type is different. Defaults to None. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + ValueError: If value is negative, the error is raised. |
| 70 | + """ |
| 71 | + if value < 0: |
| 72 | + if error_message is not None: |
| 73 | + message = error_message |
| 74 | + elif variable_name: |
| 75 | + message = f"{variable_name} should be positive.\n" f"your value : {value}" |
| 76 | + else: |
| 77 | + raise ValueError |
| 78 | + raise ValueError(message) |
| 79 | + |
| 80 | + |
| 81 | +def check_mode_input(mode: str): |
| 82 | + """Validate that mode is 'max' or 'min'. |
| 83 | +
|
| 84 | + Args: |
| 85 | + mode (str): string to validate. |
| 86 | +
|
| 87 | + Raises: |
| 88 | + ValueError: If 'mode' is not both 'max' and 'min', the error is raised. |
| 89 | + """ |
| 90 | + if mode not in ["max", "min"]: |
| 91 | + raise ValueError("mode should be max or min.\n" f"Your value : {mode}") |
0 commit comments