Specs¶
The SignalSpec: a declarative, versioned JSON description of a pipeline.
This package mirrors flex-pse’s flexcore.config conventions:
strict pydantic models;
a
schema_versionwith migrations;kind-discriminated steps;$sourcefile references;a committed JSON Schema.
The committed JSON Schema:
signal_spec.schema.json.
See The pipeline spec.
Loading, dumping and hashing¶
- flexsignal.spec.load_spec(source: SignalSpec | Mapping[str, Any] | str | Path, *, base_dir: str | Path | None = None) SignalSpec¶
Read and validate a SignalSpec.
- Parameters:
source – A
SignalSpec(returned as is), a dict, a JSON string, or the path of a.jsonfile.base_dir – Directory for relative
$sourcepaths. Defaults to the spec file’s directory, or the working directory for a dict or string.
- Returns:
The validated spec, migrated to the current schema version.
- Raises:
SignalConfigError – If the spec is invalid.
fieldnames the offending field, e.g.steps.2.window.
- flexsignal.spec.dump_spec(spec: SignalSpec, path: str | Path | None = None) dict[str, Any]¶
Return a spec as a canonical JSON-able dict, optionally writing it to a file.
Fields left at their defaults are omitted, except
schema_versionand each model’skind, which are always written.- Parameters:
spec – The spec.
path – If given, also write the spec there as indented JSON.
- Returns:
The spec as a dict.
- flexsignal.spec.spec_hash(spec: SignalSpec) str¶
Return the sha256 hex digest of the spec’s canonical JSON.
Two specs that describe the same pipeline have the same hash, whatever their key order, and whether defaults were written out or left implicit.
- flexsignal.spec.canonical_json(spec: SignalSpec) str¶
Return the spec as compact, key-sorted JSON: the input to
spec_hash().
- flexsignal.spec.migrate(data: dict[str, Any]) dict[str, Any]¶
Bring a spec dict up to
SCHEMA_VERSION.- Raises:
SignalConfigError – If the version is missing, newer than this release, or has no migration path.
- flexsignal.spec.resolve_sources(value: Any, base_dir: Path) Any¶
Replace every
{"$source": "file.json[.gz]"}with that file’s contents.- Parameters:
value – A JSON-like structure.
base_dir – Directory that relative
$sourcepaths resolve against.
- Returns:
The structure with every reference replaced. Referenced files may contain references of their own, which resolve relative to that file.
- flexsignal.spec.models.SCHEMA_VERSION = '0.1.0'¶
Version of the SignalSpec schema this release reads and writes.
- flexsignal.spec.io.MIGRATIONS: dict[str, Callable[[dict[str, Any]], dict[str, Any]]]¶
Schema migrations, keyed by the version they upgrade from.
Each entry is
from_version -> fn(spec_dict) -> spec_dict, returning the dict at the next version. Add one whenever a released schema version changes incompatibly.
The spec models¶
- class flexsignal.spec.SignalSpec¶
A declarative, versioned description of a pipeline.
stepsaccepts step objects or plain dicts with akind. Dicts are resolved through the step registry.flexsignal.spec.load_spec()is the preferred way to read a spec from JSON, because its errors point at the exact field.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
Version of the SignalSpec schema, X.Y.Z. |
|
|
required |
Name of the pipeline. |
|
|
|
What the pipeline is for. |
|
|
|
Named input slots. With none, the pipeline takes a single frame of data. |
|
|
|
The steps, applied in order. |
|
|
|
Where flexsignal.api.run writes the result. |
|
|
|
Free-form metadata, kept with the spec. |
- class flexsignal.spec.StepSpec¶
Base class of every pipeline step.
A subclass declares
kind: Literal["my_kind"] = "my_kind"and its options as fields, then implementstransform(), andfit()if it learns anything from training data.- fit(sf: SignalFrame) dict[str, Any]¶
Learn whatever the step needs from training data.
- Parameters:
sf – The training data, as it arrives at this step.
- Returns:
The fitted state as a JSON-able dict. Stateless steps return
{}.
- transform(sf: SignalFrame, state: Mapping[str, Any]) SignalFrame¶
Apply the step.
- Parameters:
sf – The data as it arrives at this step.
state – What
fit()returned.
- Returns:
A new SignalFrame.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
- class flexsignal.spec.Quantity¶
A number with units, e.g.
{"value": 5, "units": "m^3/hr"}.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
required |
The magnitude. |
|
|
required |
Units as a pyomo/pint-parseable string, e.g. m^3/hr. |
Inputs¶
- class flexsignal.spec.FrameInput¶
A named input slot that the caller fills with data at run time.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
required |
Slot name; the caller binds data to it, e.g. historian. |
|
|
|
What data belongs in this slot. |
- class flexsignal.spec.ParquetInput¶
A Parquet file, read when the caller doesn’t bind data to this slot.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
required |
Slot name; data bound to this name replaces the file. |
|
|
required |
Path to the file, relative to the spec file. |
- class flexsignal.spec.CsvInput¶
A CSV file, read when the caller doesn’t bind data to this slot.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
required |
Slot name; data bound to this name replaces the file. |
|
|
required |
Path to the file, relative to the spec file. |
|
|
|
Column holding the timestamps. |
|
|
|
Time zone of timestamps written without an offset, e.g. UTC or America/Los_Angeles. If omitted, they are taken as the local time of the machine running the spec, with a warning; set it so the spec gives the same result everywhere. |
|
|
|
Units per stream, e.g. {flow: m^3/hr}. |
Outputs¶
- class flexsignal.spec.ParquetOutput¶
Write the result to a Parquet file, keeping flags, units and provenance.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
required |
Output path, relative to the spec file. |
- class flexsignal.spec.CsvOutput¶
Write the result’s values (and optionally flags) to a CSV file.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
required |
Output path, relative to the spec file. |
|
|
|
Also write one <stream>__flags column per stream. |