-
-
Save anderser/7432419 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
import sys | |
import argparse | |
import networkx as nx | |
import community | |
from networkx.readwrite import json_graph | |
def graphmltojson(graphfile, outfile): | |
""" | |
Converts GraphML file to json while adding communities/modularity groups | |
using python-louvain. JSON output is usable with D3 force layout. | |
Usage: | |
>>> python convert.py -i mygraph.graphml -o outfile.json | |
""" | |
G = nx.read_graphml(graphfile) | |
#finds best community using louvain | |
partition = community.best_partition(G) | |
#adds partition/community number as attribute named 'modularitygroup' | |
for n,d in G.nodes_iter(data=True): | |
d['modularitygroup'] = partition[n] | |
node_link = json_graph.node_link_data(G) | |
json = json_graph.dumps(node_link) | |
# Write to file | |
fo = open(outfile, "w") | |
fo.write(json); | |
fo.close() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Convert from GraphML to json. ') | |
parser.add_argument('-i','--input', help='Input file name (graphml)',required=True) | |
parser.add_argument('-o','--output', help='Output file name/path',required=True) | |
args = parser.parse_args() | |
graphmltojson(args.input, args.output) |
To use the outputted D3 json, use an example like this: this http://bl.ocks.org/mbostock/4062045.
To color the nodes by modularity group use this instead of code in D3 sample:
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.modularitygroup); })
.call(force.drag);
how to use this for directed graph type? because i get error like "Bad graph type, use only non directed graph". Please assist.
Forked it here, was having trouble with the JSON dumper.
@Iam-jegan, go into your graphml file and change the <graph>
tag from <graph id="blah" edgedefault="directed">
to just <graph id="blah">
(obviously replace with your graph's ID). That should fix it!
I got the following error:
Traceback (most recent call last):
File "graphml2d3js.py", line 40, in <module>
graphmltojson(args.input, args.output)
File "graphml2d3js.py", line 21, in graphmltojson
partition = community.best_partition(G)
File "/home/djeb/.local/lib/python2.7/site-packages/community/community_louvain.py", line 214, in best_partition
randomize)
File "/home/djeb/.local/lib/python2.7/site-packages/community/community_louvain.py", line 293, in generate_dendrogram
status.init(current_graph, weight, part_init)
File "/home/djeb/.local/lib/python2.7/site-packages/community/community_status.py", line 57, in init
self.loops[node] = float(edge_data.get(weight, 1))
AttributeError: 'NoneType' object has no attribute 'get'
Any idea ? Thanks!
Line 28: output AttributeError: module 'networkx.readwrite.json_graph' has no attribute 'dumps'
Solution: import json
and use otherName_json = json.dumps(node_link)
. and don't forget to change the new name on line 32.
👍
Requires: https://bitbucket.org/taynaud/python-louvain and http://networkx.github.io/