Visuals

class todd.visuals.BaseVisual[source]

Bases: ABC

abstractmethod __init__(width, height)[source]
Parameters:
Return type:

None

activation(activation, left=0, top=0, width=None, height=None, opacity=0.5)[source]

Draw the activation map.

Suppose our activation map is \((256, 13, 20)\), where 256 is the number of channels, 13 is the height, and 20 is the width:

>>> activation = torch.rand(256, 13, 20)

We first reduce the channel dimension, using whatever reduction:

>>> import einops
>>> activation = einops.reduce(
...     activation,
...     'c h w -> h w',
...     reduction='mean',
... )

Then we draw the activation map:

>>> from .pptx import PPTXVisual
>>> visual = PPTXVisual(640, 426)
>>> visual.activation(
...     activation,
...     width=visual.width,
...     height=visual.height,
... )
<pptx.shapes.picture.Picture object at ...>
Parameters:
  • activation (Tensor) – \((H, W)\)

  • left (int) – x coordinate of the left side of the activation map

  • top (int) – y coordinate of the top size of the activation map

  • width (int | None) – width of the activation map

  • height (int | None) – height of the activation map

  • opacity (float) – opacity of the activation map

Return type:

Any

color(index)[source]
Parameters:

index (int)

Return type:

Color

abstract property height: int
abstractmethod image(image, left=0, top=0, width=None, height=None, opacity=1.0)[source]
Parameters:
Return type:

Any

abstractmethod line(x1, y1, x2, y2, color=RGB(0.0, 0.0, 0.0), thickness=1)[source]
Parameters:
Return type:

Any

abstractmethod marker(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

Any

abstractmethod point(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

Any

abstractmethod rectangle(left, top, width, height, color=RGB(0.0, 0.0, 0.0), thickness=1, fill=None)[source]
Parameters:
Return type:

Any

abstractmethod save(path)[source]
Parameters:

path (Any)

Return type:

None

scatter(points, sizes, colors, types)[source]
Parameters:
Return type:

Any

abstractmethod text(text, x, y, color=RGB(0.0, 0.0, 0.0), font=None)[source]
Parameters:
Return type:

Any

trajectory(trajectory, color=RGB(0.0, 0.0, 0.0), thickness=1)[source]

Draw the trajectory.

Parameters:
  • trajectory (Iterable[tuple[int, int]]) – \((T, 2)\)

  • color (Color) – color of the trajectory

  • thickness (int) – thickness of the trajectory

Return type:

Self

abstract property width: int
class todd.visuals.CV2Visual[source]

Bases: BaseVisual

__init__(width, height, channels=3, **kwargs)[source]
Parameters:
Return type:

None

property height: int
image(image, left=0, top=0, width=None, height=None, opacity=1.0)[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

line(x1, y1, x2, y2, color=RGB(0.0, 0.0, 0.0), thickness=1)[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

marker(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

point(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

rectangle(left, top, width, height, color=RGB(0.0, 0.0, 0.0), thickness=1, fill=None)[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

save(path)[source]
Parameters:

path (Any)

Return type:

None

text(text, x, y, color=RGB(0.0, 0.0, 0.0), font=None, x_anchor=XAnchor.LEFT, y_anchor=YAnchor.TOP, thickness=1)[source]
Parameters:
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

to_numpy()[source]
Return type:

ndarray[tuple[Any, …], dtype[uint8]]

property width: int
class todd.visuals.PPTXVisual[source]

Bases: BaseVisual

Visualize data in the format of PowerPoint.

The PowerPoint contains only one slide. For more details, refer to python-pptx.

__init__(width, height, **kwargs)[source]

Initialize the PowerPoint with a single slide.

To initialize a PowerPoint with width 640pt and height 426pt, use the following code:

>>> visual = PPTXVisual(640, 426)

Once initialized, the width and height of the slide cannot be altered. We can read the width and height of the PowerPoint by:

>>> visual.width
640
>>> visual.height
426
Parameters:
  • width (int) – the width of the PowerPoint in point

  • height (int) – the height of the PowerPoint in point

Return type:

None

property height: int

Height of the PowerPoint.

image(image, left=0, top=0, width=None, height=None, opacity=1.0)[source]

Add an image to the PowerPoint.

Suppose the image is \((426, 640)\):

>>> image = np.random.randint(0, 256, (426, 640, 3))

In most cases, the PowerPoint is of the same size as the image, so that the image covers the whole background:

>>> h, w, _ = image.shape
>>> visual = PPTXVisual(w, h)
>>> visual.image(image)
<pptx.shapes.picture.Picture object at ...>

The returned pptx.shapes.picture.Picture object can be used to fine-tune the properties of the image. Note that the DPI of the image should not be changed thoughtlessly, because of the bizarre measurements in PowerPoint. The most common measurement unit in PowerPoint is Point (pt), where 1pt equals 1/72 inches. However, images are measured in pixels and 1 pixel equals 1/DPI inches. By default, DPI is 72 so that 1 pixel is 1pt. When DPI is set to other values, the size of the image may become larger or smaller than it is supposed to be.

Parameters:
  • image (ndarray[tuple[Any, ...], dtype[uint8]]) – \((H, W, 3)\)

  • left (int) – x coordinate of the left side of the image

  • top (int) – y coordinate of the top side of the image

  • width (int | None) – width of the image

  • height (int | None) – height of the image

  • opacity (float) – opacity of the image

Return type:

Picture

line(x1, y1, x2, y2, color=RGB(0.0, 0.0, 0.0), thickness=1)[source]
Parameters:
Return type:

Any

marker(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

Any

point(x, y, size, color=RGB(0.0, 0.0, 0.0))[source]
Parameters:
Return type:

Any

property presentation: Presentation
rectangle(left, top, width, height, color=RGB(0.0, 0.0, 0.0), thickness=1, fill=None)[source]
Parameters:
Return type:

Shape

save(path)[source]

Save the PowerPoint.

The save target can either be a filepath, for example:

>>> import tempfile
>>> with tempfile.NamedTemporaryFile() as f:
...     PPTXVisual(640, 426).save(f.name)

Or it can simply be a file-like object:

>>> with tempfile.TemporaryFile() as f:
...     PPTXVisual(640, 426).save(f)
Parameters:

path (Any) – destination path

Return type:

None

property shapes: SlideShapes
property slide: Slide
text(text, x, y, color=RGB(0.0, 0.0, 0.0), font=None)[source]
Parameters:
Return type:

Shape

property width: int

Width of the PowerPoint.

class todd.visuals.XAnchor[source]

Bases: Enum

LEFT = 1
RIGHT = 2
class todd.visuals.YAnchor[source]

Bases: Enum

BOTTOM = 2
TOP = 1