In [12]:
import cPickle
# PARAMS
log_dir = "/home/sforesti/avakas/scratch/sforestier001/logs/PLAY2017/2017-12-11_17-48-32-play_tol02"
config_list = ["PC", "C08T05", "C08T2", "C03T05", "C03T2"]
data_competence = {}
for config_name in config_list:
print config_name
filename = log_dir + '/results/' + config_name + '_competence.pickle'
with open(filename, 'r') as f:
data_competence[config_name] = cPickle.load(f)[config_name]
vocal_tol = 0.4
toy_tol = 0.2
hand_lim = 0.5 + toy_tol / 2.
tool_lim = 0.75 + toy_tol / 2.
data_vocal = {}
for config_name in config_list:
print config_name
filename = log_dir + '/results/' + config_name + '_vocal.pickle'
with open(filename, 'r') as f:
data_vocal[config_name] = cPickle.load(f)[config_name]
In [13]:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
n_trial = 50
n_goals = 100
trial_list = range(1,n_trial + 1)
In [29]:
def maps(config_name):
bins = 100
map_comp = np.zeros((bins, bins))
map_hand = np.zeros((bins, bins))
map_tool = np.zeros((bins, bins))
map_vocal = np.zeros((bins, bins))
map_succ = np.zeros((bins, bins))
map_succ_hand = np.zeros((bins, bins))
map_succ_tool = np.zeros((bins, bins))
map_succ_vocal = np.zeros((bins, bins))
tot_succ = np.zeros((bins, bins))
min_2d = [-1.25, -1.25]
max_2d = [1.25, 1.25]
def real2map(x, y):
i = int((x - min_2d[0]) / (max_2d[0] - min_2d[0]) * 100.)
j = int((y - min_2d[0]) / (max_2d[0] - min_2d[0]) * 100.)
i = max(0, min(i, 99))
j = max(0, min(j, 99))
return i, j
#trial_list = [1]
for trial in trial_list:
if data_competence[config_name][trial].has_key("eval_results"):
for region in [1, 2, 3]:
for gi in range(n_goals):
for toy in ["toy1", "toy2", "toy3"]:
# Test only toys for which the agent produced the name at least once
#print data_vocal[config_name][trial]["errors"][-1]
if data_vocal[config_name][trial]["errors"][-1][data_vocal[config_name][trial]["human_sounds"][int(toy[-1]) - 1]] < vocal_tol:
x, y = data_competence[config_name][trial]["eval_results"][region][gi][toy]["toy_pos"]
i, j = real2map(x, y)
map_comp[i, j] += data_competence[config_name][trial]["eval_results"][region][gi][toy]["comp_error"]
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["arm_dist"] < data_competence[config_name][trial]["eval_results"][region][gi][toy]["diva_dist"]:
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["tool"]:
map_tool[i, j] += 1
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["reached"]:
map_succ_tool[i, j] += 1
else:
map_hand[i, j] += 1
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["reached"]:
map_succ_hand[i, j] += 1
else:
map_vocal[i, j] += 1
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["reached"]:
map_succ_vocal[i, j] += 1
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["reached"]:
map_succ[i, j] += 1
tot_succ[i, j] += 1
for i in range(bins):
for j in range(bins):
map_succ_hand[i, j] = float(map_succ_hand[i, j]) / map_hand[i, j]
map_succ_tool[i, j] = float(map_succ_tool[i, j]) / map_tool[i, j]
map_succ_vocal[i, j] = float(map_succ_vocal[i, j]) / map_vocal[i, j]
map_succ[i, j] = float(map_succ[i, j]) / tot_succ[i, j]
for i in range(bins):
for j in range(bins):
n = map_hand[i, j] + map_tool[i, j] + map_vocal[i, j]
if n > 0:
map_comp[i, j] /= n
else:
map_comp[i, j] = -1
map_total = map_hand + map_tool + map_vocal
for i in range(bins):
for j in range(bins):
if map_total[i, j]:
map_hand[i, j] /= map_total[i, j]
map_tool[i, j] /= map_total[i, j]
map_vocal[i, j] /= map_total[i, j]
if (i - bins/2.)**2. + (j - bins/2.)**2. > bins*bins/4.:
map_hand[i, j] = -1
map_tool[i, j] = -1
map_vocal[i, j] = -1
x_points = np.linspace(-1., 1., bins)
y_points = np.linspace(-1., 1., bins)
pcol = plt.pcolormesh(x_points, y_points, map_comp, vmin=0, vmax=3., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
cb = plt.colorbar()
#cb.ax.set_yticklabels(["Tool", "", "", "", "", "", "", "", "", "", "Hand"], fontsize = 30)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
# GET MOTOR BABBLING ERROR MAP
import cPickle
filename = '../data/motor_babbling.pickle'
with open(filename, 'r') as f:
map_comp_mb_avg = cPickle.load(f)
f.close()
pcol = plt.pcolormesh(x_points, y_points, map_comp_mb_avg, vmin=0, vmax=3., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
cb = plt.colorbar()
#cb.ax.set_yticklabels(["Tool", "", "", "", "", "", "", "", "", "", "Hand"], fontsize = 30)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
plt.clf()
# NORMALIZE COMPETENCE MAP WITH MOTOR BABBLING MAP
map_comp_norm = np.zeros((bins, bins))
for i in range(bins):
for j in range(bins):
if map_comp[i, j] < 0.:
map_comp_norm[i, j] = -1.
elif np.isnan(map_comp_mb_avg[i, j]):
map_comp_norm[i, j] = 200.
else:
map_comp_norm[i, j] = max(0., 1. - map_comp[i, j] / map_comp_mb_avg[i, j])
# the few missing points become avg of neighbors
for i in range(bins-1):
for j in range(bins-1):
if map_comp_norm[i, j] == -1. and (i-bins/2.) * (i-bins/2.) + (j-bins/2.) * (j-bins/2.) < bins * bins / 4.:
map_comp_norm[i, j] = np.mean([map_comp_norm[i+1, j],
map_comp_norm[i-1, j],
map_comp_norm[i, j+1],
map_comp_norm[i, j-1]])
x_points = np.linspace(-1., 1., bins)
y_points = np.linspace(-1., 1., bins)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
pcol = plt.pcolormesh(x_points, y_points, 100.* map_comp_norm, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('grey')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_competence_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
#print np.mean(map_comp_norm[map_comp_norm>0.])
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_hand, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_strategy_hand_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_succ_hand, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_success_hand_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_succ_tool, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_success_tool_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_succ_vocal, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_success_vocal_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_tool, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_strategy_tool_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
pcol = plt.pcolormesh(x_points, y_points, 100.* map_vocal, vmin=0., vmax=100., linewidth=0, cmap="jet")
pcol.set_rasterized(True)
pcol.set_edgecolor('face')
pcol.cmap.set_under('white')
pcol.cmap.set_over('white')
cb = plt.colorbar()
cb.ax.set_yticklabels(["$0\%$", "", "$20\%$", "", "$40\%$", "", "$60\%$", "", "$80\%$", "", "$100\%$"], fontsize = 16)
cb.solids.set_rasterized(True)
cb.solids.set_edgecolor("face")
ax = plt.axes()
ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(hand_lim * np.sin(theta), hand_lim * np.cos(theta), 'k', lw=3)
plt.plot(tool_lim * np.sin(theta), tool_lim * np.cos(theta), 'k', lw=3)
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.axhline(-1, color='k')
plt.axvline(-1, color='k')
plt.axhline(1, color='k')
plt.axvline(1, color='k')
plt.savefig('../figs/fig_strategy_vocal_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
mean_hand = np.zeros(50)
count_hand = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50:
mean_hand[d] += map_hand[i, j]
count_hand[d] += 1
mean_hand = mean_hand / count_hand
mean_tool = np.zeros(50)
count_tool = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50:
mean_tool[d] += map_tool[i, j]
count_tool[d] += 1
mean_tool = mean_tool / count_tool
mean_vocal = np.zeros(50)
count_vocal = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50:
mean_vocal[d] += map_vocal[i, j]
count_vocal[d] += 1
mean_vocal = mean_vocal / count_vocal
#print "hand", 100.*mean_hand
#print "tool", 100.*mean_tool
#print "vocal", 100.*mean_vocal
mean_total = mean_hand + mean_tool + mean_vocal
mean_hand /= mean_total
mean_tool /= mean_total
mean_vocal /= mean_total
x = np.linspace(0., max_2d[0], 50)
plt.plot(x, 100.*mean_hand, "r", lw=3, label="Hand")
plt.plot(x, 100.*mean_tool, "g", lw=3, label="Tool")
plt.plot(x, 100.*mean_vocal, "b", lw=3, label="Vocal")
#print mean_total
#print mean_hand[-1] + mean_tool[-1] + mean_vocal[-1]
#print "hand", 100.*mean_hand
#print "tool", 100.*mean_tool
#print "vocal", 100.*mean_vocal
legend = plt.legend(frameon=True, fontsize=20)
plt.xlabel("Distance to center", fontsize=20)
plt.ylabel("Strategy Use (\%)", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
frame = legend.get_frame()
frame.set_facecolor('1.')
frame.set_edgecolor('0.')
plt.plot((hand_lim, hand_lim), (0, 100), 'k--', lw=2)
plt.plot((tool_lim, tool_lim), (0, 100), 'k--', lw=2)
plt.savefig('../figs/fig_strategy_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
plt.clf()
succ_hand = np.zeros(50)
count_hand = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50 and not np.isnan(map_succ_hand[i, j]):
succ_hand[d] += map_succ_hand[i, j]
count_hand[d] += 1
succ_hand = succ_hand / count_hand
succ_tool = np.zeros(50)
count_tool = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50 and not np.isnan(map_succ_tool[i, j]):
succ_tool[d] += map_succ_tool[i, j]
count_tool[d] += 1
succ_tool = succ_tool / count_tool
succ_vocal = np.zeros(50)
count_vocal = np.zeros(50)
for i in range(bins):
for j in range(bins):
d = int(np.sqrt((i-bins/2)*(i-bins/2) + (j-bins/2)*(j-bins/2)))
if d < 50 and not np.isnan(map_succ_vocal[i, j]):
succ_vocal[d] += map_succ_vocal[i, j]
count_vocal[d] += 1
succ_vocal = succ_vocal / count_vocal
x = np.linspace(0., max_2d[0], 50)
print map_succ_hand
print map_succ_tool
print map_succ_tool
print succ_hand
print succ_tool
print succ_vocal
plt.plot(x, 100.*succ_hand, "r", lw=3, label="Hand")
plt.plot(x, 100.*succ_tool, "g", lw=3, label="Tool")
plt.plot(x, 100.*succ_vocal, "b", lw=3, label="Vocal")
legend = plt.legend(frameon=True, fontsize=20)
plt.xlabel("Distance to center", fontsize=20)
plt.ylabel("Strategy success (\%)", fontsize=20)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
frame = legend.get_frame()
frame.set_facecolor('1.')
frame.set_edgecolor('0.')
plt.plot((hand_lim, hand_lim), (0, 100), 'k--', lw=2)
plt.plot((tool_lim, tool_lim), (0, 100), 'k--', lw=2)
plt.savefig('../figs/fig_success_' + config_name.replace(".", "") + '.pdf', format='pdf', bbox_inches='tight')
In [30]:
maps("PC")
In [ ]:
maps("C03T05")
In [ ]:
maps("C03T2")
In [ ]:
maps("C08T05")
In [ ]:
maps("C08T2")
In [15]:
import seaborn as sns
from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
rgb = np.zeros((100, 100, 3))
rgb[:,:,0] = map_hand
rgb[:,:,1] = map_tool
rgb[:,:,2] = map_vocal
#rgb = 1. - rgb
rgb[rgb < 0.] = 1.
plt.figure()
plt.imshow(rgb,interpolation='none')
#for i in range(100):
# for j in range(100):
# rgb[i,j,:] = rgb[i,j,:] / max(rgb[i,j,:])
print map_hand[50,50]
print map_tool[50,50]
print map_vocal[50,50]
print rgb[50,50]
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(50. + 65. * np.sin(theta)/2., 50. + 65. * np.cos(theta)/2, 'k', lw=3)
plt.plot(50. + 85 * np.sin(theta)/2., 50. + 85 * np.cos(theta)/2., 'k', lw=3)
sns.set_style("whitegrid", {'axes.grid' : False})
plt.xlabel("X", fontsize=20)
plt.ylabel("Y", fontsize=20)
plt.xlim([0, 100])
plt.ylim([0, 100])
plt.xticks([0, 25, 50, 75, 100], ["$-1$", "$-0.5$", "$0$", "$0.5$", "$1$"], fontsize = 16)
plt.yticks([0, 25, 50, 75, 100], ["$-1$", "$-0.5$", "$0$", "$0.5$", "$1$"], fontsize = 16)
plt.axhline(0, color='k')
plt.axvline(0, color='k')
plt.axhline(100, color='k')
plt.axvline(100, color='k')
plt.savefig('../figs/fig_strategy.pdf', format='pdf', bbox_inches='tight')
In [47]:
config_name = "RMB2"
region = 3
reached = 0
for trial in range(1, 100):
for gi in range(100):
for toy in ["toy1", "toy2", "toy3"]:
if data_competence[config_name][trial]["eval_results"][region][gi][toy]["reached"]:
reached += 1
print reached