~ubuntu-branches/ubuntu/trusty/python-networkx/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/ubigraph/karate_club_community.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-02-28 13:36:24 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090228133624-9l5rwi1ftlzc7b0l
* Upload to unstable now that lenny is released (yay).
* Fix FTBFS with python-support 0.90.3: no longer rely on its internal
  behaviour, and xnow set tests/test.py executable right after “setup.py
  install” (Closes: #517065).
* Drop executable bits from bz2 files.
* Update Vcs-* fields: move from DPMT's svn to collab-maint's git.
* Remote DPMT from Uploaders, following Piotr Ożarowski's request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
Communities in Zachary's Karate Club graph.
 
4
 
 
5
Double-click to expand and contract communities.
 
6
 
 
7
Data file from:
 
8
http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm
 
9
 
 
10
Reference:
 
11
Zachary W. (1977).
 
12
An information flow model for conflict and fission in small groups.
 
13
Journal of Anthropological Research, 33, 452-473.
 
14
 
 
15
"""
 
16
__author__ = """Aric Hagberg (hagberg@lanl.gov)\n Katy Bold (kbold@princeton.edu"""
 
17
#    Copyright (C) 2008 by 
 
18
#    Aric Hagberg <hagberg@lanl.gov>
 
19
#    Dan Schult <dschult@colgate.edu>
 
20
#    Pieter Swart <swart@lanl.gov>
 
21
#    Distributed under the terms of the GNU Lesser General Public License
 
22
#    http://www.gnu.org/copyleft/lesser.html
 
23
 
 
24
import string
 
25
 
 
26
def karate_graph(create_using=None, **kwds):
 
27
    from networkx.generators.classic import empty_graph
 
28
 
 
29
    G=empty_graph(34,create_using=create_using,**kwds)
 
30
    G.name="Zachary's Karate Club"
 
31
 
 
32
    zacharydat="""\
 
33
0 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0
 
34
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0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1
 
63
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
 
64
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1
 
65
0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1 0 1
 
66
0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0"""
 
67
 
 
68
 
 
69
    row=0
 
70
    for line in string.split(zacharydat,'\n'):
 
71
        thisrow=map(int,string.split(line,' '))
 
72
        for col in range(0,len(thisrow)):
 
73
            if thisrow[col]==1:
 
74
                G.add_edge(row,col) # col goes from 0,33
 
75
        row+=1
 
76
    return G
 
77
 
 
78
def contract_community(G,K,community):
 
79
    G.add_node(community['label'],color=community['color'],size='2.0')
 
80
    for node in community['nodes']:
 
81
        for nbr in G.neighbors(node):
 
82
            if nbr not in community['nodes']:
 
83
                G.add_edge(community['label'],nbr)
 
84
        G.delete_node(node)
 
85
 
 
86
 
 
87
def expand_community(G,K,community):
 
88
    G.delete_node(community['label'])
 
89
    for node in community['nodes']:
 
90
        G.add_node(node,color=community['color'])
 
91
    for node in community['nodes']:
 
92
        for nbr in K.neighbors(node):
 
93
            if nbr in G:
 
94
                G.add_edge(node,nbr)
 
95
            else:
 
96
                G.add_edge(node,node_map[nbr])
 
97
 
 
98
 
 
99
def get_communities():
 
100
    # assign communities and some properties
 
101
    communities={}
 
102
    communities['c1']={}
 
103
    communities['c2']={}
 
104
    communities['c3']={}
 
105
    communities['c4']={}
 
106
    communities['c1']['nodes']=[0, 1, 2, 3, 7, 11, 12, 13, 17, 19, 21]
 
107
    communities['c2']['nodes']=[4, 5, 6, 10, 16]
 
108
    communities['c3']['nodes']=[8, 9, 14, 15, 18, 20, 22, 26, 29, 30, 32, 33]
 
109
    communities['c4']['nodes']=[23, 24, 25, 27, 28, 31]
 
110
    communities['c1']['color']='#ff0000'
 
111
    communities['c2']['color']='#00ff00'
 
112
    communities['c3']['color']='#0000ff'
 
113
    communities['c4']['color']='#afafaf'
 
114
    communities['c1']['label']='c1'
 
115
    communities['c2']['label']='c2'
 
116
    communities['c3']['label']='c3'
 
117
    communities['c4']['label']='c4'
 
118
 
 
119
    node_map={}
 
120
    for label,c in communities.items():
 
121
        for n in c['nodes']:
 
122
            node_map[n]=label
 
123
    return communities,node_map
 
124
 
 
125
 
 
126
def vertex_callback(id):
 
127
    import sys
 
128
    try:
 
129
        n=G.idnode[id]
 
130
        if n in node_map:
 
131
            contract_community(G,K,communities[node_map[n]])
 
132
        elif n in communities:
 
133
            expand_community(G,K,communities[n])
 
134
    except:
 
135
        return -1
 
136
    return 0
 
137
 
 
138
 
 
139
 
 
140
if __name__ == "__main__":
 
141
    import random
 
142
    import networkx as nx
 
143
    K=karate_graph()
 
144
    G=nx.UbiGraph(K)
 
145
    G.node_labels()
 
146
    communities,node_map=get_communities()
 
147
    for key,c in communities.items():
 
148
        G.set_node_attr(c['nodes'],color=c['color'])
 
149
 
 
150
 
 
151
    print "Double-click to expand and contract communities."
 
152
 
 
153
    # call back server        
 
154
    myPort = random.randint(20739,20999)
 
155
    # Set up a callback for left double-clicks on vertices.
 
156
    G.ubigraph.set_vertex_style_attribute(0, "callback_left_doubleclick", 
 
157
                "http://127.0.0.1:" + str(myPort) + "/vertex_callback")
 
158
 
 
159
    # Now make an XMLRPC server to handle tha callbacks.
 
160
    from SimpleXMLRPCServer import SimpleXMLRPCServer
 
161
    # Create server
 
162
    server = SimpleXMLRPCServer(("localhost", myPort))
 
163
    server.register_introspection_functions()
 
164
    server.register_function(vertex_callback)
 
165
    # Run the server's main loop
 
166
    print "Listening for callbacks from ubigraph_server on port " + str(myPort)
 
167
    server.serve_forever()