From 89e65cc4fa5d6203ee649f55f18fc5e40b50e12f Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 20 May 2021 12:47:44 +0200 Subject: [PATCH 1/4] [FIX] Cleanup ConfigSpace warnings --- ConfigSpace/c_util.pyx | 2 +- ConfigSpace/conditions.pxd | 2 +- ConfigSpace/conditions.pyx | 2 +- ConfigSpace/configuration_space.pyx | 2 +- ConfigSpace/forbidden.pxd | 2 +- ConfigSpace/forbidden.pyx | 4 ++-- ConfigSpace/hyperparameters.pxd | 2 +- ConfigSpace/hyperparameters.pyx | 8 ++++---- ConfigSpace/read_and_write/pcs_new.py | 4 +++- 9 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ConfigSpace/c_util.pyx b/ConfigSpace/c_util.pyx index 0415577b..f8b43cfa 100644 --- a/ConfigSpace/c_util.pyx +++ b/ConfigSpace/c_util.pyx @@ -18,7 +18,7 @@ cimport numpy as np # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -DTYPE = np.float +DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. diff --git a/ConfigSpace/conditions.pxd b/ConfigSpace/conditions.pxd index 8faae2f0..50f79c6d 100644 --- a/ConfigSpace/conditions.pxd +++ b/ConfigSpace/conditions.pxd @@ -6,7 +6,7 @@ cimport numpy as np # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -DTYPE = np.float +DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. diff --git a/ConfigSpace/conditions.pyx b/ConfigSpace/conditions.pyx index 746508f1..0ce8a9f5 100644 --- a/ConfigSpace/conditions.pyx +++ b/ConfigSpace/conditions.pyx @@ -46,7 +46,7 @@ from ConfigSpace.hyperparameters cimport Hyperparameter # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -# DTYPE = np.float +# DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. diff --git a/ConfigSpace/configuration_space.pyx b/ConfigSpace/configuration_space.pyx index 47b38ef7..e66c17c3 100644 --- a/ConfigSpace/configuration_space.pyx +++ b/ConfigSpace/configuration_space.pyx @@ -1416,7 +1416,7 @@ class Configuration(object): self._query_values = True self._vector = np.ndarray((self._num_hyperparameters,), - dtype=np.float) + dtype=float) # Populate the vector # TODO very unintuitive calls... diff --git a/ConfigSpace/forbidden.pxd b/ConfigSpace/forbidden.pxd index 3e391459..aa7abeb1 100644 --- a/ConfigSpace/forbidden.pxd +++ b/ConfigSpace/forbidden.pxd @@ -6,7 +6,7 @@ cimport numpy as np # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -DTYPE = np.float +DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. diff --git a/ConfigSpace/forbidden.pyx b/ConfigSpace/forbidden.pyx index cdea3ab5..06c5730a 100644 --- a/ConfigSpace/forbidden.pyx +++ b/ConfigSpace/forbidden.pyx @@ -40,11 +40,11 @@ from ConfigSpace.forbidden cimport AbstractForbiddenComponent # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -# DTYPE = np.float +# DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. -# ctypedef np.float_t DTYPE_t +# ctypedef float_t DTYPE_t from libc.stdlib cimport malloc, free cimport numpy as np diff --git a/ConfigSpace/hyperparameters.pxd b/ConfigSpace/hyperparameters.pxd index 81653125..fdb0ea8a 100644 --- a/ConfigSpace/hyperparameters.pxd +++ b/ConfigSpace/hyperparameters.pxd @@ -6,7 +6,7 @@ cimport numpy as np # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -DTYPE = np.float +DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. diff --git a/ConfigSpace/hyperparameters.pyx b/ConfigSpace/hyperparameters.pyx index 7c2b93b0..8ac1182c 100644 --- a/ConfigSpace/hyperparameters.pyx +++ b/ConfigSpace/hyperparameters.pyx @@ -40,7 +40,7 @@ cimport numpy as np # We now need to fix a datatype for our arrays. I've used the variable # DTYPE for this, which is assigned to the usual NumPy runtime # type info object. -# DTYPE = np.float +# DTYPE = float # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For # every type in the numpy module there's a corresponding compile-time # type with a _t-suffix. @@ -877,7 +877,7 @@ cdef class UniformIntegerHyperparameter(IntegerHyperparameter): if self.log: repr_str.write(", on log-scale") if self.q is not None: - repr_str.write(", Q: %s" % repr(np.int(self.q))) + repr_str.write(", Q: %s" % repr(np.int64(self.q))) repr_str.seek(0) return repr_str.getvalue() @@ -913,7 +913,7 @@ cdef class UniformIntegerHyperparameter(IntegerHyperparameter): return self.ufhp._inverse_transform(vector) def is_legal(self, value: int) -> bool: - if not (isinstance(value, (int, np.int, np.int32, np.int64))): + if not (isinstance(value, (int, np.int32, np.int64))): return False elif self.upper >= value >= self.lower: return True @@ -1174,7 +1174,7 @@ cdef class NormalIntegerHyperparameter(IntegerHyperparameter): q=self.q, log=self.log) def is_legal(self, value: int) -> bool: - return isinstance(value, (int, np.int, np.int32, np.int64)) + return isinstance(value, (int, np.int32, np.int64)) cpdef bint is_legal_vector(self, DTYPE_t value): return isinstance(value, float) or isinstance(value, int) diff --git a/ConfigSpace/read_and_write/pcs_new.py b/ConfigSpace/read_and_write/pcs_new.py index dae3d35e..aba50afc 100644 --- a/ConfigSpace/read_and_write/pcs_new.py +++ b/ConfigSpace/read_and_write/pcs_new.py @@ -64,7 +64,9 @@ pp_number = pp_e_notation | pp_float | pp_int pp_numberorname = pp_number | pp_param_name pp_log = pyparsing.Word("log") -pp_connective = pyparsing.Word("||" + "&&") +# A word matches each character as a set. So && is processed as & +# https://pythonhosted.org/pyparsing/pyparsing.Word-class.html +pp_connective = pyparsing.Word("|" + "&") pp_choices = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name)) pp_sequence = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name)) pp_ord_param = pp_param_name + pp_param_type + "{" + pp_sequence + "}" + "[" + pp_param_name + "]" From 63b781a031d6768566001932b0749f82df6caf6d Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 20 May 2021 14:17:34 +0200 Subject: [PATCH 2/4] Incorporate PR comments --- ConfigSpace/conditions.pyx | 8 ------- ConfigSpace/forbidden.pyx | 8 ------- ConfigSpace/hyperparameters.pyx | 9 -------- ConfigSpace/read_and_write/pcs_new.py | 2 +- test/test_hyperparameters.py | 33 +++++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/ConfigSpace/conditions.pyx b/ConfigSpace/conditions.pyx index 0ce8a9f5..24e266fb 100644 --- a/ConfigSpace/conditions.pyx +++ b/ConfigSpace/conditions.pyx @@ -43,14 +43,6 @@ import numpy as np from ConfigSpace.hyperparameters import NumericalHyperparameter, OrdinalHyperparameter from ConfigSpace.hyperparameters cimport Hyperparameter -# We now need to fix a datatype for our arrays. I've used the variable -# DTYPE for this, which is assigned to the usual NumPy runtime -# type info object. -# DTYPE = float -# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For -# every type in the numpy module there's a corresponding compile-time -# type with a _t-suffix. -# ctypedef np.float_t DTYPE_t cimport numpy as np diff --git a/ConfigSpace/forbidden.pyx b/ConfigSpace/forbidden.pyx index 06c5730a..000a32f0 100644 --- a/ConfigSpace/forbidden.pyx +++ b/ConfigSpace/forbidden.pyx @@ -37,14 +37,6 @@ from typing import List, Dict, Any, Union from ConfigSpace.forbidden cimport AbstractForbiddenComponent -# We now need to fix a datatype for our arrays. I've used the variable -# DTYPE for this, which is assigned to the usual NumPy runtime -# type info object. -# DTYPE = float -# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For -# every type in the numpy module there's a corresponding compile-time -# type with a _t-suffix. -# ctypedef float_t DTYPE_t from libc.stdlib cimport malloc, free cimport numpy as np diff --git a/ConfigSpace/hyperparameters.pyx b/ConfigSpace/hyperparameters.pyx index 8ac1182c..c111c359 100644 --- a/ConfigSpace/hyperparameters.pyx +++ b/ConfigSpace/hyperparameters.pyx @@ -37,15 +37,6 @@ from typing import List, Any, Dict, Union, Set, Tuple, Optional import numpy as np cimport numpy as np -# We now need to fix a datatype for our arrays. I've used the variable -# DTYPE for this, which is assigned to the usual NumPy runtime -# type info object. -# DTYPE = float -# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For -# every type in the numpy module there's a corresponding compile-time -# type with a _t-suffix. -# ctypedef np.float_t DTYPE_t - cdef class Hyperparameter(object): diff --git a/ConfigSpace/read_and_write/pcs_new.py b/ConfigSpace/read_and_write/pcs_new.py index aba50afc..e05f375f 100644 --- a/ConfigSpace/read_and_write/pcs_new.py +++ b/ConfigSpace/read_and_write/pcs_new.py @@ -63,7 +63,7 @@ pp_e_notation = pyparsing.Combine(pp_floatorint + pp_eorE + pp_int) pp_number = pp_e_notation | pp_float | pp_int pp_numberorname = pp_number | pp_param_name -pp_log = pyparsing.Word("log") +pp_log = pyparsing.Literal("log") # A word matches each character as a set. So && is processed as & # https://pythonhosted.org/pyparsing/pyparsing.Word-class.html pp_connective = pyparsing.Word("|" + "&") diff --git a/test/test_hyperparameters.py b/test/test_hyperparameters.py index ba9b6362..c453a096 100644 --- a/test/test_hyperparameters.py +++ b/test/test_hyperparameters.py @@ -1105,3 +1105,36 @@ def test_rvs(self): f1.rvs(random_state=np.random) f1.rvs(random_state=np.random.default_rng(1)) self.assertRaises(ValueError, f1.rvs, 1, "a") + + def test_hyperparam_representation(self): + # Float + f1 = UniformFloatHyperparameter("param", 1, 100, log=True) + self.assertEqual( + "param, Type: UniformFloat, Range: [1.0, 100.0], Default: 10.0, on log-scale", + repr(f1) + ) + f2 = NormalFloatHyperparameter("param", 8, 99.1, log=False) + self.assertEqual( + "param, Type: NormalFloat, Mu: 8.0 Sigma: 99.1, Default: 8.0", + repr(f2) + ) + i1 = UniformIntegerHyperparameter("param", 0, 100) + self.assertEqual( + "param, Type: UniformInteger, Range: [0, 100], Default: 50", + repr(i1) + ) + i2 = NormalIntegerHyperparameter("param", 5, 8) + self.assertEqual( + "param, Type: NormalInteger, Mu: 5 Sigma: 8, Default: 5", + repr(i2) + ) + o1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"]) + self.assertEqual( + "temp, Type: Ordinal, Sequence: {freezing, cold, warm, hot}, Default: freezing", + repr(o1) + ) + c1 = CategoricalHyperparameter("param", [True, False]) + self.assertEqual( + "param, Type: Categorical, Choices: {True, False}, Default: True", + repr(c1) + ) From b3418289e9a98e095094a85c9f8cab19efd2a3f9 Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 20 May 2021 15:59:45 +0200 Subject: [PATCH 3/4] Pyparsing enhancement --- ConfigSpace/read_and_write/pcs_new.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ConfigSpace/read_and_write/pcs_new.py b/ConfigSpace/read_and_write/pcs_new.py index e05f375f..e107651d 100644 --- a/ConfigSpace/read_and_write/pcs_new.py +++ b/ConfigSpace/read_and_write/pcs_new.py @@ -66,7 +66,8 @@ pp_log = pyparsing.Literal("log") # A word matches each character as a set. So && is processed as & # https://pythonhosted.org/pyparsing/pyparsing.Word-class.html -pp_connective = pyparsing.Word("|" + "&") +pp_connectiveOR = pyparsing.Literal("||") +pp_connectiveAND = pyparsing.Literal("&&") pp_choices = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name)) pp_sequence = pp_param_name + pyparsing.Optional(pyparsing.OneOrMore("," + pp_param_name)) pp_ord_param = pp_param_name + pp_param_type + "{" + pp_sequence + "}" + "[" + pp_param_name + "]" @@ -79,7 +80,7 @@ pyparsing.Optional('{') + pp_param_val + pyparsing.Optional('}') + \ pyparsing.Optional( pyparsing.OneOrMore( - pp_connective + pp_param_name + pp_param_operation + (pp_connectiveAND | pp_connectiveOR) + pp_param_name + pp_param_operation + pyparsing.Optional('{') + pp_param_val + pyparsing.Optional('}') ) ) From 37d2fdba8571a2a03abb9ea04dc6c54dc8c12f83 Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 20 May 2021 17:04:53 +0200 Subject: [PATCH 4/4] Remove not needed casting --- ConfigSpace/hyperparameters.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfigSpace/hyperparameters.pyx b/ConfigSpace/hyperparameters.pyx index c111c359..20f334ec 100644 --- a/ConfigSpace/hyperparameters.pyx +++ b/ConfigSpace/hyperparameters.pyx @@ -868,7 +868,7 @@ cdef class UniformIntegerHyperparameter(IntegerHyperparameter): if self.log: repr_str.write(", on log-scale") if self.q is not None: - repr_str.write(", Q: %s" % repr(np.int64(self.q))) + repr_str.write(", Q: %s" % repr(self.q)) repr_str.seek(0) return repr_str.getvalue()