Skip to content

Commit

Permalink
plots are not pdf with font type 42
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Marjaninejad authored and Ali Marjaninejad committed Sep 13, 2019
1 parent 5198dc2 commit 18353af
Show file tree
Hide file tree
Showing 36 changed files with 174 additions and 28 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions all_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from datetime import datetime
from copy import deepcopy
from mujoco_py.generated import const
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
################################################
#Functions for main tests
def learn_to_move_2_fcn(MuJoCo_model_name, model, cum_kinematics, cum_activations, reward_thresh=6, energy_cost_weight=0, refinement = False, Mj_render = False):
Expand Down
16 changes: 8 additions & 8 deletions experiment_1_show_errorresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
errors_all = np.load(file_address)
errors_all_mean=errors_all.mean(1)
errors_all_std = errors_all.std(1)
print("errors_mean: ",errors_all_mean)
print("errors_std: ",errors_all_std)
print("Errors_mean: ",errors_all_mean)
print("Errors_std: ",errors_all_std)
p0_0 = axes[0].boxplot(
errors_all.mean(0).reshape(1,-1,1,N).squeeze(),
positions=positions_cyclical,
Expand Down Expand Up @@ -69,9 +69,9 @@
notch=True,
patch_artist=True,
showfliers=False)
axes[0].set_title(r'average across both joints',fontsize=10)
axes[0].set_title(r'Average across both joints',fontsize=10)
axes[0].set_ylim(y_lim_p0)
axes[0].set_xlabel('stiffness (N/m)')
axes[0].set_xlabel('Stiffness (N/m)')
axes[0].set_xticklabels(stiffness_values, rotation=45, fontsize=8)
axes[0].set_ylabel('RMSE')

Expand All @@ -81,10 +81,10 @@
notch=True,
patch_artist=True,
showfliers=False)
axes[1].set_title('proximal joint ($q_0$)', fontsize=10)
axes[1].set_title('Proximal joint ($q_0$)', fontsize=10)
axes[1].set_ylim(y_lim)
axes[1].set_yticklabels([])
axes[1].set_xlabel('stiffness (N/m)')
axes[1].set_xlabel('Stiffness (N/m)')
axes[1].set_xticklabels(stiffness_values, rotation=45, fontsize=8)

p1_2 = axes[2].boxplot(
Expand All @@ -93,10 +93,10 @@
notch=True,
patch_artist=True,
showfliers=False)
axes[2].set_title('distal joint ($q_1$)', fontsize=10)
axes[2].set_title('Distal joint ($q_1$)', fontsize=10)
axes[2].set_ylim(y_lim)
axes[2].set_yticklabels([])
axes[2].set_xlabel('stiffness (N/m)')
axes[2].set_xlabel('Stiffness (N/m)')
axes[2].set_xticklabels(stiffness_values, rotation=45, fontsize=8)

# changing the box colors
Expand Down
20 changes: 13 additions & 7 deletions experiment_1_show_learningrates.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@
learning_errors_per_stiffness = np.zeros([stiffness_versions, epoch_numbers])
for stiffness_ver in range(stiffness_versions):
learning_errors_per_stiffness[stiffness_ver,:] = learning_errors[:, :, stiffness_ver, :].mean(0).squeeze()
axes.plot(np.arange(1,epoch_numbers_to_show+1), learning_errors_per_stiffness[stiffness_ver,:epoch_numbers_to_show],color=colorsys.hsv_to_rgb((8-stiffness_ver)/14,1,.75), alpha=.55, linewidth=2.2)
axes.plot(np.arange(1,epoch_numbers_to_show+1), learning_errors_per_stiffness[stiffness_ver,:epoch_numbers_to_show],
color=colorsys.hsv_to_rgb((8.5-stiffness_ver)/14,1,.75), alpha=.65, linewidth=3.0)
#(50, 1, 9, 20)
plt.sca(axes)
plt.legend(['S: 0', 'S: 500', 'S: 1K', 'S: 2K', 'S: 4K', 'S: 7K', 'S: 10K', 'S: 15K', 'S: 20K'])
plt.xticks(np.arange(2,epoch_numbers_to_show+1,2), np.arange(2,epoch_numbers_to_show+1,2))
#plt.ylim([0.035,0.1])
plt.legend(['S: 0', 'S: 500', 'S: 1K', 'S: 2K', 'S: 4K', 'S: 7K', 'S: 10K', 'S: 15K', 'S: 20K'],fontsize='small')
plt.xticks(np.arange(1,epoch_numbers_to_show+1,1), np.arange(1,epoch_numbers_to_show+1,1),
rotation=45, fontsize=8)

