Lots of stuff. Syncing to home.
This commit is contained in:
@@ -5,9 +5,9 @@ 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, strip_zeros, time_bar,\
|
||||
letter_subplot, letter_subplots
|
||||
from plot_functions import hide_axis, ylimits, xlabel, ylabel, hide_ticks,\
|
||||
plot_line, strip_zeros, time_bar, zoom_inset,\
|
||||
letter_subplot, letter_subplots, title_subplot
|
||||
from IPython import embed
|
||||
|
||||
def add_snip_axes(fig, grid_kwargs):
|
||||
@@ -15,26 +15,31 @@ def add_snip_axes(fig, 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, 'left') for ax in axes[:, 1:].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)
|
||||
handles = []
|
||||
for ax, snippet in zip(axes, snippets.T):
|
||||
plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs)
|
||||
return None
|
||||
handles.extend(plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs))
|
||||
return handles
|
||||
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
compute_ratios = True
|
||||
target = 'Omocestus_rufipes'
|
||||
data_paths = search_files(target, excl='noise', dir='../data/inv/log_hp/')
|
||||
stages = ['env', 'log', 'inv']
|
||||
load_kwargs = dict(
|
||||
files=stages,
|
||||
keywords=['scales', 'measure']
|
||||
keywords=['scales', 'snip', 'measure']
|
||||
)
|
||||
save_path = '../figures/fig_invariance_log_hp.pdf'
|
||||
if compute_ratios:
|
||||
ref_data = load_data('../data/processed/white_noise_sd-1.npz', files=stages)[0]
|
||||
ref_measures = {k: v.std() for k, v in ref_data.items() if not k.endswith('rate')}
|
||||
|
||||
# GRAPH SETTINGS:
|
||||
fig_kwargs = dict(
|
||||
@@ -42,7 +47,7 @@ fig_kwargs = dict(
|
||||
)
|
||||
super_grid_kwargs = dict(
|
||||
nrows=2,
|
||||
ncols=2,
|
||||
ncols=3,
|
||||
wspace=0,
|
||||
hspace=0,
|
||||
left=0,
|
||||
@@ -51,99 +56,127 @@ super_grid_kwargs = dict(
|
||||
top=1
|
||||
)
|
||||
subfig_specs = dict(
|
||||
pure=(0, 0),
|
||||
noise=(1, 0),
|
||||
analysis=(slice(None), 1)
|
||||
pure=(0, slice(0, -1)),
|
||||
noise=(1, slice(0, -1)),
|
||||
big=(slice(None), -1),
|
||||
)
|
||||
pure_grid_kwargs = dict(
|
||||
snip_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
ncols=None,
|
||||
wspace=0.05,
|
||||
hspace=0.1,
|
||||
left=0.13,
|
||||
wspace=0.1,
|
||||
hspace=0.15,
|
||||
left=0.16,
|
||||
right=0.95,
|
||||
bottom=0.15,
|
||||
top=0.9
|
||||
bottom=0.1,
|
||||
top=0.94,
|
||||
height_ratios=[1, 2, 1]
|
||||
)
|
||||
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,
|
||||
big_grid_kwargs = dict(
|
||||
nrows=2,
|
||||
ncols=1,
|
||||
wspace=0,
|
||||
hspace=0,
|
||||
left=0.15,
|
||||
hspace=0.1,
|
||||
left=0.19,
|
||||
right=0.96,
|
||||
bottom=0.1,
|
||||
top=0.95
|
||||
bottom=0.09,
|
||||
top=0.98
|
||||
)
|
||||
snip_specs = dict(
|
||||
env=(0, slice(None)),
|
||||
log=(1, slice(None)),
|
||||
inv=(2, slice(None))
|
||||
anchor_kwargs = dict(
|
||||
aspect='equal',
|
||||
adjustable='box',
|
||||
anchor=(0.5, 0.5)
|
||||
)
|
||||
|
||||
# PLOT SETTINGS:
|
||||
fs = dict(
|
||||
lab_norm=16,
|
||||
lab_tex=20,
|
||||
letter=22,
|
||||
tit_norm=16,
|
||||
tit_tex=20,
|
||||
)
|
||||
colors = load_colors('../data/stage_colors.npz')
|
||||
lw_snippets = 0.5
|
||||
lw_analysis = 3
|
||||
lw_big = 3
|
||||
xlabels = dict(
|
||||
analysis='scale $\\alpha$',
|
||||
)
|
||||
xlab_analysis_kwargs = dict(
|
||||
y=0.01,
|
||||
fontsize=16,
|
||||
ha='center',
|
||||
va='bottom',
|
||||
big='scale $\\alpha$',
|
||||
)
|
||||
ylabels = dict(
|
||||
env='$x_{\\text{env}}$',
|
||||
log='$x_{\\text{dB}}$',
|
||||
inv='$x_{\\text{adapt}}$',
|
||||
analysis='ratio $\\text{SD}_{\\alpha}\\,/\\,\\text{SD}_{\\min[\\alpha]}$',
|
||||
# analysis='ratio $\\sigma_{\\alpha}\\,/\\,\\sigma_{\\min[\\alpha]}$',
|
||||
big='$\\sigma_{\\alpha}\\,/\\,\\sigma_{0}$',
|
||||
)
|
||||
xlab_big_kwargs = dict(
|
||||
y=0,
|
||||
fontsize=fs['lab_norm'],
|
||||
ha='center',
|
||||
va='bottom',
|
||||
)
|
||||
ylab_snip_kwargs = dict(
|
||||
x=0.01,
|
||||
fontsize=20,
|
||||
x=0,
|
||||
fontsize=fs['lab_tex'],
|
||||
rotation=0,
|
||||
ha='left',
|
||||
va='center',
|
||||
)
|
||||
ylab_analysis_kwargs = dict(
|
||||
x=0.02,
|
||||
fontsize=16,
|
||||
ylab_big_kwargs = dict(
|
||||
x=0,
|
||||
fontsize=fs['lab_tex'],
|
||||
ha='center',
|
||||
va='top',
|
||||
)
|
||||
yloc = dict(
|
||||
env=1000,
|
||||
log=40,
|
||||
inv=20
|
||||
)
|
||||
title_kwargs = dict(
|
||||
x=0.5,
|
||||
yref=1,
|
||||
ha='center',
|
||||
va='top',
|
||||
fontsize=fs['tit_norm'],
|
||||
)
|
||||
letter_snip_kwargs = dict(
|
||||
x=0.02,
|
||||
y=0.97,
|
||||
x=0,
|
||||
y=1,
|
||||
ha='left',
|
||||
va='top',
|
||||
fontsize=22,
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
letter_analysis_kwargs = dict(
|
||||
letter_big_kwargs = dict(
|
||||
x=0,
|
||||
yref=letter_snip_kwargs['y'],
|
||||
ha='left',
|
||||
va='top',
|
||||
fontsize=22,
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
zoom_inset_bounds = [0.1, 0.2, 0.8, 0.6]
|
||||
zoom_kwargs = dict(
|
||||
x0=0.45,
|
||||
x1=0.55,
|
||||
y0=0,
|
||||
y1=0.0006,
|
||||
low_left=True,
|
||||
low_right=True,
|
||||
ec='k',
|
||||
lw=1,
|
||||
alpha=1,
|
||||
)
|
||||
bar_time = 5
|
||||
bar_kwargs = dict(
|
||||
y0=0.5,
|
||||
y1=0.6,
|
||||
y0=-0.2,
|
||||
y1=-0.05,
|
||||
color='k',
|
||||
lw = 0,
|
||||
lw=0,
|
||||
clip_on=False,
|
||||
)
|
||||
diag_kwargs = dict(
|
||||
c=(0.75, 0.75, 0.75),
|
||||
lw=2,
|
||||
ls='--',
|
||||
zorder=1.9,
|
||||
)
|
||||
|
||||
# EXECUTION:
|
||||
@@ -153,91 +186,116 @@ for data_path in data_paths:
|
||||
# Load invariance data:
|
||||
pure_data, config = load_data(data_path, **load_kwargs)
|
||||
noise_data, _ = load_data(data_path.replace('.npz', '_noise.npz'), **load_kwargs)
|
||||
t_full = np.arange(pure_data['env'].shape[0]) / config['env_rate']
|
||||
pure_scales, noise_scales = pure_data['scales'], noise_data['scales']
|
||||
t_full = np.arange(pure_data['snip_env'].shape[0]) / config['env_rate']
|
||||
|
||||
# Prepare overall graph:
|
||||
fig = plt.figure(**fig_kwargs)
|
||||
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
||||
|
||||
# Prepare pure-song snippet axes:
|
||||
snip_grid_kwargs['ncols'] = pure_data['example_scales'].size
|
||||
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)
|
||||
pure_axes = add_snip_axes(pure_subfig, snip_grid_kwargs)
|
||||
for ax, stage in zip(pure_axes[:, 0], stages):
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage]))
|
||||
ylabel(ax, ylabels[stage], **ylab_snip_kwargs,
|
||||
transform=pure_subfig.transSubfigure)
|
||||
for ax, scale in zip(pure_axes[snip_specs['env']], pure_data['example_scales']):
|
||||
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
||||
for ax, scale in zip(pure_axes[0, :], pure_data['example_scales']):
|
||||
title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', ref=pure_subfig, **title_kwargs)
|
||||
pure_inset = pure_axes[0, 0].inset_axes(zoom_inset_bounds)
|
||||
pure_inset.spines[:].set(visible=True, lw=zoom_kwargs['lw'])
|
||||
hide_ticks(pure_inset, 'bottom', ticks=False)
|
||||
hide_ticks(pure_inset, 'left', ticks=False)
|
||||
|
||||
# Prepare noise-song snippet axes:
|
||||
snip_grid_kwargs['ncols'] = noise_data['example_scales'].size
|
||||
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)
|
||||
noise_axes = add_snip_axes(noise_subfig, snip_grid_kwargs)
|
||||
for ax, stage in zip(noise_axes[:, 0], stages):
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage]))
|
||||
ylabel(ax, ylabels[stage], **ylab_snip_kwargs,
|
||||
transform=noise_subfig.transSubfigure)
|
||||
for ax, scale in zip(noise_axes[snip_specs['env']], noise_data['example_scales']):
|
||||
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
||||
letter_subplots([pure_subfig, noise_subfig], **letter_snip_kwargs)
|
||||
for ax, scale in zip(noise_axes[0, :], noise_data['example_scales']):
|
||||
title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', ref=noise_subfig, **title_kwargs)
|
||||
letter_subplots([pure_subfig, noise_subfig], 'ac', **letter_snip_kwargs)
|
||||
noise_inset = noise_axes[0, 0].inset_axes(zoom_inset_bounds)
|
||||
noise_inset.spines[:].set(visible=True, lw=zoom_kwargs['lw'])
|
||||
hide_ticks(noise_inset, 'bottom', ticks=False)
|
||||
hide_ticks(noise_inset, 'left', ticks=False)
|
||||
|
||||
# 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.set_xscale('symlog', linthresh=pure_data['scales'][1], linscale=0.5)
|
||||
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)
|
||||
letter_subplot(analysis_subfig, 'c', **letter_analysis_kwargs, ref=pure_subfig)
|
||||
# Prepare analysis axes:
|
||||
big_subfig = fig.add_subfigure(super_grid[subfig_specs['big']])
|
||||
big_grid = big_subfig.add_gridspec(**big_grid_kwargs)
|
||||
big_axes = np.zeros((big_grid.nrows,), dtype=object)
|
||||
for i, scales in enumerate([pure_scales, noise_scales]):
|
||||
ax = big_subfig.add_subplot(big_grid[i, 0])
|
||||
ax.set_xlim(scales[0], scales[-1])
|
||||
ax.set_ylim(scales[0], scales[-1])
|
||||
ax.set_xscale('symlog', linthresh=scales[1], linscale=0.5)
|
||||
ax.set_yscale('symlog', linthresh=scales[1], linscale=0.5)
|
||||
ax.set_aspect(**anchor_kwargs)
|
||||
ylabel(ax, ylabels['big'], transform=big_subfig.transSubfigure, **ylab_big_kwargs)
|
||||
if i == 0:
|
||||
hide_ticks(ax, 'bottom')
|
||||
letter_subplot(big_subfig, 'b', ref=pure_subfig, **letter_big_kwargs)
|
||||
else:
|
||||
xlabel(ax, xlabels['big'], transform=big_subfig.transSubfigure, **xlab_big_kwargs)
|
||||
letter_subplot(big_subfig, 'd', ref=noise_subfig, **letter_big_kwargs)
|
||||
big_axes[i] = ax
|
||||
|
||||
# Plot pure-song envelope snippets:
|
||||
plot_snippets(pure_axes[snip_specs['env']], t_full, pure_data['env'],
|
||||
ymin=0, c=colors['env'], lw=lw_snippets)
|
||||
|
||||
handle = plot_snippets(pure_axes[0, :], t_full, pure_data['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw_snippets)[0]
|
||||
zoom_inset(pure_axes[0, 0], pure_inset, handle, transform=pure_axes[0, 0].transAxes, **zoom_kwargs)
|
||||
|
||||
# Plot pure-song logarithmic snippets:
|
||||
plot_snippets(pure_axes[snip_specs['log']], t_full, pure_data['log'],
|
||||
ymax=None, c=colors['log'], lw=lw_snippets)
|
||||
plot_snippets(pure_axes[1, :], t_full, pure_data['snip_log'],
|
||||
c=colors['log'], lw=lw_snippets)
|
||||
|
||||
# Plot pure-song invariant snippets:
|
||||
plot_snippets(pure_axes[snip_specs['inv']], t_full, pure_data['inv'],
|
||||
plot_snippets(pure_axes[2, :], t_full, pure_data['snip_inv'],
|
||||
c=colors['inv'], lw=lw_snippets)
|
||||
|
||||
# Indicate time scale:
|
||||
time_bar(pure_axes[snip_specs['env']][0], bar_time, **bar_kwargs)
|
||||
|
||||
# Plot noise-song envelope snippets:
|
||||
plot_snippets(noise_axes[snip_specs['env']], t_full, noise_data['env'],
|
||||
ymin=0, c=colors['env'], lw=lw_snippets)
|
||||
handle = plot_snippets(noise_axes[0, :], t_full, noise_data['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw_snippets)[0]
|
||||
zoom_inset(noise_axes[0, 0], noise_inset, handle, transform=noise_axes[0, 0].transAxes, **zoom_kwargs)
|
||||
|
||||
# Plot noise-song logarithmic snippets:
|
||||
plot_snippets(noise_axes[snip_specs['log']], t_full, noise_data['log'],
|
||||
ymax=None, c=colors['log'], lw=lw_snippets)
|
||||
plot_snippets(noise_axes[1, :], t_full, noise_data['snip_log'],
|
||||
c=colors['log'], lw=lw_snippets)
|
||||
|
||||
# Plot noise-song invariant snippets:
|
||||
plot_snippets(noise_axes[snip_specs['inv']], t_full, noise_data['inv'],
|
||||
plot_snippets(noise_axes[2, :], t_full, noise_data['snip_inv'],
|
||||
c=colors['inv'], lw=lw_snippets)
|
||||
|
||||
# Indicate time scale:
|
||||
time_bar(noise_axes[snip_specs['env']][0], bar_time, **bar_kwargs)
|
||||
time_bar(noise_axes[2, -1], bar_time, **bar_kwargs)
|
||||
|
||||
# Plot pure-song SD ratios (ideal):
|
||||
base_ind = np.argmin(pure_data['scales'])
|
||||
measure_inv = pure_data['measure_inv'] / pure_data['measure_inv'][base_ind]
|
||||
analysis_ax.plot(pure_data['scales'], measure_inv, c=colors['inv'], lw=lw_analysis, ls='--')
|
||||
if compute_ratios:
|
||||
# Relate pure-song measures to zero scale:
|
||||
pure_data['measure_env'] /= ref_measures['env']
|
||||
pure_data['measure_log'] /= ref_measures['log']
|
||||
pure_data['measure_inv'] /= ref_measures['inv']
|
||||
# Relate noise-song measures to zero scale:
|
||||
noise_data['measure_env'] /= ref_measures['env']
|
||||
noise_data['measure_log'] /= ref_measures['log']
|
||||
noise_data['measure_inv'] /= ref_measures['inv']
|
||||
|
||||
# Plot noise-song SD ratios (limited):
|
||||
base_ind = np.argmin(noise_data['scales'])
|
||||
measure_env = noise_data['measure_env'] / noise_data['measure_env'][base_ind]
|
||||
measure_log = noise_data['measure_log'] / noise_data['measure_log'][base_ind]
|
||||
measure_inv = noise_data['measure_inv'] / noise_data['measure_inv'][base_ind]
|
||||
analysis_ax.plot(noise_data['scales'], measure_env, c=colors['env'], lw=lw_analysis)
|
||||
analysis_ax.plot(noise_data['scales'], measure_log, c=colors['log'], lw=lw_analysis)
|
||||
analysis_ax.plot(noise_data['scales'], measure_inv, c=colors['inv'], lw=lw_analysis)
|
||||
analysis_ax.set_ylim(0.9, measure_env.max())
|
||||
# Plot pure-song measures (ideal):
|
||||
big_axes[0].plot(pure_scales, pure_data['measure_env'], c=colors['env'], lw=lw_big)
|
||||
big_axes[0].plot(pure_scales, pure_data['measure_log'], c=colors['log'], lw=lw_big)
|
||||
big_axes[0].plot(pure_scales, pure_data['measure_inv'], c=colors['inv'], lw=lw_big)
|
||||
|
||||
# Plot noise-song measures (limited):
|
||||
big_axes[1].plot(noise_scales, noise_data['measure_env'], c=colors['env'], lw=lw_big)
|
||||
big_axes[1].plot(noise_scales, noise_data['measure_log'], c=colors['log'], lw=lw_big)
|
||||
big_axes[1].plot(noise_scales, noise_data['measure_inv'], c=colors['inv'], lw=lw_big)
|
||||
|
||||
# Indicate diagonal:
|
||||
big_axes[0].plot(pure_scales, pure_scales, **diag_kwargs)
|
||||
big_axes[1].plot(noise_scales, noise_scales, **diag_kwargs)
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
|
||||
Reference in New Issue
Block a user