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,
rounding=2,
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.
-
rounding
–How many decimal digits to show for floating-point labels, defaults to 2.
-
engine
(Literal['pyvis', 'cosmo']
, default:'pyvis'
) –The visualization engine to use. Options are
pyvis
orcosmo
.
Returns:
-
None
–Nothing.
Source code in libmg/visualizer/visualizer.py
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,
id_generator: Callable[[int], int | str] = lambda x: x,
filename: str | None = None,
open_browser: bool = True,
rounding=2,
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.
-
id_generator
(Callable[[int], int | str]
, 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.
-
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.
-
rounding
–How many decimal digits to show for floating-point labels, defaults to 2.
-
engine
(Literal['pyvis', 'cosmo']
, default:'pyvis'
) –The visualization engine to use. Options are
pyvis
orcosmo
.
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
print_labels ¶
print_labels(
x: tuple[tf.Tensor, ...],
a: tf.SparseTensor,
e: tf.Tensor | None = None,
y: tf.Tensor | None = None,
id_generator: Callable[[int], int | str] = lambda x: x,
filename: str | None = None,
open_browser: bool = True,
rounding=2,
engine: Literal["pyvis", "cosmo"] = "pyvis",
)
Visualizes the labeling of a graph.
Parameters:
-
x
(tuple[Tensor, ...]
) –The node labels.
-
a
(SparseTensor
) –The adjacency matrix.
-
e
(Tensor | None
, default:None
) –The edge labels, if any.
-
y
(Tensor | None
, default:None
) –If provided, also show the node labels alongside the node features generated by the visualized layer.
-
id_generator
(Callable[[int], int | str]
, 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.
-
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.
-
rounding
–How many decimal digits to show for floating-point labels, defaults to 2.
-
engine
(Literal['pyvis', 'cosmo']
, default:'pyvis'
) –The visualization engine to use. Options are
pyvis
orcosmo
.
Returns:
-
–
Nothing.