Skip to content

Gallery

HTML gallery generation from pipeline results, plus the one-line plot_result entry point for rendering a single concept straight from a PipelineResult.

The gallery is assembled from a GalleryModel of PlotConcept entries built by build_gallery_model; generate_gallery renders it to a self-contained HTML page.

One-line plotting

plot_result

plot_result(
    result: PipelineResult,
    concept: str,
    level: Level | str | None = _UNSET,
    *,
    backend: str = "matplotlib",
    ax: Axes | None = None,
    theme: PlotTheme | None = None,
    **overrides: Any,
) -> Any

Render one plot concept straight from a :class:PipelineResult.

The one-liner the gallery is built on, without the gallery: it derives the same per-face context (spread, focus window, snapshot) via :func:build_gallery_model -- the single source of truth for how each concept binds to its prepare function -- then prepares and renders the face. So plot_result(result, "depth_heatmap") replaces calling prepare.depth_heatmap with the run's tables yourself.

Parameters:

Name Type Description Default
result PipelineResult

Pipeline output (events / trades / depth / depth_summary).

required
concept str

Concept key, e.g. "trade_tape" (see :data:available_concepts). The name of a registered metric works too; a metric is level-less, so level does not apply to it. A metric measures result as it is, not converted with :func:~ob_analytics.display.display_result, and draws what result.metric(concept) returns.

required
level Level or str

"L2" / "L3" (or a :class:Level). Leave it out when result has the concept at one level; it is required when result has the concept at both L2 and L3 (an L3 run has most concepts at both).

_UNSET
backend str

"matplotlib" (default), "plotly", or "bokeh".

'matplotlib'
ax Axes

Axes to draw on, as in :func:~ob_analytics.visualization.plot (Matplotlib only).

None
theme PlotTheme or None

Theme for this call, on any backend; None uses :data:~ob_analytics.visualization.DEFAULT_THEME.

None
**overrides Any

Extra keyword arguments merged over the prepare call (e.g. col_bias=0.1 for the depth heatmap). For a metric, each goes to its compute when that names it and to its prepare otherwise, e.g. bucket_volume=5.0, threshold=0.8 for "vpin".

{}

Returns:

Type Description
A Matplotlib, Plotly or Bokeh figure, per *backend*.

Raises:

Type Description
UnknownNameError

If concept is not built for this result, has no variant at level, is not drawn by backend, or backend is not registered. The message names what is available: for a missing backend, the backends that draw the plot.

ConfigError

If level was left out and result has concept at both L2 and L3, or ax is given for a backend other than Matplotlib.

available_concepts

available_concepts(
    result: PipelineResult,
) -> dict[str, list[str]]

Concept keys built for result, mapped to their available level names.

A discoverability companion to :func:plot_result: shows what plot_result(result, concept, level=...) can render for this dataset (which varies by format -- e.g. hidden_executions is LOBSTER-only). It reads what each face needs from the concept table and checks result for it, so it draws nothing and builds no gallery model.

A registered metric is listed too, with an empty level list: a metric is level-less, so plot_result(result, "amihud") takes no level=.

To convert a result to display units yourself, use display_result.

build_gallery_model(result: PipelineResult) -> GalleryModel

Build the default gallery model from pipeline results.

Each order-book concept comes from the concept table (:mod:ob_analytics.visualization._concepts), with a variant at each level whose data the run has: a price-level (L2) run has no per-order events, so it gets the L2 faces only. Each face the run cannot draw is kept in :attr:~PlotConcept.unavailable, with what the face needs. A concept with both an L2 and an L3 variant is :attr:~PlotConcept.comparable. The analytics are one card per registered metric that applies to the run; callers may append more (built with the *_card helpers) to :attr:GalleryModel.analytics.

Parameters:

Name Type Description Default
result PipelineResult

Pipeline output.

required

Returns:

Type Description
GalleryModel
Notes

What every face is drawn from (the run in display units, its busiest window and price band) is built on the first call and kept on result, so a later call, :func:plot_result and :meth:~ob_analytics.pipeline.PipelineResult.plot reuse it. Each call returns a new model, so adding cards to one does not change the next. The per-order tables come from :meth:~ob_analytics.pipeline.PipelineResult.lifecycles and :meth:~ob_analytics.pipeline.PipelineResult.queue, so a face uses a table the result already holds, and builds one only when it is drawn.

GalleryModel dataclass

GalleryModel(
    concepts: list[PlotConcept],
    analytics: list[PlotSpec] = list(),
)

The gallery's inventory: leveled concepts + level-less analytics.

PlotConcept dataclass

PlotConcept(
    key: str,
    title: str,
    variants: dict[Level, PlotSpec],
    note: str = "",
    unavailable: dict[Level, str] = dict(),
)

A level-neutral plot identity with up to one variant per :class:Level.

The key never carries a level; the level lives in the registry coordinate (concept, level, backend) and in the keys of variants. unavailable holds each face the run cannot draw, keyed by its level, with the sentence that says what the face needs; the gallery shows it on the card in place of the plot.

comparable property

comparable: bool

