Configuration Builder¶
spectrans.config.builder ¶
Type-safe configuration builder for spectrans components.
This module provides the ConfigBuilder class for loading YAML configurations and constructing spectrans models and components with full type safety and validation using Pydantic configuration models.
Classes:
| Name | Description |
|---|---|
ConfigBuilder |
Main builder class for creating models and components from configuration. |
Functions:
| Name | Description |
|---|---|
load_yaml_config |
Load and validate YAML configuration file. |
build_model_from_config |
Build a complete model from configuration dictionary. |
build_component_from_config |
Build a specific component from configuration. |
Examples:
Building a model from YAML file:
>>> from spectrans.config.builder import ConfigBuilder
>>> builder = ConfigBuilder()
>>> model = builder.build_model("configs/fnet.yaml")
>>> print(type(model))
<class 'spectrans.models.fnet.FNet'>
Building a model from dictionary:
>>> config = {
... "model_type": "fnet",
... "hidden_dim": 768,
... "num_layers": 12,
... "sequence_length": 512
... }
>>> model = builder.build_model_from_dict(config)
Creating components directly:
>>> layer_config = {
... "type": "fourier_mixing",
... "hidden_dim": 768,
... "dropout": 0.1
... }
>>> layer = builder.build_layer("fourier_mixing", layer_config)
Notes
The ConfigBuilder ensures type safety by:
- Validating all parameters using Pydantic models
- Checking component compatibility before construction
- Providing detailed error messages for invalid configurations
- Supporting both YAML files and Python dictionaries
All configuration validation happens at build time, preventing runtime errors due to invalid parameters or incompatible component combinations.
See Also
spectrans.config.models : Model configuration schemas spectrans.config.layers : Layer configuration schemas
Classes¶
ConfigurationError ¶
Bases: Exception
Exception raised for configuration-related errors.
ConfigBuilder ¶
Type-safe builder for spectrans models and components.
The ConfigBuilder provides methods to load YAML configurations and construct models/components with full type safety and validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict_validation
|
bool
|
Whether to use strict validation mode, defaults to True. |
True
|
Methods:
| Name | Description |
|---|---|
load_yaml |
Load and parse YAML configuration file. |
build_model |
Build a model from a YAML configuration file. |
build_model_from_dict |
Build a model from a configuration dictionary. |
build_layer |
Build a layer from configuration dictionary. |
validate_config |
Validate a configuration dictionary without building components. |
Source code in spectrans/config/builder.py
Functions¶
load_yaml ¶
Load and parse YAML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | Path
|
Path to the YAML configuration file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed configuration dictionary. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the file cannot be loaded or parsed. |
Source code in spectrans/config/builder.py
build_model ¶
Build a model from a YAML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | Path
|
Path to the YAML configuration file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The constructed model instance. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the model cannot be built from the configuration. |
Source code in spectrans/config/builder.py
build_model_from_dict ¶
Build a model from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dict
|
dict[str, Any]
|
Configuration dictionary containing model parameters. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The constructed model instance. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the model cannot be built from the configuration. |
Source code in spectrans/config/builder.py
build_layer ¶
Build a layer from configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_type
|
str
|
Type of layer to build. |
required |
config_dict
|
dict[str, Any]
|
Configuration dictionary containing layer parameters. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The constructed layer instance. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the layer cannot be built from the configuration. |
Source code in spectrans/config/builder.py
validate_config ¶
Validate a configuration dictionary without building components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dict
|
dict[str, Any]
|
Configuration dictionary to validate. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Validated configuration dictionary. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the configuration is invalid. |
Source code in spectrans/config/builder.py
Functions¶
load_yaml_config ¶
Load YAML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | Path
|
Path to the configuration file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed configuration dictionary. |
Source code in spectrans/config/builder.py
build_model_from_config ¶
Build model from configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dict
|
dict[str, Any]
|
Configuration dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The constructed model. |
Source code in spectrans/config/builder.py
build_component_from_config ¶
Build component from configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component_type
|
str
|
Type of component to build. |
required |
config_dict
|
dict[str, Any]
|
Configuration dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The constructed component. |