Serialization¶
ConfigSpace offers json, pcs and pcs_new writers/readers. These classes can serialize and deserialize configuration spaces. Serializing configuration spaces is useful to share configuration spaces across experiments, or use them in other tools, for example, to analyze hyperparameter importance with CAVE.
Serialization to JSON¶
- ConfigSpace.read_and_write.json.read(jason_string)[source]¶
Create a configuration space definition from a json string.
from ConfigSpace import ConfigurationSpace from ConfigSpace.read_and_write import json as cs_json cs = ConfigurationSpace({"a": [1, 2, 3]}) cs_string = cs_json.write(cs) with open('configspace.json', 'w') as f: f.write(cs_string) with open('configspace.json', 'r') as f: json_string = f.read() config = cs_json.read(json_string)
- Parameters:
jason_string (str) – A json string representing a configuration space definition
- Returns:
The deserialized ConfigurationSpace object
- Return type:
- ConfigSpace.read_and_write.json.write(configuration_space, indent=2)[source]¶
Create a string representation of a
ConfigurationSpace
in json format. This string can be written to file.from ConfigSpace import ConfigurationSpace from ConfigSpace.read_and_write import json as cs_json cs = ConfigurationSpace({"a": [1, 2, 3]}) with open('configspace.json', 'w') as f: f.write(cs_json.write(cs))
- Parameters:
configuration_space (
ConfigurationSpace
) – a configuration space, which should be written to file.indent (int) – number of whitespaces to use as indent
- Returns:
String representation of the configuration space, which will be written to file
- Return type:
str
Serialization with pcs-new (new format)¶
PCS (parameter configuration space) is a simple, human-readable file format for the description of an algorithm’s configurable parameters, their possible values, as well as any parameter dependencies. There exist an old and a new version.
The new PCS format is part of the Algorithm Configuration Library 2.0. A detailed description of the new format can be found in the ACLIB 2.0 docs, in the SMACv2 docs and further examples are provided in the pysmac docs
Note
The PCS format definition has changed in the year 2016 and is supported by
AClib 2.0, as well as SMAC (v2 and v3). To write or to read the old version of pcs,
please use the pcs
module.
- ConfigSpace.read_and_write.pcs_new.read(pcs_string)[source]¶
Read in a
ConfigurationSpace
definition from a pcs file.Example
>>> from ConfigSpace.read_and_write import pcs_new >>> with open('configspace.pcs_new', 'r') as fh: ... deserialized_conf = pcs_new.read(fh)
- Parameters:
pcs_string (Iterable[str]) – ConfigSpace definition in pcs format
- Returns:
The deserialized ConfigurationSpace object
- Return type:
- ConfigSpace.read_and_write.pcs_new.write(configuration_space)[source]¶
Create a string representation of a
ConfigurationSpace
in pcs_new format. This string can be written to file.Example
>>> import ConfigSpace as CS >>> import ConfigSpace.hyperparameters as CSH >>> from ConfigSpace.read_and_write import pcs_new >>> cs = CS.ConfigurationSpace() >>> cs.add_hyperparameter(CSH.CategoricalHyperparameter('a', choices=[1, 2, 3])) a, Type: Categorical, Choices: {1, 2, 3}, Default: 1 >>> with open('configspace.pcs_new', 'w') as fh: ... fh.write(pcs_new.write(cs)) 27
- Parameters:
configuration_space (
ConfigurationSpace
) – a configuration space- Returns:
The string representation of the configuration space
- Return type:
str
Serialization with pcs (old format)¶
The old PCS format is part of the Algorithm Configuration Library.
A detailed explanation of the old PCS format can be found here.
- ConfigSpace.read_and_write.pcs.read(pcs_string)[source]¶
Read in a
ConfigurationSpace
definition from a pcs file.from ConfigSpace import ConfigurationSpace from ConfigSpace.read_and_write import pcs cs = ConfigurationSpace({"a": [1, 2, 3]}) with open('configspace.pcs', 'w') as f: f.write(pcs.write(cs)) with open('configspace.pcs', 'r') as f: deserialized_conf = pcs.read(f)
- Parameters:
pcs_string (Iterable[str]) – ConfigSpace definition in pcs format as an iterable of strings
- Returns:
The deserialized ConfigurationSpace object
- Return type:
- ConfigSpace.read_and_write.pcs.write(configuration_space)[source]¶
Create a string representation of a
ConfigurationSpace
in pcs format. This string can be written to file.from ConfigSpace import ConfigurationSpace from ConfigSpace.read_and_write import pcs cs = ConfigurationSpace({"a": [1, 2, 3]}) with open('configspace.pcs', 'w') as fh: fh.write(pcs.write(cs))
- Parameters:
configuration_space (
ConfigurationSpace
) – a configuration space- Returns:
The string representation of the configuration space
- Return type:
str