OmegaConf – how to delete a single parameter

Question:

I have a code that looks something like this:

def generate_constraints(c):
    if c.name == 'multibodypendulum':
        con_fnc = MultiBodyPendulum(**c)

where c is an OmegaConf object containing a bunch of parameters. Now I would like to pass all the parameters in c except the name to the MultiBodyPendulum class (the name was used to identify the class to send the rest of the parameters to). However I have no idea how to make a copy of c without one parameter.
Does anyone have a good solution for this?

Asked By: Tue

||

Answers:

You could try to get a dictionary from the instance using my_dict = c.__dict__ and then remove the class from the dictionary using pop()

something like:

my_dict = c.__dict__
old_name = my_dict.pop("name", None)
con_fnc = MultiBodyPendulum(**my_dict)

That way you would add every parameter except for the name, if you wanted to add a different name you can add it to the new instance creation like:

con_fnc = MultiBodyPendulum(name="New Name", **my_dict)

Edit:

To conserve the OmegaConf functionality use:

my_dict = OmegaConf.to_container(c)
Answered By: Sergio García

I’d recommend using DictConfig.copy to create a copy and del to delete attributes/items from the DictConfig object.

config: DictConfig = ...

config_copy = config.copy()
del config_copy.unwanted_parameter
pendulum = MultiBodyPendulum(**config_copy)
Answered By: Jasha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.