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

Edge width from networkx #82

Open
VolodyaCO opened this issue Feb 3, 2021 · 7 comments
Open

Edge width from networkx #82

VolodyaCO opened this issue Feb 3, 2021 · 7 comments

Comments

@VolodyaCO
Copy link

I'm trying to plot a networkx graph using pyvis version 0.1.8.2.

However, the weight edge attribute is taken from networkx to pyvis, but it does not have any relevance in the pyvis visualisation. The idea is that the width of each edge corresponds to the weight of networkx. The from_nx method assigns the weight attribute from networkx to a weight attribute in the pyvis Network. But the visualisation fails to plot the width of the edges using the information of the weight attribute:

>>> from pyvis.network  import Network
>>> net1 = Network()
>>> edges = [(0, 1, 3), (0, 2, 1)]
>>> for edge in edges:
...     net1.add_node(edge[0])
...     net1.add_node(edge[1])
...     net1.add_edge(edge[0], edge[1], weight=edge[2])
>>> net1.show("nx.html")

image

Instead, if I use value to indicate this weight, the plot is done correctly:

>>> from pyvis.network  import Network
>>> net1 = Network()
>>> edges = [(0, 1, 3), (0, 2, 1)]
>>> for edge in edges:
...     net1.add_node(edge[0])
...     net1.add_node(edge[1])
...     net1.add_edge(edge[0], edge[1], value=edge[2])
>>> net1.show("nx.html")

image

Since from_nx is written to accept weight as the canonical connection weight, when drawing the graph, it should plot the width of the edges using the information of weight instead of value. The other option is to not mess with jinja's Template, and hand-code from_nx to transform the name weight into value.

@VolodyaCO
Copy link
Author

For the moment, I hard coded weight into value. The following can be inserted into the from_nx method:

def nx2pyvis(nx_graph, pyvisnet, default_node_size=1, default_edge_weight=1):
    """
    Converts a NetworkX graph into a Pyvis Network
    This replaces the Network.from_nx method, which fails
    at plotting correctly edge width, as indicated in
    https://github.com/WestHealth/pyvis/issues/82
    """
    assert(isinstance(nx_graph, nx.Graph))
    edges = nx_graph.edges(data=True)
    nodes = nx_graph.nodes(data=True)

    if len(edges) > 0:
        for e in edges:
            if 'size' not in nodes[e[0]].keys():
                nodes[e[0]]['size'] = default_node_size
            nodes[e[0]]['size'] = int(nodes[e[0]]['size'])
            if 'size' not in nodes[e[1]].keys():
                nodes[e[1]]['size'] = default_node_size
            nodes[e[1]]['size'] = int(nodes[e[1]]['size'])
            pyvisnet.add_node(e[0], **nodes[e[0]])
            pyvisnet.add_node(e[1], **nodes[e[1]])

            if 'weight' not in e[2].keys():
                e[2]['weight'] = default_edge_weight
            e[2]['weight'] = e[2]['weight']
            edge_dict = e[2]
            edge_dict["value"] = edge_dict.pop("weight")
            pyvisnet.add_edge(e[0], e[1], **edge_dict)

    for node in nx.isolates(nx_graph):
        if 'size' not in nodes[node].keys():
            nodes[node]['size']=default_node_size
        pyvisnet.add_node(node, **nodes[node])

If you want me to, I can create a PR to address this.

@boludo00
Copy link
Collaborator

boludo00 commented Apr 29, 2021

You're right in that from_nx assumes weight is the valid name for assigning edge thickness. This should not be the case as VisJS documentation states that either value or width are valid arguments to handle the weight. To not confuse things in understanding the VisJS layer it's probably best to keep to their notation and use their argument names. So in this case, modifying from_nx to use width or value.

@jhunpingco
Copy link
Contributor

@VolodyaCO Can you submit a pull request for this?

@olive004
Copy link
Contributor

Is there also a straightforward way to toggle wether the numerical weights of the edges are shown when imported from Networkx?

@olive004
Copy link
Contributor

olive004 commented Dec 23, 2021

Nvm you can show the numerical weights of the edges by adding e[2]["label"] = e[2]["weight"] into the from_nx() function

@VolodyaCO
Copy link
Author

@jhunpingco isn't this already handled by @boludo00 commit 4c8071f?

@freytheviking
Copy link

You're right in that from_nx assumes weight is the valid name for assigning edge thickness. This should not be the case as VisJS documentation states that either value or width are valid arguments to handle the weight. To not confuse things in understanding the VisJS layer it's probably best to keep to their notation and use their argument names. So in this case, modifying from_nx to use width or value.

Thanks. This worked for me when I replaced the from_nx method with my own.

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

5 participants