pg.materialize

Accessible via pg.materialize, pg.hyper.materialize.

materialize(value, parameters, use_literal_values=True, where=None)[source]

Materialize a (maybe) hyper value using a DNA or parameter dict.

Example:

hyper_dict = pg.Dict(x=pg.oneof(['a', 'b']), y=pg.floatv(0.0, 1.0))

# Materialize using DNA.
assert pg.materialize(
  hyper_dict, pg.DNA([0, 0.5])) == pg.Dict(x='a', y=0.5)

# Materialize usign key value pairs.
# See `pg.DNA.from_dict` for more details.
assert pg.materialize(
  hyper_dict, {'x': 0, 'y': 0.5}) == pg.Dict(x='a', y=0.5)

# Partially materialize.
v = pg.materialize(
  hyper_dict, pg.DNA(0), where=lambda x: isinstance(x, pg.hyper.OneOf))
assert v == pg.Dict(x='a', y=pg.floatv(0.0, 1.0))
Return type:

Any

Parameters:
  • value – A (maybe) hyper value

  • parameters – A DNA object or a dict of string (key path) to a string (in format of ‘<selected_index>/<num_choices>’ for geno.Choices, or ‘<float_value>’ for geno.Float), or their literal values when use_literal_values is set to True.

  • use_literal_values – Applicable when parameters is a dict. If True, the values in the dict will be from geno.Choices.literal_values for geno.Choices.

  • where – Function to filter hyper primitives. If None, all hyper primitives from value will be included in the encoding/decoding process. Otherwise only the hyper primitives on which ‘where’ returns True will be included. where can be useful to partition a search space into separate optimization processes. Please see ‘Template’ docstr for details.

Returns:

A materialized value.

Raises:
  • TypeError – if parameters is not a DNA or dict.

  • ValueError – if parameters cannot be decoded.