forked from lgalke/mpd-aae-recommender
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate_dev.py
More file actions
executable file
·76 lines (50 loc) · 2.06 KB
/
evaluate_dev.py
File metadata and controls
executable file
·76 lines (50 loc) · 2.06 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import argparse
from mpd import playlists_from_slices, DATA_PATH
from mpd_metrics import aggregate_metrics
def load_submission(path):
""" Returns dict of list: pid -> track_ids """
sub = {}
with open(path, 'r') as fh:
for line in fh:
line = line.strip()
if line[0] == '#' or line.startswith('team_info'):
continue
pid, *tracks = line.split(',')
sub[int(pid)] = tracks
return sub
def main():
parser = argparse.ArgumentParser()
parser.add_argument('exclude', type=argparse.FileType('r'),
help="Path to exclude file, determines ground truth.")
parser.add_argument('submission', type=str,
help="Path to dev submission file")
parser.add_argument('-v', '--verbose', default=0, type=int)
args = parser.parse_args()
dev_slices = [line.strip() for line in args.exclude]
if args.verbose:
print("Loading ground truth from", dev_slices)
ground_truth = playlists_from_slices(DATA_PATH, only=dev_slices, verbose=args.verbose)
# Make the json stuff dictionaries from pid to track uris
ground_truth = {p['pid']: [t['track_uri'] for t in p['tracks']] for p in ground_truth}
predictions = load_submission(args.submission)
# Verify that pids match
pids = set(ground_truth.keys())
pids_pred = set(predictions.keys())
if not pids_pred:
print(args.submission, 'is empty.')
exit(1)
if args.verbose:
print(len(pids), "pids in ground truth")
print(len(pids_pred), "pids in predictions")
print(len(set.intersection(pids, pids_pred)), "pids in intersection")
# Super strict: All pids in both are the same
assert len(pids ^ pids_pred) == 0
# Less strict: all predicted pids should be also in gold
assert len(pids_pred - pids) == 0
summary = aggregate_metrics(ground_truth, predictions,
500, pids)
print(summary)
args = parser.parse_args()
if __name__ == '__main__':
main()