Finished (:D) fig_invariance_log_hp.pdf.
Added movable label string to time_bar().
This commit is contained in:
@@ -7,7 +7,7 @@ from thunderhopper.modeltools import load_data
|
||||
from color_functions import load_colors
|
||||
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
|
||||
letter_subplot, title_subplot
|
||||
from IPython import embed
|
||||
|
||||
def add_snip_axes(fig, grid_kwargs):
|
||||
@@ -28,7 +28,6 @@ def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
||||
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
compute_ratios = True
|
||||
target = 'Omocestus_rufipes'
|
||||
data_paths = search_files(target, excl='noise', dir='../data/inv/log_hp/')
|
||||
stages = ['env', 'log', 'inv']
|
||||
@@ -37,10 +36,14 @@ load_kwargs = dict(
|
||||
keywords=['scales', 'snip', 'measure']
|
||||
)
|
||||
save_path = '../figures/fig_invariance_log_hp.pdf'
|
||||
compute_ratios = True
|
||||
show_diag = True
|
||||
show_noise = True
|
||||
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(
|
||||
figsize=(32/2.54, 16/2.54),
|
||||
@@ -60,22 +63,35 @@ subfig_specs = dict(
|
||||
noise=(1, slice(0, -1)),
|
||||
big=(slice(None), -1),
|
||||
)
|
||||
snip_grid_kwargs = dict(
|
||||
block_height = 0.8
|
||||
edge_padding = 0.08
|
||||
pure_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
ncols=None,
|
||||
wspace=0.1,
|
||||
hspace=0.15,
|
||||
left=0.16,
|
||||
right=0.95,
|
||||
bottom=0.1,
|
||||
top=0.94,
|
||||
bottom=1 - block_height - edge_padding,
|
||||
top=1 - edge_padding,
|
||||
height_ratios=[1, 2, 1]
|
||||
)
|
||||
noise_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
ncols=None,
|
||||
wspace=0.1,
|
||||
hspace=0.15,
|
||||
left=0.16,
|
||||
right=0.95,
|
||||
bottom=edge_padding,
|
||||
top=edge_padding + block_height,
|
||||
height_ratios=[1, 2, 1]
|
||||
)
|
||||
big_grid_kwargs = dict(
|
||||
nrows=2,
|
||||
ncols=1,
|
||||
wspace=0,
|
||||
hspace=0.1,
|
||||
hspace=0.3,
|
||||
left=0.19,
|
||||
right=0.96,
|
||||
bottom=0.09,
|
||||
@@ -94,9 +110,10 @@ fs = dict(
|
||||
letter=22,
|
||||
tit_norm=16,
|
||||
tit_tex=20,
|
||||
bar=16,
|
||||
)
|
||||
colors = load_colors('../data/stage_colors.npz')
|
||||
lw_snippets = 0.5
|
||||
lw_snippets = 1
|
||||
lw_big = 3
|
||||
xlabels = dict(
|
||||
big='scale $\\alpha$',
|
||||
@@ -105,7 +122,7 @@ ylabels = dict(
|
||||
env='$x_{\\text{env}}$',
|
||||
log='$x_{\\text{dB}}$',
|
||||
inv='$x_{\\text{adapt}}$',
|
||||
big='$\\sigma_{\\alpha}\\,/\\,\\sigma_{0}$',
|
||||
big='$\\sigma_{\\alpha}\\,/\\,\\sigma_{\\eta}$',
|
||||
)
|
||||
xlab_big_kwargs = dict(
|
||||
y=0,
|
||||
@@ -121,7 +138,7 @@ ylab_snip_kwargs = dict(
|
||||
va='center',
|
||||
)
|
||||
ylab_big_kwargs = dict(
|
||||
x=0,
|
||||
x=0.05,
|
||||
fontsize=fs['lab_tex'],
|
||||
ha='center',
|
||||
va='top',
|
||||
@@ -133,23 +150,23 @@ yloc = dict(
|
||||
)
|
||||
title_kwargs = dict(
|
||||
x=0.5,
|
||||
yref=1,
|
||||
y=1,
|
||||
ha='center',
|
||||
va='top',
|
||||
va='bottom',
|
||||
fontsize=fs['tit_norm'],
|
||||
)
|
||||
letter_snip_kwargs = dict(
|
||||
x=0,
|
||||
y=1,
|
||||
yref=0.5,
|
||||
ha='left',
|
||||
va='top',
|
||||
va='center',
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
letter_big_kwargs = dict(
|
||||
x=0,
|
||||
yref=letter_snip_kwargs['y'],
|
||||
x=0.05,
|
||||
yref=letter_snip_kwargs['yref'],
|
||||
ha='left',
|
||||
va='top',
|
||||
va='center',
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
zoom_inset_bounds = [0.1, 0.2, 0.8, 0.6]
|
||||
@@ -164,13 +181,31 @@ zoom_kwargs = dict(
|
||||
lw=1,
|
||||
alpha=1,
|
||||
)
|
||||
inset_tick_kwargs = dict(
|
||||
axis='y',
|
||||
length=3,
|
||||
pad=1,
|
||||
left=False,
|
||||
labelleft=False,
|
||||
right=True,
|
||||
labelright=True,
|
||||
)
|
||||
bar_time = 5
|
||||
bar_kwargs = dict(
|
||||
y0=-0.2,
|
||||
y1=-0.05,
|
||||
dur=bar_time,
|
||||
y0=-0.25,
|
||||
y1=-0.1,
|
||||
xshift=1,
|
||||
color='k',
|
||||
lw=0,
|
||||
clip_on=False,
|
||||
text_pos=(-0.1, 0.5),
|
||||
text_str=f'${bar_time}\\,\\text{{s}}$',
|
||||
text_kwargs=dict(
|
||||
fontsize=fs['bar'],
|
||||
ha='right',
|
||||
va='center',
|
||||
)
|
||||
)
|
||||
diag_kwargs = dict(
|
||||
c=(0.75, 0.75, 0.75),
|
||||
@@ -178,6 +213,13 @@ diag_kwargs = dict(
|
||||
ls='--',
|
||||
zorder=1.9,
|
||||
)
|
||||
noise_rel_thresh = 0.95
|
||||
noise_kwargs = dict(
|
||||
fc=(0.9, 0.9, 0.9),
|
||||
ec='none',
|
||||
lw=0,
|
||||
zorder=1.5,
|
||||
)
|
||||
|
||||
# EXECUTION:
|
||||
for data_path in data_paths:
|
||||
@@ -192,37 +234,39 @@ for data_path in data_paths:
|
||||
# Prepare overall graph:
|
||||
fig = plt.figure(**fig_kwargs)
|
||||
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
||||
fig.canvas.draw()
|
||||
|
||||
# Prepare pure-song snippet axes:
|
||||
snip_grid_kwargs['ncols'] = pure_data['example_scales'].size
|
||||
pure_grid_kwargs['ncols'] = pure_data['example_scales'].size
|
||||
pure_subfig = fig.add_subfigure(super_grid[subfig_specs['pure']])
|
||||
pure_axes = add_snip_axes(pure_subfig, snip_grid_kwargs)
|
||||
pure_axes = add_snip_axes(pure_subfig, pure_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[0, :], pure_data['example_scales']):
|
||||
title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', ref=pure_subfig, **title_kwargs)
|
||||
pure_title = title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', **title_kwargs)
|
||||
letter_subplot(pure_subfig, 'a', ref=pure_title, **letter_snip_kwargs)
|
||||
pure_inset = pure_axes[0, 0].inset_axes(zoom_inset_bounds)
|
||||
pure_inset.spines[:].set(visible=True, lw=zoom_kwargs['lw'])
|
||||
pure_inset.tick_params(**inset_tick_kwargs)
|
||||
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_grid_kwargs['ncols'] = noise_data['example_scales'].size
|
||||
noise_subfig = fig.add_subfigure(super_grid[subfig_specs['noise']])
|
||||
noise_axes = add_snip_axes(noise_subfig, snip_grid_kwargs)
|
||||
noise_axes = add_snip_axes(noise_subfig, noise_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[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_title = title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', **title_kwargs)
|
||||
letter_subplot(noise_subfig, 'c', ref=noise_title, **letter_snip_kwargs)
|
||||
noise_inset = noise_axes[0, 0].inset_axes(zoom_inset_bounds)
|
||||
noise_inset.spines[:].set(visible=True, lw=zoom_kwargs['lw'])
|
||||
noise_inset.tick_params(**inset_tick_kwargs)
|
||||
hide_ticks(noise_inset, 'bottom', ticks=False)
|
||||
hide_ticks(noise_inset, 'left', ticks=False)
|
||||
|
||||
# Prepare analysis axes:
|
||||
big_subfig = fig.add_subfigure(super_grid[subfig_specs['big']])
|
||||
@@ -238,17 +282,17 @@ for data_path in data_paths:
|
||||
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)
|
||||
letter_subplot(big_subfig, 'b', ref=pure_title, **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)
|
||||
letter_subplot(big_subfig, 'd', ref=noise_title, **letter_big_kwargs)
|
||||
big_axes[i] = ax
|
||||
|
||||
# Plot pure-song envelope 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[1, :], t_full, pure_data['snip_log'],
|
||||
c=colors['log'], lw=lw_snippets)
|
||||
@@ -258,20 +302,23 @@ for data_path in data_paths:
|
||||
c=colors['inv'], lw=lw_snippets)
|
||||
|
||||
# Plot noise-song envelope snippets:
|
||||
ymin, ymax = pure_axes[0, 0].get_ylim()
|
||||
handle = plot_snippets(noise_axes[0, :], t_full, noise_data['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw_snippets)[0]
|
||||
ymin, ymax, 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:
|
||||
ymin, ymax = pure_axes[1, 0].get_ylim()
|
||||
plot_snippets(noise_axes[1, :], t_full, noise_data['snip_log'],
|
||||
c=colors['log'], lw=lw_snippets)
|
||||
ymin, ymax, c=colors['log'], lw=lw_snippets)
|
||||
|
||||
# Plot noise-song invariant snippets:
|
||||
ymin, ymax = pure_axes[2, 0].get_ylim()
|
||||
plot_snippets(noise_axes[2, :], t_full, noise_data['snip_inv'],
|
||||
c=colors['inv'], lw=lw_snippets)
|
||||
ymin, ymax, c=colors['inv'], lw=lw_snippets)
|
||||
|
||||
# Indicate time scale:
|
||||
time_bar(noise_axes[2, -1], bar_time, **bar_kwargs)
|
||||
time_bar(noise_axes[-1, -1], **bar_kwargs)
|
||||
|
||||
if compute_ratios:
|
||||
# Relate pure-song measures to zero scale:
|
||||
@@ -293,12 +340,25 @@ for data_path in data_paths:
|
||||
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 show_diag:
|
||||
# Indicate diagonal:
|
||||
big_axes[0].plot(pure_scales, pure_scales, **diag_kwargs)
|
||||
big_axes[1].plot(noise_scales, noise_scales, **diag_kwargs)
|
||||
|
||||
if show_noise:
|
||||
# Indicate noise floor:
|
||||
if compute_ratios:
|
||||
span_measure = noise_data['measure_inv'][-1] - ref_measures['inv']
|
||||
thresh_measure = ref_measures['inv'] + noise_rel_thresh * span_measure
|
||||
else:
|
||||
span_measure = noise_data['measure_inv'][-1] - noise_data['measure_inv'][0]
|
||||
thresh_measure = noise_data['measure_inv'][0] + noise_rel_thresh * span_measure
|
||||
thresh_ind = np.nonzero(noise_data['measure_inv'] < thresh_measure)[0][-1]
|
||||
thresh_scale = noise_scales[thresh_ind]
|
||||
big_axes[1].axvspan(noise_scales[0], thresh_scale, **noise_kwargs)
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
fig.savefig(save_path, bbox_inches='tight')
|
||||
plt.show()
|
||||
|
||||
print('Done.')
|
||||
|
||||
Reference in New Issue
Block a user