plesty.lib.report.svg

A figure’s drawables as one self-contained SVG.

Series take their colours from the packaged palette (the same tokens a live view resolves), and everything else — axes, ticks, labels — is drawn in currentColor, so the figure reads on the page it is set in, light or dark. An Image is embedded as a PNG in the palette’s colour map; nothing outside the standard library and numpy is needed to draw any of it.

Attributes

MARGIN

Trace

_CORNERS

Functions

figure_svg(→ str)

Draw drawables on one pair of axes.

_finite(→ numpy.ndarray)

Return the finite entries of values as a flat float array.

_span(→ tuple[float, float])

Return the range covering every array, widened when it is a point.

_ranges(→ tuple[tuple[float, float], tuple[float, float]])

Return the x and y ranges the axes show.

_ticks(→ list[float])

Return round tick positions inside [low, high]: steps of 1, 2 or 5.

_tick_label(→ str)

Return a tick value with just the decimals its step needs.

_mapper(→ Any)

Return a function from data coordinates to SVG coordinates.

_axes(→ str)

Draw the frame, the ticks with their values, and the axis labels.

_trace(→ str)

Draw a line (broken where a value is missing) or a set of points.

_polyline(→ str)

Return one unbroken run of a line.

_legend_entries(→ list[Trace])

Return the traces a legend lists: labelled, and not hidden by a _ label.

_legend_box(→ tuple[float, float, float, float])

Return the box (x0, y0, x1, y1) a legend of rows entries takes in corner.

_legend_corner(→ str)

Choose the corner where the legend covers the fewest trace points.

_legend(→ str)

List every labelled trace in corner of the plot.

_marks(→ str)

Set scalars and text marks in the top-left corner, one line each.

_levels(→ tuple[numpy.ndarray, float, float])

Return the image scaled to [0, 1] (nan where missing), and its range.

_colour(→ numpy.ndarray)

Map levels in [0, 1] onto the colour map, as RGBA bytes.

_png(→ bytes)

Encode an (rows, columns, 4) byte array as a PNG.

_data_uri(→ str)

Return a PNG as a data: URI.

_image(→ str)

Embed the image, first row at the bottom, stretched over its extent.

_colour_bar(→ str)

Draw the colour map beside the plot, labelled with the image's range.

Module Contents

plesty.lib.report.svg.MARGIN = (64, 20, 16, 48)
plesty.lib.report.svg.Trace
plesty.lib.report.svg.figure_svg(drawables: Sequence[plesty.lib.monitor.plots.Drawable], theme: plesty.lib.ui.theme.Theme | None = None) → str

Draw drawables on one pair of axes.

Parameters:
Returns:

An <svg> element.

Return type:

str

plesty.lib.report.svg._finite(values: Any) → numpy.ndarray

Return the finite entries of values as a flat float array.

Parameters:

values (Any)

Return type:

numpy.ndarray

plesty.lib.report.svg._span(values: Sequence[numpy.ndarray]) → tuple[float, float]

Return the range covering every array, widened when it is a point.

Parameters:

values (Sequence[numpy.ndarray])

Return type:

tuple[float, float]

plesty.lib.report.svg._ranges(images: Sequence[plesty.lib.monitor.plots.Image], traces: Sequence[Trace]) → tuple[tuple[float, float], tuple[float, float]]

Return the x and y ranges the axes show.

Parameters:
Return type:

tuple[tuple[float, float], tuple[float, float]]

plesty.lib.report.svg._ticks(low: float, high: float, count: int = 6) → list[float]

Return round tick positions inside [low, high]: steps of 1, 2 or 5.

Parameters:
  • low (float)

  • high (float)

  • count (int)

Return type:

list[float]

plesty.lib.report.svg._tick_label(value: float, ticks: Sequence[float]) → str

Return a tick value with just the decimals its step needs.

Parameters:
  • value (float)

  • ticks (Sequence[float])

Return type:

str

plesty.lib.report.svg._mapper(area: tuple[float, float, float, float], x_range: tuple[float, float], y_range: tuple[float, float]) → Any

Return a function from data coordinates to SVG coordinates.

Parameters:
  • area (tuple[float, float, float, float])

  • x_range (tuple[float, float])

  • y_range (tuple[float, float])

Return type:

Any

