I have downloaded the gml file which contains the dolphins social network.
Some time ago I did some analysis on that network running python 3.4 and networkx 1.9 on a a Windows7 machine, but now I am running on a Arch linux machine (with the same version of python but with networkx 1.10) and found an issue when tried to read the file.
This is the code used to read the file:
import networkx as nx
nx.read_gml("dolphins.gml")
And this is the stack trace of the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in read_gml
File "/usr/lib/python3.4/site-packages/networkx/utils/decorators.py",line 220, in _open_file
result = func(*new_args, **kwargs)
File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 210, in read_gml
G = parse_gml_lines(filter_lines(path), label, destringizer)
File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 383, in parse_gml_lines
graph = parse_graph()
File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 372, in parse_graph
curr_token, dct = parse_kv(next(tokens))
File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 323, in tokenize
(line[pos:], lineno + 1, pos + 1))
networkx.exception.NetworkXError: cannot tokenize 'graph' at (1, 1)
Are you able to read the file? Someone has experienced a simmilar issue? or knows what is generating the error?
Thank you in advance!
In the newer versions of networkx, the gml file should follow a more specific format. The problem with the dolphins.gml is that there should not be any carriage return before the open square brackets. For example:
Wrong format:
graph
[
directed 0
node
[
id 0
label "Beak"
]
.
.
.
Correct format:
graph [
directed 0
node [
id 0
label "Beak"
]
.
.
.
It does not care about how many spaces there are before the square bracket as long as there is more than one and there is no carriage return.
What I ended up doing was using regular expression to get rid of the white spaces before the opening square brackets. The following regex worked for me:
\s+\[
and just replace it with " [". There has to be at least one space before the bracket.
Also keep in mind that every node has to have a unique label.
Hope it helped.
It worked by downgrading the networkx version from 1.10 to 1.9.1.
Hope this answer can help someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With