annotate trunk/experiments/scripts/cnbh-syllables/results_plotting/gen_results.py @ 386:b0ce50fa8a95

- Typos
author tomwalters
date Tue, 14 Sep 2010 01:09:04 +0000
parents b4bfdfae0dd7
children d9be18850b55
rev   line source
tomwalters@381 1 #!/usr/bin/env python
tomwalters@381 2 # encoding: utf-8
tomwalters@381 3 """
tomwalters@381 4 gen_results.py
tomwalters@381 5
tomwalters@381 6 Created by Thomas Walters on 2010-09-12.
tomwalters@381 7 """
tomwalters@381 8
tomwalters@381 9 import sys
tomwalters@381 10 import getopt
tomwalters@381 11 import re
tomwalters@381 12
tomwalters@381 13
tomwalters@381 14 help_message = '''
tomwalters@381 15 Generate a file containing all the results for a run of the
tomwalters@381 16 syllable recognition experiment. Expected input is a 'misclassificaions'
tomwalters@381 17 file of the type generated by run_test_instance.sh, along with the locations
tomwalters@381 18 of files containing the training talkers and all the talkers that the system
tomwalters@381 19 was tested on.
tomwalters@381 20
tomwalters@381 21 Arguments:
tomwalters@381 22 -i --input_file
tomwalters@381 23 -t --train_talkers
tomwalters@381 24 -s --test_talkers
tomwalters@381 25 -o --output_filename
tomwalters@381 26 -c --value_count
tomwalters@381 27 -p --spoke_pattern
tomwalters@381 28 '''
tomwalters@381 29
tomwalters@381 30 class Usage(Exception):
tomwalters@381 31 def __init__(self, msg):
tomwalters@381 32 self.msg = msg
tomwalters@381 33
tomwalters@381 34
tomwalters@381 35 def main(argv=None):
tomwalters@381 36 if argv is None:
tomwalters@381 37 argv = sys.argv
tomwalters@381 38 try:
tomwalters@381 39 try:
tomwalters@381 40 opts, args = getopt.getopt(argv[1:], "hi:t:s:o:c:p:v",
tomwalters@381 41 ["help", "input_file=", "train_talkers=",
tomwalters@381 42 "test_talkers=", "output_filename=",
tomwalters@381 43 "value_count=", "spoke_pattern="])
tomwalters@381 44 except getopt.error, msg:
tomwalters@381 45 raise Usage(msg)
tomwalters@381 46
tomwalters@381 47 # defaults
tomwalters@381 48 input_file = "misclassified_syllables_iteration_15"
tomwalters@381 49 train_talkers = "training_talkers"
tomwalters@381 50 test_talkers = "testing_talkers"
tomwalters@381 51 output_filename = "results.txt"
tomwalters@381 52 total_value_count = 185
tomwalters@381 53 spoke_pattern_file = "spoke_pattern.txt"
tomwalters@381 54
tomwalters@381 55 # option processing
tomwalters@381 56 for option, value in opts:
tomwalters@381 57 if option == "-v":
tomwalters@381 58 verbose = True
tomwalters@381 59 if option in ("-h", "--help"):
tomwalters@381 60 raise Usage(help_message)
tomwalters@381 61 if option in ("-i", "--input_file"):
tomwalters@381 62 input_file = value
tomwalters@381 63 if option in ("-t", "--train_talkers"):
tomwalters@381 64 train_talkers = value
tomwalters@381 65 if option in ("-s", "--test_talkers"):
tomwalters@381 66 test_talkers = value
tomwalters@381 67 if option in ("-c", "--value_count"):
tomwalters@381 68 total_value_count = int(value)
tomwalters@386 69 if option in ("-p", "--spoke_pattern"):
tomwalters@381 70 spoke_pattern_file = value
tomwalters@383 71 if option in ("-o", "--output_filename"):
tomwalters@382 72 output_filename = value
tomwalters@381 73
tomwalters@381 74 except Usage, err:
tomwalters@381 75 print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
tomwalters@381 76 print >> sys.stderr, "\t for help use --help"
tomwalters@381 77 return 2
tomwalters@381 78
tomwalters@381 79 results = dict()
tomwalters@381 80 f = open(input_file, 'r')
tomwalters@381 81 for line in f:
tomwalters@381 82 values = line.strip().split(' ')
tomwalters@381 83 results[values[1]]=100*(1-float(values[0])/total_value_count)
tomwalters@381 84
tomwalters@381 85 f = open(test_talkers, 'r')
tomwalters@381 86 test_talkers_list = f.readlines()
tomwalters@381 87 f.close()
tomwalters@381 88
tomwalters@381 89 f = open(train_talkers, 'r')
tomwalters@381 90 train_talkers_list = f.readlines()
tomwalters@381 91 f.close()
tomwalters@381 92
tomwalters@381 93 spoke_pattern = []
tomwalters@381 94 f = open(spoke_pattern_file, 'r')
tomwalters@381 95 for line in f:
tomwalters@381 96 spoke_pattern.append(line.strip().split(' '))
tomwalters@381 97
tomwalters@381 98 all_talkers_list = []
tomwalters@381 99 all_talkers_list.extend(train_talkers_list)
tomwalters@381 100 all_talkers_list.extend(test_talkers_list)
tomwalters@381 101
tomwalters@381 102 # Here I make the rather rash assumption that the model was tested on all talkers
tomwalters@381 103 # this should be true if the training and testing was done using my scripts.
tomwalters@381 104 for t in all_talkers_list:
tomwalters@381 105 results.setdefault(t.strip(), 100.0)
tomwalters@381 106
tomwalters@381 107 total_score = 0.0
tomwalters@381 108 element_count = 0
tomwalters@381 109 for t in train_talkers_list:
tomwalters@381 110 total_score += results[t.strip()]
tomwalters@381 111 element_count += 1
tomwalters@381 112 score = total_score / element_count
tomwalters@381 113 print ("# Score on training talkers: %.1f%%" % score)
tomwalters@381 114
tomwalters@381 115 total_score = 0.0
tomwalters@381 116 element_count = 0
tomwalters@381 117 for t in all_talkers_list:
tomwalters@381 118 total_score += results[t.strip()]
tomwalters@381 119 element_count += 1
tomwalters@381 120 score = total_score / element_count
tomwalters@381 121 print ("# Score on all talkers: %.1f%%" % score)
tomwalters@381 122
tomwalters@381 123 total_score = 0.0
tomwalters@381 124 element_count = 0
tomwalters@381 125 for t in test_talkers_list:
tomwalters@381 126 total_score += results[t.strip()]
tomwalters@381 127 element_count += 1
tomwalters@381 128 score = total_score / element_count
tomwalters@381 129 print ("# Score on test talkers: %.1f%%" % score)
tomwalters@381 130
tomwalters@381 131 score = results[spoke_pattern[0][0]]
tomwalters@381 132 print ("# Score on central talker: %.1f" % score)
tomwalters@381 133
tomwalters@381 134 for s in xrange(1,9):
tomwalters@381 135 print ("# Results for spoke %d" % s)
tomwalters@381 136 for p in xrange(0, 7):
tomwalters@381 137 score = results[spoke_pattern[s][p]]
tomwalters@381 138 m = re.match('(.*)p(.*)s', spoke_pattern[s][p])
tomwalters@381 139 gpr = m.group(1)
tomwalters@381 140 vtl=m.group(2)
tomwalters@381 141 print ("%s %s %s" % (gpr, vtl, score))
tomwalters@381 142
tomwalters@381 143 if __name__ == "__main__":
tomwalters@381 144 sys.exit(main())