Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Instantiation from config using path to class / function #1606

Open
Mxttak opened this issue Nov 19, 2024 · 0 comments
Open

[Feature] Instantiation from config using path to class / function #1606

Mxttak opened this issue Nov 19, 2024 · 0 comments

Comments

@Mxttak
Copy link

Mxttak commented Nov 19, 2024

What is the feature?

For new users (as I am) it might be hard to navigate the registry - it is not immediately obvious where and how a component is instantiated. Also, it might be challenging to maintain multiple registries in large projects.

An alternative approach to instantiation is to use the python path to a component (class, function, ...) as e.g. done here https://github.com/Farama-Foundation/HighwayEnv/blob/master/highway_env/utils.py#L38

Applying the approach from highway-env, instantiating from config could look as follows

cfg = dict(type="my_package.subdir.ComponentClass", one_param=-2., another_param="a_param")
obj_from_config = instantiate_from_config(cfg)

with

def instantiate_from_config(cfg: dict) -> Any:
     cfg_copy = copy(cfg)
     cls =  class_from_path(path=cfg_copy.pop("type"))
     if cls.hasattr("from_config"):
          return cls.from_config(cfg_copy)
     return cls(**cfg_copy)

This object instantiation approach does not require any registration and can instantiate any object from any library that is installed in the same python environment.
Additionally, it can be extended to handle partial functions and many more.

What is your opinion on that? I can implement a small example demoing this instantiation

Any other context?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant