forked from smarsland/AviaNZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveletSegment.py
1711 lines (1484 loc) · 80.2 KB
/
WaveletSegment.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# WaveletSegment.py
# Wavelet Segmentation
# Version 3.0 14/09/20
# Authors: Stephen Marsland, Nirosha Priyadarshani, Julius Juodakis, Virginia Listanti
# AviaNZ bioacoustic analysis program
# Copyright (C) 2017--2020
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import WaveletFunctions
import librosa
import copy
import numpy as np
import time, os, math, csv, gc
import SignalProc
import Segment
from ext import ce_denoise as ce
from ext import ce_detect
from itertools import combinations
class WaveletSegment:
# This class implements wavelet segmentation for the AviaNZ interface
def __init__(self, spInfo={}, wavelet='dmey2'):
self.wavelet = wavelet
self.spInfo = spInfo
self.currentSR = 0
if not spInfo == {}:
# for now, we default to the first subfilter:
print("Detected %d subfilters in this filter" % len(spInfo["Filters"]))
self.sp = SignalProc.SignalProc(256, 128)
def readBatch(self, data, sampleRate, d, spInfo, wpmode="new", wind=False):
""" File (or page) loading for batch mode. Must be followed by self.waveletSegment.
Args:
1. data to be segmented, ndarray
2. sampleRate of the data, int
3. d - turn on denoising before calling?
4. spInfo - List of filters to determine which nodes are needed & target sample rate
5. wpmode - old/new/aa to indicate no/partial/full antialias
6. wind - if True, will produce a WP with all nodes to be used in de-winding
"""
if data is None or data == [] or len(data) == 0:
print("ERROR: data must be provided for WS")
return
opst = time.time()
# resample or adjust nodes if needed
self.spInfo = spInfo
# target sample rates should be equal over all requested species
fsOut = set([filt["SampleRate"] for filt in spInfo])
if len(fsOut)>1:
print("ERROR: sample rates must match in all selected filters")
return
fsOut = fsOut.pop()
# copy the filters so that the originals wouldn't be affected between files
# WARNING: this set self.spInfo to be a list [Filters],
# while usually it is just a single filter. I'm sorry.
self.spInfo = copy.deepcopy(spInfo)
# in batch mode, it's worth trying some tricks to avoid resampling
if fsOut == 2*sampleRate:
print("Adjusting nodes for upsampling to", fsOut)
for filter in self.spInfo:
for subfilter in filter["Filters"]:
subfilter["WaveletParams"]['nodes'] = WaveletFunctions.adjustNodes(subfilter["WaveletParams"]['nodes'], "down2")
# Don't want to resample again, so fsTarget = fsIn
fsOut = sampleRate
elif fsOut == 4*sampleRate:
print("Adjusting nodes for upsampling to", fsOut)
# same. Wouldn't recommend repeating for larger ratios than 4x
for filter in self.spInfo:
for subfilter in filter["Filters"]:
downsampled2x = WaveletFunctions.adjustNodes(subfilter["WaveletParams"]['nodes'], "down2")
subfilter["WaveletParams"]['nodes'] = WaveletFunctions.adjustNodes(downsampled2x, "down2")
# Don't want to resample again, so fsTarget = fsIn
fsOut = sampleRate
# Could also similarly "downsample" by adding an extra convolution, but it's way slower
# elif sampleRate == 2*fsOut:
# # don't actually downsample audio, just "upsample" the nodes needed
# for subfilter in self.spInfo["Filters"]:
# subfilter["WaveletParams"]['nodes'] = WaveletFunctions.adjustNodes(subfilter["WaveletParams"]['nodes'], "up2")
# print("upsampled nodes")
# self.spInfo["SampleRate"] = sampleRate
# After upsampling, there will be a sharp drop in energy
# at the original Fs. This will really distort the polynomial
# fit used for wind prediction, so do not allow it.
# (If the nodes were adjusted by the mechanism above, the fit
# will use a continuous block of lower freqs and mostly be fine.)
if wind and fsOut>sampleRate:
print("ERROR: upsampling will cause problems for wind removal. Either turn off the wind filter, or retrain your recognizer to match the sampling rate of these files.")
return
denoisedData = self.preprocess(data, sampleRate, fsOut, d=d, fastRes=True)
# Find out which nodes will be needed:
allnodes = []
if wind:
allnodes = list(range(31, 63))
for filt in self.spInfo:
for subfilter in filt["Filters"]:
allnodes.extend(subfilter["WaveletParams"]["nodes"])
allnodes = list(set(allnodes))
# Generate a full 5 level wavelet packet decomposition (stored in WF.tree)
self.WF = WaveletFunctions.WaveletFunctions(data=denoisedData, wavelet=self.wavelet, maxLevel=20, samplerate=fsOut)
if wpmode == "pywt":
print("ERROR: pywt wpmode is deprecated, use new or aa")
return
if wpmode == "new" or wpmode == "old":
self.WF.WaveletPacket(allnodes, mode='symmetric', antialias=False)
if wpmode == "aa":
self.WF.WaveletPacket(allnodes, mode='symmetric', antialias=True, antialiasFilter=True)
print("File loaded in", time.time() - opst)
# no return, just preloaded self.WF
def waveletSegment(self, filtnum, wpmode="new"):
""" Main analysis wrapper (segmentation in batch mode).
Also do species-specific post-processing.
Reads data pre-loaded onto self.WF.tree by self.readBatch.
Args:
1. filtnum: index of the current filter in self.spInfo (which is a list of filters...)
2. wpmode: old/new/aa to indicate no/partial/full antialias
Returns: list of lists of segments found (over each subfilter)-->[[sub-filter1 segments], [sub-filter2 segments]]
"""
opst = time.time()
# No resampling here. Will read nodes from self.spInfo, which may already be adjusted
### find segments with each subfilter separately
detected_allsubf = []
for subfilter in self.spInfo[filtnum]["Filters"]:
print("-- Identifying calls using subfilter %s --" % subfilter["calltype"])
goodnodes = subfilter['WaveletParams']["nodes"]
detected = self.detectCalls(self.WF, nodelist=goodnodes, subfilter=subfilter, rf=True, aa=wpmode!="old")
# merge neighbours in order to convert the detections into segments
# note: detected np[0 1 1 1] becomes [[1,3]]
segmenter = Segment.Segmenter()
detected = segmenter.convert01(detected)
detected = segmenter.joinGaps(detected, maxgap=0)
detected_allsubf.append(detected)
print("--- Wavelet segmenting completed in %.3f s ---" % (time.time() - opst))
return detected_allsubf
def waveletSegmentChp(self, filtnum, alg, alpha=None, window=None, maxlen=None, silent=True, wind=0):
""" Main analysis wrapper, similar to waveletSegment,
but uses changepoint detection for postprocessing.
Args:
1. filtnum: index of the current filter in self.spInfo (which is a list of filters...)
2. alg: 1 - standard epidemic detector, 2 - nuisance-signal detector
3. alpha: penalty strength for the detector
4. window: wavelets will be merged in groups of this size (s) before analysis
5. maxlen: maximum allowed length (s) of signal segments
3-5 can be None, in which case they are retrieved from self.spInfo.
6. silent: silent (True) or verbose (False) mode
7. adjust for wind? 0=no, 1=interpolate by OLS, 2=interpolate by quantreg
Returns: list of lists of segments found (over each subfilter)-->[[sub-filter1 segments], [sub-filter2 segments]]
"""
opst = time.time()
if silent:
printing=0
else:
printing=1
# No resampling here. Will read nodes from self.spInfo, which may already be adjusted.
### find segments with each subfilter separately
detected_allsubf = []
for subfilter in self.spInfo[filtnum]["Filters"]:
print("-- Identifying calls using subfilter %s --" % subfilter["calltype"])
goodnodes = subfilter['WaveletParams']["nodes"]
if alpha is None:
alpha = subfilter["WaveletParams"]["thr"]
if window is None:
window = subfilter["WaveletParams"]["win"]
if maxlen is None:
maxlen = subfilter["TimeRange"][1]
detected = self.detectCallsChp(self.WF, nodelist=goodnodes, alpha=alpha, window=window, maxlen=maxlen, alg=alg, printing=printing, wind=wind)
detected_allsubf.append(detected)
print("--- WV changepoint segmenting completed in %.3f s ---" % (time.time() - opst))
return detected_allsubf
def waveletSegment_train(self, dirName, thrList, MList, d=False, learnMode='recaa', window=1,
inc=None):
""" Entry point to use during training, called from DialogsTraining.py.
Switches between various training methods, orders data loading etc.,
then just passes the arguments to the right training method and returns the results.
Input: path to directory with wav & wav.data files.
window is window length in sec.
inc is increment length in sec.
Argument _learnMode_ will determine which detectCalls function is used:
learnMode=="ethr":
"1" - get wavelet node energies, threshold over those
learnMode=="recsep":
"3" - reconstruct signal from each node individually, threshold over that
("the old way")
learnMode=="recmulti":
"2" - reconstruct signal from all selected nodes, threshold over that
learnMode=="recaa":
reconstruct signal from each node individually,
using homebrew NOT antialiased WPs, and antialiased reconstruction.
learnMode=="recaafull":
reconstruct signal from each node individually,
using homebrew antialiased WPs (SLOW), and antialiased reconstruction.
Return: tuple of arrays (nodes, tp, fp, tn, fn)
"""
# 1. read wavs and annotations into self.annotation, self.audioList
self.filenames = []
self.loadDirectory(dirName=dirName, denoise=d)
if len(self.annotation) == 0:
print("ERROR: no files loaded!")
return
# 2. find top nodes for each file (self.nodeCorrs)
nlevels = 5
# recommend using wpmode="new", because it is fast and almost alias-free.
wpmode = "new"
self.nodeCorrs = []
self.bestNodes = []
self.worstNodes = []
self.maxEs = []
inc = 1
if len(self.spInfo["Filters"])>1:
print("ERROR: must provide only 1 subfilter at a time!")
return
else:
subfilter = self.spInfo["Filters"][0]
# 2a. prefilter audio to species freq range
for filenum in range(len(self.audioList)):
self.audioList[filenum] = self.sp.bandpassFilter(self.audioList[filenum],
self.spInfo['SampleRate'],
start=subfilter['FreqRange'][0],
end=subfilter['FreqRange'][1])
# 2b. actually compute correlations
for filenum in range(len(self.audioList)):
print("Computing wavelet node correlations in file", filenum+1)
currWCs = self.computeWaveletEnergy(self.audioList[filenum], self.spInfo['SampleRate'], nlevels, wpmode, window=window, inc=inc)
# Compute all WC-annot correlations
nodeCorr = self.compute_r(self.annotation[filenum], currWCs)
self.nodeCorrs.append(nodeCorr)
# find best nodes
bestnodes, worstnodes = self.listTopNodes(filenum)
self.bestNodes.append(bestnodes)
self.worstNodes.append(worstnodes)
print("Adding to negative nodes", worstnodes)
# 3. generate WPs for each file and store the max energies
for filenum in range(len(self.audioList)):
print("Extracting energies from file", filenum+1)
data = self.audioList[filenum]
self.WF = WaveletFunctions.WaveletFunctions(data=data, wavelet=self.wavelet, maxLevel=20, samplerate=self.spInfo['SampleRate'])
# Generate a full 5 level wavelet packet decomposition
if learnMode == "recaa" or learnMode == "recold":
self.WF.WaveletPacket(self.bestNodes[filenum], mode='symmetric', antialias=False)
elif learnMode == "recaafull":
self.WF.WaveletPacket(self.bestNodes[filenum], mode='symmetric', antialias=True, antialiasFilter=True)
else:
print("ERROR: learnMode unrecognised")
return
# find E peaks over possible M (returns [MxTxN])
maxEsFile = self.extractE(self.WF, self.bestNodes[filenum], MList, aa=learnMode!="recold", window=window, inc=inc, annotation=self.annotation[filenum])
self.maxEs.append(maxEsFile)
# self.maxEs now is a list of [files][M][TxN] ndarrays
# 4. mark calls and learn threshold
res = self.gridSearch(self.maxEs, thrList, MList, learnMode, window, inc)
return res
def waveletSegment_trainChp(self, dirName, thrList, window, maxlen):
""" Entry point to use during training, called from DialogsTraining.py.
Switches between various training methods, orders data loading etc.,
then just passes the arguments to the right training method and returns the results.
Input: path to directory with wav & wav.data files.
thrList: list of possible alphas to test.
window: window length in sec (for averaging energies in detection).
maxlen: maximum signal length in sec.
Return: tuple of arrays (nodes, tp, fp, tn, fn)
"""
if len(self.spInfo["Filters"])>1:
print("ERROR: must provide only 1 subfilter at a time!")
return
else:
subfilter = self.spInfo["Filters"][0]
# verify that the provided window will result in an integer
# number of WCs at any level <=5
estWCperWindow = math.ceil(window * self.spInfo['SampleRate']/32)
estrealwindow = estWCperWindow / self.spInfo['SampleRate']*32
if estrealwindow!=window:
print("ERROR: provided window (%f s) will not produce an integer number of WCs. This is currently disabled for safety." % window)
return
# 1. read wavs and annotations into self.annotation, self.audioList
self.filenames = []
self.loadDirectoryChp(dirName=dirName, window=window)
if len(self.annotation) == 0:
print("ERROR: no files loaded!")
return
nwins = [len(annot) for annot in self.annotation]
# --------------------
# 2. determine what nodes can be meaningfully tested, given freq limits
nodeList = []
freqrange = subfilter['FreqRange']
# levels: 0-1, 2-5, 6-13, 14-29, 30-61 (unrooted tree)
# so this will take the last three levels:
for node_un in range(14, 62):
# corresponding node in a rooted tree (as used by WF)
node = node_un + 1
nodefrl, nodefru = WaveletFunctions.getWCFreq(node, self.spInfo["SampleRate"])
if nodefrl < freqrange[1] and nodefru > freqrange[0]:
# node has some overlap with the target range, so can be tested
nodeList.append(node)
# nodeList now stores ROOTED numbers of nodes, to match WF.tree (which has tree[0] = data)
# --------------------
# 3. extract node energies for all files and get node correlations:
# TODO see what can go to loadData
opstartingtime = time.time()
print("--- Starting correlation stage ---")
# TODO convert to a matrix maybe?
allEs = []
allwindows = np.zeros((62, len(self.audioList)))
nodeCorrs = np.zeros((62, len(self.audioList)))
for indexF in range(len(self.audioList)):
print("-- Computing wavelet correlations in file %d / %d --" %(indexF+1, len(self.audioList)))
filenwins = nwins[indexF]
# foffs = sum(nwins[:indexF]) # start of this file's pieces in allEs
# extract energies
currWCs = np.zeros((62, filenwins))
# Generate a full 5 level wavelet packet decomposition
# (this will not be downsampled. antialias=False means no post-filtering)
self.WF = WaveletFunctions.WaveletFunctions(data=self.audioList[indexF], wavelet=self.wavelet, maxLevel=5, samplerate=self.spInfo['SampleRate'])
self.WF.WaveletPacket(nodeList, mode='symmetric', antialias=False)
for node in nodeList:
nodeE, noderealwindow = self.WF.extractE(node, window, wpantialias=True)
allwindows[node-1, indexF] = noderealwindow
# the wavelet energies may in theory have one more or less windows than annots
# b/c they adjust the window size to use integer number of WCs.
# If they differ by <=1, we allow that and just equalize them:
if filenwins==len(nodeE)+1:
currWCs[node-1,:-1] = nodeE
currWCs[node-1,-1] = currWCs[node-1,-2] # repeat last element
elif filenwins==len(nodeE)-1:
# drop last WC
currWCs[node-1,:] = nodeE[:-1]
elif np.abs(filenwins-len(nodeE))>1:
print("ERROR: lengths of annotations and energies differ:", filenwins, len(nodeE))
return
else:
currWCs[node-1,:] = nodeE
allEs.append(currWCs)
# note that currWCs and nodeCorrs are UNROOTED
# Compute all WC-annot correlations for this file
nodeCorrs[:,indexF] = self.compute_r(self.annotation[indexF], currWCs) * len(self.annotation[indexF])
# get a single "correlation" value for each node:
# Note: averaged correlation is not the same as calculating correlation over
# all files, but should be meaningful enough for this.
nodeCorrs = np.abs(np.sum(nodeCorrs, axis=1) / sum(nwins))
print(nodeCorrs)
print("Correlations completed in", time.time() - opstartingtime)
# find best nodes (+1 b/c nodeCorrs are unrooted indices, and nodeList is rooted)
# this will likely include nodes in top levels as well (0-14), just keep in mind.
bestnodes = np.argsort(nodeCorrs)[-15:]+1
print("Best nodes: ", bestnodes)
print("Before filtering: ", nodeList)
nodeList = [node for node in nodeList if node in bestnodes]
# --------------------
# 4. run the detector for each setting (node x thr x filepieces)
opstartingtime = time.time()
print("--- Starting detection stage ---")
alldetections = np.zeros((len(nodeList), len(thrList), sum(nwins)))
for indexF in range(len(self.audioList)):
print("-- Extracting energies from file %d / %d --" %(indexF+1, len(self.audioList)))
# Could bandpass, but I wasn't doing it in the paper
# audio = self.sp.ButterworthBandpass(self.audioList[filenum],
# self.spInfo['SampleRate'],
# low=freqrange[0], high=freqrange[1])
filenwins = nwins[indexF]
foffs = sum(nwins[:indexF]) # start of this file's pieces in alldetections
for indexn in range(len(nodeList)):
node = nodeList[indexn]
print("Analysing node", node)
# extract node from file num, and average over windows of set size
# (wpantialias=True specifies the non-downsampled WP)
nodeE = allEs[indexF][node-1,:]
noderealwindow = allwindows[node-1, indexF]
nodesigma2 = np.percentile(nodeE, 10)
# NOTE: we're providing points on the original scale (non-squared) for the C part
nodeE = np.sqrt(nodeE)
# Convert max segment length from s to realized windows
# (segments exceeding this length will be marked as 'n')
realmaxlen = math.ceil(maxlen / noderealwindow)
print("node prepared for detection")
for indext in range(len(thrList)):
print("Detecting with alpha=", thrList[indext])
# run detector with thr on nodeE
thrdet = ce_detect.launchDetector2(nodeE, nodesigma2, realmaxlen, alpha=thrList[indext], printing=0).astype('float')
# keep only S and their positions:
if np.shape(thrdet)[0]>0:
thrdet = thrdet[np.logical_or(thrdet[:,2]==ord('s'), thrdet[:,2]==ord('o')), 0:2]
# convert detections from the window scale into actual seconds
# and then back into 0/1 over some windows for computing F1 score later
# (the original binary detections are in realised windows which may differ over nodes)
thrdet[:,:2] = thrdet[:,:2] * noderealwindow / window
thrdetbin = np.zeros(filenwins, dtype=np.uint8)
for i in range(np.shape(thrdet)[0]):
start = math.floor(thrdet[i,0])
end = min(filenwins, math.ceil(thrdet[i,1]))
thrdetbin[start:end] = 1
# store the detections
alldetections[indexn, indext, foffs:(foffs+filenwins)] = thrdetbin
# alldetections is now a 3d np.array of 0/1 over nodes x thr x windows over all files
print("Detections completed in", time.time() - opstartingtime)
# for Fbeta score, concat 0/1 annotations over all files into a long vector:
allannots = np.concatenate(self.annotation)
# --------------------
# 5. find top nodes by Fbeta-score, for each thr
# enumerate all possible subsets of up to k nodes:
opstartingtime = time.time()
print("--- Starting best-subset search ---")
MAXK = 6
print("Enumerating subsets up to K=", MAXK)
ksubsets = []
for k in range(1,MAXK+1):
ksubsets.extend(list(combinations(range(len(nodeList)), k)))
subsetfbs = np.zeros((len(ksubsets), len(thrList)))
NODEPEN = 0.01
total_positives = np.count_nonzero(allannots) # precalculated for speed
for indext in range(len(thrList)):
# Best-subset part
# find the best set of k nodes to start with:
print("-- thr = ", thrList[indext])
for nodeset_ix in range(len(ksubsets)):
# evaluate F1 score of this nodeset over all files
# (each nodeset is a list of indices to nodes in nodeList)
nodeset = list(ksubsets[nodeset_ix])
# print("evaluating subset", [nodeList[nn] for nn in nodeset])
detect_allnodes = np.logical_or.reduce(alldetections[nodeset, indext, :])
fB = self.fBetaScore_fast(allannots, detect_allnodes, total_positives)
# Add a penalty for the number of nodes
subsetfbs[nodeset_ix, indext] = fB - len(nodeset)*NODEPEN
print("Best-subset search completed in", time.time() - opstartingtime)
# output arrays
# (extra dimension for compatibility w/ multiple Ms)
tpa = np.zeros((1, len(thrList)))
fpa = np.zeros((1, len(thrList)))
tna = np.zeros((1, len(thrList)))
fna = np.zeros((1, len(thrList)))
finalnodes = []
# TODO this part can be entirely replaced with larger K
# b/c 15 nodes produce 32k subsets over all K.
# STEPWISE part, with top N best subsets used to initialise N runs
opstartingtime = time.time()
print("--- Starting stepwise search ---")
NTOPSETS = 1 # how many different initialisations to use
for indext in range(len(thrList)):
print("-- Optimising with t %d/%d (thr=%f)" % (indext + 1, len(thrList), thrList[indext]))
# best nodesets for this t
bestix = np.argsort(subsetfbs[:,indext])[-NTOPSETS:]
# take a good subset, and continue from there:
# (repeat with NTOPSETS different initializations for each threshold)
fB_out = 0
nodes_out = []
tp_out = 0
fp_out = 1 # init the bad stats to 1, so that nothing breaks
tn_out = 0 # even if the detector fails entirely
fn_out = 1
for i in range(np.shape(bestix)[0]):
b = bestix[i]
top_subset_nodes = [nodeList[nix] for nix in ksubsets[b]]
print("Top subset:", top_subset_nodes)
# detections with the nodes in the current subset
# (note that alldetections is indexed by node position in nodeList,
# not by actual node number!)
detect_best = np.maximum.reduce(alldetections[list(ksubsets[b]), indext, :])
# recalculate statistics for these nodes (should be same as subsetfbs)
bestfB, bestRecall, tp, fp, tn, fn = self.fBetaScore(allannots, detect_best)
# Add a penalty for the number of nodes
if bestfB is not None:
bestfB = bestfB - NODEPEN*len(top_subset_nodes)
else:
bestfB = 0
print("starting nodes", top_subset_nodes)
print("starting fb", bestfB, "recall", bestRecall)
# nodes still available for the stepwise search:
# reversed to start with lower-level nodes
for new_node_ix in reversed(range(len(nodeList))):
new_node = nodeList[new_node_ix]
if new_node in top_subset_nodes:
continue
print("testing node", new_node)
# try adding the new node
detect_withnew = np.maximum.reduce([detect_best, alldetections[new_node_ix, indext, :]])
fB, recall, tp, fp, tn, fn = self.fBetaScore(allannots, detect_withnew)
if fB is None:
continue
# Add a penalty for the number of nodes
fB = fB - NODEPEN*(len(top_subset_nodes)+1)
# If this node improved fB,
# store it and update fB, recall, best detections, and optimum nodes
if fB > bestfB:
print("Adding node", new_node)
print("new fb", fB, "recall", recall)
top_subset_nodes.append(new_node)
detect_best = detect_withnew
bestfB = fB
bestRecall = recall
# Adding more nodes will not reduce FPs, so this is sufficient to stop:
# Stopping a bit earlier to have fewer nodes and fewer FPs:
if bestfB >= 0.95 or bestRecall >= 0.95:
break
# store this if this is the best initialisation
if bestfB>fB_out:
fB_out = bestfB
nodes_out = top_subset_nodes
tp_out = tp
fp_out = fp
tn_out = tn
fn_out = fn
# populate output
tpa[0, indext] = tp_out
fpa[0, indext] = fp_out
tna[0, indext] = tn_out
fna[0, indext] = fn_out
finalnodes.append(nodes_out)
print("-- Run t %d/%d complete\n---------------- " % (indext + 1, len(thrList)))
print("Stepwise search completed in", time.time() - opstartingtime)
return [finalnodes], tpa, fpa, tna, fna
def waveletSegment_cnn(self, dirName, filt):
""" Wrapper for segmentation to be used when generating cnn data.
Should be identical to processing the files in batch mode,
+ returns annotations.
Does not do any processing besides basic conversion 0/1 -> [s,e].
Uses 15 min pages, if files are larger than that.
Args:
1. directory to process (recursively)
2. a filter with a single subfilter.
Return values:
1. list of (filename, [segments]) over all files and pages
"""
# constant - files longer than this will be processed in pages
samplesInPage = 900*16000
# clear storage for multifile processing
detected_out = []
filenames = []
self.annotation = []
# find audio files with 0/1 annotations:
for root, dirs, files in os.walk(dirName):
for file in files:
if file.lower().endswith('.wav') and os.stat(os.path.join(root, file)).st_size != 0 and file[:-4] + '-GT.txt' in files:
filenames.append(os.path.join(root, file))
if len(filenames) < 1:
print("ERROR: no suitable files")
return
for filename in filenames:
# similar to _batch: loadFile(self.species)
# sets self.sp.data, self.sp.sampleRate, appends self.annotation
succ = self.loadData(filename)
if not succ:
print("ERROR: failed to load file", filename)
return
# (ceil division for large integers)
numPages = (len(self.sp.data) - 1) // samplesInPage + 1
for page in range(numPages):
print("Processing page %d / %d" % (page+1, numPages))
start = page*samplesInPage
end = min(start+samplesInPage, len(self.sp.data))
filelen = math.ceil((end-start)/self.sp.sampleRate)
if filelen < 2:
print("Warning: can't process short file ends (%.2f s)" % filelen)
continue
# read in page and resample as needed
# will also set self.spInfo with ADJUSTED nodes if resampling!
self.readBatch(self.sp.data[start:end], self.sp.sampleRate, d=False, spInfo=[filt], wpmode="new", wind=False)
# TODO not sure if wind removal should be done here.
# Maybe not, to generate more noise examples?
# segmentation, same as in batch mode. returns [[sub-filter1 segments]]
if "method" not in filt or filt["method"]=="wv":
detected_segs = self.waveletSegment(0, wpmode="new")
elif filt["method"]=="chp":
detected_segs = self.waveletSegmentChp(0, alg=2, wind=False)
# flatten over the call types and store
out = []
for subfilterdet in detected_segs:
out.extend(subfilterdet)
detected_out.append((filename, out))
return detected_out
def computeWaveletEnergy(self, data, sampleRate, nlevels=5, wpmode="new", window=1, inc=1):
""" Computes the energy of the nodes in the wavelet packet decomposition
Args:
1. data (waveform)
2. sample rate
3. max levels for WP decomposition
4. WP style ("new"-our non-downsampled, "aa"-our fully AA'd)
5-6. window and inc in seconds, as in other functions. NOTE: this does NOT take into account annotation length
There are 62 coefficients up to level 5 of the wavelet tree (without root!!), and 300 seconds [N sliding window] in 5 mins
Hence returned coefs would then be a 62*300 matrix [62*N matrix]
For smaller windows (controlled by window and inc args), returns the energy within each window, so the return has len/inc columns.
The energy is the sum of the squares of the data in each node divided by the total in that level of the tree as a percentage.
"""
if data is None or sampleRate is None:
print("ERROR: data and Fs need to be specified")
return
# number of samples in window
win_sr = int(math.ceil(window*sampleRate))
# number of sample in increment
inc_sr = int(math.ceil(inc*sampleRate))
# output columns dimension equal to number of sliding window
N = int(math.ceil(len(data)/inc_sr))
coefs = np.zeros((2 ** (nlevels + 1) - 2, N))
# generate a WP on all of the data
WF = WaveletFunctions.WaveletFunctions(data, wavelet=self.wavelet, maxLevel=20, samplerate=sampleRate)
if wpmode == "pywt":
print("ERROR: pywt mode deprecated, use new or aa")
return
elif wpmode == "new":
allnodes = range(2 ** (nlevels + 1) - 1)
WF.WaveletPacket(allnodes, mode='symmetric', antialias=False)
elif wpmode == "aa":
allnodes = range(2 ** (nlevels + 1) - 1)
WF.WaveletPacket(allnodes, mode='symmetric', antialias=True, antialiasFilter=True)
# TODO this nonsense could be replaced w/ WF.extractE for consistency
# for each sliding window:
# start,end are its coordinates (in samples)
start = 0
for t in range(N):
E = []
end = min(len(data), start+win_sr)
# Calculate energies of all nodes EXCEPT ROOT - from 1 to 2^(nlevel+1)-1
for level in range(1, nlevels + 1):
# Calculate the window position in WC coordinates
dsratio = 2**level
WCperWindow = math.ceil(win_sr/dsratio)
if wpmode=="aa" or wpmode=="new": # account for non-downsampled tree
WCperWindow = 2*WCperWindow
# (root would not require this, but is skipped here anyway)
startwc = t*WCperWindow
endwc = startwc+WCperWindow
# Extract the energy
lvlnodes = WF.tree[2 ** level - 1:2 ** (level + 1) - 1]
e = np.array([np.sum(n[startwc:endwc] ** 2) for n in lvlnodes])
if np.sum(e) > 0:
e = 100.0 * e / np.sum(e) # normalize per-level
E = np.concatenate((E, e), axis=0)
start += inc_sr
# so now 0-1 is the first level, 2-5 the second etc.
coefs[:,t] = E
return coefs
def fBetaScore_fast(self, annotation, predicted, T, beta=2):
""" Computes the beta scores given two sets of redictions.
Simplified by dropping printouts and some safety checks.
(Assumes logical or int 1/0 input.)
Outputs 0 when the score is undefined. """
TP = np.count_nonzero(annotation & predicted)
# T = np.count_nonzero(annotation) # precalculated and passed in for speed
P = np.count_nonzero(predicted)
if T==0 or P==0 or TP==0:
return 0
recall = float(TP) / T # TruePositive/#True
precision = float(TP) / P # TruePositive/#Positive
fB = ((1. + beta ** 2) * recall * precision) / (recall + beta ** 2 * precision)
return fB
def fBetaScore(self, annotation, predicted, beta=2):
""" Computes the beta scores given two sets of predictions """
annotation = np.array(annotation)
predicted = np.array(predicted)
TP = float(np.sum(np.where((annotation == 1) & (predicted == 1), 1, 0)))
T = float(np.sum(annotation)) # to force all divisions to float
P = float(np.sum(predicted))
if T != 0:
recall = TP / T # TruePositive/#True
else:
recall = None
if P != 0:
precision = TP / P # TruePositive/#Positive
else:
precision = None
if recall is not None and precision is not None and not (recall == 0 and precision == 0):
fB = ((1. + beta ** 2) * recall * precision) / (recall + beta ** 2 * precision)
else:
fB = None
if recall is None and precision is None:
print("TP=%d \tFP=%d \tTN=%d \tFN=%d \tRecall=%s \tPrecision=%s \tfB=%s" % (
TP, P - TP, len(annotation) - (P + T - TP), T - TP, recall, precision, fB))
elif recall is None:
print("TP=%d \tFP=%d \tTN=%d \tFN=%d \tRecall=%s \tPrecision=%0.2f \tfB=%s" % (
TP, P - TP, len(annotation) - (P + T - TP), T - TP, recall, precision, fB))
elif precision is None:
print("TP=%d \tFP=%d \tTN=%d \tFN=%d \tRecall=%0.2f \tPrecision=%s \tfB=%s" % (
TP, P - TP, len(annotation) - (P + T - TP), T - TP, recall, precision, fB))
elif fB is None:
print("TP=%d \tFP=%d \tTN=%d \tFN=%d \tRecall=%0.2f \tPrecision=%0.2f \tfB=%s" % (
TP, P - TP, len(annotation) - (P + T - TP), T - TP, recall, precision, fB))
else:
print("TP=%d \tFP=%d \tTN=%d \tFN=%d \tRecall=%0.2f \tPrecision=%0.2f \tfB=%0.2f" % (
TP, P - TP, len(annotation) - (P + T - TP), T - TP, recall, precision, fB))
# print TP, int(T), int(P), recall, precision, ((1.+beta**2)*recall*precision)/(recall + beta**2*precision)
return fB, recall, TP, P - TP, len(annotation) - (P + T - TP), T - TP # fB, recall, TP, FP, TN, FN
def compute_r(self, annotation, waveletCoefs):
""" Computes the point-biserial correlations for a set of labels and a set of wavelet coefficients.
r = (M_p - M_q) / S * sqrt(p*q), M_p = mean for those that are 0, S = std dev overall, p = proportion that are 0.
Inputs:
1. annotation - np.array of length n, where n - number of blocks (with resolution length) in file
2. waveletCoefs - np.array of DxN, where D - number of nodes in WP (62 for lvl 5) N= number of sliding windows
"""
if len(annotation)!=np.shape(waveletCoefs)[1]:
print("ERROR: wavelet and annotation lengths must match")
return
w0 = np.where(annotation == 0)[0]
w1 = np.where(annotation == 1)[0]
r = np.zeros(np.shape(waveletCoefs)[0])
for node in range(len(r)):
# for safety e.g. when an array was filled with a const and SD=0
if np.all(waveletCoefs[node,:]==waveletCoefs[node,0]):
r[node] = 0
continue
r[node] = (np.mean(waveletCoefs[(node, w1)]) - np.mean(waveletCoefs[(node, w0)])) / np.std(waveletCoefs[node, :]) * np.sqrt(len(w0) * len(w1)) / len(annotation)
return r
def sortListByChild(self, order):
""" Inputs is a list sorted into order of correlation.
This functions resort so that any children of the current node that are in the list go first.
Assumes that there are five levels in the tree (easy to extend, though)
"""
# TODO: simplify and make flexible for any size list
newlist = []
currentIndex = 0
# Need to keep track of where each level of the tree starts
# unrooted:
starts = [0, 2, 6, 14, 30, 62]
# rooted:
# starts = [1, 3, 7, 15, 31, 63]
while len(order) > 0:
if order[0] < 30:
# It could have children lower down the list
# Build a list of the children of the first element of order
level = int(np.log2(order[0] + 2))
nc = 2
first = order[0]
for l in range(level + 1, 6):
children = []
current = currentIndex
for i in range(nc):
children.append(starts[l] + 2 * (first - starts[l - 1]) + i)
nc *= 2
first = starts[l] + 2 * (first - starts[l - 1])
# Have to do it this annoying way since Python seems to ignore the next element if you delete one while iterating over the list
i = 0
order_sub = []
while i < len(children):
if children[i] not in order:
del (children[i])
else:
order_sub.append(order.index(children[i]))
i += 1
# Sort into order
children = [x for (y, x) in sorted(zip(order_sub, children), key=lambda pair: pair[0])]
for a in children:
# If so, remove and insert at the current location in the new list
newlist.insert(current, a)
current += 1
order.remove(a)
# Finally, add the first element
newlist.append(order[0])
currentIndex = newlist.index(order[0]) + 1
del (order[0])
return newlist
def extractE(self, wf, nodelist, MList, annotation=None, window=1, inc=None, aa=True):
"""
Regenerates the signal from each of nodes and finds max standardized E.
Args:
1. wf - WaveletFunctions with a homebrew wavelet tree (list of ndarray nodes)
2. nodelist - will reconstruct signal and run detections on each of these nodes separately
3. MList - passed here to allow multiple Ms to be tested from one reconstruction
4. rf - bandpass to species freq range?
5. annotation - for calculating noise properties during training
6-7. window, inc - window / increment length, seconds
8. antialias - True/False
Return: ndarrays of MxTxN energies, for each of M values, T windows and N nodes
"""
if inc is None:
inc = window
resol = window
else:
resol = (math.gcd(int(100 * window), int(100 * inc))) / 100
annotation = np.array(annotation)
duration = len(wf.tree[0])
# Window, inrement, and resolution are converted to true seconds based on the tree samplerate
samplerate = wf.treefs
win_sr = math.ceil(window * samplerate)
inc_sr = math.ceil(inc * samplerate)
resol_sr = math.ceil(resol * samplerate)
# number of windows of length inc
nw = int(np.ceil(duration / inc_sr))
nodenum = 0
maxE = np.zeros((len(MList), nw, len(nodelist)))
for node in nodelist:
useWCenergies = False
# Option 1: use wavelet coef energies directly
if useWCenergies:
# how many samples went into one WC?
samples_wc = 2**math.floor(math.log2(node+1))
duration = int(duration/samples_wc)
# put WC from test node(s) on the new tree
C = wf.tree[node][0::2]
# Option 2: reconstruct from the WCs, as before
else:
samples_wc = 1
C = wf.reconstructWP2(node, antialias=aa, antialiasFilter=True)
# Sanity check for all zero case
if not any(C):
continue
if len(C) > duration:
C = C[:duration]
C = np.abs(C)
N = len(C)
# Compute threshold using mean & sd from non-call sections
if annotation is not None:
noiseSamples = np.repeat(annotation == 0, resol_sr/samples_wc)
noiseSamples = noiseSamples[:len(C)]
else:
print("Warning: no annotations detected in file")
noiseSamples = np.full(len(C), True)
meanC = np.mean(np.log(C[noiseSamples]))
stdC = np.std(np.log(C[noiseSamples]))
# Compute the energy curve (a la Jinnai et al. 2012)
# using different M values, for a single node.
for indexM in range(len(MList)):
# Compute the number of samples in a window -- species specific
# Convert M to number of WCs
M = int(MList[indexM] * win_sr/samples_wc)
E = ce.EnergyCurve(C, M)
# for each sliding window, find largest E
start = 0
for j in range(nw):
end = min(N, int(start + win_sr/samples_wc))
# NOTE: here we determine the statistic (mean/max...) for detecting calls
maxE[indexM, j, nodenum] = (np.log(np.mean(E[start:end])) - meanC) / stdC
start += int(inc_sr/samples_wc)
nodenum += 1
C = None
E = None
del C
del E
gc.collect()
return maxE
def detectCalls(self, wf, nodelist, subfilter, rf=True, annotation=None, window=1, inc=None, aa=True):
"""
For wavelet TESTING and general SEGMENTATION
Regenerates the signal from the node and threshold.
Args:
1. wf - WaveletFunctions with a homebrew wavelet tree (list of ndarray nodes)
2. nodelist - will reconstruct signal and run detections on each of these nodes separately
3. subfilter - used to pass thr, M, and other parameters
4. rf - bandpass to species freq range?
5. annotation - for calculating noise properties during training
6-7. window, inc
8. antialias - True/False
Return: ndarray of 1/0 annotations for each of T windows
"""
# Virginia: this function now works with OVERLAPPING sliding windows
# Added window and increment input.
# Window is window length in seconds
# inc is increment length in seconds
# Changed detection to work with sliding overlapping window
# It compute energy in "centered" window.
# Virginia: if no increment I set it equal to window
if inc is None:
inc = window
resol = window
else:
resol = (math.gcd(int(100 * window), int(100 * inc))) / 100
duration = len(wf.tree[0])
# To convert window / increment / resolution to samples,
# we use the actual sampling rate of the tree
# i.e. "wf.treefs" samples of wf.tree[0] will always correspond to 1 s
win_sr = math.ceil(window * wf.treefs)
inc_sr = math.ceil(inc * wf.treefs)
resol_sr = math.ceil(resol * wf.treefs)