annotate scripts/score_parser.py @ 1533:317faa29ab11

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