Search code examples
juliagraph-toollightgraphs

Import a graph (network) in Julia


Does anybody now if there is any module that could let me import a graph (network) in Julia?

Working with Python, I used the graph-tool package, which served me very well! I have my graphs in .gt file format. Can I use any module in Julia, so that I can import them there?

I have searched for LightGraphs and Junet, which is fairly new but cannot seem to see any "import" section in the documentation.


Solution

  • The most straightforward solution is to convert your gt files to graphml format, which is compatible with LightGraphs, and is the recommended alternative format by graph-tool.


    Suppose you have a ".gt" file that was generated in the past by the following python code:

    from graph_tool.all import *
    g = Graph()
    v1 = g.add_vertex()
    v2 = g.add_vertex()
    e = g.add_edge(v1,v2)
    g.save('g.gt')
    

    Start a new python session, and convert from "gt" to "graphml" format:

    import graph_tool as gt
    g = gt.Graph()
    g.load('g.gt')
    g.save('g.xml.gz')
    

    Then, in julia, use LightGraphs with the GraphIO package to load from the GraphML file:

    using LightGraphs, GraphIO
    D = loadgraphs("g.xml.gz", GraphMLFormat())
    #> Dict{String,LightGraphs.AbstractGraph} with 1 entry:
    #   "G" => {2, 1} directed simple Int64 graph
    

    If you'd like to use PyCall to perform the conversion directly from within julia (i.e. in a script), here's how:

    using PyCall
    @pyimport graph_tool as gt
    G = gt.Graph()
    G[:load]("g.gt")
    G[:save]("g.xml.gz")
    

    (Note that this implies python and the graph-tool library are already installed on your machine and accessible from julia).


    In theory, if you prefer graph-tools and you're used to its syntax and wish to keep working directly with the .gt file format, you can use it via PyCall from within julia throughout as above. Whether this is preferable to migrating over to LightGraphs which is designed for julia though is another thing. It's your call :)


    PS. Greetings from Leamington, fellow Leamingtonian!