-
Notifications
You must be signed in to change notification settings - Fork 8
/
create_dev_set.py
executable file
·131 lines (101 loc) · 3.98 KB
/
create_dev_set.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
#!/usr/bin/env python3
import argparse
import json
import os
import random
from datetime import date
import numpy as np
from eval.mpd.mpd import playlists_from_slices
# Ratio between named and nameless playlists
P_NAMELESS = 0.2
# distribution dicts of form n_retain: n_count
DIST_NAMELESS = {10: 1000, 5: 1000}
DIST_NAMED = {100: 2000, 25: 2000, 0: 1000, 1: 1000, 5: 1000, 10: 1000}
DATA_PATH = "/data21/lgalke/MPD/data"
def random_keep(tracks, dist):
"""
Returns randomly sampled tracks according to dist and the number of
holdouts
"""
# Unzip distribution
values, weights = zip(*dist.items())
# Brute-force fix for too small playlists, might skew distribution a little
# Always drop at least some...
red_dist = [(val, p) for (val, p) in dist.items() if val < len(tracks)]
values, weights = zip(*red_dist)
# Renormalize such that weights sum up to 1
weights_sum = sum(weights)
weights = [w / weights_sum for w in weights]
# Determine number of tracks to keep according to (reduced) dist
keep = np.random.choice(values, p=weights)
# in-place shuffle
random.shuffle(tracks)
retain = tracks[:keep]
num_holdouts = len(tracks[keep:])
if num_holdouts == 0:
print("WARNING: no holdouts")
return retain, num_holdouts
def corrupt_playlists(playlists):
# Name-less playlists should have more than 10 tracks
long_enough = [p for p in playlists if len(p) > max(DIST_NAMELESS.keys())]
long_enough = []
too_short = []
len_threshold = max(DIST_NAMELESS.keys())
# Split playlists into the ones that are long enough for nameless
# treatment and the ones that are too short
for playlist in playlists:
if len(playlist['tracks']) > len_threshold:
long_enough.append(playlist)
else:
too_short.append(playlist)
dev_playlists = []
# Random sample n_nameless among long enough ones
random.shuffle(long_enough)
n_nameless = int(P_NAMELESS * len(playlists))
for playlist in long_enough[:n_nameless]:
pcopy = {k: v for (k, v) in playlist.items()}
del pcopy['name']
retained_tracks, num_holdouts = random_keep(pcopy['tracks'],
DIST_NAMELESS)
pcopy['tracks'] = retained_tracks
pcopy['num_holdouts'] = num_holdouts
dev_playlists.append(pcopy)
# Merge remaining and too short playlists
remainder = long_enough[n_nameless:] + too_short
# Treat named, in place
for playlist in remainder:
pcopy = {k: v for (k, v) in playlist.items()}
retained_tracks, num_holdouts = random_keep(pcopy['tracks'],
DIST_NAMED)
pcopy['tracks'] = retained_tracks
pcopy['num_holdouts'] = num_holdouts
dev_playlists.append(pcopy)
assert len(dev_playlists) == len(playlists)
return dev_playlists
def main():
parser = argparse.ArgumentParser()
parser.add_argument('slices', type=argparse.FileType('r'),
help="Path to file with one slice filename per line")
parser.add_argument('-o', '--output', type=str, default=None,
help="File to put output")
args = parser.parse_args()
if args.output is None:
print("No output file specified, performing a dry run")
if os.path.exists(args.output) and \
input("Path '{}' exists. Overwrite? [y/N]"
.format(args.output)) != 'y':
exit(-1)
# strip newlines from exclude names
slices = [s.strip() for s in args.slices]
print("Creating dev set from slices:", slices)
playlists = playlists_from_slices(DATA_PATH, only=slices)
dev_playlists = corrupt_playlists(playlists)
dev_set = {
'date': str(date.today()),
'version': 'dev set created from: ' + str(slices),
'playlists': dev_playlists
}
with open(args.output, 'w') as fhandle:
json.dump(dev_set, fhandle)
if __name__ == '__main__':
main()