• 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_pesq.npy')):
50        data['pesq'] = np.load(os.path.join(folder, 'results_pesq.npy'), allow_pickle=True).item()
51
52    if os.path.isfile(os.path.join(folder, 'results_warpq.npy')):
53        data['warpq'] = np.load(os.path.join(folder, 'results_warpq.npy'), allow_pickle=True).item()
54
55    if os.path.isfile(os.path.join(folder, 'results_nomad.npy')):
56        data['nomad'] = np.load(os.path.join(folder, 'results_nomad.npy'), allow_pickle=True).item()
57
58    if os.path.isfile(os.path.join(folder, 'results_laceloss.npy')):
59        data['laceloss'] = np.load(os.path.join(folder, 'results_laceloss.npy'), allow_pickle=True).item()
60
61    return data
62
63def make_table(filename, data, title=None):
64
65    # mean values
66    tbl = PrettyTable()
67    tbl.field_names = ['bitrate (bps)', 'Opus', 'LACE', 'NoLACE']
68    for br in data.keys():
69        opus = data[br][:, 0]
70        lace = data[br][:, 1]
71        nolace = data[br][:, 2]
72        tbl.add_row([br, f"{float(opus.mean()):.3f} ({float(opus.std()):.2f})", f"{float(lace.mean()):.3f} ({float(lace.std()):.2f})", f"{float(nolace.mean()):.3f} ({float(nolace.std()):.2f})"])
73
74    with open(filename + ".txt", "w") as f:
75        f.write(str(tbl))
76
77    with open(filename + ".html", "w") as f:
78        f.write(tbl.get_html_string())
79
80    with open(filename + ".csv", "w") as f:
81        f.write(tbl.get_csv_string())
82
83    print(tbl)
84
85
86def make_diff_table(filename, data, title=None):
87
88    # mean values
89    tbl = PrettyTable()
90    tbl.field_names = ['bitrate (bps)', 'LACE - Opus', 'NoLACE - Opus']
91    for br in data.keys():
92        opus = data[br][:, 0]
93        lace = data[br][:, 1] - opus
94        nolace = data[br][:, 2] - opus
95        tbl.add_row([br, f"{float(lace.mean()):.3f} ({float(lace.std()):.2f})", f"{float(nolace.mean()):.3f} ({float(nolace.std()):.2f})"])
96
97    with open(filename + ".txt", "w") as f:
98        f.write(str(tbl))
99
100    with open(filename + ".html", "w") as f:
101        f.write(tbl.get_html_string())
102
103    with open(filename + ".csv", "w") as f:
104        f.write(tbl.get_csv_string())
105
106    print(tbl)
107
108if __name__ == "__main__":
109    args = parser.parse_args()
110    data = load_data(args.folder)
111
112    metrics = list(data.keys()) if args.metric == 'all' else [args.metric]
113    folder = args.folder if args.output is None else args.output
114    os.makedirs(folder, exist_ok=True)
115
116    for metric in metrics:
117        print(f"Plotting data for {metric} metric...")
118        make_table(os.path.join(folder, f"table_{metric}"), data[metric])
119        make_diff_table(os.path.join(folder, f"table_diff_{metric}"), data[metric])
120
121    print("Done.")