pythonfb-hydraomegaconf

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?


Solution

  • 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)