diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index f7a0c4fc..ee615f67 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -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: | diff --git a/ConfigSpace/configuration_space.pyx b/ConfigSpace/configuration_space.pyx index 48eb5a38..77d2b856 100644 --- a/ConfigSpace/configuration_space.pyx +++ b/ConfigSpace/configuration_space.pyx @@ -1573,7 +1573,7 @@ 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) @@ -1581,14 +1581,10 @@ class Configuration(collections.abc.Mapping): 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: diff --git a/docs/source/User-Guide.rst b/docs/source/User-Guide.rst index 19e3f209..22b7f733 100644 --- a/docs/source/User-Guide.rst +++ b/docs/source/User-Guide.rst @@ -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, + }) diff --git a/docs/source/conf.py b/docs/source/conf.py index 64895d97..53e47365 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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. diff --git a/docs/source/index.rst b/docs/source/index.rst index 8aafab4b..065f13c5 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -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', + }) Installation diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index ad45b221..b704e93d 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -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, + }) And that's it. diff --git a/test/test_configuration_space.py b/test/test_configuration_space.py index 0e4ca908..149fa6e3 100644 --- a/test/test_configuration_space.py +++ b/test/test_configuration_space.py @@ -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)