Skip to content

Commit 433474e

Browse files
author
trevor.stout
committed
Handle lower original sr, Add extra optional time buffer around pulses
1 parent 7120f6f commit 433474e

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

batbot/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def pipeline(
7070
quiet=False,
7171
plot_uncompressed_amplitude=False,
7272
include_original_sr=False,
73+
time_buffer_ms=1.0,
7374
debug=False,
7475
):
7576
"""
@@ -111,6 +112,7 @@ def pipeline(
111112
quiet=quiet,
112113
plot_uncompressed_amplitude=plot_uncompressed_amplitude,
113114
include_original_sr=include_original_sr,
115+
time_buffer_ms=time_buffer_ms,
114116
debug=debug,
115117
)
116118

@@ -311,6 +313,7 @@ def example():
311313
force_overwrite=True,
312314
plot_uncompressed_amplitude=True,
313315
include_original_sr=True,
316+
time_buffer_ms=5.0,
314317
)
315318
stop_time = time.time()
316319
print('Example pipeline completed in {} seconds.'.format(stop_time - start_time))

batbot/spectrogram/__init__.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,15 @@ def tighten_ranges(
604604
duration,
605605
skew_stddev=2.0,
606606
min_duration_ms=2.0,
607+
extra_buffer_pix=0.0,
607608
output_path='.',
608609
quiet=False,
609610
):
610611
minimum_duration = int(np.around(stft_db.shape[1] / (duration * 1e3) * min_duration_ms))
611612

612613
stride_ = 2
613614
window = int(window)
614-
buffer = int(round(window / stride_ / 2))
615+
buffer = int(round(window / stride_ / 2)) + extra_buffer_pix
615616

616617
ranges_ = []
617618
for index, (start, stop) in tqdm.tqdm(list(enumerate(ranges)), disable=quiet):
@@ -1422,6 +1423,7 @@ def compute_wrapper(
14221423
mask_secondary_effects=False,
14231424
plot_uncompressed_amplitude=False,
14241425
include_original_sr=False,
1426+
time_buffer_ms=1.0,
14251427
debug=False,
14261428
**kwargs,
14271429
):
@@ -1606,9 +1608,12 @@ def compute_wrapper(
16061608
else:
16071609

16081610
# Tighten the ranges by looking for substantial right-side skew (use stride for a smaller sampling window)
1611+
extra_buffer_pix = int(max(0.0, (time_buffer_ms - 1.0) / x_step_ms))
16091612
ranges = tighten_ranges(
1610-
stft_db, ranges, stride, duration, output_path=debug_path, quiet=quiet
1613+
stft_db, ranges, stride, duration, output_path=debug_path, extra_buffer_pix=extra_buffer_pix, quiet=quiet
16111614
)
1615+
# Merge all range segments into contiguous range blocks
1616+
ranges = merge_ranges(ranges, stft_db.shape[1])
16121617

16131618
# Extract chirp metrics and metadata
16141619
segments = {
@@ -1744,7 +1749,7 @@ def compute_wrapper(
17441749
metadata.update(slopes)
17451750

17461751
# Trim segment around the bat call with a small buffer
1747-
buffer_ms = 1.0
1752+
buffer_ms = time_buffer_ms
17481753
buffer_pix = int(round(buffer_ms / x_step_ms))
17491754
trim_begin = max(0, min(segment.shape[1], call_begin[1] - buffer_pix))
17501755
trim_end = max(0, min(segment.shape[1], call_end[1] + buffer_pix))
@@ -1893,15 +1898,20 @@ def compute_wrapper(
18931898
assert (
18941899
np.abs(y_step_freq - y_step_freq_origsr) / y_step_freq <= tol
18951900
), 'frequency step changed unexpectedly much when using original sample rate'
1896-
assert all(
1897-
[np.abs(x - y) / x <= tol for x, y in zip(bands, bands_origsr[-len(bands) :])]
1898-
), 'lower frequency bands changed unexpectedly much when using original sample rate'
1901+
if orig_sr >= sr:
1902+
assert all(
1903+
[np.abs(x - y) / x <= tol for x, y in zip(bands, bands_origsr[-len(bands) :])]
1904+
), 'lower frequency bands changed unexpectedly much when using original sample rate'
1905+
else:
1906+
assert all(
1907+
[np.abs(x - y) / x <= tol for x, y in zip(bands[-len(bands_origsr) :], bands_origsr)]
1908+
), 'lower frequency bands changed unexpectedly much when using original sample rate'
18991909

19001910
# Create compressed spectrogram using segment start and stop times
19011911
segments_origsr = []
19021912
for segment_meta in metas:
1903-
start = int(np.round(segment_meta['segment start.ms'] / x_step_ms_origsr))
1904-
end = int(np.round(segment_meta['segment end.ms'] / x_step_ms_origsr))
1913+
start = max(0, int(np.round(segment_meta['segment start.ms'] / x_step_ms_origsr)))
1914+
end = min(stft_db_origsr.shape[1], int(np.round(segment_meta['segment end.ms'] / x_step_ms_origsr)))
19051915
segments_origsr.append(stft_db_origsr[:, start:end])
19061916
segments['stft_db_origsr'] = np.concatenate(segments_origsr, axis=1)
19071917

@@ -1990,13 +2000,19 @@ def compute_wrapper(
19902000
(segments['stft_db_origsr'].shape[1], masked.shape[0]),
19912001
interpolation=cv2.INTER_LINEAR,
19922002
)
1993-
# Pad mask and masked to account for extra higher frequencies
1994-
mask_interp = np.pad(
1995-
mask_interp, ((stft_db_origsr.shape[0] - mask_interp.shape[0], 0), (0, 0))
1996-
)
1997-
masked_interp = np.pad(
1998-
masked_interp, ((stft_db_origsr.shape[0] - masked_interp.shape[0], 0), (0, 0))
1999-
)
2003+
if orig_sr >= sr:
2004+
# Pad mask and masked to account for extra higher frequencies
2005+
mask_interp = np.pad(
2006+
mask_interp, ((stft_db_origsr.shape[0] - mask_interp.shape[0], 0), (0, 0))
2007+
)
2008+
masked_interp = np.pad(
2009+
masked_interp, ((stft_db_origsr.shape[0] - masked_interp.shape[0], 0), (0, 0))
2010+
)
2011+
else:
2012+
# remove higher frequencies from mask which aren't present with original sr
2013+
mask_interp = mask_interp[mask_interp.shape[0] - stft_db_origsr.shape[0] :]
2014+
masked_interp = masked_interp[masked_interp.shape[0] - stft_db_origsr.shape[0] :]
2015+
pass
20002016
datas += [
20012017
(mask_paths, 'mask.origsr.jpg', mask_interp),
20022018
(masked_paths, 'masked.origsr.jpg', masked_interp),

0 commit comments

Comments
 (0)