-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathplot_dataset.py
More file actions
60 lines (51 loc) · 2.4 KB
/
Copy pathplot_dataset.py
File metadata and controls
60 lines (51 loc) · 2.4 KB
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
import numpy as np
import pandas as pd
import sys
import ast
import json
import matplotlib.pyplot as plt
''' this script is used to plot characteristics of the dataset '''
def community_size_distribution():
# plots the distribution of the communities in the topology
# assumes that there are two topology files on disk, one for the cliques and one
# for communities that have been generated by growing those cliques (extends CAA)
df = pd.read_csv('cliques', sep='\] ', engine='python', header=None)
sizes = [len(ast.literal_eval(row[0])) for idx, row in df.iterrows()]
df = pd.read_csv('communities', sep='\] ', engine='python', header=None)
sizes += [len(ast.literal_eval(row[0])) for idx, row in df.iterrows()]
x_axis = np.arange(0, max(sizes), 10)
plt.bar(x_axis, bin_by_x_axis(sizes, x_axis), align='center', width=10, color='y')
plt.xlabel('Community Size')
plt.ylabel('Number of Communities')
plt.title('Community Size Distribution')
plt.xticks(x_axis, generate_x_ticks(x_axis), rotation='60', ha='right', fontsize='small')
plt.xlim([-10, np.max(x_axis) + 10])
plt.tight_layout()
plt.savefig('community_size_distribution')
plt.close()
def user_tweet_distribution():
# plots the distribution of number of tweets that the user has on their timeline
# assumes that all tweets (from users that exists) in a given topology are downloaded to
# the dnld_tweets/ directory using get_community_tweets.py script
with open('dnld_tweets/active_users.json', 'r') as infile:
d = json.load(infile)
num_tweets = [d[x] for x in d]
x_axis = np.arange(0, 3300, 100)
plt.bar(x_axis, bin_by_x_axis(num_tweets, x_axis), width=100, color='r', align='center')
plt.xlabel('Number of Tweets')
plt.xticks(x_axis, generate_x_ticks(x_axis), rotation=60, ha='right', fontsize=8)
plt.xlim([-100, 3300])
plt.ylabel('Number of Users')
plt.title('Tweets per User')
plt.tight_layout()
plt.savefig('tweet_distribution')
plt.close()
def generate_x_ticks(x_axis):
return [('> ' + str(x_axis[i])) if i == len(x_axis) -1 else (str(x_axis[i]) + ' - ' + str(x_axis[i + 1])) for i in range(0, len(x_axis))]
def bin_by_x_axis(sizes, x_axis):
return np.bincount([x - 1 for x in np.digitize(sizes, x_axis)])
def main():
community_size_distribution()
user_tweet_distribution()
if __name__ == '__main__':
sys.exit(main())