Skip to content

Commit f7c2f45

Browse files
authored
Move utils HPO uses into HPO directory (#1912)
move utils into hpo dir
1 parent f08e9cb commit f7c2f45

File tree

6 files changed

+97
-80
lines changed

6 files changed

+97
-80
lines changed

otx/algorithms/common/utils/utils.py

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import importlib
1818
import inspect
1919
from collections import defaultdict
20-
from typing import Callable, Literal, Optional, Tuple
20+
from typing import Callable, Optional, Tuple
2121

2222
import yaml
2323

@@ -94,77 +94,3 @@ def get_arg_spec( # noqa: C901 # pylint: disable=too-many-branches
9494
if spec.varkw is None and spec.varargs is None:
9595
break
9696
return tuple(args)
97-
98-
99-
def left_vlaue_is_better(val1, val2, mode: Literal["max", "min"]) -> bool:
100-
"""Check left value is better than right value.
101-
102-
Whether check it's greather or lesser is changed depending on 'model'.
103-
104-
Args:
105-
val1 : value to check that it's bigger than other value.
106-
val2 : value to check that it's bigger than other value.
107-
mode (Literal['max', 'min']): value to decide whether better means greater or lesser.
108-
109-
Returns:
110-
bool: whether val1 is better than val2.
111-
"""
112-
check_mode_input(mode)
113-
if mode == "max":
114-
return val1 > val2
115-
return val1 < val2
116-
117-
118-
def check_positive(value, variable_name: Optional[str] = None, error_message: Optional[str] = None):
119-
"""Validate that value is positivle.
120-
121-
Args:
122-
value (Any): value to validate.
123-
variable_name (Optional[str], optional): name of value. It's used for error message. Defaults to None.
124-
error_message (Optional[str], optional): Error message to use when type is different. Defaults to None.
125-
126-
Raises:
127-
ValueError: If value isn't positive, the error is raised.
128-
"""
129-
if value <= 0:
130-
if error_message is not None:
131-
message = error_message
132-
elif variable_name:
133-
message = f"{variable_name} should be positive.\n" f"your value : {value}"
134-
else:
135-
raise ValueError
136-
raise ValueError(message)
137-
138-
139-
def check_not_negative(value, variable_name: Optional[str] = None, error_message: Optional[str] = None):
140-
"""Validate that value isn't negative.
141-
142-
Args:
143-
value (Any): value to validate.
144-
variable_name (Optional[str], optional): name of value. It's used for error message. Defaults to None.
145-
error_message (Optional[str], optional): Error message to use when type is different. Defaults to None.
146-
147-
Raises:
148-
ValueError: If value is negative, the error is raised.
149-
"""
150-
if value < 0:
151-
if error_message is not None:
152-
message = error_message
153-
elif variable_name:
154-
message = f"{variable_name} should be positive.\n" f"your value : {value}"
155-
else:
156-
raise ValueError
157-
raise ValueError(message)
158-
159-
160-
def check_mode_input(mode: str):
161-
"""Validate that mode is 'max' or 'min'.
162-
163-
Args:
164-
mode (str): string to validate.
165-
166-
Raises:
167-
ValueError: If 'mode' is not both 'max' and 'min', the error is raised.
168-
"""
169-
if mode not in ["max", "min"]:
170-
raise ValueError("mode should be max or min.\n" f"Your value : {mode}")

otx/hpo/hpo_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from enum import IntEnum
2222
from typing import Any, Dict, List, Optional, Union
2323

24-
from otx.algorithms.common.utils.utils import check_mode_input, check_positive
2524
from otx.hpo.search_space import SearchSpace
25+
from otx.hpo.utils import check_mode_input, check_positive
2626

2727
logger = logging.getLogger(__name__)
2828

otx/hpo/hyperband.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
from scipy.stats.qmc import LatinHypercube
2525

26-
from otx.algorithms.common.utils.utils import (
26+
from otx.hpo.hpo_base import HpoBase, Trial, TrialStatus
27+
from otx.hpo.utils import (
2728
check_mode_input,
2829
check_not_negative,
2930
check_positive,
3031
left_vlaue_is_better,
3132
)
32-
from otx.hpo.hpo_base import HpoBase, Trial, TrialStatus
3333

3434
logger = logging.getLogger(__name__)
3535

otx/hpo/resource_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import torch
2323

24-
from otx.algorithms.common.utils.utils import check_positive
24+
from otx.hpo.utils import check_positive
2525

2626
logger = logging.getLogger(__name__)
2727

otx/hpo/search_space.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import typing
2121
from typing import Any, Dict, List, Optional, Tuple, Union
2222

23-
from otx.algorithms.common.utils.utils import check_positive
23+
from otx.hpo.utils import check_positive
2424

2525
logger = logging.getLogger(__name__)
2626

otx/hpo/utils.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)