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 ( |
required |
concept
|
str
|
Concept key, e.g. |
required |
level
|
Level or str
|
|
_UNSET
|
backend
|
str
|
|
'matplotlib'
|
ax
|
Axes
|
Axes to draw on, as in :func: |
None
|
theme
|
PlotTheme or None
|
Theme for this call, on any backend; |
None
|
**overrides
|
Any
|
Extra keyword arguments merged over the prepare call (e.g.
|
{}
|
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 ¶
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.
Gallery model¶
build_gallery_model ¶
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
¶
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
¶
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.
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. |
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: |
required |
prepare
|
Callable
|
|
required |
prep_kwargs
|
dict
|
Keyword arguments passed to prepare. |
dict()
|
generate_gallery ¶
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 |
required |
output_dir
|
str or Path
|
Root directory for gallery output. |
required |
model
|
GalleryModel
|
Inventory to render. Defaults to :func: |
None
|
view
|
str
|
One of |
'both'
|
backends
|
list of str
|
Backends to render. Defaults to |
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 ¶
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 ¶
Build a Kyle's lambda card for :attr:GalleryModel.analytics.
trading_halts_card ¶
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.