---
url: /dev/api/sklink.engine.md
description: >-
  API reference of sklink.engine. Evaluation of expressions on a database: the
  only layer that reads data.
---

::: v-pre

# `sklink.engine` {#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`](./sklink#sklink.set_config).

**Classes:**

* [**Backend**](#sklink.engine.Backend): Factory of sessions; registered under a name.
* [**ChildBlock**](#sklink.engine.ChildBlock): Rows reached through a path, grouped by group in CSR layout.
* [**FeatureBlock**](#sklink.engine.FeatureBlock): Values of several expressions, one row per sample.
* [**Population**](#sklink.engine.Population): Samples to evaluate: the entity key of each sample and its optional cutoff.
* [**ResolvedPopulation**](#sklink.engine.ResolvedPopulation): Samples mapped to distinct (entity row, cutoff) groups.
* [**Session**](#sklink.engine.Session): An open evaluation context on one database.
* [**Visibility**](#sklink.engine.Visibility): Which rows a sample may read.

**Functions:**

* [**clear_cache**](#sklink.engine.clear_cache): Drop every entry.
* [**get_backend**](#sklink.engine.get_backend): Return a backend by name; `None` uses the configured one.
* [**list_backends**](#sklink.engine.list_backends): Names of the available backends.

## `sklink.engine.Backend` {#sklink.engine.Backend}

Bases: [Protocol](https://docs.python.org/3/library/typing.html#typing.Protocol)

Factory of sessions; registered under a name.

**Functions:**

* [**open**](#sklink.engine.Backend.open): Open a session on `database`.

**Attributes:**

* [**name**](#sklink.engine.Backend.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)):

### `sklink.engine.Backend.name` {#sklink.engine.Backend.name}

```python
name: str
```

### `sklink.engine.Backend.open` {#sklink.engine.Backend.open}

```python
open(database: Database, *, n_threads: int = 1) -> Session
```

Open a session on `database`.

## `sklink.engine.ChildBlock` {#sklink.engine.ChildBlock}

```python
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()) -> None
```

Rows 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**](#sklink.engine.ChildBlock.categories) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**columns**](#sklink.engine.ChildBlock.columns) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**group**](#sklink.engine.ChildBlock.group) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):
* [**masks**](#sklink.engine.ChildBlock.masks) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**offsets**](#sklink.engine.ChildBlock.offsets) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):
* [**rows**](#sklink.engine.ChildBlock.rows) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):

### `sklink.engine.ChildBlock.categories` {#sklink.engine.ChildBlock.categories}

```python
categories: dict[str, np.ndarray] = field(default_factory=dict)
```

### `sklink.engine.ChildBlock.columns` {#sklink.engine.ChildBlock.columns}

```python
columns: dict[str, np.ndarray]
```

### `sklink.engine.ChildBlock.group` {#sklink.engine.ChildBlock.group}

```python
group: np.ndarray
```

### `sklink.engine.ChildBlock.masks` {#sklink.engine.ChildBlock.masks}

```python
masks: dict[str, np.ndarray]
```

### `sklink.engine.ChildBlock.offsets` {#sklink.engine.ChildBlock.offsets}

```python
offsets: np.ndarray
```

### `sklink.engine.ChildBlock.rows` {#sklink.engine.ChildBlock.rows}

```python
rows: np.ndarray
```

## `sklink.engine.FeatureBlock` {#sklink.engine.FeatureBlock}

```python
FeatureBlock(values: dict[str, np.ndarray], masks: dict[str, np.ndarray], categories: dict[str, np.ndarray] = dict()) -> None
```

Values 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:**

* [**categories**](#sklink.engine.FeatureBlock.categories) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**masks**](#sklink.engine.FeatureBlock.masks) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**values**](#sklink.engine.FeatureBlock.values) ([dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):

### `sklink.engine.FeatureBlock.categories` {#sklink.engine.FeatureBlock.categories}

```python
categories: dict[str, np.ndarray] = field(default_factory=dict)
```

### `sklink.engine.FeatureBlock.masks` {#sklink.engine.FeatureBlock.masks}

```python
masks: dict[str, np.ndarray]
```

### `sklink.engine.FeatureBlock.values` {#sklink.engine.FeatureBlock.values}

```python
values: dict[str, np.ndarray]
```

## `sklink.engine.Population` {#sklink.engine.Population}

```python
Population(keys: Sequence[np.ndarray], missing: np.ndarray, cutoff: np.ndarray | None = None) -> None
```

Samples 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**](#sklink.engine.Population.cutoff) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) | None):
* [**keys**](#sklink.engine.Population.keys) ([Sequence](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)\[[ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)]):
* [**missing**](#sklink.engine.Population.missing) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):
* [**n\_samples**](#sklink.engine.Population.n_samples) ([int](https://docs.python.org/3/library/functions.html#int)): Number of samples.

### `sklink.engine.Population.cutoff` {#sklink.engine.Population.cutoff}

```python
cutoff: np.ndarray | None = None
```

### `sklink.engine.Population.keys` {#sklink.engine.Population.keys}

```python
keys: Sequence[np.ndarray]
```

### `sklink.engine.Population.missing` {#sklink.engine.Population.missing}

```python
missing: np.ndarray
```

### `sklink.engine.Population.n_samples` {#sklink.engine.Population.n_samples}

```python
n_samples: int
```

Number of samples.

## `sklink.engine.ResolvedPopulation` {#sklink.engine.ResolvedPopulation}

```python
ResolvedPopulation(entity: str, sample_to_group: np.ndarray, group_row: np.ndarray, group_cutoff: np.ndarray | None, unknown: np.ndarray, token: str) -> None
```

Samples 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**](#sklink.engine.ResolvedPopulation.entity) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**group_cutoff**](#sklink.engine.ResolvedPopulation.group_cutoff) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) | None):
* [**group_row**](#sklink.engine.ResolvedPopulation.group_row) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):
* [**n\_groups**](#sklink.engine.ResolvedPopulation.n_groups) ([int](https://docs.python.org/3/library/functions.html#int)): Number of distinct groups.
* [**n\_samples**](#sklink.engine.ResolvedPopulation.n_samples) ([int](https://docs.python.org/3/library/functions.html#int)): Number of samples.
* [**sample_to_group**](#sklink.engine.ResolvedPopulation.sample_to_group) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):
* [**token**](#sklink.engine.ResolvedPopulation.token) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**unknown**](#sklink.engine.ResolvedPopulation.unknown) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)):

### `sklink.engine.ResolvedPopulation.entity` {#sklink.engine.ResolvedPopulation.entity}

```python
entity: str
```

### `sklink.engine.ResolvedPopulation.group_cutoff` {#sklink.engine.ResolvedPopulation.group_cutoff}

```python
group_cutoff: np.ndarray | None
```

### `sklink.engine.ResolvedPopulation.group_row` {#sklink.engine.ResolvedPopulation.group_row}

```python
group_row: np.ndarray
```

### `sklink.engine.ResolvedPopulation.n_groups` {#sklink.engine.ResolvedPopulation.n_groups}

```python
n_groups: int
```

Number of distinct groups.

### `sklink.engine.ResolvedPopulation.n_samples` {#sklink.engine.ResolvedPopulation.n_samples}

```python
n_samples: int
```

Number of samples.

### `sklink.engine.ResolvedPopulation.sample_to_group` {#sklink.engine.ResolvedPopulation.sample_to_group}

```python
sample_to_group: np.ndarray
```

### `sklink.engine.ResolvedPopulation.token` {#sklink.engine.ResolvedPopulation.token}

```python
token: str
```

### `sklink.engine.ResolvedPopulation.unknown` {#sklink.engine.ResolvedPopulation.unknown}

```python
unknown: np.ndarray
```

## `sklink.engine.Session` {#sklink.engine.Session}

Bases: [Protocol](https://docs.python.org/3/library/typing.html#typing.Protocol)

An open evaluation context on one database.

**Functions:**

* [**child_block**](#sklink.engine.Session.child_block): Rows reached through `path` for every group, with some columns.
* [**close**](#sklink.engine.Session.close): Release resources.
* [**column_stats**](#sklink.engine.Session.column_stats): Statistics of a column over the rows reachable from the population.
* [**evaluate**](#sklink.engine.Session.evaluate): Evaluate expressions for every sample.
* [**evaluate_groups**](#sklink.engine.Session.evaluate_groups): Evaluate expressions once per group: values, masks and categories.
* [**resolve_population**](#sklink.engine.Session.resolve_population): Map samples to entity rows and distinct groups.

### `sklink.engine.Session.child_block` {#sklink.engine.Session.child_block}

```python
child_block(path: Path, columns: Sequence[str], resolved: ResolvedPopulation, visibility: Visibility, *, lookup: Path | None = None) -> ChildBlock
```

Rows 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` {#sklink.engine.Session.close}

```python
close() -> None
```

Release resources.

### `sklink.engine.Session.column_stats` {#sklink.engine.Session.column_stats}

```python
column_stats(path: Path, column: str, resolved: ResolvedPopulation, visibility: Visibility, *, kind: Literal['top_k', 'values'], k: int | None = None, lookup: Path | None = None) -> np.ndarray
```

Statistics 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` {#sklink.engine.Session.evaluate}

```python
evaluate(expressions: Sequence[Expression], resolved: ResolvedPopulation, visibility: Visibility) -> FeatureBlock
```

Evaluate expressions for every sample.

A condition with a non-empty [`lookup`](./sklink.expressions#sklink.expressions.Condition.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` {#sklink.engine.Session.evaluate_groups}

```python
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` {#sklink.engine.Session.resolve_population}

```python
resolve_population(entity: str, population: Population, visibility: Visibility) -> ResolvedPopulation
```

Map samples to entity rows and distinct groups.

## `sklink.engine.Visibility` {#sklink.engine.Visibility}

```python
Visibility(time: bool = False, allow_reverse_paths: bool = False, include_cutoff: bool = False) -> None
```

Which rows a sample may read.

**Parameters:**

* **time** ([bool](https://docs.python.org/3/library/functions.html#bool)): Temporal mode: rows of tables with a time column are visible only
  before the sample's cutoff. `False` is the static default.
* **allow_reverse_paths** ([bool](https://docs.python.org/3/library/functions.html#bool)): Allow paths that come back down from a shared table to rows of other
  entities.
* **include_cutoff** ([bool](https://docs.python.org/3/library/functions.html#bool)): In temporal mode, also show rows whose time equals the cutoff.

**Attributes:**

* [**allow_reverse_paths**](#sklink.engine.Visibility.allow_reverse_paths) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**include_cutoff**](#sklink.engine.Visibility.include_cutoff) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**time**](#sklink.engine.Visibility.time) ([bool](https://docs.python.org/3/library/functions.html#bool)):

### `sklink.engine.Visibility.allow_reverse_paths` {#sklink.engine.Visibility.allow_reverse_paths}

```python
allow_reverse_paths: bool = False
```

### `sklink.engine.Visibility.include_cutoff` {#sklink.engine.Visibility.include_cutoff}

```python
include_cutoff: bool = False
```

### `sklink.engine.Visibility.time` {#sklink.engine.Visibility.time}

```python
time: bool = False
```

## `sklink.engine.clear_cache` {#sklink.engine.clear_cache}

```python
clear_cache() -> None
```

Drop every entry.

## `sklink.engine.get_backend` {#sklink.engine.get_backend}

```python
get_backend(name: str | None = None) -> Backend
```

Return a backend by name; `None` uses the configured one.

## `sklink.engine.list_backends` {#sklink.engine.list_backends}

```python
list_backends() -> list[str]
```

Names of the available backends.

:::
