annotate scripts/score_parser.py @ 872:ca4ae613f1dd

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