forked from smarsland/AviaNZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareCalls.py
1103 lines (959 loc) · 42.3 KB
/
CompareCalls.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
# Self-standing executable for tall comparison mode
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QLabel,
QFileDialog,
QPushButton,
QPlainTextEdit,
QWidget,
QGridLayout,
QDoubleSpinBox,
QGroupBox,
QSizePolicy,
QSpacerItem,
QLayout,
QProgressDialog,
QMessageBox,
QVBoxLayout,
QHBoxLayout,
QStyle,
QComboBox,
QDialog,
QToolButton,
QCheckBox,
)
from PyQt5.QtCore import QDir, Qt, QSize
from PyQt5.QtGui import QIcon, QPixmap
import sys
import os
import datetime as dt
import numpy as np
import pyqtgraph as pg
import SupportClasses_GUI
import Segment
import SignalProc
import colourMaps
class CompareCalls(QMainWindow):
def __init__(self):
super(CompareCalls, self).__init__()
print("Starting...")
self.setWindowTitle("AviaNZ call comparator")
self.setWindowIcon(QIcon("img/Avianz.ico"))
self.setWindowFlags(
(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
| Qt.WindowMaximizeButtonHint
| Qt.WindowCloseButtonHint
)
# menu bar
fileMenu = self.menuBar() # .addMenu("&File")
fileMenu.addAction(
"About", lambda: SupportClasses_GUI.MessagePopup("a", "About", ".").exec_()
)
fileMenu.addAction("Quit", QApplication.quit)
# do we need this?
# if platform.system() == 'Darwin':
# helpMenu.addAction("About",self.showAbout,"Ctrl+A")
# main dock setup
area = QWidget()
mainbox = QVBoxLayout()
area.setLayout(mainbox)
self.setCentralWidget(area)
## Data dir selection
self.dirName = ""
label = QLabel(
"Select input folder containing files named in one of the following formats:"
)
formatlabel = QLabel(
"recordername_YYMMDD_hhmmss.wav\n or \nrecordername_YYYYMMDD_hhmmss.wav"
)
formatlabel.setAlignment(Qt.AlignCenter)
label.setStyleSheet("QLabel {color: #303030}")
formatlabel.setStyleSheet("QLabel {color: #303030}")
self.w_browse = QPushButton(" Browse Folder")
self.w_browse.setIcon(self.style().standardIcon(QStyle.SP_DialogOpenButton))
self.w_browse.setStyleSheet(
"QPushButton {background-color: #c4ccd3; font-weight: bold; font-size:14px; padding: 3px 3px 3px 3px}"
)
self.w_browse.setMinimumSize(170, 40)
self.w_browse.setSizePolicy(QSizePolicy(1, 1))
self.w_browse.clicked.connect(self.browse)
# area showing the selected folder
self.w_dir = QPlainTextEdit()
self.w_dir.setReadOnly(True)
self.w_dir.setFixedHeight(40)
self.w_dir.setPlainText("")
self.w_dir.setSizePolicy(QSizePolicy(3, 1))
# feedback label
self.labelDatasIcon = QLabel()
self.labelDatas = QLabel()
## Clock shift adjustment
labelClockSp = QLabel("Select species to analyse:")
self.clockSpBox = QComboBox()
labelHop = QLabel("Clock shift resolution (s)")
self.hopSpin = QDoubleSpinBox()
self.hopSpin.setSingleStep(0.1)
self.hopSpin.setDecimals(3)
self.hopSpin.setValue(1.0)
self.suggestAdjBtn = QPushButton("Auto-suggest adjustment")
self.suggestAdjBtn.setStyleSheet(
"QPushButton {background-color: #c4ccd3; font-weight: bold; font-size:14px; padding: 3px 3px 3px 3px}"
)
self.suggestAdjBtn.clicked.connect(self.suggestAdjustment)
self.suggestAdjBtn.setEnabled(False)
self.labelAdjustIcon = QLabel()
self.labelAdjust = QLabel()
self.reviewAdjBtn = QPushButton("Review suggested adjustments")
self.reviewAdjBtn.setStyleSheet(
"QPushButton {background-color: #c4ccd3; font-weight: bold; font-size:14px; padding: 3px 3px 3px 3px}"
)
self.reviewAdjBtn.clicked.connect(self.showAdjustmentsDialog)
self.reviewAdjBtn.setEnabled(False)
# call review
self.compareBtn = QPushButton("Compare calls")
self.compareBtn.setStyleSheet(
"QPushButton {background-color: #c4ccd3; font-weight: bold; font-size:14px; padding: 3px 3px 3px 3px}"
)
self.compareBtn.clicked.connect(self.showComparisonDialog)
self.compareBtn.setEnabled(False)
self.compareRecSelector = QComboBox()
self.compareRecSelector.currentTextChanged.connect(self.updateShiftSpinbox)
self.compareShiftSpinbox = QDoubleSpinBox()
self.compareShiftSpinbox.setSingleStep(1.0)
self.compareShiftSpinbox.setRange(-300, 300)
self.adjustmentsOut = QPlainTextEdit()
self.adjustmentsOut.setReadOnly(True)
self.componentsOut = QPlainTextEdit()
self.componentsOut.setReadOnly(True)
### Layouts for each box
# input dir
inputGroup = QGroupBox("Input")
inputGrid = QGridLayout()
inputGroup.setLayout(inputGrid)
inputGroup.setStyleSheet("QGroupBox:title{color: #505050; font-weight: 50}")
inputGrid.addWidget(label, 0, 0, 1, 4)
inputGrid.addWidget(formatlabel, 1, 0, 1, 4)
inputGrid.addWidget(self.w_dir, 2, 0, 1, 3)
inputGrid.addWidget(self.w_browse, 2, 3, 1, 1)
hboxLabel = QHBoxLayout()
hboxLabel.addWidget(self.labelDatasIcon)
hboxLabel.addWidget(self.labelDatas)
hboxLabel.addStretch(10)
inputGrid.addLayout(hboxLabel, 3, 0, 1, 4)
# clock adjustment
clockadjGroup = QGroupBox("Clock adjustment")
clockadjGrid = QGridLayout()
clockadjGroup.setLayout(clockadjGrid)
clockadjGroup.setStyleSheet("QGroupBox:title{color: #505050; font-weight: 50}")
clockadjGrid.addWidget(labelClockSp, 0, 0, 1, 1)
clockadjGrid.addWidget(self.clockSpBox, 0, 1, 1, 3)
clockadjGrid.addWidget(labelHop, 1, 0, 1, 1)
clockadjGrid.addWidget(self.hopSpin, 1, 1, 1, 3)
clockadjGrid.addWidget(self.suggestAdjBtn, 2, 0, 1, 4)
hboxLabel2 = QHBoxLayout()
hboxLabel2.addWidget(self.labelAdjustIcon)
hboxLabel2.addWidget(self.labelAdjust)
hboxLabel2.addStretch(10)
clockadjGrid.addLayout(hboxLabel2, 3, 0, 1, 4)
clockadjGrid.addWidget(self.reviewAdjBtn, 4, 0, 1, 4)
# call comparator
comparisonGroup = QGroupBox("Call comparison")
comparisonGrid = QGridLayout()
comparisonGroup.setLayout(comparisonGrid)
comparisonGroup.setStyleSheet(
"QGroupBox:title{color: #505050; font-weight: 50}"
)
comparisonGrid.addWidget(QLabel("Compare recorders:"), 0, 0)
comparisonGrid.addWidget(self.compareRecSelector, 0, 2)
comparisonGrid.addWidget(QLabel("with shift:"), 0, 3)
comparisonGrid.addWidget(self.compareShiftSpinbox, 0, 4)
comparisonGrid.addWidget(self.compareBtn, 1, 0, 1, 4)
# output save btn and settings
outputGroup = QGroupBox("Output")
outputGrid = QGridLayout()
outputGroup.setLayout(outputGrid)
outputGroup.setStyleSheet("QGroupBox:title{color: #505050; font-weight: 50}")
outputGrid.addWidget(self.adjustmentsOut, 0, 0, 1, 4)
outputGrid.addWidget(self.componentsOut, 1, 0, 1, 4)
### Main Layout
mainbox.addWidget(inputGroup)
mainbox.addWidget(clockadjGroup)
mainbox.addWidget(comparisonGroup)
mainbox.addWidget(outputGroup)
def browse(self):
if self.dirName:
self.dirName = QFileDialog.getExistingDirectory(
self, "Choose Folder to Process", str(self.dirName)
)
else:
self.dirName = QFileDialog.getExistingDirectory(
self, "Choose Folder to Process"
)
# Reset interface so that it is empty/disabled in case an error is encountered
self.w_dir.setPlainText("")
self.w_dir.setReadOnly(True)
self.labelDatas.setText("")
pm = (
self.style()
.standardIcon(QStyle.SP_DialogCancelButton)
.pixmap(QSize(12, 12))
)
self.labelDatasIcon.setPixmap(pm)
self.suggestAdjBtn.setEnabled(False)
self.reviewAdjBtn.setEnabled(False)
self.compareBtn.setEnabled(False)
self.clockSpBox.clear()
self.compareRecSelector.clear()
self.annots = []
self.allrecs = set()
# this will load the folder and populate annots
with pg.BusyCursor():
succ = self.checkInputDir()
self.allrecs = list(self.allrecs)
if succ > 0:
return
self.w_dir.setPlainText(self.dirName)
self.w_dir.setReadOnly(True)
# Check if any DATA files were read, enable GUI for starting analysis
if len(self.annots) == 0:
# can't do clock adjustment, nor call comparison
self.labelDatas.setText("No DATA files detected")
self.labelDatas.setStyleSheet("QLabel {color: #ff2020}")
print("ERROR: no WAV or DATA files found")
return 1
else:
self.labelDatas.setText(
"Read %d data files from %d recorders"
% (len(self.annots), len(self.allrecs))
)
self.labelDatas.setStyleSheet("QLabel {color: #000000}")
pm = (
self.style()
.standardIcon(QStyle.SP_DialogApplyButton)
.pixmap(QSize(12, 12))
)
self.labelDatasIcon.setPixmap(pm)
print(
"Read %d data files from %d recorders"
% (len(self.annots), len(self.allrecs))
)
# allow analysis to be started
spList = []
for sl in self.annots:
if len(sl) > 0:
filesp = [lab["species"] for seg in sl for lab in seg[4]]
spList.extend(filesp)
spList = list(set(spList))
self.clockSpBox.addItems(spList)
for i in range(len(self.allrecs)):
for j in range(i + 1, len(self.allrecs)):
pair = self.allrecs[i] + "-" + self.allrecs[j]
self.compareRecSelector.addItem(pair)
self.pairwiseShifts = np.zeros((len(self.allrecs), len(self.allrecs)))
self.suggestAdjBtn.setEnabled(True)
self.compareBtn.setEnabled(True)
def checkInputDir(self):
"""Checks the input file dir filenames etc. for validity
Returns an error code if the specified directory is bad.
"""
if not os.path.isdir(self.dirName):
print("ERROR: directory %s doesn't exist" % self.dirName)
return 1
# list all datas that will be processed
alldatas = []
try:
for root, dirs, files in os.walk(str(self.dirName)):
for filename in files:
if filename.lower().endswith(".wav.data"):
alldatas.append(os.path.join(root, filename))
except Exception as e:
print("ERROR: could not load dir %s" % self.dirName)
print(e)
return 1
# TODO Note: this is a bit fidgety as needs to be adapted to each survey
recsInDirNames = False
defaultToDMY = True
# read in all datas to self.annots
for f in alldatas:
# must have correct naming format:
infilestem = os.path.basename(f)[:-9]
try:
if recsInDirNames:
recname = os.path.basename(os.path.normpath(os.path.dirname(f)))
filedate, filetime = infilestem.split("_") # get [date, time]
else:
recname, filedate, filetime = infilestem.split(
"_"
) # get [rec, date, time]
datestamp = filedate + "_" + filetime # make "date_time"
# check both 4-digit and 2-digit codes (century that produces closest year to now is inferred)
if len(filedate) == 8:
d = dt.datetime.strptime(datestamp, "%Y%m%d_%H%M%S")
else:
if defaultToDMY:
try:
d = dt.datetime.strptime(datestamp, "%d%m%y_%H%M%S")
except ValueError:
d = dt.datetime.strptime(datestamp, "%y%m%d_%H%M%S")
else:
try:
d = dt.datetime.strptime(datestamp, "%y%m%d_%H%M%S")
except ValueError:
d = dt.datetime.strptime(datestamp, "%d%m%y_%H%M%S")
print("Recorder ", recname, " timestamp ", d)
# timestamp identified, so read this file:
segs = Segment.SegmentList()
try:
segs.parseJSON(f, silent=True)
except Exception as e:
print("Warning: could not read file %s" % f)
print(e)
continue
# store the wav filename
segs.wavname = f[:-5]
segs.recname = recname
segs.datetime = d
self.annots.append(segs)
# also keep track of the different recorders
self.allrecs.add(recname)
except ValueError:
print("Could not identify timestamp in", f)
continue
print("Detected recorders:", self.allrecs)
return 0
def updateShiftSpinbox(self, text):
"""Updates the shift selection spinbox, when the selected
recorder pair or estimated shift change.
"""
# find the currently suggested shift for rec2 w.r.t rec1
rec1, rec2 = text.split("-")
i = self.allrecs.index(rec1)
j = self.allrecs.index(rec2)
rec2shift = float(self.pairwiseShifts[i, j])
self.compareShiftSpinbox.setValue(rec2shift)
def calculateOverlap(self, dtl1, dtl2, shift=0):
"""Calculates total overlap between two lists of pairs of datetime obj:
[(s1, e1), (s2, e2)] + [(s3, e3), (s4, e4)] -> total overl. in s
Shift: shifts dt1 by this many seconds (+ for lead, - for lag)
Assumes that each tuple is in correct order (start, end).
"""
overl = dt.timedelta()
for dt1 in dtl1:
st1 = dt1[0] + shift
end1 = dt1[1] + shift
for dt2 in dtl2:
laststart = max(st1, dt2[0])
firstend = min(end1, dt2[1])
if firstend > laststart:
overl = firstend - laststart + overl
# convert from datetime timedelta to seconds
overl = overl.total_seconds()
return overl
def suggestAdjustment(self):
"""Goes over all possible pairs of recorders and suggests best adjustment
(based on max annotation overlap) for them.
"""
print("Starting clock adjustment")
species = self.clockSpBox.currentText()
# do shifts in this resolution (seconds)
hop = self.hopSpin.value()
# Disable GUI in case of errors
self.labelAdjust.setText("Clock adjustment not done")
pm = (
self.style()
.standardIcon(QStyle.SP_DialogCancelButton)
.pixmap(QSize(12, 12))
)
self.labelAdjustIcon.setPixmap(pm)
self.reviewAdjBtn.setEnabled(False)
# generate all recorder pairs
if len(self.allrecs) > 30:
print("ERROR: using more than 30 recorders disabled for safety")
return 1
with pg.BusyCursor():
# gather all annotations for each rec and this species
# and convert them into: [(datetime_start, datetime_end), (ds, de), ...] for each recorder
print("Collecting annotations for species %s" % species)
speciesAnnots = []
for i in range(len(self.allrecs)):
rec1 = self.allrecs[i]
thisRecAnnots = []
for sl in self.annots:
if sl.recname != rec1:
continue
# indices of segments in this file that contain the right species in at least one label:
six = sl.getSpecies(species)
for ix in six:
# convert in-file annot times into absolute time (in datetime obj)
absstart = sl.datetime + dt.timedelta(seconds=sl[ix][0])
absend = sl.datetime + dt.timedelta(seconds=sl[ix][1])
# segments from old versions can still be reversed
if absstart > absend:
absstart, absend = absend, absstart
thisRecAnnots.append((absstart, absend))
print("Found %d annotations" % len(thisRecAnnots))
if len(thisRecAnnots) > 150:
print(
"ERROR: using more than 150 annotations per recorder disabled for safety"
)
return 1
speciesAnnots.append(thisRecAnnots)
# matrix of pairwise connectivity between recorders
self.recConnections = np.ones((len(self.allrecs), len(self.allrecs)))
# determine 1 best time shift for each pair of recorders
self.pairwiseShifts = np.zeros((len(self.allrecs), len(self.allrecs)))
self.overlaps_forplot = []
for i in range(len(self.allrecs)):
rec1 = self.allrecs[i]
rec1annots = speciesAnnots[i]
print("Working on recorder", rec1)
print("Found %d annotations" % len(rec1annots))
# find best adjustment for rec1
for j in range(i + 1, len(self.allrecs)):
rec2 = self.allrecs[j]
rec2annots = speciesAnnots[j]
print("Comparing with recorder", rec2)
overlap = np.zeros(601)
for shifti in range(-300, 301):
shift = dt.timedelta(seconds=shifti * hop)
overlap[shifti + 300] = self.calculateOverlap(
rec1annots, rec2annots, shift
)
bestOverlap = np.max(overlap)
bestShift = (np.argmax(overlap) - 300) * hop
self.pairwiseShifts[i, j] = bestShift
self.pairwiseShifts[j, i] = -bestShift
print(
"Calculated best overlap: %.1f seconds at deltat = %.2f"
% (bestOverlap, bestShift)
)
# The full series of overlaps for each delta is needed for review, so store
self.overlaps_forplot.append(
{
"series": overlap,
"bestOverlap": bestOverlap,
"bestShift": bestShift,
"recorders": (rec1, rec2),
}
)
# At this point, we have a matrix of best pairwise adjustments (in s)
# Extract connected components (subgraphs, each is a list of rec indices):
# will also determine the total shift for each recorder relative to component start
# (=sum of edge weights from pairwiseShifts adj. matrix)
# The shifts take into account user-set connectedness b/c shifts are added when traversing over recConnections.
self.cclist = self.connectedComponents()
print("Final global shifts:", self.allrecs, self.cclist)
print("Clock adjustment complete")
self.labelAdjust.setText("Clock adjustment complete")
pm = (
self.style().standardIcon(QStyle.SP_DialogApplyButton).pixmap(QSize(12, 12))
)
self.labelAdjustIcon.setPixmap(pm)
self.reviewAdjBtn.setEnabled(True)
self.refreshMainOut()
def showAdjustmentsDialog(self):
self.adjrevdialog = ReviewAdjustments(
self.overlaps_forplot, self.hopSpin.value(), self
)
self.adjrevdialog.exec_()
self.cclist = self.connectedComponents()
# update GUI after the dialog closes
self.refreshMainOut()
def showComparisonDialog(self):
print("Extracting overlapping calls...")
# self.allrecs and annots were set after browsing the input dir,
# so here we just select the right annotations from them and pass to the dialog
rec1, rec2 = self.compareRecSelector.currentText().split("-")
annots1 = [sl for sl in self.annots if sl.recname == rec1]
annots2 = [sl for sl in self.annots if sl.recname == rec2]
# find the currently suggested shift for rec2 w.r.t rec1
# This is the estimated shift:
# i = self.allrecs.index(rec1)
# j = self.allrecs.index(rec2)
# rec2shift = self.pairwiseShifts[i, j]
# This is the manually adjusted shift:
rec2shift = self.compareShiftSpinbox.value()
# find the matching annotations from that pair of Segment Lists
# ideally need the one with biggest overlap to the other
# Or just: show an annotation if there is at least some overlap with another
# TODO in the dialog maybe?
annotspaired1 = []
annotspaired2 = []
for sl in annots1:
slpaired1 = []
slpaired2 = []
# extract the right sl from annots2 (matching timestamp)
# The initial check is quick & dirty & might miss some annotation pairs:
# as we're only looking for segment matches in the first overlapping file from annots2
# - extreme case: file 00:00-15:00 cam have sl2match file -14:59-00:01
# and then very few segment-level matches will be found.
sl2match = None
filelen1 = dt.timedelta(seconds=sl.metadata["Duration"])
for sl2 in annots2:
filelen2 = dt.timedelta(seconds=sl2.metadata["Duration"])
if sl2.datetime - filelen1 < sl.datetime < sl2.datetime + filelen2:
print(
"--- potential data file overlap detected:",
rec1,
sl.datetime,
rec2,
sl2.datetime,
)
# Check more carefully if the overlap is not very small, and has segments:
overlSize = min(
sl.datetime + filelen1, sl2.datetime + filelen2
) - max(sl.datetime, sl2.datetime)
if overlSize > 0.2 * filelen1 and len(sl2) > 0:
print("overlap confirmed")
sl2match = sl2
break
if sl2match is None:
print("Warning: no match for file %s found" % sl.wavname)
continue
# difference in start times of the two files
# (needed b/c subsequent analysis uses relative segment times)
rec2startdiff = (sl.datetime - sl2match.datetime).total_seconds()
# identify the matching segment from sl2
for seg in sl:
# find matching pair at segment level,
# after adjusting for the proposed recorder shift
# and any recorder start timestamp difference:
recdelay = rec2startdiff + rec2shift
seg2 = self.findMatchIn(sl2match, seg, recdelay)
# store the pair in a temp list (including filenames)
if len(seg2) > 0:
seg.recname = rec1
seg2.recname = rec2
seg.wavname = sl.wavname
seg2.wavname = sl2match.wavname
print("Storing SEG1", seg)
slpaired1.append(seg)
# Use this if you want to show THE BEST MATCHING SEGMENT
# in the comparison dialog
# slpaired2.append(seg2)
# or this if you want to show THE SAME TIME PERIODS
# after adjustment
print("SEG2 before adj", seg2)
seg2[0] = seg[0] + recdelay
seg2[1] = seg[1] + recdelay
print("Storing SEG2", seg2)
slpaired2.append(seg2)
# add this set of segments to the main long list
if len(slpaired1) > 0:
annotspaired1.extend(slpaired1)
annotspaired2.extend(slpaired2)
if len(annotspaired1) == 0:
print("Warning: no overlapping annotations found!")
return
print("Reviewing calls...")
self.comparedialog = CompareCallsDialog(annotspaired1, annotspaired2, self)
self.comparedialog.exec_()
print("Call comparison complete")
def findMatchIn(self, seglist, seg, tshift):
"""Returns one segment from seglist that has the most overlap with seg.
tshift will be added to the segment timestamp temporarily:
this is used to adjusted for things like file timestamp
differences, without affecting the stored segment.
"""
# print("--- trying to match segment", seg, "(delayed by ", tshift, "s) in list", [(s[0], s[1]) for s in seglist])
overlaps = [
min(seg[1] + tshift, seg2[1]) - max(seg[0] + tshift, seg2[0])
for seg2 in seglist
]
mpos = np.argmax(overlaps)
if overlaps[mpos] <= 0:
print("No match found for segment", seg)
return []
m = seglist[mpos]
m.overlap = overlaps[mpos]
return m
def refreshMainOut(self):
"""Collect information from self about shifts and connectivity
and print out nicely."""
adjstr = []
for i in range(len(self.allrecs)):
for j in range(i + 1, len(self.allrecs)):
pairtext = "Shift %s w.r.t. %s: %.2f" % (
self.allrecs[i],
self.allrecs[j],
self.pairwiseShifts[i, j],
)
if self.recConnections[i, j] == 1:
adjstr.append(pairtext)
else:
adjstr.append(pairtext + " (not connected)")
self.adjustmentsOut.setPlainText("\n".join(adjstr))
self.adjustmentsOut.setReadOnly(True)
# cclist is a list of vertex ids
ccstr = []
for comp in self.cclist:
recnames = []
shifts = []
for v in comp:
recnames.append(self.allrecs[v[0]])
shifts.append(v[1])
recnames = "-".join(recnames) # ZA-ZB-ZC
shifts = " ".join(map(str, shifts)) # 1 -2 4
ccstr.append(recnames + "\t|\t" + shifts)
ccstr = ";\n".join(ccstr) # ZA-ZB-ZC | 1 -2 4; ZD-ZE | 5 -10
self.componentsOut.setPlainText(ccstr)
self.componentsOut.setReadOnly(True)
self.updateShiftSpinbox(self.compareRecSelector.currentText())
# depth-first search for finding a connected subgraph
# and relative shift for each recorder within this subgraph
# Args:
# 1. list where to store found nodes
# 2. length of temp at time of calling - used to identify prev node
# 3. starting node index
# 4. Bool list of visited nodes
# Return: list of tuples [(rec number, shift)]
def DFS(self, temp, templen, v, visited):
# Mark the current vertex as visited
visited[v] = True
# Add this vertex and its shift to the current component
if len(temp) > 0:
prevv = temp[templen - 1][0]
shift = temp[templen - 1][1] + self.pairwiseShifts[v, prevv]
print("total shift %.2f at %d / %s" % (shift, v, self.allrecs[v]))
else:
shift = 0
temp.append((v, shift))
templen = len(temp)
# Run DFS for all vertices adjacent to this
listOfNeighbours = np.nonzero(self.recConnections[v,])[0]
print("Neighbours are", [self.allrecs[nb] for nb in listOfNeighbours])
for i in listOfNeighbours:
if not visited[i]:
temp = self.DFS(temp, templen, i, visited)
return temp
# Retrieve connected components from an undirected graph
# (and calculate within-component shifts as we are traversing the graph anyway)
def connectedComponents(self):
visited = [False] * len(self.allrecs)
# symmetrize the adjacency matrix (which was upper triang so far)
uptri = np.triu_indices_from(self.recConnections, k=1)
self.recConnections[(uptri[1], uptri[0])] = self.recConnections[uptri]
print("Using this adjacency matrix:", self.recConnections)
componentList = []
for v in range(len(visited)):
if not visited[v]:
component = []
componentList.append(self.DFS(component, 0, v, visited))
# Additionally, total sum of absolute adjustments could be minimized if we de-median the shifts
# just need some testing before doing this
# component[,1] = component[,1] - np.median(component[,1])
print("Found the following components and shifts:")
print(componentList)
return componentList
class ReviewAdjustments(QDialog):
def __init__(self, adj, hop, parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle("Check Clock Adjustments")
self.setWindowIcon(QIcon("img/Avianz.ico"))
self.setWindowFlags(
(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
| Qt.WindowMaximizeButtonHint
| Qt.WindowCloseButtonHint
)
self.parent = parent
# list of dicts for each pair: {overlaps for each t, best overlap, best shift, (rec pair names)}
self.shifts = adj
self.xs = np.arange(-300 * hop, 301 * hop, hop)
# top section
self.labelCurrPage = QLabel("Page %s of %s" % (1, len(self.shifts)))
self.labelRecs = QLabel()
# middle section
# The buttons to move through the overview
self.leftBtn = QToolButton()
self.leftBtn.setArrowType(Qt.LeftArrow)
self.leftBtn.clicked.connect(self.moveLeft)
self.leftBtn.setToolTip("View previous pair")
self.rightBtn = QToolButton()
self.rightBtn.setArrowType(Qt.RightArrow)
self.rightBtn.clicked.connect(self.moveRight)
self.rightBtn.setToolTip("View next pair")
self.leftBtn.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.MinimumExpanding)
self.rightBtn.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.MinimumExpanding)
self.connectCheckbox = QCheckBox("Recorders are connected")
self.connectCheckbox.setChecked(bool(self.parent.recConnections[0, 1] == 1))
self.connectCheckbox.toggled.connect(self.connectToggled)
# main graphic
mainpl = pg.PlotWidget()
self.p1 = mainpl.plot()
self.p1.setPen(pg.mkPen("k", width=1))
col = pg.mkColor(20, 255, 20, 150)
self.bestLineO = pg.InfiniteLine(
angle=0, movable=False, pen={"color": col, "width": 2}
)
self.bestLineSh = pg.InfiniteLine(
angle=90, movable=False, pen={"color": col, "width": 2}
)
mainpl.addItem(self.bestLineO)
mainpl.addItem(self.bestLineSh)
# init plot with page 1 data
self.currpage = 1
self.leftBtn.setEnabled(False)
if len(self.shifts) == 1:
self.rightBtn.setEnabled(False)
self.setData()
# accept / close
closeBtn = QPushButton("Close")
closeBtn.clicked.connect(self.accept)
# cancelBtn = QPushButton("Cancel")
# cancelBtn.clicked.connect(self.reject)
# Layouts
box = QVBoxLayout()
topHBox = QHBoxLayout()
topHBox.addWidget(self.labelCurrPage, stretch=1)
topHBox.addWidget(self.labelRecs, stretch=3)
box.addLayout(topHBox)
midHBox = QHBoxLayout()
midHBox.addWidget(self.leftBtn)
midHBox.addWidget(mainpl, stretch=10)
midHBox.addWidget(self.rightBtn)
box.addLayout(midHBox)
box.addWidget(self.connectCheckbox)
bottomHBox = QHBoxLayout()
bottomHBox.addWidget(closeBtn)
# bottomHBox.addWidget(cancelBtn)
box.addLayout(bottomHBox)
self.setLayout(box)
def connectToggled(self, ischecked):
# convert recorder names to indices
i = self.parent.allrecs.index(self.rec1)
j = self.parent.allrecs.index(self.rec2)
# change that connectivity edge
if ischecked:
self.parent.recConnections[i, j] = 1
else:
self.parent.recConnections[i, j] = 0
print("removing connection between", i, j)
def moveLeft(self):
self.currpage = self.currpage - 1
self.rightBtn.setEnabled(True)
if self.currpage == 1:
self.leftBtn.setEnabled(False)
self.setData()
def moveRight(self):
self.currpage = self.currpage + 1
self.leftBtn.setEnabled(True)
if self.currpage == len(self.shifts):
self.rightBtn.setEnabled(False)
self.setData()
def setData(self):
# update plot etc
currdata = self.shifts[self.currpage - 1]
self.p1.setData(y=currdata["series"], x=self.xs)
self.bestLineO.setValue(currdata["bestOverlap"])
self.bestLineSh.setValue(currdata["bestShift"])
self.labelCurrPage.setText("Page %s of %s" % (self.currpage, len(self.shifts)))
self.rec1 = currdata["recorders"][0]
self.rec2 = currdata["recorders"][1]
self.labelRecs.setText(
"Suggested shift for recorder %s w.r.t. %s" % (self.rec1, self.rec2)
)
i = self.parent.allrecs.index(self.rec1)
j = self.parent.allrecs.index(self.rec2)
result = self.parent.recConnections[i, j] == 1
self.connectCheckbox.setChecked(bool(result))
class CompareCallsDialog(QDialog):
# Init args:
# 1. list of SegmentLists for rec1
# 2. list of SegmentLists for rec2
# 3. best shift for rec2
# Will show the segments in the lists in order.
# Times in the segment lists must be as usual: relative to file start.
def __init__(self, annots1, annots2, parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle("Compare Call Pairs")
self.setWindowIcon(QIcon("img/Avianz.ico"))
self.setWindowFlags(
(self.windowFlags() ^ Qt.WindowContextHelpButtonHint)
| Qt.WindowMaximizeButtonHint
| Qt.WindowCloseButtonHint
)
self.parent = parent
self.rec1 = annots1[0].recname
self.rec2 = annots2[0].recname
self.annots1 = annots1
self.annots2 = annots2
# Set colour map
# cmap = self.config['cmap'] # TODO Read these from config
self.lut = colourMaps.getLookupTable("Grey")
self.cmapInverted = False
self.topLabel = QLabel(
"Comparing recorders %s (top) and %s (bottom)" % (self.rec1, self.rec2)
)
self.labelPair = QLabel()
# Volume, brightness and contrast sliders. (NOTE hardcoded init)
self.specControls = SupportClasses_GUI.BrightContrVol(80, 20, False)
self.specControls.colChanged.connect(self.setColourLevels)
# self.specControls.volChanged.connect(self.volSliderMoved)
# TODO add volume and playback at some point
# spectrograms
self.wPlot = pg.GraphicsLayoutWidget()
self.pPlot1 = self.wPlot.addViewBox(enableMouse=False, row=0, col=1)
self.pPlot2 = self.wPlot.addViewBox(enableMouse=False, row=1, col=1)
self.plot1 = pg.ImageItem()
self.plot2 = pg.ImageItem()
self.pPlot1.addItem(self.plot1)
self.pPlot2.addItem(self.plot2)
# Y (freq) axis
self.sg_axis1 = pg.AxisItem(orientation="left")
self.sg_axis1.linkToView(self.pPlot1)
self.sg_axis2 = pg.AxisItem(orientation="left")
self.sg_axis2.linkToView(self.pPlot2)
self.wPlot.addItem(self.sg_axis1, row=0, col=0)
self.wPlot.addItem(self.sg_axis2, row=1, col=0)
# The buttons to move through the overview
self.leftBtn = QToolButton()
self.leftBtn.setArrowType(Qt.LeftArrow)
self.leftBtn.clicked.connect(self.moveLeft)
self.leftBtn.setToolTip("View previous pair")
self.rightBtn = QToolButton()
self.rightBtn.setArrowType(Qt.RightArrow)
self.rightBtn.clicked.connect(self.moveRight)
self.rightBtn.setToolTip("View next pair")
self.leftBtn.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.MinimumExpanding)
self.rightBtn.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.MinimumExpanding)
# accept / close
closeBtn = QPushButton("Close")
closeBtn.clicked.connect(self.accept)
# init GUI with page 1 data
self.currpage = 1
self.leftBtn.setEnabled(False)
if len(self.annots1) == 1:
self.rightBtn.setEnabled(False)
self.setData()
# layout
box = QVBoxLayout()
box.addWidget(self.topLabel)
box.addWidget(self.labelPair)
box.addWidget(self.specControls)
midHBox = QHBoxLayout()
midHBox.addWidget(self.leftBtn)
midHBox.addWidget(self.wPlot, stretch=10)
midHBox.addWidget(self.rightBtn)
box.addLayout(midHBox)
box.addWidget(closeBtn)
self.setLayout(box)
def moveLeft(self):
self.currpage = self.currpage - 1
self.rightBtn.setEnabled(True)
if self.currpage == 1:
self.leftBtn.setEnabled(False)
self.setData()
def moveRight(self):
self.currpage = self.currpage + 1
self.leftBtn.setEnabled(True)
if self.currpage == len(self.annots1):
self.rightBtn.setEnabled(False)
self.setData()
def setData(self):
# update plot etc
currseg1 = self.annots1[self.currpage - 1]
currseg2 = self.annots2[self.currpage - 1]
wav1 = currseg1.wavname
wav2 = currseg2.wavname
print("Showing:", wav1, wav2)
wav1start = currseg1[0]
wav2start = currseg2[0]
wav1len = currseg1[1] - currseg1[0]
wav2len = currseg2[1] - currseg2[0]
# want to show equal duration for both spectrograms
# so choose min, in case one of them is near file end
wavlen = min(wav1len, wav2len)
# TODO temporary hacks to avoid trying to read past wav end
# (this happens if the adjustment forces the matching part out of
# the file where the matching segment was found)
if wav1start + wavlen > 900: