-
Notifications
You must be signed in to change notification settings - Fork 36
/
split-dev-train-test.py
executable file
·157 lines (118 loc) · 3.13 KB
/
split-dev-train-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
#Copyright (c) 2015 Idiap Research Institute, http://www.idiap.ch/
#Written by Marc Ferras <marc.ferras@idiap.ch>,
#
#This file is part of AcSim.
#
#AcSim is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 3 as
#published by the Free Software Foundation.
#
#Foobar is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with AcSim. If not, see <http://www.gnu.org/licenses/>.
import sys
import random
import os
def initRandom (file, seed):
global rnd
global rndidx
rndidx = 0
rnd=[]
with open(file,'r') as f:
for l in f.readlines():
rnd.append(l.strip())
if seed == '' or seed =='0':
rndidx=0
else:
rndidx=int(seed)
if rndidx >= len(rnd):
rndidx = rndidx % len(rnd)
return (rnd)
def getRandom():
global rndidx
global rnd
maxint=9223372036854775807
out = float(rnd[rndidx]) /float(maxint)
rndidx = rndidx + 1
if rndidx==len(rnd):
rndidx=0
return out
def getRandomInt(nvalues):
global rndidx
global rnd
maxint=9223372036854775807
out = int ( float(nvalues-1)*float(rnd[rndidx]) /float(maxint) )
rndidx = rndidx + 1
if rndidx==len(rnd):
rndidx=0
return out
def listShuffle(l):
global rnd
global rndidx
idx = range(0,len(l))
idxp = list(idx)
for i in idx:
ri1 = getRandomInt(len(l))
itmp = idxp[i]
idxp[i] = idxp[ri1]
idxp[ri1] = itmp
l2 = [ l[i] for i in idxp ]
return (l2)
if len(sys.argv)!=4:
print './split-train-test.py noise-file-list.txt train.list test.list'
sys.exit(0)
fileList=sys.argv[1]
trainList=sys.argv[2]
testList=sys.argv[3]
with open(trainList,'r') as f:
train=f.readlines()
with open(testList,'r') as f:
test=f.readlines()
# generate train and test file names
fileName, fileExtension = os.path.splitext(fileList)
#open output file list for writing
fileListDev = fileName +'-dev' + fileExtension
fileListTrn = fileName +'-trn' + fileExtension
fileListTst = fileName +'-tst' + fileExtension
initRandom('random',0)
noises=[]
with open(fileList,'r') as f:
for ln in f.readlines():
ln =ln.strip()
noises.append(ln)
noises = listShuffle (noises)
# assign train first
assignedNoises=[]
for f in train:
noise = noises[0]
if noise not in assignedNoises:
assignedNoises.append(noise)
del noises[0]
trainNoises = list (assignedNoises)
print str(len(trainNoises)) + ' noise files to train set'
with open(fileListTrn,'w') as f:
for line in trainNoises:
f.write(line+'\n')
# assign test
assignedNoises=[]
for f in test:
noise = noises[0]
if noise not in assignedNoises:
assignedNoises.append(noise)
del noises[0]
testNoises = list (assignedNoises)
print str(len(testNoises)) + ' noise files to test set'
with open(fileListTst,'w') as f:
for line in testNoises:
f.write(line+'\n')
# assign dev
devNoises = list(noises)
print str(len(devNoises)) + ' noise files to dev set'
with open(fileListDev,'w') as f:
for line in devNoises:
f.write(line+'\n')