Parser problem + fix
Snaicol opened this issue · 2 comments
I'm trying to plot a multilayer network using Py3plex. I've created a multiedge file using this setup:
n1 l1 n2 l2 0.4
n1 l3 n3 l1 1
From the file i can assure you:
- There are only 2 layers, called l1 and l2.
- There are no edges between the two layers
Actually i'm using this code:
from py3plex.visualization.multilayer import *
from py3plex.core import multinet
multilayer_network = multinet.multi_layer_network().load_network("multyL.txt", directed=True, input="multiedgelist")
multilayer_network.basic_stats()
multilayer_network.visualize_network(style="diagonal")
plt.show()
This is the result:
I know that this is wrong. I've done a reverse engineering on the library to find what could be the problem. I've found it was a parse error.
Since i'm using a multi_edgelist i've analized only the method i'm using. In line 141
in ".../py3plex/core/parser.py"
there is this method:
def parse_multi_edgelist(input_name,directed):
if directed:
G = nx.MultiDiGraph()
else:
G = nx.MultiGraph()
with open(input_name) as IN:
for line in IN:
node_first,layer_first,node_second,layer_second,weight = line.strip().split()
G.add_node((node_first,layer_first),type=layer_first)
G.add_node((node_second,layer_second),type=layer_second)
G.add_edge(node_first,node_second,weight=weight)
return (G,None)
I've tried to rewrite this method multiples times adding a print(G.nodes(data=True)
to see if I could fix it. I've notice these problems:
- self loops: if
l1 = l2_ and _n1 = n2
this code below is not working. I think there is a problem when it tries to add a second node((node_second,layer_second),type=layer_second)
since layer_first and layer_second are the same.
G.add_node((node_first,layer_first),type=layer_first)
G.add_node((node_second,layer_second),type=layer_second)
G.add_edge(node_first, node_first, weight=weight)
I think there is a problem when it tries to add a second node
((node_second,layer_second),type=layer_second)
since layer_first and layer_second are the
same. In this case the code is trying to do:
G.add_node((n1,l1),type=layer_first)
G.add_node((n1, l1),type=layer_second)
Since n1 = n2
it is trying to add the same node added before with a different type even if the layers are equals.
- If
l1 = l2_ and _n1 != n2
. I think the problem is always when it tries to add a second node((node_second,layer_second),type=layer_second)
since layer_first and layer_second are the same. In this case the code is trying to do:
G.add_node((n1,l1),type=layer_first)
G.add_node((n2, l1),type=layer_second)
I've modified this method to:
def parse_multi_edgelist(input_name,directed):
if directed:
G = nx.MultiDiGraph()
else:
G = nx.MultiGraph()
with open(input_name) as IN:
for line in IN:
node_first,layer_first,node_second,layer_second,weight = line.strip().split()
if layer_first == layer_second and node_first == node_second:
# first case
G.add_node((node_first, layer_first), type=layer_first)
elif layer_first == layer_second and node_first != node_second:
# second case
G.add_node((node_first, layer_first), type=layer_first)
G.add_node((node_second, layer_second), type=layer_first)
G.add_edge((node_first, layer_first), (node_second, layer_second), weight=weight)
else:
#default case
G.add_node((node_first, layer_first), type=layer_first)
G.add_node((node_second, layer_second), type=layer_second)
G.add_edge(node_first, node_second, weight=weight)
return (G,None)
With my code i've never used the third case yet. So i don't know if there are other cases. This is the correct plot:
There is still a problem: in the first case if you add G.add_edge(node_first, node_first, weight=weight)
it plots this:
This is the file i'm using.
multyL.txt
Hello @Snaicol !
Thanks for the extensive report, I see you've more or less solved the problem yourself! I adapted a bit your code and updated the library to a newer version ('0.65), the parser should work OK now.
Your example is now part of https://github.com/SkBlaz/Py3plex/blob/master/examples/example_multilayer_visualization.py
for reproducibility.
I've also fixed the self loop problem, it seems to work fine now (case 1), please report any additional problems!
Thanks!
If this solved the problem, you can close the issue.
Thanks!