True when both an L2 and an L3 face are registered.

Derived, never curated: the existence of both variants is the L2<->L3 pairing, so it can never drift out of sync with what is built.

at

at(level: Level) -> PlotSpec | None

Return the variant registered at level, or None.

PlotSpec dataclass

PlotSpec(
    name: str,
    title: str,
    plot_name: str,
    prepare: Callable[..., dict],
    prep_kwargs: dict[str, Any] = dict(),
)

How to prepare and label a single plot face.

A spec carries no level of its own -- the level is a coordinate on the owning :class:PlotConcept (for leveled concepts) or None (for the level-less analytics in :attr:GalleryModel.analytics).

Parameters:

Name Type Description Default
name str

Identifier / file stem for level-less cards (e.g. "vpin"). For a concept variant the file stem is derived from the concept + level, so name is only metadata there.

required
title str

Human-readable title (used directly for analytic cards; concept cards take their title from the concept).

required
plot_name str

Dispatcher concept key for :func:ob_analytics.visualization.plot.

required
prepare Callable

prepare_<name>_data helper returning the renderer payload.

required
prep_kwargs dict

Keyword arguments passed to prepare.

dict()
generate_gallery(
    result: PipelineResult | None,
    output_dir: str | Path,
    *,
    model: GalleryModel | None = None,
    view: str = "both",
    backends: list[str] | None = None,
    title: str = "ob-analytics Plot Gallery",
) -> Path

Render a gallery view and write a standalone HTML page.

Parameters:

Name Type Description Default
result PipelineResult or None

Pipeline output. May be None when model is supplied (the result is only consulted by :func:build_gallery_model).

required
output_dir str or Path

Root directory for gallery output.

required
model GalleryModel

Inventory to render. Defaults to :func:build_gallery_model.

None
view str

One of "l2", "l3", "both" (default), "comparison".

'both'
backends list of str

Backends to render. Defaults to ["plotly", "matplotlib"] when plotly is installed (plotly is the primary column), else ["matplotlib"]. Pass "bokeh" explicitly to add a Bokeh column -- it is not auto-detected like plotly since it covers only some concepts (trade_tape, depth_heatmap, book_snapshot, depth_chart, order_outcome, trade_size, volume_profile, cumulative_signed_volume and spread); other concepts render as "Not available" in that column. In comparison view the backend axis collapses to a single backend (plotly if available) so the two columns carry L2 vs L3.

None
title str

Gallery page title.

'ob-analytics Plot Gallery'

Returns:

Type Description
Path

Path to the generated HTML gallery file.

Extra cards

The built-in metrics (the L1 quote, book signals, VPIN, Kyle's λ, trade imbalance, the trade imbalance horizon graph, transaction costs, Amihud illiquidity and the Roll spread) already have cards, with their default settings. To add bars, LOBSTER trading halts, or a metric's plot from a frame you computed with other settings, build a card and append it to model.analytics before rendering. The *_card helpers below build the common ones. For any other level-less plot, build a PlotSpec with the plot's prepare function:

from ob_analytics import Pipeline, vpin, sample_csv_path
from ob_analytics.visualization import prepare
from ob_analytics.visualization.gallery import (
    PlotSpec, build_gallery_model, generate_gallery,
)

result = Pipeline().run(sample_csv_path())
fit = vpin(result.trades)

model = build_gallery_model(result)
model.analytics.append(
    PlotSpec(
        name="vpin_alert_08",
        title="VPIN (alert at 0.8)",
        plot_name="vpin",
        prepare=prepare.vpin,
        prep_kwargs={"vpin_result": fit, "threshold": 0.8},
    )
)
generate_gallery(result, "out/gallery", model=model)

bars_card

bars_card(bars: DataFrame, *, label: str = '') -> PlotSpec

Build a bars card for :attr:GalleryModel.analytics.

bars is a table from :func:ob_analytics.bars.bars. label names the rule and threshold it was cut with and is shown in the chart title; the default reads them off the frame. Prices are drawn as they arrive, so cut the bars from a display-unit trades frame (see :func:~ob_analytics.display.display_result) when the rest of the gallery is in the quote currency.

trade_imbalance_by_window_card

trade_imbalance_by_window_card(
    trades: DataFrame,
    *,
    windows: tuple[str, ...] = TRADE_IMBALANCE_WINDOWS,
    grid: str = "5s",
    start_time: Timestamp | None = None,
    end_time: Timestamp | None = None,
) -> PlotSpec

Build a trade-imbalance-by-window card for :attr:GalleryModel.analytics (one band per window).

kyle_lambda_card

kyle_lambda_card(kyle_result: Any) -> PlotSpec

Build a Kyle's lambda card for :attr:GalleryModel.analytics.

trading_halts_card

trading_halts_card(
    trades: DataFrame, halts: DataFrame
) -> PlotSpec

Build a trading-halts card for :attr:GalleryModel.analytics.

LOBSTER halts are not part of the slim :class:PipelineResult; read them from :attr:~ob_analytics.lobster.LobsterLoader.trading_halts and pass them here.