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