mirror of
https://github.com/macaodha/batdetect2.git
synced 2025-06-29 14:41:58 +02:00
Remove test migration
This commit is contained in:
parent
257e1e01bf
commit
8a463e3942
@ -1,156 +0,0 @@
|
|||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
from soundevent import data
|
|
||||||
|
|
||||||
from batdetect2 import preprocess
|
|
||||||
from batdetect2.utils import audio_utils
|
|
||||||
|
|
||||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
|
||||||
EXAMPLE_AUDIO = ROOT_DIR / "example_data" / "audio"
|
|
||||||
TEST_AUDIO = ROOT_DIR / "tests" / "data"
|
|
||||||
|
|
||||||
|
|
||||||
TEST_FILES = [
|
|
||||||
EXAMPLE_AUDIO / "20170701_213954-MYOMYS-LR_0_0.5.wav",
|
|
||||||
EXAMPLE_AUDIO / "20180530_213516-EPTSER-LR_0_0.5.wav",
|
|
||||||
EXAMPLE_AUDIO / "20180627_215323-RHIFER-LR_0_0.5.wav",
|
|
||||||
TEST_AUDIO / "20230322_172000_selec2.wav",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("audio_file", TEST_FILES)
|
|
||||||
@pytest.mark.parametrize("scale", [True, False])
|
|
||||||
def test_audio_loading_hasnt_changed(
|
|
||||||
audio_file,
|
|
||||||
scale,
|
|
||||||
):
|
|
||||||
time_expansion = 1
|
|
||||||
target_sampling_rate = 256_000
|
|
||||||
recording = data.Recording.from_file(
|
|
||||||
audio_file,
|
|
||||||
time_expansion=time_expansion,
|
|
||||||
)
|
|
||||||
clip = data.Clip(
|
|
||||||
recording=recording,
|
|
||||||
start_time=0,
|
|
||||||
end_time=recording.duration,
|
|
||||||
)
|
|
||||||
|
|
||||||
_, audio_original = audio_utils.load_audio(
|
|
||||||
audio_file,
|
|
||||||
time_expansion,
|
|
||||||
target_samp_rate=target_sampling_rate,
|
|
||||||
scale=scale,
|
|
||||||
)
|
|
||||||
audio_new = preprocess.load_clip_audio(
|
|
||||||
clip,
|
|
||||||
config=preprocess.AudioConfig(
|
|
||||||
resample=preprocess.ResampleConfig(
|
|
||||||
samplerate=target_sampling_rate,
|
|
||||||
),
|
|
||||||
center=scale,
|
|
||||||
scale=scale,
|
|
||||||
duration=None,
|
|
||||||
),
|
|
||||||
dtype=np.float32,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert audio_original.shape == audio_new.shape
|
|
||||||
assert audio_original.dtype == audio_new.dtype
|
|
||||||
assert np.isclose(audio_original, audio_new.data).all()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("audio_file", TEST_FILES)
|
|
||||||
@pytest.mark.parametrize("spec_scale", ["log", "pcen", "amplitude"])
|
|
||||||
@pytest.mark.parametrize("denoise_spec_avg", [True, False])
|
|
||||||
@pytest.mark.parametrize("max_scale_spec", [True, False])
|
|
||||||
@pytest.mark.parametrize("fft_win_length", [512 / 256_000, 1024 / 256_000])
|
|
||||||
def test_spectrogram_generation_hasnt_changed(
|
|
||||||
audio_file,
|
|
||||||
spec_scale,
|
|
||||||
denoise_spec_avg,
|
|
||||||
max_scale_spec,
|
|
||||||
fft_win_length,
|
|
||||||
):
|
|
||||||
time_expansion = 1
|
|
||||||
target_sampling_rate = 256_000
|
|
||||||
min_freq = 10_000
|
|
||||||
max_freq = 120_000
|
|
||||||
fft_overlap = 0.75
|
|
||||||
|
|
||||||
if spec_scale == "log":
|
|
||||||
scale = preprocess.LogScaleConfig()
|
|
||||||
elif spec_scale == "pcen":
|
|
||||||
scale = preprocess.PcenConfig()
|
|
||||||
else:
|
|
||||||
scale = preprocess.AmplitudeScaleConfig()
|
|
||||||
|
|
||||||
config = preprocess.SpectrogramConfig(
|
|
||||||
stft=preprocess.STFTConfig(
|
|
||||||
window_overlap=fft_overlap,
|
|
||||||
window_duration=fft_win_length,
|
|
||||||
),
|
|
||||||
frequencies=preprocess.FrequencyConfig(
|
|
||||||
min_freq=min_freq,
|
|
||||||
max_freq=max_freq,
|
|
||||||
),
|
|
||||||
scale=scale,
|
|
||||||
spectral_mean_substraction=denoise_spec_avg,
|
|
||||||
size=None,
|
|
||||||
peak_normalize=max_scale_spec,
|
|
||||||
)
|
|
||||||
|
|
||||||
recording = data.Recording.from_file(
|
|
||||||
audio_file,
|
|
||||||
time_expansion=time_expansion,
|
|
||||||
)
|
|
||||||
|
|
||||||
clip = data.Clip(
|
|
||||||
recording=recording,
|
|
||||||
start_time=0,
|
|
||||||
end_time=recording.duration,
|
|
||||||
)
|
|
||||||
|
|
||||||
audio = preprocess.load_clip_audio(
|
|
||||||
clip,
|
|
||||||
config=preprocess.AudioConfig(
|
|
||||||
resample=preprocess.ResampleConfig(
|
|
||||||
samplerate=target_sampling_rate,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
spec_original, _ = audio_utils.generate_spectrogram(
|
|
||||||
audio.data,
|
|
||||||
sampling_rate=target_sampling_rate,
|
|
||||||
params=dict(
|
|
||||||
fft_win_length=fft_win_length,
|
|
||||||
fft_overlap=fft_overlap,
|
|
||||||
max_freq=max_freq,
|
|
||||||
min_freq=min_freq,
|
|
||||||
spec_scale=spec_scale,
|
|
||||||
denoise_spec_avg=denoise_spec_avg,
|
|
||||||
max_scale_spec=max_scale_spec,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
new_spec = preprocess.compute_spectrogram(
|
|
||||||
audio,
|
|
||||||
config=config,
|
|
||||||
dtype=np.float32,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert spec_original.shape == new_spec.shape
|
|
||||||
assert spec_original.dtype == new_spec.dtype
|
|
||||||
|
|
||||||
# Check that the spectrogram content is the same within a tolerance of 1e-5
|
|
||||||
# for each element of the spectrogram at least 99.5% of the time.
|
|
||||||
# NOTE: The pcen function is not the same as the one in the original code
|
|
||||||
# thus the need for a tolerance, but the values are still very similar.
|
|
||||||
# NOTE: The original spectrogram is flipped vertically
|
|
||||||
assert (
|
|
||||||
np.isclose(spec_original, np.flipud(new_spec.data), atol=1e-5).mean()
|
|
||||||
> 0.995
|
|
||||||
)
|
|
@ -1,85 +0,0 @@
|
|||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from batdetect2.compat.params import get_training_preprocessing_config
|
|
||||||
from batdetect2.data import BatDetect2FilesAnnotations, load_annotated_dataset
|
|
||||||
from batdetect2.train.preprocess import generate_train_example
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def regression_dir(data_dir: Path) -> Path:
|
|
||||||
dir = data_dir / "regression"
|
|
||||||
assert dir.exists()
|
|
||||||
return dir
|
|
||||||
|
|
||||||
|
|
||||||
def test_can_generate_similar_training_inputs(
|
|
||||||
example_audio_dir: Path,
|
|
||||||
example_audio_files: List[Path],
|
|
||||||
example_anns_dir: Path,
|
|
||||||
regression_dir: Path,
|
|
||||||
):
|
|
||||||
old_parameters = json.loads((regression_dir / "params.json").read_text())
|
|
||||||
config = get_training_preprocessing_config(old_parameters)
|
|
||||||
|
|
||||||
assert config is not None
|
|
||||||
|
|
||||||
for audio_file in example_audio_files:
|
|
||||||
example_file = regression_dir / f"{audio_file.name}.npz"
|
|
||||||
|
|
||||||
dataset = np.load(example_file)
|
|
||||||
|
|
||||||
spec = dataset["spec"][0]
|
|
||||||
detection_mask = dataset["detection_mask"][0]
|
|
||||||
size_mask = dataset["size_mask"]
|
|
||||||
class_mask = dataset["class_mask"]
|
|
||||||
|
|
||||||
project = load_annotated_dataset(
|
|
||||||
BatDetect2FilesAnnotations(
|
|
||||||
name="test",
|
|
||||||
annotations_dir=example_anns_dir,
|
|
||||||
audio_dir=example_audio_dir,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
clip_annotation = next(
|
|
||||||
ann
|
|
||||||
for ann in project.clip_annotations
|
|
||||||
if ann.clip.recording.path == audio_file
|
|
||||||
)
|
|
||||||
|
|
||||||
new_dataset = generate_train_example(
|
|
||||||
clip_annotation,
|
|
||||||
preprocessing_config=config.preprocessing,
|
|
||||||
target_config=config.target,
|
|
||||||
label_config=config.labels,
|
|
||||||
)
|
|
||||||
new_spec = new_dataset["spectrogram"].values
|
|
||||||
new_detection_mask = new_dataset["detection"].values
|
|
||||||
new_size_mask = new_dataset["size"].values
|
|
||||||
new_class_mask = new_dataset["class"].values
|
|
||||||
|
|
||||||
assert spec.shape == new_spec.shape
|
|
||||||
assert detection_mask.shape == new_detection_mask.shape
|
|
||||||
assert size_mask.shape == new_size_mask.shape
|
|
||||||
assert class_mask.shape[1:] == new_class_mask.shape[1:]
|
|
||||||
assert class_mask.shape[0] == new_class_mask.shape[0] + 1
|
|
||||||
|
|
||||||
x_new, y_new = np.nonzero(new_size_mask.max(axis=0))
|
|
||||||
x_orig, y_orig = np.nonzero(np.flipud(size_mask.max(axis=0)))
|
|
||||||
|
|
||||||
assert (x_new == x_orig).all()
|
|
||||||
|
|
||||||
# NOTE: a difference of 1 pixel is due to discrepancies on how
|
|
||||||
# frequency bins are interpreted. Shouldn't be an issue
|
|
||||||
assert (y_new == y_orig + 1).all()
|
|
||||||
|
|
||||||
width_new, height_new = new_size_mask[:, x_new, y_new]
|
|
||||||
width_orig, height_orig = np.flip(size_mask, axis=1)[:, x_orig, y_orig]
|
|
||||||
|
|
||||||
assert (np.floor(width_new) == width_orig).all()
|
|
||||||
assert (np.ceil(height_new) == height_orig).all()
|
|
Loading…
Reference in New Issue
Block a user