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