annotate scripts/score_parser.py @ 1555:45b469cfcd51

Scripts: score_parser.py: ignore empty rows (subject with no ratings for current song)
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 29 Jun 2015 19:02:48 +0100
parents 276482e6215f
children 7a8fcf04aad3
rev   line source
b@1554 1 #!/usr/bin/python
b@1554 2
nickjillings@1542 3 import xml.etree.ElementTree as ET
nickjillings@1542 4 import os
nickjillings@1542 5 import csv
nickjillings@1542 6
nickjillings@1542 7 #TODO Remove DEBUG statements
nickjillings@1542 8
b@1553 9 # XML results files location (modify as needed):
b@1553 10 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
b@1553 11
nickjillings@1542 12 # get every XML file in folder
b@1555 13 for file in os.listdir(folder_name):
nickjillings@1542 14 if file.endswith(".xml"):
b@1553 15 tree = ET.parse(folder_name + '/' + file)
nickjillings@1542 16 root = tree.getroot()
b@1554 17 #print "DEBUG Reading " + file + "..."
nickjillings@1542 18
nickjillings@1542 19 # get subject ID from XML file
b@1554 20 subject_id = file[:-4] # file name (without extension) as subject ID
nickjillings@1542 21
nickjillings@1542 22 # get list of all pages this subject evaluated
nickjillings@1542 23 for audioholder in root.findall("./audioholder"): # iterate over pages
nickjillings@1542 24 page_name = audioholder.get('id') # get page name
b@1554 25
nickjillings@1542 26 if page_name is None: # ignore 'empty' audio_holders
nickjillings@1542 27 break
nickjillings@1542 28
b@1553 29 file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name
nickjillings@1542 30
nickjillings@1542 31 # create folder 'ratings if not yet created
b@1553 32 if not os.path.exists(folder_name + '/ratings'):
b@1553 33 os.makedirs(folder_name + '/ratings')
nickjillings@1542 34
nickjillings@1542 35 # header: fragment IDs in 'alphabetical' order
nickjillings@1542 36 # go to fragment column, or create new column if it doesn't exist yet
nickjillings@1542 37
nickjillings@1542 38 # get array of audio elements and number of audio elements
b@1553 39 audiolist = audioholder.findall("./audioelement")
nickjillings@1542 40 n_fragments = len(audiolist)
nickjillings@1542 41
nickjillings@1542 42 # get alphabetical array of fragment IDs from this subject's XML
nickjillings@1542 43 fragmentnamelist = [] # make empty list
nickjillings@1542 44 for audioelement in audiolist: # iterate over all audioelements
nickjillings@1542 45 fragmentnamelist.append(audioelement.get('id')) # add to list
nickjillings@1542 46
nickjillings@1542 47
nickjillings@1542 48 # if file exists, get header and add 'new' fragments
nickjillings@1542 49 if os.path.isfile(file_name):
b@1554 50 #print "DEBUG file " + file_name + " already exists - reading header"
nickjillings@1542 51 with open(file_name, 'r') as readfile:
nickjillings@1542 52 filereader = csv.reader(readfile, delimiter=',')
nickjillings@1542 53 headerrow = filereader.next()
nickjillings@1542 54
b@1554 55 # Which of the fragmentes are in fragmentnamelist but not in headerrow?
b@1554 56 newfragments = list(set(fragmentnamelist)-set(headerrow))
b@1554 57 newfragments = sorted(newfragments) # new fragments in alphabetical order
b@1554 58 # If not empty, read file and rewrite adding extra columns
b@1554 59 if newfragments: # if not empty
b@1554 60 #print "DEBUG New fragments found: " + str(newfragments)
b@1554 61 with open('temp.csv', 'w') as writefile:
b@1554 62 filewriter = csv.writer(writefile, delimiter=',')
b@1554 63 filewriter.writerow(headerrow + newfragments) # write new header
b@1554 64 #print " "+str(headerrow + newfragments) # DEBUG
b@1554 65 with open(file_name, 'r') as readfile:
b@1554 66 filereader = csv.reader(readfile, delimiter=',')
b@1554 67 filereader.next() # skip header
nickjillings@1542 68 for row in filereader: # rewrite row plus empty cells for every new fragment name
b@1554 69 #print " Old row: " + str(row) # DEBUG
nickjillings@1542 70 filewriter.writerow(row + ['']*len(newfragments))
b@1554 71 #print " New row: " + str(row + ['']*len(newfragments)) # DEBUG
b@1554 72 os.rename('temp.csv', file_name) # replace old file with temp file
b@1554 73 headerrow = headerrow + newfragments
b@1554 74 #print "DEBUG New header row: " + str(headerrow)
nickjillings@1542 75
nickjillings@1542 76 # if not, create file and make header
nickjillings@1542 77 else:
nickjillings@1542 78 #print ["DEBUG file " + file_name + " doesn't exist yet - making new one"]
nickjillings@1542 79 headerrow = sorted(fragmentnamelist) # sort alphabetically
nickjillings@1542 80 headerrow.insert(0,'')
nickjillings@1542 81 fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist
nickjillings@1542 82 with open(file_name, 'w') as writefile:
nickjillings@1542 83 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1542 84 filewriter.writerow(headerrow)
nickjillings@1542 85
nickjillings@1542 86 # open file to write for this page
nickjillings@1542 87 writefile = open(file_name, 'a')
nickjillings@1542 88 filewriter = csv.writer(writefile, delimiter=',')
nickjillings@1542 89
nickjillings@1542 90 # prepare row to be written for this subject for this page
nickjillings@1542 91 ratingrow = [subject_id]
nickjillings@1542 92
nickjillings@1542 93 # get scores related to fragment [id]
nickjillings@1542 94 for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column)
b@1553 95 elementvalue = audioholder.find("./audioelement/[@id='"
nickjillings@1542 96 + fragmentname
nickjillings@1542 97 + "']/value")
nickjillings@1542 98 if hasattr(elementvalue, 'text'): # if rating for this fragment exists
nickjillings@1542 99 ratingrow.append(elementvalue.text) # add to rating row
nickjillings@1542 100 else: # if this subject has not rated this fragment
nickjillings@1542 101 ratingrow.append('') # append empty cell
nickjillings@1542 102
nickjillings@1542 103 # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M]
b@1555 104 if any(ratingrow[1:]): # append to file if row non-empty (except subject name)
b@1555 105 filewriter.writerow(ratingrow)
nickjillings@1542 106