plt.xlabel("epoch #")
plt.xlabel("Epoch #", fontsize=9)
#axes.set_xlabel("epoch #")
plt.ylabel("epoch MSE")
plt.title("learning curves for different stiffness values (S)")
fig.savefig('./results/{}/learningcurves.png'.format(experiment_ID))
plt.ylabel("Epoch MSE", fontsize=9)
plt.yticks(np.arange(0.035,0.096,0.005),rotation=45, fontsize=8)
plt.title("Learning curves for different stiffness values (S)", fontsize=10)
plt.grid()
fig.savefig('./results/{}/exp1_learningcurves.pdf'.format(experiment_ID))
fig.savefig('./results/figures/exp1_learningcurves.pdf'.format(experiment_ID))
plt.show()


Expand Down
6 changes: 3 additions & 3 deletions experiment_2_run_2way.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
simplefilter(action='ignore', category=FutureWarning)

experiment_ID = "experiment_2_2way"
mc_run_number = 2
mc_run_number = 50
babbling_time = 3
number_of_refinements = 5
errors_all_cyc_A_A = np.zeros([2, number_of_refinements+1, mc_run_number])
Expand Down Expand Up @@ -173,8 +173,8 @@

np.random.seed(random_seed) # change the seed for different initial conditions
tf.random.set_random_seed(random_seed)
model_B_refined_B_p2p = copy_model_fcn(original_model=model_B_babble)
[model_B_refined_B_p2p, errors, cum_kinematics_B_refined, cum_activations_B_refined] =\
model_B_refined_A_p2p = copy_model_fcn(original_model=model_B_babble)
[model_B_refined_A_p2p, errors, cum_kinematics_A_refined, cum_activations_A_refined] =\
p2p_run_fcn(
MuJoCo_model_name=MuJoCo_model_name_A,
model=model_B_refined_A_cyc,
Expand Down
11 changes: 6 additions & 5 deletions experiment_2_show_learningrates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from all_functions import *
import pickle
from warnings import simplefilter
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
matplotlib.rcParams['mathtext.fontset'] = 'cm'

def exp2_learning_curves_cal_fcn(errors_all):
average_curve_mean = errors_all.mean(0).mean(1)
Expand All @@ -18,10 +22,6 @@ def exp2_learning_curves_cal_fcn(errors_all):

simplefilter(action='ignore', category=FutureWarning)

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

experiment_ID = "experiment_2"
number_of_refinements = 5
errors_all_cyc_A_A = np.load("./results/{}/errors_all_cyc_A_A.npy".format(experiment_ID))
Expand Down Expand Up @@ -99,6 +99,7 @@ def exp2_learning_curves_cal_fcn(errors_all):
axes[subplot_iter].set_ylim(y_lim)
axes[subplot_iter].legend(['A_A','A_B','B_B'], fontsize=6)
fig.subplots_adjust(top=.9, bottom=.2, left=.06, right=.95)
fig.savefig('./results/{}/learningcurves.png'.format(experiment_ID))
fig.savefig('./results/{}/exp2_learningcurves.pdf'.format(experiment_ID))
fig.savefig('./results/figures/exp2_learningcurves.pdf'.format(experiment_ID))
plt.show()
#import pdb; pdb.set_trace()
14 changes: 9 additions & 5 deletions experiment_3_pool_showresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from warnings import simplefilter

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
matplotlib.rcParams['mathtext.fontset'] = 'cm'

plt.rcParams['svg.fonttype'] = 'none'
def calculate_mean_std(data, method='mean'):
stiffness_versions = data.shape[0]
output = np.zeros(stiffness_versions)
Expand All @@ -23,8 +27,7 @@ def calculate_mean_std(data, method='mean'):
raise NameError('invalid method: please use mean or std')
return output

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42


experiment_ID = "experiment_3_pool_G"

Expand Down Expand Up @@ -96,8 +99,8 @@ def calculate_mean_std(data, method='mean'):
#axes[1].set_xticklabels(stiffness_values, rotation=45, fontsize=8)
stiffness_values = ["0", "500", "1K", "2K", "4K", "7K", "10K", "15K", "20K"]
#titles = []
xlabels = ['stiffness']*3
ylabels = ['success rate (%)', 'reward', 'energy']
xlabels = ['Stiffness']*3
ylabels = ['Success rate (%)', 'Reward', 'Energy']

for ii in range(ncols):
plt.sca(axes[ii])
Expand All @@ -106,7 +109,8 @@ def calculate_mean_std(data, method='mean'):
plt.ylabel(ylabels[ii], fontsize=9)
plt.yticks(rotation=45, fontsize=8)
fig.subplots_adjust(top=.9, bottom=.2, left=.06, right=.95, wspace=.30)
fig.savefig('./results/{}/learn2walk_results.png'.format(experiment_ID))
fig.savefig('./results/{}/exp3_learn2walk_results.pdf'.format(experiment_ID))
fig.savefig('./results/figures/exp3_learn2walk_results.pdf'.format(experiment_ID))
# p0 = axes[1].boxplot(
# [errors_all_cyc_A_A.mean(0)[0], errors_all_cyc_A_B.mean(0)[0], errors_all_cyc_B_B.mean(0)[0]],
# notch=True,
Expand Down
132 changes: 132 additions & 0 deletions experiment_4_pool_show_withMC_singlefigure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import json
import numpy as np
from scipy import signal, stats
from matplotlib import pyplot as plt
import colorsys
import pickle
from warnings import simplefilter
import os

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
matplotlib.rcParams['mathtext.fontset'] = 'cm'

extended_view = False
stiffness_versions = 9
if extended_view:
select_stiffness = range(stiffness_versions)
else:
select_stiffness = np.array([0, 2, 4, 6, 8])
RL_method = "PPO1"
total_MC_runs = 50 # starts from 1
experiment_ID = "experiment_4_pool_with_MC_C"
total_timesteps = 500000
episode_timesteps = 1000
total_episodes = int(total_timesteps/episode_timesteps)

episode_rewards_all = np.zeros([total_MC_runs, stiffness_versions, total_episodes])

for stiffness_value in range(stiffness_versions):
stiffness_value_str = "stiffness_{}".format(stiffness_value)
for mc_cntr in range(total_MC_runs):
log_dir = "./logs/{}/MC_{}/{}/{}/".format(experiment_ID, mc_cntr, RL_method, stiffness_value_str)
jsonFile = open(log_dir+"monitor/openaigym.episode_batch.{}.Monitor_info.stats.json".format(0))
jsonString = jsonFile.read()
jsonData = json.loads(jsonString)
print("stiffness_value: ", stiffness_value, "mc_cntr: ", mc_cntr)
episode_rewards_all[mc_cntr, stiffness_value, :] = np.array(jsonData['episode_rewards'])
reward_to_displacement_coeficient = .01
episode_displacement_all = episode_rewards_all*reward_to_displacement_coeficient
episode_displacement_average = episode_displacement_all.mean(0)
episode_displacement_std = episode_displacement_all.std(0)
final_displacement = np.zeros([total_MC_runs, stiffness_versions])
pass_displacement_point = np.zeros([total_MC_runs ,stiffness_versions])
displacement_point = 9

#fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(4.5, 3))
plt.figure(figsize=(9,3))
plt.subplot(121)
stiffness_values_full = ["0", "500", "1K", "2K", "4K", "7K", "10K", "15K", "20K"]
stiffness_values_legend_full = ["S: 0", "S: 500", "S: 1K", "S: 2K", "S: 4K", "S: 7K", "S: 10K", "S: 15K", "S: 20K"]

if extended_view:
stiffness_values = stiffness_values_full
stiffness_values_legend = stiffness_values_legend_full
else:
stiffness_values = ["0", "1K", "4K", "10K", "20K"]
stiffness_values_legend = ["S: 0", "S: 1K", "S: 4K", "S: 10K", "S: 20K"]
## Figure 1
for stiffness_value in select_stiffness:
x0=range(total_episodes)
y0=episode_displacement_average[stiffness_value,:]
std0 = episode_displacement_std[stiffness_value,:]
plt.plot(x0, y0, color=colorsys.hsv_to_rgb((8.5-stiffness_value)/14,1,.75), alpha=.75)
plt.fill_between(x0, y0-std0/2, y0+std0/2,
color=colorsys.hsv_to_rgb((8.5-stiffness_value)/14,1,.75), alpha=0.20)
plt.legend(stiffness_values_legend, fontsize='x-small',loc='lower right')
plt.xlabel('Episode #', fontsize=8)
plt.ylabel('Displacement (m)', fontsize=8)
plt.xticks(np.arange(0,501,50),rotation=45, fontsize=8)
plt.yticks(rotation=45, fontsize=8)
plt.title('a) Learning curves: reward vs. episode plots', fontsize=8)
plt.grid()


for stiffness_value in range(stiffness_versions):
final_displacement[:,stiffness_value] = episode_displacement_all[:,stiffness_value,-1]
for mc_cntr in range(total_MC_runs):
pass_displacement_point[mc_cntr, stiffness_value] = np.min(np.where(episode_displacement_all[mc_cntr, stiffness_value,:]>=displacement_point))

#fig.subplots_adjust(top=.98, bottom=.15, left=.13, right=.95, wspace=.33)
#fig.savefig('./results/{}/PPO_results_1.png'.format(experiment_ID))
## Figure 2
plt.subplot(143)
x1=range(stiffness_versions)
y1 = pass_displacement_point.mean(0)
std1 = pass_displacement_point.std(0)
plt.plot(x1, y1, '-')
plt.fill_between(x1, y1-std1/2, y1+std1/2, alpha=0.25, edgecolor='C9', facecolor='C9')
for stiffness_value in range(stiffness_versions):
plt.plot(x1[stiffness_value], y1[stiffness_value], 'o',alpha=.9, color=colorsys.hsv_to_rgb((8.5-stiffness_value)/14,1,.75))
plt.xlabel('Stiffness (N/M)', fontsize=8)
plt.ylabel('Episode #', fontsize=8)
plt.xticks(range(stiffness_versions), stiffness_values_full, rotation=45, fontsize=8)
plt.yticks(rotation=45, fontsize=8)
plt.title('b) Passing threshold episode', fontsize=8)
plt.grid()
## Figure 3
plt.subplot(144)
x2=range(stiffness_versions)
y2 = final_displacement.mean(0)
std2 = final_displacement.std(0)
plt.plot(x2, y2, '-')
plt.fill_between(x2, y2-std2/2, y2+std2/2, alpha=0.25, edgecolor='C9', facecolor='C9')
for stiffness_value in range(stiffness_versions):
plt.plot(x2[stiffness_value], y2[stiffness_value], 'o',alpha=.9, color=colorsys.hsv_to_rgb((8.5-stiffness_value)/14,1,.75))
plt.xlabel('Stiffness (N/M)', fontsize=8)
plt.ylabel('Displacement (m)', fontsize=8)
plt.xticks(range(stiffness_versions), stiffness_values_full, rotation=45, fontsize=8)
plt.yticks(rotation=45, fontsize=8)
plt.title('c) Average final rewards', fontsize=8)
plt.grid()
# for ii in range(ncols):
# plt.sca(axes[ii])
# plt.xlabel(xlabels[ii], fontsize=9)
# plt.xticks(x1, stiffness_values_full, rotation=45, fontsize=8)
# # plt.ylabel(ylabels[ii], fontsize=9)
# # plt.yticks(rotation=45, fontsize=8)
# fig.subplots_adjust(top=.95, bottom=.17, left=.11, right=.95, wspace=.33)
# fig.savefig('./results/{}/PPO_results_2.png'.format(experiment_ID))
# p0 = axes[1].boxplot(
# [errors_all_cyc_A_A.mean(0)[0], errors_all_cyc_A_B.mean(0)[0], errors_all_cyc_B_B.mean(0)[0]],
# notch=True,
# patch_artist=True)
os.makedirs("./results/{}".format(experiment_ID), exist_ok=True)
plt.subplots_adjust(left=0.06, bottom=0.16, right=0.95, top=0.92, wspace=0.32)
plt.savefig("./results/{}/exp4_PPO_results_combined.pdf".format(experiment_ID))
plt.savefig("./results/figures/exp4_PPO_results_combined.pdf".format(experiment_ID))
plt.show()


#import pdb; pdb.set_trace()
Binary file modified results/.DS_Store
Binary file not shown.
Binary file modified results/experiment_1B/exp1_error_vs_stiffness.pdf
Binary file not shown.
Binary file added results/experiment_1B/exp1_learningcurves.pdf
Binary file not shown.
Binary file added results/experiment_1B/learningcurves.pdf
Binary file not shown.
Binary file modified results/experiment_1B/learningcurves.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added results/experiment_2/exp2_learningcurves.pdf
Binary file not shown.
Binary file added results/experiment_2/learningcurves.pdf
Binary file not shown.
Binary file modified results/experiment_2/learningcurves.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed results/experiment_2_2way/errors_all_cyc_A_A.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_cyc_A_B.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_cyc_B_A.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_cyc_B_B.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_p2p_A_A.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_p2p_A_B.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_p2p_B_A.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/errors_all_p2p_B_B.npy
Binary file not shown.
Binary file removed results/experiment_2_2way/learningcurves.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added results/figures/.DS_Store
Binary file not shown.
Binary file added results/figures/MuJoCo_env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified results/figures/exp1_error_vs_stiffness.pdf
Binary file not shown.
Binary file added results/figures/exp1_learningcurves.pdf
Binary file not shown.
Binary file added results/figures/exp2_learningcurves.pdf
Binary file not shown.
Binary file added results/figures/exp3_learn2walk_results.pdf
Binary file not shown.
Binary file added results/figures/exp4_PPO_results_combined.pdf
Binary file not shown.

0 comments on commit 18353af

Please sign in to comment.