annotate scripts/score_parser.py @ 1316:279930a008ca

All interfaces support comment boxes. Comment box identification matches presented tag (for instance, AB will be Comment on fragment A, rather than 1). Tighter buffer loading protocol, audioObjects register with the buffer rather than checking for buffer existence (which can be buggy depending on the buffer state). Buffers now have a state to ensure exact location in loading chain (downloading, decoding, LUFS, ready).
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 29 Jan 2016 11:11:57 +0000
parents
children 8540d153caec
rev   line source
nickjillings@1316 1 #!/usr/bin/python
nickjillings@1316 2
nickjillings@1316 3 import xml.etree.ElementTree as ET
nickjillings@1316 4 import os
nickjillings@1316 5 import sys
nickjillings@1316 6 import csv
nickjillings@1316 7
nickjillings@1316 8 # COMMAND LINE ARGUMENTS
nickjillings@1316 9
nickjillings@1316 10 assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\
nickjillings@1316 11 "Use: python score_parser.py [rating_folder_location]"
nickjillings@1316 12
nickjillings@1316 13 # XML results files location
nickjillings@1316 14 if len(sys.argv) == 1:
nickjillings@1316 15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nickjillings@1316 16 print "Use: python score_parser.py [rating_folder_location]"
nickjillings@1316 17 print "Using default path: " + folder_name
nickjillings@1316 18 elif len(sys.argv) == 2:
nickjillings@1316 19 folder_name = sys.argv[1] # First command line argument is folder
nickjillings@1316 20
nickjillings@1316 21 # check if folder_name exists
nickjillings@1316 22 if not os.path.exists(folder_name):
nickjillings@1316 23 #the file is not there
nickjillings@1316 24 print "Folder '"+folder_name+"' does not exist."
nickjillings@1316 25 sys.exit() # terminate script execution
nickjillings@1316 26 elif not os.access(os.path.dirname(folder_name), os.W_OK):
nickjillings@1316 27 #the file does exist but write privileges are not given
nickjillings@1316 28 print "No write privileges in folder '"+folder_name+"'."
nickjillings@1316 29
nickjillings@1316 30
nickjillings@1316 31 # CODE
nickjillings@1316 32
nickjillings@1316 33 # remember which files have been opened this time
nickjillings@1316 34 file_history = []
nickjillings@1316 35
nickjillings@1316 36 # get every XML file in folder
nickjillings@1316 37 for file in os.listdir(folder_name):
nickjillings@1316 38 if file.endswith(".xml"):
nickjillings@1316 39 tree = ET.parse(folder_name + '/' + file)
nickjillings@1316 40 root = tree.getroot()
nickjillings@1316 41
nickjillings@1316 42 # get subject ID from XML file
nickjillings@1316 43 subject_id = file[:-4] # file name (without extension) as subject ID
nickjillings@1316 44
nickjillings@1316 45 # get list of all pages this subject evaluated
nickjillings@1316 46 for audioholder in root.findall("./audioholder"): # iterate over pages
nickjillings@1316 47 page_name = audioholder.get('id') # get page name
nickjillings@1316 48
nickjillings@1316 49 if page_name is None: # ignore 'empty' audio_holders
nickjillings@1316 50 print "WARNING: " + file + " contains empty audio holder. (score_parser.py)"
nickjillings@1316 51 break
nickjillings@1316 52
nickjillings@1316 53 file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name
nickjillings@1316 54
nickjillings@1316 55 # create folder 'ratings' if not yet created
nickjillings@1316 56 if not os.path.exists(folder_name + '/ratings'):
nickjillings@1316 57 os.makedirs(folder_name + '/ratings')
nickjillings@1316 58
nickjillings@1316 59 # header: fragment IDs in 'alphabetical' order
nickjillings@1316 60 # go to fragment column, or create new column if it doesn't exist yet
nickjillings@1316 61
nickjillings@1316 62 # get array of audio elements and number of audio elements
nickjillings@1316 63 audiolist = audioholder.findall("./audioelement")
nickjillings@1316 64 n_fragments = len(audiolist)
nickjillings@1316 65
nickjillings@1316 66 # get alphabetical array of fragment IDs from this subject's XML
nickjillings@1316 67 fragmentnamelist = [] # make empty list
nickjillings@1316 68 for audioelement in audiolist: # iterate over all audioelements
nickjillings@1316 69 fragmentnamelist.append(audioelement.get('id')) # add to list
nickjillings@1316 70
nickjillings@1316 71 # if file exists, get header and add 'new' fragments
nickjillings@1316 72 if os.path.isfile(file_name):
nickjillings@1316 73 with open(file_name, 'r') as readfile:
nickjillings@1316 74 filereader = csv.reader(readfile, delimiter=',')
nickjillings@1316 75 headerrow = filereader.next()
nickjillings@1316 76
nickjillings@1316 77 # If file hasn't been opened yet this time, remove all rows except header
nickjillings@1316 78 if file_name not in file_history:
nickjillings@1316 79 with open(file_name, 'w') as writefile:
nickjillings@1316 80 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1316 81 headerrow = sorted(headerrow)
nickjillings@1316 82 filewriter.writerow(headerrow)
nickjillings@1316 83 file_history.append(file_name)
nickjillings@1316 84
nickjillings@1316 85 # Which of the fragmentes are in fragmentnamelist but not in headerrow?
nickjillings@1316 86 newfragments = list(set(fragmentnamelist)-set(headerrow))
nickjillings@1316 87 newfragments = sorted(newfragments) # new fragments in alphabetical order
nickjillings@1316 88 # If not empty, read file and rewrite adding extra columns
nickjillings@1316 89 if newfragments: # if not empty
nickjillings@1316 90 with open('temp.csv', 'w') as writefile:
nickjillings@1316 91 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1316 92 filewriter.writerow(headerrow + newfragments) # write new header
nickjillings@1316 93 with open(file_name, 'r') as readfile:
nickjillings@1316 94 filereader = csv.reader(readfile, delimiter=',')
nickjillings@1316 95 filereader.next() # skip header
nickjillings@1316 96 for row in filereader: # rewrite row plus empty cells for every new fragment name
nickjillings@1316 97 filewriter.writerow(row + ['']*len(newfragments))
nickjillings@1316 98 os.rename('temp.csv', file_name) # replace old file with temp file
nickjillings@1316 99 headerrow = headerrow + newfragments
nickjillings@1316 100
nickjillings@1316 101 # if not, create file and make header
nickjillings@1316 102 else:
nickjillings@1316 103 headerrow = sorted(fragmentnamelist) # sort alphabetically
nickjillings@1316 104 headerrow.insert(0,'')
nickjillings@1316 105 fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist
nickjillings@1316 106 with open(file_name, 'w') as writefile:
nickjillings@1316 107 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1316 108 filewriter.writerow(headerrow)
nickjillings@1316 109
nickjillings@1316 110 # open file to write for this page
nickjillings@1316 111 writefile = open(file_name, 'a')
nickjillings@1316 112 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1316 113
nickjillings@1316 114 # prepare row to be written for this subject for this page
nickjillings@1316 115 ratingrow = [subject_id]
nickjillings@1316 116
nickjillings@1316 117 # get scores related to fragment [id]
nickjillings@1316 118 for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column)
nickjillings@1316 119 elementvalue = audioholder.find("./audioelement/[@id='"
nickjillings@1316 120 + fragmentname
nickjillings@1316 121 + "']/value")
nickjillings@1316 122 if hasattr(elementvalue, 'text'): # if rating for this fragment exists
nickjillings@1316 123 ratingrow.append(elementvalue.text) # add to rating row
nickjillings@1316 124 else: # if this subject has not rated this fragment
nickjillings@1316 125 ratingrow.append('') # append empty cell
nickjillings@1316 126
nickjillings@1316 127 # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M]
nickjillings@1316 128 if any(ratingrow[1:]): # append to file if row non-empty (except subject name)
nickjillings@1316 129 filewriter.writerow(ratingrow)