annotate experiments/scripts/cnbh-syllables/results_plotting/gen_results.py @ 213:1ebf6aa96e3c

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