diff options
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 83 |
1 files changed, 10 insertions, 73 deletions
@@ -1,9 +1,8 @@ | |||
1 | from banapedia.Ban import * | 1 | from banapedia.Ban import * |
2 | import bandict | ||
2 | from collections import Counter | 3 | from collections import Counter |
3 | import json | ||
4 | import pygal | 4 | import pygal |
5 | import numpy as np | 5 | import numpy as np |
6 | import urllib.request | ||
7 | 6 | ||
8 | __author__ = 'pacien' | 7 | __author__ = 'pacien' |
9 | 8 | ||
@@ -12,44 +11,15 @@ BAN_MAP_FILE = "output/ban-map.svg" | |||
12 | BAN_DURATION_MAP_FILE = "output/ban-duration-map.svg" | 11 | BAN_DURATION_MAP_FILE = "output/ban-duration-map.svg" |
13 | HIST_FILE = "output/histogram.svg" | 12 | HIST_FILE = "output/histogram.svg" |
14 | 13 | ||
15 | BAN_FILE = "resources/ban_list.json" | ||
16 | |||
17 | SAMPLES = 30000 | 14 | SAMPLES = 30000 |
18 | SAMPLES_BY_QUERY = 500 | ||
19 | |||
20 | |||
21 | def configure_proxy(): | ||
22 | proxy = urllib.request.ProxyHandler(urllib.request.getproxies()) | ||
23 | opener = urllib.request.build_opener(proxy) | ||
24 | urllib.request.install_opener(opener) | ||
25 | |||
26 | |||
27 | def load_from_internet(): | ||
28 | configure_proxy() | ||
29 | return fetch_multipart_ban_dict(SAMPLES, SAMPLES_BY_QUERY) | ||
30 | |||
31 | |||
32 | def load_from_local(): | ||
33 | with open(BAN_FILE, "r") as ban_dict_file: | ||
34 | return json.load(ban_dict_file) | ||
35 | |||
36 | |||
37 | def write_to_local(ban_dict_list): | ||
38 | with open(BAN_FILE, "w") as ban_dict_file: | ||
39 | json.dump(ban_dict_list, ban_dict_file, indent="\t") | ||
40 | |||
41 | |||
42 | # ban_dict_list = load_from_internet() | ||
43 | # write_to_local(ban_dict_list) | ||
44 | |||
45 | ban_dict_list = load_from_local() | ||
46 | 15 | ||
47 | ban_list = map_bans(ban_dict_list) | 16 | BAN_FILE = "resources/ban_list.json" |
48 | 17 | ||
18 | ban_dict_list = bandict.BanList(BAN_FILE) | ||
49 | 19 | ||
50 | ########## HISTOGRAM ########## | 20 | # ======== HISTOGRAM ======= # |
51 | 21 | ||
52 | ban_durations = [ban.get_duration() for ban in ban_list] | 22 | ban_durations = ban_dict_list.get_durations() |
53 | (ban_durations_bars, bins) = np.histogram(ban_durations, bins=[round(365/12*x) for x in range(1, 50+2)]) | 23 | (ban_durations_bars, bins) = np.histogram(ban_durations, bins=[round(365/12*x) for x in range(1, 50+2)]) |
54 | 24 | ||
55 | print("[INFO]", "Generating histogram") | 25 | print("[INFO]", "Generating histogram") |
@@ -60,13 +30,10 @@ bar_chart.add("Number of active bans", ban_durations_bars) | |||
60 | bar_chart.render_to_file(HIST_FILE) | 30 | bar_chart.render_to_file(HIST_FILE) |
61 | print("[INFO]", "Histogram generation complete") | 31 | print("[INFO]", "Histogram generation complete") |
62 | 32 | ||
63 | ########## NB BAN MAP ########## | 33 | # ======= NB BAN MAP ======= # |
64 | |||
65 | def count_by_country(ban_list): | ||
66 | country_ban_list = [ban.get_country_code() for ban in ban_list] | ||
67 | return Counter(country_ban_list) | ||
68 | 34 | ||
69 | nb_bans_by_country = count_by_country(ban_list) | 35 | country_ban_list = ban_dict_list.get_countries() |
36 | nb_bans_by_country = Counter(country_ban_list) | ||
70 | 37 | ||
71 | print("[INFO]", "Generating ban map") | 38 | print("[INFO]", "Generating ban map") |
72 | worldmap_chart = pygal.Worldmap(legend_at_bottom=True) | 39 | worldmap_chart = pygal.Worldmap(legend_at_bottom=True) |
@@ -76,35 +43,9 @@ worldmap_chart.render_to_file(BAN_MAP_FILE) | |||
76 | print("[INFO]", "Ban map generation complete") | 43 | print("[INFO]", "Ban map generation complete") |
77 | 44 | ||
78 | 45 | ||
79 | ########## BAN DURATION MAP ########## | 46 | # ======= BAN DURATION MAP ======= # |
80 | |||
81 | def group_by_country(ban_list): | ||
82 | ban_duration_by_country = {} | ||
83 | |||
84 | for ban in ban_list: | ||
85 | country_code = ban.get_country_code() | ||
86 | |||
87 | if country_code not in ban_duration_by_country.keys(): | ||
88 | ban_duration_by_country[country_code] = [] | ||
89 | |||
90 | ban_duration_by_country[country_code].append(ban) | ||
91 | |||
92 | return ban_duration_by_country | ||
93 | 47 | ||
94 | 48 | average_ban_duration_by_country = ban_dict_list.average_ban_by_country() | |
95 | def calc_average_ban_by_country(ban_by_country_dict): | ||
96 | average_ban_duration_by_country = {} | ||
97 | |||
98 | for country, bans in ban_by_country_dict.items(): | ||
99 | average = np.mean([ban.get_duration() for ban in bans]) | ||
100 | average_ban_duration_by_country[country] = average | ||
101 | |||
102 | return average_ban_duration_by_country | ||
103 | |||
104 | ban_duration_by_country = group_by_country(ban_list) | ||
105 | average_ban_duration_by_country = calc_average_ban_by_country(ban_duration_by_country) | ||
106 | |||
107 | average_ban_duration_by_country = {country: duration/30 for country, duration in average_ban_duration_by_country.items()} | ||
108 | 49 | ||
109 | print("[INFO]", "Generating ban duration map") | 50 | print("[INFO]", "Generating ban duration map") |
110 | worldmap_chart = pygal.Worldmap(legend_at_bottom=True) | 51 | worldmap_chart = pygal.Worldmap(legend_at_bottom=True) |
@@ -113,10 +54,6 @@ worldmap_chart.add("Average ban duration (months)", average_ban_duration_by_coun | |||
113 | worldmap_chart.render_to_file(BAN_DURATION_MAP_FILE) | 54 | worldmap_chart.render_to_file(BAN_DURATION_MAP_FILE) |
114 | print("[INFO]", "Ban duration map generation complete") | 55 | print("[INFO]", "Ban duration map generation complete") |
115 | 56 | ||
116 | print("\nTHIS WAS A TRIUMPH!") | ||
117 | print("I'M MAKING A NOTE HERE:") | ||
118 | print("HUGE [SUCCESS]\n") | ||
119 | |||
120 | print("Some additional stats about ban durations:") | 57 | print("Some additional stats about ban durations:") |
121 | print(" Mean: %.2f days" % np.mean(ban_durations)) | 58 | print(" Mean: %.2f days" % np.mean(ban_durations)) |
122 | print(" Median: %.2f days" % np.median(ban_durations)) | 59 | print(" Median: %.2f days" % np.median(ban_durations)) |