annotate scripts/score_parser.py @ 798:397f19747594

Instructions update (WIP); removed basic instructions from README.
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Fri, 18 Dec 2015 01:45:54 +0000
parents 519baf8648a9
children 8540d153caec
rev   line source
nicholas@763 1 #!/usr/bin/python
nicholas@763 2
nicholas@763 3 import xml.etree.ElementTree as ET
nicholas@763 4 import os
nicholas@763 5 import sys
nicholas@763 6 import csv
nicholas@763 7
nicholas@763 8 # COMMAND LINE ARGUMENTS
nicholas@763 9
nicholas@763 10 assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\
nicholas@763 11 "Use: python score_parser.py [rating_folder_location]"
nicholas@763 12
nicholas@763 13 # XML results files location
nicholas@763 14 if len(sys.argv) == 1:
nicholas@763 15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nicholas@763 16 print "Use: python score_parser.py [rating_folder_location]"
nicholas@763 17 print "Using default path: " + folder_name
nicholas@763 18 elif len(sys.argv) == 2:
nicholas@763 19 folder_name = sys.argv[1] # First command line argument is folder
nicholas@763 20
nicholas@763 21 # check if folder_name exists
nicholas@763 22 if not os.path.exists(folder_name):
nicholas@763 23 #the file is not there
nicholas@763 24 print "Folder '"+folder_name+"' does not exist."
nicholas@763 25 sys.exit() # terminate script execution
nicholas@763 26 elif not os.access(os.path.dirname(folder_name), os.W_OK):
nicholas@763 27 #the file does exist but write privileges are not given
nicholas@763 28 print "No write privileges in folder '"+folder_name+"'."
nicholas@763 29
nicholas@763 30
nicholas@763 31 # CODE
nicholas@763 32
nicholas@763 33 # remember which files have been opened this time
nicholas@763 34 file_history = []
nicholas@763 35
nicholas@763 36 # get every XML file in folder
nicholas@763 37 for file in os.listdir(folder_name):
nicholas@763 38 if file.endswith(".xml"):
nicholas@763 39 tree = ET.parse(folder_name + '/' + file)
nicholas@763 40 root = tree.getroot()
nicholas@763 41
nicholas@763 42 # get subject ID from XML file
nicholas@763 43 subject_id = file[:-4] # file name (without extension) as subject ID
nicholas@763 44
nicholas@763 45 # get list of all pages this subject evaluated
nicholas@763 46 for audioholder in root.findall("./audioholder"): # iterate over pages
nicholas@763 47 page_name = audioholder.get('id') # get page name
nicholas@763 48
nicholas@763 49 if page_name is None: # ignore 'empty' audio_holders
nicholas@763 50 print "WARNING: " + file + " contains empty audio holder. (score_parser.py)"
nicholas@763 51 break
nicholas@763 52
nicholas@763 53 file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name
nicholas@763 54
nicholas@763 55 # create folder 'ratings' if not yet created
nicholas@763 56 if not os.path.exists(folder_name + '/ratings'):
nicholas@763 57 os.makedirs(folder_name + '/ratings')
nicholas@763 58
nicholas@763 59 # header: fragment IDs in 'alphabetical' order
nicholas@763 60 # go to fragment column, or create new column if it doesn't exist yet
nicholas@763 61
nicholas@763 62 # get array of audio elements and number of audio elements
nicholas@763 63 audiolist = audioholder.findall("./audioelement")
nicholas@763 64 n_fragments = len(audiolist)
nicholas@763 65
nicholas@763 66 # get alphabetical array of fragment IDs from this subject's XML
nicholas@763 67 fragmentnamelist = [] # make empty list
nicholas@763 68 for audioelement in audiolist: # iterate over all audioelements
nicholas@763 69 fragmentnamelist.append(audioelement.get('id')) # add to list
nicholas@763 70
nicholas@763 71 # if file exists, get header and add 'new' fragments
nicholas@763 72 if os.path.isfile(file_name):
nicholas@763 73 with open(file_name, 'r') as readfile:
nicholas@763 74 filereader = csv.reader(readfile, delimiter=',')
nicholas@763 75 headerrow = filereader.next()
nicholas@763 76
nicholas@763 77 # If file hasn't been opened yet this time, remove all rows except header
nicholas@763 78 if file_name not in file_history:
nicholas@763 79 with open(file_name, 'w') as writefile:
nicholas@763 80 filewriter = csv.writer(writefile, delimiter=',')
nicholas@763 81 headerrow = sorted(headerrow)
nicholas@763 82 filewriter.writerow(headerrow)
nicholas@763 83 file_history.append(file_name)
nicholas@763 84
nicholas@763 85 # Which of the fragmentes are in fragmentnamelist but not in headerrow?
nicholas@763 86 newfragments = list(set(fragmentnamelist)-set(headerrow))
nicholas@763 87 newfragments = sorted(newfragments) # new fragments in alphabetical order
nicholas@763 88 # If not empty, read file and rewrite adding extra columns
nicholas@763 89 if newfragments: # if not empty
nicholas@763 90 with open('temp.csv', 'w') as writefile:
nicholas@763 91 filewriter = csv.writer(writefile, delimiter=',')
nicholas@763 92 filewriter.writerow(headerrow + newfragments) # write new header
nicholas@763 93 with open(file_name, 'r') as readfile:
nicholas@763 94 filereader = csv.reader(readfile, delimiter=',')
nicholas@763 95 filereader.next() # skip header
nicholas@763 96 for row in filereader: # rewrite row plus empty cells for every new fragment name
nicholas@763 97 filewriter.writerow(row + ['']*len(newfragments))
nicholas@763 98 os.rename('temp.csv', file_name) # replace old file with temp file
nicholas@763 99 headerrow = headerrow + newfragments
BrechtDeMan@798 100
nicholas@763 101 # if not, create file and make header
nicholas@763 102 else:
nicholas@763 103 headerrow = sorted(fragmentnamelist) # sort alphabetically
nicholas@763 104 headerrow.insert(0,'')
nicholas@763 105 fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist
nicholas@763 106 with open(file_name, 'w') as writefile:
nicholas@763 107 filewriter = csv.writer(writefile, delimiter=',')
nicholas@763 108 filewriter.writerow(headerrow)
nicholas@763 109
nicholas@763 110 # open file to write for this page
nicholas@763 111 writefile = open(file_name, 'a')
nicholas@763 112 filewriter = csv.writer(writefile, delimiter=',')
nicholas@763 113
nicholas@763 114 # prepare row to be written for this subject for this page
nicholas@763 115 ratingrow = [subject_id]
nicholas@763 116
nicholas@763 117 # get scores related to fragment [id]
nicholas@763 118 for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column)
nicholas@763 119 elementvalue = audioholder.find("./audioelement/[@id='"
nicholas@763 120 + fragmentname
nicholas@763 121 + "']/value")
nicholas@763 122 if hasattr(elementvalue, 'text'): # if rating for this fragment exists
nicholas@763 123 ratingrow.append(elementvalue.text) # add to rating row
nicholas@763 124 else: # if this subject has not rated this fragment
nicholas@763 125 ratingrow.append('') # append empty cell
nicholas@763 126
nicholas@763 127 # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M]
nicholas@763 128 if any(ratingrow[1:]): # append to file if row non-empty (except subject name)
nicholas@763 129 filewriter.writerow(ratingrow)