Skip to content

Commit df0737a

Browse files
authored
Extend configuration space to act as mapping (#191)
* Added Mapping functionality to Configurations * Removed old flake8 plugin * Skipping old tests that existed * flake'8d * Went with code review changes * Deleted old test file
1 parent 1a23bd5 commit df0737a

5 files changed

Lines changed: 92 additions & 276 deletions

File tree

.flake8

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ exclude =
88
build
99
.eggs
1010
*.egg
11-
per-file-ignores =
12-
*/*.pyx: E225, E226, E251, E999
13-
*/*.pxd: E225, E226, E251, E999
11+

ConfigSpace/configuration_space.pyx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
# cython: language_level=3
3030

31+
import collections.abc
3132
from collections import defaultdict, deque, OrderedDict
3233
import copy
3334
from itertools import chain
@@ -52,12 +53,14 @@ from ConfigSpace.forbidden import (
5253
AbstractForbiddenClause,
5354
AbstractForbiddenConjunction,
5455
)
55-
from typing import Union, List, Any, Dict, Iterable, Set, Tuple, Optional
56+
from typing import (
57+
Union, List, Any, Dict, Iterable, Set, Tuple, Optional, KeysView
58+
)
5659
from ConfigSpace.exceptions import ForbiddenValueError
5760
import ConfigSpace.c_util
5861

5962

60-
class ConfigurationSpace(object):
63+
class ConfigurationSpace(collections.abc.Mapping):
6164

6265
# TODO add a method to add whole configuration spaces as a child "tree"
6366

@@ -744,6 +747,9 @@ class ConfigurationSpace(object):
744747
"""
745748
return list(self._hyperparameters.keys())
746749

750+
def __getitem__(self, key: str) -> Hyperparameter:
751+
return self.get_hyperparameter(key)
752+
747753
def get_hyperparameter(self, name: str) -> Hyperparameter:
748754
"""
749755
Gives the hyperparameter from the configuration space given its name.
@@ -1229,6 +1235,12 @@ class ConfigurationSpace(object):
12291235
""" Allows to iterate over the hyperparameter names in (hopefully?) the right order."""
12301236
return iter(self._hyperparameters.keys())
12311237

1238+
def keys(self) -> KeysView[str]:
1239+
return self._hyperparameters.keys()
1240+
1241+
def __len__(self) -> int:
1242+
return len(self._hyperparameters)
1243+
12321244
def sample_configuration(self, size: int = 1) -> Union['Configuration', List['Configuration']]:
12331245
"""
12341246
Sample ``size`` configurations from the configuration space object.
@@ -1330,7 +1342,7 @@ class ConfigurationSpace(object):
13301342
self.random = np.random.RandomState(seed)
13311343

13321344

1333-
class Configuration(object):
1345+
class Configuration(collections.abc.Mapping):
13341346
def __init__(self, configuration_space: ConfigurationSpace,
13351347
values: Union[None, Dict[str, Union[str, float, int]]] = None,
13361348
vector: Union[None, np.ndarray] = None,
@@ -1582,6 +1594,9 @@ class Configuration(object):
15821594
def __iter__(self) -> Iterable:
15831595
return iter(self.keys())
15841596

1597+
def __len__(self) -> int:
1598+
return len(self.configuration_space)
1599+
15851600
def keys(self) -> List[str]:
15861601
"""
15871602
Cache the keys to speed up the process of retrieving the keys.

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def finalize_options(self):
9494
"mypy",
9595
"pre-commit",
9696
"pytest-cov",
97-
'flake8-per-file-ignores',
9897
],
9998
"docs": ["sphinx", "sphinx-gallery", "sphinx_bootstrap_theme", "numpydoc"],
10099
}

test/read_and_write/test_irace_writer.py

Lines changed: 0 additions & 269 deletions
This file was deleted.

0 commit comments

Comments
 (0)