annotate experiments/scripts/cnbh-syllables/results_plotting/gen_results.py @ 626:586b0677aae8

Fourth revision of Alex Brandmeyer's C++ implementation. Fixed more style issues, changed AGC structures to vectors, replaced FloatArray2d with vector<FloatArray>, implemented first tests using GTest to verify coefficients and monaural output against Matlab values (stored in aimc/carfac/test_data/). To run tests, change the path stored in carfac_test.h in TEST_SRC_DIR. Added CARFAC_GenerateTestData to the Matlab branch, fixed stage indexing in CARFAC_Cross_Couple.m to reflect changes in AGCCoeffs and AGCState structs.
author alexbrandmeyer
date Wed, 22 May 2013 21:30:02 +0000
parents 42d66bfbd9c8
children
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@218 15 Write out 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@100 25 -c --value_count
tomwalters@100 26 -p --spoke_pattern
tomwalters@100 27 '''
tomwalters@100 28
tomwalters@100 29 class Usage(Exception):
tomwalters@100 30 def __init__(self, msg):
tomwalters@100 31 self.msg = msg
tomwalters@100 32
tomwalters@100 33
tomwalters@100 34 def main(argv=None):
tomwalters@100 35 if argv is None:
tomwalters@100 36 argv = sys.argv
tomwalters@100 37 try:
tomwalters@100 38 try:
tomwalters@100 39 opts, args = getopt.getopt(argv[1:], "hi:t:s:o:c:p:v",
tomwalters@100 40 ["help", "input_file=", "train_talkers=",
tomwalters@218 41 "test_talkers=",
tomwalters@100 42 "value_count=", "spoke_pattern="])
tomwalters@100 43 except getopt.error, msg:
tomwalters@100 44 raise Usage(msg)
tomwalters@100 45
tomwalters@100 46 # defaults
tomwalters@100 47 input_file = "misclassified_syllables_iteration_15"
tomwalters@100 48 train_talkers = "training_talkers"
tomwalters@100 49 test_talkers = "testing_talkers"
tomwalters@100 50 output_filename = "results.txt"
tomwalters@100 51 total_value_count = 185
tomwalters@100 52 spoke_pattern_file = "spoke_pattern.txt"
tomwalters@100 53
tomwalters@100 54 # option processing
tomwalters@100 55 for option, value in opts:
tomwalters@100 56 if option == "-v":
tomwalters@100 57 verbose = True
tomwalters@100 58 if option in ("-h", "--help"):
tomwalters@100 59 raise Usage(help_message)
tomwalters@100 60 if option in ("-i", "--input_file"):
tomwalters@100 61 input_file = value
tomwalters@100 62 if option in ("-t", "--train_talkers"):
tomwalters@100 63 train_talkers = value
tomwalters@100 64 if option in ("-s", "--test_talkers"):
tomwalters@100 65 test_talkers = value
tomwalters@100 66 if option in ("-c", "--value_count"):
tomwalters@100 67 total_value_count = int(value)
tomwalters@216 68 if option in ("-p", "--spoke_pattern"):
tomwalters@100 69 spoke_pattern_file = value
tomwalters@100 70
tomwalters@100 71 except Usage, err:
tomwalters@100 72 print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
tomwalters@100 73 print >> sys.stderr, "\t for help use --help"
tomwalters@100 74 return 2
tomwalters@100 75
tomwalters@100 76 results = dict()
tomwalters@100 77 f = open(input_file, 'r')
tomwalters@100 78 for line in f:
tomwalters@100 79 values = line.strip().split(' ')
tomwalters@100 80 results[values[1]]=100*(1-float(values[0])/total_value_count)
tomwalters@100 81
tomwalters@100 82 f = open(test_talkers, 'r')
tomwalters@100 83 test_talkers_list = f.readlines()
tomwalters@100 84 f.close()
tomwalters@100 85
tomwalters@100 86 f = open(train_talkers, 'r')
tomwalters@100 87 train_talkers_list = f.readlines()
tomwalters@100 88 f.close()
tomwalters@100 89
tomwalters@100 90 spoke_pattern = []
tomwalters@100 91 f = open(spoke_pattern_file, 'r')
tomwalters@100 92 for line in f:
tomwalters@100 93 spoke_pattern.append(line.strip().split(' '))
tomwalters@100 94
tomwalters@100 95 all_talkers_list = []
tomwalters@100 96 all_talkers_list.extend(train_talkers_list)
tomwalters@100 97 all_talkers_list.extend(test_talkers_list)
tomwalters@100 98
tomwalters@100 99 # Here I make the rather rash assumption that the model was tested on all talkers
tomwalters@100 100 # this should be true if the training and testing was done using my scripts.
tomwalters@100 101 for t in all_talkers_list:
tomwalters@100 102 results.setdefault(t.strip(), 100.0)
tomwalters@100 103
tomwalters@100 104 total_score = 0.0
tomwalters@100 105 element_count = 0
tomwalters@100 106 for t in train_talkers_list:
tomwalters@100 107 total_score += results[t.strip()]
tomwalters@100 108 element_count += 1
tomwalters@100 109 score = total_score / element_count
tomwalters@100 110 print ("# Score on training talkers: %.1f%%" % score)
tomwalters@100 111
tomwalters@100 112 total_score = 0.0
tomwalters@100 113 element_count = 0
tomwalters@100 114 for t in all_talkers_list:
tomwalters@100 115 total_score += results[t.strip()]
tomwalters@100 116 element_count += 1
tomwalters@100 117 score = total_score / element_count
tomwalters@100 118 print ("# Score on all talkers: %.1f%%" % score)
tomwalters@100 119
tomwalters@100 120 total_score = 0.0
tomwalters@100 121 element_count = 0
tomwalters@100 122 for t in test_talkers_list:
tomwalters@100 123 total_score += results[t.strip()]
tomwalters@100 124 element_count += 1
tomwalters@100 125 score = total_score / element_count
tomwalters@100 126 print ("# Score on test talkers: %.1f%%" % score)
tomwalters@100 127
tomwalters@100 128 score = results[spoke_pattern[0][0]]
tomwalters@100 129 print ("# Score on central talker: %.1f" % score)
tomwalters@100 130
tomwalters@100 131 for s in xrange(1,9):
tomwalters@100 132 print ("# Results for spoke %d" % s)
tomwalters@100 133 for p in xrange(0, 7):
tomwalters@100 134 score = results[spoke_pattern[s][p]]
tomwalters@100 135 m = re.match('(.*)p(.*)s', spoke_pattern[s][p])
tomwalters@100 136 gpr = m.group(1)
tomwalters@100 137 vtl=m.group(2)
tomwalters@100 138 print ("%s %s %s" % (gpr, vtl, score))
tomwalters@100 139
tomwalters@100 140 if __name__ == "__main__":
tomwalters@100 141 sys.exit(main())