G = nx.read_gml("polblogs.gml")

"""
Explanation: Data is political blogs linked to each other.  Most of these blogs links are dead
Lada A. Adamic and Natalie Glance, "The
political blogosphere and the 2004 US Election", in Proceedings of the
WWW-2005 Workshop on the Weblogging Ecosystem (2005)
First we will load the data and look at some of its properties
End of explanation
"""


G.node[345]

nx.is_directed(G)

G.number_of_edges()

G.number_of_nodes()

nx.is_strongly_connected(G)

nx.is_weakly_connected(G)

"""
Explanation: The label is the website and the value is 0 or 1 depending on whether the political blog is left or right leaning
End of explanation
"""


i=nx.weakly_connected_component_subgraphs(G)

l = max(i, key = len)

l.number_of_nodes()

nx.is_weakly_connected(l)

"""
Explanation: We will only concern ourselves with the largest weakly connected subgraph
End of explanation
"""


deg=nx.degree(G)

deg_cent=nx.degree_centrality(G)

deg_sort=sorted(deg_cent.iteritems(),key=lambda(k,v):(-v,k))

deg_sort[0:9]

"""
Explanation: First we will look at centrality at all the nodes in the Graph
Degree Centrality
End of explanation
"""


bet_cent=nx.betweenness_centrality(G)

bet_sort=sorted(bet_cent.iteritems(),key=lambda(k,v):(-v,k))

bet_sort[0:9]

"""
Explanation: Betweenness Centrality
End of explanation
"""


clo_cent=nx.closeness_centrality(G)

clo_sort=sorted(clo_cent.iteritems(),key=lambda(k,v):(-v,k))

clo_sort[0:9]

"""
Explanation: Closeness Centrality
End of explanation
"""


G2=nx.Graph(G)

eig_cent = nx.eigenvector_centrality(G2)
eig_sort = sorted(eig_cent.iteritems(), key = lambda(k,v):(-v,k))
eig_sort[0:9]

"""
Explanation: Eigenvector Centrality- We need to convert multigraph to graph
End of explanation
"""


names1=[x[0] for x in deg_sort[:10]]
names2=[x[0] for x in bet_sort[:10]]
names3=[x[0] for x in clo_sort[:10]]
names4=[x[0] for x in eig_sort[:10]]
names=list(set(names1)|set(names2)|set(names3)|set(names4))

table=[[name,G.node[name]['label'],G.node[name]['value'],deg[name],round(deg_cent[name],4),round(bet_cent[name],4),round(clo_cent[name],4),round(eig_cent[name],4)] for name in names]

table=sorted(table,key=lambda x: -x[3])

t = PrettyTable(['ID','Link','Value','Degree','Degree Cent','Betweenness','Closeness','EigenCent'])
for i in range(0, len(table)):
    t.add_row(table[i])

print(t)

"""
Explanation: Like the book, we shall look at a table of the blogs for the top measures
End of explanation
"""


nodes0=[]
nodes1=[]

for i,j in G.nodes_iter(data=True):
    if j['value']==0:
        nodes0.append(i)
    else:
        nodes1.append(i)

G_node0 = G.subgraph(nodes0)
G_node1 = G.subgraph(nodes1)

G_node0.number_of_nodes()

G_node0.number_of_edges()

G_node1.number_of_nodes()

G_node1.number_of_edges()

def createTable(graph):
    deg=nx.degree(graph)
    deg_cent=nx.degree_centrality(graph)
    deg_sort=sorted(deg_cent.iteritems(),key=lambda(k,v):(-v,k))
    bet_cent=nx.betweenness_centrality(graph)
    bet_sort=sorted(bet_cent.iteritems(),key=lambda(k,v):(-v,k))
    clo_cent=nx.closeness_centrality(graph)
    clo_sort=sorted(clo_cent.iteritems(),key=lambda(k,v):(-v,k))
    G2=nx.Graph(graph)
    eig_cent = nx.eigenvector_centrality(G2)
    eig_sort = sorted(eig_cent.iteritems(), key = lambda(k,v):(-v,k))
    names1=[x[0] for x in deg_sort[:10]]
    names2=[x[0] for x in bet_sort[:10]]
    names3=[x[0] for x in clo_sort[:10]]
    names4=[x[0] for x in eig_sort[:10]]
    names=list(set(names1)|set(names2)|set(names3)|set(names4))
    table=[[name,graph.node[name]['label'],graph.node[name]['value'],deg[name],round(deg_cent[name],4),round(bet_cent[name],4),round(clo_cent[name],4),round(eig_cent[name],4)] for name in names]
    table=sorted(table,key=lambda x: -x[3])
    t = PrettyTable(['ID','Link','Value','Degree','Degree Cent','Betweenness','Closeness','EigenCent'])
    for i in range(0, len(table)):
        t.add_row(table[i])
    return t

print(createTable(G_node0))

print(createTable(G_node1))

"""
Explanation: Now let us look at each of the left and right leaning groups
First we will create subgraphs for each of the groups
End of explanation
"""


def getDetails(G, deg, deg_cent, bet_cent, clo_cent, eig_cent, name):
    table=[[name,G.node[name]['label'],G.node[name]['value'],deg[name],round(deg_cent[name],4),round(bet_cent[name],4),round(clo_cent[name],4),round(eig_cent[name],4)]]
    table=sorted(table,key=lambda x: -x[3])
    t = PrettyTable(['ID','Link','Value','Degree','Degree Cent','Betweenness','Closeness','EigenCent'])
    for i in range(0, len(table)):
        t.add_row(table[i])
    return t

print(getDetails(G,deg,deg_cent,bet_cent,clo_cent,eig_cent,363))

print(getDetails(G,deg,deg_cent,bet_cent,clo_cent,eig_cent,1000))

"""
Explanation: It seems that the right leaning blogs have higher degree and closeness within themselves
Two blogs stick out, ID 363 and ID 1000 have relatively high degree and centralities within the respective 0 and 1 subgraphs, but are not in the overall top measurements for the whole graph
End of explanation
"""


def neighbor_details(G, node):
    count0 = 0
    count1 = 0
    for i in G.neighbors(node):
        if G.node[i]['value'] == 0:
            count0 +=1
        else:
            count1 +=1
    return [count0,count1]

neighbor_details(G, 363)

neighbor_details(G, 1000)

"""
Explanation: I'm interested to see the make up these nodes' neighbors
End of explanation
"""


from scipy import stats
import numpy as np

stats.ttest_ind((np.array([[deg_cent[name]] for name in nodes0])),(np.array([[deg_cent[name]] for name in nodes1])))

stats.ttest_ind((np.array([[bet_cent[name]] for name in nodes0])),(np.array([[bet_cent[name]] for name in nodes1])))

stats.ttest_ind((np.array([[clo_cent[name]] for name in nodes0])),(np.array([[clo_cent[name]] for name in nodes1])))

stats.ttest_ind((np.array([[eig_cent[name]] for name in nodes0])),(np.array([[eig_cent[name]] for name in nodes1])))

"""
Explanation: So each of these blogs are only connected within their respective groups and do not really link to the other group
Some t-tests:
End of explanation
"""


