nickjillings@1316: #!/usr/bin/python nickjillings@1316: nickjillings@1316: import xml.etree.ElementTree as ET nickjillings@1316: import os nickjillings@1316: import sys nickjillings@1316: import csv nickjillings@1316: nickjillings@1316: # COMMAND LINE ARGUMENTS nickjillings@1316: nickjillings@1316: assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\ nickjillings@1316: "Use: python score_parser.py [rating_folder_location]" nickjillings@1316: nickjillings@1316: # XML results files location nickjillings@1316: if len(sys.argv) == 1: nickjillings@1316: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder nickjillings@1316: print "Use: python score_parser.py [rating_folder_location]" nickjillings@1316: print "Using default path: " + folder_name nickjillings@1316: elif len(sys.argv) == 2: nickjillings@1316: folder_name = sys.argv[1] # First command line argument is folder nickjillings@1316: nickjillings@1316: # check if folder_name exists nickjillings@1316: if not os.path.exists(folder_name): nickjillings@1316: #the file is not there nickjillings@1316: print "Folder '"+folder_name+"' does not exist." nickjillings@1316: sys.exit() # terminate script execution nickjillings@1316: elif not os.access(os.path.dirname(folder_name), os.W_OK): nickjillings@1316: #the file does exist but write privileges are not given nickjillings@1316: print "No write privileges in folder '"+folder_name+"'." nickjillings@1316: nickjillings@1316: nickjillings@1316: # CODE nickjillings@1316: nickjillings@1316: # remember which files have been opened this time nickjillings@1316: file_history = [] nickjillings@1316: nickjillings@1316: # get every XML file in folder nickjillings@1316: for file in os.listdir(folder_name): nickjillings@1316: if file.endswith(".xml"): nickjillings@1316: tree = ET.parse(folder_name + '/' + file) nickjillings@1316: root = tree.getroot() nickjillings@1316: nickjillings@1316: # get subject ID from XML file nickjillings@1316: subject_id = file[:-4] # file name (without extension) as subject ID nickjillings@1316: nickjillings@1316: # get list of all pages this subject evaluated nickjillings@1316: for audioholder in root.findall("./audioholder"): # iterate over pages nickjillings@1316: page_name = audioholder.get('id') # get page name nickjillings@1316: nickjillings@1316: if page_name is None: # ignore 'empty' audio_holders nickjillings@1316: print "WARNING: " + file + " contains empty audio holder. (score_parser.py)" nickjillings@1316: break nickjillings@1316: nickjillings@1316: file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name nickjillings@1316: nickjillings@1316: # create folder 'ratings' if not yet created nickjillings@1316: if not os.path.exists(folder_name + '/ratings'): nickjillings@1316: os.makedirs(folder_name + '/ratings') nickjillings@1316: nickjillings@1316: # header: fragment IDs in 'alphabetical' order nickjillings@1316: # go to fragment column, or create new column if it doesn't exist yet nickjillings@1316: nickjillings@1316: # get array of audio elements and number of audio elements nickjillings@1316: audiolist = audioholder.findall("./audioelement") nickjillings@1316: n_fragments = len(audiolist) nickjillings@1316: nickjillings@1316: # get alphabetical array of fragment IDs from this subject's XML nickjillings@1316: fragmentnamelist = [] # make empty list nickjillings@1316: for audioelement in audiolist: # iterate over all audioelements nickjillings@1316: fragmentnamelist.append(audioelement.get('id')) # add to list nickjillings@1316: nickjillings@1316: # if file exists, get header and add 'new' fragments nickjillings@1316: if os.path.isfile(file_name): nickjillings@1316: with open(file_name, 'r') as readfile: nickjillings@1316: filereader = csv.reader(readfile, delimiter=',') nickjillings@1316: headerrow = filereader.next() nickjillings@1316: nickjillings@1316: # If file hasn't been opened yet this time, remove all rows except header nickjillings@1316: if file_name not in file_history: nickjillings@1316: with open(file_name, 'w') as writefile: nickjillings@1316: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1316: headerrow = sorted(headerrow) nickjillings@1316: filewriter.writerow(headerrow) nickjillings@1316: file_history.append(file_name) nickjillings@1316: nickjillings@1316: # Which of the fragmentes are in fragmentnamelist but not in headerrow? nickjillings@1316: newfragments = list(set(fragmentnamelist)-set(headerrow)) nickjillings@1316: newfragments = sorted(newfragments) # new fragments in alphabetical order nickjillings@1316: # If not empty, read file and rewrite adding extra columns nickjillings@1316: if newfragments: # if not empty nickjillings@1316: with open('temp.csv', 'w') as writefile: nickjillings@1316: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1316: filewriter.writerow(headerrow + newfragments) # write new header nickjillings@1316: with open(file_name, 'r') as readfile: nickjillings@1316: filereader = csv.reader(readfile, delimiter=',') nickjillings@1316: filereader.next() # skip header nickjillings@1316: for row in filereader: # rewrite row plus empty cells for every new fragment name nickjillings@1316: filewriter.writerow(row + ['']*len(newfragments)) nickjillings@1316: os.rename('temp.csv', file_name) # replace old file with temp file nickjillings@1316: headerrow = headerrow + newfragments nickjillings@1316: nickjillings@1316: # if not, create file and make header nickjillings@1316: else: nickjillings@1316: headerrow = sorted(fragmentnamelist) # sort alphabetically nickjillings@1316: headerrow.insert(0,'') nickjillings@1316: fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist nickjillings@1316: with open(file_name, 'w') as writefile: nickjillings@1316: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1316: filewriter.writerow(headerrow) nickjillings@1316: nickjillings@1316: # open file to write for this page nickjillings@1316: writefile = open(file_name, 'a') nickjillings@1316: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1316: nickjillings@1316: # prepare row to be written for this subject for this page nickjillings@1316: ratingrow = [subject_id] nickjillings@1316: nickjillings@1316: # get scores related to fragment [id] nickjillings@1316: for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column) nickjillings@1316: elementvalue = audioholder.find("./audioelement/[@id='" nickjillings@1316: + fragmentname nickjillings@1316: + "']/value") nickjillings@1316: if hasattr(elementvalue, 'text'): # if rating for this fragment exists nickjillings@1316: ratingrow.append(elementvalue.text) # add to rating row nickjillings@1316: else: # if this subject has not rated this fragment nickjillings@1316: ratingrow.append('') # append empty cell nickjillings@1316: nickjillings@1316: # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M] nickjillings@1316: if any(ratingrow[1:]): # append to file if row non-empty (except subject name) nickjillings@1316: filewriter.writerow(ratingrow)