286 lines
8.9 KiB
Python
286 lines
8.9 KiB
Python
from pyparsing import alphanums
|
|
|
|
import plotstyle_plt
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from matplotlib.transforms import BboxTransformTo
|
|
from itertools import product
|
|
from thunderhopper.filetools import search_files
|
|
from thunderhopper.modeltools import load_data
|
|
from color_functions import load_colors
|
|
from plot_functions import hide_axis, ylimits, xlabel, ylabel, plot_line, plot_barcode, strip_zeros
|
|
from IPython import embed
|
|
|
|
def time_bar(ax, dur, y0=0.9, y1=0.95, xshift=0.5, parent=None, transform=None, **kwargs):
|
|
t0, t1 = ax.get_xlim()
|
|
offset = (t1 - t0 - dur) * xshift
|
|
x0 = t0 + offset
|
|
x1 = x0 + dur
|
|
if parent is None:
|
|
parent = ax
|
|
if transform is None:
|
|
transform = BboxTransformTo(parent.bbox)
|
|
if transform is not ax.transData:
|
|
trans = ax.transData + transform.inverted()
|
|
x0 = trans.transform((x0, 0))[0]
|
|
x1 = trans.transform((x1, 0))[0]
|
|
parent.add_artist(plt.Rectangle((x0, y0), x1 - x0, y1 - y0,
|
|
transform=transform, **kwargs))
|
|
return None
|
|
|
|
def add_snip_axes(fig, grid_kwargs):
|
|
grid = fig.add_gridspec(**grid_kwargs)
|
|
axes = np.zeros((grid.nrows, grid.ncols), dtype=object)
|
|
for i, j in product(range(grid.nrows), range(grid.ncols)):
|
|
axes[i, j] = fig.add_subplot(grid[i, j])
|
|
[hide_axis(ax, 'left') for ax in axes.flatten()]
|
|
[hide_axis(ax, 'bottom') for ax in axes.flatten()]
|
|
return axes
|
|
|
|
def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
|
ymin, ymax = ylimits(snippets, minval=ymin, maxval=ymax, pad=0.05)
|
|
for ax, snippet in zip(axes, snippets.T):
|
|
plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs)
|
|
return None
|
|
|
|
def plot_bi_snippets(axes, time, binary, **kwargs):
|
|
for ax, binary in zip(axes, binary.T):
|
|
plot_barcode(ax, time, binary[:, None], **kwargs)
|
|
return None
|
|
|
|
|
|
# GENERAL SETTINGS:
|
|
target = 'Omocestus_rufipes'
|
|
data_paths = search_files(target, excl='noise', dir='../data/inv/thresh_lp/')
|
|
stages = ['conv', 'bi', 'feat']
|
|
files = stages + ['scales', 'example_scales', 'measure_conv', 'spread_conv',
|
|
'measure_feat', 'spread_feat']
|
|
save_path = '../figures/fig_invariance_thresh_lp.pdf'
|
|
|
|
# GRAPH SETTINGS:
|
|
fig_kwargs = dict(
|
|
figsize=(32/2.54, 16/2.54),
|
|
)
|
|
super_grid_kwargs = dict(
|
|
nrows=2,
|
|
ncols=2,
|
|
wspace=0,
|
|
hspace=0,
|
|
left=0,
|
|
right=1,
|
|
bottom=0,
|
|
top=1
|
|
)
|
|
subfig_specs = dict(
|
|
pure=(0, 0),
|
|
noise=(1, 0),
|
|
analysis=(slice(None), 1)
|
|
)
|
|
pure_grid_kwargs = dict(
|
|
nrows=len(stages),
|
|
ncols=None,
|
|
wspace=0.05,
|
|
hspace=0.1,
|
|
left=0.13,
|
|
right=0.95,
|
|
bottom=0.15,
|
|
top=0.9
|
|
)
|
|
noise_grid_kwargs = dict(
|
|
nrows=len(stages),
|
|
ncols=None,
|
|
wspace=0.05,
|
|
hspace=0.1,
|
|
left=0.13,
|
|
right=0.95,
|
|
bottom=0.15,
|
|
top=0.9
|
|
)
|
|
analysis_grid_kwargs = dict(
|
|
nrows=1,
|
|
ncols=1,
|
|
wspace=0,
|
|
hspace=0,
|
|
left=0.15,
|
|
right=0.96,
|
|
bottom=0.1,
|
|
top=0.95
|
|
)
|
|
snip_specs = dict(
|
|
conv=(0, slice(None)),
|
|
bi=(1, slice(None)),
|
|
feat=(2, slice(None))
|
|
)
|
|
|
|
# PLOT SETTINGS:
|
|
colors = load_colors('../data/stage_colors.npz')
|
|
lw_snippets = 0.5
|
|
lw_analysis = 3
|
|
xlabels = dict(
|
|
analysis='scale $\\alpha$',
|
|
)
|
|
xlab_analysis_kwargs = dict(
|
|
y=0.01,
|
|
fontsize=16,
|
|
ha='center',
|
|
va='bottom',
|
|
)
|
|
ylabels = dict(
|
|
conv='$c_i$',
|
|
bi='$b_i$',
|
|
feat='$f_i$',
|
|
analysis='ratio $\\text{SD}_{\\alpha}\\,/\\,\\text{SD}_{\\min[\\alpha]}$',
|
|
# analysis='ratio $\\sigma_{\\alpha}\\,/\\,\\sigma_{\\min[\\alpha]}$',
|
|
)
|
|
ylab_snip_kwargs = dict(
|
|
x=0.01,
|
|
fontsize=20,
|
|
rotation=0,
|
|
ha='left',
|
|
va='center',
|
|
)
|
|
ylab_analysis_kwargs = dict(
|
|
x=0.02,
|
|
fontsize=16,
|
|
ha='center',
|
|
va='top',
|
|
)
|
|
xloc = dict(
|
|
analysis=10,
|
|
)
|
|
letter_snip_kwargs = dict(
|
|
x=0.02,
|
|
y=1,
|
|
ha='left',
|
|
va='top',
|
|
fontsize=22,
|
|
fontweight='bold'
|
|
)
|
|
letter_analysis_kwargs = dict(
|
|
x=0,
|
|
y=1,
|
|
ha='left',
|
|
va='top',
|
|
fontsize=22,
|
|
fontweight='bold'
|
|
)
|
|
bar_time = 5
|
|
bar_kwargs = dict(
|
|
y0=0.5,
|
|
y1=0.6,
|
|
color='k',
|
|
lw = 0,
|
|
)
|
|
spread_kwargs = dict(
|
|
alpha=0.3,
|
|
lw=0,
|
|
zorder=0
|
|
)
|
|
kernel_ind = 0
|
|
|
|
# EXECUTION:
|
|
for data_path in data_paths:
|
|
print(f'Processing {data_path}')
|
|
|
|
# Load invariance data:
|
|
pure_data, config = load_data(data_path, files)
|
|
noise_data, _ = load_data(data_path.replace('.npz', '_noise.npz'), files)
|
|
t_full = np.arange(pure_data['conv'].shape[0]) / config['env_rate']
|
|
|
|
# Reduce snippet data to kernel subset:
|
|
pure_data['conv'] = pure_data['conv'][:, kernel_ind]
|
|
pure_data['bi'] = pure_data['bi'][:, kernel_ind]
|
|
pure_data['feat'] = pure_data['feat'][:, kernel_ind]
|
|
noise_data['conv'] = noise_data['conv'][:, kernel_ind]
|
|
noise_data['bi'] = noise_data['bi'][:, kernel_ind]
|
|
noise_data['feat'] = noise_data['feat'][:, kernel_ind]
|
|
|
|
# Prepare overall graph:
|
|
fig = plt.figure(**fig_kwargs)
|
|
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
|
|
|
# Prepare pure-song snippet axes:
|
|
pure_subfig = fig.add_subfigure(super_grid[subfig_specs['pure']])
|
|
pure_grid_kwargs['nrows' if pure_grid_kwargs['nrows'] is None else 'ncols'] = pure_data['example_scales'].size
|
|
pure_axes = add_snip_axes(pure_subfig, pure_grid_kwargs)
|
|
for ax, stage in zip(pure_axes[:, 0], stages):
|
|
ylabel(ax, ylabels[stage], **ylab_snip_kwargs,
|
|
transform=pure_subfig.transSubfigure)
|
|
for ax, scale in zip(pure_axes[snip_specs['conv']], pure_data['example_scales']):
|
|
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
|
pure_subfig.text(s='a', **letter_snip_kwargs)
|
|
|
|
# Prepare noise-song snippet axes:
|
|
noise_subfig = fig.add_subfigure(super_grid[subfig_specs['noise']])
|
|
noise_grid_kwargs['nrows' if noise_grid_kwargs['nrows'] is None else 'ncols'] = noise_data['example_scales'].size
|
|
noise_grid = noise_subfig.add_gridspec(**noise_grid_kwargs)
|
|
noise_axes = add_snip_axes(noise_subfig, noise_grid_kwargs)
|
|
for ax, stage in zip(noise_axes[:, 0], stages):
|
|
ylabel(ax, ylabels[stage], **ylab_snip_kwargs,
|
|
transform=noise_subfig.transSubfigure)
|
|
for ax, scale in zip(noise_axes[snip_specs['conv']], noise_data['example_scales']):
|
|
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
|
noise_subfig.text(s='b', **letter_snip_kwargs)
|
|
|
|
# Prepare analysis axis:
|
|
analysis_subfig = fig.add_subfigure(super_grid[subfig_specs['analysis']])
|
|
analysis_grid = analysis_subfig.add_gridspec(**analysis_grid_kwargs)
|
|
analysis_ax = analysis_subfig.add_subplot(analysis_grid[0, 0])
|
|
analysis_ax.set_xlim(noise_data['scales'].min(), noise_data['scales'].max())
|
|
analysis_ax.xaxis.set_major_locator(plt.MultipleLocator(xloc['analysis']))
|
|
xlabel(analysis_ax, xlabels['analysis'], **xlab_analysis_kwargs,
|
|
transform=analysis_subfig.transSubfigure)
|
|
# analysis_ax.set_yscale('log')
|
|
ylabel(analysis_ax, ylabels['analysis'], **ylab_analysis_kwargs,
|
|
transform=analysis_subfig.transSubfigure)
|
|
analysis_subfig.text(s='c', **letter_analysis_kwargs)
|
|
|
|
# Plot pure-song kernel response snippets:
|
|
plot_snippets(pure_axes[snip_specs['conv']], t_full, pure_data['conv'],
|
|
ymin=0, c=colors['conv'], lw=lw_snippets)
|
|
|
|
# Plot pure-song binary snippets:
|
|
plot_bi_snippets(pure_axes[snip_specs['bi']], t_full, pure_data['bi'],
|
|
color=colors['bi'], lw=0)
|
|
|
|
# Plot pure-song feature snippets:
|
|
plot_snippets(pure_axes[snip_specs['feat']], t_full, pure_data['feat'],
|
|
c=colors['feat'], lw=lw_snippets)
|
|
|
|
# Indicate time scale:
|
|
time_bar(pure_axes[snip_specs['conv']][0], bar_time, **bar_kwargs)
|
|
|
|
# Plot noise-song kernel response snippets:
|
|
plot_snippets(noise_axes[snip_specs['conv']], t_full, noise_data['conv'],
|
|
ymin=0, c=colors['conv'], lw=lw_snippets)
|
|
|
|
# Plot noise-song binary snippets:
|
|
plot_bi_snippets(noise_axes[snip_specs['bi']], t_full, noise_data['bi'],
|
|
color=colors['bi'], lw=0)
|
|
|
|
# Plot noise-song feature snippets:
|
|
plot_snippets(noise_axes[snip_specs['feat']], t_full, noise_data['feat'],
|
|
c=colors['feat'], lw=lw_snippets)
|
|
|
|
# Indicate time scale:
|
|
time_bar(noise_axes[snip_specs['conv']][0], bar_time, **bar_kwargs)
|
|
|
|
# Plot noise-song SD ratios (limited):
|
|
analysis_ax.plot(noise_data['scales'], noise_data['measure_conv'],
|
|
c=colors['conv'], lw=lw_analysis)
|
|
lower, upper = noise_data['spread_conv']
|
|
analysis_ax.fill_between(noise_data['scales'], lower, upper,
|
|
color=colors['conv'], **spread_kwargs)
|
|
analysis_ax.plot(noise_data['scales'], noise_data['measure_feat'],
|
|
c=colors['feat'], lw=lw_analysis)
|
|
lower, upper = noise_data['spread_feat']
|
|
analysis_ax.fill_between(noise_data['scales'], lower, upper,
|
|
color=colors['feat'], **spread_kwargs)
|
|
|
|
if save_path is not None:
|
|
fig.savefig(save_path)
|
|
plt.show()
|
|
|
|
print('Done.')
|
|
embed()
|