annotate scripts/score_parser.py @ 1542:db0823f2210d

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