Skip to content

Hydra tools¤

Utility functions for integrating f3dasm with Hydra configuration management.

See also

f3dasm.hydra_tools.update_config_with_experiment_sample(config: OmegaConf, experiment_sample: ExperimentSample, force_add: bool = False) -> OmegaConf ¤

Update the config with the values from the experiment sample.

Parameters:

Name Type Description Default
config OmegaConf

The configuration to update.

required
experiment_sample ExperimentSample

The experiment sample to update the configuration with.

required
force_add bool

If True, the function will add keys that are not present in the configuration. If False, the function will ignore keys that are not present in the configuration. Default is False.

False

Returns:

Type Description
OmegaConf

The updated configuration.

Notes

The function will update the configuration with the values from the experiment sample. The function will only update the configuration with values that are present in the experiment sample. If the experiment sample contains values that are not present in the configuration, they will be ignored. Keys can be nested using dots, e.g. 'a.b' will update the value of 'c' in the configuration key 'b'.

The function will return a new configuration object with the updated values. The original configuration object will not be modified.

Examples:

>>> from omegaconf import OmegaConf
>>> from f3dasm._src.experimentdata.experimentsample
import ExperimentSample
>>> config = OmegaConf.create({'param1': 1, 'param2': 2})
>>> sample = ExperimentSample(input_data={'param1': 10})
>>> updated_config = update_config_with_experiment_sample(config, sample)
>>> print(updated_config)
{'param1': 10, 'param2': 2}
Source code in src/f3dasm/_src/hydra_utils.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def update_config_with_experiment_sample(
    config: OmegaConf,
    experiment_sample: ExperimentSample,
    force_add: bool = False,
) -> OmegaConf:
    """
    Update the config with the values from the experiment sample.

    Parameters
    ----------
    config : OmegaConf
        The configuration to update.
    experiment_sample : ExperimentSample
        The experiment sample to update the configuration with.
    force_add : bool, optional
        If True, the function will add keys that are not present in the
        configuration. If False, the function will ignore keys that are not
        present in the configuration. Default is False.

    Returns
    -------
    OmegaConf
        The updated configuration.

    Notes
    -----
    The function will update the configuration with the values from the
    experiment sample. The function will only update the configuration with
    values that are present in the experiment sample. If the experiment sample
    contains values that are not present in the configuration, they will be
    ignored. Keys can be nested using dots, e.g. 'a.b' will update the value
    of 'c' in the configuration key 'b'.

    The function will return a new configuration object with the
    updated values. The original configuration object will not be modified.

    Examples
    --------
    >>> from omegaconf import OmegaConf
    >>> from f3dasm._src.experimentdata.experimentsample
    import ExperimentSample
    >>> config = OmegaConf.create({'param1': 1, 'param2': 2})
    >>> sample = ExperimentSample(input_data={'param1': 10})
    >>> updated_config = update_config_with_experiment_sample(config, sample)
    >>> print(updated_config)
    {'param1': 10, 'param2': 2}
    """
    cfg = deepcopy(config)
    for key, value in experiment_sample.to_dict().items():
        try:
            OmegaConf.update(cfg, key, value, force_add=force_add)
        except AttributeError:
            continue

    return cfg