nickjillings@1528: #!/usr/bin/python nickjillings@1528: nickjillings@1528: import xml.etree.ElementTree as ET nickjillings@1528: import os nickjillings@1528: import csv nickjillings@1528: nickjillings@1528: #TODO Remove DEBUG statements nickjillings@1528: nickjillings@1528: # XML results files location (modify as needed): nickjillings@1528: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder nickjillings@1528: nickjillings@1528: # get every XML file in folder nickjillings@1528: for file in os.listdir(folder_name): nickjillings@1528: if file.endswith(".xml"): nickjillings@1528: tree = ET.parse(folder_name + '/' + file) nickjillings@1528: root = tree.getroot() nickjillings@1528: #print "DEBUG Reading " + file + "..." nickjillings@1528: nickjillings@1528: # get subject ID from XML file nickjillings@1528: subject_id = file[:-4] # file name (without extension) as subject ID nickjillings@1528: nickjillings@1528: # get list of all pages this subject evaluated nickjillings@1528: for audioholder in root.findall("./audioholder"): # iterate over pages nickjillings@1528: page_name = audioholder.get('id') # get page name nickjillings@1528: nickjillings@1528: if page_name is None: # ignore 'empty' audio_holders nickjillings@1528: print "WARNING: " + file + " contains empty audio holder. (score_parser.py)" nickjillings@1528: break nickjillings@1528: nickjillings@1528: file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name nickjillings@1528: nickjillings@1528: # create folder 'ratings if not yet created nickjillings@1528: if not os.path.exists(folder_name + '/ratings'): nickjillings@1528: os.makedirs(folder_name + '/ratings') nickjillings@1528: nickjillings@1528: # header: fragment IDs in 'alphabetical' order nickjillings@1528: # go to fragment column, or create new column if it doesn't exist yet nickjillings@1528: nickjillings@1528: # get array of audio elements and number of audio elements nickjillings@1528: audiolist = audioholder.findall("./audioelement") nickjillings@1528: n_fragments = len(audiolist) nickjillings@1528: nickjillings@1528: # get alphabetical array of fragment IDs from this subject's XML nickjillings@1528: fragmentnamelist = [] # make empty list nickjillings@1528: for audioelement in audiolist: # iterate over all audioelements nickjillings@1528: fragmentnamelist.append(audioelement.get('id')) # add to list nickjillings@1528: nickjillings@1528: nickjillings@1528: # if file exists, get header and add 'new' fragments nickjillings@1528: if os.path.isfile(file_name): nickjillings@1528: #print "DEBUG file " + file_name + " already exists - reading header" nickjillings@1528: with open(file_name, 'r') as readfile: nickjillings@1528: filereader = csv.reader(readfile, delimiter=',') nickjillings@1528: headerrow = filereader.next() nickjillings@1528: nickjillings@1528: # Which of the fragmentes are in fragmentnamelist but not in headerrow? nickjillings@1528: newfragments = list(set(fragmentnamelist)-set(headerrow)) nickjillings@1528: newfragments = sorted(newfragments) # new fragments in alphabetical order nickjillings@1528: # If not empty, read file and rewrite adding extra columns nickjillings@1528: if newfragments: # if not empty nickjillings@1528: #print "DEBUG New fragments found: " + str(newfragments) nickjillings@1528: with open('temp.csv', 'w') as writefile: nickjillings@1528: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1528: filewriter.writerow(headerrow + newfragments) # write new header nickjillings@1528: #print " "+str(headerrow + newfragments) # DEBUG nickjillings@1528: with open(file_name, 'r') as readfile: nickjillings@1528: filereader = csv.reader(readfile, delimiter=',') nickjillings@1528: filereader.next() # skip header nickjillings@1528: for row in filereader: # rewrite row plus empty cells for every new fragment name nickjillings@1528: #print " Old row: " + str(row) # DEBUG nickjillings@1528: filewriter.writerow(row + ['']*len(newfragments)) nickjillings@1528: #print " New row: " + str(row + ['']*len(newfragments)) # DEBUG nickjillings@1528: os.rename('temp.csv', file_name) # replace old file with temp file nickjillings@1528: headerrow = headerrow + newfragments nickjillings@1528: #print "DEBUG New header row: " + str(headerrow) nickjillings@1528: nickjillings@1528: # if not, create file and make header nickjillings@1528: else: nickjillings@1528: #print ["DEBUG file " + file_name + " doesn't exist yet - making new one"] nickjillings@1528: headerrow = sorted(fragmentnamelist) # sort alphabetically nickjillings@1528: headerrow.insert(0,'') nickjillings@1528: fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist nickjillings@1528: with open(file_name, 'w') as writefile: nickjillings@1528: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1528: filewriter.writerow(headerrow) nickjillings@1528: nickjillings@1528: # open file to write for this page nickjillings@1528: writefile = open(file_name, 'a') nickjillings@1528: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1528: nickjillings@1528: # prepare row to be written for this subject for this page nickjillings@1528: ratingrow = [subject_id] nickjillings@1528: nickjillings@1528: # get scores related to fragment [id] nickjillings@1528: for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column) nickjillings@1528: elementvalue = audioholder.find("./audioelement/[@id='" nickjillings@1528: + fragmentname nickjillings@1528: + "']/value") nickjillings@1528: if hasattr(elementvalue, 'text'): # if rating for this fragment exists nickjillings@1528: ratingrow.append(elementvalue.text) # add to rating row nickjillings@1528: else: # if this subject has not rated this fragment nickjillings@1528: ratingrow.append('') # append empty cell nickjillings@1528: nickjillings@1528: # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M] nickjillings@1528: if any(ratingrow[1:]): # append to file if row non-empty (except subject name) nickjillings@1528: filewriter.writerow(ratingrow) nickjillings@1528: