Skip to content

Formatters

coola.formatters

Contain the formatter implementations.

coola.formatters.BaseFormatter

Bases: ABC, Generic[T]

Define the base class to implement a formatter.

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.format(Summarizer(), 1)
<class 'int'> 1

coola.formatters.BaseFormatter.clone abstractmethod

clone() -> BaseFormatter

Return a copy of the formatter.

Returns:

Type Description
BaseFormatter

A copy of the formatter.

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter2 = formatter.clone()
>>> formatter.set_max_characters(10)
>>> formatter
DefaultFormatter(max_characters=10)
>>> formatter2
DefaultFormatter(max_characters=-1)

coola.formatters.BaseFormatter.equal abstractmethod

equal(other: Any) -> bool

Indicate if the other object is equal to the self object.

Parameters:

Name Type Description Default
other Any

The other object to compare.

required

Returns:

Type Description
bool

True if the objects are equal, otherwise False.

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.equal(DefaultFormatter())
True
>>> formatter.equal(DefaultFormatter(max_characters=10))
False

coola.formatters.BaseFormatter.format abstractmethod

format(
    summarizer: BaseSummarizer,
    value: T,
    depth: int,
    max_depth: int,
) -> str

Format a value.

Parameters:

Name Type Description Default
summarizer BaseSummarizer

The summarizer.

required
value T

The value to summarize.

required
depth int

The current depth.

required
max_depth int

The maximum depth.

required

Returns:

Name Type Description
str str

The formatted value.

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.format(Summarizer(), 1)
<class 'int'> 1

coola.formatters.BaseFormatter.load_state_dict abstractmethod

load_state_dict(state_dict: dict) -> None

Load the state values from a dict.

Parameters:

Name Type Description Default
state_dict dict

a dict with parameters

required

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> # Please take a look to the implementation of the state_dict
>>> # function to know the expected structure
>>> formatter.load_state_dict({"max_characters": 10})
>>> formatter
DefaultFormatter(max_characters=10)

coola.formatters.BaseFormatter.state_dict abstractmethod

state_dict() -> dict

Return a dictionary containing state values.

Returns:

Type Description
dict

The state values in a dict.

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.state_dict()
{'max_characters': -1}

coola.formatters.DefaultFormatter

Bases: BaseFormatter[Any]

Implement the default formatter.

Parameters:

Name Type Description Default
max_characters int

The maximum number of characters to show. If a negative value is provided, all the characters are shown.

-1

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.format(Summarizer(), 1)
<class 'int'> 1

coola.formatters.DefaultFormatter.get_max_characters

get_max_characters() -> int

Get the maximum number of characters to show.

Returns:

Type Description
int

The maximum number of characters to show.

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.get_max_characters()
-1

coola.formatters.DefaultFormatter.set_max_characters

set_max_characters(max_characters: int) -> None

Set the maximum number of characters to show.

Parameters:

Name Type Description Default
max_characters int

The maximum number of characters to show.

required

Raises:

Type Description
TypeError

if max_characters is not an integer.

Example usage:

>>> from coola.formatters import DefaultFormatter
>>> formatter = DefaultFormatter()
>>> formatter.set_max_characters(10)
>>> formatter.get_max_characters()
10

coola.formatters.MappingFormatter

Bases: BaseCollectionFormatter[Mapping]

Implement a formatter for Mapping.

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import MappingFormatter
>>> formatter = MappingFormatter()
>>> print(formatter.format(Summarizer(), {"key1": 1.2, "key2": "abc", "key3": 42}))
<class 'dict'> (length=3)
  (key1): 1.2
  (key2): abc
  (key3): 42

coola.formatters.NDArrayFormatter

Bases: BaseFormatter[ndarray]

Implement a formatter for numpy.ndarray.

Parameters:

Name Type Description Default
show_data bool

If True, the returned string is the default string representation (repr). If False, the returned string only contains the tensor metadata.

False

Example usage:

>>> import numpy as np
>>> from coola import Summarizer
>>> from coola.formatters import NDArrayFormatter
>>> formatter = NDArrayFormatter()
>>> formatter.format(Summarizer(), np.arange(21))
<class 'numpy.ndarray'> | shape=(21,) | dtype=int64

coola.formatters.NDArrayFormatter.get_show_data

get_show_data() -> bool

Indicate if the array data or metadata are shown.

Returns:

Type Description
bool

True if the array data are shown, False if the array metadata are shown.

Example usage:

>>> from coola.formatters import NDArrayFormatter
>>> formatter = NDArrayFormatter()
>>> formatter.get_show_data()
False

coola.formatters.NDArrayFormatter.set_show_data

set_show_data(show_data: bool) -> None

Set if the array data or metadata are shown.

Parameters:

Name Type Description Default
show_data bool

True if the array data are shown, False if the array metadata are shown.

required

Raises:

Type Description
TypeError

if show_data is not a boolean.

Example usage:

>>> from coola.formatters import NDArrayFormatter
>>> formatter = NDArrayFormatter()
>>> formatter.set_show_data(True)
>>> formatter.get_show_data()
True

coola.formatters.SequenceFormatter

Bases: BaseCollectionFormatter[Sequence]

Implement a formatter for Sequence.

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import SequenceFormatter
>>> formatter = SequenceFormatter()
>>> print(formatter.format(Summarizer(), [1, 2, 3]))
<class 'list'> (length=3)
  (0): 1
  (1): 2
  (2): 3

coola.formatters.SetFormatter

Bases: BaseCollectionFormatter[set]

Implement a formatter for set.

Example usage:

>>> from coola import Summarizer
>>> from coola.formatters import SetFormatter
>>> formatter = SetFormatter()
>>> formatter.format(Summarizer(), {1})
<class 'set'> (length=1)\n  (0): 1

coola.formatters.TensorFormatter

Bases: BaseFormatter[Tensor]

Implement a formatter for torch.Tensor.

Parameters:

Name Type Description Default
show_data bool

If True, the returned string is the default string representation (repr). If False, the returned string only contains the tensor metadata.

False

Example usage:

>>> import torch
>>> from coola import Summarizer
>>> from coola.formatters import TensorFormatter
>>> formatter = TensorFormatter()
>>> formatter.format(Summarizer(), torch.arange(21))
<class 'torch.Tensor'> | shape=torch.Size([21]) | dtype=torch.int64 | device=cpu

coola.formatters.TensorFormatter.get_show_data

get_show_data() -> bool

Indicate if the tensor data or metadata are shown.

Returns:

Type Description
bool

True if the tensor data are shown, False if the tensor metadata are shown.

Example usage:

>>> from coola.formatters import TensorFormatter
>>> formatter = TensorFormatter()
>>> formatter.get_show_data()
False

coola.formatters.TensorFormatter.set_show_data

set_show_data(show_data: bool) -> None

Set if the tensor data or metadata are shown.

Parameters:

Name Type Description Default
show_data bool

True if the tensor data are shown, False if the tensor metadata are shown.

required

Raises:

Type Description
TypeError

if show_data is not an boolean.

Example usage:

>>> from coola.formatters import TensorFormatter
>>> formatter = TensorFormatter()
>>> formatter.set_show_data(True)
>>> formatter.get_show_data()
True