annotate python/score_parser.py @ 2720:a6a0d2b786af

Merge branch 'vnext' into hot_fix # Conflicts: # js/core.js
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 14 Apr 2017 16:28:19 +0100
parents dde81c372fdc
children 151ae5a4b979
rev   line source
b@2264 1 #!/usr/bin/python
b@2264 2
b@2264 3 import xml.etree.ElementTree as ET
b@2264 4 import os
b@2264 5 import sys
b@2264 6 import csv
b@2264 7
b@2264 8 # COMMAND LINE ARGUMENTS
b@2264 9
b@2264 10 assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\
b@2264 11 "Use: python score_parser.py [rating_folder_location]"
b@2264 12
b@2264 13 # XML results files location
b@2264 14 if len(sys.argv) == 1:
b@2264 15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
b@2266 16 print("Use: python score_parser.py [rating_folder_location]")
b@2266 17 print("Using default path: " + folder_name)
b@2264 18 elif len(sys.argv) == 2:
b@2264 19 folder_name = sys.argv[1] # First command line argument is folder
b@2264 20
b@2264 21 # check if folder_name exists
b@2264 22 if not os.path.exists(folder_name):
b@2264 23 #the file is not there
b@2266 24 print("Folder '"+folder_name+"' does not exist.")
b@2264 25 sys.exit() # terminate script execution
b@2264 26 elif not os.access(os.path.dirname(folder_name), os.W_OK):
b@2264 27 #the file does exist but write privileges are not given
b@2266 28 print("No write privileges in folder '"+folder_name+"'.")
nicholas@2524 29
b@2264 30
b@2264 31 # CODE
b@2264 32
nicholas@2524 33 storage = {}
b@2264 34
nicholas@2524 35 # create folder 'ratings' if not yet created
nicholas@2524 36 if not os.path.exists(folder_name + '/ratings'):
nicholas@2524 37 os.makedirs(folder_name + '/ratings')
nicholas@2524 38
nicholas@2524 39 # Get every XML file in the folder
b@2264 40 for file_name in os.listdir(folder_name):
nicholas@2524 41 if (file_name.endswith(".xml")):
b@2264 42 tree = ET.parse(folder_name + '/' + file_name)
b@2264 43 root = tree.getroot()
nicholas@2524 44
nicholas@2524 45 subject_id = root.get('key');
nicholas@2524 46
nicholas@2524 47 # get the list of the pages this subject evaluated
nicholas@2524 48 for page in root.findall("./page"): # iterate over pages
nicholas@2524 49 page_name = page.get('ref') # get page ID
nicholas@2524 50
b@2264 51 if page_name is None: # ignore 'empty' audio_holders
b@2266 52 print("WARNING: " + file_name + " contains empty audio holder. (score_parser.py)")
b@2264 53 break
b@2264 54
b@2264 55 if page.get('state') != "complete":
b@2266 56 print("WARNING: " + file_name + " contains incomplete page " +page_name+ ". (score_parser.py)")
b@2264 57 break;
nicholas@2524 58
nicholas@2524 59 # Check if page in the store
nicholas@2524 60 if storage.get(page_name) == None:
nicholas@2524 61 storage[page_name] = {'header':[], 'axis':{}} # add to the store
nicholas@2524 62
nicholas@2524 63 # Get the axis names
nicholas@2524 64 pageConfig = root.find('./waet/page/[@id="'+page_name+'"]')
nicholas@2524 65 for interface in pageConfig.findall('./interface'): # Get the <interface> noeds
nicholas@2524 66 interfaceName = interface.get("name"); # Get the axis name
nicholas@2524 67 if interfaceName == None:
nicholas@2524 68 interfaceName = "default" # If name not set, make name 'default'
nicholas@2524 69 if storage[page_name]['axis'].get(interfaceName) == None:
nicholas@2524 70 storage[page_name]['axis'][interfaceName] = {} # If not in store for page, add empty dict
nicholas@2524 71 storage[page_name]['axis'][interfaceName][subject_id] = [] # Add the store for the session
nicholas@2524 72
b@2264 73 # header: fragment IDs in 'alphabetical' order
b@2264 74 # go to fragment column, or create new column if it doesn't exist yet
nicholas@2524 75
b@2264 76 # get alphabetical array of fragment IDs from this subject's XML
b@2264 77 fragmentnamelist = [] # make empty list
nicholas@2524 78 for audioelement in page.findall("./audioelement"): # iterate over all audioelements
b@2264 79 fragmentnamelist.append(audioelement.get('ref')) # add to list
nicholas@2524 80
nicholas@2524 81 fragmentnamelist = sorted(fragmentnamelist); # Sort the list
nicholas@2524 82 storage[page_name]['header'] = fragmentnamelist;
nicholas@2524 83
nicholas@2524 84 for fragmentname in fragmentnamelist:
nicholas@2524 85 audioElement = page.find("./audioelement/[@ref='"+ fragmentname+ "']") # Get the element
nicholas@2524 86 for value in audioElement.findall('./value'):
nicholas@2524 87 axisName = value.get('interface-name')
nicholas@2524 88 if axisName == None:
nicholas@2524 89 axisName = 'default'
nicholas@2524 90 axisStore = storage[page_name]['axis'][axisName]
nicholas@2524 91 if hasattr(value, 'text'):
nicholas@2524 92 axisStore[subject_id].append(value.text)
nicholas@2524 93 else:
nicholas@2524 94 axisStore[subject_id].append('')
b@2264 95
nicholas@2524 96 # Now create the individual files
nicholas@2524 97 for page_name in storage:
nicholas@2524 98 for axis_name in storage[page_name]['axis']:
nicholas@2524 99
nicholas@2524 100 file_name = folder_name+'/ratings/'+page_name+'-'+axis_name+'-ratings.csv' # score file name
nicholas@2524 101
nicholas@2524 102 # I'm not as elegant, I say burn the files and start again
nicholas@2524 103 headerrow = list(storage[page_name]['header']) # Extract the element IDs
nicholas@2524 104 headerrow.insert(0,'file_keys')
nicholas@2524 105 with open(file_name, 'w') as writefile:
b@2264 106 filewriter = csv.writer(writefile, delimiter=',')
nicholas@2524 107 filewriter.writerow(headerrow)
nicholas@2524 108
nicholas@2524 109 # open file to write the page
nicholas@2524 110 writefile = open(file_name, 'a')
nicholas@2524 111 filewriter = csv.writer(writefile, delimiter=',')
nicholas@2524 112
nicholas@2524 113 for subject_id in storage[page_name]['axis'][axis_name]:
nicholas@2524 114 entry = [subject_id]
nicholas@2524 115 for value in storage[page_name]['axis'][axis_name][subject_id]:
nicholas@2524 116 entry.append(value)
nicholas@2524 117 filewriter.writerow(entry)
nicholas@2524 118 writefile.close()