Skip to content

visualizer

Defines a visualizer for graphs.

This package defines the functions to view graphs and mG model outputs on a web browser in an interactive way.

The package contains the following functions:

  • print_graph(graph, node_names_func='id', hierarchical=False, show_labels=False, open_browser=True)
  • print_layer(model, inputs, labels=None, layer_name=None, layer_idx=None, open_browser=True)

print_graph

print_graph(
    graph: Graph,
    id_generator: Callable[[int], int] = lambda x: x,
    hierarchical: bool = False,
    show_labels: bool = False,
    filename: str | None = None,
    open_browser: bool = True,
    engine: Literal["pyvis", "cosmo"] = "pyvis",
) -> None

Visualizes a graph.

Parameters:

  • graph (Graph) –

    The graph to visualize.

  • id_generator (Callable[[int], int], default: lambda x: x ) –

    Used for determining the ID of a node. Usually this is the identity function so that the first node has ID = 0, the second has ID = 1, and so on. In other situations, it might be necessary to have different mappings for integer IDs or to use string IDs.

  • hierarchical (bool, default: False ) –

    If true, visualize the graph in hierarchical mode. The graph must have a hierarchy attribute containing the hierarchy.

  • show_labels (bool, default: False ) –

    If true, show the node labels alongside the node features.

  • filename (str | None, default: None ) –

    The name of the .html file to save in the working directory. The string graph_ will be prepended to it.

  • open_browser (bool, default: True ) –

    If true, opens the default web browser and loads up the generated .html page.

  • engine (Literal['pyvis', 'cosmo'], default: 'pyvis' ) –

    The visualization engine to use. Options are pyvis or cosmo.

Returns:

  • None

    Nothing.

Source code in libmg/visualizer/visualizer.py
def print_graph(graph: Graph, id_generator: Callable[[int], int] = lambda x: x, hierarchical: bool = False, show_labels: bool = False,
                filename: str | None = None, open_browser: bool = True, engine: Literal["pyvis", "cosmo"] = 'pyvis') -> None:
    """Visualizes a graph.

    Args:
        graph: The graph to visualize.
        id_generator: Used for determining the ID of a node. Usually this is the identity function so that the first node has ID = 0, the second has ID = 1, and
            so on. In other situations, it might be necessary to have different mappings for integer IDs or to use string IDs.
        hierarchical: If true, visualize the graph in hierarchical mode. The graph must have a ``hierarchy`` attribute containing the hierarchy.
        show_labels: If true, show the node labels alongside the node features.
        filename: The name of the .html file to save in the working directory. The string ``graph_`` will be prepended to it.
        open_browser: If true, opens the default web browser and loads up the generated .html page.
        engine: The visualization engine to use. Options are ``pyvis`` or ``cosmo``.

    Returns:
        Nothing.
    """
    engines[engine](graph.x, zip(graph.a.row, graph.a.col), graph.e, graph.y if show_labels else None, graph.hierarchy if hierarchical else None, id_generator,
                    filename if filename is not None else str(graph), open_browser)

print_layer

print_layer(
    model: MGModel,
    inputs: tuple[tf.Tensor, ...],
    labels: tf.Tensor | None = None,
    layer_name: str | Tree | None = None,
    layer_idx: int | None = None,
    filename: str | None = None,
    open_browser: bool = True,
    engine: Literal["pyvis", "cosmo"] = "pyvis",
) -> None

Visualizes the outputs of a model's layer.

Layer must be identified either by name or index. If both are given, index takes precedence.

Parameters:

  • model (MGModel) –

    The mG model where the layer to visualize is to be found.

  • inputs (tuple[Tensor, ...]) –

    The inputs of the model that are used to generate the output to visualize.

  • labels (Tensor | None, default: None ) –

    If provided, also show the node labels alongside the node features generated by the visualized layer.

  • layer_name (str | Tree | None, default: None ) –

    The name of the layer to find.

  • layer_idx (int | None, default: None ) –

    The index of the layer to find.

  • filename (str | None, default: None ) –

    The name of the .html file to save in the working directory. The string graph_ will be prepended to it and the provided index or layer name will be appended to it.

  • open_browser (bool, default: True ) –

    If true, opens the default web browser and loads up the generated .html page.

  • engine (Literal['pyvis', 'cosmo'], default: 'pyvis' ) –

    The visualization engine to use. Options are pyvis or cosmo.

Returns:

  • None

    Nothing.

Raises:

  • ValueError

    Neither a name nor an index have been given.

  • KeyError

    No layer of the given name is present in the model.

Source code in libmg/visualizer/visualizer.py
def print_layer(model: MGModel, inputs: tuple[tf.Tensor, ...], labels: tf.Tensor | None = None, layer_name: str | Tree | None = None,
                layer_idx: int | None = None, filename: str | None = None, open_browser: bool = True, engine: Literal["pyvis", "cosmo"] = 'pyvis') -> None:
    """Visualizes the outputs of a model's layer.

    Layer must be identified either by name or index. If both are given, index takes precedence.

    Args:
        model: The mG model where the layer to visualize is to be found.
        inputs: The inputs of the model that are used to generate the output to visualize.
        labels: If provided, also show the node labels alongside the node features generated by the visualized layer.
        layer_name: The name of the layer to find.
        layer_idx: The index of the layer to find.
        filename: The name of the .html file to save in the working directory. The string ``graph_`` will be prepended to it and the provided index or layer
            name will be appended to it.
        open_browser: If true, opens the default web browser and loads up the generated .html page.
        engine: The visualization engine to use. Options are ``pyvis`` or ``cosmo``.

    Returns:
        Nothing.

    Raises:
        ValueError: Neither a name nor an index have been given.
        KeyError: No layer of the given name is present in the model.
    """
    layer = fetch_layer(model, layer_name, layer_idx)
    debug_model = MGModel(model.inputs, layer.output, None, None, None, None, None, None)
    idx_or_name = layer_idx if layer_idx is not None else layer.name
    labels = labels.numpy() if labels is not None else None
    _, a, e, _ = unpack_inputs(inputs)
    engines[engine](tuple(t.numpy() for t in debug_model(inputs)), a.indices.numpy(), e.numpy() if e is not None else None, labels, None, lambda x: x,
                    filename + '_' + str(idx_or_name) if filename is not None else str(idx_or_name), open_browser)