Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pipekit-ops quickstart

The README screens a month of Sentinel-2 scene records with a filter, a retried and cached read on four threads, and a count per tile, and shows that the whole chain round-trips through JSON with the same content hash. This page picks up from there: the same composition on NumPy arrays with a Graph and an observer, then how the pieces work (the operator model, the wrappers, the execution engine, the dict and list operators, saving and hashing), the zero-dependency guarantee, what pipekit-ops deliberately leaves out, and how to move code written for older versions.

The notation is the README’s. A pipeline p=oK∘…∘o1p = o_K \circ \ldots \circ o_1 is a chain of KK operators, where ok:Xk−1→Xko_k : \mathcal{X}_{k-1} \to \mathcal{X}_k maps one carrier to the next, and h(p)h(p) is its content hash.

Operators on arrays: |, Graph, observers and JSON

We standardise a row of NN reflectance values x∈RN\mathbf{x} \in \mathbb{R}^N by a fixed mean μ\mu and spread σ\sigma, so z=(x−μ)/σ∈RN\mathbf{z} = (\mathbf{x} - \mu) / \sigma \in \mathbb{R}^N, clip z\mathbf{z} to [−3,3][-3, 3] to give z~∈RN\tilde{\mathbf{z}} \in \mathbb{R}^N, and then take the difference d=a−b∈RN\mathbf{d} = \mathbf{a} - \mathbf{b} \in \mathbb{R}^N of two dates a,b∈RN\mathbf{a}, \mathbf{b} \in \mathbb{R}^N, each cleaned the same way. The carrier is a NumPy array this time, so the example also needs NumPy and jaxtyping (uv add numpy jaxtyping); pipekit-ops itself never imports them.

import numpy as np
from jaxtyping import Float
from pipekit_ops import Graph, Input, Operator, Sequential, Tap, dumps, loads


# Shapes: N = 3 samples
Vector = Float[np.ndarray, " N"]


# user
class Standardize(Operator):
    """Centre and scale by a fixed mean and spread."""

    def __init__(self, *, mean: float, std: float) -> None:
        # μ
        self.mean = mean
        # σ
        self.std = std

    def _apply(self, x: Vector) -> Vector:
        # z = (x − μ) / σ
        return (x - self.mean) / self.std


# user
class Clip(Operator):
    """Clip to the interval [lo, hi]."""

    def __init__(self, *, lo: float, hi: float) -> None:
        self.lo = lo
        self.hi = hi

    def _apply(self, z: Vector) -> Vector:
        # z̃ = min(max(z, lo), hi)
        return np.clip(z, self.lo, self.hi)


# user
class Difference(Operator):
    """Subtract the second input from the first."""

    def _apply(self, a: Vector, b: Vector) -> Vector:
        # d = a − b
        return a - b


# user
def show(v: Vector) -> None:
    print("clean:", v)


# 1 · compose with |: a Sequential, itself an Operator

# user
# z = (x − 10) / 2
standardize = Standardize(mean=10.0, std=2.0)
# user
# z̃ = min(max(z, −3), 3)
clip = Clip(lo=-3.0, hi=3.0)
# today (pipekit-ops)
# o_2 ∘ o_1
clean: Sequential = standardize | clip

# (N,)
x: Vector = np.array([4.0, 10.0, 30.0])
# (N,) → (N,)
z: Vector = clean(x)

# 2 · wire a Graph: named inputs, shared operators, named outputs

# user
difference = Difference()
# today (pipekit-ops)
a = Input("a")
# today (pipekit-ops)
b = Input("b")
a_clean = clean(a)
b_clean = clean(b)
anomaly = difference(a_clean, b_clean)
# today (pipekit-ops)
graph = Graph(inputs={"a": a, "b": b}, outputs={"anomaly": anomaly})

# d = clean(a) − clean(b)
# (N,), (N,) → {"anomaly": (N,)}
out: dict[str, Vector] = graph(a=x, b=x + 2.0)

# 3 · observe without changing the flow

# today (pipekit-ops)
traced: Sequential = clean | Tap(show)
# (N,) → (N,), and show prints the clean values
same_values: Vector = traced(x)

# 4 · configuration is data: an operator round-trips through JSON

# today (pipekit-ops)
text: str = dumps(standardize)
# today (pipekit-ops)
same: Operator = loads(text)

print(z, out["anomaly"], same)