plesty.lib.report.svg._axes(area: tuple[float, float, float, float], x_range: tuple[float, float], y_range: tuple[float, float], drawables: Sequence[plesty.lib.monitor.plots.Drawable]) → str

Draw the frame, the ticks with their values, and the axis labels.

Parameters:
Return type:

str

plesty.lib.report.svg._trace(item: Trace, area: tuple[float, float, float, float], x_range: tuple[float, float], y_range: tuple[float, float], theme: plesty.lib.ui.theme.Theme) → str

Draw a line (broken where a value is missing) or a set of points.

Parameters:
Return type:

str

plesty.lib.report.svg._polyline(points: list[str], colour: str, width: float) → str

Return one unbroken run of a line.

Parameters:
  • points (list[str])

  • colour (str)

  • width (float)

Return type:

str

plesty.lib.report.svg._CORNERS = ('top-right', 'bottom-right', 'top-left', 'bottom-left')
plesty.lib.report.svg._legend_entries(traces: Sequence[Trace]) → list[Trace]

Return the traces a legend lists: labelled, and not hidden by a _ label.

Parameters:

traces (Sequence[Trace])

Return type:

list[Trace]

plesty.lib.report.svg._legend_box(corner: str, area: tuple[float, float, float, float], rows: int, longest: int) → tuple[float, float, float, float]

Return the box (x0, y0, x1, y1) a legend of rows entries takes in corner.

Parameters:
  • corner (str)

  • area (tuple[float, float, float, float])

  • rows (int)

  • longest (int)

Return type:

tuple[float, float, float, float]

plesty.lib.report.svg._legend_corner(drawables: Sequence[plesty.lib.monitor.plots.Drawable]) → str

Choose the corner where the legend covers the fewest trace points.

A legend fixed top-right sat on every series that rises to the right — in a log–log power dependence, on the ends of all six lines and their fits. A line is sampled along its segments, not only at its points, so a sparse line crossing a corner counts too. The top-left corner holds the scalars and text marks, so a figure with any never puts its legend there. The LaTeX renderer asks the same question, so both outputs agree.

Parameters:

drawables (Sequence[plesty.lib.monitor.plots.Drawable]) – The figure’s drawables, as figure_svg() receives them.

Returns:

One of _CORNERS.

Return type:

str

plesty.lib.report.svg._legend(traces: Sequence[Trace], area: tuple[float, float, float, float], theme: plesty.lib.ui.theme.Theme, corner: str = 'top-right') → str

List every labelled trace in corner of the plot.

Parameters:
Return type:

str

plesty.lib.report.svg._marks(marks: Sequence[plesty.lib.monitor.plots.Scalar | plesty.lib.monitor.plots.Text], area: tuple[float, float, float, float]) → str

Set scalars and text marks in the top-left corner, one line each.

Parameters:
Return type:

str

plesty.lib.report.svg._levels(image: plesty.lib.monitor.plots.Image) → tuple[numpy.ndarray, float, float]

Return the image scaled to [0, 1] (nan where missing), and its range.

Parameters:

image (plesty.lib.monitor.plots.Image)

Return type:

tuple[numpy.ndarray, float, float]

plesty.lib.report.svg._colour(level: numpy.ndarray, stops: Sequence[str]) → numpy.ndarray

Map levels in [0, 1] onto the colour map, as RGBA bytes.

Parameters:
  • level (numpy.ndarray)

  • stops (Sequence[str])

Return type:

numpy.ndarray

plesty.lib.report.svg._png(rgba: numpy.ndarray) → bytes

Encode an (rows, columns, 4) byte array as a PNG.

Parameters:

rgba (numpy.ndarray)

Return type:

bytes

plesty.lib.report.svg._data_uri(rgba: numpy.ndarray) → str

Return a PNG as a data: URI.

Parameters:

rgba (numpy.ndarray)

Return type:

str

plesty.lib.report.svg._image(image: plesty.lib.monitor.plots.Image, area: tuple[float, float, float, float], x_range: tuple[float, float], y_range: tuple[float, float], theme: plesty.lib.ui.theme.Theme) → str

Embed the image, first row at the bottom, stretched over its extent.

Parameters:
Return type:

str

plesty.lib.report.svg._colour_bar(image: plesty.lib.monitor.plots.Image, area: tuple[float, float, float, float], theme: plesty.lib.ui.theme.Theme) → str

Draw the colour map beside the plot, labelled with the image’s range.

Parameters:
Return type:

str