Skip to content

Commit 6b438db

Browse files
authored
Make print(config) friendlier for copy-paste (#210)
* Make print(config) friendlier for copy-paste * Update docs
1 parent b6aa830 commit 6b438db

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

.github/workflows/docs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ jobs:
1717
run: |
1818
cd docs
1919
make html
20+
- name: Run doctests
21+
run: |
22+
cd docs
23+
make doctest
2024
- name: Pull latest gh-pages
2125
if: (contains(github.ref, 'master')) && github.event_name == 'push'
2226
run: |

ConfigSpace/configuration_space.pyx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,22 +1573,18 @@ class Configuration(collections.abc.Mapping):
15731573
self._populate_values()
15741574

15751575
representation = io.StringIO()
1576-
representation.write("Configuration:\n")
1576+
representation.write("Configuration(values={\n")
15771577

15781578
hyperparameters = self.configuration_space.get_hyperparameters()
15791579
hyperparameters.sort(key=lambda t: t.name)
15801580
for hyperparameter in hyperparameters:
15811581
hp_name = hyperparameter.name
15821582
if hp_name in self._values and self._values[hp_name] is not None:
15831583
representation.write(" ")
1584-
15851584
value = repr(self._values[hp_name])
1586-
if isinstance(hyperparameter, Constant):
1587-
representation.write("%s, Constant: %s" % (hp_name, value))
1588-
else:
1589-
representation.write("%s, Value: %s" % (hp_name, value))
1590-
representation.write("\n")
1585+
representation.write("'%s': %s,\n" % (hp_name, value))
15911586

1587+
representation.write("})\n")
15921588
return representation.getvalue()
15931589

15941590
def __iter__(self) -> Iterable:

docs/source/User-Guide.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ For demonstration purpose, we sample a configuration from it.
4949
>>> cs.add_hyperparameters([c, max_iter])
5050
[C, Type: UniformFloat, Range: [-1.0, 1.0], Default: 0.0, max_iter, Type: ...]
5151
>>> cs.sample_configuration()
52-
Configuration:
53-
C, Value: -0.6169610992422154
54-
max_iter, Value: 66
52+
Configuration(values={
53+
'C': -0.6169610992422154,
54+
'max_iter': 66,
55+
})
5556
<BLANKLINE>
5657

5758

docs/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
'sphinx.ext.viewcode',
5252
'sphinx.ext.autosummary',
5353
'sphinx.ext.napoleon',
54-
'sphinx.ext.githubpages'
54+
'sphinx.ext.githubpages',
55+
'sphinx.ext.doctest',
5556
]
5657

5758
# Add any paths that contain templates here, relative to this directory.

docs/source/index.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ Basic usage
4444
>>> cs.add_hyperparameters([a, b])
4545
[a, Type: UniformInteger, Range: [10, 100], Default: 55,...]
4646
>>> cs.sample_configuration()
47-
Configuration:
48-
a, Value: 27
49-
b, Value: 'blue'
47+
Configuration(values={
48+
'a': 27,
49+
'b': 'blue',
50+
})
5051
<BLANKLINE>
5152

5253
Installation

docs/source/quickstart.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ For demonstration purpose, we sample a configuration from the :class:`~ConfigSpa
5757
.. doctest::
5858

5959
>>> cs.sample_configuration()
60-
Configuration:
61-
alpha, Value: 0.1915194503788923
60+
Configuration(values={
61+
'alpha': 0.1915194503788923,
62+
})
6263
<BLANKLINE>
6364

6465
And that's it.

test/test_configuration_space.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,18 @@ def test_meta_field(self):
10441044
self.assertEqual(parent.get_hyperparameter("sub:chp").meta, dict(chp=True))
10451045
self.assertEqual(parent.get_hyperparameter("sub:ohp").meta, dict(ohp=True))
10461046
self.assertEqual(parent.get_hyperparameter("sub:const").meta, dict(const=True))
1047+
1048+
def test_repr_roundtrip(self):
1049+
cs = ConfigurationSpace()
1050+
cs.add_hyperparameter(UniformIntegerHyperparameter("uihp", lower=1, upper=10))
1051+
cs.add_hyperparameter(NormalIntegerHyperparameter("nihp", mu=0, sigma=1))
1052+
cs.add_hyperparameter(UniformFloatHyperparameter("ufhp", lower=1, upper=10))
1053+
cs.add_hyperparameter(NormalFloatHyperparameter("nfhp", mu=0, sigma=1))
1054+
cs.add_hyperparameter(CategoricalHyperparameter("chp", choices=['1', '2', '3']))
1055+
cs.add_hyperparameter(OrdinalHyperparameter("ohp", sequence=['1', '2', '3']))
1056+
cs.add_hyperparameter(Constant("const", value=1))
1057+
default = cs.get_default_configuration()
1058+
repr = default.__repr__()
1059+
repr = repr.replace('})', '}, configuration_space=cs)')
1060+
config = eval(repr)
1061+
self.assertEqual(default, config)

0 commit comments

Comments
 (0)