"""
Davis Southern Club Women

Shows how to make unipartite projections of the graph and compute the
properties of those graphs.

These data were collected by Davis et al
in the 1930s.  They represent observed attendance at 14 social events
by 18 Southern women.  The graph is bipartite (clubs, women).

Data from:
http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm

"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
__date__ = "$Date: 2005-05-12 14:33:11 -0600 (Thu, 12 May 2005) $"
__credits__ = """"""
__revision__ = "$Revision: 998 $"
#    Copyright (C) 2004 by
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

def davis_club_graph(create_using=None, **kwds):
    nwomen=14
    nclubs=18
    G=nx.generators.empty_graph(nwomen+nclubs,create_using=create_using,**kwds)
    G.clear()
    G.name="Davis Southern Club Women"

    women="""\
EVELYN
LAURA
THERESA
BRENDA
CHARLOTTE
FRANCES
ELEANOR
PEARL
RUTH
VERNE
MYRNA
KATHERINE
SYLVIA
NORA
HELEN
DOROTHY
OLIVIA
FLORA"""

    clubs="""\
E1
E2
E3
E4
E5
E6
E7
E8
E9
E10
E11
E12
E13
E14"""

    davisdat="""\
1 1 1 1 1 1 0 1 1 0 0 0 0 0
1 1 1 0 1 1 1 1 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0 0 0 0 0
1 0 1 1 1 1 1 1 0 0 0 0 0 0
0 0 1 1 1 0 1 0 0 0 0 0 0 0
0 0 1 0 1 1 0 1 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 0 1 1 0 0 0 0 0
0 0 0 0 1 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 0 0 0 0 0 1 1 1 0 1 0 0
0 0 0 0 0 0 0 1 1 1 0 1 1 1
0 0 0 0 0 0 1 1 1 1 0 1 1 1
0 0 0 0 0 1 1 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 0 1 1 1 1 1
0 0 0 0 0 0 0 1 1 1 0 1 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0"""


    # women names
    w={}
    n=0
    for name in women.split('\n'):
        w[n]=name
        n+=1

    # club names
    c={}
    n=0
    for name in clubs.split('\n'):
        c[n]=name
        n+=1

    # parse matrix
    row=0
    for line in davisdat.split('\n'):
        thisrow=list(map(int,line.split(' ')))
        for col in range(0,len(thisrow)):
            if thisrow[col]==1:
                G.add_edge(w[row],c[col])
        row+=1
    return (G,list(w.values()),list(c.values()))

def project(B,pv,result=False,**kwds):
    """
    Returns a graph that is the unipartite projection of the
    bipartite graph B onto the set of nodes given in list pv.

    The nodes retain their names and are connected if they share a
    common node in the vertex set of {B not pv}.

    No attempt is made to verify that the input graph B is bipartite.
    """
    if result:
        G=result
    else:
        G=nx.Graph(**kwds)
    for v in pv:
        G.add_node(v)
        for cv in B.neighbors(v):
            G.add_edges_from([(v,u) for u in B.neighbors(cv)])
    return G

# return graph and women and clubs lists
(G,women,clubs)=davis_club_graph()

# project bipartite graph onto women nodes
W=project(G,women)
# project bipartite graph onto club nodes
C=project(G,clubs)

nx.draw_networkx(G,with_labels=True,pos=nx.circular_layout(G))

nx.draw_networkx(W,with_labels=True,pos=nx.circular_layout(W))

nx.draw_networkx(C,with_labels=True, pos=nx.circular_layout(C))

"""
Explanation: We will use the networkx code to contruct the graph
https://networkx.github.io/documentation/latest/examples/algorithms/davis_club.html
End of explanation
"""


for i in W.nodes_iter():
    if (len(W.neighbors(i))) != 18:
        print i + " --- " + str(len(W.neighbors(i)))

"""
Explanation: The Women's graph seems to imply that all of the women are connected to each other, that is that all of the women share atleast one group in common.  We can check this below:
End of explanation
"""


def createTable(graph):
    deg=nx.degree(graph)
    deg_cent=nx.degree_centrality(graph)
    bet_cent=nx.betweenness_centrality(graph)
    clo_cent=nx.closeness_centrality(graph)
    eig_cent = nx.eigenvector_centrality(graph)
    table=[[name,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 graph.nodes()]
    table=sorted(table,key=lambda x: -x[2])
    t = PrettyTable(['Name','Degree','Degree Cent','Betweenness','Closeness','EigenCent'])
    for i in range(0, len(table)):
        t.add_row(table[i])
    return t

print(createTable(W))

"""
Explanation: So not all the women are connected to each other, can we use centrality measures to see who is most/least important?
End of explanation
"""


print(createTable(C))

"""
Explanation: Due to self-loops, all names are connected to themselves.  We can also see that there are 7 women who are most important and 3 who are least important (in the sense of how many other women they possibly know).   Thus, if we wanted to get a message to all of the women, which club should we broadcast to assuming that the women will propagate the idea?
End of explanation
"""


def getneigh(W,G):
    for i in W.nodes_iter():
        print i + " --- " + str(len(G.neighbors(i))) + " --- " + str(G.neighbors(i))

getneigh(W,G)

getneigh(C,G)

"""
Explanation: With the clubs, we can see that clubs E6-E9 have members in common with all other clubs and thus have the greatest reach
End of explanation
"""


