quantlab.core.component#
The component rule: how a configurable object is serialised and rebuilt.
A component is an object built from one config dataclass: a dataset, a
factor, a label. Some config fields hold other components (a factor’s
dataset, a label’s factor, a merged dataset’s inputs). Those fields are
declared on the config dataclass with component(), next to the fields
themselves, and that one declaration drives both directions:
config_to_dictwrites a config as a plain dict, each declared field replaced by its component’s ownget_config(), whose"name"is the component class’s import path;Component.from_config(d, run_dir=None)rebuilds the declared fields first, recursing only along them, then constructs the object from the config class its class declares inconfig_cls.run_dir, the run directory the dict was read from, reaches every level.
Nothing is recognised by key name or by shape: a free-form dict field (a
factor’s kwargs) is never entered, even when it holds a "name" key.
The same declarations give the component tree: walk_components yields
every component under a root with its path of field names, and inside
recorded_configs a component found in a declared field is written as the
config recorded for it instead of its own get_config() (a run directory
records an in-memory dataset reading its copy under the run). A component
held at several places of a tree is written at each place with a
"shared_as" key naming its first path, and one rebuild call builds
every config carrying the same mark into one object, so a rebuilt tree
shares what the saved one shared. Which stores a tree uses is declared on
each component (Component.store_uses) and gathered by
quantlab.core.store_uses.walk_store_uses.
Only a class that declares a config class is ever rebuilt, so a saved dict
cannot instantiate an arbitrary importable callable.
Examples#
With momentum a Momentum factor over a SpotKlineDataset:
>>> from quantlab.label.config import ForwardConfig
>>> from quantlab.label.forward import Forward
>>> label = Forward(ForwardConfig(factor=momentum, span=5))
>>> saved = label.get_config()
>>> saved["factor"]["name"], saved["factor"]["dataset"]["name"]
('quantlab.factor.predefined.momentum.Momentum', 'quantlab.dataset.spot.SpotKlineDataset')
>>> rebuild(saved) == label
True
Classes
Root of every configurable object: serialised and rebuilt by declaration. |
Functions
Yield the components held directly in |
|
Declare a config dataclass field as holding a component, or several. |
|
Return the declared component fields of a config dataclass. |
|
Return the config class |
|
Return a config dataclass as a plain dict, components as their own configs. |
|
Import |
|
Rebuild the component a saved config names, and everything nested in it. |
|
Write each component |
|
Yield every component under |