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