# HG changeset patch # User Amine Sehili # Date 1573487612 -3600 # Node ID 5382bcab7acb6b104d9c9c35ebc9d20c88710fa5 # Parent 979343fe59e06138ef84fad5aeae62b0a16c05a6 Add tests for plotting diff -r 979343fe59e0 -r 5382bcab7acb auditok/plotting.py --- a/auditok/plotting.py Sun Nov 10 21:16:16 2019 +0100 +++ b/auditok/plotting.py Mon Nov 11 16:53:32 2019 +0100 @@ -59,7 +59,8 @@ if len(y.shape) == 1: y = y.reshape(1, -1) nb_subplots, nb_samples = y.shape - time_axis = _make_time_axis(nb_samples, audio_region.sampling_rate) + sampling_rate = audio_region.sampling_rate + time_axis = _make_time_axis(nb_samples, sampling_rate) if energy_threshold is not None: eth_log10 = energy_threshold * np.log(10) / 10 amplitude_threshold = np.sqrt(np.exp(eth_log10)) @@ -68,7 +69,12 @@ if detections is None: detections = [] else: - detections = list(detections) + # End of detection corresponds to the end of the last sample but + # to stay compatible with the time axis of signal plotting we want end + # of detection to correspond to the *start* of the that last sample. + detections = [ + (start, end - (1 / sampling_rate)) for (start, end) in detections + ] if theme == "auditok": theme = AUDITOK_PLOT_THEME @@ -140,6 +146,5 @@ if save_as is not None: plt.savefig(save_as, dpi=dpi) - if show: plt.show() diff -r 979343fe59e0 -r 5382bcab7acb tests/images/plot_mono_region.png Binary file tests/images/plot_mono_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/plot_stereo_region.png Binary file tests/images/plot_stereo_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/split_and_plot_mono_region.png Binary file tests/images/split_and_plot_mono_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/split_and_plot_uc_0_stereo_region.png Binary file tests/images/split_and_plot_uc_0_stereo_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/split_and_plot_uc_1_stereo_region.png Binary file tests/images/split_and_plot_uc_1_stereo_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/split_and_plot_uc_any_stereo_region.png Binary file tests/images/split_and_plot_uc_any_stereo_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/images/split_and_plot_uc_mix_stereo_region.png Binary file tests/images/split_and_plot_uc_mix_stereo_region.png has changed diff -r 979343fe59e0 -r 5382bcab7acb tests/test_plotting.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_plotting.py Mon Nov 11 16:53:32 2019 +0100 @@ -0,0 +1,61 @@ +import os +import unittest +from unittest import TestCase +from tempfile import TemporaryDirectory +from genty import genty, genty_dataset +import matplotlib +import matplotlib.pyplot as plt +from auditok.core import AudioRegion + +matplotlib.use("agg") +matplotlib.rcParams["figure.figsize"] = (10, 4) + + +@genty +class TestPlotting(TestCase): + @genty_dataset(mono=(1,), stereo=(2,)) + def test_region_plot(self, channels): + type_ = "mono" if channels == 1 else "stereo" + audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_) + image_filename = "tests/images/plot_{}_region.png".format(type_) + expected_image = plt.imread(image_filename) + with TemporaryDirectory() as tmpdir: + output_image_filename = os.path.join(tmpdir, "image.png") + region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels) + region.plot(show=False, save_as=output_image_filename) + output_image = plt.imread(output_image_filename) + self.assertTrue((output_image == expected_image).all()) + + @genty_dataset( + mono=(1,), + stereo_any=(2, "any"), + stereo_uc_0=(2, 0), + stereo_uc_1=(2, 1), + stereo_uc_mix=(2, "mix"), + ) + def test_region_split_and_plot(self, channels, use_channel=None): + type_ = "mono" if channels == 1 else "stereo" + audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_) + if type_ == "mono": + image_filename = "tests/images/split_and_plot_mono_region.png" + else: + fmt = "tests/images/split_and_plot_uc_{}_stereo_region.png" + image_filename = fmt.format(use_channel) + + expected_image = plt.imread(image_filename) + with TemporaryDirectory() as tmpdir: + output_image_filename = os.path.join(tmpdir, "image.png") + region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels) + region.split_and_plot( + aw=0.1, + uc=use_channel, + max_silence=0, + show=False, + save_as=output_image_filename, + ) + output_image = plt.imread(output_image_filename) + self.assertTrue((output_image == expected_image).all()) + + +if __name__ == "__main__": + unittest.main()