Skip to content

Commit

Permalink
Make print(config) friendlier for copy-paste (automl#210)
Browse files Browse the repository at this point in the history
* Make print(config) friendlier for copy-paste

* Update docs
  • Loading branch information
mfeurer authored Dec 15, 2021
1 parent b6aa830 commit 6b438db
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
run: |
cd docs
make html
- name: Run doctests
run: |
cd docs
make doctest
- name: Pull latest gh-pages
if: (contains(github.ref, 'master')) && github.event_name == 'push'
run: |
Expand Down
10 changes: 3 additions & 7 deletions ConfigSpace/configuration_space.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1573,22 +1573,18 @@ class Configuration(collections.abc.Mapping):
self._populate_values()

representation = io.StringIO()
representation.write("Configuration:\n")
representation.write("Configuration(values={\n")

hyperparameters = self.configuration_space.get_hyperparameters()
hyperparameters.sort(key=lambda t: t.name)
for hyperparameter in hyperparameters:
hp_name = hyperparameter.name
if hp_name in self._values and self._values[hp_name] is not None:
representation.write(" ")

value = repr(self._values[hp_name])
if isinstance(hyperparameter, Constant):
representation.write("%s, Constant: %s" % (hp_name, value))
else:
representation.write("%s, Value: %s" % (hp_name, value))
representation.write("\n")
representation.write("'%s': %s,\n" % (hp_name, value))

representation.write("})\n")
return representation.getvalue()

def __iter__(self) -> Iterable:
Expand Down
7 changes: 4 additions & 3 deletions docs/source/User-Guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ For demonstration purpose, we sample a configuration from it.
>>> cs.add_hyperparameters([c, max_iter])
[C, Type: UniformFloat, Range: [-1.0, 1.0], Default: 0.0, max_iter, Type: ...]
>>> cs.sample_configuration()
Configuration:
C, Value: -0.6169610992422154
max_iter, Value: 66
Configuration(values={
'C': -0.6169610992422154,
'max_iter': 66,
})
<BLANKLINE>


Expand Down
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
'sphinx.ext.viewcode',
'sphinx.ext.autosummary',
'sphinx.ext.napoleon',
'sphinx.ext.githubpages'
'sphinx.ext.githubpages',
'sphinx.ext.doctest',
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
7 changes: 4 additions & 3 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ Basic usage
>>> cs.add_hyperparameters([a, b])
[a, Type: UniformInteger, Range: [10, 100], Default: 55,...]
>>> cs.sample_configuration()
Configuration:
a, Value: 27
b, Value: 'blue'
Configuration(values={
'a': 27,
'b': 'blue',
})
<BLANKLINE>

Installation
Expand Down
5 changes: 3 additions & 2 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ For demonstration purpose, we sample a configuration from the :class:`~ConfigSpa
.. doctest::

>>> cs.sample_configuration()
Configuration:
alpha, Value: 0.1915194503788923
Configuration(values={
'alpha': 0.1915194503788923,
})
<BLANKLINE>

And that's it.
Expand Down
15 changes: 15 additions & 0 deletions test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,18 @@ def test_meta_field(self):
self.assertEqual(parent.get_hyperparameter("sub:chp").meta, dict(chp=True))
self.assertEqual(parent.get_hyperparameter("sub:ohp").meta, dict(ohp=True))
self.assertEqual(parent.get_hyperparameter("sub:const").meta, dict(const=True))

def test_repr_roundtrip(self):
cs = ConfigurationSpace()
cs.add_hyperparameter(UniformIntegerHyperparameter("uihp", lower=1, upper=10))
cs.add_hyperparameter(NormalIntegerHyperparameter("nihp", mu=0, sigma=1))
cs.add_hyperparameter(UniformFloatHyperparameter("ufhp", lower=1, upper=10))
cs.add_hyperparameter(NormalFloatHyperparameter("nfhp", mu=0, sigma=1))
cs.add_hyperparameter(CategoricalHyperparameter("chp", choices=['1', '2', '3']))
cs.add_hyperparameter(OrdinalHyperparameter("ohp", sequence=['1', '2', '3']))
cs.add_hyperparameter(Constant("const", value=1))
default = cs.get_default_configuration()
repr = default.__repr__()
repr = repr.replace('})', '}, configuration_space=cs)')
config = eval(repr)
self.assertEqual(default, config)

0 comments on commit 6b438db

Please sign in to comment.