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.

Structural summaries: Sequential.summary and Graph.summary

Open In Colab

This notebook walks through the shape inference protocol in xrtoolz — the same idea Keras uses for Model.summary(), but for xrtoolz pipelines.

Three pieces:

  1. Signature (pipekit’s) — a tiny value object: ordered (dim_name, size | None) pairs plus a dtype. It carries what shape an op expects/produces without holding any data.

  2. Operator.compute_output_signature(input_signature) — every operator can declare how it transforms shapes. The base default is shape-preserving; ops that rename, subset, reduce, regrid, etc. override it.

  3. Sequential.summary(signature) and Graph.summary(...) — render an end-to-end table of input/output signatures per step, no real data needed.

Below: the basics, four worked pipelines on different tasks, multi-input metric graphs, nested Graph-in-Graph composition, and a couple of common error/edge cases.

Setup

Colab-friendly install + imports.

1. Signature basics

A Signature captures dim sizes and a dtype. None means “size not known symbolically” — you’ll see this on operators where the output size depends on the data (irregular bbox crops, time-resamples, climatology grouping).

(time=365, lat=181, lon=360) float32
(time=?, lat=4) ?

Signature is a value object: it’s frozen, and its dims are ordered (name, size) pairs, the carrier’s axis order. So equality compares dims in order, plus the dtype label.

True False
(('time', 12), ('lat', 4))

Helpers replace_dims and drop_dims return new signatures (no in-place mutation), and matches tests the dims against a pattern:

(time=365, lat=?, lon=360) float32
(time=365) float32
True False

2. Sequential pipeline — SSH preprocessing

The canonical “open a CMEMS / GLORYS Dataset → validate → crop to a region → resample time → spatially reduce” preprocessing chain. Each summary row shows what shape the step receives and what it emits.

Operator            Input sig                                        Output sig                                     
--------------------------------------------------------------------------------------------------------------------
[0] ValidateCoords  Signature((time=365, lat=181, lon=360) float32)  Signature((time=365, lat=181, lon=360) float32)
[1] SubsetBBox      Signature((time=365, lat=181, lon=360) float32)  Signature((time=365, lat=?, lon=?) float32)    
[2] ResampleTime    Signature((time=365, lat=?, lon=?) float32)      Signature((time=?, lat=?, lon=?) float32)      
[3] ReduceDims      Signature((time=?, lat=?, lon=?) float32)        Signature((time=?) float32)                    

Reading the table:

  • ValidateCoords is shape-preserving — no override needed; the default kicks in.

  • SubsetBBox knows it touches lat/lon but can’t tell how many points land inside the bbox without the actual coords; both axes go to ?.

  • ResampleTime similarly fuzzes time.

  • ReduceDims(dim=("lat","lon")) drops the spatial axes; only time remains.

3. Sequential pipeline — climatology + anomaly setup

A different shape-changing pattern: CalculateClimatology swaps the time axis for a month (or dayofyear/year) axis whose length depends on the group-by — so it’s ? symbolically.

Operator                  Input sig                                         Output sig                                      
----------------------------------------------------------------------------------------------------------------------------
[0] ValidateCoords        Signature((time=4017, lat=181, lon=360) float32)  Signature((time=4017, lat=181, lon=360) float32)
[1] SubsetTime            Signature((time=4017, lat=181, lon=360) float32)  Signature((time=?, lat=181, lon=360) float32)   
[2] CalculateClimatology  Signature((time=?, lat=181, lon=360) float32)     Signature((month=?, lat=181, lon=360) float32)  

4. Sequential pipeline — coordinate reshape + regrid

This one mixes a CF rename, a coarsen, and a regrid onto another grid. RegridLike knows the target’s exact size, so the output is fully concrete.

Operator          Input sig                                                   Output sig                                     
-----------------------------------------------------------------------------------------------------------------------------
[0] RenameCoords  Signature((time=12, latitude=720, longitude=1440) float64)  Signature((time=12, lat=720, lon=1440) float64)
[1] Coarsen       Signature((time=12, lat=720, lon=1440) float64)             Signature((time=12, lat=360, lon=720) float64) 
[2] RegridLike    Signature((time=12, lat=360, lon=720) float64)              Signature((time=12, lat=91, lon=180) float64)  