This prints clean: [-3. 0. 3.] from the Tap, then [-3. 0. 3.] [-1. -1. 0.] Standardize(mean=10.0, std=2.0). The first is z~\tilde{\mathbf{z}}: (4−10)/2=−3(4 - 10) / 2 = -3, 0, and (30−10)/2=10(30 - 10) / 2 = 10 clipped to 3. The second is d\mathbf{d} for b=x+2\mathbf{b} = \mathbf{x} + 2, whose cleaned values are [−2,1,3][-2, 1, 3]. The third is the standardiser rebuilt from its JSON, {"class": "Standardize", "config": {"mean": 10.0, "std": 2.0}, "module": "__main__"}. Inside the Graph, clean(a) called on an Input does not compute anything: it records a node, and the graph runs the nodes in topological order when it is called on data.

The operator model

A pipekit Operator takes a carrier and returns one through a single _apply method; it composes into a Sequential (a | b | c), a Graph with named inputs and outputs, wrappers such as Retry, Cache and Tap, and stateful steps such as Cycle and TrainingLoop, each itself an Operator
  1. An Operator has one method, _apply(self, carrier) -> carrier. pipekit never inspects the carrier; domain packages narrow it.

  2. Composition is |. a | b | c is a Sequential, flattened, so (a | b) | c and a | (b | c) are the same three steps. Flows that branch and merge are a Graph, built by calling operators on Inputs; it rejects cycles and evaluates in topological order.

  3. Wrappers are operators too. Retry, Timeout, Try, Cache, Map and the QC asserts take an operator and return one, so they nest anywhere.

  4. State is explicit. A StatefulOperator maps (x,s)↦(x′,s′)(x, s) \mapsto (x', s'), a carrier and a state to the next carrier and state, and a Sequential or Graph threads the state through its stateful steps; pipekit-cycle’s Cycle and DACycle and pipekit-train’s TrainingLoop are built on it.

  5. Configuration is data. An operator’s constructor arguments, stored under their own names, are its config; dumps writes it as JSON and loads rebuilds the operator.

  6. Libraries plug in by protocol. Predictor, FittableTransformer and the named-array adapters are runtime-checkable seams, so a library satisfies them without importing pipekit (Protocols).

The long form is Concepts. A single operator also carries backends (the array backends it accepts) and compute_output_signature (how it changes a Signature), so Sequential.summary and Graph.summary can print shapes before anything runs.

Wrappers: control, flow, cache and store, observe, QC

Every wrapper is in the core tour, with a run output for each.

A bigger graph puts these together: an anomaly detector that reads a stack of multi-channel images in parallel with retries and a cache, checks the stack, picks a composite by the number of exposures, scores against a background input and summarises, with four named outputs.

An anomaly detector as one pipekit Graph: sources are read in parallel (ThreadMap around Cache around Retry around ReadTile), stacked and checked, composited by a Branch (median for few exposures, mean for many), turned into a z-score against a background input and a channel ratio, scored, thresholded with a Tap, and summarised by a Fanout; composite, score, mask and summary are its outputs

The wiring and the six domain operators, step by step and runnable, are in the tutorial Build a bigger graph.

Execution: Map, executors, Prefetch, Fold and the presets

Map(op, executor=...) is the one engine for running an operator over many items. executor is "serial", "thread", "process" (spawn by default), "async", or "scope", which borrows the concurrent.futures.Executor that an executor_scope binds (a Dask or Ray one included) and never shuts it down. The executor is not in the config, so the pipeline hashes the same wherever it runs. Input is pulled lazily, at most max_in_flight items are held at once, and on_error is "raise" (fail fast), "collect" (raise a MapError holding the partial results, with a Failed record per failure) or "log_and_continue".

Prefetch(op, depth) reads up to depth items ahead of the consumer, and Fold(op, initial, chunksize) folds chunks in parallel through Map and then combines the partial results. ThreadMap, ProcessMap, AsyncMap and BatchedMap are presets of Map with their own arguments (n_workers, semaphore, batch_size). Process pools need every operator to pickle, so check_pickleable(op) walks a pipeline and returns the operators that hold a closure (they set forbid_in_yaml = True) before you fan out. The details are in the execution design.

Dict and list plumbing

