compositional_graph_models/data.py
def plot_graph(
    graph: dgl.DGLGraph, function_vocab: Dict[str, int], token_vocab: Dict[str, int]
) -> None:
    """
    Plot an equation graph in a human-readable format.

    Each node is labeled with its token type if it is a leaf or its function type if it
    is internal, as well as its input index.

    Parameters
    ----------
    graph
        The graph to display.
    function_vocab
        A mapping from function name to id.
    token_vocab
        A mapping from token name to id.
    """
    function_names = {idx: name for name, idx in function_vocab.items()}
    token_names = {idx: name for name, idx in token_vocab.items()}

    labels = {}
    for i, (function_id, token_id) in enumerate(
        zip(graph.ndata[FUNCTION_FIELD].numpy(), graph.ndata[TOKEN_FIELD].numpy(),)
    ):
        if function_names[function_id] == LEAF_FUNCTION:
            labels[i] = f"{token_names[token_id]}"
        else:
            labels[i] = f"{function_names[function_id]}"

    plt.subplots()
    nx_graph = graph.to_networkx()
    positions = nx.nx_agraph.graphviz_layout(nx_graph, prog="dot")
    nx.draw(nx_graph, positions, with_labels=False)
    nx.draw_networkx_labels(nx_graph, positions, labels)
    plt.axis("off")
    plt.show(block=False)
    plt.ion()

    Plot an equation graph in a human-readable format.

    Each node is labeled with its token type if it is a leaf or its function type if it
    is internal, as well as its input index.

    Parameters
    ----------
    graph
        The graph to display.
    function_vocab
        A mapping from function name to id.
    token_vocab
        A mapping from token name to id.
    
Plot an equation graph in a human-readable format.
Each node is labeled with its token type if it is a leaf or its function type if it
is internal, as well as its input index.

Parameters

graph
The graph to display.
function_vocab
A mapping from function name to id.
token_vocab
A mapping from token name to id.
Plot an equation graph in a human - readable format . Each node is labeled with its token type if it is a leaf or its function type if it is internal as well as its input index . Parameters graph The graph to display . function_vocab A mapping from function name to id . token_vocab A mapping from token name to id .
def plot_graph(
    graph: dgl.DGLGraph, function_vocab: Dict[str, int], token_vocab: Dict[str, int]
) -> None:
    function_names = {idx: name for name, idx in function_vocab.items()}
    token_names = {idx: name for name, idx in token_vocab.items()}
    labels = {}
    for i, (function_id, token_id) in enumerate(
        zip(graph.ndata[FUNCTION_FIELD].numpy(), graph.ndata[TOKEN_FIELD].numpy(),)
    ):
        if function_names[function_id] == LEAF_FUNCTION:
            labels[i] = f"{token_names[token_id]}"
        else:
            labels[i] = f"{function_names[function_id]}"
    plt.subplots()
    nx_graph = graph.to_networkx()
    positions = nx.nx_agraph.graphviz_layout(nx_graph, prog="dot")
    nx.draw(nx_graph, positions, with_labels=False)
    nx.draw_networkx_labels(nx_graph, positions, labels)
    plt.axis("off")
    plt.show(block=False)
    plt.ion()
def plot_graph ( graph : dgl . DGLGraph , function_vocab : Dict [ str , int ] , token_vocab : Dict [ str , int ] ) -> None : function_names = { idx : name for name , idx in function_vocab . items ( ) } token_names = { idx : name for name , idx in token_vocab . items ( ) } labels = { } for i , ( function_id , token_id ) in enumerate ( zip ( graph . ndata [ FUNCTION_FIELD ] . numpy ( ) , graph . ndata [ TOKEN_FIELD ] . numpy ( ) , ) ) : if function_names [ function_id ] == LEAF_FUNCTION : labels [ i ] = f"{token_names[token_id]}" else : labels [ i ] = f"{function_names[function_id]}" plt . subplots ( ) nx_graph = graph . to_networkx ( ) positions = nx . nx_agraph . graphviz_layout ( nx_graph , prog = "dot" ) nx . draw ( nx_graph , positions , with_labels = False ) nx . draw_networkx_labels ( nx_graph , positions , labels ) plt . axis ( "off" ) plt . show ( block = False ) plt . ion ( )
Plot an equation graph in a human-readable format.
Plot an equation graph in a human - readable format .
"""
    Plot an equation graph in a human-readable format.

    Each node is labeled with its token type if it is a leaf or its function type if it
    is internal, as well as its input index.

    Parameters
    ----------
    graph
        The graph to display.
    function_vocab
        A mapping from function name to id.
    token_vocab
        A mapping from token name to id.
    """
graph
dgl.DGLGraph
function_vocab
Dict[str, int]
token_vocab
Dict[str, int]