annotate python/score_parser2.py @ 2586:42446b5eeee8

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