Skip to content

Commit a5177d3

Browse files
authored
feat: Allow empty config path to run from overrides (#206)
1 parent 03a7f65 commit a5177d3

File tree

8 files changed

+43
-10
lines changed

8 files changed

+43
-10
lines changed

docs/inference/apis/code/level3_1.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
anememoi-inference run aifs.yaml
1+
anemoi-inference run aifs.yaml

docs/inference/apis/code/level3_2.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
anememoi-inference run aifs.yaml date=2020-01-01
1+
anemoi-inference run aifs.yaml date=2020-01-01

docs/inference/apis/code/level3_3.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
anemoi-inference run checkpoint=mycheckpoint.ckpt date=2020-01-01

docs/inference/apis/level3.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ You can also override values by providing them on the command line:
2626
.. literalinclude:: code/level3_2.sh
2727
:language: bash
2828

29+
You can also run entirely from the command line without a config file,
30+
by passing all required options as an override:
31+
32+
.. literalinclude:: code/level3_3.sh
33+
:language: bash
34+
2935
The configuration below shows how to run the inference from the data
3036
that was used to train the model, by setting ``dataset`` entry to
3137
``true``:

src/anemoi/inference/commands/couple.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ def add_arguments(self, command_parser: ArgumentParser) -> None:
4040
The argument parser to which the arguments will be added.
4141
"""
4242
command_parser.add_argument("--defaults", action="append", help="Sources of default values.")
43-
command_parser.add_argument("config", help="Path to config file.")
44-
command_parser.add_argument("overrides", nargs="*", help="Overrides.")
43+
command_parser.add_argument(
44+
"config",
45+
help="Path to config file. Can be omitted to pass config with overrides and defaults.",
46+
)
47+
command_parser.add_argument("overrides", nargs="*", help="Overrides as key=value")
4548

4649
def run(self, args: Namespace) -> None:
4750
"""Run the couple command.
@@ -51,7 +54,15 @@ def run(self, args: Namespace) -> None:
5154
args : Namespace
5255
The arguments passed to the command.
5356
"""
54-
config = CoupleConfiguration.load(args.config, args.overrides, defaults=args.defaults)
57+
if "=" in args.config:
58+
args.overrides.append(args.config)
59+
args.config = {}
60+
61+
config = CoupleConfiguration.load(
62+
args.config,
63+
args.overrides,
64+
defaults=args.defaults,
65+
)
5566

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

src/anemoi/inference/commands/retrieve.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,19 @@ def add_arguments(self, command_parser: ArgumentParser) -> None:
140140
The argument parser to which the arguments will be added.
141141
"""
142142
command_parser.description = self.__doc__
143-
command_parser.add_argument("config", type=str, help="Path to config file")
143+
command_parser.add_argument(
144+
"config",
145+
type=str,
146+
help="Path to config file. Can be omitted to pass config with overrides and defaults.",
147+
)
144148
command_parser.add_argument("--defaults", action="append", help="Sources of default values.")
145149
command_parser.add_argument("--date", type=str, help="Date")
146150
command_parser.add_argument("--output", type=str, default=None, help="Output file")
147151
command_parser.add_argument("--staging-dates", type=str, help="Path to a file with staging dates")
148152
command_parser.add_argument("--extra", action="append", help="Additional request values. Can be repeated")
149153
command_parser.add_argument("--retrieve-fields-type", type=str, help="Type of fields to retrieve")
150154
command_parser.add_argument("--use-scda", action="store_true", help="Use scda stream for 6/18 input time")
151-
command_parser.add_argument("overrides", nargs="*", help="Overrides.")
155+
command_parser.add_argument("overrides", nargs="*", help="Overrides as key=value")
152156

153157
def run(self, args: Namespace) -> None:
154158
"""Run the retrieve command.
@@ -158,6 +162,10 @@ def run(self, args: Namespace) -> None:
158162
args : Namespace
159163
The arguments passed to the command.
160164
"""
165+
if "=" in args.config:
166+
args.overrides.append(args.config)
167+
args.config = {}
168+
161169
config: RunConfiguration = RunConfiguration.load(args.config, args.overrides, defaults=args.defaults)
162170

163171
runner = create_runner(config)

src/anemoi/inference/commands/run.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ def add_arguments(self, command_parser: ArgumentParser) -> None:
3232
The argument parser to which the arguments will be added.
3333
"""
3434
command_parser.add_argument("--defaults", action="append", help="Sources of default values.")
35-
command_parser.add_argument("config", help="Path to config file.")
36-
command_parser.add_argument("overrides", nargs="*", help="Overrides.")
35+
command_parser.add_argument(
36+
"config",
37+
help="Path to config file. Can be omitted to pass config with overrides and defaults.",
38+
)
39+
command_parser.add_argument("overrides", nargs="*", help="Overrides as key=value")
3740

3841
def run(self, args: Namespace) -> None:
3942
"""Run the inference command.
@@ -43,6 +46,10 @@ def run(self, args: Namespace) -> None:
4346
args : Namespace
4447
The arguments passed to the command.
4548
"""
49+
if "=" in args.config:
50+
args.overrides.append(args.config)
51+
args.config = {}
52+
4653
config = RunConfiguration.load(
4754
args.config,
4855
args.overrides,

src/anemoi/inference/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def load(
7272

7373
# Load the configuration
7474
if isinstance(path, dict):
75-
config = deepcopy(path)
75+
config.update(deepcopy(path))
7676
else:
7777
with open(path) as f:
7878
config.update(yaml.safe_load(f))

0 commit comments

Comments
 (0)