Component Registry¶
spectrans.core.registry ¶
Component registry system for dynamic component registration and retrieval.
This module implements a registry pattern that allows spectral transformer components to be registered at runtime and retrieved by name. The registry system enables modular composition of different spectral transforms, mixing layers, attention mechanisms, and complete models without tight coupling.
The registry supports metadata storage, configuration-driven instantiation, and category-based organization. This design facilitates experimentation with different component combinations and makes the library easily extensible with custom implementations.
Classes:
| Name | Description |
|---|---|
ComponentRegistry |
Central registry for storing and retrieving component classes. |
Functions:
| Name | Description |
|---|---|
register_component |
Decorator to register component classes in the global registry. |
get_component |
Retrieve a registered component class by category and name. |
create_component |
Create an instance of a registered component with parameters. |
list_components |
List available components in a category or all categories. |
Examples:
Registering a custom component:
>>> from spectrans.core.registry import register_component
>>> from spectrans.layers.mixing.base import MixingLayer
>>> @register_component('mixing', 'my_custom_mixer')
... class CustomMixer(MixingLayer):
... def forward(self, x):
... return x # Custom implementation
Using the registry to create components:
>>> from spectrans.core.registry import create_component, list_components
>>> # List available transforms
>>> list_components('transform')
['fourier', 'cosine', 'hadamard', 'wavelet']
>>> # Create a Fourier transform instance
>>> fft = create_component('transform', 'fourier', dim=-1)
Configuration-driven component creation:
>>> from spectrans.core.registry import registry
>>> config = {'type': 'fourier', 'params': {'dim': -1}}
>>> transform = registry.create_from_config('transform', config)
Notes
Registry Architecture:
The registry implements several design patterns:
- Singleton Pattern: Global registry instance for system-wide access
- Factory Pattern: Component creation through factory methods
- Registry Pattern: Dynamic component discovery and instantiation
- Decorator Pattern: Clean component registration via decorators
Component Categories:
- 'transform': Spectral transforms (FFT, DCT, DWT, Hadamard)
- 'mixing': Token mixing layers (FourierMixing, GlobalFilter, AFNO)
- 'attention': Spectral attention mechanisms (SpectralAttention, LST)
- 'block': Complete transformer blocks combining mixing + FFN
- 'model': Full model implementations (FNet, GFNet, etc.)
- 'kernel': Kernel functions for attention approximation
- 'operator': Neural operators (FNO layers)
The registry supports metadata storage for each component, enabling rich component descriptions and configuration schemas.
Thread Safety: The registry is not thread-safe. Component registration should occur during module initialization, before concurrent access.
See Also
spectrans.core.base : Base classes for components stored in registry spectrans.core.types : Type definitions for registry operations
Classes¶
ComponentRegistry ¶
Registry for dynamically registering and retrieving components.
This registry allows for flexible component registration and retrieval, enabling users to easily extend the library with custom implementations.
Attributes:
| Name | Type | Description |
|---|---|---|
_components |
RegistryDict
|
Dictionary mapping component categories to their registered components. |
_metadata |
dict[str, dict[str, dict[str, Any]]]
|
Dictionary storing metadata about registered components. |
Methods:
| Name | Description |
|---|---|
register |
Register a component. |
get |
Get a registered component. |
list |
List registered components. |
get_metadata |
Get metadata for a registered component. |
create |
Create an instance of a registered component. |
create_from_config |
Create a component instance from a configuration dictionary. |
__contains__ |
Check if a component is registered. |
clear |
Clear registered components. |
Source code in spectrans/core/registry.py
Functions¶
register ¶
register(category: ComponentType, name: str, component: ComponentClass, metadata: dict[str, Any] | None = None) -> None
Register a component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component (e.g., 'transform', 'mixing'). |
required |
name
|
str
|
Name to register the component under. |
required |
component
|
ComponentClass
|
The component class to register. |
required |
metadata
|
dict[str, Any] | None
|
Optional metadata about the component. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the category is unknown or component name already exists. |
Source code in spectrans/core/registry.py
get ¶
Get a registered component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
name
|
str
|
Name of the component. |
required |
Returns:
| Type | Description |
|---|---|
ComponentClass
|
The registered component class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the category or component name is not found. |
Source code in spectrans/core/registry.py
list ¶
List registered components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType | None
|
Category to list components for. If None, lists all categories. |
None
|
Returns:
| Type | Description |
|---|---|
list[str] | dict[str, list[str]]
|
If category is specified, returns list of component names. Otherwise, returns dict mapping categories to component names. |
Source code in spectrans/core/registry.py
get_metadata ¶
Get metadata for a registered component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
name
|
str
|
Name of the component. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Metadata dictionary if available, None otherwise. |
Source code in spectrans/core/registry.py
create ¶
Create an instance of a registered component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
name
|
str
|
Name of the component. |
required |
**kwargs
|
Any
|
Keyword arguments to pass to the component constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Instance of the component. |
Source code in spectrans/core/registry.py
create_from_config ¶
Create a component instance from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
config
|
ConfigDict
|
Configuration dictionary with 'type' and optional 'params' keys. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Instance of the component. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'type' key is missing from config. |
Source code in spectrans/core/registry.py
__contains__ ¶
Check if a component is registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
tuple[ComponentType, str]
|
Tuple of (category, name) to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the component is registered. |
Source code in spectrans/core/registry.py
clear ¶
Clear registered components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType | None
|
Category to clear. If None, clears all categories. |
None
|
Source code in spectrans/core/registry.py
Functions¶
register_component ¶
register_component(category: ComponentType, name: str, metadata: dict[str, Any] | None = None) -> Callable[[ComponentClass], ComponentClass]
Decorator for registering components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category to register the component under. |
required |
name
|
str
|
Name to register the component as. |
required |
metadata
|
dict[str, Any] | None
|
Optional metadata about the component. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[ComponentClass], ComponentClass]
|
Decorator function. |
Examples:
Source code in spectrans/core/registry.py
get_component ¶
Get a registered component class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
name
|
str
|
Name of the component. |
required |
Returns:
| Type | Description |
|---|---|
ComponentClass
|
The registered component class. |
Source code in spectrans/core/registry.py
create_component ¶
Create an instance of a registered component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType
|
Category of the component. |
required |
name
|
str
|
Name of the component. |
required |
**kwargs
|
Any
|
Keyword arguments for the component constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Instance of the component. |
Source code in spectrans/core/registry.py
list_components ¶
List available components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
ComponentType | None
|
Category to list. If None, lists all categories. |
None
|
Returns:
| Type | Description |
|---|---|
list[str] | dict[str, list[str]]
|
Component names or dict of categories to names. |