Skip to content

Commit

Permalink
WIP added hydra utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvanderschelling committed Mar 29, 2024
1 parent 24b7699 commit f96cc24
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/f3dasm/_src/hydra_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
This module defines utility functions for the Hydra configuration system.
"""
# Modules
# =============================================================================

# Standard
from copy import deepcopy

# Third-party
from omegaconf import OmegaConf

# Local
from .experimentdata.experimentsample import ExperimentSample

# Authorship & Credits
# =============================================================================
__author__ = 'Martin van der Schelling ([email protected])'
__credits__ = ['Martin van der Schelling']
__status__ = 'Alpha'
# =============================================================================
#
# =============================================================================


def update_config_with_experiment_sample(
config: OmegaConf, experiment_sample: ExperimentSample) -> 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
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.
"""
cfg = deepcopy(config)
for key, value in experiment_sample.to_dict().items():
try:
OmegaConf.update(cfg, key, value)
except AttributeError:
continue

return cfg
21 changes: 21 additions & 0 deletions src/f3dasm/hydra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Module for hydra utilities
"""
# Modules
# =============================================================================

# Local
from ._src.hydra_utils import update_config_with_experiment_sample

# Authorship & Credits
# =============================================================================
__author__ = 'Martin van der Schelling ([email protected])'
__credits__ = ['Martin van der Schelling']
__status__ = 'Stable'
# =============================================================================
#
# =============================================================================

__all__ = [
'update_config_with_experiment_sample',
]

0 comments on commit f96cc24

Please sign in to comment.