annotate trunk/experiments/scripts/cnbh-syllables/results_plotting/gen_results.py @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents d9be18850b55
children
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@388 15 Write out 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 -c --value_count
tomwalters@381 26 -p --spoke_pattern
tomwalters@381 27 '''
tomwalters@381 28
tomwalters@381 29 class Usage(Exception):
tomwalters@381 30 def __init__(self, msg):
tomwalters@381 31 self.msg = msg
tomwalters@381 32
tomwalters@381 33
tomwalters@381 34 def main(argv=None):
tomwalters@381 35 if argv is None:
tomwalters@381 36 argv = sys.argv
tomwalters@381 37 try:
tomwalters@381 38 try:
tomwalters@381 39 opts, args = getopt.getopt(argv[1:], "hi:t:s:o:c:p:v",
tomwalters@381 40 ["help", "input_file=", "train_talkers=",
tomwalters@388 41 "test_talkers=",
tomwalters@381 42 "value_count=", "spoke_pattern="])
tomwalters@381 43 except getopt.error, msg:
tomwalters@381 44 raise Usage(msg)
tomwalters@381 45
tomwalters@381 46 # defaults
tomwalters@381 47 input_file = "misclassified_syllables_iteration_15"
tomwalters@381 48 train_talkers = "training_talkers"
tomwalters@381 49 test_talkers = "testing_talkers"
tomwalters@381 50 output_filename = "results.txt"
tomwalters@381 51 total_value_count = 185
tomwalters@381 52 spoke_pattern_file = "spoke_pattern.txt"
tomwalters@381 53
tomwalters@381 54 # option processing
tomwalters@381 55 for option, value in opts:
tomwalters@381 56 if option == "-v":
tomwalters@381 57 verbose = True
tomwalters@381 58 if option in ("-h", "--help"):
tomwalters@381 59 raise Usage(help_message)
tomwalters@381 60 if option in ("-i", "--input_file"):
tomwalters@381 61 input_file = value
tomwalters@381 62 if option in ("-t", "--train_talkers"):
tomwalters@381 63 train_talkers = value
tomwalters@381 64 if option in ("-s", "--test_talkers"):
tomwalters@381 65 test_talkers = value
tomwalters@381 66 if option in ("-c", "--value_count"):
tomwalters@381 67 total_value_count = int(value)
tomwalters@386 68 if option in ("-p", "--spoke_pattern"):
tomwalters@381 69 spoke_pattern_file = value
tomwalters@381 70
tomwalters@381 71 except Usage, err:
tomwalters@381 72 print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
tomwalters@381 73 print >> sys.stderr, "\t for help use --help"
tomwalters@381 74 return 2
tomwalters@381 75
tomwalters@381 76 results = dict()
tomwalters@381 77 f = open(input_file, 'r')
tomwalters@381 78 for line in f:
tomwalters@381 79 values = line.strip().split(' ')
tomwalters@381 80 results[values[1]]=100*(1-float(values[0])/total_value_count)
tomwalters@381 81
tomwalters@381 82 f = open(test_talkers, 'r')
tomwalters@381 83 test_talkers_list = f.readlines()
tomwalters@381 84 f.close()
tomwalters@381 85
tomwalters@381 86 f = open(train_talkers, 'r')
tomwalters@381 87 train_talkers_list = f.readlines()
tomwalters@381 88 f.close()
tomwalters@381 89
tomwalters@381 90 spoke_pattern = []
tomwalters@381 91 f = open(spoke_pattern_file, 'r')
tomwalters@381 92 for line in f:
tomwalters@381 93 spoke_pattern.append(line.strip().split(' '))
tomwalters@381 94
tomwalters@381 95 all_talkers_list = []
tomwalters@381 96 all_talkers_list.extend(train_talkers_list)
tomwalters@381 97 all_talkers_list.extend(test_talkers_list)
tomwalters@381 98
tomwalters@381 99 # Here I make the rather rash assumption that the model was tested on all talkers
tomwalters@381 100 # this should be true if the training and testing was done using my scripts.
tomwalters@381 101 for t in all_talkers_list:
tomwalters@381 102 results.setdefault(t.strip(), 100.0)
tomwalters@381 103
tomwalters@381 104 total_score = 0.0
tomwalters@381 105 element_count = 0
tomwalters@381 106 for t in train_talkers_list:
tomwalters@381 107 total_score += results[t.strip()]
tomwalters@381 108 element_count += 1
tomwalters@381 109 score = total_score / element_count
tomwalters@381 110 print ("# Score on training talkers: %.1f%%" % score)
tomwalters@381 111
tomwalters@381 112 total_score = 0.0
tomwalters@381 113 element_count = 0
tomwalters@381 114 for t in all_talkers_list:
tomwalters@381 115 total_score += results[t.strip()]
tomwalters@381 116 element_count += 1
tomwalters@381 117 score = total_score / element_count
tomwalters@381 118 print ("# Score on all talkers: %.1f%%" % score)
tomwalters@381 119
tomwalters@381 120 total_score = 0.0
tomwalters@381 121 element_count = 0
tomwalters@381 122 for t in test_talkers_list:
tomwalters@381 123 total_score += results[t.strip()]
tomwalters@381 124 element_count += 1
tomwalters@381 125 score = total_score / element_count
tomwalters@381 126 print ("# Score on test talkers: %.1f%%" % score)
tomwalters@381 127
tomwalters@381 128 score = results[spoke_pattern[0][0]]
tomwalters@381 129 print ("# Score on central talker: %.1f" % score)
tomwalters@381 130
tomwalters@381 131 for s in xrange(1,9):
tomwalters@381 132 print ("# Results for spoke %d" % s)
tomwalters@381 133 for p in xrange(0, 7):
tomwalters@381 134 score = results[spoke_pattern[s][p]]
tomwalters@381 135 m = re.match('(.*)p(.*)s', spoke_pattern[s][p])
tomwalters@381 136 gpr = m.group(1)
tomwalters@381 137 vtl=m.group(2)
tomwalters@381 138 print ("%s %s %s" % (gpr, vtl, score))
tomwalters@381 139
tomwalters@381 140 if __name__ == "__main__":
tomwalters@381 141 sys.exit(main())