visualizer#

Submodule implementing data visualizations.

Classes from this module encapsulate interaction with visualisation engines like Matplotlib and TensorBoard. All interactions with vizualizers are done from monitorch.lens and PyTorchInspector. AbstractVisualizer defines methods for vizualizers. To pass visualizer to a PyTorchInspector, one could pass an instance of AbstractVisualizer or a string "matplotlib", "tensorboard" or "print" as a vizualizer argument.

Examples

>>> from monitorch.inspector import PyTorchInspector
>>> from monitorch.lens import ...
>>>
>>> inspector = PyTorchInspector(
...     lenses = [...],
...     vizualizer = "tensorboard"
... )
class monitorch.visualizer.AbstractVisualizer[source]#

Bases: ABC

Base class for all visualizers.

PyTorchInspector and lenses from monitorch.lens use methods provided by this class for vizualization.

abstract plot_numerical_values(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]], ranges_dict: OrderedDict[str, dict[tuple[str, str], tuple[float, float]]] | None = None) None[source]#

Plots numerical values and ranges for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict and ranges_dict. Plots single epoch values, does not process a sequence of points.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str,float]]) –

    Ordered dictionary of subtags and their numerical values. Most common is an ordered dict of layer names and layer statstics.

    Example

    OrderedDict([
        ('lin1', {'mean': 0.0,  'median': 1.0}),
        ('lin2', {'mean': 42.0, 'median': 10.0})
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

  • ranges_dict (OrderedDict[str, dict[tuple[str, str], tuple[float, float]]]|None) –

    Optional ordered dictionary of subtags and its ranges. Most common is an ordered dict of layer names and layer statstics range.

    Example

    OrderedDict([
        ('lin1', {'min': 1.0,  'max': 10.0}),
        ('lin2', {'min': -4.0, 'max': 0.0})
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

abstract plot_probabilities(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plots proportions/probabilities for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict. Plots single epoch values, does not process a sequence of points. Sets y-axis limits to (0, 1) if it is possible to do programmatically.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str, float]]) –

    Ordered dictionary of subtags and propotions. Most common is a dictionary of layer names and layer activations/death rates.

    Example

    OrderedDict([
        ('relu1', {'activation_rate' : 0.8, death_rate : 0.15}),
        ('relu2 , {'activation_rate' : 0.6, death_rate : 0.2 })
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

abstract plot_relations(epoch: int, main_tag, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plot comparison for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict. Plots single epoch values, does not process a sequence of points.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str, float]]) –

    Ordered dictionary of subtags and statistics. Most common is a big plot with layer information.

    Example

    OrderedDict([
        ('Output Log Norm', {
            'relu1' : 1.2,
            'relu2' : 1.5
        })
    ])
    

abstract register_tags(main_tag: str, tag_attr: TagAttributes) None[source]#

Prepare visualizer’s inner state for plot.

Parameters:
  • main_tag (str) – Name of the collection of plots.

  • tag_attr (TagAttributes) – Tag attributes to configure plot.

class monitorch.visualizer.MatplotlibVisualizer(**kwargs)[source]#

Bases: AbstractVisualizer

Visualises data using matplotlib.

Saves data provided by public plot methods, allocates figures, axes and draws plots on show_fig(). Autoconfigures figures and legends. Allocates one superfigure, containing a figure for big plots (big-plot-figure) and a figure for collections of small figures (small-plot-figure). For every bigplot tag allcoates another subfigure inside big-plot-figure. Similarly allocates a figure for each small tag inside small-plot-figure.

Parameters:

**kwargs – Passes all arguments to matplotlib.pyplot.figure.

figure_#

Superfigure of the plot.

Type:

matplotlib.figure.Figure

plot_numerical_values(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]], ranges_dict: OrderedDict[str, dict[tuple[str, str], tuple[float, float]]] | None = None) None[source]#

Does not draw any plots. Provided data is saved.

For description of arguments see base class.

plot_probabilities(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Does not draw any plots. Provided data is saved.

For description of arguments see base class.

plot_relations(epoch: int, main_tag, values_dict: OrderedDict[str, OrderedDict[str, float]]) None[source]#

Does not draw any plots. Provided data is saved.

For description of arguments see base class.

register_tags(main_tag: str, tag_attr: TagAttributes) None[source]#

See base class.

reset_fig() Self[source]#

Resets figure.

Sets figure_ to None and internal state to default.

Return type:

Self

show_fig() Figure[source]#

Composes figure if it was not shown before and displays it.

If figure_ is not None returns it. Otherwise allocates figures, draws plots defined with register_tags() and data provided by plot methods.

Returns:

Superfigure containing all plots.

Return type:

matplotlib.figure.Figure

class monitorch.visualizer.PlayerVisualizer(source_filepath: str, target_visualizer: AbstractVisualizer)[source]#

Bases: object

Reads a recorded visualizer file and executes calls on a target instance.

playback() PlayerVisualizer[source]#

Iterates through the pickle file and triggers methods on the target visualizer.

class monitorch.visualizer.PrintVisualizer[source]#

Bases: AbstractVisualizer

Prints all information to stdout.

Does not require preconfiguration, nor has any state.

plot_numerical_values(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]], ranges_dict: OrderedDict[str, dict[tuple[str, str], tuple[float, float]]] | None = None) None[source]#

