Skip to content

Instantly share code, notes, and snippets.

@g-simmons
Created May 10, 2023 22:51
Show Gist options
  • Save g-simmons/64acf1d6dd0a4ae2da1f805e527af434 to your computer and use it in GitHub Desktop.
Save g-simmons/64acf1d6dd0a4ae2da1f805e527af434 to your computer and use it in GitHub Desktop.
convert a dvc file template with parameter matrices into a final dvc file with corresponding foreach grids
import yaml
from itertools import product, starmap
from collections import namedtuple
import click
judgement_styles = [1, 2, 3, 4, 5]
groups = ["liberal", "conservative"]
def named_product(**items):
Product = namedtuple("Product", items.keys())
return [x._asdict() for x in starmap(Product, product(*items.values()))]
def yamlgrid(**items):
return yaml.dump(named_product(**items), default_flow_style=False)
@click.command(
name="yamlgrid",
context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
),
)
@click.pass_context
def cli(ctx):
dvcyaml = yaml.safe_load(open("dvc_matrix.yaml", "r"))
for _, stage in dvcyaml["stages"].items():
if "foreach-matrix" in stage:
args = stage["foreach-matrix"]
stage["foreach"] = named_product(**args)
del stage["foreach-matrix"]
with open("dvc.yaml", "w") as f:
yaml.dump(dvcyaml, f, default_flow_style=False)
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment