amine@347
|
1 import os
|
amine@348
|
2 import sys
|
amine@347
|
3 import unittest
|
amine@347
|
4 from unittest import TestCase
|
amine@347
|
5 from tempfile import TemporaryDirectory
|
amine@347
|
6 from genty import genty, genty_dataset
|
amine@347
|
7 import matplotlib
|
amine@348
|
8
|
amine@348
|
9 matplotlib.use("AGG") # noqa E402
|
amine@347
|
10 import matplotlib.pyplot as plt
|
amine@347
|
11 from auditok.core import AudioRegion
|
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@347
|
18 matplotlib.rcParams["figure.figsize"] = (10, 4)
|
amine@347
|
19
|
amine@347
|
20
|
amine@347
|
21 @genty
|
amine@347
|
22 class TestPlotting(TestCase):
|
amine@347
|
23 @genty_dataset(mono=(1,), stereo=(2,))
|
amine@347
|
24 def test_region_plot(self, channels):
|
amine@347
|
25 type_ = "mono" if channels == 1 else "stereo"
|
amine@347
|
26 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
|
amine@348
|
27 image_filename = "tests/images/{}plot_{}_region.png".format(
|
amine@348
|
28 PREFIX, type_
|
amine@348
|
29 )
|
amine@347
|
30 expected_image = plt.imread(image_filename)
|
amine@347
|
31 with TemporaryDirectory() as tmpdir:
|
amine@347
|
32 output_image_filename = os.path.join(tmpdir, "image.png")
|
amine@347
|
33 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
|
amine@347
|
34 region.plot(show=False, save_as=output_image_filename)
|
amine@347
|
35 output_image = plt.imread(output_image_filename)
|
amine@347
|
36 self.assertTrue((output_image == expected_image).all())
|
amine@347
|
37
|
amine@347
|
38 @genty_dataset(
|
amine@347
|
39 mono=(1,),
|
amine@347
|
40 stereo_any=(2, "any"),
|
amine@347
|
41 stereo_uc_0=(2, 0),
|
amine@347
|
42 stereo_uc_1=(2, 1),
|
amine@347
|
43 stereo_uc_mix=(2, "mix"),
|
amine@347
|
44 )
|
amine@347
|
45 def test_region_split_and_plot(self, channels, use_channel=None):
|
amine@347
|
46 type_ = "mono" if channels == 1 else "stereo"
|
amine@347
|
47 audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_)
|
amine@347
|
48 if type_ == "mono":
|
amine@348
|
49 fmt = "tests/images/{}split_and_plot_mono_region.png"
|
amine@347
|
50 else:
|
amine@348
|
51 fmt = "tests/images/{}split_and_plot_uc_{}_stereo_region.png"
|
amine@348
|
52 image_filename = fmt.format(PREFIX, use_channel)
|
amine@347
|
53
|
amine@347
|
54 expected_image = plt.imread(image_filename)
|
amine@347
|
55 with TemporaryDirectory() as tmpdir:
|
amine@347
|
56 output_image_filename = os.path.join(tmpdir, "image.png")
|
amine@347
|
57 region = AudioRegion.load(audio_filename, sr=10, sw=2, ch=channels)
|
amine@347
|
58 region.split_and_plot(
|
amine@347
|
59 aw=0.1,
|
amine@347
|
60 uc=use_channel,
|
amine@347
|
61 max_silence=0,
|
amine@347
|
62 show=False,
|
amine@347
|
63 save_as=output_image_filename,
|
amine@347
|
64 )
|
amine@347
|
65 output_image = plt.imread(output_image_filename)
|
amine@347
|
66 self.assertTrue((output_image == expected_image).all())
|
amine@347
|
67
|
amine@347
|
68
|
amine@347
|
69 if __name__ == "__main__":
|
amine@347
|
70 unittest.main()
|