forked from smarsland/AviaNZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment.py
1562 lines (1388 loc) · 63.2 KB
/
Segment.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
# Segment.py
# A variety of segmentation algorithms for AviaNZ
# 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 SignalProc
import SupportClasses
import Shapes
import numpy as np
import scipy.ndimage as spi
from scipy import signal
import librosa
import time
from ext import ce_denoise as ce
import json
import os
import math
import copy
import wavio
from scipy.interpolate import interp1d
from scipy.signal import medfilt
import skimage.measure as skm
import tensorflow as tf
try:
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
pass
class Segment(list):
""" A single AviaNZ annotation ("segment" or "box" type).
Deals with identifying the right Label from this list.
Labels should be added either when initiating Segment,
or through Segment.addLabel.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if len(self) != 5:
print("ERROR: incorrect number of args provided to Segment (need 5, not %d)" % len(self))
return
if self[0]<0 or self[1]<0:
print("ERROR: Segment times must be positive or 0")
return
if self[2]<0 or self[3]<0:
print("ERROR: Segment frequencies must be positive or 0")
return
if not isinstance(self[4], list):
print("ERROR: Segment labels must be a list")
return
# check if labels have the right structure
for lab in self[4]:
if not isinstance(lab, dict):
print("ERROR: Segment label must be a dict")
return
if "species" not in lab or not isinstance(lab["species"], str):
print("ERROR: species bad or missing from label")
return
if "certainty" not in lab or not isinstance(lab["certainty"], (int, float)):
print("ERROR: certainty bad or missing from label")
return
if "filter" in lab and lab["filter"]!="M" and "calltype" not in lab:
print("ERROR: calltype required when automated filter provided in label")
return
# fix types to avoid numpy types etc
self[0] = float(self[0])
self[1] = float(self[1])
self[2] = int(self[2])
self[3] = int(self[3])
self.keys = [(lab['species'], lab['certainty']) for lab in self[4]]
if len(self.keys)>len(set(self.keys)):
print("ERROR: non-unique species/certainty combo detected")
return
def hasLabel(self, species, certainty):
""" Check if label identified by species-cert combo is present in this segment. """
return (species, certainty) in self.keys
def addLabel(self, species, certainty, **label):
""" Adds a label to this segment.
Species and certainty are required and passed positionally.
Any further label properties (filter, calltype...) must be passed as keyword args:
addLabel("LSK", 100, filter="M"...)
"""
if not isinstance(species, str):
print("ERROR: bad species provided")
return
if not isinstance(certainty, (int, float)):
print("ERROR: bad certainty provided")
return
if "filter" in label and label["filter"]!="M" and "calltype" not in label:
print("ERROR: calltype required when automated filter provided in label")
return
if self.hasLabel(species, certainty):
print("ERROR: this species-certainty label already present")
return
label["species"] = species
label["certainty"] = certainty
self[4].append(label)
self.keys.append((species, certainty))
### --- couple functions to process all labels for a given species ---
def wipeSpecies(self, species):
""" Remove all labels for species, return True if all labels were wiped
(and the interface should delete the segment).
"""
deletedAll = list(set([lab["species"] for lab in self[4]])) == [species]
# note that removeLabel will re-add a Don't Know in the end, so can't just check the final label.
for lab in reversed(self[4]):
if lab["species"]==species:
print("Wiping label", lab)
self.removeLabel(lab["species"], lab["certainty"])
return deletedAll
def confirmLabels(self, species=None):
""" Raise the certainty of this segment's uncertain labels to 100.
Affects all species (if None) or indicated species.
Ignores "Don't Know" labels.
"""
toremove = []
for labix in range(len(self[4])):
lab = self[4][labix]
# check if this label is yellow:
if (species is None or lab["species"]==species) and lab["certainty"] < 100 and lab["species"]!="Don't Know":
# check if this segment has a green label for this species already
if (lab["species"], 100) in self.keys:
# then just delete the yellow label
toremove.append(lab)
else:
lab["certainty"] = 100
self.keys[labix] = (lab["species"], lab["certainty"])
for trlab in toremove:
self.removeLabel(trlab["species"], trlab["certainty"])
def questionLabels(self, species=None):
""" Lower the certainty of this segment's certain labels to 50.
Affects all species (if None) or indicated species.
Ignores "Don't Know" labels.
(Could be merged with the above at some point.)
Returns True if it changed any labels.
"""
anyChanged = False
toremove = []
for labix in range(len(self[4])):
lab = self[4][labix]
# check if this label is green:
if (species is None or lab["species"]==species) and lab["certainty"]==100 and lab["species"]!="Don't Know":
# check if this segment has a yellow label for this species already
otherLabels = [k[0]==lab["species"] and k[1]<100 for k in self.keys]
if any(otherLabels):
# then just delete this label
toremove.append(lab)
else:
lab["certainty"] = 50
self.keys[labix] = (lab["species"], lab["certainty"])
anyChanged = True
for trlab in toremove:
self.removeLabel(trlab["species"], trlab["certainty"])
return(anyChanged)
def removeLabel(self, species, certainty):
""" Removes label from this segment.
Does not delete the actual segment - that's left for the interface to take care of.
"""
deleted = False
for lab in self[4]:
if lab["species"]==species and lab["certainty"]==certainty:
self[4].remove(lab)
try:
self.keys.remove((species, certainty))
except Exception as e:
text = "************ WARNING ************\n"
text += str(e)
text += "\nWhile trying to remove key "+str(species)+"-"+str(certainty) + " from "+ str(self[4])
text += "\nWhich had keys" + str(self.keys)
import SupportClasses_GUI
msg = SupportClasses_GUI.MessagePopup("w", "ERROR - please report", text)
msg.exec_()
# if that was the last label, flip to Don't Know
if len(self[4])==0:
self.addLabel("Don't Know", 0)
deleted = True
break
if not deleted:
print("ERROR: could not find species-certainty combo to remove:", species, certainty)
return
def infoString(self):
""" Returns a nicely-formatted string of this segment's info."""
s = []
for lab in self[4]:
labs = "sp.: %s, cert.: %d%%" % (lab["species"], lab["certainty"])
if "filter" in lab and lab["filter"]!="M":
labs += ", filter: " + lab["filter"]
if "calltype" in lab:
labs += ", call: " + lab["calltype"]
s.append(labs)
return "; ".join(s)
class SegmentList(list):
""" List of Segments. Deals with I/O - parsing JSON,
and retrieving the right Segment from this list.
"""
def parseJSON(self, file, duration=0, silent=False):
""" Takes in a filename and reads metadata to self.metadata,
and other segments to just the main body of self.
If wav file is loaded, pass the true duration in s to check
(it will override any duration read from the JSON).
"""
try:
f = open(file, 'r')
annots = json.load(f)
f.close()
except Exception as e:
print("ERROR: file %s failed to load with error:" % file)
print(e)
return
# first segment stores metadata
self.metadata = dict()
if isinstance(annots[0], list) and annots[0][0] == -1:
if not silent:
print("old format metadata detected")
self.metadata = {"Operator": annots[0][2], "Reviewer": annots[0][3]}
# when file is loaded, true duration can be passed. Otherwise,
# some old files have duration in samples, so need a rough check
if duration>0:
self.metadata["Duration"] = duration
elif isinstance(annots[0][1], (int, float)) and annots[0][1]>0 and annots[0][1]<100000:
self.metadata["Duration"] = annots[0][1]
else:
# fallback to reading the wav:
try:
self.metadata["Duration"] = wavio.readFmt(file[:-5])[1]
except Exception as e:
print("ERROR: duration not found in metadata, arguments, or read from wav")
print(file)
print(e)
return
# noise metadata
if len(annots[0])<5 or not isinstance(annots[0][4], list):
self.metadata["noiseLevel"] = None
self.metadata["noiseTypes"] = []
else:
self.metadata["noiseLevel"] = annots[0][4][0]
self.metadata["noiseTypes"] = annots[0][4][1]
del annots[0]
elif isinstance(annots[0], dict):
# New format has 3 required fields:
self.metadata = annots[0]
if duration>0:
self.metadata["Duration"] = duration
if "Operator" not in self.metadata or "Reviewer" not in self.metadata or "Duration" not in self.metadata:
print("ERROR: required metadata fields not found")
return
del annots[0]
# read the segments
self.clear()
for annot in annots:
if not isinstance(annot, list) or len(annot)!=5:
print("ERROR: annotation in wrong format:", annot)
return
# This could be turned on to skip segments outside Duration bounds,
# but may result in deleting actually useful annotations if Duration was wrong
# if annot[0] > self.metadata["Duration"] and annot[1] > self.metadata["Duration"]:
# print("Warning: ignoring segment outside set duration", annot)
# continue
# deal with old formats here, so that the Segment class
# could require (and validate) clean input
# Early version of AviaNZ stored freqs as values between 0 and 1.
# The .1 is to take care of rounding errors
if 0 < annot[2] < 1.1 and 0 < annot[3] < 1.1:
print("Warning: ignoring old-format frequency marks")
annot[2] = 0
annot[3] = 0
# single string-type species labels
if isinstance(annot[4], str):
annot[4] = [annot[4]]
# for list-type labels, parse each into certainty and species
if isinstance(annot[4], list):
listofdicts = []
for lab in annot[4]:
# new format:
if isinstance(lab, dict):
labdict = lab
# old format parsing:
elif lab == "Don't Know":
labdict = {"species": "Don't Know", "certainty": 0}
elif lab.endswith('?'):
labdict = {"species": lab[:-1], "certainty": 50}
else:
labdict = {"species": lab, "certainty": 100}
listofdicts.append(labdict)
# if no labels were present, i.e. "[]", addSegment will create a Don't Know
annot[4] = listofdicts
self.addSegment(annot)
if not silent:
print("%d segments read" % len(self))
def addSegment(self, segment):
""" Just a cleaner wrapper to allow adding segments quicker.
Passes a list "segment" to the Segment class.
"""
# allows passing empty label list - creates "Don't Know" then
if len(segment[4]) == 0:
segment[4] = [{"species": "Don't Know", "certainty": 0}]
self.append(Segment(segment))
def addBasicSegments(self, seglist, freq=[0,0], **kwd):
""" Allows to add bunch of basic segments from segmentation
with identical species/certainty/freq values.
seglist - list of 2-col segments [[t1, t2],prob]
label is built from kwd.
These will be converted to [[t1, t2, freq[0], freq[1], label], ...]
and stored.
"""
if not isinstance(freq, list) or freq[0]<0 or freq[1]<0:
print("ERROR: cannot use frequencies", freq)
return
for seg in seglist:
newseg = [seg[0][0], seg[0][1], freq[0], freq[1], [kwd]]
self.addSegment(newseg)
def getSpecies(self, species):
""" Returns indices of all segments that have the indicated species in label. """
out = []
for segi in range(len(self)):
# check each label in this segment:
labs = self[segi][4]
for lab in labs:
if lab["species"] == species:
out.append(segi)
# go to next seg
break
return(out)
def getCalltype(self, species, calltype):
""" Returns indices of all segments that have the indicated species & calltype in label. """
out = []
for segi in range(len(self)):
# check each label in this segment:
labs = self[segi][4]
for lab in labs:
try:
if lab["species"] == species and lab["calltype"] == calltype:
out.append(segi)
# go to next seg
break
except:
pass
return(out)
def saveJSON(self, file, reviewer=""):
""" Returns 1 on succesful save."""
if reviewer != "":
self.metadata["Reviewer"] = reviewer
annots = [self.metadata]
for seg in self:
annots.append(seg)
file = open(file, 'w')
json.dump(annots, file)
file.write("\n")
file.close()
return 1
def orderTime(self):
""" Returns the order of segments in this list sorted by start time.
Sorts itself using the order. Can then be used to sort any additional lists
in matching order (graphics etc). """
sttimes = [s[0] for s in self]
sttimes = np.argsort(sttimes)
self.sort(key=lambda s: s[0])
return(sttimes)
def splitLongSeg(self, maxlen=10, species=None):
"""
Splits long segments (> maxlen) evenly
Operates on segment data structure
[1,5,a,b, [{}]] -> [1,3,a,b, [{}]], [3,5,a,b, [{}]]
"""
toadd = []
for seg in self:
# if species is given, only split segments where it is present:
if species is not None and species not in [lab["species"] for lab in seg[4]]:
continue
l = seg[1]-seg[0]
if l > maxlen:
n = int(np.ceil(l/maxlen))
d = l/n
# adjust current seg to be the first piece
seg[1] = seg[0]+d
for i in range(1,n):
end = min(l, d * (i+1))
segpiece = copy.deepcopy(seg)
segpiece[0] = seg[0] + d*i
segpiece[1] = seg[0] + end
# store further pieces to be added
toadd.append(segpiece)
# now add them, to avoid messing with the loop length above
for seg in toadd:
self.addSegment(seg)
def mergeSplitSeg(self):
""" Inverse of the above: merges overlapping segments.
Merges only segments with identical labels,
so e.g. [kiwi, morepork] [kiwi] will not be merged.
Unlike analogs in Segmenter and PostProcess,
merges segments that only touch ([1,2][2,3]->[1,3]).
DOES NOT DELETE segments - returns indices to be deleted,
so an external handler needs to do the required interface updates.
ASSUMES sorted input!
"""
todelete = []
if len(self)==0:
return []
# ideally, we'd loop over different labels, but not easy since they're unhashable.
# so we use a marker array to keep track of checked segments:
done = np.zeros(len(self))
while not np.all(done):
firstsegi = None
for segi in range(len(self)):
# was this already reviewed (when mergin another sp combo)?
if done[segi]==1:
continue
# sets the first segment of this label
# (and the sp combo that will be merged now)
if firstsegi is None:
firstsegi = segi
done[segi] = 1
continue
# ignore segments with labels other than the current one
if self[segi][4]!=self[firstsegi][4]:
continue
# for subsequent segs, see if this can be merged to the previous one
if self[segi][0]<=self[firstsegi][1]:
self[firstsegi][1] = max(self[segi][1], self[firstsegi][1])
done[segi] = 1
# mark this for deleting
todelete.append(segi)
else:
firstsegi = segi
done[segi] = 1
# no need to delete anything
# avoid duplicates in output to make life easier for later deletion
todelete = list(set(todelete))
todelete.sort(reverse=True)
return todelete
def getSummaries(self):
""" Calculates some summary parameters relevant for populating training dialogs.
and returns other parameters for populating the training dialogs.
"""
if len(self)==0:
print("ERROR: no annotations for this calltype found")
return
# get parameter limits for populating training dialogs:
# FreqRange, in Hz
fLow = np.min([seg[2] for seg in self])
fHigh = np.max([seg[3] for seg in self])
# TimeRange, in s
lenMin = np.min([seg[1] - seg[0] for seg in self])
lenMax = np.max([seg[1] - seg[0] for seg in self])
return(lenMin, lenMax, fLow, fHigh)
def exportGT(self, filename, species, resolution):
""" Given the AviaNZ annotations, exports a 0/1 ground truth as a txt file
filename - current wav file name.
species - string, will export the annotations for it.
resolution - resolution at which to dichotomize the timestamps.
set this to match the analysis resolution
(i.e. window, inc in waveletSegment).
E.g. with integer parameters can use
resolution = math.gcd(window, inc)
"""
# number of segments of width window at inc overlap
duration = int(np.ceil(self.metadata["Duration"] / resolution))
eFile = filename[:-4] + '-GT.txt'
# deal with empty files
thisSpSegs = self.getSpecies(species)
# if len(thisSpSegs)==0:
# print("Warning: no annotations for this species found in file", filename)
# # delete the file to avoid problems with old GT files
# try:
# os.remove(eFile)
# except Exception:
# pass
# return
GT = np.tile([0, 0, None], (duration,1))
# fill first column with "time"
GT[:,0] = range(1, duration+1)
GT[:,0] = GT[:,0] * resolution
print("exporting GT with resolution", resolution)
for segix in thisSpSegs:
seg = self[segix]
# start and end in resolution base
s = int(max(0, math.floor(seg[0] / resolution)))
e = int(min(duration, math.ceil(seg[1] / resolution)))
for i in range(s, e):
GT[i,1] = 1
GT[i,2] = species
GT = GT.tolist()
# now save the resulting txt:
with open(eFile, "w") as f:
for l, el in enumerate(GT):
string = '\t'.join(map(str,el))
for item in string:
f.write(item)
f.write('\n')
f.write('\n')
print("output successfully saved to file", eFile)
class Segmenter:
""" This class implements six forms of segmentation for the AviaNZ interface:
Amplitude threshold (rubbish)
Energy threshold
Harma
Median clipping of spectrogram
Fundamental frequency using yin
FIR
It also computes ways to merge them
Important parameters:
mingap: the smallest space between two segments (otherwise merge them)
minlength: the smallest size of a segment (otherwise delete it)
ignoreInsideEnvelope: whether you keep the superset of a set of segments or the individuals when merging
maxlength: the largest size of a segment (currently unused)
threshold: generally this is of the form mean + threshold * std dev and provides a way to filter
And two forms of recognition:
Cross-correlation
DTW
Each returns start and stop times for each segment (in seconds) as a Python list of pairs.
It is up to the caller to convert these to a true SegmentList.
See also the species-specific segmentation in WaveletSegment
"""
def __init__(self, sp=None, fs=0, mingap=0.3, minlength=0.2):
# This is the reference to SignalProc
self.sp = sp
self.fs = fs
# Spectrogram
if hasattr(sp, 'sg'):
self.sg = sp.sg
else:
self.sg = None
# These are the spectrogram params. Needed to compute times.
if sp:
self.data = sp.data
self.fs = sp.sampleRate
self.window_width = sp.window_width
self.incr = sp.incr
self.mingap = mingap
self.minlength = minlength
def setNewData(self, sp):
# To be called when a new sound file is loaded
self.sp = sp
self.data = sp.data
self.fs = sp.sampleRate
self.sg = sp.sg
self.window_width = sp.window_width
self.incr = sp.incr
def bestSegments(self,FIRthr=0.7, medianClipthr=3.0, yinthr=0.9):
""" A reasonably good segmentaion - a merged version of FIR, median clipping, and fundamental frequency using yin
"""
segs1 = self.segmentByFIR(FIRthr)
segs2 = self.medianClip(medianClipthr)
segs3 = self.yinSegs(100, thr=yinthr)
segs1 = self.mergeSegments(segs1, segs2)
segs = self.mergeSegments(segs1, segs3)
# mergeSegments also sorts, so not needed:
# segs = sorted(segs, key=lambda x: x[0])
return segs
def mergeSegments(self, segs1, segs2=None):
""" Given two segmentations of the same file, join them,
and if one wasn't empty, merge any overlapping segments.
format: [[1,3] [2,4] [5,7] [7,8]] -> [[1,4] [5,7] [7,8]]
Can take in one or two lists. """
if segs1 == [] and segs2 == []:
return []
elif segs1 == []:
return segs2
elif segs2 == []:
return segs1
if segs2 is not None:
segs1.extend(segs2)
out = self.checkSegmentOverlap(segs1)
return out
def convert01(self, presabs, window=1):
""" Turns a list of presence/absence [0 1 1 1]
into a list of start-end segments [[1,4]].
Can use non-1 s units of pres/abs.
"""
# squeeze any extra axes except axis 0 (don't make scalars)
presabs = np.reshape(presabs, (np.shape(presabs)[0]))
out = []
t = 0
while t < len(presabs):
if presabs[t]==1:
start = t
while t<len(presabs) and presabs[t]!=0:
t += 1
out.append([start*window, t*window])
t += 1
return out
def deleteShort(self, segments, minlength=0.25):
""" Checks whether segments are long enough.
Operates on start-end list:
[[1,3], [4,5]] -> [[1,3]].
"""
out = []
if minlength == 0:
minlength = self.minlength
for seg in segments:
if seg[1]-seg[0] >= minlength:
out.append(seg)
return out
def deleteShort3(self, segments, minlength=0.25):
""" Checks whether segments are long enough.
Operates on start-end list with probs:
[[[1,3], 50], [[4,5], 90]] -> [[[1,3], 50]].
"""
out = []
if minlength == 0:
minlength = self.minlength
for seg in segments:
if seg[0][1]-seg[0][0] >= minlength:
out.append(seg)
return out
def splitLong3(self, segments, maxlen=10):
"""
Splits long segments (> maxlen) evenly
Operates on list of 3-element segments:
[[[1,5], 50]] -> [[[1,3], 50], [[3,5], 50]]
"""
out = []
for seg in segments:
l = seg[0][1]-seg[0][0]
if l > maxlen:
n = int(np.ceil(l/maxlen))
d = l/n
for i in range(n):
end = min(l, d * (i+1))
out.append([[seg[0][0] + d*i, seg[0][0] + end], seg[1]])
else:
out.append(seg)
return out
## MERGING ALGORITHMS: do the same with very small settings variations
def checkSegmentOverlap(self, segments):
""" Merges overlapping segments.
Does not merge if the segments only touch.
Operates on start-end list [[1,3], [2,4]] -> [[1,4]]
"""
# Needs to be python array, not np array
# Sort by increasing start times
if isinstance(segments, np.ndarray):
segments = segments.tolist()
segments = sorted(segments)
segments = np.array(segments)
# Loop over segs until the start value of next segment
# is not inside the end value of the previous
out = []
i = 0
while i < len(segments):
start = segments[i][0]
end = segments[i][1]
while i+1 < len(segments) and segments[i+1][0]-end < 0:
# there is overlap, so merge
i += 1
end = max(end, segments[i][1])
# no more overlap, so store the current:
out.append([start, end])
i += 1
return out
def joinGaps(self, segments, maxgap=3):
""" Merges segments within maxgap units.
Identical to above, except merges touching segments, and allows a gap.
Operates on start-end list [[1,2], [3,4]] -> [[1,4]]
"""
if isinstance(segments, np.ndarray):
segments = segments.tolist()
if len(segments)==0:
return []
segments.sort(key=lambda seg: seg[0])
out = []
i = 0
while i < len(segments):
start = segments[i][0]
end = segments[i][1]
while i+1 < len(segments) and segments[i+1][0]-end <= maxgap:
i += 1
end = max(end, segments[i][1])
out.append([start, end])
i += 1
return out
def checkSegmentOverlap3(self, segments):
""" Merges overlapping segments.
Does not merge if the segments only touch.
Identical to above, but operates on list of 3-element segments:
[[[1,3], 50], [[2,5], 70]] -> [[[1,5], 60]]
Currently, certainties are just averaged over the number of segs.
"""
if isinstance(segments, np.ndarray):
segments = segments.tolist()
if len(segments)==0:
return []
segments.sort(key=lambda seg: seg[0][0])
# sorted() appears to work fine as well
out = []
i = 0
while i < len(segments):
start = segments[i][0][0]
end = segments[i][0][1]
cert = [segments[i][1]]
while i+1 < len(segments) and segments[i+1][0][0]-end < 0:
i += 1
end = max(end, segments[i][0][1])
cert.append(segments[i][1])
out.append([[start, end], np.mean(cert)])
i += 1
return out
def joinGaps3(self, segments, maxgap=3):
""" Merges segments within maxgap units.
Operates on list of 3-element segments:
[[[1,2], 50], [[3,5], 70]] -> [[[1,5], 60]]
Identical to above, except merges touching segments, and allows a gap.
Currently, certainties are just averaged over the number of segs.
"""
if isinstance(segments, np.ndarray):
segments = segments.tolist()
if len(segments)==0:
return []
segments.sort(key=lambda seg: seg[0][0])
out = []
i = 0
while i < len(segments):
start = segments[i][0][0]
end = segments[i][0][1]
cert = [segments[i][1]]
while i+1 < len(segments) and segments[i+1][0][0]-end <= maxgap:
i += 1
end = max(end, segments[i][0][1])
cert.append(segments[i][1])
out.append([[start, end], np.mean(cert)])
i += 1
return out
def segmentByFIR(self, threshold):
""" Segmentation using FIR envelope.
"""
nsecs = len(self.data) / float(self.fs)
fftrate = int(np.shape(self.sg)[0]) / nsecs
upperlimit = 100
FIR = [0.078573000000000004, 0.053921000000000004, 0.041607999999999999, 0.036006000000000003, 0.031521,
0.029435000000000003, 0.028122000000000001, 0.027286999999999999, 0.026241000000000004,
0.025225999999999998, 0.024076, 0.022926999999999999, 0.021703999999999998, 0.020487000000000002,
0.019721000000000002, 0.019015000000000001, 0.018563999999999997, 0.017953, 0.01753,
0.017077000000000002, 0.016544, 0.015762000000000002, 0.015056, 0.014456999999999999, 0.013913,
0.013299, 0.012879, 0.012568000000000001, 0.012454999999999999, 0.012056000000000001, 0.011634,
0.011077, 0.010707, 0.010217, 0.0098840000000000004, 0.0095959999999999986, 0.0093607000000000013,
0.0090197999999999997, 0.0086908999999999997, 0.0083841000000000002, 0.0081481999999999995,
0.0079185000000000002, 0.0076363000000000004, 0.0073406000000000009, 0.0070686999999999998,
0.0068438999999999991, 0.0065873000000000008, 0.0063688999999999994, 0.0061700000000000001,
0.0059743000000000001, 0.0057561999999999995, 0.0055351000000000003, 0.0053633999999999991,
0.0051801, 0.0049743000000000001, 0.0047431000000000001, 0.0045648999999999993,
0.0043972000000000004, 0.0042459999999999998, 0.0041016000000000004, 0.0039503000000000003,
0.0038013000000000005, 0.0036351, 0.0034856000000000002, 0.0033270999999999999,
0.0032066999999999998, 0.0030569999999999998, 0.0029206999999999996, 0.0027760000000000003,
0.0026561999999999996, 0.0025301999999999998, 0.0024185000000000001, 0.0022967,
0.0021860999999999998, 0.0020696999999999998, 0.0019551999999999998, 0.0018563,
0.0017562000000000001, 0.0016605000000000001, 0.0015522000000000001, 0.0014482999999999998,
0.0013492000000000001, 0.0012600000000000001, 0.0011788, 0.0010909000000000001, 0.0010049,
0.00091527999999999998, 0.00082061999999999999, 0.00074465000000000002, 0.00067159000000000001,
0.00060258999999999996, 0.00053370999999999996, 0.00046135000000000002, 0.00039071,
0.00032736000000000001, 0.00026183000000000001, 0.00018987999999999999, 0.00011976000000000001,
6.0781000000000006e-05, 0.0]
f = interp1d(np.arange(0, len(FIR)), np.squeeze(FIR))
samples = f(np.arange(1, upperlimit, float(upperlimit) / int(fftrate / 10.)))
padded = np.concatenate((np.zeros(int(fftrate / 10.)), np.mean(self.sg, axis=1), np.zeros(int(fftrate / 10.))))
envelope = spi.filters.convolve(padded, samples, mode='constant')[:-int(fftrate / 10.)]
ind = envelope > np.median(envelope) + threshold * np.std(envelope)
segs = self.convert01(ind, self.incr / self.fs)
return segs
def segmentByAmplitude(self, threshold, usePercent=True):
""" Bog standard amplitude segmentation.
A straw man, do not use.
"""
if usePercent:
threshold = threshold*np.max(self.data)
seg = np.abs(self.data)>threshold
seg = self.convert01(seg, self.fs)
return seg
def segmentByEnergy(self, thr, width, min_width=450):
""" Based on description in Jinnai et al. 2012 paper in Acoustics
Computes the 'energy curve' as windowed sum of absolute values of amplitude
I median filter it, 'cos it's very noisy
And then threshold it (no info on their threshold) and find max in each bit above threshold
I also check for width of those (they don't say anything)
They then return the max-width:max+width segments for each max
"""
data = np.abs(self.data)
E = np.zeros(len(data))
E[width] = np.sum(data[:2*width+1])
for i in range(width+1,len(data)-width):
E[i] = E[i-1] - data[i-width-1] + data[i+width]
E = E/(2*width)
# TODO: Automatic energy gain (normalisation method)
# This thing is noisy, so I'm going to median filter it. SoundID doesn't seem to?
Em = np.zeros(len(data))
for i in range(width,len(data)-width):
Em[i] = np.median(E[i-width:i+width])
for i in range(width):
Em[i] = np.median(E[0:2*i])
Em[-i] = np.median(E[-2 * i:])
# TODO: Better way to do this?
threshold = np.mean(Em) + thr*np.std(Em)
# Pick out the regions above threshold and the argmax of each, assuming they are wide enough
starts = []
ends = []
insegment = False
for i in range(len(data)-1):
if not insegment:
if Em[i]<threshold and Em[i+1]>threshold:
starts.append(i)
insegment = True
if insegment:
if Em[i]>threshold and Em[i+1]<threshold:
ends.append(i)
insegment = False
if insegment:
ends.append(len(data))
maxpoints = []
Emm = np.zeros(len(data))
for i in range(len(starts)):
if ends[i] - starts[i] > min_width:
maxpoints.append(np.argmax(Em[starts[i]:ends[i]]))
Emm[starts[i]:ends[i]] = Em[starts[i]:ends[i]]
# TODO: SoundID appears to now compute the 44 LPC coeffs for each [midpoint-width:midpoint+width]
# TODO: And then compute the geometric distance to templates
segs = []
for i in range(len(starts)):
segs.append([float(starts[i])/self.fs,float(ends[i])/self.fs])
return segs
def Harma(self, thr=10., stop_thr=0.8, minSegment=50):
""" Harma's method, but with a different stopping criterion
# Assumes that spectrogram is not normalised
maxFreqs = 10. * np.log10(np.max(self.sg, axis = 1))
"""
maxFreqs = 10. * np.log10(np.max(self.sg, axis=1))
maxFreqs = medfilt(maxFreqs,21)
biggest = np.max(maxFreqs)
segs = []
while np.max(maxFreqs)>stop_thr*biggest:
t0 = np.argmax(maxFreqs)
a_n = maxFreqs[t0]
# Go backwards looking for where the syllable stops
t = t0
while maxFreqs[t] > a_n - thr and t>0:
t -= 1
t_start = t
# And forwards
t = t0
while maxFreqs[t] > a_n - thr and t<len(maxFreqs)-1:
t += 1
t_end = t
# Set the syllable just found to 0
maxFreqs[t_start:t_end] = 0
if float(t_end - t_start)*self.incr/self.fs*1000.0 > minSegment:
segs.append([float(t_start)* self.incr / self.fs,float(t_end)* self.incr / self.fs])
return segs
def segmentByPower(self, thr=1.):
""" Segmentation simply on the power
"""
maxFreqs = 10. * np.log10(np.max(self.sg, axis=1))
maxFreqs = medfilt(maxFreqs, 21)
ind = maxFreqs > (np.mean(maxFreqs)+thr*np.std(maxFreqs))
segs = self.convert01(ind, self.incr / self.fs)
return segs
def medianClip(self, thr=3.0, medfiltersize=5, minaxislength=5, minSegment=70):
""" Median clipping for segmentation
Based on Lasseck's method
minaxislength - min "length of the minor axis of the ellipse that has the same normalized second central moments as the region", based on skm.
minSegment - min number of pixels exceeding thr to declare an area as segment.
This version only clips in time, ignoring frequency
And it opens up the segments to be maximal (so assumes no overlap).
The multitaper spectrogram helps a lot
"""
tt = time.time()
sg = self.sg/np.max(self.sg)
# This next line gives an exact match to Lasseck, but screws up bitterns!
#sg = sg[4:232, :]
rowmedians = np.median(sg, axis=1)
colmedians = np.median(sg, axis=0)
clipped = np.zeros(np.shape(sg),dtype=int)
for i in range(np.shape(sg)[0]):
for j in range(np.shape(sg)[1]):
if (sg[i, j] > thr * rowmedians[i]) and (sg[i, j] > thr * colmedians[j]):
clipped[i, j] = 1
print("Found", np.sum(clipped), "pixels")
# This is the stencil for the closing and dilation. It's a 5x5 diamond. Can also use a 3x3 diamond
diamond = np.zeros((5,5),dtype=int)
diamond[2,:] = 1
diamond[:,2] = 1
diamond[1,1] = diamond[1,3] = diamond[3,1] = diamond[3,3] = 1
#diamond[2, 1:4] = 1
#diamond[1:4, 2] = 1
clipped = spi.binary_closing(clipped,structure=diamond).astype(int)
clipped = spi.binary_dilation(clipped,structure=diamond).astype(int)
clipped = spi.median_filter(clipped,size=medfiltersize)
clipped = spi.binary_fill_holes(clipped)