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