-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.py
274 lines (220 loc) · 7.85 KB
/
processing.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import pandas as pd
from ast import literal_eval
from collections import Counter
import networkx as nx
from networkx.readwrite import json_graph
import json
import numpy as np
import community as community_louvain
from scipy import stats
# Processes and parses the raw data from scraper into the networks and data
# that we want to graph
class NaNCounter(Counter):
"""
A counter that excludes nan values
"""
def update(self, *args, **kwds):
if isinstance(args[0], pd.Series):
args = (args[0].dropna(),)
super(NaNCounter, self).update(*args, **kwds)
def load_csv_files():
"""
Loads csv files of all the raw data from the scraper
"""
regions = pd.read_csv('./data/top-regions.csv')
clubs = pd.read_csv('./data/top-clubs.csv', index_col='id').fillna('')
dates = pd.read_csv(
'./data/top-clubs-dates.csv',
index_col='id',
parse_dates=['date']
)
date_details = pd.read_csv(
r'./data/date-details-2019.csv',
index_col='id',
header=0
)
# artists are saved as list representations of lists
date_details.artists = date_details.artists.apply(lambda x: literal_eval(x))
return regions, clubs, dates, date_details
def normalize_artist_data(date_details):
"""
Normalizes artist data into their own data frame, to be joined on
event_id
"""
artists_to_dates_list = []
for date_id, row in date_details.iterrows():
for artist_id, artist_name in row['artists']:
artists_to_dates_list.append([date_id, artist_id, artist_name])
artists_to_dates = pd.DataFrame.from_records(
artists_to_dates_list,
columns=['event_id', 'artist_id','artist_name']
)
artists_to_dates = artists_to_dates.set_index('event_id')
return artists_to_dates
def join_data_frames(regions, clubs, dates, date_details, artists_to_dates):
"""
Join all the data frames into one big "table"
"""
# join regions and clubs
regions_top_clubs = regions.join(
clubs.reset_index().set_index('region'),
on='region',
lsuffix='_region',
rsuffix='_club'
)
# Join top clubs and regions to club dates.
regions_top_club_dates = regions_top_clubs.join(
dates.reset_index().set_index('club_id'),
on='id',
rsuffix='_date'
)
regions_top_club_dates = regions_top_club_dates.rename(
columns={'id': 'id_club'}
)
# join with date details
regions_top_club_dates_details = regions_top_club_dates.set_index(
'id_date'
).join(
date_details,
rsuffix='date_detail',
# date details only contain a subset of events (fridays and saturdays)
# so exclude all other rows
how='inner'
)
all_data = regions_top_club_dates_details.join(
artists_to_dates,
rsuffix='artists',
).drop_duplicates()
all_data.index.name = 'id_date'
return all_data
def group_by_year_and_club(all_data):
"""
Group all data by years and clubs
"""
all_data['year'] = all_data['date'].map(lambda x: x.year)
attending = all_data.drop_duplicates(
'id_date'
).groupby(
['year', 'name_club']
).agg(
attending=('attending', 'sum')
)
by_year_and_club = all_data.groupby(['year', 'name_club']).agg(
club_id=('id_club', 'first'),
region=('name_region', 'first'),
country=('country', 'first'),
logo=('img', 'first'),
number_of_dates=('id_date', pd.Series.nunique),
rank=('rank_club', 'first'),
number_of_unique_artists=('artist_name', pd.Series.nunique),
total_number_of_artists=('artist_name', 'count'),
artists=('artist_name', NaNCounter),
followers=('followers', 'first'),
capacity=('capacity', 'first'),
)
return by_year_and_club.join(attending)
def calculate_club_likeness(club_data):
"""
Calculate the Jaccard index for each pair of clubs in club data
return as a list of lists with each list containing:
[source, target, jaccard_score]
"""
similarities = []
size = len(club_data)
for i in range(0, size):
club1 = club_data.iloc[i]
for j in range(i+1, size):
club2 = club_data.iloc[j]
similarity = jaccard_index(club1.artists, club2.artists)
if similarity > 0:
similarities.append((club1.name[1], club2.name[1], similarity))
return similarities
def jaccard_index(a, b):
"""
Calculates the Jaccard index for two multisets described by dictionaries
where the keys are the names of the entries and the values are the number
of times they are included in the set
https://en.wikipedia.org/wiki/Jaccard_index
"""
intersection = len(list((Counter(a) & Counter(b)).elements()))
union = sum(Counter(a).values()) + sum(Counter(b).values()) - intersection
if union == 0:
return 0
else:
return intersection / union
def create_graph(nodes, edges):
G = nx.Graph()
for (year, club_name), data in nodes.iterrows():
G.add_node(club_name, **data.to_dict())
G.add_weighted_edges_from(edges)
partition = community_louvain.best_partition(G)
for key, value in partition.items():
G.nodes[key]["group"] = value
return G
def artist_id_to_name_dict(all_data):
"""
For most artists their id is a slugified version of their name
But there are exceptions, keep track of those
"""
artist_name_to_ids = all_data.loc[:, 'artist_id':'artist_name']. \
drop_duplicates().set_index('artist_name').to_dict()['artist_id']
d = {}
for artist_name, artist_id in artist_name_to_ids.items():
if artist_id is not np.nan:
if artist_id != artist_name.lower().replace(' ', ''):
d[artist_name] = artist_id
return d
def optimise_json(d):
"""
Change dictionaries into lists to optimise space in json.
Convert back in js.
"""
d['node_keys'] = list(d['nodes'][0].keys())
for i, node in enumerate(d['nodes']):
d['nodes'][i] = list(node.values())
d['link_keys'] = list(d['links'][0].keys())
for i, link in enumerate(d['links']):
d['links'][i] = list(link.values())
return d
def residency_factor(club):
if club['number_of_unique_artists'] == 0:
return 0
return club['total_number_of_artists'] / club['number_of_unique_artists']
def compare_underlying_distribution(clubs, calculation, aggregation):
t = []
aggregate_values = set()
for club in clubs:
t.append([
club[aggregation],
calculation(club)
])
aggregate_values.add(club[aggregation])
for key in aggregate_values:
n1 = [a[1] for a in t if a[0] == key]
n2 = [a[1] for a in t if a[0] != key]
a = stats.ks_2samp(n1, n2)
if a[1] <= 0.01:
print(f'{key} is likely not from the same distribution')
else:
print(f'{key}')
if __name__ == "__main__":
regions, clubs, dates, date_details = load_csv_files()
artists_to_dates = normalize_artist_data(date_details)
date_details = date_details.drop('artists', axis=1)
all_data = join_data_frames(
regions, clubs, dates, date_details, artists_to_dates
)
all_data = all_data.reset_index()
club_data = group_by_year_and_club(all_data)
club_data.logo = club_data.logo.fillna('')
artist_name_to_ids = artist_id_to_name_dict(all_data)
for year, df in club_data.groupby(level=0):
similarities = calculate_club_likeness(df)
G = create_graph(df, similarities)
d = json_graph.node_link_data(G)
d['artist_names_to_ids'] = artist_name_to_ids
compare_underlying_distribution(
d['nodes'], residency_factor, 'region'
)
d = optimise_json(d)
json.dump(d, open('./public/network-{}.json'.format(year), 'w'))