Skip to content

Core Type Definitions

spectrans.core.types

Type definitions and aliases for the spectrans library.

This module provides type definitions, aliases, and literal types used throughout the spectrans library. The type system ensures safety and provides clear documentation of expected parameter types for all spectral transformer components.

The types are organized into logical groups covering tensors, shapes, transforms, neural network components, training configurations, and mathematical operations. All definitions use modern Python 3.13 syntax with union operators and the new statement for aliases.

Attributes:

Name Type Description
Tensor type

Alias for torch.Tensor, the primary data type.

ComplexTensor type

Alias for complex-valued torch.Tensor.

BatchSize, SequenceLength, HiddenDim int

Common tensor dimension types.

Classes:

Name Description
TransformType : Literal

Valid spectral transform names ('fourier', 'cosine', 'hadamard', 'wavelet').

WaveletType : Literal

Valid wavelet family names (Daubechies, Symlets, Coiflets, Biorthogonal).

ActivationType : Literal

Valid activation function names ('relu', 'gelu', 'swish', etc.).

NormType : Literal

Valid normalization layer types ('layernorm', 'batchnorm', etc.).

ModelType : Literal

Valid model architecture names ('fnet', 'gfnet', 'afno', etc.).

ComponentType : Literal

Component categories for the registry system.

Examples:

Using annotations in function signatures:

>>> from spectrans.core.types import Tensor, TransformType
>>> def apply_transform(x: Tensor, transform: TransformType) -> Tensor:
...     # Implementation with proper hints
...     pass

Working with configuration dictionaries:

>>> from spectrans.core.types import ConfigDict, ModelType
>>> config: ConfigDict = {
...     'model_type': 'fnet',
...     'hidden_dim': 768,
...     'num_layers': 12
... }

Complex tensor operations:

>>> from spectrans.core.types import ComplexTensor
>>> def process_spectral_component(x: ComplexTensor) -> ComplexTensor:
...     return x  # Process and return complex tensor
Notes

Type System Philosophy:

  1. Explicit over Implicit: All function signatures should include hints
  2. Literal Types: Use Literal for enumerated options to prevent typos
  3. Type Aliases: Semantic names (BatchSize) over raw types (int)
  4. Union Syntax: Modern | syntax instead of Union[] for Python 3.13
  5. Optional Types: T, None pattern instead of Optional[T]

Mathematical Type Categories:

  • Tensor Types: Basic tensor operations and complex number support
  • Shape Types: Common tensor dimension patterns (2D, 3D, 4D)
  • Transform Types: Spectral transform varieties and parameters
  • Configuration Types: Structured configuration and parameter passing

The system supports both runtime checking (where appropriate) and static analysis with mypy, providing comprehensive safety for the library.

See Also

spectrans.core.base : Base classes that use these definitions spectrans.utils.complex : Complex tensor operations with proper typing

Classes