Skip to content

Commit

Permalink
add support for default configs (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult authored Dec 4, 2024
1 parent 8771331 commit 57bbe09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/anemoi/inference/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ class RunCmd(Command):
need_logging = False

def add_arguments(self, command_parser):
command_parser.add_argument("--defaults", action="append", help="Sources of default values.")
command_parser.add_argument("config", help="Path to config file.")
command_parser.add_argument("overrides", nargs="*", help="Overrides.")

def run(self, args):

config = load_config(args.config, args.overrides)
config = load_config(args.config, args.overrides, defaults=args.defaults)

if config.description is not None:
LOG.info("%s", config.description)

runner = DefaultRunner(config)

Expand Down
21 changes: 17 additions & 4 deletions src/anemoi/inference/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Configuration(BaseModel):
class Config:
extra = "forbid"

description: str | None = None

checkpoint: str
"""A path an Anemoi checkpoint file."""

Expand Down Expand Up @@ -81,12 +83,23 @@ class Config:
"""A dictionary of development hacks to apply to the runner. This is used to test new features or to work around"""


def load_config(path, overrides, Configuration=Configuration):
def load_config(path, overrides, defaults=None, Configuration=Configuration):

# Load the configuration
config = {}

# Set default values
if defaults is not None:
if not isinstance(defaults, list):
defaults = [defaults]
for d in defaults:
if isinstance(d, str):
with open(d) as f:
d = yaml.safe_load(f)
config.update(d)

# Load the configuration
with open(path) as f:
config = yaml.safe_load(f)
config.update(yaml.safe_load(f))

# Apply overrides
for override in overrides:
Expand All @@ -97,7 +110,7 @@ def load_config(path, overrides, Configuration=Configuration):
path = path.setdefault(key, {})
path[keys[-1]] = value

# Load the configuration
# Validate the configuration
config = Configuration(**config)

# Set environment variables found in the configuration
Expand Down

0 comments on commit 57bbe09

Please sign in to comment.