giuliomoro@15
|
1 #!/usr/bin/python
|
giuliomoro@15
|
2
|
giuliomoro@15
|
3 import xml.etree.ElementTree as ET
|
giuliomoro@15
|
4 import os
|
giuliomoro@15
|
5 import sys
|
giuliomoro@15
|
6 import csv
|
giuliomoro@15
|
7
|
giuliomoro@15
|
8 # COMMAND LINE ARGUMENTS
|
giuliomoro@15
|
9
|
giuliomoro@15
|
10 assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\
|
giuliomoro@15
|
11 "Use: python score_parser.py [rating_folder_location]"
|
giuliomoro@15
|
12
|
giuliomoro@15
|
13 # XML results files location
|
giuliomoro@15
|
14 if len(sys.argv) == 1:
|
giuliomoro@15
|
15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
giuliomoro@15
|
16 print("Use: python score_parser.py [rating_folder_location]")
|
giuliomoro@15
|
17 print("Using default path: " + folder_name)
|
giuliomoro@15
|
18 elif len(sys.argv) == 2:
|
giuliomoro@15
|
19 folder_name = sys.argv[1] # First command line argument is folder
|
giuliomoro@15
|
20
|
giuliomoro@15
|
21 # check if folder_name exists
|
giuliomoro@15
|
22 if not os.path.exists(folder_name):
|
giuliomoro@15
|
23 #the file is not there
|
giuliomoro@15
|
24 print("Folder '"+folder_name+"' does not exist.")
|
giuliomoro@15
|
25 sys.exit() # terminate script execution
|
giuliomoro@15
|
26 elif not os.access(os.path.dirname(folder_name), os.W_OK):
|
giuliomoro@15
|
27 #the file does exist but write privileges are not given
|
giuliomoro@15
|
28 print("No write privileges in folder '"+folder_name+"'.")
|
giuliomoro@15
|
29
|
giuliomoro@15
|
30
|
giuliomoro@15
|
31 # CODE
|
giuliomoro@15
|
32
|
giuliomoro@15
|
33 # remember which files have been opened this time
|
giuliomoro@15
|
34 file_history = []
|
giuliomoro@15
|
35
|
giuliomoro@15
|
36 # get every XML file in folder
|
giuliomoro@15
|
37 for file_name in os.listdir(folder_name):
|
giuliomoro@15
|
38 if file_name.endswith(".xml"):
|
giuliomoro@15
|
39 tree = ET.parse(folder_name + '/' + file_name)
|
giuliomoro@15
|
40 root = tree.getroot()
|
giuliomoro@15
|
41
|
giuliomoro@15
|
42 # get subject ID from XML file
|
giuliomoro@15
|
43 subject_id = file_name[:-4] # file name (without extension) as subject ID
|
giuliomoro@15
|
44
|
giuliomoro@15
|
45 # get list of all pages this subject evaluated
|
giuliomoro@15
|
46 for page in root.findall("./page"): # iterate over pages
|
giuliomoro@15
|
47 page_name = page.get('ref') # get page reference ID
|
giuliomoro@15
|
48
|
giuliomoro@15
|
49 if page_name is None: # ignore 'empty' audio_holders
|
giuliomoro@15
|
50 print("WARNING: " + file_name + " contains empty audio holder. (score_parser.py)")
|
giuliomoro@15
|
51 break
|
giuliomoro@15
|
52
|
giuliomoro@15
|
53 if page.get('state') != "complete":
|
giuliomoro@15
|
54 print("WARNING: " + file_name + " contains incomplete page " +page_name+ ". (score_parser.py)")
|
giuliomoro@15
|
55 break;
|
giuliomoro@15
|
56
|
giuliomoro@15
|
57 file_name = folder_name+'/ratings/'+page_name+'-ratings.csv' # score file name
|
giuliomoro@15
|
58
|
giuliomoro@15
|
59 # create folder 'ratings' if not yet created
|
giuliomoro@15
|
60 if not os.path.exists(folder_name + '/ratings'):
|
giuliomoro@15
|
61 os.makedirs(folder_name + '/ratings')
|
giuliomoro@15
|
62
|
giuliomoro@15
|
63 # header: fragment IDs in 'alphabetical' order
|
giuliomoro@15
|
64 # go to fragment column, or create new column if it doesn't exist yet
|
giuliomoro@15
|
65
|
giuliomoro@15
|
66 # get array of audio elements and number of audio elements
|
giuliomoro@15
|
67 audiolist = page.findall("./audioelement")
|
giuliomoro@15
|
68 n_fragments = len(audiolist)
|
giuliomoro@15
|
69
|
giuliomoro@15
|
70 # get alphabetical array of fragment IDs from this subject's XML
|
giuliomoro@15
|
71 fragmentnamelist = [] # make empty list
|
giuliomoro@15
|
72 for audioelement in audiolist: # iterate over all audioelements
|
giuliomoro@15
|
73 fragmentnamelist.append(audioelement.get('ref')) # add to list
|
giuliomoro@15
|
74
|
giuliomoro@15
|
75
|
giuliomoro@15
|
76 # if file exists, get header and add any 'new' fragments not yet in the header
|
giuliomoro@15
|
77 if os.path.isfile(file_name):
|
giuliomoro@15
|
78 with open(file_name, 'r') as readfile:
|
giuliomoro@15
|
79 filereader = csv.reader(readfile, delimiter=',')
|
giuliomoro@15
|
80 headerrow = next(filereader)
|
giuliomoro@15
|
81
|
giuliomoro@15
|
82 # If file hasn't been opened yet this time, remove all rows except header
|
giuliomoro@15
|
83 if file_name not in file_history:
|
giuliomoro@15
|
84 with open(file_name, 'w') as writefile:
|
giuliomoro@15
|
85 filewriter = csv.writer(writefile, delimiter=',')
|
giuliomoro@15
|
86 headerrow = sorted(headerrow)
|
giuliomoro@15
|
87 filewriter.writerow(headerrow)
|
giuliomoro@15
|
88 file_history.append(file_name)
|
giuliomoro@15
|
89
|
giuliomoro@15
|
90 # Which of the fragments are in fragmentnamelist but not in headerrow?
|
giuliomoro@15
|
91 newfragments = list(set(fragmentnamelist)-set(headerrow))
|
giuliomoro@15
|
92 newfragments = sorted(newfragments) # new fragments in alphabetical order
|
giuliomoro@15
|
93 # If not empty, read file and rewrite adding extra columns
|
giuliomoro@15
|
94 if newfragments: # if not empty
|
giuliomoro@15
|
95 with open('temp.csv', 'w') as writefile:
|
giuliomoro@15
|
96 filewriter = csv.writer(writefile, delimiter=',')
|
giuliomoro@15
|
97 filewriter.writerow(headerrow + newfragments) # write new header
|
giuliomoro@15
|
98 with open(file_name, 'r') as readfile:
|
giuliomoro@15
|
99 filereader = csv.reader(readfile, delimiter=',')
|
giuliomoro@15
|
100 next(filereader) # skip header
|
giuliomoro@15
|
101 for row in filereader: # rewrite row plus empty cells for every new fragment name
|
giuliomoro@15
|
102 filewriter.writerow(row + ['']*len(newfragments))
|
giuliomoro@15
|
103 os.rename('temp.csv', file_name) # replace old file with temp file
|
giuliomoro@15
|
104 headerrow = headerrow + newfragments
|
giuliomoro@15
|
105
|
giuliomoro@15
|
106
|
giuliomoro@15
|
107 # if file does not exist yet, create file and make header
|
giuliomoro@15
|
108 else:
|
giuliomoro@15
|
109 headerrow = sorted(fragmentnamelist) # sort alphabetically
|
giuliomoro@15
|
110 headerrow.insert(0,'')
|
giuliomoro@15
|
111 fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist
|
giuliomoro@15
|
112 with open(file_name, 'w') as writefile:
|
giuliomoro@15
|
113 filewriter = csv.writer(writefile, delimiter=',')
|
giuliomoro@15
|
114 filewriter.writerow(headerrow)
|
giuliomoro@15
|
115 file_history.append(file_name)
|
giuliomoro@15
|
116
|
giuliomoro@15
|
117 # open file to write for this page
|
giuliomoro@15
|
118 writefile = open(file_name, 'a')
|
giuliomoro@15
|
119 filewriter = csv.writer(writefile, delimiter=',')
|
giuliomoro@15
|
120
|
giuliomoro@15
|
121 # prepare row to be written for this subject for this page
|
giuliomoro@15
|
122 ratingrow = [subject_id]
|
giuliomoro@15
|
123
|
giuliomoro@15
|
124 # get scores related to fragment [id]
|
giuliomoro@15
|
125 for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column)
|
giuliomoro@15
|
126 elementvalue = page.find("./audioelement/[@ref='"
|
giuliomoro@15
|
127 + fragmentname
|
giuliomoro@15
|
128 + "']/value")
|
giuliomoro@15
|
129 if hasattr(elementvalue, 'text'): # if rating for this fragment exists
|
giuliomoro@15
|
130 ratingrow.append(elementvalue.text) # add to rating row
|
giuliomoro@15
|
131 else: # if this subject has not rated this fragment
|
giuliomoro@15
|
132 ratingrow.append('') # append empty cell
|
giuliomoro@15
|
133
|
giuliomoro@15
|
134 # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M]
|
giuliomoro@15
|
135 if any(ratingrow[1:]): # append to file if row non-empty (except subject name)
|
giuliomoro@15
|
136 filewriter.writerow(ratingrow)
|