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_dict writes a config as a plain dict, each declared field replaced by its component’s own get_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 in config_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

Component

Root of every configurable object: serialised and rebuilt by declaration.

Functions

child_components

Yield the components held directly in root's declared fields, with their paths.

component

Declare a config dataclass field as holding a component, or several.

component_fields

Return the declared component fields of a config dataclass.

config_cls_of

Return the config class cls declares in config_cls.

config_to_dict

Return a config dataclass as a plain dict, components as their own configs.

get_cls_from_path

Import path ("pkg.module.ClassName") and return the class.

rebuild

Rebuild the component a saved config names, and everything nested in it.

recorded_configs

Write each component configs names, by id, as the config recorded for it.

walk_components

Yield every component under root with its path, depth first, root excluded.