dvc icon indicating copy to clipboard operation
dvc copied to clipboard

Conditional parameters

Open schwobr opened this issue 3 years ago • 3 comments
trafficstars

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 in vars depending on selector_param. I thought this would work but it seems like templating is disabled in vars.
  • In a similar vein, I could put all the dependent params in another file dependent_params.yaml and then load them in vars using templating. That would require to be able to rename the loaded params to avoid nested templating, like dependent_params.yaml:${selector_param} as selected_params. That would allow to do something like ${selected_params.p1}
  • Create a new keyword in dvc.yaml that 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.

schwobr avatar Sep 12 '22 11:09 schwobr

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'

daavoo avatar Sep 12 '22 12:09 daavoo

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 !

schwobr avatar Sep 12 '22 12:09 schwobr

Forgot to mention, you need to run:

dvc config hydra.enabled True

daavoo avatar Sep 12 '22 12:09 daavoo

Closing as I believe it was solved. Don't hesitate to re-open

daavoo avatar Nov 23 '22 11:11 daavoo