# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Community Detection Example 

# Implement from this [site](https://python-louvain.readthedocs.io/en/latest/)
#
# Beware of install using `pip install python-louvain`

# ## Key Concepts:
#
# Maximum the modularity
#
# 1. Modularity
# For the disjoint sets, modularity propotional to the difference between (# of edegs in the group and the expected # edges inside the group)
#
# 2. Use Null model to get the expected # edges inside the group 
#
#
# [Source from Stanford CS224W 2019 Fall](http://snap.stanford.edu/class/cs224w-2019/slides/04-communities.pdf)
#

# Forom this example, the `karate_club_graph` from networkx is used as an graph example to demostarte the Louvain community detection method.
#
# 1. First load the `karate_club_graph`
# 2. Use `best_partition()` to find the best partition of the graph
# 3. Draw the graph using networkx's `draw_network_nodes` and `draw_network_edges`

import community as community_louvain
import matplotlib.cm as cm 
import matplotlib.pyplot as plt 
import networkx as nx  

# +
# load the karate club graph
G = nx.karate_club_graph()

# first compute the best partition
partition = community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)

# color the nodes based on the partitions
cmap = cm.get_cmap('viridis', max(partition.values())+1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=80,cmap=cmap, node_color=list(partition.values()))

nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.show()


# -

partition.keys()

partition.values()

G.nodes

G.edges

# ## Implement American Colledge Football Community

# +
import urllib.request as urllib
import io
import zipfile

import matplotlib.pyplot as plt
import networkx as nx

url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"

sock = urllib.urlopen(url)  # open URL
s = io.BytesIO(sock.read())  # read into BytesIO "file"
sock.close()

zf = zipfile.ZipFile(s)  # zipfile object
txt = zf.read("football.txt").decode()  # read info file
gml = zf.read("football.gml").decode()  # read gml data
# throw away bogus first line with # from mejn files
gml = gml.split("\n")[1:]
# -

G_ball = nx.parse_gml(gml)  # parse gml data

print(txt)

# +
for n, d in G_ball.degree():
    print(f"{n:20} {d:2}")

options = {
    "node_color": "black",
    "node_size": 50,
    "linewidths": 0,
    "width": 0.1,
}
nx.draw(G_ball, **options)
plt.show()
# -

# ### Implement commnity detection

partition = community_louvain.best_partition(G_ball)



# +
# draw the graph
pos = nx.spring_layout(G_ball)

# color the nodes based on the partitions
cmap = cm.get_cmap('viridis', max(partition.values())+1)
nx.draw_networkx_nodes(G_ball, pos, partition.keys(), node_size=40,cmap=cmap, node_color=list(partition.values()))

nx.draw_networkx_edges(G_ball, pos, alpha=0.5)
plt.show()
# -

set(partition.values())

from collections import defaultdict
group_dict = defaultdict(list)
for k,v in partition.items():
    group_dict[v].append(k)

group_dict



StarcoderdataJupyterScriptsDedupFiltered