Skip to content

Commit 390263e

Browse files
[FIX] Cleanup ConfigSpace warnings (#183)
* [FIX] Cleanup ConfigSpace warnings * Incorporate PR comments * Pyparsing enhancement * Remove not needed casting
1 parent b2fe51e commit 390263e

10 files changed

Lines changed: 47 additions & 36 deletions

File tree

ConfigSpace/c_util.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cimport numpy as np
1818
# We now need to fix a datatype for our arrays. I've used the variable
1919
# DTYPE for this, which is assigned to the usual NumPy runtime
2020
# type info object.
21-
DTYPE = np.float
21+
DTYPE = float
2222
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
2323
# every type in the numpy module there's a corresponding compile-time
2424
# type with a _t-suffix.

ConfigSpace/conditions.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cimport numpy as np
66
# We now need to fix a datatype for our arrays. I've used the variable
77
# DTYPE for this, which is assigned to the usual NumPy runtime
88
# type info object.
9-
DTYPE = np.float
9+
DTYPE = float
1010
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
1111
# every type in the numpy module there's a corresponding compile-time
1212
# type with a _t-suffix.

ConfigSpace/conditions.pyx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ import numpy as np
4343
from ConfigSpace.hyperparameters import NumericalHyperparameter, OrdinalHyperparameter
4444
from ConfigSpace.hyperparameters cimport Hyperparameter
4545

46-
# We now need to fix a datatype for our arrays. I've used the variable
47-
# DTYPE for this, which is assigned to the usual NumPy runtime
48-
# type info object.
49-
# DTYPE = np.float
50-
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
51-
# every type in the numpy module there's a corresponding compile-time
52-
# type with a _t-suffix.
53-
# ctypedef np.float_t DTYPE_t
5446
cimport numpy as np
5547

5648

ConfigSpace/configuration_space.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ class Configuration(object):
14161416

14171417
self._query_values = True
14181418
self._vector = np.ndarray((self._num_hyperparameters,),
1419-
dtype=np.float)
1419+
dtype=float)
14201420

14211421
# Populate the vector
14221422
# TODO very unintuitive calls...

ConfigSpace/forbidden.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cimport numpy as np
66
# We now need to fix a datatype for our arrays. I've used the variable
77
# DTYPE for this, which is assigned to the usual NumPy runtime
88
# type info object.
9-
DTYPE = np.float
9+
DTYPE = float
1010
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
1111
# every type in the numpy module there's a corresponding compile-time
1212
# type with a _t-suffix.

ConfigSpace/forbidden.pyx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ from typing import List, Dict, Any, Union
3737

3838
from ConfigSpace.forbidden cimport AbstractForbiddenComponent
3939

40-
# We now need to fix a datatype for our arrays. I've used the variable
41-
# DTYPE for this, which is assigned to the usual NumPy runtime
42-
# type info object.
43-
# DTYPE = np.float
44-
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
45-
# every type in the numpy module there's a corresponding compile-time
46-
# type with a _t-suffix.
47-
# ctypedef np.float_t DTYPE_t
4840
from libc.stdlib cimport malloc, free
4941
cimport numpy as np
5042

ConfigSpace/hyperparameters.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cimport numpy as np
66
# We now need to fix a datatype for our arrays. I've used the variable
77
# DTYPE for this, which is assigned to the usual NumPy runtime
88
# type info object.
9-
DTYPE = np.float
9+
DTYPE = float
1010
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
1111
# every type in the numpy module there's a corresponding compile-time
1212
# type with a _t-suffix.

ConfigSpace/hyperparameters.pyx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ from typing import List, Any, Dict, Union, Set, Tuple, Optional
3737
import numpy as np
3838
cimport numpy as np
3939

40-
# We now need to fix a datatype for our arrays. I've used the variable
41-
# DTYPE for this, which is assigned to the usual NumPy runtime
42-
# type info object.
43-
# DTYPE = np.float
44-
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
45-
# every type in the numpy module there's a corresponding compile-time
46-
# type with a _t-suffix.
47-
# ctypedef np.float_t DTYPE_t
48-
4940

5041
cdef class Hyperparameter(object):
5142

@@ -877,7 +868,7 @@ cdef class UniformIntegerHyperparameter(IntegerHyperparameter):
877868
if self.log:
878869
repr_str.write(", on log-scale")
879870
if self.q is not None:
880-
repr_str.write(", Q: %s" % repr(np.int(self.q)))
871+
repr_str.write(", Q: %s" % repr(self.q))
881872
repr_str.seek(0)
882873
return repr_str.getvalue()
883874

