BrechtDeMan@884
|
1 #!/usr/bin/python
|
BrechtDeMan@884
|
2
|
nicholas@872
|
3 import xml.etree.ElementTree as ET
|
nicholas@872
|
4 import os
|
nicholas@872
|
5 import csv
|
nicholas@872
|
6
|
nicholas@872
|
7 #TODO Remove DEBUG statements
|
nicholas@872
|
8
|
BrechtDeMan@883
|
9 # XML results files location (modify as needed):
|
BrechtDeMan@883
|
10 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
BrechtDeMan@883
|
11
|
nicholas@872
|
12 # get every XML file in folder
|
BrechtDeMan@883
|
13 for file in os.listdir(folder_name): # You have to put this in folder where output XML files are.
|
nicholas@872
|
14 if file.endswith(".xml"):
|
BrechtDeMan@883
|
15 tree = ET.parse(folder_name + '/' + file)
|
nicholas@872
|
16 root = tree.getroot()
|
BrechtDeMan@884
|
17 #print "DEBUG Reading " + file + "..."
|
nicholas@872
|
18
|
nicholas@872
|
19 # get subject ID from XML file
|
BrechtDeMan@884
|
20 subject_id = file[:-4] # file name (without extension) as subject ID
|
nicholas@872
|
21
|
nicholas@872
|
22 # get list of all pages this subject evaluated
|
nicholas@872
|
23 for audioholder in root.findall("./audioholder"): # iterate over pages
|
nicholas@872
|
24 page_name = audioholder.get('id') # get page name
|
BrechtDeMan@884
|
25
|
nicholas@872
|
26 if page_name is None: # ignore 'empty' audio_holders
|
nicholas@872
|
27 break
|
nicholas@872
|
28
|
BrechtDeMan@883
|
29 file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name
|
nicholas@872
|
30
|
nicholas@872
|
31 # create folder 'ratings if not yet created
|
BrechtDeMan@883
|
32 if not os.path.exists(folder_name + '/ratings'):
|
BrechtDeMan@883
|
33 os.makedirs(folder_name + '/ratings')
|
nicholas@872
|
34
|
nicholas@872
|
35 # header: fragment IDs in 'alphabetical' order
|
nicholas@872
|
36 # go to fragment column, or create new column if it doesn't exist yet
|
nicholas@872
|
37
|
nicholas@872
|
38 # get array of audio elements and number of audio elements
|
BrechtDeMan@883
|
39 audiolist = audioholder.findall("./audioelement")
|
nicholas@872
|
40 n_fragments = len(audiolist)
|
nicholas@872
|
41
|
nicholas@872
|
42 # get alphabetical array of fragment IDs from this subject's XML
|
nicholas@872
|
43 fragmentnamelist = [] # make empty list
|
nicholas@872
|
44 for audioelement in audiolist: # iterate over all audioelements
|
nicholas@872
|
45 fragmentnamelist.append(audioelement.get('id')) # add to list
|
nicholas@872
|
46
|
nicholas@872
|
47
|
nicholas@872
|
48 # if file exists, get header and add 'new' fragments
|
nicholas@872
|
49 if os.path.isfile(file_name):
|
BrechtDeMan@884
|
50 #print "DEBUG file " + file_name + " already exists - reading header"
|
nicholas@872
|
51 with open(file_name, 'r') as readfile:
|
nicholas@872
|
52 filereader = csv.reader(readfile, delimiter=',')
|
nicholas@872
|
53 headerrow = filereader.next()
|
nicholas@872
|
54
|
BrechtDeMan@884
|
55 # Which of the fragmentes are in fragmentnamelist but not in headerrow?
|
BrechtDeMan@884
|
56 newfragments = list(set(fragmentnamelist)-set(headerrow))
|
BrechtDeMan@884
|
57 newfragments = sorted(newfragments) # new fragments in alphabetical order
|
BrechtDeMan@884
|
58 # If not empty, read file and rewrite adding extra columns
|
BrechtDeMan@884
|
59 if newfragments: # if not empty
|
BrechtDeMan@884
|
60 print ' '+page_name+','+file_name+','+subject_id
|
BrechtDeMan@884
|
61 #print "DEBUG New fragments found: " + str(newfragments)
|
BrechtDeMan@884
|
62 with open('temp.csv', 'w') as writefile:
|
BrechtDeMan@884
|
63 filewriter = csv.writer(writefile, delimiter=',')
|
BrechtDeMan@884
|
64 filewriter.writerow(headerrow + newfragments) # write new header
|
BrechtDeMan@884
|
65 #print " "+str(headerrow + newfragments) # DEBUG
|
BrechtDeMan@884
|
66 with open(file_name, 'r') as readfile:
|
BrechtDeMan@884
|
67 filereader = csv.reader(readfile, delimiter=',')
|
BrechtDeMan@884
|
68 filereader.next() # skip header
|
nicholas@872
|
69 for row in filereader: # rewrite row plus empty cells for every new fragment name
|
BrechtDeMan@884
|
70 #print " Old row: " + str(row) # DEBUG
|
nicholas@872
|
71 filewriter.writerow(row + ['']*len(newfragments))
|
BrechtDeMan@884
|
72 #print " New row: " + str(row + ['']*len(newfragments)) # DEBUG
|
BrechtDeMan@884
|
73 os.rename('temp.csv', file_name) # replace old file with temp file
|
BrechtDeMan@884
|
74 headerrow = headerrow + newfragments
|
BrechtDeMan@884
|
75 #print "DEBUG New header row: " + str(headerrow)
|
nicholas@872
|
76
|
nicholas@872
|
77 # if not, create file and make header
|
nicholas@872
|
78 else:
|
nicholas@872
|
79 #print ["DEBUG file " + file_name + " doesn't exist yet - making new one"]
|
nicholas@872
|
80 headerrow = sorted(fragmentnamelist) # sort alphabetically
|
nicholas@872
|
81 headerrow.insert(0,'')
|
nicholas@872
|
82 fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist
|
nicholas@872
|
83 with open(file_name, 'w') as writefile:
|
nicholas@872
|
84 filewriter = csv.writer(writefile, delimiter=',')
|
nicholas@872
|
85 filewriter.writerow(headerrow)
|
nicholas@872
|
86
|
nicholas@872
|
87 # open file to write for this page
|
nicholas@872
|
88 writefile = open(file_name, 'a')
|
nicholas@872
|
89 filewriter = csv.writer(writefile, delimiter=',')
|
nicholas@872
|
90
|
nicholas@872
|
91 # prepare row to be written for this subject for this page
|
nicholas@872
|
92 ratingrow = [subject_id]
|
nicholas@872
|
93
|
nicholas@872
|
94 # get scores related to fragment [id]
|
nicholas@872
|
95 for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column)
|
BrechtDeMan@883
|
96 elementvalue = audioholder.find("./audioelement/[@id='"
|
nicholas@872
|
97 + fragmentname
|
nicholas@872
|
98 + "']/value")
|
nicholas@872
|
99 if hasattr(elementvalue, 'text'): # if rating for this fragment exists
|
nicholas@872
|
100 ratingrow.append(elementvalue.text) # add to rating row
|
nicholas@872
|
101 else: # if this subject has not rated this fragment
|
nicholas@872
|
102 ratingrow.append('') # append empty cell
|
nicholas@872
|
103
|
nicholas@872
|
104 # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M]
|
nicholas@872
|
105 filewriter.writerow(ratingrow)
|
nicholas@872
|
106
|