-
Notifications
You must be signed in to change notification settings - Fork 390
/
display_song.py
126 lines (106 loc) · 3.87 KB
/
display_song.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
"""
Thierry Bertin-Mahieux (2010) Columbia University
tb2332@columbia.edu
Code to quickly see the content of an HDF5 file.
This is part of the Million Song Dataset project from
LabROSA (Columbia University) and The Echo Nest.
Copyright 2010, Thierry Bertin-Mahieux
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import hdf5_getters
import numpy as np
def die_with_usage():
""" HELP MENU """
print 'display_song.py'
print 'T. Bertin-Mahieux (2010) tb2332@columbia.edu'
print 'to quickly display all we know about a song'
print 'usage:'
print ' python display_song.py [FLAGS] <HDF5 file> <OPT: song idx> <OPT: getter>'
print 'example:'
print ' python display_song.py mysong.h5 0 danceability'
print 'INPUTS'
print ' <HDF5 file> - any song / aggregate /summary file'
print ' <song idx> - if file contains many songs, specify one'
print ' starting at 0 (OPTIONAL)'
print ' <getter> - if you want only one field, you can specify it'
print ' e.g. "get_artist_name" or "artist_name" (OPTIONAL)'
print 'FLAGS'
print ' -summary - if you use a file that does not have all fields,'
print ' use this flag. If not, you might get an error!'
print ' Specifically desgin to display summary files'
sys.exit(0)
if __name__ == '__main__':
""" MAIN """
# help menu
if len(sys.argv) < 2:
die_with_usage()
# flags
summary = False
while True:
if sys.argv[1] == '-summary':
summary = True
else:
break
sys.argv.pop(1)
# get params
hdf5path = sys.argv[1]
songidx = 0
if len(sys.argv) > 2:
songidx = int(sys.argv[2])
onegetter = ''
if len(sys.argv) > 3:
onegetter = sys.argv[3]
# sanity check
if not os.path.isfile(hdf5path):
print 'ERROR: file',hdf5path,'does not exist.'
sys.exit(0)
h5 = hdf5_getters.open_h5_file_read(hdf5path)
numSongs = hdf5_getters.get_num_songs(h5)
if songidx >= numSongs:
print 'ERROR: file contains only',numSongs
h5.close()
sys.exit(0)
# get all getters
getters = filter(lambda x: x[:4] == 'get_', hdf5_getters.__dict__.keys())
getters.remove("get_num_songs") # special case
if onegetter == 'num_songs' or onegetter == 'get_num_songs':
getters = []
elif onegetter != '':
if onegetter[:4] != 'get_':
onegetter = 'get_' + onegetter
try:
getters.index(onegetter)
except ValueError:
print 'ERROR: getter requested:',onegetter,'does not exist.'
h5.close()
sys.exit(0)
getters = [onegetter]
getters = np.sort(getters)
# print them
for getter in getters:
try:
res = hdf5_getters.__getattribute__(getter)(h5,songidx)
except AttributeError, e:
if summary:
continue
else:
print e
print 'forgot -summary flag? specified wrong getter?'
if res.__class__.__name__ == 'ndarray':
print getter[4:]+": shape =",res.shape
else:
print getter[4:]+":",res
# done
print 'DONE, showed song',songidx,'/',numSongs-1,'in file:',hdf5path
h5.close()