@@ -913,7 +904,7 @@ cdef class UniformIntegerHyperparameter(IntegerHyperparameter):
913904
return self.ufhp._inverse_transform(vector)
914905

915906
def is_legal(self, value: int) -> bool:
916-
if not (isinstance(value, (int, np.int, np.int32, np.int64))):
907+
if not (isinstance(value, (int, np.int32, np.int64))):
917908
return False
918909
elif self.upper >= value >= self.lower:
919910
return True
@@ -1174,7 +1165,7 @@ cdef class NormalIntegerHyperparameter(IntegerHyperparameter):
11741165
q=self.q, log=self.log)
11751166

11761167
def is_legal(self, value: int) -> bool:
1177-
return isinstance(value, (int, np.int, np.int32, np.int64))
1168+
return isinstance(value, (int, np.int32, np.int64))
11781169

11791170
cpdef bint is_legal_vector(self, DTYPE_t value):
11801171
return isinstance(value, float) or isinstance(value, int)

ConfigSpace/read_and_write/pcs_new.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@
6363
pp_e_notation = pyparsing.Combine(pp_floatorint + pp_eorE + pp_int)
6464
pp_number = pp_e_notation | pp_float | pp_int
6565
pp_numberorname = pp_number | pp_param_name
66-
pp_log = pyparsing.Word("log")
67-
pp_connective = pyparsing.Word("||" + "&&")
66+
pp_log = pyparsing.Literal("log")
67+
# A word matches each character as a set. So && is processed as &
68+
# https://pythonhosted.org/pyparsing/pyparsing.Word-class.html
69+
pp_connectiveOR = pyparsing.Literal("||")
70+
pp_connectiveAND = pyparsing.Literal("&&")
6871
pp_choices = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name))
6972
pp_sequence = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name))
7073
pp_ord_param = pp_param_name + pp_param_type + "{" + pp_sequence + "}" + "[" + pp_param_name + "]"
@@ -77,7 +80,7 @@
7780
pyparsing.Optional('{') + pp_param_val + pyparsing.Optional('}') + \
7881
pyparsing.Optional(
7982
pyparsing.OneOrMore(
80-
pp_connective + pp_param_name + pp_param_operation
83+
(pp_connectiveAND | pp_connectiveOR) + pp_param_name + pp_param_operation
8184
+ pyparsing.Optional('{') + pp_param_val + pyparsing.Optional('}')
8285
)
8386
)

test/test_hyperparameters.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,36 @@ def test_rvs(self):
11051105
f1.rvs(random_state=np.random)
11061106
f1.rvs(random_state=np.random.default_rng(1))
11071107
self.assertRaises(ValueError, f1.rvs, 1, "a")
1108+
1109+
def test_hyperparam_representation(self):
1110+
# Float
1111+
f1 = UniformFloatHyperparameter("param", 1, 100, log=True)
1112+
self.assertEqual(
1113+
"param, Type: UniformFloat, Range: [1.0, 100.0], Default: 10.0, on log-scale",
1114+
repr(f1)
1115+
)
1116+
f2 = NormalFloatHyperparameter("param", 8, 99.1, log=False)
1117+
self.assertEqual(
1118+
"param, Type: NormalFloat, Mu: 8.0 Sigma: 99.1, Default: 8.0",
1119+
repr(f2)
1120+
)
1121+
i1 = UniformIntegerHyperparameter("param", 0, 100)
1122+
self.assertEqual(
1123+
"param, Type: UniformInteger, Range: [0, 100], Default: 50",
1124+
repr(i1)
1125+
)
1126+
i2 = NormalIntegerHyperparameter("param", 5, 8)
1127+
self.assertEqual(
1128+
"param, Type: NormalInteger, Mu: 5 Sigma: 8, Default: 5",
1129+
repr(i2)
1130+
)
1131+
o1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"])
1132+
self.assertEqual(
1133+
"temp, Type: Ordinal, Sequence: {freezing, cold, warm, hot}, Default: freezing",
1134+
repr(o1)
1135+
)
1136+
c1 = CategoricalHyperparameter("param", [True, False])
1137+
self.assertEqual(
1138+
"param, Type: Categorical, Choices: {True, False}, Default: True",
1139+
repr(c1)
1140+
)

0 commit comments

Comments
 (0)