Skip to content

Labels

Labels connect anomaly annotations to the parsed dataset.

This page covers the built-in label readers and lookup helpers used to expose line-level or group-level anomaly labels during dataset building and sequence construction.

>>> from pathlib import Path
>>> from anomalog.labels import CSVReader
>>> reader = CSVReader(relative_path=Path("labels.csv"))
>>> reader.entity_column, reader.label_column
('entity_id', 'anomalous')
>>> reader.with_context(dataset_root=Path("."), sink=None).dataset_root
PosixPath('.')

anomalog.labels

Helpers for loading anomaly labels from different sources.

AnomalyLabelLookup dataclass

Lookup accessors for anomaly labels.

AnomalyLabelReader

Bases: Protocol

Loads anomaly label lookups.

load()

Return callables that map line or group identifiers to labels.

with_context(*, dataset_root, sink)

Bind dataset context and return a configured reader instance.

CSVReader dataclass

Bases: AnomalyLabelReader

Reads anomaly labels from a CSV file (group/entity level only).

Column names are configurable: entity_column, label_column.

load()

Load labels from the configured CSV file into lookup callables.

Returns:

Name Type Description
AnomalyLabelLookup AnomalyLabelLookup

Lookup functions backed by the configured CSV.

Raises:

Type Description
ValueError

If dataset context is missing or the CSV schema is invalid.

with_context(*, dataset_root, sink)

Attach dataset context when missing and return a new reader.

Parameters:

Name Type Description Default
dataset_root Path

Dataset root used to resolve the CSV path.

required
sink StructuredSink

Structured sink for the dataset. Unused for CSV-backed labels.

required

Returns:

Name Type Description
CSVReader CSVReader

Reader bound to the supplied dataset root.

InlineReader dataclass

Bases: AnomalyLabelReader

Derives labels directly from the structured sink.

load()

Collect inline labels from the sink and return lookup callables.

Returns:

Name Type Description
AnomalyLabelLookup AnomalyLabelLookup

Lookup functions backed by the structured sink.

Raises:

Type Description
ValueError

If no structured sink has been attached.

RuntimeError

If the sink fails while loading inline labels.

with_context(*, dataset_root, sink)

Attach sink context when missing and return a new reader.

Parameters:

Name Type Description Default
dataset_root Path

Dataset root for the current build. Unused for inline labels.

required
sink StructuredSink

Structured sink that provides inline labels.

required

Returns:

Name Type Description
InlineReader InlineReader

Reader bound to the supplied structured sink.