annotate scripts/score_parser.py @ 1306:cc55cc323592

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