in summarize_from_feedback/utils/hyperparams.py [0:0]
def override_from_pair(self, flat_k, v, separator="."):
"""Overrides values from a key-value pair (flat_k, v) = ('x.y', 1) or ('name', "foobar").
Treats keys with separators as paths into nested HParams.
"""
typemap = _flat_type_map(type(self), separator=separator)
*ks, f = flat_k.split(separator)
# Traverse down to the nested hparam value which the field will be set on
hp = self
for i, k in enumerate(ks):
try:
hp = getattr(hp, k)
except AttributeError:
raise AttributeError(
f"{separator.join(ks[:i]) if i else 'hparams'} is {hp} which has no field '{k}'"
)
try:
old_v = getattr(hp, f)
except AttributeError:
raise AttributeError(f"{separator.join(ks)} is {hp} which has no field '{f}'")
# Figure out what to set; handle the special 'on' and 'off' values for nested hparams
hps_cls = _hparam_constructible_class(typemap[flat_k])
if hps_cls is not None:
if v == "on":
if old_v is None:
# Set the nested hparam class to its default values
v = hps_cls()
else:
# The field is already set; skip it so we don't override any of its values
# with the defaults
return
elif v == "off":
v = None
# Set it!
check_type(flat_k, v, typemap[flat_k])
setattr(hp, f, v)