vis4d.common.prettyprint

This module contains utilities for pretty printing.

Functions

describe_shape(obj)

Recursively output the shape of tensors in an object's structure.

Classes

PrettyRepMixin()

Creates a pretty string representation of a class with parameters.

class PrettyRepMixin[source]

Creates a pretty string representation of a class with parameters.

Examples

>>> class TestClass(PrettyRepMixin):
...     def __init__(self, a: int, b: str):
...         self.a = a
...         self.b = b
>>> obj = TestClass(1, 'hello')
>>> str(obj)
'TestClass(a=1, b=hello)'
__repr__()[source]

Return a string representation of the class and its parameters.

Return type:

str

Returns:

The string representation of the class and its parameters.

Examples

>>> class TestClass(PrettyRepMixin):
...     def __init__(self, a: int, b: str):
...         self.a = a
...         self.b = b
>>> obj = TestClass(1, 'hello')
>>> obj.__repr__()
'TestClass(a=1, b=hello)'
describe_shape(obj)[source]

Recursively output the shape of tensors in an object’s structure.

Parameters:
  • obj (Any) – The object to describe the shape of. Can be a dictionary,

  • list

  • torch.Tensor

  • numpy.ndarray

  • float

  • type. (or any other)

Returns:

A string representing the shapes of all tensors in the object’s

structure.

Return type:

str

Examples

>>> describe_shape({'a': torch.rand(2, 3)})
"{a: shape[2, 3]}"
>>> describe_shape({'a': [torch.rand(2, 3), torch.rand(4, 5)]})
"{a: [shape[2, 3], shape[4, 5]]}"
>>> describe_shape([torch.rand(2, 3), {'a': torch.rand(4, 5)}])
"[shape[2, 3], {a: shape[4, 5]}]"