Tue
Tue

Reputation: 432

OmegaConf - how to delete a single parameter

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?

Upvotes: 2

Views: 2505

Answers (2)

Sergio García
Sergio García

Reputation: 533

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)

Upvotes: 1

Jasha
Jasha

Reputation: 7639

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)

Upvotes: 4

Related Questions