251 lines
7.9 KiB
Python
251 lines
7.9 KiB
Python
import plotstyle_plt
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
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 IPython import embed
|
|
|
|
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
|
|
|
|
|
|
# GENERAL SETTINGS:
|
|
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']
|
|
)
|
|
save_path = '../figures/fig_invariance_log_hp.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(
|
|
env=(0, slice(None)),
|
|
log=(1, slice(None)),
|
|
inv=(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(
|
|
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]}$',
|
|
)
|
|
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=200,
|
|
)
|
|
letter_snip_kwargs = dict(
|
|
x=0.02,
|
|
y=0.97,
|
|
ha='left',
|
|
va='top',
|
|
fontsize=22,
|
|
)
|
|
letter_analysis_kwargs = dict(
|
|
x=0,
|
|
yref=letter_snip_kwargs['y'],
|
|
ha='left',
|
|
va='top',
|
|
fontsize=22,
|
|
)
|
|
bar_time = 5
|
|
bar_kwargs = dict(
|
|
y0=0.5,
|
|
y1=0.6,
|
|
color='k',
|
|
lw = 0,
|
|
)
|
|
|
|
# EXECUTION:
|
|
for data_path in data_paths:
|
|
print(f'Processing {data_path}')
|
|
|
|
# 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']
|
|
|
|
# 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['env']], pure_data['example_scales']):
|
|
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
|
|
|
# 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['env']], noise_data['example_scales']):
|
|
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
|
letter_subplots([pure_subfig, noise_subfig], **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)
|
|
letter_subplot(analysis_subfig, 'c', **letter_analysis_kwargs, ref=pure_subfig)
|
|
|
|
# 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)
|
|
|
|
# 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 pure-song invariant snippets:
|
|
plot_snippets(pure_axes[snip_specs['inv']], t_full, pure_data['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)
|
|
|
|
# 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 noise-song invariant snippets:
|
|
plot_snippets(noise_axes[snip_specs['inv']], t_full, noise_data['inv'],
|
|
c=colors['inv'], lw=lw_snippets)
|
|
|
|
# Indicate time scale:
|
|
time_bar(noise_axes[snip_specs['env']][0], 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='--')
|
|
|
|
# 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.1, measure_env.max())
|
|
|
|
if save_path is not None:
|
|
fig.savefig(save_path)
|
|
plt.show()
|
|
|
|
print('Done.')
|
|
embed()
|