In [1]:
import itertools
import os.path
import sys
sys.path.append('../plume')
from neatplots.colors import create_distinct_colors, MplLCHColor, SequentialLCPalette
from neatplots.predefined import four as palette
from neatplots.tools import SharedAxesGrid
from neatplots.visualize import plot_colors
from matplotlib.gridspec import GridSpec, SubplotSpec
import tables
%pylab inline
import latexstyle
latexstyle.setup()
In [2]:
def to_paramstr(kappa, scaling, search, rho=None):
if rho is None:
return '_kappa={kappa} scaling={scaling}_search={search!r}'.format(
kappa=kappa, scaling=scaling, search=search)
else:
return '_kappa={kappa} scaling={scaling}_search={search!r} rho={rho}'.format(
kappa=kappa, scaling=scaling, search=search, rho=rho)
def iter_trial_filenames(basename, start=0):
i = start
while True:
filename = os.path.join('..', 'Data', '{}.{}.h5'.format(basename, i))
if not os.path.isfile(filename):
if i == start:
warnings.warn("No datafiles for '{}'.".format(basename))
return
yield filename
i += 1
def extend_data(a, size):
cur_size = a.size
a = np.resize(a, size)
a[cur_size:] = a[cur_size - 1]
return a
def cached(fn):
cache = {}
def fn_with_cache(*args):
if args not in cache:
cache[args] = fn(*args)
return cache[args]
return fn_with_cache
In [3]:
@cached
def get_error_trace(experiment, paramstr, error_type, std=False):
datasets = []
for filename in iter_trial_filenames(os.path.join(experiment, paramstr)):
with tables.open_file(filename) as fileh:
try:
x = fileh.root.plume_found_step[0]
except:
if hasattr(fileh.root, 'exception'):
print fileh.root.exception[0]
else:
print 'f'
pass
d = fileh.get_node(fileh.root, error_type).read()
d /= d[0]
datasets.append(d)
if len(datasets) <= 0:
return np.ones(5000)
datasets = [extend_data(d, 5000) for d in datasets]
if std:
return np.mean(datasets, axis=0), np.std(datasets, axis=0)
else:
return np.mean(datasets, axis=0)
In [7]:
exp_labels = {'SingleSourceGaussianDispersion': 'D-SN-SS-SV', 'MultiSourceGaussianDispersion': 'D-SN-MS-SV'}
error_labels = {'rmse': 'RMISE', 'wrmse': 'WRMISE', 'rewards': 'QRSim reward'}
def plot_error_traces(kappa, experiments):
fig = plt.figure(figsize=(5.35, 3.1))
legend_height = 0.05
grid = GridSpec(
2, 1, height_ratios=(1.0 - legend_height, legend_height),
hspace=0.2, bottom=0.1, left=0.1, top=0.925, right=0.9725)
class ErrorPlotMatrix(SharedAxesGrid):
def _create_axes(self, subplot_spec, sharex, sharey):
return fig.add_subplot(subplot_spec, sharex=sharex, sharey=sharey)
def _plot(self, ax, experiment, error_type):
ax.set_title('{}, {}'.format(exp_labels[experiment], error_labels[error_type]))
ax.plot(
get_error_trace(experiment, to_paramstr(kappa, '70', 'complete'), error_type),
label='complete search', c=palette.thin[0])
ax.plot(
get_error_trace(experiment, to_paramstr(kappa, 'auto', 'complete'), error_type),
label='complete search, auto-scaled', lw=1.2, c=palette.thin[1])
ln, = ax.plot(
get_error_trace(experiment, to_paramstr(kappa, '70', 'wind'), error_type),
label='wind based search', c=palette.thin[2])
ln.set_dashes((3, 3))
ln, = ax.plot(
get_error_trace(experiment, to_paramstr(kappa, 'auto', 'wind'), error_type),
label='wind based search, auto-scaled', lw=1.2, c=palette.thin[3])
ln.set_dashes((3, 3))
error_types = ['rmse', 'wrmse', 'rewards']
p = ErrorPlotMatrix(experiments, error_types, SubplotSpec(grid, 0), hspace=0.25)
latexstyle.style_axes(p.axes)
for ax in p.axes_by_row[:-1]:
plt.setp(ax.get_xticklabels(), visible=False)
for ax in p.axes_by_col[1:]:
plt.setp(ax.get_yticklabels(), visible=False)
p.axes_by_row[-1].set_xlabel('Simulation time [s]', labelpad=0)
p.axes_by_col[0].set_ylabel('Normalized error', labelpad=0)
ax_legend = fig.add_subplot(grid[1])
ax_legend.set_axis_off()
ax_legend.legend(
*p.axes[0].get_legend_handles_labels(), ncol=2, loc='upper center',
bbox_to_anchor=(0.5, 1.0), frameon=False, columnspacing=1.5, handletextpad=0.2)
return fig
In [8]:
fig = plot_error_traces('1.25e9', ['SingleSourceGaussianDispersion', 'MultiSourceGaussianDispersion'])
In [9]:
fig.savefig('../../thesis/plots/noisy-sv.pdf')
In [5]:
def get_data(experiment, kappa, scaling, search, error_type):
rhos = ['1e-5', '1e-6', '1e-7', '1e-8', '1e-9', '1e-10', '1e-11']
errors = []
stds = []
for rho in rhos:
m, s = get_error_trace(experiment, to_paramstr(kappa, scaling, search, rho), error_type, True)
errors.append(m[-1])
stds.append(m[-1])
return [float(rho) for rho in rhos], errors, stds
fig = plt.figure(figsize=(5.35, 1.7))
legend_height = 0.2
grid = GridSpec(
2, 1, height_ratios=(1.0 - legend_height, legend_height),
hspace=0.5, bottom=0.1, left=0.1, top=0.875, right=0.9725)
class ErrorPlotMatrix(SharedAxesGrid):
def _create_axes(self, subplot_spec, sharex, sharey):
return fig.add_subplot(subplot_spec, sharex=sharex, sharey=sharey)
def _plot(self, ax, experiment, error_type):
experiment = 'MultiHeli'
kappa = '1.25e9'
rhos, mean, std = get_data(experiment, kappa, '70', 'complete', error_type)
ax.plot(rhos, mean,
label='complete search', c=palette.thin[0], marker='o')
rhos, mean, std = get_data(experiment, kappa, 'auto', 'complete', error_type)
ax.plot(rhos, mean,
label='complete search, auto-scaled', lw=1.2, c=palette.thin[1], marker='o')
rhos, mean, std = get_data(experiment, kappa, '70', 'wind', error_type)
ln, = ax.plot(rhos, mean,
label='wind based search', c=palette.thin[2], marker='o')
ln.set_dashes((3, 3))
rhos, mean, std = get_data(experiment, kappa, 'auto', 'wind', error_type)
ln, = ax.plot(rhos, mean,
label='wind based search, auto-scaled', lw=1.2, c=palette.thin[3], marker='o')
ln.set_dashes((3, 3))
ax.set_title(error_labels[error_type])
error_types = ['rmse', 'wrmse', 'rewards']
p = ErrorPlotMatrix(['MultiHeli'], error_types, SubplotSpec(grid, 0), hspace=0.25)
p.axes.semilogx()
latexstyle.style_axes(p.axes)
for ax in p.axes_by_col[1:]:
plt.setp(ax.get_yticklabels(), visible=False)
p.axes_by_row[-1].set_xlabel(r'$\rho$', labelpad=0)
p.axes_by_col[0].set_ylabel('Normalized error', labelpad=0)
p.axes.set_ylim(top=1.0)
ax_legend = fig.add_subplot(grid[1])
ax_legend.set_axis_off()
ax_legend.legend(
*p.axes[0].get_legend_handles_labels(), ncol=2, loc='upper center',
bbox_to_anchor=(0.5, 1.0), frameon=False, columnspacing=1.5, handletextpad=0.2)
Out[5]:
In [6]:
fig.savefig('../../thesis/plots/multiple-uav.pdf')
In [13]:
fig = plt.figure(figsize=(2.7, 2))
legend_height = 0.05
grid = GridSpec(
2, 1, height_ratios=(1.0 - legend_height, legend_height), hspace=0.25,
left=0.13, right=0.925, bottom=0.1, top=0.82)
ax = fig.add_subplot(grid[0])
kappa = '1.25e9'
rhos = ['1e-5', '1e-6', '1e-7', '1e-8', '1e-9', '1e-10', '1e-11']
experiment = 'MultiHeli'
error_type = 'rmse'
for rho in rhos:
ln, = ax.plot(
get_error_trace(experiment, to_paramstr(kappa, '70', 'complete', rho), error_type),
c=palette.thin[1], label=r'multiple UAV, different $\rho$')
ln.set_dashes((3, 3))
ax.plot(
get_error_trace('MultiSourceGaussianDispersion', to_paramstr(kappa, '70', 'complete'), error_type),
c=palette.thin[3], label='single UAV')
latexstyle.style_axes(ax)
ax.set_xlabel('Simulation time [s]', labelpad=2)
ax.set_ylabel('Normalized RMISE', labelpad=3)
ax_legend = fig.add_subplot(grid[1])
ax_legend.set_axis_off()
handles, labels = ax.get_legend_handles_labels()
handles = handles[-2:]
labels = labels[-2:]
ax_legend.legend(
handles, labels, ncol=2, loc='upper center',
bbox_to_anchor=(0.5, 1.0), frameon=False, columnspacing=1.5, handletextpad=0.2)
Out[13]:
In [14]:
fig.savefig('../../thesis/plots/sv-vs-mv.pdf')
In [22]:
def print_error_tbl_line(experiment, search, scaling, error_type):
if experiment == 'SingleSourceGaussianDispersion':
err_cor_path = '../Data/d-sn-ss-sv.errcor.h5'
elif experiment == 'MultiSourceGaussianDispersion':
err_cor_path = '../Data/d-sn-ms-sv.errcor.h5'
with tables.open_file(err_cor_path) as fileh:
err_cor = fileh.root.errcor.read()
paramstr = to_paramstr('1.25e9', scaling, search)
err0 = []
err_final = []
for filename in iter_trial_filenames(os.path.join(experiment, paramstr)):
with tables.open_file(filename) as fileh:
d = fileh.get_node(fileh.root, error_type).read()
err0.append(d[0])
err_final.append(d[-1])
err0 = np.asarray(err0)
err_final = np.asarray(err_final)
umean = np.mean(err_final * err_cor)
ustd = np.std(err_final * err_cor)
mean = np.mean(err_final / err0)
std = np.std(err_final / err0)
s = scaling
if s != 'auto':
s = 'constant'
print r'{search} & {scaling} & {umean:.2f} & {ustd:.2f} & {mean:.2f} & {std:.2f} \\'.format(
search=search, scaling=s, umean=umean * 1e9, ustd=ustd * 1e9, mean=mean, std=std)
In [23]:
print_error_tbl_line('SingleSourceGaussianDispersion', 'complete', '70', 'rmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'complete', 'auto', 'rmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'wind', '70', 'rmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'wind', 'auto', 'rmse')
In [24]:
print_error_tbl_line('SingleSourceGaussianDispersion', 'complete', '70', 'wrmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'complete', 'auto', 'wrmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'wind', '70', 'wrmse')
print_error_tbl_line('SingleSourceGaussianDispersion', 'wind', 'auto', 'wrmse')
In [25]:
print_error_tbl_line('MultiSourceGaussianDispersion', 'complete', '70', 'rmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'complete', 'auto', 'rmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'wind', '70', 'rmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'wind', 'auto', 'rmse')
In [26]:
print_error_tbl_line('MultiSourceGaussianDispersion', 'complete', '70', 'wrmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'complete', 'auto', 'rmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'wind', '70', 'wrmse')
print_error_tbl_line('MultiSourceGaussianDispersion', 'wind', 'auto', 'rmse')
In [ ]: