-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
139 lines (96 loc) · 4.96 KB
/
preprocessing.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
import argparse
import gzip
import json
import os
import os.path as osp
import shutil
from pathlib import Path
import numpy as np
import torch
from torch_geometric.data import download_url, extract_zip
from e2e.io import read_spotify_data
dataset_name = 'spotify_million_playlist_dataset'
challenge_name = 'spotify_million_playlist_dataset_challenge'
def write_dict_to_compressed_file(dict, filename):
json_str = json.dumps(dict) + "\n"
json_bytes = json_str.encode('utf-8')
with gzip.open(filename, 'w') as fout:
fout.write(json_bytes)
def get_raw_file_names():
raw_file_names = []
# Add the milion playlist dataset split files
for i in range(0, 10**6, 10**3):
raw_file_names.append(f'mpd.slice.{i}-{i+999}.json')
# Add the million playlist challenge file
raw_file_names.append('challenge_set.json')
return raw_file_names
def main(args):
print(args)
device = torch.device("cpu")
if (args.device == 'gpu'):
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available() and torch.backends.mps.is_built():
device = torch.device("mps")
args.device = device
output_dataset_path = args.output_dataset_path if args.output_dataset_path.is_absolute() else osp.join(Path('.').resolve(), args.output_dataset_path)
orig_dataset_path = args.orig_dataset_path if args.orig_dataset_path.is_absolute() else osp.join(Path('.').resolve(), args.orig_dataset_path)
raw_dir_path = osp.join(output_dataset_path, 'raw')
raw_file_names = get_raw_file_names()
download(raw_dir_path, orig_dataset_path, output_dataset_path, raw_file_names)
process(raw_dir_path, output_dataset_path, raw_file_names, args)
def download(raw_dir_path, orig_dataset_path, output_dataset_path, raw_file_names):
# Download and unpack the relevant data to `raw_dir_path` if it doesn't exist
if os.path.isdir(raw_dir_path) and len(os.listdir(raw_dir_path)) > 0:
return
dataset_folder = osp.join(output_dataset_path, dataset_name)
dataset_path = download_url(f'file://{osp.join(orig_dataset_path, dataset_name)}.zip', dataset_folder)
extract_zip(dataset_path, dataset_folder)
os.unlink(dataset_path)
challenge_folder = osp.join(output_dataset_path, challenge_name)
challenge_path = download_url(f'file://{osp.join(orig_dataset_path, challenge_name)}.zip', dataset_folder)
extract_zip(challenge_path, challenge_folder)
os.unlink(challenge_path)
# Rename the data subfolder of the dataset as the raw dir
os.rename(osp.join(f'{dataset_folder}', 'data'), raw_dir_path)
# Copy the challenge file to the raw dir
shutil.copyfile(osp.join(f'{challenge_folder}', f'{raw_file_names[-1]}'), osp.join(f'{raw_dir_path}', f'{raw_file_names[-1]}'))
# Remove the intermediate folders
shutil.rmtree(dataset_folder)
shutil.rmtree(challenge_folder)
def process(raw_dir_path, output_dir_path, raw_file_names, args):
spotify_data = read_spotify_data(raw_dir_path, raw_file_names, args)
np.savez(
osp.join(output_dir_path, 'data.npz'),
x_playlist_train=spotify_data["x_playlist_train"],
x_playlist_test=spotify_data["x_playlist_test"],
x_track=spotify_data["x_track"],
train_edge_index=spotify_data["train_edge_index"],
train_edge_label=spotify_data["train_edge_label"],
test_edge_index=spotify_data["test_edge_index"],
test_edge_label=spotify_data["test_edge_label"],
)
write_dict_to_compressed_file(
spotify_data["playlist_id_map"],
osp.join(output_dir_path, 'playlist_id_map.json.gz'),
)
write_dict_to_compressed_file(
spotify_data["track_uri_map"],
osp.join(output_dir_path, 'track_uri_map.json.gz'),
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Pre-process the Spotify Million Playlists Dataset.')
parser.add_argument('--orig_dataset_path', type=Path, default='./spotify_raw_dataset', required=False,
help='Path to original Spotify MPD')
parser.add_argument('--output_dataset_path', type=Path, default='./spotify_preprocessed_dataset', required=False,
help='Path to the output pre-processed Spotify MPD')
parser.add_argument('--node_feature_size', type=int, default=384, required=False,
help='Dimension of the produced node features')
parser.add_argument('--batch_size', type=int, default=8192, required=False,
help='Batch size for the Sentence Transformer')
parser.add_argument('--dimension_reduction', type=bool, default=False, required=False,
help='Reduce node feature dimensionality to `node_feature_size`')
parser.add_argument('--device', type=str, default='gpu', choices=['cpu', 'gpu'], required=False,
help='Device to be used')
args = parser.parse_args()
main(args)