-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_comm_CK_show.py
167 lines (128 loc) · 13.9 KB
/
block_comm_CK_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
community-new_CK_show_sortact.py
Created by Satyam Mukherjee on 2011-03-04.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
Plot the communities with different shapes. This is a quick way to visualize the blocks of the co-classification matrix. Color the nodes according to the average weight-loss of the communities. The size of the node is proportional to the number of internal nodes. The thickness of the link is proportional to the number of links between two communities.
"""
import sys
import os
import networkx as nx
import matplotlib.pyplot as plt
import string
import random
import numpy as np
from scipy.stats import *
from matplotlib.colors import LogNorm
from operator import itemgetter
from math import pi,sin,cos,sqrt
import pylab
from matplotlib.patches import Wedge, Polygon
import matplotlib.ticker as ticker
from matplotlib.widgets import Slider, Button
graph_name = "friend_graph_all0_2010.gml"
G = nx.read_gml(graph_name)
listsort = [[15726, 1440], [40402, 998], [28436, 42484], [46622, 45486, 40466], [12484, 23266, 32662], [18026, 16514, 33973, 46930], [42334, 13954, 43013, 6218], [34754, 181, 17097, 18656], [20625, 36585, 38954, 38714], [39953, 33875, 39600, 46451, 45064], [41015, 40802, 20411, 28516, 13731], [39111, 36701, 40854, 46315, 22593, 614, 6883, 7264, 32700, 43541], [29930, 42886, 36157, 41656, 39220, 45298, 43969, 36124, 44265, 36411, 41613, 46441, 18997, 46998, 46655, 30166], [32571, 46326, 6197, 36925, 43434, 35493, 41292, 32108, 28050, 40734, 46798, 10378, 5817, 42467, 42225, 28301, 30898, 27776, 33372, 44214, 43573, 37258, 10915, 39130, 21010, 45655, 43282, 43435, 3424, 44195, 38629, 10411, 33161, 29040, 13662, 30372, 45303, 45833, 15389, 6322, 43402, 8997, 40844, 36865, 47052, 32833, 29826, 46148, 9486, 35766, 46162, 19708, 46118, 46878, 41316, 47067, 29415, 10924, 39816, 4784, 6497, 19546, 3944, 2832, 20312, 45116, 43927], [44003, 41792, 28851, 30000, 38315, 29371, 41890, 19939, 13739, 44121, 42585, 40284, 30264, 45860, 33075, 47053, 37678, 35059, 46812, 40520, 41553, 41480, 9816, 38955, 3217, 36977, 43192, 44546, 43294, 32214, 3719, 45233, 19311, 39372, 45419, 45379, 37747, 22211, 46785, 45154, 41049, 43252, 27186, 41899, 45243, 39770, 29874, 45557, 12977, 45037, 3144, 21654, 38900, 40824, 31353, 44757, 42317, 40079, 19943, 33062, 43810, 28981, 3557, 36088, 43858, 42464, 32328, 18654, 32811, 31124, 41165, 47030, 42810, 45437, 46269, 39252, 45175, 40443, 19022, 45459, 15311, 38029, 39706], [42911, 42270, 43821, 12414, 45758, 46480, 46186, 40603, 18956, 36529, 28945, 46959, 29840, 39809, 40247, 34775, 19515, 41906, 30359, 28308, 44021, 2697, 40455, 29881, 42510, 40842, 44881, 20972, 22783, 19501, 7073, 7330, 31246, 2908, 10542, 12992, 12252, 21395, 42371, 39714, 27847, 38044, 39018, 43951, 36351, 12048, 27171, 13518, 14722, 5842, 10127, 33242, 45670, 39201, 2362, 19136, 6933, 32005, 833, 36783, 45392, 27995, 24575, 39539, 39823, 31461, 11729, 42190, 30050, 31761, 40582, 34107, 35309, 43334, 45795, 36237, 46600, 3326, 41720, 6390, 29250, 27127, 41986, 21215, 38641, 32340, 29802, 6137], [11926, 34579, 20392, 14376, 12013, 37600, 39478, 1250, 43617, 45799, 19450, 45583, 310, 43150, 44570, 46533, 43020, 27345, 17484, 45220, 4030, 14378, 20527, 37781, 39422, 17755, 12449, 24629, 40811, 45784, 46384, 31076, 27759, 33369, 46026, 967, 13228, 33964, 37143, 41371, 16567, 46230, 21994, 9813, 20327, 24308, 33340, 16584, 19, 39055, 17258, 21108, 2464, 42000, 26102, 32508, 47063, 26528, 39526, 33422, 32116, 38914, 8034, 40545, 27811, 25991, 39043, 39573, 36268, 27933, 12486, 42018, 38479, 29399, 35561, 44682, 8487, 3864, 2915, 18647, 15479, 41728, 40218, 8638, 34141, 4597, 45513, 46601, 41401, 30067, 17369, 37574, 17598, 29314, 47046, 35973, 27101, 29592, 4204, 18790, 46234, 38798, 39438, 27724, 9429, 43542, 39017, 42716, 41261, 11477, 36915, 15611, 23012, 41242, 46121, 18531, 35135, 1432, 33322, 14142, 36330, 259, 7077, 27385, 42108, 39065, 42419, 38101, 24962, 39167, 7786, 38214, 17923, 42263, 40296, 42401, 39948, 45760, 4655, 33282, 4336, 40232, 43083, 42927, 2959, 17480, 46329, 12115, 11149, 45139, 37188, 17458, 42087, 41521, 25619, 41797, 41999, 3869, 33717, 40229, 18061, 45527, 41942, 39313, 5242, 29522, 16735, 6083, 42150, 40161, 15373, 14026, 38481, 15367, 46496, 41584, 10684, 40052, 41203, 8371, 24180, 26136, 45261, 33895, 43318, 27711, 47038, 31513, 29472, 46046, 41510, 391, 32863, 16541, 27851, 6568, 12798, 24642, 32395, 727, 20494, 9655, 42319, 37578, 18967, 44334, 27511, 13790, 4455, 13803, 45676, 11427, 44866, 35955, 22711, 44169, 36469, 34955, 30300, 25318, 31305, 32683, 36176, 41063, 17857, 44213, 18106, 46668, 14867, 29540], [42637, 46689, 38945, 43599, 29284, 34450, 46123, 45933, 25386, 42608, 6627, 39978, 41559, 38407, 44791, 39003, 34752, 39485, 35930, 29418, 39019, 47019, 19000, 42156, 5068, 3815, 43401, 39686, 43728, 42971, 43348, 28182, 41781, 40666, 43607, 7226, 46771, 42305, 20347, 32167, 39862, 41435, 39059, 39625, 41970, 29115, 43437, 25486, 31391, 41509, 28627, 47043, 28845, 42471, 4477, 33985, 46819, 31826, 28506, 8592, 43982, 41187, 12743, 46850, 43373, 26751, 27575, 39898, 45148, 46803, 9831, 32171, 11613, 40782, 46613, 5286, 34065, 42399, 32741, 32085, 44188, 43442, 28391, 44884, 34061, 42574, 45190, 28342, 42065, 42240, 44291, 6161, 22903, 44183, 31021, 20377, 18541, 42146, 39808, 36990, 34064, 37639, 46782, 30244, 45061, 18842, 35274, 42022, 41092, 36631, 17989, 45916, 45506, 32476, 46726, 10959, 39022, 42550, 4800, 45009, 39207, 36115, 27958, 20206, 37000, 28118, 39155, 46389, 31949, 41987, 9590, 39383, 32349, 46941, 45443, 45788, 43178, 41851, 31547, 42418, 28688, 31383, 41847, 44636, 44352, 44662, 215, 1055, 29453, 37287, 80, 31954, 29018, 43543, 46153, 40394, 45528, 38164, 5528, 3284, 42380, 45470, 13511, 40656, 40434, 14977, 33441, 39673, 46439, 46365, 29480, 46062, 43084, 46772, 30570, 46347, 5820, 4046, 3753, 42262, 46254, 27973, 35847, 43981, 41769, 39688, 41131, 46397, 44554, 43446, 35839, 42807, 3314, 36917, 45674, 46487, 12681, 45411, 40324, 47061, 45511, 46721, 41310, 28640, 45374, 18992, 30964, 5564, 44449, 28212, 43572, 42612, 40539, 42457, 40654, 44539, 28169, 40188, 41160, 33425, 42825, 22520, 5613, 44349, 44571, 29376, 5536, 45922, 46042, 46549, 41586, 4323, 43848, 40393, 5580, 5295, 41794, 29070, 42358, 46271, 40027, 2573, 32915, 3240, 36369, 46639, 30392, 36591, 26900, 20553, 29749, 44614, 41669, 42384, 42348, 27756, 36222, 41745, 31337, 40653, 29467, 11313, 17086, 46806, 31425, 4094, 45903, 3940, 32637, 2158, 30917, 45368, 41830, 35544, 17427, 28719, 39690, 37648, 39250, 43953, 37763, 41225, 44263, 36629, 32242], [41069, 29247, 43695, 42993, 43210, 21860, 45050, 45702, 2693, 29662, 42381, 28383, 1067, 40958, 43394, 43911, 29917, 27342, 44139, 40596, 43948, 3445, 37041, 5738, 30395, 44166, 44030, 42777, 43009, 38924, 46478, 6578, 40102, 33143, 39301, 38972, 27209, 13719, 27137, 44111, 39728, 36991, 28473, 38052, 27983, 43778, 44221, 30058, 39006, 28775, 41698, 19268, 38121, 36543, 35201, 37976, 46582, 27582, 36071, 29272, 44521, 5705, 41152, 5139, 45049, 40271, 29527, 45330, 45342, 41724, 39129, 43041, 47072, 41693, 41675, 44831, 46824, 44385, 31585, 5739, 35886, 4185, 42559, 21754, 46453, 8316, 43142, 46345, 35944, 43205, 45831, 40133, 46359, 42747, 2112, 41291, 43603, 28890, 7311, 44527, 39411, 38941, 32337, 42009, 31137, 45861, 43144, 45099, 7519, 28601, 43053, 44060, 33665, 29058, 35531, 5131, 30803, 42054, 45573, 40935, 40999, 5513, 42090, 37751, 41895, 43941, 40501, 45508, 45704, 43403, 40205, 28414, 11285, 46599, 36472, 40146, 34227, 4651, 39189, 28001, 43674, 32164, 42714, 29861, 38953, 18163, 44054, 28838, 32892, 32968, 4699, 30776, 41067, 39182, 26179, 27523, 35812, 45712, 38932, 14901, 39364, 36312, 27396, 44353, 33489, 29495, 39836, 39971, 43612, 28882, 40680, 31487, 34217, 36376, 12821, 40338, 41402, 46250, 43499, 39444, 44663, 32039, 35044, 29528, 29033, 45771, 19063, 28305, 31778, 34771, 40821, 20427, 44855, 46621, 46037, 33960, 45150, 33729, 19096, 39707, 42947, 36092, 46020, 2469, 36819, 42434, 5965, 27132, 45983, 42849, 46881, 27836, 45545, 27280, 43213, 26718, 46363, 41689, 39604, 36726, 33436, 27978, 14694, 37755, 46605, 16722, 38893, 6758, 33493, 37065, 45584, 42636, 41419, 46891, 37424, 28854, 46464, 46205, 35755, 43662, 42241, 42207, 4916, 2041, 7258, 2639, 34221, 39768, 26933, 27976, 44483, 39342, 43238, 36719, 41095, 44916, 31717, 28743, 46507, 45641, 39567, 44018, 3989, 18462, 40478, 41059, 37115, 45497, 42432, 27284, 28808, 42589, 36332, 42180, 16058, 40803, 42116, 37030, 45556, 30477, 40733, 43307, 34841, 44105, 45364, 34977, 29988, 43813, 29053, 39120, 40252, 42053, 22923, 41331, 42179, 42259, 43177, 45476, 41194, 39400, 42694, 22613, 27330, 30051, 31044, 14475, 41929, 40627, 40155, 27798, 13136, 12882, 35551, 43884, 12375, 45173, 46921, 43902, 45288, 44212, 42227, 41081, 3368, 30032, 42853, 36473, 28335, 39624, 34270, 28361, 40853, 28022, 28035, 4393, 46029, 28387, 42035, 44305, 31384, 45902, 45772, 37617, 43717, 30154, 37138, 31574, 46073, 43206, 41886, 35849, 31799, 33549, 32397, 40230, 6025, 45565, 41508, 5960, 40786, 39353, 7408, 30736, 45627, 44835, 45815, 45656, 27574, 39032, 46663, 43635, 34110, 33946, 1930, 47060, 45610, 42914, 28850, 46585, 43112, 3159, 43698, 41807, 18799, 39656, 43762, 6613, 4331, 35305, 46643, 43854, 37111, 20418, 40373, 42678, 7129, 37874, 39402, 2552, 36343, 32533, 42610, 36255, 45966, 35819, 7189, 41479, 5470, 41790, 25458, 46412, 44037, 28664, 4993, 43995, 40597, 43462, 43865, 11864, 36308, 42445, 3041, 27854, 41736, 41021, 46612, 4342, 45435, 20059, 32233, 36611, 45160, 29086, 44709, 29962, 2960, 42527, 37378, 40235, 44258, 39224, 27489, 37228, 28793, 30761, 37683, 28309, 46915, 45941, 6651, 43812, 17578, 38959, 39222, 43807, 42063, 33460, 36992, 41248, 40138, 44220, 45569, 27605, 36526, 31261, 45876, 33530, 44052, 41561, 29476, 44407, 40123, 39442, 31151, 19701, 46180, 31566, 2536, 35280, 40847, 43890, 34175, 4044, 31067, 43295, 41369, 41374, 5369, 30192, 40484, 29531, 44199, 44377, 4849, 30344, 45060, 28404, 2901, 37027, 25688, 42226, 22594, 42584, 34341, 31006, 40777, 32070, 42925, 27610, 40476, 40613, 44184, 45279, 46809, 30741, 32426, 37380, 30565, 23782, 39693, 35379, 43490, 27301, 46281, 41680, 40037, 45895, 39361, 40929, 43818, 16468, 40692, 30769, 30951, 39152, 46820, 43581, 43740, 43461, 40073, 6458, 46119, 45923, 42590, 41650, 22238, 45265, 859, 19256, 31965, 30246, 40719, 19476, 42765, 26710, 38271, 39044, 42249, 42160, 42877, 37849, 33235, 27490, 39811, 45077, 43715, 26602, 40400, 46571, 45422, 43138, 39834, 41140, 41373, 44103, 29031, 40939, 42029, 44158, 43582]]
#sorted(listcom,key=len)
#print listcom
H=nx.Graph()
#H=nx.Graph()
ynode = []
for com in listsort :
xnode = []
for comnodes in com :
for node in G.nodes(data=True) :
if int(node[1]['label']) == int(comnodes) :
xnode.append(int(node[1]['id']))
xnode.sort()
bunch = [int(node[1]['id'])]+G.neighbors(int(node[1]['id']))
Gprime = G.subgraph(bunch)
H.add_edges_from(Gprime.edges())
ynode.append(xnode)
BM=nx.blockmodel(H,ynode)
#pos=nx.circular_layout(H)
pos = nx.graphviz_layout(H,prog='twopi')
#pos=nx.spring_layout(H, iterations = 50, weighted = False)
fig = plt.figure(figsize=(10,10))
ax = fig.add_axes((0.0,0.0,1.0,1.0))
nshells=len(ynode)
listcomattr = []
weightcom = []
comact = []
bmicom = []
for s in range(nshells) :
weightloss = 0
bmichange = 0
for n in ynode[s] :
weightloss += float(G.node[n]['weight_change'])
comact.append(float(G.node[n]['activity']))
bmichange += float(float(G.node[n]['final_BMI']) - float(G.node[n]['initial_BMI']))
listcomattr.append((s, float(weightloss)*1.0/len(ynode[s])))
weightcom.append(float(weightloss)*1.0/len(ynode[s]))
bmicom.append(float(bmichange)*1.0/len(ynode[s]))
maxwtl = max(bmicom)
minwtl = min(bmicom)
color_node = []
for colors in bmicom :
color_node.append(plt.cm.jet(float(colors+abs(minwtl))/(maxwtl+abs(minwtl))))
#node_size=[BM.node[x]['nnodes']*1.25 for x in BM.nodes()]
node_size = []
for x in BM.nodes() :
if BM.node[x]['nnodes'] >= 10 :
node_size.append(BM.node[x]['nnodes']*1.25)
if BM.node[x]['nnodes'] < 10 :
node_size.append(BM.node[x]['nnodes']*20)
#edge_width=[(10.*d['weight']*1./293.) for (u,v,d) in BM.edges(data=True)]
edge_width = []
for (u,v,d) in BM.edges(data=True) :
if d['weight'] > 5 :
edge_width.append(15.*d['weight']*1./293.)
if d['weight'] >= 1 :
edge_width.append(d['weight']*1.75)
edge_width.sort()
edgepos = []
for (u,v,d) in BM.edges(data=True) :
edgepos.append((u, v))
posBM={}
nodepos = []
for x in BM.nodes() :
print x, BM.node[x]
nodepos.append(int(x))
#for n in pos :
# pos[n] = pos[n] * 250.
for n in BM:
# print n
# nodepos.append(n)
xy=np.array([pos[u] for u in BM.node[n]['graph']])
posBM[n]=xy.mean(axis = 0)
# posBM[n] = posBM[n] * 200
N = len(BM.edges(data=True))
#for n in BM:
# posBM[n] = posBM[n]*200.
#values = []
#for i in range(5):
# values.append(i)
#ind = np.arange(5)+0.5
#val = 3+10*random.random() # the bar lengths
#pos = np.arange(5)+.5
#for n in posBM:
# posBM[n]=posBM[n]*200.0
### Coloring the nodes ~!
formatter=ticker.LogFormatterMathtext()
cax = ax.imshow([bmicom],cmap=plt.cm.jet,alpha=1)
nx.draw_networkx_nodes(BM, posBM, nodelist=nodepos, node_size = node_size, node_shape='o', node_color=color_node, cmap=plt.cm.jet, alpha=1 )
nx.draw_networkx_edges(BM, posBM, edgelist = edgepos, alpha=0.85,edge_color = 'k',width = edge_width)
#nx.draw_networkx_labels(H,pos,labels=None,font_size=15)
#axfreq = pylab.axes([0.25, 0.1, 0.65, 0.03], axisbg='r')
#axamp = pylab.axes([0.25, 0.15, 0.65, 0.03], axisbg='r')
#sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=3)
#samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=5)
#plt.barh(ind,values, align='center')
#plt.yticks(ind, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
#plt.barh(ind, values, edge_width, color='k',alpha=0.5)
plt.rcParams["font.size"] = 12
c1=plt.colorbar(cax,orientation='vertical',shrink = 0.45)
c1.set_label("Average change in BMI of community")
#plt.title('Each shell represents a community')
#plt.fill([130,140,140,130], [120,120,140,140], 'k', alpha=0.5, edgecolor='k')
plt.axis('off')
plt.show()