annotate tests/test_plotting.py @ 455:7dae98b84cdd tip master

Merge branch 'master' of https://github.com/amsehili/auditok
author www-data <www-data@c4dm-xenserv-virt2.eecs.qmul.ac.uk>
date Tue, 03 Dec 2024 09:18:01 +0000
parents c5b4178aa80f
children
rev   line source
amine@347 1 import os
amine@348 2 import sys
amine@403 3 from tempfile import TemporaryDirectory
amine@403 4
amine@403 5 import matplotlib
amine@400 6 import pytest
amine@348 7
amine@400 8 matplotlib.use("AGG")
amine@400 9 import matplotlib.pyplot as plt # noqa E402
amine@403 10
amine@400 11 from auditok.core import AudioRegion # noqa E402
amine@347 12
amine@400 13 SAVE_NEW_IMAGES = False
amine@400 14 if SAVE_NEW_IMAGES:
amine@400 15 import shutil # noqa E402
amine@400 16
amine@347 17 matplotlib.rcParams["figure.figsize"] = (10, 4)
amine@347 18
amine@347 19
amine@400 20 @pytest.mark.parametrize("channels", [1, 2], ids=["mono", "stereo"])
amine@400 21 def test_region_plot(channels):
amine@400 22 type_ = "mono" if channels == 1 else "stereo"
amine@403 23 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
amine@426 24 image_filename = "tests/images/plot_{}_region.png".format(type_)
amine@400 25 expected_image = plt.imread(image_filename)
amine@400 26 with TemporaryDirectory() as tmpdir:
amine@400 27 output_image_filename = os.path.join(tmpdir, "image.png")
amine@400 28 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
amine@400 29 region.plot(show=False, save_as=output_image_filename)
amine@400 30 output_image = plt.imread(output_image_filename)
amine@400 31
amine@400 32 if SAVE_NEW_IMAGES:
amine@400 33 shutil.copy(output_image_filename, image_filename)
amine@400 34 assert (output_image == expected_image).all() # mono, stereo
amine@400 35
amine@400 36
amine@400 37 @pytest.mark.parametrize(
amine@400 38 "channels, use_channel",
amine@400 39 [
amine@400 40 (1, None), # mono
amine@400 41 (2, "any"), # stereo_any
amine@400 42 (2, 0), # stereo_uc_0
amine@400 43 (2, 1), # stereo_uc_1
amine@400 44 (2, "mix"), # stereo_uc_mix
amine@400 45 ],
amine@400 46 ids=["mono", "stereo_any", "stereo_uc_0", "stereo_uc_1", "stereo_uc_mix"],
amine@400 47 )
amine@400 48 def test_region_split_and_plot(channels, use_channel):
amine@400 49 type_ = "mono" if channels == 1 else "stereo"
amine@403 50 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
amine@400 51 if type_ == "mono":
amine@426 52 image_filename = "tests/images/split_and_plot_mono_region.png"
amine@400 53 else:
amine@426 54 image_filename = (
amine@426 55 f"tests/images/split_and_plot_uc_{use_channel}_stereo_region.png"
amine@426 56 )
amine@400 57
amine@400 58 expected_image = plt.imread(image_filename)
amine@400 59 with TemporaryDirectory() as tmpdir:
amine@400 60 output_image_filename = os.path.join(tmpdir, "image.png")
amine@400 61 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
amine@400 62 region.split_and_plot(
amine@400 63 aw=0.1,
amine@400 64 uc=use_channel,
amine@400 65 max_silence=0,
amine@400 66 show=False,
amine@400 67 save_as=output_image_filename,
amine@348 68 )
amine@400 69 output_image = plt.imread(output_image_filename)
amine@347 70
amine@400 71 if SAVE_NEW_IMAGES:
amine@400 72 shutil.copy(output_image_filename, image_filename)
amine@400 73 assert (output_image == expected_image).all()