How to pass a reference to a function in yaml config file #13613
-
If I want to use CLI to initialize datamodule object I should use:
But what if one the
Thank you for attention. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
This is supported in jsonargparse. Just use class MyData:
def __init__(self, data_dir: str, filter: Callable):
... Then the config could be as you had it: class_path: foo.datasets.my_data
init_args:
data_dir: data
filter: foo.datasets.filter1 Or class_path: foo.datasets.my_data
init_args:
data_dir: data
filter:
class_path: path.to.callable.class
init_args:
param1: val1
... |
Beta Was this translation helpful? Give feedback.
-
Using the class UseInsteadOfFunction(ReturnTypeClass): # <-- return type class is the class that you would have your function return
def __new__(*args, **kwargs) -> ReturnTypeClass: # <-- not __init__!
return your_function(*args, **kwargs) |
Beta Was this translation helpful? Give feedback.
This is supported in jsonargparse. Just use
Callable
as type hint, see its mention in the docs. Basically you would have:Then the config could be as you had it:
Or
filter
could be a class that once instantiated becomes callable, like: