/
icapp.py
executable file
·425 lines (360 loc) · 14.2 KB
/
icapp.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python
# tests
# 1) fake data
# - generate fake data
# - run ica
# - split and plot
#
# 2) real data
# - read in real data
# - run ica
# - split and plot
#
# basic parts
# 1) read audio files (type, names) and subsample
# 2) run ica
# 3) determine what parts are good/bad
# 4) remove bad parts/keep good parts
# 5) recombine and save
import glob
import logging
import optparse
import os
import re
import sys
import warnings
import numpy as np
import pylab as pl
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import scikits.audiolab as al
from scikits.learn.decomposition import FastICA
def parse_options(args=None):
"""
chain these like so:
main parser -> get subsample method ->
parse subsample options -> check options -> GO
"""
parser = optparse.OptionParser( \
usage="usage: %prog [options] audiofiles...")
parser.add_option("-a", "--alphabetic",
help="sort files alphabetically",
default=False, action="store_true")
parser.add_option("-m", "--method", dest="method",
help="subsample method: simple,",
default="random", type="string")
parser.add_option("-M", "--mixingmatrix", dest="mixingmatrix",
help="load a pre-computed mixingmatrix (must also define U)",
default="", type="string")
parser.add_option("-U", "--unmixingmatrix", dest="unmixingmatrix",
help="load a pre-computed unmixingmatrix (must also define M)",
default="", type="string")
parser.add_option("-s", "--subsample", dest="subsample",
help="subsample argument", action="append",
default=[])
parser.add_option("-S", "--short", dest="short",
help="only calculate matrices, do not convert files",
default=False, action="store_true")
parser.add_option("-n", "--ncomponents", dest="ncomponents",
help="number of ica components",
default=32, type="int")
parser.add_option("-d", "--dtype", dest="dtype",
help="data type to read from file",
default="float64", type="string")
parser.add_option("-v", "--verbose", dest="verbose",
help="enable verbose output",
default=False, action="store_true")
parser.add_option("-p", "--plot", dest="plot",
help="generate debugging plots",
default=False, action="store_true")
parser.add_option("-o", "--output", dest="output",
help="output directory",
default="cleaned")
parser.add_option("-t", "--threshold", dest="threshold",
help="Threshold at which to consider a mixing matrix cell active",
default=None, type="float")
parser.add_option("-c", "--count", dest="count",
help="Number of cells within a column of the mixing matrix "
"required to count the component as noise",
default=3, type="int")
parser.add_option("-C", "--chunksize", dest="chunksize",
help="Number of samples (per file) to reproject per chunk",
default=44100, type="int")
parser.add_option("-g", "--glob", dest="glob",
help="Filename glob for file filtering (for directory reading)",
default="input_*.wav", type="string")
parser.add_option("-P", "--position-sort", dest="psort",
help="Sort filenames by position (assumes tdt ordering): "
"1/0, enable/disable", default=1, type="int")
(options, args) = parser.parse_args(args)
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)
# check options
if len(args) == 0:
raise ValueError("Must supply at least 1 argument")
if len(args) == 1:
# test if so use all wav
if not os.path.isdir(args[0]):
raise ValueError("If 1 arguments is supplied it must "
"be a directory: %s" % str(args[0]))
files = glob.glob(args[0] + '/' + options.glob)
logging.debug("Found %i input files: %s" % (len(files), str(files)))
if len(files) < 2:
raise ValueError("Found less than 2 files")
args = files
if options.alphabetic:
args.sort()
options.dtype = np.dtype(options.dtype)
if not os.path.exists(options.output):
os.makedirs(options.output)
return options, args
def subsample(audioFiles, n):
"""
This is a simple 'subsample' function which just reads the first n frames
Parameters
----------
audioFiles : list of scikits.audiolab.Sndfile
Should all be open
n : int
Number of samples to read
Returns
-------
data : numpy.ndarray
Subsampled data (ordered: FILE x SAMPLE)
Notes
-----
More complex functions could take it's place. Such as...
1) uniform sampling throughout file
2) random sampling throughout file
3) oversampling of licking periods (or other artifact-known periods)
"""
logging.debug("simple subsampling: %i" % n)
[af.seek(0) for af in audioFiles]
data = [af.read_frames(n) for af in audioFiles]
return np.array(data)
def random_subsample(audioFiles, n):
logging.debug("random subsampling: %i" % n)
samps = np.random.randint(0, audioFiles[0].nframes, n)
data = np.empty((len(audioFiles), n))
for (si, s) in enumerate(samps):
for (ai, af) in enumerate(audioFiles):
af.seek(s)
data[ai, si] = af.read_frames(1)
return data
def range_subsample(audioFiles, start, end):
logging.debug("range subsampling: %i %i" % (start, end))
[af.seek(0) for af in audioFiles]
data = [af.read_frames(end - start) for af in audioFiles]
return np.array(data)
def multi_range_subsample(audioFiles, ranges):
"""
ranges is a list of (start, stop) tuples
"""
total = sum([r[1] - r[0] for r in ranges])
data = np.empty((len(audioFiles), total))
cursors = np.zeros(len(audioFiles))
for (ri, r) in enumerate(ranges):
for (ai, af) in enumerate(audioFiles):
af.seek(r[0])
data[ai, cursors[ai] + r[1] - r[0]] = af.read_frames(r[1] - r[0])
cursors[ai] += r[1] - r[0]
return data
def run_ica(data, ncomponents):
logging.debug("running ica: %i" % ncomponents)
ica = FastICA(ncomponents)
ica.fit(data)
return ica
def clean_ica(ica, threshold, count):
mm = ica.get_mixing_matrix()
if threshold is None:
mf = mm.reshape(mm.shape[0] * mm.shape[1])
threshold = np.mean(mf) + np.std(mf) * 2.
logging.debug("cleaning ica: %f, %i" % (threshold, count))
logging.debug("column maxes: %s" % str(np.max(np.abs(mm), 0)))
votes = np.sum(np.abs(mm) > threshold, 0)
logging.debug("component votes: %s" % str(votes))
bad = np.where(votes > count)[0]
logging.debug("noise components: %s" % str(bad))
mm[:, bad] = np.zeros_like(mm[:, bad])
return np.matrix(mm)
try:
umm = np.linalg.inv(mm)
except np.linalg.LinAlgError:
logging.debug("Failed to mathematically invert matrix, "
"trying pseudo-inverse")
umm = np.linalg.pinv(mm)
return np.matrix(umm)
def make_output_files(inputFilenames, outputdir, auformat, samplerate):
logging.debug("Making output files in directory %s" % outputdir)
outputFiles = []
for infile in inputFilenames:
ofn = "%s/%s" % (outputdir, os.path.basename(infile))
outputFiles.append(al.Sndfile(ofn, 'w', auformat, 1, samplerate))
return outputFiles
def chunk(n, chunksize, overlap=0):
"""
Chunk generator
"""
for i in xrange((n / chunksize) + 1):
if (i * chunksize) >= n:
return
if ((i + 1) * chunksize + overlap) < n:
yield (i * chunksize, (i + 1) * chunksize + overlap)
else:
yield (i * chunksize, n)
def clean_files(audioFiles, outputFiles, UM, remixer, chunksize):
"""
Parameters
----------
audioFiles : list of scikits.audiolab.Sndfile
Should be open
outputFiles : list of scikits.audiolab.Sndfile
Should be open
"""
CM = remixer * UM
logging.debug("cleaning files, chunksize: %i" % chunksize)
nframes = audioFiles[0].nframes
[f.seek(0) for f in audioFiles]
for (s, e) in chunk(nframes, chunksize):
logging.debug("chunk: %i to %i" % (s, e))
data = np.vstack([f.read_frames(e - s) for f in audioFiles])
#data = []
#for infile in audioFiles:
# infile.seek(s)
# data.append(infile.read_frames(e - s))
#tdata = ica.transform(np.array(data))
#tdata = UM * np.array(data)
#cdata = np.array(remixer * tdata)
cdata = np.array(CM * np.array(data))
for (cd, outfile) in zip(cdata, outputFiles):
outfile.write_frames(cd)
outfile.sync()
def psorted(fns):
TDT_POS = (-1, 2, 8, 6, 12, 4, 16, 0, 20, 13, 1, 7, 5, 17, 3, 11, 9, 22, \
14, 30, 26, 18, 10, 28, 24, 29, 25, 19, 15, 31, 27, 23, 21)
regex = r'_([0-9]+)\#'
return sorted(fns, key=lambda fn: TDT_POS[int(re.findall(regex, \
os.path.basename(fn))[0])])
def foo(fn):
i = int(re.findall(regex, os.path.basename(fn))[0])
t = TDT_POS[i]
print os.path.basename(fn), i, t
return t
return sorted(fns, key=foo)
def process():
options, inFilenames = parse_options()
options.port = bool(options.psort)
# open files
if options.psort == 1:
inFilenames = psorted(inFilenames)
afs = [al.Sndfile(f) for f in inFilenames]
if (options.mixingmatrix.strip() == "") and \
(options.unmixingmatrix.strip() == ""):
# subsample
if options.method == 'simple':
if len(options.subsample) == 0:
args = [44100, ] # set defaults
elif len(options.subsample) == 1:
try:
args = [int(options.subsample[0]), ]
except ValueError:
raise ValueError("Could not convert subsample argument "
"to int[%s]" % options.subsample[0])
else:
raise ValueError("Wrong number of subsample arguments, "
"expected 1: %s" % str(options.subsample))
data = subsample(afs, *tuple(args))
elif options.method == 'random':
if len(options.subsample) == 0:
args = [44100, ]
elif len(options.subsample) == 1:
try:
args = [int(options.subsample[0]), ]
except ValueError:
raise ValueError("Could not convert subsample argument "
"to int[%s]" % options.subsample[0])
else:
raise ValueError("Wrong number of subsample arguments, "
"expected 1: %s" % str(options.subsample))
data = random_subsample(afs, *tuple(args))
elif options.method == 'range':
# only accept two arguments, a start and stop
if len(options.subsample) == 2:
try:
args = [int(options.subsample[0]), \
int(options.subsample[1])]
except ValueError:
raise ValueError("Could not convert subsample arguments "
"to int[%s,%s]" % tuple(options.subsample))
else:
raise ValueError("Wrong number of subsample arguments, "
"expected 2: %s" % str(options.subsample))
data = range_subsample(afs, *tuple(args))
elif options.method == 'multi':
if (len(options.subsample) % 2) or (len(options.subsample) == 0):
raise ValueError("Wrong number of subsample arguments, "
"expected at least or multiples of 2: %s" % \
str(options.subsample))
else:
ranges = []
for (s, e) in zip(options.subsample[::2], \
options.subsample[1::2]):
ranges.append((s, e))
data = multi_range_subsample(afs, ranges)
else:
raise ValueError("Unknown subsample method: %s" % options.method)
# ica
ica = run_ica(data, options.ncomponents)
# clean
MM = clean_ica(ica, options.threshold, options.count)
UM = pl.matrix(ica.unmixing_matrix_)
# save M
mmfilename = '%s/mixingmatrix' % options.output
pl.savetxt(mmfilename, MM)
umfilename = '%s/unmixingmatrix' % options.output
pl.savetxt(umfilename, UM)
# add meta info
for fn in [mmfilename, umfilename]:
with open(fn, 'a') as f:
f.write('# method: %s\n' % options.method)
f.write('# subsample: %s\n' % str(options.subsample))
f.write('# threshold: %s\n' % str(options.threshold))
f.write('# count: %i\n' % options.count)
for (i, ifn) in enumerate(inFilenames):
f.write('# %i %s\n' % (i, ifn))
else:
logging.debug("Loading matrix from file: %s" % options.mixingmatrix)
MM = pl.matrix(pl.loadtxt(options.mixingmatrix))
logging.debug("Loading matrix from file: %s" % options.unmixingmatrix)
UM = pl.matrix(pl.loadtxt(options.unmixingmatrix))
if not options.short:
ofs = make_output_files(inFilenames, options.output, \
afs[0].format, afs[0].samplerate)
#clean_files(afs, ofs, ica, MM, options.chunksize)
clean_files(afs, ofs, UM, MM, options.chunksize)
# close
logging.debug("Closing files")
[ofile.close() for ofile in ofs]
if options.plot:
pl.figure()
#pl.imshow(ica.get_mixing_matrix(), interpolation='none')
pl.imshow(MM, interpolation='none')
pl.colorbar()
pl.suptitle("Mixing matrix (pre-cleaning)")
pl.figure()
pl.imshow(MM, interpolation='none')
pl.colorbar()
pl.suptitle("Mixing matrix (post-cleaning)")
# plot components?
pl.show()
return UM, MM
if __name__ == '__main__':
if len(sys.argv[1:]) == 0:
sys.argv += glob.glob('data/clip_*')
sys.argv.append('-v')
sys.argv.append('-t')
sys.argv.append('0.1')
ica, M = process()
sys.exit(0)