arithmetic manipulations in YAML configuration file with omegaconf

1.7k Views Asked by At

Currently, I have this configuration file which I'm using as an input to hydra/omegaconf

db: 
  range: 
    - -10
    - 10

Since the values in the range field are related by simple algebra, I'm looking for a way to encode this into the configuration file. Conceptually, I would like to have something like this:

db: 
  db_val: 10
  range: 
    - (-1) * ${..db_val}
    - ${..db_val}

But this not seems to work.

3

There are 3 best solutions below

0
On

Arithmetic operations in the OmegaConf configs are not currently supported. There is feature request to support it though.

0
On

You could define a custom interpolation resolver:

def symmetric_range(a):
  return (-a, a)

OmegaConf.register_resolver("symmetric_range", symmetric_range)

and then in your config file:

db: 
  range: ${symmetric_range:10}

or

db:
  db_val: 10
  range: ${symmetric_range:${db_val}}
0
On

A simple solution would be to register an eval resolver

OmegaConf.register_new_resolver("eval", eval)

and then in your config file:

db: 
  db_val: 10
  range: 
    - ${eval:'(-1) * ${..db_val}'}
    - ${..db_val}