annotate scripts/score_parser.py @ 1030:057c6b039f4e

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