-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
119 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import inspect | ||
from copy import deepcopy | ||
from functools import wraps | ||
from typing import Callable | ||
|
||
|
||
def init_collector(init: Callable) -> Callable: | ||
""" | ||
Make decorator for collecting init parameters. | ||
N.B. if init method has positional only parameters, they will be ignored. | ||
""" | ||
|
||
@wraps(init) | ||
def wrapper(*args, **kwargs): | ||
self, *args = args | ||
init_args = inspect.signature(self.__init__).parameters | ||
|
||
deepcopy_args = deepcopy(args) | ||
deepcopy_kwargs = deepcopy(kwargs) | ||
|
||
self._init_params = {} | ||
args_dict = dict( | ||
zip([arg for arg, param in init_args.items() if param.kind == param.POSITIONAL_OR_KEYWORD], deepcopy_args) | ||
) | ||
self._init_params.update(args_dict) | ||
self._init_params.update(deepcopy_kwargs) | ||
|
||
return init(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def create_type_with_init_collector(type_: type) -> type: | ||
"""Create type with init decorated with init_collector.""" | ||
previous_frame = inspect.stack()[1] | ||
module = inspect.getmodule(previous_frame[0]) | ||
if module is None: | ||
return type_ | ||
new_type = type(type_.__name__, (type_,), {"__module__": module.__name__}) | ||
if hasattr(type_, "__init__"): | ||
new_type.__init__ = init_collector(new_type.__init__) # type: ignore | ||
return new_type |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
from copy import deepcopy | ||
|
||
from etna.core.utils import create_type_with_init_collector | ||
|
||
|
||
from pytorch_lightning.callbacks import __all__ as pl_callbacks | ||
|
||
generated_types = [] | ||
|
||
for type_name in pl_callbacks: | ||
|
||
type_ = deepcopy(getattr(__import__('pytorch_lightning.callbacks', fromlist=[type_name]), type_name)) | ||
new_type = create_type_with_init_collector(type_) | ||
globals()[type_name] = new_type | ||
generated_types.append(new_type) | ||
|
||
__all__ = generated_types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76b8081
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://etna-docs.netlify.app as production
🚀 Deployed on https://632da342299c480449ae780c--etna-docs.netlify.app