sklink.engine
Evaluation of expressions on a database: the only layer that reads data.
This is a semi-public API, stable for backend authors. End users only pick a backend by name with sklink.set_config.
Classes:
- Backend: Factory of sessions; registered under a name.
- ChildBlock: Rows reached through a path, grouped by group in CSR layout.
- FeatureBlock: Values of several expressions, one row per sample.
- Population: Samples to evaluate: the entity key of each sample and its optional cutoff.
- ResolvedPopulation: Samples mapped to distinct (entity row, cutoff) groups.
- Session: An open evaluation context on one database.
- Visibility: Which rows a sample may read.
Functions:
- clear_cache: Drop every entry.
- get_backend: Return a backend by name;
Noneuses the configured one. - list_backends: Names of the available backends.
sklink.engine.Backend
Bases: Protocol
Factory of sessions; registered under a name.
Functions:
- open: Open a session on
database.
Attributes:
sklink.engine.Backend.name
name: strsklink.engine.Backend.open
open(database: Database, *, n_threads: int = 1) -> SessionOpen a session on database.
sklink.engine.ChildBlock
ChildBlock(offsets: np.ndarray, group: np.ndarray, rows: np.ndarray, columns: dict[str, np.ndarray], masks: dict[str, np.ndarray], categories: dict[str, np.ndarray] = dict()) -> NoneRows reached through a path, grouped by group in CSR layout.
offsets[g]:offsets[g + 1] are the rows of group g; group repeats that information per row. Column arrays are aligned with the rows: float64 values (codes for categorical columns, seconds for datetimes) with masks marking missing values.
Attributes:
- categories (
dict[str, ndarray]): - columns (
dict[str, ndarray]): - group (
ndarray): - masks (
dict[str, ndarray]): - offsets (
ndarray): - rows (
ndarray):
sklink.engine.ChildBlock.categories
categories: dict[str, np.ndarray] = field(default_factory=dict)sklink.engine.ChildBlock.columns
columns: dict[str, np.ndarray]sklink.engine.ChildBlock.group
group: np.ndarraysklink.engine.ChildBlock.masks
masks: dict[str, np.ndarray]sklink.engine.ChildBlock.offsets
offsets: np.ndarraysklink.engine.ChildBlock.rows
rows: np.ndarraysklink.engine.FeatureBlock
FeatureBlock(values: dict[str, np.ndarray], masks: dict[str, np.ndarray], categories: dict[str, np.ndarray] = dict()) -> NoneValues of several expressions, one row per sample.
values[name] is float64 with NaN where masks[name] is true. Categorical results are integer codes into categories[name].
Attributes:
sklink.engine.FeatureBlock.categories
categories: dict[str, np.ndarray] = field(default_factory=dict)sklink.engine.FeatureBlock.masks
masks: dict[str, np.ndarray]sklink.engine.FeatureBlock.values
values: dict[str, np.ndarray]sklink.engine.Population
Population(keys: Sequence[np.ndarray], missing: np.ndarray, cutoff: np.ndarray | None = None) -> NoneSamples to evaluate: the entity key of each sample and its optional cutoff.
Parameters:
- keys (
sequence of ndarray): One array per key column, in the order of the entity's primary key. - missing (
ndarray of bool): Samples whose key has a missing component. - cutoff (
ndarray of int64 or None): Cutoff of each sample in nanoseconds since the epoch (UTC).
Attributes:
- cutoff (
ndarray | None): - keys (
Sequence[ndarray]): - missing (
ndarray): - n_samples (
int): Number of samples.
sklink.engine.Population.cutoff
cutoff: np.ndarray | None = Nonesklink.engine.Population.keys
keys: Sequence[np.ndarray]sklink.engine.Population.missing
missing: np.ndarraysklink.engine.Population.n_samples
n_samples: intNumber of samples.
sklink.engine.ResolvedPopulation
ResolvedPopulation(entity: str, sample_to_group: np.ndarray, group_row: np.ndarray, group_cutoff: np.ndarray | None, unknown: np.ndarray, token: str) -> NoneSamples mapped to distinct (entity row, cutoff) groups.
Samples with the same entity and cutoff share a group and are computed once. token identifies the entity and the groups (rows and cutoffs, in group order): backends key cached values by it, so equal tokens must mean equal groups.
Attributes:
- entity (
str): - group_cutoff (
ndarray | None): - group_row (
ndarray): - n_groups (
int): Number of distinct groups. - n_samples (
int): Number of samples. - sample_to_group (
ndarray): - token (
str): - unknown (
ndarray):
sklink.engine.ResolvedPopulation.entity
entity: strsklink.engine.ResolvedPopulation.group_cutoff
group_cutoff: np.ndarray | Nonesklink.engine.ResolvedPopulation.group_row
group_row: np.ndarraysklink.engine.ResolvedPopulation.n_groups
n_groups: intNumber of distinct groups.
sklink.engine.ResolvedPopulation.n_samples
n_samples: intNumber of samples.
sklink.engine.ResolvedPopulation.sample_to_group
sample_to_group: np.ndarraysklink.engine.ResolvedPopulation.token
token: strsklink.engine.ResolvedPopulation.unknown
unknown: np.ndarraysklink.engine.Session
Bases: Protocol
An open evaluation context on one database.
Functions:
- child_block: Rows reached through
pathfor every group, with some columns. - close: Release resources.
- column_stats: Statistics of a column over the rows reachable from the population.
- evaluate: Evaluate expressions for every sample.
- evaluate_groups: Evaluate expressions once per group: values, masks and categories.
- resolve_population: Map samples to entity rows and distinct groups.
sklink.engine.Session.child_block
child_block(path: Path, columns: Sequence[str], resolved: ResolvedPopulation, visibility: Visibility, *, lookup: Path | None = None) -> ChildBlockRows reached through path for every group, with some columns.
With a non-empty lookup (to_one hops from the last table), the columns belong to the row each row reaches through it; rows without a visible such row have missing values. The rows and their order do not depend on lookup.
sklink.engine.Session.close
close() -> NoneRelease resources.
sklink.engine.Session.column_stats
column_stats(path: Path, column: str, resolved: ResolvedPopulation, visibility: Visibility, *, kind: Literal['top_k', 'values'], k: int | None = None, lookup: Path | None = None) -> np.ndarrayStatistics of a column over the rows reachable from the population.
With lookup, of a column of the row each reachable row reaches by those to_one hops, counted once per reachable row.
sklink.engine.Session.evaluate
evaluate(expressions: Sequence[Expression], resolved: ResolvedPopulation, visibility: Visibility) -> FeatureBlockEvaluate expressions for every sample.
A condition with a non-empty lookup reads its column on the row reached by those to_one hops, visible at the group's cutoff, and is a missing value where there is none.
sklink.engine.Session.evaluate_groups
evaluate_groups(expressions: Sequence[Expression], resolved: ResolvedPopulation, visibility: Visibility) -> tuple[dict[str, np.ndarray], dict[str, np.ndarray], dict[str, np.ndarray]]Evaluate expressions once per group: values, masks and categories.
Callers must not write to the returned arrays: a backend may share them with other callers through its cache.
sklink.engine.Session.resolve_population
resolve_population(entity: str, population: Population, visibility: Visibility) -> ResolvedPopulationMap samples to entity rows and distinct groups.
sklink.engine.Visibility
Visibility(time: bool = False, allow_reverse_paths: bool = False, include_cutoff: bool = False) -> NoneWhich rows a sample may read.
Parameters:
- time (
bool): Temporal mode: rows of tables with a time column are visible only before the sample's cutoff.Falseis the static default. - allow_reverse_paths (
bool): Allow paths that come back down from a shared table to rows of other entities. - include_cutoff (
bool): In temporal mode, also show rows whose time equals the cutoff.
Attributes:
sklink.engine.Visibility.allow_reverse_paths
allow_reverse_paths: bool = Falsesklink.engine.Visibility.include_cutoff
include_cutoff: bool = Falsesklink.engine.Visibility.time
time: bool = Falsesklink.engine.clear_cache
clear_cache() -> NoneDrop every entry.
sklink.engine.get_backend
get_backend(name: str | None = None) -> BackendReturn a backend by name; None uses the configured one.
sklink.engine.list_backends
list_backends() -> list[str]Names of the available backends.