dvc
dvc copied to clipboard
Conditional parameters
I'd like to be able to select a set of parameters depending on another parameter. I've looked into it quite a bit and I don't think it is a current feature, but don't hesitate to tell me if I'm mistaken.
To be more clear, I would like my params.yaml to look like this:
selector_param: A
dependent_params:
A:
p1: a1
p2: a2
B:
p1: b1
p2: b2
Then I could load the corresponding set of dependent_params depending on the selector_param. I thought about several ways of doing this but none seemed to work:
- Use nested templating, like
${dependent_params.${selector_param}}. I don't find this option very elegant. - Have several parameters files (
A.yaml,B.yaml) for each option and load the right one invarsdepending onselector_param. I thought this would work but it seems like templating is disabled invars. - In a similar vein, I could put all the dependent params in another file
dependent_params.yamland then load them invarsusing templating. That would require to be able to rename the loaded params to avoid nested templating, likedependent_params.yaml:${selector_param} as selected_params. That would allow to do something like${selected_params.p1} - Create a new keyword in
dvc.yamlthat could look like:
param_load:
from: dependent_params
key: ${selector_param}
cmd: python script.py ${dependent_params}
There are probably other solutions, but all in all I feel this would be a nice feature to have.
We have recently added support for using Hydra alongside DVC in https://github.com/iterative/dvc/pull/8093
I believe it could be a simpler solution for your use case.
Setup would be to split each dependent config into separated files grouped under a selector folder:
$ tree conf
conf
├── config.yaml
└── option
├── A.yaml
└── B.yaml
$ cat conf/option/A.yaml
A:
p1: a1
p2: a2
And reference the selector in the defaults list:
$ cat conf/config.yaml
defaults:
- option: A
So you could have dvc.yaml:
stages:
foo:
cmd: python foo.py ${option}
And run as:
$ dvc exp run -S 'option=B'
Thanks for the quick answer. I was feeling there was something for me in dvc exp run but I couldn't quite understand the difference with dvc repro. I'll try that, thanks !
Forgot to mention, you need to run:
dvc config hydra.enabled True
Closing as I believe it was solved. Don't hesitate to re-open