Created
April 26, 2014 16:11
-
-
Save ohadle/11323991 to your computer and use it in GitHub Desktop.
Import graph from NetworkX JSON to Graphs.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using JSON | |
g_dict = open(JSON.parse, "/home/olevinkr/Documents/julia/nbrs_graph2.json") | |
using Graphs | |
#transfer all nodes to ExVertex types | |
new_vs = Array(ExVertex, length(g_dict["nodes"])) | |
i = 1 | |
for vertex in g_dict["nodes"] | |
new_vs[i] = ExVertex(i, vertex["id"]) | |
for (key, value) in vertex | |
if !(key == "id") | |
new_vs[i].attributes[key] = value | |
end | |
end | |
i += 1 | |
end | |
#transfer all edges to ExEdge types | |
#source/target indices in the "links" dicts are 0-based, while ExEdge indices are 1-based | |
new_es = Array(ExEdge{ExVertex}, length(g_dict["links"])) | |
i = 1 | |
for edge in g_dict["links"] | |
new_es[i] = ExEdge(i, new_vs[edge["source"]+1], new_vs[edge["target"]+1]) | |
for (key, value) in edge | |
if !(key in ("source", "target")) | |
new_es[i].attributes[key] = value | |
end | |
end | |
i += 1 | |
end | |
#create graph | |
g = graph(new_vs, new_es, is_directed=g_dict["directed"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment