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 plot_data(filename, data, title=None): 64 compare_dict = dict() 65 for br in data.keys(): 66 compare_dict[f'Opus {br/1000:.1f} kb/s'] = data[br][:, 0] 67 compare_dict[f'LACE (MOC only) {br/1000:.1f} kb/s'] = data[br][:, 1] 68 compare_dict[f'LACE (MOC + TD) {br/1000:.1f} kb/s'] = data[br][:, 2] 69 70 plt.rcParams.update({ 71 "text.usetex": True, 72 "font.family": "Helvetica", 73 "font.size": 32 74 }) 75 colors = ['pink', 'lightblue', 'lightgreen'] 76 legend_elements = [Patch(facecolor=colors[0], label='Opus SILK'), 77 Patch(facecolor=colors[1], label='MOC loss only'), 78 Patch(facecolor=colors[2], label='MOC + TD loss')] 79 80 fig, ax = plt.subplots() 81 fig.set_size_inches(40, 20) 82 bplot = ax.boxplot(compare_dict.values(), showfliers=False, notch=True, patch_artist=True) 83 84 for i, patch in enumerate(bplot['boxes']): 85 patch.set_facecolor(colors[i%3]) 86 87 ax.set_xticklabels(compare_dict.keys(), rotation=290) 88 89 if title is not None: 90 ax.set_title(title) 91 92 ax.legend(handles=legend_elements) 93 94 fig.savefig(filename, bbox_inches='tight') 95 96if __name__ == "__main__": 97 args = parser.parse_args() 98 data = load_data(args.folder) 99 100 101 metrics = list(data.keys()) if args.metric == 'all' else [args.metric] 102 folder = args.folder if args.output is None else args.output 103 os.makedirs(folder, exist_ok=True) 104 105 for metric in metrics: 106 print(f"Plotting data for {metric} metric...") 107 plot_data(os.path.join(folder, f"boxplot_{metric}.png"), data[metric], title=metric.upper()) 108 109 print("Done.")