Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert from NodeXL (via GraphML-format) to D3-ready json for force layout while adding modularity groups (communities) as attribute to nodes. Useful for coloring nodes via modularitygroup attribute. Requires networkx and python-louvain. First export as GraphML file from your NodeXL-sheet. Then run: >>> python convert.py -i mygraph.graphml -o ou…
#!/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)
@anderser

This comment has been minimized.

Copy link
Owner Author

commented Nov 12, 2013

@anderser

This comment has been minimized.

Copy link
Owner Author

commented Nov 13, 2013

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);
@Iam-jegan

This comment has been minimized.

Copy link

commented Nov 6, 2015

how to use this for directed graph type? because i get error like "Bad graph type, use only non directed graph". Please assist.

@j6k4m8

This comment has been minimized.

Copy link

commented Jun 15, 2016

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!

@unvaiso

This comment has been minimized.

Copy link

commented Jul 2, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.