Prints f'({epoch}) {main_tag}: {tag} - values : {values_dict.get(tag, {})}; ranges : {ranges_dict.get(tag, {}) if ranges_dict else {}}' for all tags that are in values_dict or ranges_dict.

For argument description see base class.

plot_probabilities(epoch: int, main_tag: str, values_dict: dict[str, dict[str, float]]) None[source]#

Prints f'({epoch}) {main_tag}: {tag} - prbs: {prbs}' for all tags that are in values_dict.

For argument description see base class.

plot_relations(epoch: int, main_tag, values_dict: dict[str, dict[str, float]]) None[source]#

Prints f'({epoch}) {main_tag}: {tag} - {relations}' for all tags that are in values_dict.

For argument description see base class.

register_tags(main_tag: str, tag_attr: TagAttributes) None[source]#

Prints f"{main_tag}: {tag_attr}".

For argument description see base class.

class monitorch.visualizer.RecorderVisualizer(filepath: str)[source]#

Bases: AbstractVisualizer

Serializes all visualizer calls to a binary file using pickle.

close()[source]#

Safely close the file handle.

plot_numerical_values(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]], ranges_dict: OrderedDict[str, dict[tuple[str, str], tuple[float, float]]] | None = None) None[source]#

Plots numerical values and ranges for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict and ranges_dict. Plots single epoch values, does not process a sequence of points.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str,float]]) –

    Ordered dictionary of subtags and their numerical values. Most common is an ordered dict of layer names and layer statstics.

    Example

    OrderedDict([
        ('lin1', {'mean': 0.0,  'median': 1.0}),
        ('lin2', {'mean': 42.0, 'median': 10.0})
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

  • ranges_dict (OrderedDict[str, dict[tuple[str, str], tuple[float, float]]]|None) –

    Optional ordered dictionary of subtags and its ranges. Most common is an ordered dict of layer names and layer statstics range.

    Example

    OrderedDict([
        ('lin1', {'min': 1.0,  'max': 10.0}),
        ('lin2', {'min': -4.0, 'max': 0.0})
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

plot_probabilities(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plots proportions/probabilities for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict. Plots single epoch values, does not process a sequence of points. Sets y-axis limits to (0, 1) if it is possible to do programmatically.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str, float]]) –

    Ordered dictionary of subtags and propotions. Most common is a dictionary of layer names and layer activations/death rates.

    Example

    OrderedDict([
        ('relu1', {'activation_rate' : 0.8, death_rate : 0.15}),
        ('relu2 , {'activation_rate' : 0.6, death_rate : 0.2 })
    ])
    

    There is no strict restriction for each subtag to have the same dictionary structure, though some visualizers may not behave as expected if inner dictionaries differ.

plot_relations(epoch: int, main_tag, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plot comparison for a collection of tags.

For single big plot use main_tag as an only subtag in values_dict. Plots single epoch values, does not process a sequence of points.

Parameters:
  • epoch (int) – Training epoch.

  • main_tag (str) – Name of the collection of plots.

  • values_dict (OrderedDict[str, dict[str, float]]) –

    Ordered dictionary of subtags and statistics. Most common is a big plot with layer information.

    Example

    OrderedDict([
        ('Output Log Norm', {
            'relu1' : 1.2,
            'relu2' : 1.5
        })
    ])
    

register_tags(main_tag: str, tag_attr: TagAttributes) None[source]#

Prepare visualizer’s inner state for plot.

Parameters:
  • main_tag (str) – Name of the collection of plots.

  • tag_attr (TagAttributes) – Tag attributes to configure plot.

class monitorch.visualizer.TagAttributes(logy: bool, big_plot: bool, annotate: bool, type: TagType, ylim: tuple[float, float] | None = None)[source]#

Bases: object

Packaged data about plot.

Used by visualizers, must be given by lens using register_tags().

annotate: bool#

bool Indicator if legend must be plotted, flag is relecant only for MatplotlibVisualizer.

Type:

annotate

big_plot: bool#

bool Indicator if plot should be big or a collection of small ones, flag is relecant only for MatplotlibVisualizer.

Type:

bigplot

logy: bool#

bool Indicator if scale of y-axis must be log, flag is relecant only for MatplotlibVisualizer.

Type:

logy

type: TagType#

TagType Type of plot.

Type:

type

ylim: tuple[float, float] | None = None#

tuple[flaot, float]|None Optional limits for y-axis.

Type:

ylim

class monitorch.visualizer.TagType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Enum of types of plots. Used in TagAttributes.

NUMERICAL = 0#

Numerical line and band-range plot, no implicit y-axis limits.

PROBABILITY = 1#

Line plot with implicit y-axis limits (0, 1).

RELATIONS = 2#

Displays comparison between several numerical variables. Plot method depends on visualizer: stackplot for MatplotlibVisualizer, multiline plot for TensorBoardVisualizer.

class monitorch.visualizer.TensorBoardVisualizer(log_dir=None, comment='', **kwargs)[source]#

Bases: AbstractVisualizer

Wrapper around torch.utils.tensorboard.SummaryWriter.

Creates a SummaryWriter and uses it to plot data. Translates names to be keyboard-friendly.

Parameters:

**kwargs – All arguments are passed to internal SummaryWriter. Default behaiviour is the same as the default behaiviour of standalone writer.

plot_numerical_values(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]], ranges_dict: OrderedDict[str, dict[tuple[str, str], tuple[float, float]]] | None = None) None[source]#

Plots numerical data onto Tensoroard.

Splits ranges into two separate plots. Uses `add_scalar` for drawing.

For parameter description see base class.

plot_probabilities(epoch: int, main_tag: str, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plots proportions onto Tensoroard.

Splits ranges into two separate plots. Uses `add_scalar` for drawing.

For parameter description see base class.

plot_relations(epoch: int, main_tag, values_dict: OrderedDict[str, dict[str, float]]) None[source]#

Plots relational data onto Tensoroard.

Splits ranges into two separate plots. Uses `add_scalars` for drawing, therefore creating additional runs.

For parameter description see base class.

register_tags(main_tag: str, tag_attr: TagAttributes) None[source]#

Tensorboard needs no registration. Is present for consitency.

static transform_tag_str(general_tag, subtag, delimiter='/') str[source]#

Transforms tag and subtag string to be keyboard friendly.

Parameters:
  • general_tag (str) – Tag string to be concatenated and transformed.

  • subtag (str) – Subtag string to be concatenated and transformed.

  • delimiter (str = '/') – Delimiter to put between tag and subtag.