Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bytes/String node name interaction with NX #13

Open
nicktimko opened this issue Aug 15, 2015 · 0 comments
Open

Bytes/String node name interaction with NX #13

nicktimko opened this issue Aug 15, 2015 · 0 comments

Comments

@nicktimko
Copy link

I'm trying to use this bootleg (:P, fix #12 ASAP!) package with NX 1.9.1 and Python 3 on Windows with GV 2.38 and when NX tries to read back in the position data, it coughs because of this line (errors a couple lines down when it tries to get the 0th element of an empty list) because it seems to think the nodes are named with bytes (py2 string), rather than unicode (py3 string). Without the encode it works (on Python 3 at least...can't easily switch to 2 on Windows)...

Aaanyways, I'm not sure if this should be fixed here or in NX. It's probably not been noticed/reported there because the "official" (#12...) pydot only supports Python 2.

Here's a monkeypatch that seems to work wherein I strip out the .encode()

import pydot
import networkx as nx
from networkx.drawing.nx_pydot import to_pydot, make_str
def pydot_layout(G,prog='neato',root=None, **kwds):
    """Create node positions using Pydot and Graphviz.
    Returns a dictionary of positions keyed by node.
    Examples
    --------
    >>> G=nx.complete_graph(4)
    >>> pos=nx.pydot_layout(G)
    >>> pos=nx.pydot_layout(G,prog='dot')
    """
    #pydot = load_pydot() #### EDIT (but not really important for this)

    P=to_pydot(G)
    if root is not None :
        P.set("root",make_str(root))

    D=P.create_dot(prog=prog)

    if D=="":  # no data returned
        print("Graphviz layout with %s failed"%(prog))
        print()
        print("To debug what happened try:")
        print("P=pydot_from_networkx(G)")
        print("P.write_dot(\"file.dot\")")
        print("And then run %s on file.dot"%(prog))
        #return

    Q=pydot.graph_from_dot_data(D)

    node_pos={}
    for n in G.nodes():
        ####################################################
        pydot_node = pydot.Node(make_str(n)).get_name()#.encode('utf-8') # no encode = fix
        ####################################################
        node=Q.get_node(pydot_node)

        if isinstance(node,list):
            node=node[0]
        pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
        if pos != None:
            xx,yy=pos.split(",")
            node_pos[n]=(float(xx),float(yy))
    return node_pos
nx.drawing.nx_pydot.pydot_layout = pydot_layout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant