Skip to content

Commit 3c2fa4b

Browse files
committed
Support passing batch kwargs in run_chimera_batches
1 parent 077d238 commit 3c2fa4b

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

rubin_sim/maf/chimera_progress.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"make_chimera_summary_table",
66
)
77

8+
import ast
89
import datetime
910
import glob
1011
import os
@@ -180,6 +181,7 @@ def run_chimera_batches(
180181
chimera_specs: list[tuple[int, str]],
181182
batch_func: Callable[..., dict] | None = None,
182183
out_dir: str = ".",
184+
batch_kwargs: dict | None = None,
183185
) -> str:
184186
"""Run MAF metric batches on a collection of chimera visit sequences.
185187
@@ -199,6 +201,9 @@ def run_chimera_batches(
199201
Defaults to `rubin_sim.maf.batches.glanceBatch`.
200202
out_dir : `str`, optional
201203
Directory for results_db and metric output files.
204+
batch_kwargs : `dict`, optional
205+
Additional keyword arguments forwarded to ``batch_func`` for each
206+
chimera run.
202207
203208
Returns
204209
-------
@@ -210,17 +215,18 @@ def run_chimera_batches(
210215

211216
os.makedirs(out_dir, exist_ok=True)
212217
results_db = db.ResultsDb(out_dir=out_dir)
218+
batch_kwargs = {} if batch_kwargs is None else dict(batch_kwargs)
213219

214220
for transition_dayobs, hdf5_path in chimera_specs:
215221
run_name = _run_name_from_dayobs(transition_dayobs)
216222
try:
217-
bdict = batch_func(run_name=run_name)
223+
bdict = batch_func(run_name=run_name, **batch_kwargs)
218224
except TypeError as batch_error:
219225
if "got an unexpected keyword argument 'run_name'" not in str(batch_error):
220226
# we got some other exception, just pass it along.
221227
raise
222228
# We have a batch that uses runName instead of run_name.
223-
bdict = batch_func(runName=run_name)
229+
bdict = batch_func(runName=run_name, **batch_kwargs)
224230

225231
group = mb.MetricBundleGroup(
226232
bdict,
@@ -370,8 +376,33 @@ def build_chimeras_cmd(consdb_file, opsim_file, start_dayobs, end_dayobs, step,
370376
show_default=True,
371377
help="Batch function name from rubin_sim.maf.batches.",
372378
)
373-
def run_chimera_batches_cmd(chimera_dir, out_dir, batch):
379+
@click.option(
380+
"--batch-kwarg",
381+
"batch_kwargs",
382+
multiple=True,
383+
help="Additional batch kwarg as KEY=VALUE. May be specified multiple times.",
384+
)
385+
def run_chimera_batches_cmd(chimera_dir, out_dir, batch, batch_kwargs):
374386
"""Run MAF metric batches on all chimera HDF5 files in a directory."""
387+
parsed_batch_kwargs = {}
388+
for item in batch_kwargs:
389+
if "=" not in item:
390+
raise click.BadParameter(
391+
f"Invalid --batch-kwarg '{item}'. Expected KEY=VALUE.",
392+
param_hint="--batch-kwarg",
393+
)
394+
key, value_text = item.split("=", 1)
395+
if not key:
396+
raise click.BadParameter(
397+
f"Invalid --batch-kwarg '{item}'. Key cannot be empty.",
398+
param_hint="--batch-kwarg",
399+
)
400+
try:
401+
value = ast.literal_eval(value_text)
402+
except (ValueError, SyntaxError):
403+
value = value_text
404+
parsed_batch_kwargs[key] = value
405+
375406
batch_func = getattr(batches, batch, None)
376407
if batch_func is None:
377408
raise click.BadParameter(
@@ -386,7 +417,12 @@ def run_chimera_batches_cmd(chimera_dir, out_dir, batch):
386417
t = _dayobs_from_filename(path)
387418
if t is not None:
388419
chimera_specs.append((t, path))
389-
results_db_path = run_chimera_batches(chimera_specs, batch_func=batch_func, out_dir=out_dir)
420+
results_db_path = run_chimera_batches(
421+
chimera_specs,
422+
batch_func=batch_func,
423+
out_dir=out_dir,
424+
batch_kwargs=parsed_batch_kwargs,
425+
)
390426
click.echo(f"Results written to {results_db_path}.")
391427

392428

0 commit comments

Comments
 (0)