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