annotate experiments/scripts/cnbh-syllables/run_training_and_testing/randomize_snrs.py @ 144:f8ace1ee8782

- Support for training on random SNRs
author tom@acousticscale.org
date Sun, 07 Nov 2010 07:38:20 +0000
parents
children
rev   line source
tom@144 1 #!/usr/bin/env python
tom@144 2 # encoding: utf-8
tom@144 3 """
tom@144 4 randomize_snrs.py
tom@144 5
tom@144 6 Created by Thomas Walters on 2010-11-06.
tom@144 7 """
tom@144 8
tom@144 9 import sys
tom@144 10 import getopt
tom@144 11 import re
tom@144 12 import random
tom@144 13
tom@144 14 help_message = '''
tom@144 15 Replace a string in each of two files with a string randomly selected from a list.
tom@144 16 '''
tom@144 17
tom@144 18 replacement_strings = ('snr_0dB',
tom@144 19 'snr_3dB',
tom@144 20 'snr_6dB',
tom@144 21 'snr_9dB',
tom@144 22 'snr_12dB',
tom@144 23 'snr_15dB',
tom@144 24 'snr_18dB',
tom@144 25 'snr_21dB',
tom@144 26 'snr_24dB',
tom@144 27 'snr_27dB',
tom@144 28 'snr_30dB',
tom@144 29 'snr_33dB',
tom@144 30 'snr_36dB',
tom@144 31 'snr_39dB',
tom@144 32 'snr_42dB',
tom@144 33 'snr_45dB',
tom@144 34 'clean')
tom@144 35
tom@144 36 class Usage(Exception):
tom@144 37 def __init__(self, msg):
tom@144 38 self.msg = msg
tom@144 39
tom@144 40
tom@144 41 def main(argv=None):
tom@144 42 if argv is None:
tom@144 43 argv = sys.argv
tom@144 44 try:
tom@144 45 try:
tom@144 46 opts, args = getopt.getopt(argv[1:], "hs:f:m:o:p:", ["help", "string_to_replace=", "input_script_file=", "input_mlf=", "output_script_file=", "output_mlf="])
tom@144 47 except getopt.error, msg:
tom@144 48 raise Usage(msg)
tom@144 49
tom@144 50 # option processing
tom@144 51 string_to_replace = ""
tom@144 52 script_file = ""
tom@144 53 ml_file = ""
tom@144 54 output_script_file = ""
tom@144 55 output_ml_file = ""
tom@144 56 for option, value in opts:
tom@144 57 if option in ("-h", "--help"):
tom@144 58 raise Usage(help_message)
tom@144 59 if option in ("-s", "--string_to_replace"):
tom@144 60 string_to_replace = value
tom@144 61 if option in ("-f", "--input_script_file"):
tom@144 62 script_file = value
tom@144 63 if option in ("-o", "--outut_script_file"):
tom@144 64 output_script_file = value
tom@144 65 if option in ("-p", "--output_mlf"):
tom@144 66 output_ml_file = value
tom@144 67 if option in ("-m", "--input_mlf"):
tom@144 68 ml_file = value
tom@144 69
tom@144 70 except Usage, err:
tom@144 71 print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
tom@144 72 print >> sys.stderr, "\t for help use --help"
tom@144 73 return 2
tom@144 74
tom@144 75 script = open(script_file, 'r')
tom@144 76 mlf = open(ml_file, 'r')
tom@144 77 out_script = open(output_script_file, 'w')
tom@144 78 out_mlf = open(output_ml_file, 'w')
tom@144 79 out_mlf.write(mlf.readline())
tom@144 80 for l in script:
tom@144 81 replacement = random.choice(replacement_strings)
tom@144 82 out_script.write(re.sub(string_to_replace, replacement, l))
tom@144 83 out_mlf.write(re.sub(string_to_replace, replacement, mlf.readline()))
tom@144 84 out_mlf.write(mlf.readline())
tom@144 85 out_mlf.write(mlf.readline())
tom@144 86 out_mlf.write(mlf.readline())
tom@144 87 out_mlf.write(mlf.readline())
tom@144 88
tom@144 89
tom@144 90
tom@144 91 if __name__ == "__main__":
tom@144 92 sys.exit(main())