# ---
# 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
# ---

# +
import pandas
import networkx

import matplotlib.pyplot as plt

from operator import itemgetter
# -

graph_as_pdf = pandas.read_csv("./datasets/processed/enron_graph.tsv", sep="\t")
graph_as_pdf.groupby(by=["source", "target"]).count().reset_index()

# let's create a graph object 
# actually it should be a DiGraph object
eg = networkx.Graph()
for _, row in graph_as_pdf.iterrows():
    eg.add_edge(u=row["source"], v=row["target"], weight=row["weight"])

# decending degree sequence of nodes of the graph
degree_sequence = sorted((d for n, d in eg.degree()), reverse=True)
degree_sequence[0:10]

# max degree of the nodes
dmax = max(degree_sequence)

# get the greatest connexted component
gcc = sorted(networkx.connected_component_subgraphs(eg), key = len, reverse=True)[0]
len(gcc.nodes()), len(eg.nodes())

pos = networkx.spring_layout(gcc)

# let's draw the gcc
plt.figure(figsize=(13, 10))
networkx.draw_networkx_nodes(gcc, pos, node_size=5)
networkx.draw_networkx_edges(gcc, pos, alpha=0.4)
plt.show()

plt.figure(figsize=(13, 8))
plt.loglog(degree_sequence,'purple', marker='o')
plt.title("Degree rank plot")
plt.ylabel("vertex degree")
plt.xlabel("vertex rank")
plt.axes([0.45, 0.45, 0.45, 0.45])
plt.axis('off')
plt.show()

# dictionary containing each node and its degree
node_and_degree = dict(eg.degree())

# get the node with greatest degree
largest_hub, degree = sorted(node_and_degree.items(), key=itemgetter(1))[-1]

hub_ego_net = networkx.ego_graph(eg, largest_hub)

pos = networkx.spring_layout(hub_ego_net)

plt.figure(figsize=(5, 5))
networkx.draw(hub_ego_net, pos, node_color='green', node_size=30, with_labels=False)
networkx.draw_networkx_nodes(hub_ego_net, pos, nodelist=[largest_hub], node_size=300, node_color='red')
plt.show()

sorted(networkx.clustering(eg).items(), key=itemgetter(1))[-1]

# +
hub_ego_net = networkx.ego_graph(eg, "<EMAIL>")
pos = networkx.spectral_layout(hub_ego_net)

plt.figure(figsize=(8, 8))
networkx.draw(hub_ego_net, pos, node_color='pink', node_size=100, with_labels=True)
networkx.draw_networkx_nodes(hub_ego_net, pos, nodelist=["<EMAIL>"], 
                             node_size=200, node_color='red')
plt.show()
# -

shortest_path = networkx.shortest_path(eg, "<EMAIL>", "<EMAIL>")

# +
path = networkx.subgraph(eg, nbunch=shortest_path)
pos = networkx.spectral_layout(path)

plt.figure(figsize=(5, 5))
networkx.draw(path, pos, nodelist=path,  with_labels=True,
                             node_size=200, node_color='red')
plt.show()
# -

communities = []
for community in networkx.community.girvan_newman(eg):
    communities.append(community)

# +
step = 0
comms = communities[step]

ck = [community for community in comms if len(community) > 10 and len(community) < 20]

comm = networkx.subgraph(eg, nbunch=ck[0])
pos = networkx.spring_layout(comm)

plt.figure(figsize=(5, 5))
networkx.draw(comm, pos, node_color='red', node_size=200, with_labels=True)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered