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