annotate scripts/score_parser.py @ 1553:99d738b5732c

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