Registry#

class torch_ecg.utils.Registry(name: str)[source]#

Bases: object

Registry for managing and building modules.

A registry is used to map strings (module names) to classes, and provides a unified interface to instantiate modules from configurations.

Parameters:

name (str) – Name of the registry.

Examples

>>> BACKBONES = Registry("backbones")
>>> @BACKBONES.register()
... class ResNet(nn.Module):
...     def __init__(self, depth):
...         self.depth = depth
>>> # Build from string
>>> model = BACKBONES.build("ResNet", depth=50)
>>> # Build from config dict
>>> model = BACKBONES.build({"name": "ResNet", "depth": 101})
build(config: str | Dict[str, Any], **kwargs: Any) Any[source]#

Build a module from a configuration.

Parameters:
  • config (str or dict) – Configuration of the module. If it’s a string, it should be the name of the registered module. If it’s a dict, it must contain a “name” key.

  • **kwargs (Any) – Additional keyword arguments passed to the module’s constructor.

Returns:

The instantiated module.

Return type:

Any

get(name: str) type | None[source]#

Get the module class by name.

Parameters:

name (str) – Name of the module.

Returns:

The registered module class.

Return type:

type or None

list_all() List[str][source]#

List all registered modules.

Returns:

A list of all registered module names.

Return type:

List[str]

register(name: str | None = None, override: bool = False) Any[source]#

Decorator to register a module.

Parameters:
  • name (str, optional) – Name of the module. If not specified, the class name will be used.

  • override (bool, default False) – Whether to override the existing module with the same name.