• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2/* Copyright (c) 2023 Amazon
3   Written by Jan Buethe */
4/*
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   - Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11
12   - Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28"""
29
30import os
31import argparse
32
33import numpy as np
34import matplotlib.pyplot as plt
35from prettytable import PrettyTable
36from matplotlib.patches import Patch
37
38parser = argparse.ArgumentParser()
39parser.add_argument('folder', type=str, help='path to folder with pre-calculated metrics')
40parser.add_argument('--metric', choices=['pesq', 'moc', 'warpq', 'nomad', 'laceloss', 'all'], default='all', help='default: all')
41parser.add_argument('--output', type=str, default=None, help='alternative output folder, default: folder')
42
43def load_data(folder):
44    data = dict()
45
46    if os.path.isfile(os.path.join(folder, 'results_moc.npy')):
47        data['moc'] = np.load(os.path.join(folder, 'results_moc.npy'), allow_pickle=True).item()
48
49    if os.path.isfile(os.path.join(folder, 'results_moc2.npy')):
50        data['moc2'] = np.load(os.path.join(folder, 'results_moc2.npy'), allow_pickle=True).item()
51
52    if os.path.isfile(os.path.join(folder, 'results_pesq.npy')):
53        data['pesq'] = np.load(os.path.join(folder, 'results_pesq.npy'), allow_pickle=True).item()
54
55    if os.path.isfile(os.path.join(folder, 'results_warpq.npy')):
56        data['warpq'] = np.load(os.path.join(folder, 'results_warpq.npy'), allow_pickle=True).item()
57
58    if os.path.isfile(os.path.join(folder, 'results_nomad.npy')):
59        data['nomad'] = np.load(os.path.join(folder, 'results_nomad.npy'), allow_pickle=True).item()
60
61    if os.path.isfile(os.path.join(folder, 'results_laceloss.npy')):
62        data['laceloss'] = np.load(os.path.join(folder, 'results_laceloss.npy'), allow_pickle=True).item()
63
64    return data
65
66def plot_data(filename, data, title=None):
67    compare_dict = dict()
68    for br in data.keys():
69        compare_dict[f'Opus {br/1000:.1f} kb/s'] = data[br][:, 0]
70        compare_dict[f'LACE {br/1000:.1f} kb/s'] = data[br][:, 1]
71        compare_dict[f'NoLACE {br/1000:.1f} kb/s'] = data[br][:, 2]
72
73    plt.rcParams.update({
74        "text.usetex": True,
75        "font.family": "Helvetica",
76        "font.size": 32
77    })
78
79    black = '#000000'
80    red = '#ff5745'
81    blue = '#007dbc'
82    colors = [black, red, blue]
83    legend_elements = [Patch(facecolor=colors[0], label='Opus SILK'),
84                    Patch(facecolor=colors[1], label='LACE'),
85                    Patch(facecolor=colors[2], label='NoLACE')]
86
87    fig, ax = plt.subplots()
88    fig.set_size_inches(40, 20)
89    bplot = ax.boxplot(compare_dict.values(), showfliers=False, notch=True, patch_artist=True)
90
91    for i, patch in enumerate(bplot['boxes']):
92        patch.set_facecolor(colors[i%3])
93
94    ax.set_xticklabels(compare_dict.keys(), rotation=290)
95
96    if title is not None:
97        ax.set_title(title)
98
99    ax.legend(handles=legend_elements)
100
101    fig.savefig(filename, bbox_inches='tight')
102
103if __name__ == "__main__":
104    args = parser.parse_args()
105    data = load_data(args.folder)
106
107
108    metrics = list(data.keys()) if args.metric == 'all' else [args.metric]
109    folder = args.folder if args.output is None else args.output
110    os.makedirs(folder, exist_ok=True)
111
112    for metric in metrics:
113        print(f"Plotting data for {metric} metric...")
114        plot_data(os.path.join(folder, f"boxplot_{metric}.png"), data[metric], title=metric.upper())
115
116    print("Done.")