annotate python/score_parser.py @ 3141:335bc77627e0 tip

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