-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_figures.py
384 lines (321 loc) · 14.1 KB
/
helpers_figures.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Several classes and functions helping making plots
import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
from globals import *
class MetadataProcesor:
"""Process and format metadata files to use them in MakePlots class"""
def __init__(self, LFM_metadata_file, DEEZER_metadata_file) -> None:
self.LFM_metadata_file = LFM_metadata_file
self.DEEZER_metadata_file = DEEZER_metadata_file
def clean_metadata(self, df):
"""Keeping wanted data columns"""
return df[["country", "item_id"]]
def process(self):
"""Import and clean data"""
metadata_LFM = pd.read_csv(
f"./dataset/{self.LFM_metadata_file}.csv", low_memory=False
)
metadata_DEEZER = pd.read_csv(
f"./dataset/{self.DEEZER_metadata_file}.csv", low_memory=False
)
metadata_LFM = self.clean_metadata(metadata_LFM)
metadata_DEEZER = self.clean_metadata(metadata_DEEZER)
self.metadata = {"DEEZER": metadata_DEEZER, "LFM": metadata_LFM}
class MakePlots:
""" "General class for making all plots"""
def __init__(self, metadata, k_values, matadata_filename, global_models) -> None:
self.metadata = metadata # metadata attribute of MetadataProcesor class
self.k_values = k_values # k values in top-k reco. to consider
self.matadata_filename = matadata_filename
self.global_label = "GLOBAL" if global_models else "LOCAL"
self.global_models = global_models
if self.global_models: # Load and transform GLOBAL data
LFM_user_country = dict(
pd.read_csv("dataset/LFM_GLOBAL/demo.txt", sep="\t")
.T.reset_index()
.T.reset_index(drop=True)
.reset_index()[["index", 0]]
.to_numpy()
)
DEEZER_user_country = dict(
pd.read_csv("dataset/DEEZER_GLOBAL/user_country.csv").to_numpy()
)
self.user_country_dict = {
"DEEZER": DEEZER_user_country,
"LFM": LFM_user_country,
}
def get_try_indices(self, filenames):
if self.n_tries == "max":
try_indices = range(len(filenames))
else:
try_indices = range(self.n_tries)
return try_indices
def extract_top_k_reco(self, df, k):
n_users = len(df.user_id.unique())
k_max = int(len(df) / n_users) # getting back k value in top-k reco.
df["rank"] = list(range(1, k_max + 1)) * n_users # Adding rank column
return df[df["rank"] <= k].drop(columns=["rank"])
def drop_unlabeled_streams(self, stream_df):
df = stream_df.copy()
df = df.dropna(subset=["country"])
df = df[df["country"] != 0]
df = df[df["country"] != "0"]
return df
def load_predictions(self, n_tries):
self.n_tries = n_tries
self.predictions = dict()
for dataset in PLATFORMS:
for model in ["NeuMF", "ItemKNN"]:
if self.global_models:
filenames = sorted(
os.listdir(f"predicted/{dataset}/GLOBAL/{model}/"),
reverse=True,
)
try_indices = self.get_try_indices(filenames)
for try_index in tqdm(
try_indices, desc=f"loading {dataset} GLOBAL {model}"
):
filename = filenames[try_index]
filepath = f"predicted/{dataset}/GLOBAL/{model}/{filename}"
all_predictions_df = pd.read_csv(filepath)[
["user_id", "item_id"]
]
for country in COUNTRIES:
country_user_ids = [
int(uid)
for uid, user_country in self.user_country_dict[
dataset
].items()
if user_country == country
]
predictions_df = all_predictions_df[
all_predictions_df["user_id"].isin(country_user_ids)
].copy()
self.predictions[(dataset, country, model, try_index)] = (
self.extract_top_k_reco(
predictions_df, max(self.k_values)
)
)
else: # LOCAL models
for country in COUNTRIES:
filenames = sorted(
os.listdir(f"predicted/{dataset}/{country}/{model}/"),
reverse=True,
)
try_indices = self.get_try_indices(filenames)
for try_index in tqdm(
try_indices, desc=f"loading {dataset} {country} {model}"
):
filename = filenames[try_index]
filepath = (
f"predicted/{dataset}/{country}/{model}/{filename}"
)
predictions_df = pd.read_csv(filepath)[
["user_id", "item_id"]
]
self.predictions[(dataset, country, model, try_index)] = (
self.extract_top_k_reco(
predictions_df, max(self.k_values)
)
)
def load_datasets(self):
self.datasets = dict()
for dataset in PLATFORMS:
for country in COUNTRIES:
print(f"Loading {dataset} {country} dataset")
filename = f"{dataset}_{country}"
df = pd.read_csv(f"dataset/{filename}/{filename}.inter")
df = df.rename(
columns={"user_id:token": "user_id", "item_id:token": "item_id"}
)
df = pd.merge(df, self.metadata[dataset], on=["item_id"], how="left")
self.datasets[(dataset, country)] = df
def plot_dataset_local_streams_percents(self, save=False):
proportion_local_datasets = []
for dataset in PLATFORMS:
for country in COUNTRIES:
labeled_streams_dataset = self.drop_unlabeled_streams(
self.datasets[(dataset, country)]
)
proportion_local_datasets.append(
[
dataset,
country,
labeled_streams_dataset["country"].value_counts(normalize=True)[
country
],
]
)
self.proportion_local_datasets = pd.DataFrame(
proportion_local_datasets,
columns=["Dataset", "Country", "Proportion of Local Streams"],
)
self.proportion_local_datasets["Dataset_"] = self.proportion_local_datasets[
"Dataset"
].replace({"LFM": "LFM-2b", "DEEZER": "Deezer"})
self.proportion_local_datasets["Country_"] = self.proportion_local_datasets[
"Country"
].replace(COUNTRY_ALIASES)
sns.set_style("white")
sns.barplot(
self.proportion_local_datasets,
x="Country_",
y="Proportion of Local Streams",
hue="Dataset_",
order=["France", "Germany", "Brazil"],
palette=['#d97c7c', '#cccccc'],
)
plt.xticks(fontsize=18)
plt.yticks(ticks=[0, 0.1, 0.2, 0.3, 0.4, 0.45], labels=['0', '0.1', '0.2', '0.3', '0.4', ''], fontsize=16)
plt.xlabel("")
plt.legend(title="", fontsize=16, loc="upper center")
plt.ylabel("Proportion of Local Streams", fontsize=18)
if save:
plt.savefig(
f"figures/proportion_local_datasets_{self.matadata_filename}.pdf"
)
plt.show()
plt.close()
def plot_local_listening_distribution_hist(self, save=False):
for country in COUNTRIES:
df = self.datasets[("LFM", country)][["user_id", "country"]]
df = pd.DataFrame(
self.drop_unlabeled_streams(df).groupby("user_id").value_counts(normalize=True)
).reset_index()
LFM_proportions_list = df[df["country"] == country].proportion.tolist()
df = self.datasets[("DEEZER", country)][["user_id", "country"]]
df = pd.DataFrame(
self.drop_unlabeled_streams(df).groupby("user_id").value_counts(normalize=True)
).reset_index()
DEEZER_proportions_list = df[df["country"] == country].proportion.tolist()
plt.figure()
sns.set_style("white")
sns.histplot(
LFM_proportions_list,
stat="proportion",
bins=10,
color=COUNTRY_COLORS[country],
label="LFM",
)
plt.ylim(0, 0.5)
plt.yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5])
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.ylabel("User Proportion", fontsize=18)
plt.xlabel("Proportion of Local Streams", fontsize=18)
if save:
plt.savefig(
f"./figures/local_listening_distribution_hist_LFM_{country}.pdf"
)
plt.plot()
plt.close()
plt.figure()
sns.set_style("white")
sns.histplot(
DEEZER_proportions_list,
stat="proportion",
bins=10,
color=COUNTRY_COLORS[country],
label="DEEZER",
)
plt.ylim(0, 0.5)
plt.yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5])
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.ylabel("User Proportion", fontsize=18)
plt.xlabel("Proportion of Local Streams", fontsize=18)
if save:
plt.savefig(
f"./figures/local_listening_distribution_hist_DEEZER_{self.matadata_filename}_{country}.pdf"
)
plt.plot()
plt.close()
def compute_reco_results(self):
result_df = []
for dataset in PLATFORMS:
for country in COUNTRIES:
for model in ["NeuMF", "ItemKNN"]:
if self.global_models:
filenames = sorted(
os.listdir(f"predicted/{dataset}/GLOBAL/{model}/"),
reverse=True,
)
else:
filenames = sorted(
os.listdir(f"predicted/{dataset}/{country}/{model}/"),
reverse=True,
)
try_indices = self.get_try_indices(filenames)
for try_index in tqdm(
try_indices, desc=f"processing {dataset} {country} {model}"
):
predictions_df = self.predictions[
(dataset, country, model, try_index)
]
predictions_df = pd.merge(
predictions_df,
self.metadata[dataset],
on=["item_id"],
how="left",
)
for k in self.k_values:
proportion_local_value = self.extract_top_k_reco(
predictions_df, k
)["country"].value_counts(normalize=True)[country]
result_df.append(
[dataset, model, country, proportion_local_value, k]
)
result_df = pd.DataFrame(
result_df, columns=["Data", "Model", "Country", "% local streams", "k"]
)
result_df["bias"] = result_df.apply(
lambda row: row["% local streams"]
- self.proportion_local_datasets[
(self.proportion_local_datasets["Dataset"] == row.Data)
& (self.proportion_local_datasets["Country"] == row.Country)
]["Proportion of Local Streams"].values[0],
axis=1,
)
self.result_df = result_df
def plot_bias_topk_k_reco(self, save=False):
for dataset in PLATFORMS:
filtered_data = self.result_df[
self.result_df["Data"] == dataset
].sort_values(by="k")
filtered_data["Country"] = filtered_data["Country"].replace(COUNTRY_ALIASES)
sns.set_style("whitegrid")
plt.figure(figsize=(14, 7))
sns.lineplot(
data=filtered_data,
x="k",
y="bias",
hue="Country",
style="Model",
markers=True,
dashes=False,
err_style="band",
hue_order=["France", "Germany", "Brazil"],
markersize=10,
)
plt.axhline(y=0, color="black", linestyle="--")
plt.text(113.5, 0, "No bias", color="black", ha="right", fontsize=18)
plt.xticks(self.k_values, fontsize=18)
plt.yticks(fontsize=18)
plt.xlabel("k", fontsize=18)
plt.ylabel("Local Bias", fontsize=18)
if self.global_label == "LOCAL":
plt.legend(loc="upper right", bbox_to_anchor=(1, 0.85), fontsize=16)
else:
plt.legend().remove()
if save:
if dataset == "LFM":
plt.savefig(
f"./figures/bias_topk_{dataset}_{self.global_label}.pdf"
)
else:
plt.savefig(
f"./figures/bias_topk_{dataset}_{self.matadata_filename}_{self.global_label}.pdf"
)
plt.show()
plt.close()