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