tomwalters@381: #!/usr/bin/env python tomwalters@381: # encoding: utf-8 tomwalters@381: """ tomwalters@381: gen_results.py tomwalters@381: tomwalters@381: Created by Thomas Walters on 2010-09-12. tomwalters@381: """ tomwalters@381: tomwalters@381: import sys tomwalters@381: import getopt tomwalters@381: import re tomwalters@381: tomwalters@381: tomwalters@381: help_message = ''' tomwalters@388: Write out all the results for a run of the tomwalters@381: syllable recognition experiment. Expected input is a 'misclassificaions' tomwalters@381: file of the type generated by run_test_instance.sh, along with the locations tomwalters@381: of files containing the training talkers and all the talkers that the system tomwalters@381: was tested on. tomwalters@381: tomwalters@381: Arguments: tomwalters@381: -i --input_file tomwalters@381: -t --train_talkers tomwalters@381: -s --test_talkers tomwalters@381: -c --value_count tomwalters@381: -p --spoke_pattern tomwalters@381: ''' tomwalters@381: tomwalters@381: class Usage(Exception): tomwalters@381: def __init__(self, msg): tomwalters@381: self.msg = msg tomwalters@381: tomwalters@381: tomwalters@381: def main(argv=None): tomwalters@381: if argv is None: tomwalters@381: argv = sys.argv tomwalters@381: try: tomwalters@381: try: tomwalters@381: opts, args = getopt.getopt(argv[1:], "hi:t:s:o:c:p:v", tomwalters@381: ["help", "input_file=", "train_talkers=", tomwalters@388: "test_talkers=", tomwalters@381: "value_count=", "spoke_pattern="]) tomwalters@381: except getopt.error, msg: tomwalters@381: raise Usage(msg) tomwalters@381: tomwalters@381: # defaults tomwalters@381: input_file = "misclassified_syllables_iteration_15" tomwalters@381: train_talkers = "training_talkers" tomwalters@381: test_talkers = "testing_talkers" tomwalters@381: output_filename = "results.txt" tomwalters@381: total_value_count = 185 tomwalters@381: spoke_pattern_file = "spoke_pattern.txt" tomwalters@381: tomwalters@381: # option processing tomwalters@381: for option, value in opts: tomwalters@381: if option == "-v": tomwalters@381: verbose = True tomwalters@381: if option in ("-h", "--help"): tomwalters@381: raise Usage(help_message) tomwalters@381: if option in ("-i", "--input_file"): tomwalters@381: input_file = value tomwalters@381: if option in ("-t", "--train_talkers"): tomwalters@381: train_talkers = value tomwalters@381: if option in ("-s", "--test_talkers"): tomwalters@381: test_talkers = value tomwalters@381: if option in ("-c", "--value_count"): tomwalters@381: total_value_count = int(value) tomwalters@386: if option in ("-p", "--spoke_pattern"): tomwalters@381: spoke_pattern_file = value tomwalters@381: tomwalters@381: except Usage, err: tomwalters@381: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) tomwalters@381: print >> sys.stderr, "\t for help use --help" tomwalters@381: return 2 tomwalters@381: tomwalters@381: results = dict() tomwalters@381: f = open(input_file, 'r') tomwalters@381: for line in f: tomwalters@381: values = line.strip().split(' ') tomwalters@381: results[values[1]]=100*(1-float(values[0])/total_value_count) tomwalters@381: tomwalters@381: f = open(test_talkers, 'r') tomwalters@381: test_talkers_list = f.readlines() tomwalters@381: f.close() tomwalters@381: tomwalters@381: f = open(train_talkers, 'r') tomwalters@381: train_talkers_list = f.readlines() tomwalters@381: f.close() tomwalters@381: tomwalters@381: spoke_pattern = [] tomwalters@381: f = open(spoke_pattern_file, 'r') tomwalters@381: for line in f: tomwalters@381: spoke_pattern.append(line.strip().split(' ')) tomwalters@381: tomwalters@381: all_talkers_list = [] tomwalters@381: all_talkers_list.extend(train_talkers_list) tomwalters@381: all_talkers_list.extend(test_talkers_list) tomwalters@381: tomwalters@381: # Here I make the rather rash assumption that the model was tested on all talkers tomwalters@381: # this should be true if the training and testing was done using my scripts. tomwalters@381: for t in all_talkers_list: tomwalters@381: results.setdefault(t.strip(), 100.0) tomwalters@381: tomwalters@381: total_score = 0.0 tomwalters@381: element_count = 0 tomwalters@381: for t in train_talkers_list: tomwalters@381: total_score += results[t.strip()] tomwalters@381: element_count += 1 tomwalters@381: score = total_score / element_count tomwalters@381: print ("# Score on training talkers: %.1f%%" % score) tomwalters@381: tomwalters@381: total_score = 0.0 tomwalters@381: element_count = 0 tomwalters@381: for t in all_talkers_list: tomwalters@381: total_score += results[t.strip()] tomwalters@381: element_count += 1 tomwalters@381: score = total_score / element_count tomwalters@381: print ("# Score on all talkers: %.1f%%" % score) tomwalters@381: tomwalters@381: total_score = 0.0 tomwalters@381: element_count = 0 tomwalters@381: for t in test_talkers_list: tomwalters@381: total_score += results[t.strip()] tomwalters@381: element_count += 1 tomwalters@381: score = total_score / element_count tomwalters@381: print ("# Score on test talkers: %.1f%%" % score) tomwalters@381: tomwalters@381: score = results[spoke_pattern[0][0]] tomwalters@381: print ("# Score on central talker: %.1f" % score) tomwalters@381: tomwalters@381: for s in xrange(1,9): tomwalters@381: print ("# Results for spoke %d" % s) tomwalters@381: for p in xrange(0, 7): tomwalters@381: score = results[spoke_pattern[s][p]] tomwalters@381: m = re.match('(.*)p(.*)s', spoke_pattern[s][p]) tomwalters@381: gpr = m.group(1) tomwalters@381: vtl=m.group(2) tomwalters@381: print ("%s %s %s" % (gpr, vtl, score)) tomwalters@381: tomwalters@381: if __name__ == "__main__": tomwalters@381: sys.exit(main())