annotate tests/test_plotting.py @ 425:1b78211b7e07

Update docstrings
author Amine Sehili <amine.sehili@gmail.com>
date Sat, 19 Oct 2024 16:58:19 +0200
parents 996948ada980
children c5b4178aa80f
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@348 13 if sys.version_info.minor <= 5:
amine@348 14 PREFIX = "py34_py35/"
amine@348 15 else:
amine@348 16 PREFIX = ""
amine@348 17
amine@400 18 SAVE_NEW_IMAGES = False
amine@400 19 if SAVE_NEW_IMAGES:
amine@400 20 import shutil # noqa E402
amine@400 21
amine@347 22 matplotlib.rcParams["figure.figsize"] = (10, 4)
amine@347 23
amine@347 24
amine@400 25 @pytest.mark.parametrize("channels", [1, 2], ids=["mono", "stereo"])
amine@400 26 def test_region_plot(channels):
amine@400 27 type_ = "mono" if channels == 1 else "stereo"
amine@403 28 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
amine@403 29 image_filename = "tests/images/{}plot_{}_region.png".format(PREFIX, type_)
amine@400 30 expected_image = plt.imread(image_filename)
amine@400 31 with TemporaryDirectory() as tmpdir:
amine@400 32 output_image_filename = os.path.join(tmpdir, "image.png")
amine@400 33 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
amine@400 34 region.plot(show=False, save_as=output_image_filename)
amine@400 35 output_image = plt.imread(output_image_filename)
amine@400 36
amine@400 37 if SAVE_NEW_IMAGES:
amine@400 38 shutil.copy(output_image_filename, image_filename)
amine@400 39 assert (output_image == expected_image).all() # mono, stereo
amine@400 40
amine@400 41
amine@400 42 @pytest.mark.parametrize(
amine@400 43 "channels, use_channel",
amine@400 44 [
amine@400 45 (1, None), # mono
amine@400 46 (2, "any"), # stereo_any
amine@400 47 (2, 0), # stereo_uc_0
amine@400 48 (2, 1), # stereo_uc_1
amine@400 49 (2, "mix"), # stereo_uc_mix
amine@400 50 ],
amine@400 51 ids=["mono", "stereo_any", "stereo_uc_0", "stereo_uc_1", "stereo_uc_mix"],
amine@400 52 )
amine@400 53 def test_region_split_and_plot(channels, use_channel):
amine@400 54 type_ = "mono" if channels == 1 else "stereo"
amine@403 55 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
amine@400 56 if type_ == "mono":
amine@400 57 fmt = "tests/images/{}split_and_plot_mono_region.png"
amine@400 58 else:
amine@400 59 fmt = "tests/images/{}split_and_plot_uc_{}_stereo_region.png"
amine@400 60 image_filename = fmt.format(PREFIX, use_channel)
amine@400 61
amine@400 62 expected_image = plt.imread(image_filename)
amine@400 63 with TemporaryDirectory() as tmpdir:
amine@400 64 output_image_filename = os.path.join(tmpdir, "image.png")
amine@400 65 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
amine@400 66 region.split_and_plot(
amine@400 67 aw=0.1,
amine@400 68 uc=use_channel,
amine@400 69 max_silence=0,
amine@400 70 show=False,
amine@400 71 save_as=output_image_filename,
amine@348 72 )
amine@400 73 output_image = plt.imread(output_image_filename)
amine@347 74
amine@400 75 if SAVE_NEW_IMAGES:
amine@400 76 shutil.copy(output_image_filename, image_filename)
amine@400 77 assert (output_image == expected_image).all()