infomap python draw code

What is infomap?

A library implemented that clusters into a modular structure with a nice feeling called Map equation.

For details, please refer to the following URL. https://www.mapequation.org/index.html https://mapequation.github.io/infomap/

problem

The sample code didn't work when using infomap.

https://github.com/mapequation/infomap/blob/master/examples/python/example-networkx.py

The cause seems to be that the code is old.

Resolution code

(Referenced site) https://mapequation.github.io/infomap/

example-networkx.py


import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.colors as colors

import infomap

"""
Generate and draw a network with NetworkX, colored
according to the community structure found by Infomap.
"""

def findCommunities(G):
	"""
	Partition network with the Infomap algorithm.
	Annotates nodes with 'community' id and return number of communities found.
	"""
	
	infomapWrapper = infomap.Infomap("--two-level --directed")

	print("Building Infomap network from a NetworkX graph...")
	for e in G.edges():
		infomapWrapper.addLink(*e)

	print("Find communities with Infomap...")
	infomapWrapper.run();

	


	communities = {}
	for node in infomapWrapper.iterTree():
		if node.isLeaf():
			communities[node.physicalId] = node.moduleIndex()

	nx.set_node_attributes(G, name='community', values=communities)
	return infomapWrapper.numTopModules()


def drawNetwork(G):
	# position map
	pos = nx.spring_layout(G)
	# community ids
	communities = [v for k,v in nx.get_node_attributes(G, 'community').items()]
	numCommunities = max(communities) + 1
	# color map from http://colorbrewer2.org/
	cmapLight = colors.ListedColormap(['#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f', '#cab2d6'], 'indexed', numCommunities)
	cmapDark = colors.ListedColormap(['#1f78b4', '#33a02c', '#e31a1c', '#ff7f00', '#6a3d9a'], 'indexed', numCommunities)

	# edges
	nx.draw_networkx_edges(G, pos)

	# nodes
	nodeCollection = nx.draw_networkx_nodes(G,
		pos = pos,
		node_color = communities,
		cmap = cmapLight
	)
	# set node border color to the darker shade
	darkColors = [cmapDark(v) for v in communities]
	nodeCollection.set_edgecolor(darkColors)

	# Print node labels separately instead
	for n in G.nodes():
		plt.annotate(n,
			xy = pos[n],
			textcoords = 'offset points',
			horizontalalignment = 'center',
			verticalalignment = 'center',
			xytext = [0, 2],
			color = cmapDark(communities[n])
		)

	plt.axis('off')
	# plt.savefig("karate.png ")
	plt.show()


G=nx.karate_club_graph()

findCommunities(G)

drawNetwork(G)

Finally

I would appreciate it if you could let me know if there are any mistakes.

Recommended Posts

infomap python draw code
python character code
[Python] Algorithm-aware code
Python code acceleration approach
Rewrite Python2 code to Python3 (2to3)
Before writing Python code
About Python3 character code
Python Requests status code
OpenCV basic code (python)
Draw graph in python
Get country code with python
Python with VS Code (Windows 10)
Draw mp3 waveform in Python
Draw netCDF file with python
Draw Poincare's disk in Python
Python code memo for yourself
[Python] Frequently used library code
Debug Python with VS Code
2.x, 3.x character code of python
Draw "Draw Ferns Programmatically" in Python
Stop Omxplayer from Python code
Draw implicit function in python
Python frequently used code snippets
Generate QR code in Python
[Python] Sample code for Python grammar
Draw a heart in Python
Character code learned in Python
Convert python 3.x code to python 2.x
Document Python code with Doxygen
Draw Sine Waves in Blender Python
Python hand play (argparse minimum code)
That Python code has no classes ...
Python
[Python] Generate QR code in memory
Draw knots interactively in Plotly (Python)
Automatically format Python code in Vim
Draw a scatterplot matrix in python
[Code] Module and Python version output
Write selenium test code in python
Draw Koch curve with Python Turtle
Execute Python code from C # GUI
Python parallel / parallel processing sample code summary
Check python code styles using pep8
Draw an illustration with Python + OpenCV
Draw a CNN diagram in Python
[Python] Read the Flask source code
Draw Lyapunov Fractal with Python, matplotlib
Code tests around time in Python
[VS Code] ~ Tips when using python ~
Install python with mac vs code
Draw arrows (vectors) with opencv / python
Installation of Visual studio code and installation of python
Fourier series verification code written in Python
python> link> strftime () and strptime () behavior / code
Write test-driven FizzBuzz code using Python doctest.
Getting Python source code metrics using radon
Draw a heart in Python Part 2 (SymPy)
[Python3] Rewrite the code object of the function
Enumerate duplicate combinations (C ++) → Add Python code
python> coding guide> PEP 0008 --Style Guide for Python Code
A tool for easily entering Python code