|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Thierry Bertin-Mahieux (2012) Columbia University |
| 4 | + |
| 5 | +
|
| 6 | +Code to validate a submission file for the Million Song Dataset |
| 7 | +Challenge on Kaggle. Requires an internet connection. |
| 8 | +This code is developed under python 2.7 (Ubuntu machine). |
| 9 | +
|
| 10 | +Copyright 2012, Thierry Bertin-Mahieux |
| 11 | +
|
| 12 | +This program is free software: you can redistribute it and/or modify |
| 13 | +it under the terms of the GNU General Public License as published by |
| 14 | +the Free Software Foundation, either version 3 of the License, or |
| 15 | +(at your option) any later version. |
| 16 | +
|
| 17 | +This program is distributed in the hope that it will be useful, |
| 18 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +GNU General Public License for more details. |
| 21 | +
|
| 22 | +You should have received a copy of the GNU General Public License |
| 23 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | +""" |
| 25 | +__author__ = 'Thierry Bertin-Mahieux <[email protected]>' |
| 26 | +__date__ = 'Sun Mar 11 18:39:03 EDT 2012' |
| 27 | + |
| 28 | + |
| 29 | +import os |
| 30 | +import sys |
| 31 | +import time |
| 32 | +import urllib2 |
| 33 | + |
| 34 | + |
| 35 | +# Number of predicted songs required per user. |
| 36 | +CUTOFF = 500 |
| 37 | + |
| 38 | +# Million Song Dataset website file directory. |
| 39 | +HTML_PREFIX = 'http://labrosa.ee.columbia.edu/millionsong/sites/default/files/' |
| 40 | + |
| 41 | +# Canonical list of users for the contest, there should be predictions for |
| 42 | +# each user, one user per line, users are in the same order as in this file. |
| 43 | +CANONICAL_USER_LIST = '%s%s' % (HTML_PREFIX, |
| 44 | + 'challenge/canonical/kaggle_users.txt') |
| 45 | + |
| 46 | +# Canonical list of songs and their integer index. |
| 47 | +CANONICAL_SONG_LIST = '%s%s' % (HTML_PREFIX, |
| 48 | + 'challenge/canonical/kaggle_songs.txt') |
| 49 | + |
| 50 | + |
| 51 | +def load_list_from_the_web(url): |
| 52 | + """Grab a text file, return each line in a list.""" |
| 53 | + print '---retrieveing url %s...' % url |
| 54 | + t1 = time.time() |
| 55 | + stream = urllib2.urlopen(url) |
| 56 | + data = [l.strip() for l in stream.readlines()] |
| 57 | + stream.close() |
| 58 | + print ' DONE! It took %d seconds.' % int(time.time() - t1) |
| 59 | + return data |
| 60 | + |
| 61 | + |
| 62 | +def print_error_message(msg, line_num=None): |
| 63 | + """Formatted error message.""" |
| 64 | + prefix = 'ERROR! ' |
| 65 | + if line_num: |
| 66 | + prefix += '[line %d] ' % line_num |
| 67 | + print '%s%s' % (prefix, msg) |
| 68 | + |
| 69 | + |
| 70 | +def validate_one_line(line, line_num, min_max_song_indexes): |
| 71 | + """Make sure an individual line looks valid, return True if so.""" |
| 72 | + is_valid = True |
| 73 | + min_index, max_index = min_max_song_indexes |
| 74 | + assert min_index == 1, 'Problem, minimum song index is not 1.' |
| 75 | + # Line too small or empty? |
| 76 | + if len(line) < 500: |
| 77 | + print_error_message("Line too short! (%d characters)" % len(line), |
| 78 | + line_num) |
| 79 | + is_valid = False |
| 80 | + parts = line.split(' ') |
| 81 | + # Not the right number of items per line? |
| 82 | + if len(parts) != CUTOFF: |
| 83 | + msg = "Line should have %d one-space-separated elements, " % CUTOFF |
| 84 | + msg += "found %d" % len(parts) |
| 85 | + print_error_message(msg, line_num) |
| 86 | + is_valid = False |
| 87 | + for song_index in parts: |
| 88 | + # Is the song an integer? |
| 89 | + try: |
| 90 | + index = int(song_index) |
| 91 | + except ValueError: |
| 92 | + if len(song_index) == 18 and song_index[:2] == 'SO': |
| 93 | + msg = 'Predicted songs should be integers, not SO...' |
| 94 | + msg += 'Found: %s' % song_index |
| 95 | + print_error_message(msg, line_num) |
| 96 | + else: |
| 97 | + msg = 'Found non-integer song ID: %s' % song_index |
| 98 | + print_error_message(msg, line_num) |
| 99 | + is_valid = False |
| 100 | + break |
| 101 | + # Is it 0-indexed instead of 1? |
| 102 | + if index == 0: |
| 103 | + msg = 'Found song index 0, song indexes start at 1.' |
| 104 | + print_error_message(msg, line_num) |
| 105 | + is_valid = False |
| 106 | + break |
| 107 | + # Is the index a valid integer? |
| 108 | + elif index < 1 or index > max_index: |
| 109 | + msg = 'Found song index %d, ' % index |
| 110 | + msg += 'it should be between 1 and %d.' % max_index |
| 111 | + print_error_message(msg, line_num) |
| 112 | + is_valid = False |
| 113 | + break |
| 114 | + # Are there song duplicates? |
| 115 | + if is_valid: |
| 116 | + if len(set(parts[1:])) != len(parts[1:]): |
| 117 | + msg = 'There is at least one song ID duplicate.' |
| 118 | + print_error_message(msg, line_num) |
| 119 | + is_valid = False |
| 120 | + # Done. |
| 121 | + return is_valid |
| 122 | + |
| 123 | + |
| 124 | +def main(argv): |
| 125 | + """Validate the submission from canonical files fetched online.""" |
| 126 | + |
| 127 | + # Sanity check on the file. |
| 128 | + submission_filename = argv[1] |
| 129 | + if not os.path.isfile(submission_filename): |
| 130 | + print 'ERROR: file %s does not exist.' % submission_filename |
| 131 | + die_with_usage() |
| 132 | + |
| 133 | + # Fetch data files. |
| 134 | + users = load_list_from_the_web(CANONICAL_USER_LIST) |
| 135 | + songs_and_indexes = load_list_from_the_web(CANONICAL_SONG_LIST) |
| 136 | + |
| 137 | + # Check user file. |
| 138 | + assert len(users) == 110000, 'Problem with the online user file.' |
| 139 | + for user in users: |
| 140 | + assert len(user) == 40, '%s' % ( |
| 141 | + 'Problem with the online user file (user: %s).' % user, ) |
| 142 | + |
| 143 | + print '***************************************' |
| 144 | + print '**********ANALYZING SUBMISSION*********' |
| 145 | + |
| 146 | + # Extract indexes from the list of songs. |
| 147 | + indexes = [int(line.split(' ')[1]) for line in songs_and_indexes] |
| 148 | + min_index = min(indexes) |
| 149 | + max_index = max(indexes) |
| 150 | + msg_song_file_prob = 'Problem with the online song file, aborting.' |
| 151 | + assert min_index == 1, msg_song_file_prob |
| 152 | + assert max_index == len(indexes), msg_song_file_prob |
| 153 | + min_max_index = (min_index, max_index) |
| 154 | + |
| 155 | + # Keep stats |
| 156 | + submission_is_valid = True |
| 157 | + |
| 158 | + # Go through each line, validates it, keep some stats. |
| 159 | + line_number = 0 |
| 160 | + fIn = open(submission_filename, 'r') |
| 161 | + for line in fIn.xreadlines(): |
| 162 | + line_number += 1 |
| 163 | + submission_is_valid = validate_one_line(line.strip(), |
| 164 | + line_number, |
| 165 | + min_max_index) |
| 166 | + if not submission_is_valid: |
| 167 | + fIn.close() |
| 168 | + sys.exit(0) |
| 169 | + fIn.close() |
| 170 | + |
| 171 | + # Final message. |
| 172 | + if submission_is_valid: |
| 173 | + print '***************************************' |
| 174 | + print 'Awesome, your submission is good to go!' |
| 175 | + sys.exit(0) |
| 176 | + |
| 177 | + |
| 178 | +def die_with_usage(): |
| 179 | + """Help menu.""" |
| 180 | + print 'MSD CHallenge: script to validate your submission to Kaggle.' |
| 181 | + print '(you need an internet connection)' |
| 182 | + print '------------------------------------------------------------' |
| 183 | + print '' |
| 184 | + print 'python validate_submission.py <submission file>' |
| 185 | + print '' |
| 186 | + print 'ARGS' |
| 187 | + print ' <submission file> File to be uploaded to Kaggle.' |
| 188 | + sys.exit(0) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == '__main__': |
| 192 | + |
| 193 | + # Display the help menu and quit? |
| 194 | + HELP_KEYWORDS = ('help', '-help', '--help') |
| 195 | + if len(sys.argv) < 2 or sys.argv[1].lower() in HELP_KEYWORDS: |
| 196 | + die_with_usage() |
| 197 | + |
| 198 | + main(sys.argv) |
0 commit comments