diff --git a/plotter/reward_plotter.py b/plotter/reward_plotter.py index ffeaa75..8b30d05 100644 --- a/plotter/reward_plotter.py +++ b/plotter/reward_plotter.py @@ -7,8 +7,8 @@ def plot_csv(paths, x_axis, y_axis): for path_ in paths: data = np.genfromtxt(path_, delimiter=',', skip_header=0, dtype=float) - mean = np.mean(data, axis=1) - std = np.std(data, axis=1) + mean = np.mean(data, axis=0) + std = np.std(data, axis=0) x = np.linspace(0, mean.shape[0], mean.shape[0]) @@ -35,13 +35,10 @@ def plot_csv(paths, x_axis, y_axis): if __name__ == '__main__': - filenames = ['cp-e150r10-bf15-base/cp-ei-random-1_0-15-1690282051_2959082.csv', - 'cp-e150r10-bf15-noshaping/cp-pei-random-0_95-15-1690276946_1944933.csv', - 'cp-e150r10-bf15-noshaping/cp-pei-regular-25_0-15-1690290021_6843266.csv', - 'cp-e150r10-bf15-noshaping/cp-pei-improvement-0_1-15-1690292664_0382216.csv', + filenames = ['franka-pei-random-1_0-6-1694787936_385925.csv', ] home_dir = os.path.expanduser('~') - file_path = os.path.join(home_dir, 'Documents/IntRLResults/CP-Results') + file_path = os.path.join(home_dir, 'Documents/IntRLResults/Franka-Results') paths = [os.path.join(file_path, filename) for filename in filenames] plot_csv(paths, 'Episodes', 'Reward') # diff --git a/plotter/reward_plotter_final.py b/plotter/reward_plotter_final.py index 2bbd9f5..9e3edb6 100644 --- a/plotter/reward_plotter_final.py +++ b/plotter/reward_plotter_final.py @@ -1,56 +1,129 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +import matplotlib.ticker as ticker import os +def modified_plot_v7(paths_dict, x_axis, y_axis, subplot_titles, ylims=(0, 200),offset=0): + def fixed_width_formatter(x, _): + return "{:>7}".format(x) + + # Create a figure with 4 subplots + # fig, axs = plt.subplots(1, 4, figsize=(20, 6)) + fig = plt.figure(figsize=(20, 6)) + gs = gridspec.GridSpec(1, 4, width_ratios=[1, 1, 1, 1]) + axs=[] + axs.append(fig.add_subplot(gs[0, 0])) + axs.append(fig.add_subplot(gs[0, 1])) + axs.append(fig.add_subplot(gs[0, 2])) + axs.append(fig.add_subplot(gs[0, 3])) + + plt.rcParams['font.size'] = 16 + label_font_size = 14 + plt.tick_params(axis='y', labelsize=14) + + # Plotting mean and std for the first 3 subplots + for idx, key in enumerate(['subplot1', 'subplot2', 'subplot3']): + for path_, color, plot_line in paths_dict[key]: + if plot_line: + data = np.genfromtxt(path_, delimiter=',', skip_header=0, dtype=float) + + if data.shape[0] > data.shape[1]: + mean = np.mean(data, axis=1) + offset + std = np.std(data, axis=1) + else: + mean = np.mean(data, axis=0) +offset + std = np.std(data, axis=0) + + x = np.linspace(0, mean.shape[0], mean.shape[0]) + + axs[idx].plot(x, mean, color=color) + axs[idx].fill_between( + x, + mean - 1.96 * std, + mean + 1.96 * std, + color=color, + alpha=0.5 + ) + + axs[idx].tick_params(axis='x', labelsize=14) + axs[idx].tick_params(axis='y', labelsize=14) + axs[idx].set_xlabel(x_axis, fontsize=label_font_size) + axs[idx].text(0.5, -0.15, subplot_titles[idx], horizontalalignment='center', verticalalignment='center', + transform=axs[idx].transAxes, fontsize=label_font_size) + axs[idx].set_xlim([0, mean.shape[0]]) + axs[idx].set_ylim(ylims) + axs[idx].spines['right'].set_visible(False) + axs[idx].spines['top'].set_visible(False) + axs[idx].set_xlabel(x_axis, fontsize=label_font_size, position=(0.5, -0.2)) + if idx != 0: + axs[idx].set_ylabel('') + axs[idx].set_yticklabels([]) + else: + axs[0].set_ylabel(y_axis, fontsize=label_font_size) + axs[0].yaxis.set_tick_params(size=14, pad=0) + axs[idx].yaxis.set_major_formatter(ticker.FuncFormatter(fixed_width_formatter)) -def plot_csv_with_titles(paths_dict, x_axis, y_axis, subplot_titles, y_limits=(0, 200)): - # Adjustments for dynamic subplot creation based on the dictionary size - num_subplots = len(paths_dict) - fig, axs = plt.subplots(1, num_subplots, figsize=(4*num_subplots, 4)) + # Preparing data for the grouped bar chart in the fourth subplot + groups = [ + [paths_dict['subplot1'][0]], + [paths_dict['subplot1'][1], paths_dict['subplot1'][2], paths_dict['subplot1'][3]], # Adjusted order + [paths_dict['subplot2'][1], paths_dict['subplot2'][2], paths_dict['subplot2'][3]], # Adjusted order + [paths_dict['subplot3'][1], paths_dict['subplot3'][2], paths_dict['subplot3'][3]] # Adjusted order + ] - # If only one subplot, axs is not an array, so we need to convert it - if num_subplots == 1: - axs = [axs] + last_means = [] + last_stds = [] + colors = [] - for idx, (_, file_list) in enumerate(paths_dict.items()): - for path_, color in file_list: + for group in groups: + for path_, color, _ in group: data = np.genfromtxt(path_, delimiter=',', skip_header=0, dtype=float) - if data.shape[0]> data.shape[1]: - mean = np.mean(data, axis=1) + if data.shape[0] > data.shape[1]: + mean = np.mean(data, axis=1) + offset std = np.std(data, axis=1) else: - mean = np.mean(data, axis=0) + mean = np.mean(data, axis=0) + offset std = np.std(data, axis=0) - x = np.linspace(0, mean.shape[0], mean.shape[0]) + last_means.append(mean[-1]) + last_stds.append(std[-1]) + colors.append(color) - axs[idx].plot(x, mean, color=color) - axs[idx].fill_between( - x, - mean - 1.96 * std, - mean + 1.96 * std, - color=color, - alpha=0.5 - ) - axs[idx].set_title(subplot_titles[idx]) - axs[idx].set_xlabel(x_axis) - axs[idx].set_xlim([0, mean.shape[0]]) - axs[idx].grid(True) - axs[idx].set_ylim(y_limits) + # Plotting the grouped bar chart in the fourth subplot + bar_width = 0.2 + r = [0] + [x * bar_width + 0.5 for x in np.arange(len(groups[1]))] + \ + [x * bar_width + 1.5 for x in np.arange(len(groups[2]))] + \ + [x * bar_width + 2.5 for x in np.arange(len(groups[3]))] - # Only label the y-axis for the leftmost plot - if idx == 0: - axs[idx].set_ylabel(y_axis) - else: - axs[idx].set_ylabel('') + labels = ['BO', 'IBO-Random', 'IBO-Regular', 'IBO-Improvement', None, None, None, None, None, None] + for i in range(len(r)): + axs[3].bar(r[i], last_means[i]-ylims[0], yerr=last_stds[i], width=bar_width, + color=colors[i], capsize=5, bottom=ylims[0], label=labels[i]) + + axs[3].set_xticks([r[0], r[2], r[5], r[8]]) + axs[3].set_xticklabels(['BO', 'IBO Preference', 'IBO Shaping', 'IBO Mixture'], fontsize=label_font_size, rotation=15) + axs[3].set_xlabel('') + axs[3].spines['right'].set_visible(False) + axs[3].spines['top'].set_visible(False) + axs[3].set_ylim(ylims) + axs[3].set_ylabel('') + axs[3].set_yticklabels([]) + axs[3].text(0.5, -0.15, subplot_titles[3], horizontalalignment='center', + verticalalignment='center', transform=axs[3].transAxes, fontsize=label_font_size) + + # Display legend for the first and second groups + handles, labels = axs[3].get_legend_handles_labels() + filtered_handles = [h for h, l in zip(handles, labels) if l] + filtered_labels = [l for l in labels if l] + axs[3].legend(filtered_handles, filtered_labels, loc='upper left', frameon=False, fontsize=12) plt.tight_layout() plt.show() if __name__ == '__main__': - home_dir = os.path.expanduser('~') file_path = os.path.join(home_dir, 'Documents/IntRLResults/CP-Results') @@ -69,31 +142,58 @@ if __name__ == '__main__': filepaths = [os.path.join(file_path, filename) for filename in filenames] # Demonstrating the adjusted function with subplot titles - titles = ["Preference", "Shaping", "Combination", "Regular"] - - data_dict_colored = { - 'subplot1': [(filepaths[0], 'C0'), (filepaths[1], 'C1'), (filepaths[2], 'C2'), (filepaths[3], 'C3')], - 'subplot2': [(filepaths[0], 'C0'), (filepaths[4], 'C1'), (filepaths[5], 'C2'), (filepaths[6], 'C3')], - 'subplot3': [(filepaths[0], 'C0'), (filepaths[7], 'C1'), (filepaths[8], 'C2'), (filepaths[9], 'C3')], - 'subplot4': [(filepaths[0], 'C0'), (filepaths[2], 'C6'), (filepaths[5], 'C8'), (filepaths[8], 'C5')], - } - - plot_csv_with_titles(data_dict_colored, 'Episodes', 'Reward', titles) + titles = ["a) Preference", "b) Shaping", "c) Combination"] file_path_reacher = os.path.join(home_dir, 'Documents/IntRLResults/RE-Results') filenames_reacher = ['base_line/re-ei-random-1_0-5-1694370994_0363934.csv', - 'shaping/re-ei-random-1_0-10-1694359559_616903.csv', - 'shaping/re-ei-regular-10_0-5-1694371946_5364418.csv' + 'preference/re-pei-random-0_95-5-1694419627_105307.csv', + 'preference/re-pei-regular_outliner_free.csv', + 'preference/re-pei-improvement-0_1-5-1694416723_645404.csv', + 'shaping/re-ei-random-0_95-5-1694611010_6658554.csv', + 'shaping/re-ei-regular-10_0-5-1694371946_5364418.csv', + 'shaping/re-ei-improvement-0_1-5-1694415947_8134162.csv', + 'combination/re-pei-random-0_95-5-1694423273_9474058.csv', + 'combination/re-pei-regular_outliner_free.csv', + 'combination/re-pei-improvement-0_1-5-1694430822_181309.csv', ] filepaths_reacher = [os.path.join(file_path_reacher, filename) for filename in filenames_reacher] - titles_reacher = ["Shaping"] + titles_reacher = ["(a)", "(b)", "(c)", "(d)"] - data_dict_reacher = { - 'subplot1': [(filepaths_reacher[0], 'C0'), (filepaths_reacher[1], 'C1'), (filepaths_reacher[2], 'C2')] + data_dict_colored = { + 'subplot1': [(filepaths[0], 'C0', True), (filepaths[1], 'C3', True), (filepaths[2], 'C1', False), + (filepaths[3], 'C2', False)], + 'subplot2': [(filepaths[0], 'C0', True), (filepaths[4], 'C3', False), (filepaths[5], 'C1', True), + (filepaths[6], 'C2', False)], + 'subplot3': [(filepaths[0], 'C0', True), (filepaths[7], 'C3', False), (filepaths[8], 'C1', True), + (filepaths[9], 'C2', False)], + # 'subplot4': [(filepaths[0], 'C0'), (filepaths[2], 'C6'), (filepaths[5], 'C8'), (filepaths[8], 'C5')], } - plot_csv_with_titles(data_dict_reacher, 'Episodes', 'Reward', titles_reacher, y_limits=(-150, 50)) + # plot_csv_with_titles(data_dict_colored, 'Episodes', 'Reward', titles) + data_dict_reacher = { + 'subplot1': [(filepaths_reacher[0], 'C0', True), (filepaths_reacher[1], 'C3', False), + (filepaths_reacher[2], 'C1', True), (filepaths_reacher[3], 'C2', False)], + 'subplot2': [(filepaths_reacher[0], 'C0', True), (filepaths_reacher[4], 'C3', True), + (filepaths_reacher[5], 'C1', False), (filepaths_reacher[6], 'C2', False)], + 'subplot3': [(filepaths_reacher[0], 'C0', True), (filepaths_reacher[7], 'C3', False), + (filepaths_reacher[8], 'C1', True), (filepaths_reacher[9], 'C2', False)], + } + # plot_csv_with_titles(data_dict_reacher, 'Episodes', 'Reward', titles_reacher, y_limits=(-150, 50)) + + # data_dict_combined = {**data_dict_colored, **data_dict_reacher} + # combined_titles = titles + titles_reacher + # + # plot_csv_with_titles( + # paths_dict=data_dict_combined, + # x_axis='Episodes', + # y_axis='Rewards', + # subplot_titles=combined_titles + # ) + + modified_plot_v7(paths_dict=data_dict_colored, x_axis='Episodes', y_axis='Rewards', subplot_titles=titles_reacher) + modified_plot_v7(paths_dict=data_dict_reacher, x_axis='Episodes', y_axis='Rewards with +80 offset', subplot_titles=titles_reacher, + ylims=(-80, 150), offset=80)