Skip to content

GraphUtils.to_pydot creates duplicated nodes in pydot #239

@Snow-Crash

Description

@Snow-Crash

GraphUtils.to_pydot creates duplicated nodes in pydot.
here is a sample of resulting dot file, there are duplicated node info, though no trouble to plot with the output

digraph {
fontsize=18;
dpi=200;
0 [label=x1];
0 [label=x1];
1 [label=x2];
1 [label=x2];
2 [label=x3];
2 [label=x3];
3 [label=x4];
3 [label=x4];
...

The problem is with below function. The if else statement append nodes again.

    def to_pydot(G: Graph, edges: List[Edge] | None = None, labels: List[str] | None = None, title: str = "", dpi: float = 200):
        nodes = G.get_nodes()
        if labels is not None:
            assert len(labels) == len(nodes)

        pydot_g = pydot.Dot(title, graph_type="digraph", fontsize=18)
        pydot_g.obj_dict["attributes"]["dpi"] = dpi
        nodes = G.get_nodes()
        for i, node in enumerate(nodes):
            node_name = labels[i] if labels is not None else node.get_name()
            # first time append node into pydot
            pydot_g.add_node(pydot.Node(i, label=node.get_name()))
            if node.get_node_type() == NodeType.LATENT:
                pydot_g.add_node(pydot.Node(i, label=node_name, shape='square'))
            else: # this is the problem, then append again
                pydot_g.add_node(pydot.Node(i, label=node_name))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions