Pipelines and entry points

Running a spec

These are the entry points flex-orchestrator will wrap (see PLAN.md §4.3): a spec goes in and a JSON-able result comes out, with no file I/O when data is passed in.

flexsignal.run(spec: SignalSpec | Mapping[str, Any] | str | Path, data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None, write_outputs: bool = True, progress: Callable[[float, str], None] | None = None) → RunResult

Fit a pipeline on data and return the transformed result.

Parameters:
  • spec – A SignalSpec, a spec dict, JSON text, or the path of a .json spec.

  • data – One frame, or {slot_name: frame}. File inputs not bound here are read from disk.

  • base_dir – Directory for relative paths (default: the spec file’s directory, or the working directory).

  • write_outputs – Write the spec’s outputs. Pass False to keep the run free of file I/O.

  • progress – Called as progress(fraction, message) after each step.

Returns:

A RunResult.

flexsignal.fit(spec: SignalSpec | Mapping[str, Any] | str | Path, data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None, progress: Callable[[float, str], None] | None = None) → FittedPipeline

Fit a pipeline from a spec.

Parameters:
  • spec – A SignalSpec, a spec dict, JSON text, or the path of a .json spec.

  • data – Training data: one frame, or {slot_name: frame}. File inputs not bound here are read from disk.

  • base_dir – Directory for relative paths (default: the spec file’s directory, or the working directory).

  • progress – Called as progress(fraction, message) after each step.

Returns:

The fitted pipeline.

class flexsignal.RunResult

What run() returns.

signal: SignalFrame

The result as a SignalFrame (values, flags, metadata, history).

fitted: FittedPipeline

The pipeline fitted on the run’s data. Save it to reuse it on new data.

outputs: tuple[str, ...] = ()

Paths written for the spec’s outputs.

property data: DataFrame

The values, as a DataFrame with a UTC index and one column per stream.

property flags: DataFrame

The quality flags, same shape as data.

property units: dict[str, str | None]

Units per stream, as passed to orch.store_data(name, df, units=).

property spec_hash: str

sha256 of the spec that produced this result.

property provenance: list[dict[str, Any]]

One JSON-able record per step applied.

property report: dict[str, Any]

A JSON-able summary of the spec hash, fit report, flag counts and history.

to_dict() → dict[str, Any]

Return the JSON-able parts of the result (everything except the data).

__init__(signal: SignalFrame, fitted: FittedPipeline, outputs: tuple[str, ...] = ()) → None

Pipelines

class flexsignal.Pipeline

An unfitted pipeline: a spec you can fit.

Parameters:
  • steps – The steps, in order.

  • name – Pipeline name.

  • inputs – Input slots (see bind_inputs()).

  • outputs – Where flexsignal.run() writes results.

  • description – What the pipeline is for.

  • metadata – Free-form metadata kept with the spec.

Examples

>>> import flexsignal as fs
>>> pipe = fs.Pipeline([fs.clean.Range("flow", min=0)], name="demo")
>>> pipe.spec.steps[0].kind
'range'
__init__(steps: Sequence[StepSpec | Mapping[str, Any]] = (), *, name: str = 'pipeline', inputs: Sequence[FrameInput | ParquetInput | CsvInput | Mapping[str, Any]] = (), outputs: Sequence[ParquetOutput | CsvOutput | Mapping[str, Any]] = (), description: str | None = None, metadata: Mapping[str, Any] | None = None)

Build the spec (see the class docstring).

classmethod from_spec(spec: SignalSpec | Mapping[str, Any] | str | Path) → Pipeline

Build a pipeline from a spec, a spec dict, JSON text, or a .json path.

fit(data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None, progress: Callable[[float, str], None] | None = None) → FittedPipeline

Fit every step, in order, on training data.

Parameters:
  • data – Training data (see bind_inputs()).

  • base_dir – Directory for relative input file paths.

  • progress – Called as progress(fraction, message) after each step.

Returns:

The fitted pipeline.

fit_transform(data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None, progress: Callable[[float, str], None] | None = None) → tuple[FittedPipeline, SignalFrame]

Fit every step; return the fitted pipeline and transformed training data.

classmethod __new__(*args, **kwargs)
class flexsignal.FittedPipeline

A pipeline whose steps have been fitted: the spec plus each step’s state.

Parameters:
  • spec – The spec it was fitted from.

  • states – One JSON-able state per step, in order.

  • fit_report – What happened during fitting (from Pipeline.fit()).

__init__(spec: SignalSpec, states: Sequence[Mapping[str, Any]], fit_report: Mapping[str, Any] | None = None)

Store the parts (see the class docstring).

transform(data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None, progress: Callable[[float, str], None] | None = None) → SignalFrame

Apply the fitted steps to new data.

Parameters:
  • data – The data (see bind_inputs()).

  • base_dir – Directory for relative input file paths.

  • progress – Called as progress(fraction, message) after each step.

Returns:

The transformed SignalFrame, with one history record per step.

classmethod __new__(*args, **kwargs)
report(result: SignalFrame | None = None) → dict[str, Any]

Summarize the pipeline and, optionally, one result, as a JSON-able dict.

Parameters:

result – A frame this pipeline produced. Its flag counts and per-step history are included.

to_payload() → dict[str, Any]

Return the fitted pipeline as a storage-agnostic payload.

Returns:

{"format", "format_version", "flexsignal_version", "spec", "spec_hash", "state": {"steps": [...]}, "fit_report", "blobs"}. Everything except blobs is JSON-able. blobs maps a name to bytes, for steps whose state can’t be JSON (none in M1).

classmethod from_payload(payload: Mapping[str, Any]) → FittedPipeline

Rebuild a fitted pipeline from to_payload() output.

Raises:

SignalConfigError – If the payload isn’t a flex-signal fitted pipeline, or its spec doesn’t match its recorded hash.

save(directory: str | Path, *, overwrite: bool = False) → Path

Save as a directory of JSON files.

The layout is spec.json, fitted/manifest.json, one fitted/NN_<kind>.json state per step, and fitted/blobs/ for binary state.

Parameters:
  • directory – Where to save. Created if needed.

  • overwrite – Replace an existing saved pipeline in that directory.

Returns:

The directory.

Raises:

SignalConfigError – If a pipeline is already saved there and overwrite is False.

classmethod load(directory: str | Path) → FittedPipeline

Load a pipeline saved with save().

flexsignal.pipeline.bind_inputs(spec: SignalSpec, data: SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]] | Mapping[str, SignalFrame | DataFrame | tuple[DataFrame, Mapping[str, str]]] | None = None, *, base_dir: str | Path | None = None) → SignalFrame

Turn the caller’s data and the spec’s inputs into one SignalFrame.

With no inputs in the spec, data must be a single frame. Otherwise each input slot takes the data bound to its name. File inputs (parquet, csv) are read only when no data is bound to them. Several inputs are joined on time (an outer join). The rows the join adds to a stream are flagged MISSING and NO_SAMPLE; put an align step first to bring them onto one grid.

Parameters:
  • spec – The spec.

  • data – One frame, or {slot_name: frame}.

  • base_dir – Directory that relative file paths resolve against (default: the working directory).

Returns:

The joined SignalFrame.

Raises:

SignalConfigError – If data is missing for a slot, or given for an unknown slot.

flexsignal.pipeline.step_record(index: int, step: StepSpec, before: SignalFrame, after: SignalFrame, digest: str) → dict[str, Any]

Describe what one step did, by comparing its input and output.

Returns:

A JSON-able dict with the step’s index, kind and name, the spec hash, row counts, streams added and removed, and (when the time index is unchanged) how many samples changed per stream.