-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_users_activity_distribution.py
195 lines (143 loc) · 5.43 KB
/
all_users_activity_distribution.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! /usr/bin/env python
"""
all_users_activity_distrubition.py
Created by Rufaro Mukogo on 2010-12-21.
Copyright (c) 2010 __Northwestern University__. All rights reserved.
This script computes the distribution of all users activity (blog posts, messages, forum posts) in the CK database.
"""
import numpy
import sys
import os
import matplotlib
from matplotlib import pyplot as plt
from pylab import *
from build_friend_graph import *
from function_to_plot_histogram import *
def compute_activity(cut_off,lb,ub, path):
f = open(str(path)+'candidate_activity_with_'+str(cut_off)+'/total_activity_for_all_user_'+str(cut_off)+'_threshold.dat','r')
print "lower and upper", lb,ub
lines=f.readlines()[1:]
activity = []
for line in lines:
datum = float(line.split(',')[2])
activity.append(datum)
activity = filter(lambda x: lb<=x<=ub, activity)
#print activity
return lines, activity
def return_ids_for_graph(lines,lb,ub):
user_data = []
for line in lines:
datum = line
user_data.append([line.split(',')[0],line.split(',')[1],line.split(',')[2]])
data_scrubbed = []
for datum in user_data:
#print "lower and upper bounds 3", lb, ub
b=float(datum[2])
if(b>=lb) and (b<=ub):
data_scrubbed.append(datum[0])
else:
pass
#print "user_data_scrub", data_scrubbed
return data_scrubbed
def main(cut_off,lb,ub, dir, path):
lines,activity = compute_activity(cut_off,lb,ub,path)
uids = return_ids_for_graph(lines,lb,ub)
if ub >= 500:
nbins =int(ub)
else:
nbins = 200
#plot network
ckg = CKGraph()
G=ckg.build_undirected_graph(uids=uids)
Gprime = G[0]
#nx.draw(Gprime)
#plt.show()
degree = Gprime.degree()
degree_values = map(int,list(degree.values()))
closeness = nx.closeness_centrality(Gprime)
closeness_values = map(float,list(closeness.values()))
betweenness = nx.betweenness_centrality(Gprime)
betweenness_values = map(float,list(betweenness.values()))
figure(1)
nbins = [int(i) for i in xrange(1,max(degree_values)+1)]
n, bins, patches=hist(degree_values,normed=0,bins=nbins, log=True)
#print "binn values", bins
#print "max degree" , max(degree_values)
plt.xlabel('degree, k')
plt.ylabel('N(k)')
plt.title('degree distribution for '+str(cut_off)+'_points_with_active_count_'+str(int(lb))+'_to_'+str(int(ub)))
ax = gca()
#ax.set_xscale("log")
#ax.set_yscale("log")
ax.autoscale_view()
plt.grid(True)
plt.savefig(str(dir)+"/plots/"+'degree_distribution_'+str(cut_off)+'_points_'+str(lb)+'_'+str(ub)+'.eps', dpi=100)
data = zip(bins,n)
f = open(str(dir)+"/data/degree_distribution_data.dat","w")
nonzero_data = []
for datum in data:
if datum[1]>=1:
print>>f, datum
nonzero_data.append(datum)
else:
pass
histogram_plotting(nonzero_data, dir+"data/", filename="degree_distribution_complete_network"+str(cut_off)+"_points",\
x_axis="degree, k", y_axis="N(k)")
figure(2)
plt.hist(betweenness_values, normed=0,bins=120, log=True)
plt.xlabel('betweenness, b')
plt.ylabel('N(b)')
plt.title('betweenness distribution for '+str(cut_off)+'_points_with_active_count_'+str(int(lb))+'_to_'+str(int(ub)))
ax = gca()
ax.autoscale_view()
plt.grid(True)
plt.savefig(str(dir)+'betweennness_distribution_'+str(cut_off)+'_points_'+str(lb)+'_'+str(ub)+'.eps', dpi=100)
figure(3)
plt.xlabel('closeness, c')
plt.hist(closeness_values,normed=0,bins=120, log=True)
plt.ylabel('N(c)')
plt.title('closeness distribution for '+str(cut_off)+'_points_with_active_count_'+str(int(lb))+'_to_'+str(int(ub)))
ax = gca()
ax.autoscale_view()
plt.grid(True)
plt.savefig(str(dir)+'closeness_distribution_'+str(cut_off)+'_points_'+str(lb)+'_'+str(ub)+'.eps', dpi=100)
figure(4)
n, bins, patches = hist(activity,normed=0,bins=max(activity), log=True)
plt.xlabel('activity, a')
plt.ylabel('N(a)')
plt.title('activity distribution for '+str(cut_off)+'_points_with_active_count_'+str(int(lb))+'_to_'+str(int(ub)))
ax=gca()
ax.autoscale_view()
plt.grid(True)
plt.savefig(str(dir)+'activity_distribution_'+str(cut_off)+'_points_'+str(int(lb))+'_'+str(int(ub))+'.eps', dpi=100)
#plt.show()
print "activity bins", bins
data_act = zip(bins,n)
g = open(str(dir)+"/data/activity_distribution_data.dat","w")
nonzero_act_data = []
for datum in data_act:
if datum[0]>0 and datum[1]>=1:
print>>g, datum
nonzero_act_data.append(datum)
else:
pass
histogram_plotting(nonzero_act_data, dir+"/data/", filename="activity_distribution_complete_network"+str(cut_off)+"_points"\
, x_axis="activity, a", y_axis="N(a)")
if __name__ == '__main__':
if len(sys.argv) > 1:
number_of_weightpoints = int(sys.argv[1])
else:
number_of_weightpoints = 2
if len(sys.argv) >3:
lb = float(sys.argv[2])
ub = float(sys.argv[3])
else:
lb = 0.0
ub = 5000.0
if len(sys.argv) > 4:
path = sys.argv[4]
else:
path = ""
cut_off = number_of_weightpoints
dir = str(path)+str(cut_off)+'_points_network/'
main(cut_off,lb,ub, dir, path)