Skip to content

Instantly share code, notes, and snippets.

@diije
Created May 1, 2018 12:43
Show Gist options
  • Save diije/193ec57d7e1347db91c52699c4f08ccd to your computer and use it in GitHub Desktop.
Save diije/193ec57d7e1347db91c52699c4f08ccd to your computer and use it in GitHub Desktop.
Converting from Networkx to Python-Igraph
import networkx as nx
Gnx = nx.path_graph(4) # Create a random NX graph
nx.write_graphml(G,'graph.graphml') # Export NX graph to file
import igraph as ig
Gix = ig.read('graph.graphml',format="graphml") # Create new IG graph from file
@diije
Copy link
Author

diije commented May 1, 2018

Most straigt-forward method in my experience: simply write your graph to a file, then read it back.

@parth-verma
Copy link

So stupid. What happens when the graph has millions of nodes, this would take quite a lot of time in that case

@diije
Copy link
Author

diije commented May 26, 2020

@parth-verma thank you for such a nice comment!
In my experience of working with the kind of volume you mention, that's quicker and requires less RAM than other methods I tried. But please feel free to share a better way to do this.

@thepunitsingh
Copy link

I applied your solution, and it looks like igraph currently do not support GraphML. Please check the below error.

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-0d19e71fcb45> in <module>
----> 1 G = ig.read('thesaurus_com_syn_graph_maincomponent.graphml',format="graphml")

~\anaconda3\lib\site-packages\igraph\__init__.py in read(filename, *args, **kwds)
   4771     @param filename: the name of the file to be loaded
   4772     """
-> 4773     return Graph.Read(filename, *args, **kwds)
   4774 load=read
   4775 

~\anaconda3\lib\site-packages\igraph\__init__.py in Read(klass, f, format, *args, **kwds)
   2621             raise IOError("no reader method for file format: %s" % str(format))
   2622         reader = getattr(klass, reader)
-> 2623         return reader(f, *args, **kwds)
   2624     Load = Read
   2625 

NotImplementedError: Error at c:\users\vssadministrator\appdata\local\temp\pip-req-build-f_aw61lh\vendor\build\igraph\igraph-0.8.3-msvc\src\foreign-graphml.c:1446: GraphML support is disabled, Unimplemented function call

@diije
Copy link
Author

diije commented Dec 17, 2020

@thepunitsingh igraph lib might have evolved a bit since I published this gist. Check the docs: https://igraph.org/python/doc/igraph.GraphBase-class.html#Read_GraphML

@JIBarrionuevoGaltier
Copy link

Great! Your solution help me! Thank!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment