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_version with migrations;

  • kind-discriminated steps;

  • $source file 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 .json file.

  • base_dir – Directory for relative $source paths. 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. field names 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_version and each model’s kind, 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 $source paths 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.

steps accepts step objects or plain dicts with a kind. 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

schema_version

str

'0.1.0'

Version of the SignalSpec schema, X.Y.Z.

name

str

required

Name of the pipeline.

description

str | None

None

What the pipeline is for.

inputs

list[FrameInput | ParquetInput | CsvInput]

()

Named input slots. With none, the pipeline takes a single frame of data.

steps

list[StepSpec]

()

The steps, applied in order.

outputs

list[ParquetOutput | CsvOutput]

()

Where flexsignal.api.run writes the result.

metadata

dict[str, Any]

{}

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 implements transform(), and fit() 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

value

float

required

The magnitude.

units

str

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

kind

'frame'

'frame'

name

str

required

Slot name; the caller binds data to it, e.g. historian.

description

str | None

None

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

kind

'parquet'

'parquet'

name

str

required

Slot name; data bound to this name replaces the file.

path

str

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

kind

'csv'

'csv'

name

str

required

Slot name; data bound to this name replaces the file.

path

str

required

Path to the file, relative to the spec file.

time_column

str

'timestamp'

Column holding the timestamps.

timezone

str | None

None

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

dict[str, str]

{}

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

kind

'parquet'

'parquet'

path

str

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

kind

'csv'

'csv'

path

str

required

Output path, relative to the spec file.

include_flags

bool

False

Also write one <stream>__flags column per stream.

Generating the JSON Schema

flexsignal.spec.schema.build_json_schema() → dict[str, Any]

Return the JSON Schema of a SignalSpec, with every registered step kind.

steps becomes a union discriminated by kind over the registered step classes, so editors and UIs can validate and autocomplete each step’s options.

flexsignal.spec.schema.export_json_schema(path: str | Path = PosixPath('/home/runner/work/flex-signal/flex-signal/src/flexsignal/spec/schemas/signal_spec.schema.json')) → Path

Write the JSON Schema to path (default: the committed location).

Returns:

The path written.