A Graph returns a dict, a Fanout returns a dict, and a STAC search returns a list of dicts. The dict operators (Get, GetIn, Pick, Rename, Merge, Assoc, UpdateIn, ValMap, Dissoc) reshape those without a Lambda, and the list operators (Filter, Remove, GroupBy, CountBy, Reduce, Take, SortBy, …) work on a list of items inside the pipeline. Their predicates and keys are operators too, so a chain built from them still round-trips through dumps and loads.

Saving and hashing

dumps(op) writes op.state as JSON with sorted keys, nested operators included: a child operator appears in its parent’s config as its own record, a Graph records every node and its wiring, and an exception class is written as {"exception": "builtins.OSError"}. loads rebuilds the whole tree, as long as the modules that define the classes are imported. pipeline_hash hashes that record,

h(p)=SHA256(json(sp))h(p) = \mathrm{SHA256}(\mathrm{json}(s_p))

where sps_p is the state of pp and json\mathrm{json} writes it with sorted keys; optional weight bytes are appended after a zero byte. So two pipelines share a hash exactly when they are configured the same, and the pipekit-experiment registries store models under it. Each public class is recorded under the module pipekit_ops, whichever area its file sits in, so moving a class between files inside the package does not change a hash.

For pipelines you did not write (uploaded by users, read from a queue), loads_sandboxed builds only the classes you have registered, at any depth, and checks every class before constructing anything. The operators that hold a function or a debug payload (Lambda, Sink, Tap, ShapeTrace, Branch, Switch, Coalesce, Quarantine and AssertCallable) set forbid_in_yaml: dumps still writes them, but only as a debug record that names the function, loads refuses to rebuild them with a RuntimeError, and pipeline_hash hashes that record, so two different lambdas share a hash. When the pipeline must round-trip, use Log in place of Tap and an operator in place of a plain function wherever the operator takes one (the predicate of Filter, the key of CountBy); the flagged control-flow operators are rebuilt in code. pipekit_ops.testing.assert_round_trip(op, *inputs) checks the contract from your own test suite, and public_operators(module) lists the operators a module exports, so a sweep can fail on one that has no test case yet.

The zero-dependency guarantee

pipekit-ops declares no dependencies, and we keep it that way: import pipekit_ops loads only the standard library, and tests/test_no_dependencies.py checks both (the import in a fresh interpreter). The one exception is pipekit_ops.testing, which imports NumPy lazily when assert_round_trip compares two arrays. Array-aware features find what they need through the carrier instead. AssertShape reads shape, Cache keys an array by its bytes, dtype and shape, and the named-array adapters (a NamedArrayAdapter registered by pipekit-array, gridax, geotoolz-array or another library) give a carrier’s dims to AssertSignature and Signature. on_import runs a callback when a module such as jax is imported, without importing it, which is how pipekit-array’s JAX pytree registrations stay optional.

So you get real value from this package without the rest of pipekit, and a library can depend on it without pulling in NumPy, JAX or anything else. If your science has its own carrier (AnnData, astropy tables, a GeoTensor), you write operators for it and get composition, control flow, caching, parallel maps, QC and a JSON round-trip for free. pipekit-ops and pipekit-array are the two foundations of the workspace, each usable on its own, and every other pipekit package builds on pipekit-ops; the workspace README has the layering.

What is not built

Two things are left out by design.

The open design questions are in the roadmap.

Moving from older versions

From pipekit before 0.2. Until 0.2.0 this package was called pipekit. Change the import; every name and argument is unchanged.

# Today: since 0.2.0 this line raises an ImportError that names the new import
# from pipekit import Operator, Retry, Sequential
# After:
# today (pipekit-ops)
from pipekit_ops import Operator, Retry, Sequential

Libraries should depend on pipekit-ops, not on pipekit: since 0.2.0 pipekit is the umbrella that installs the whole workspace, JAX included. States saved before 0.2.0 (dumps output, registry entries) still load: a module saved as pipekit.<name> resolves to pipekit_ops.<name>. Their pipeline_hash changes, because the hash covers each operator’s module.

From the flat layout before 0.4. Until 0.4.0 the package was a flat list of modules (pipekit_ops.control, pipekit_ops.serial, pipekit_ops.hashing, …); since then it is the seven areas. Those modules are gone, so import pipekit_ops.control raises ModuleNotFoundError: import the name from pipekit_ops or from its area (pipekit_ops.saving for sha256_hex) instead. A state saved under one of the flat modules still loads, because record_moves reads those module names as pipekit_ops; its pipeline_hash changes once, since every class is now recorded under pipekit_ops (decision O17 in Decisions).

Further reading