Skip to content

Commit

Permalink
Fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiebergman committed Dec 19, 2023
1 parent e915964 commit 2a29120
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 28 deletions.
4 changes: 3 additions & 1 deletion ConfigSpace/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from ConfigSpace import c_util
from ConfigSpace.exceptions import HyperparameterNotFoundError, IllegalValueError
from ConfigSpace.hyperparameters import FloatHyperparameter
from ConfigSpace.hyperparameters.hyperparameter import NotSet

if TYPE_CHECKING:
from ConfigSpace.configuration_space import ConfigurationSpace

NotSet = object() # Sentinal value for unset values


class Configuration(Mapping[str, Any]):
def __init__(
self,
Expand Down
3 changes: 1 addition & 2 deletions ConfigSpace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
ConditionComponent,
EqualsCondition,
)
from ConfigSpace.configuration import Configuration
from ConfigSpace.configuration import Configuration, NotSet
from ConfigSpace.exceptions import (
ActiveHyperparameterNotSetError,
AmbiguousConditionError,
Expand All @@ -72,7 +72,6 @@
UniformFloatHyperparameter,
UniformIntegerHyperparameter,
)
from ConfigSpace.hyperparameters.hyperparameter import NotSet

_ROOT: Final = "__HPOlib_configuration_space_root__"

Expand Down
3 changes: 1 addition & 2 deletions ConfigSpace/hyperparameters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ConfigSpace.hyperparameters.categorical import CategoricalHyperparameter
from ConfigSpace.hyperparameters.constant import Constant, UnParametrizedHyperparameter
from ConfigSpace.hyperparameters.float_hyperparameter import FloatHyperparameter
from ConfigSpace.hyperparameters.hyperparameter import Hyperparameter, NotSet
from ConfigSpace.hyperparameters.hyperparameter import Hyperparameter
from ConfigSpace.hyperparameters.integer_hyperparameter import IntegerHyperparameter
from ConfigSpace.hyperparameters.normal_float import NormalFloatHyperparameter
from ConfigSpace.hyperparameters.normal_integer import NormalIntegerHyperparameter
Expand All @@ -27,5 +27,4 @@
"NormalIntegerHyperparameter",
"BetaFloatHyperparameter",
"BetaIntegerHyperparameter",
"NotSet",
]
1 change: 0 additions & 1 deletion ConfigSpace/hyperparameters/hyperparameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import numpy as np

NotSet = object()

class Comparison(Enum):
"""Enumeration of possible comparison results."""
Expand Down
5 changes: 4 additions & 1 deletion ConfigSpace/hyperparameters/normal_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def __repr__(self) -> str:

if self.lower is None or self.upper is None:
repr_str.write(
f"{self.name}, Type: NormalFloat, Mu: {self.mu!r} Sigma: {self.sigma!r}, Default: {self.default_value!r}",
f"{self.name}, Type: NormalFloat,"
f" Mu: {self.mu!r}"
f" Sigma: {self.sigma!r},"
f" Default: {self.default_value!r}",
)
else:
repr_str.write(
Expand Down
1 change: 0 additions & 1 deletion ConfigSpace/hyperparameters/ordinal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import copy
import io
from collections import OrderedDict
from typing import Any

import numpy as np
Expand Down
26 changes: 14 additions & 12 deletions ConfigSpace/hyperparameters/uniform_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
if q < 1:
raise ValueError(
"Setting quantization < 1 for Integer "
f"Hyperparameter {name} has no effect."
f"Hyperparameter {name} has no effect.",
)

self.q = self.check_int(q, "q")
Expand Down Expand Up @@ -102,12 +102,14 @@ def __init__(
def __repr__(self) -> str:
repr_str = io.StringIO()
repr_str.write(
f"{self.name}, Type: UniformInteger, Range: [{self.lower!r}, {self.upper!r}], Default: {self.default_value!r}",
f"{self.name}, Type: UniformInteger,"
f" Range: [{self.lower!r}, {self.upper!r}],"
f" Default: {self.default_value!r}",
)
if self.log:
repr_str.write(", on log-scale")
if self.q is not None:
repr_str.write(", Q: %s" % repr(self.q))
repr_str.write(f", Q: {self.q}")
repr_str.seek(0)
return repr_str.getvalue()

Expand Down Expand Up @@ -165,8 +167,8 @@ def check_default(self, default_value: int | float | None) -> int:

if self.is_legal(default_value):
return default_value
else:
raise ValueError("Illegal default value %s" % str(default_value))

raise ValueError("Illegal default value %s" % str(default_value))

Check warning on line 171 in ConfigSpace/hyperparameters/uniform_integer.py

View check run for this annotation

Codecov / codecov/patch

ConfigSpace/hyperparameters/uniform_integer.py#L171

Added line #L171 was not covered by tests

def has_neighbors(self) -> bool:
if self.log:
Expand All @@ -179,13 +181,13 @@ def has_neighbors(self) -> bool:
# If there is only one active value, this is not enough
return upper - lower >= 1

def get_num_neighbors(self, value=None) -> int:
def get_num_neighbors(self, value: int | None = None) -> int:
# If there is a value in the range, then that value is not a neighbor of itself
# so we need to remove one
if value is not None and self.lower <= value <= self.upper:
return self.upper - self.lower - 1
else:
return self.upper - self.lower

return self.upper - self.lower

def get_neighbors(
self,
Expand Down Expand Up @@ -252,8 +254,8 @@ def get_neighbors(

if transform:
return neighbors
else:
return self._inverse_transform(np.asarray(neighbors)).tolist()

return self._inverse_transform(np.asarray(neighbors)).tolist()

Check warning on line 258 in ConfigSpace/hyperparameters/uniform_integer.py

View check run for this annotation

Codecov / codecov/patch

ConfigSpace/hyperparameters/uniform_integer.py#L258

Added line #L258 was not covered by tests

# A truncated normal between 0 and 1, centered on the value with a scale of std.
# This will be sampled from and converted to the corresponding int value
Expand Down Expand Up @@ -297,8 +299,8 @@ def get_neighbors(
neighbors = list(seen)
if transform:
return neighbors
else:
return self._inverse_transform(np.array(neighbors)).tolist()

return self._inverse_transform(np.array(neighbors)).tolist()

def _pdf(self, vector: np.ndarray) -> np.ndarray:
"""
Expand Down
4 changes: 2 additions & 2 deletions ConfigSpace/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def get_one_exchange_neighbourhood(
array = configuration.get_array() # type: np.ndarray
value = array[index] # type: float

# Check for NaNs (inactive value)
if value != value:
# Inactive value
if isinstance(value, float) and np.isnan(value):
continue

iteration = 0
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ select = [
ignore = [
"D100",
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method (Should be active but it's too much for right now)
"D104", # Missing docstring in public package
"D105", # Missing docstring in magic mthod
"D203", # 1 blank line required before class docstring
Expand Down
6 changes: 0 additions & 6 deletions test/test_configspace_from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@
],
)
def test_individual_hyperparameters(value: Any, expected: Hyperparameter) -> None:
"""
Expects
-------
* Creating a constant with the dictionary easy api will insert a Constant
into it's hyperparameters.
"""
cs = ConfigurationSpace({"hp": value})
assert cs["hp"] == expected

Expand Down

0 comments on commit 2a29120

Please sign in to comment.