Notice the rename flows through cleanly — the table tracks dim names, not just sizes.

5. Graph.summary — multi-input metric

Sequential is for linear chains. The functional Graph API handles DAGs, including ops with multiple inputs (the canonical case: pixel metrics like RMSE that take pred and ref). The shape protocol propagates through:

Node                 Input sig                                                                   Output sig
-----------------------------------------------------------------------------------------------------------
[0] RMSE(pred, ref)  (time=365, lat=181, lon=360) float32, (time=365, lat=181, lon=360) float32  (lon=360) float64
→ score = [0]                                                                                    (lon=360) float64

output_signatures returns each output’s signature — what running the graph on real data would give, minus the data:

{'score': Signature((lon=360) float64)}

What happens with mismatched shapes?

A node with several inputs gets its operator’s signature only when the inputs agree; otherwise the graph reports it as unknown (?) rather than guessing. The pixel metric itself validates dim names and sizes, so asking it directly says what disagrees — before any xr.apply_ufunc broadcast error at runtime.

Node                 Input sig                                                                  Output sig
----------------------------------------------------------------------------------------------------------
[0] RMSE(pred, ref)  (time=365, lat=181, lon=360) float32, (time=90, lat=181, lon=360) float32  ?
→ score = [0]                                                                                   ?
caught: Metric input signature sizes do not match: {'time': (365, 90)}.

6. Single-input Graph (the convenience form)

Graphs with one input/output accept a bare Signature (you don’t have to wrap it in a dict). Same UX as Sequential.summary:

Node                   Input sig                             Output sig
-----------------------------------------------------------------------
[0] SubsetBBox(ds)     (time=365, lat=181, lon=360) float32  (time=365, lat=?, lon=?) float32
[1] ResampleTime([0])  (time=365, lat=?, lon=?) float32      (time=?, lat=?, lon=?) float32
→ out = [1]                                                  (time=?, lat=?, lon=?) float32

7. Nested Graph-in-Graph composition

Because Graph is itself an Operator, you can drop a fully-built Graph into a larger Graph. A graph returns a dict of outputs rather than one carrier, so to the outer graph the inner graph’s output shape is unknown (?), and so are the steps after it. Summarise the inner graph on its own for its shapes:

Node                   Input sig                              Output sig
------------------------------------------------------------------------
[0] Graph(ds)          (time=3650, lat=181, lon=360) float32  ?
[1] ResampleTime([0])  ?                                      ?
[2] ReduceDims([1])    ?                                      ?
→ global_mean = [2]                                           ?

Node                   Input sig                              Output sig
------------------------------------------------------------------------
[0] SubsetTime(ds_in)  (time=3650, lat=181, lon=360) float32  (time=?, lat=181, lon=360) float32
[1] SubsetBBox([0])    (time=?, lat=181, lon=360) float32     (time=?, lat=?, lon=?) float32
→ cropped = [1]                                               (time=?, lat=?, lon=?) float32

8. Common mistakes the summary catches early

  • Coarsen with boundary="exact" and a non-divisible factor. summary refuses to render a misleading shape and points at the offending dim:

caught: coarsen boundary='exact' requires 'lat' size 10 to be divisible by factor 7.
  • Coarsen with an unknown boundary string. Constructor-time validation catches the typo before you ever call summary:

caught: Coarsen boundary must be one of ('exact', 'trim', 'pad'), got 'trip'.

9. When to reach for which

  • Sequential.describe() — structural tree only (operator names + configs). Doesn’t need a Signature. Use when you want to show the plan of a pipeline.

  • Sequential.summary(signature) — adds per-step input/output shape. Use when shape questions matter: “how many lat points after the regrid?”, “where did the time axis go?”.

  • Graph.summary(...) — same idea for DAGs. Required when an operator has multiple inputs (metrics) or multiple outputs (multi-task pipelines).

All three are eager-free: no real data is touched, just signatures threaded through compute_output_signature overrides.