annotate scripts/score_parser.py @ 1187:f08343647d2b

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