Added complete "rect-lp" analysis except figure. Added multiple appendix figs. Overhauled normalization options across all condense scripts. Co-authored-by: Copilot <copilot@github.com>
454 lines
14 KiB
Python
454 lines
14 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 misc_functions import shorten_species, get_saturation
|
|
from color_functions import load_colors
|
|
from plot_functions import hide_axis, ylimits, super_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):
|
|
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[:, 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):
|
|
handles.extend(plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs))
|
|
return handles
|
|
|
|
# GENERAL SETTINGS:
|
|
target = 'Omocestus_rufipes_DJN_32-40s724ms-48s779ms'
|
|
data_path = search_files(target, excl='noise', dir='../data/inv/log_hp/')[0]
|
|
ref_path = '../data/inv/log_hp/ref_measures.npz'
|
|
save_path = '../figures/fig_invariance_log_hp.pdf'
|
|
target_species = [
|
|
'Chorthippus_biguttulus',
|
|
'Chorthippus_mollis',
|
|
'Chrysochraon_dispar',
|
|
# 'Euchorthippus_declivus',
|
|
'Gomphocerippus_rufus',
|
|
'Omocestus_rufipes',
|
|
'Pseudochorthippus_parallelus',
|
|
]
|
|
stages = ['env', 'log', 'inv']
|
|
load_kwargs = dict(
|
|
files=stages,
|
|
keywords=['scales', 'snip', 'measure']
|
|
)
|
|
compute_ratios = True
|
|
exclude_zero = True
|
|
show_diag = True
|
|
show_plateaus = True
|
|
|
|
# GRAPH SETTINGS:
|
|
fig_kwargs = dict(
|
|
figsize=(32/2.54, 32/2.54),
|
|
)
|
|
super_grid_kwargs = dict(
|
|
nrows=3,
|
|
ncols=1,
|
|
wspace=0,
|
|
hspace=0,
|
|
left=0,
|
|
right=1,
|
|
bottom=0,
|
|
top=1,
|
|
height_ratios=[1, 1, 1]
|
|
)
|
|
subfig_specs = dict(
|
|
pure=(0, slice(None)),
|
|
noise=(1, slice(None)),
|
|
big=(2, slice(None)),
|
|
)
|
|
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.11,
|
|
right=0.95,
|
|
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=pure_grid_kwargs['wspace'],
|
|
hspace=pure_grid_kwargs['hspace'],
|
|
left=pure_grid_kwargs['left'],
|
|
right=pure_grid_kwargs['right'],
|
|
bottom=edge_padding,
|
|
top=edge_padding + block_height,
|
|
height_ratios=[1, 2, 1]
|
|
)
|
|
big_grid_kwargs = dict(
|
|
nrows=1,
|
|
ncols=3,
|
|
wspace=0.3,
|
|
hspace=0,
|
|
left=pure_grid_kwargs['left'],
|
|
right=pure_grid_kwargs['right'],
|
|
bottom=0.05,
|
|
top=1
|
|
)
|
|
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,
|
|
bar=16,
|
|
)
|
|
colors = load_colors('../data/stage_colors.npz')
|
|
species_colors = load_colors('../data/species_colors.npz')
|
|
noise_colors = [(0.6,) * 3, (0.8,) * 3]
|
|
lw = dict(
|
|
snip=1,
|
|
big=4,
|
|
spec=2,
|
|
plateau=1.5,
|
|
legend=5,
|
|
)
|
|
xlabels = dict(
|
|
big='scale $\\alpha$',
|
|
)
|
|
ylabels = dict(
|
|
env='$x_{\\text{env}}$',
|
|
log='$x_{\\text{dB}}$',
|
|
inv='$x_{\\text{adapt}}$',
|
|
big='$\\sigma_x\\,/\\,\\sigma_{\\eta}$',
|
|
)
|
|
xlab_big_kwargs = dict(
|
|
y=0,
|
|
fontsize=fs['lab_norm'],
|
|
ha='center',
|
|
va='bottom',
|
|
)
|
|
ylab_snip_kwargs = dict(
|
|
x=0,
|
|
fontsize=fs['lab_tex'],
|
|
rotation=0,
|
|
ha='left',
|
|
va='center',
|
|
)
|
|
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,
|
|
y=1,
|
|
ha='center',
|
|
va='bottom',
|
|
fontsize=fs['tit_norm'],
|
|
)
|
|
letter_snip_kwargs = dict(
|
|
x=0,
|
|
yref=0.5,
|
|
ha='left',
|
|
va='center',
|
|
fontsize=fs['letter'],
|
|
)
|
|
letter_big_kwargs = dict(
|
|
x=0,
|
|
y=1,
|
|
ha='left',
|
|
va='bottom',
|
|
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,
|
|
)
|
|
inset_tick_kwargs = dict(
|
|
axis='y',
|
|
length=3,
|
|
pad=1,
|
|
left=False,
|
|
labelleft=False,
|
|
right=True,
|
|
labelright=True,
|
|
)
|
|
bar_time = 5
|
|
bar_kwargs = dict(
|
|
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',
|
|
)
|
|
)
|
|
leg_kwargs = dict(
|
|
ncols=2,
|
|
loc='upper right',
|
|
bbox_to_anchor=(0, 0.6, 1, 0.4),
|
|
frameon=False,
|
|
prop=dict(
|
|
size=12,
|
|
style='italic',
|
|
),
|
|
borderpad=0,
|
|
borderaxespad=0,
|
|
handlelength=1,
|
|
columnspacing=1,
|
|
)
|
|
diag_kwargs = dict(
|
|
c=(0.3,) * 3,
|
|
lw=2,
|
|
ls='--',
|
|
zorder=1.9,
|
|
)
|
|
plateau_settings = dict(
|
|
low=0.05,
|
|
high=0.95,
|
|
first=True,
|
|
last=True,
|
|
condense=None,
|
|
)
|
|
plateau_rect_kwargs = dict(
|
|
ec='none',
|
|
lw=0,
|
|
zorder=1.5,
|
|
)
|
|
plateau_line_kwargs = dict(
|
|
lw=lw['plateau'],
|
|
ls='--',
|
|
zorder=1,
|
|
)
|
|
plateau_dot_kwargs = dict(
|
|
marker='o',
|
|
markersize=8,
|
|
markeredgewidth=1,
|
|
clip_on=False,
|
|
)
|
|
|
|
# PREPARATION:
|
|
species_measures = {}
|
|
thresh_inds = np.zeros((len(target_species),), dtype=int)
|
|
for i, species in enumerate(target_species):
|
|
spec_path = search_files(species, incl=['noise', 'norm-base'], dir='../data/inv/log_hp/condensed/')[0]
|
|
spec_data = dict(np.load(spec_path))
|
|
measure = spec_data['mean_inv'].mean(axis=-1)
|
|
if exclude_zero:
|
|
measure = measure[spec_data['scales'] > 0]
|
|
species_measures[species] = measure
|
|
thresh_inds[i] = get_saturation(measure, **plateau_settings)[1]
|
|
|
|
# EXECUTION:
|
|
print(f'Processing {data_path}')
|
|
|
|
# Load invariance data:
|
|
pure_data, config = load_data(data_path, **load_kwargs)
|
|
noise_data, _ = load_data(data_path.replace('pure', 'noise'), **load_kwargs)
|
|
pure_scales, noise_scales = pure_data['scales'], noise_data['scales']
|
|
t_full = np.arange(pure_data['snip_env'].shape[0]) / config['env_rate']
|
|
|
|
if compute_ratios:
|
|
# Relate pure-song measures to near-zero scale:
|
|
pure_data['measure_env'] /= pure_data['measure_env'][1]
|
|
pure_data['measure_log'] /= pure_data['measure_log'][1]
|
|
pure_data['measure_inv'] /= pure_data['measure_inv'][1]
|
|
# Relate noise-song measures to zero scale:
|
|
noise_data['measure_env'] /= noise_data['measure_env'][0]
|
|
noise_data['measure_log'] /= noise_data['measure_log'][0]
|
|
noise_data['measure_inv'] /= noise_data['measure_inv'][0]
|
|
|
|
if exclude_zero:
|
|
# Exclude zero scales:
|
|
inds = pure_scales > 0
|
|
pure_scales = pure_scales[inds]
|
|
pure_data['measure_env'] = pure_data['measure_env'][inds]
|
|
pure_data['measure_log'] = pure_data['measure_log'][inds]
|
|
pure_data['measure_inv'] = pure_data['measure_inv'][inds]
|
|
inds = noise_scales > 0
|
|
noise_scales = noise_scales[inds]
|
|
noise_data['measure_env'] = noise_data['measure_env'][inds]
|
|
noise_data['measure_log'] = noise_data['measure_log'][inds]
|
|
noise_data['measure_inv'] = noise_data['measure_inv'][inds]
|
|
|
|
# Prepare overall graph:
|
|
fig = plt.figure(**fig_kwargs)
|
|
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
|
fig.canvas.draw()
|
|
|
|
# Prepare pure-song snippet axes:
|
|
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, 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']):
|
|
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)
|
|
|
|
# Prepare noise-song snippet axes:
|
|
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, 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']):
|
|
noise_title = title_subplot(ax, f'$\\alpha={strip_zeros(scale)}$', **title_kwargs)
|
|
letter_subplot(noise_subfig, 'b', 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)
|
|
|
|
# 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.ncols,), dtype=object)
|
|
for i, scales in enumerate([pure_scales, noise_scales, noise_scales]):
|
|
ax = big_subfig.add_subplot(big_grid[0, i])
|
|
ax.set_xlim(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)
|
|
if i in [0, 1]:
|
|
ax.set_ylim(scales[0], scales[-1])
|
|
pos_equal = ax.get_position().bounds
|
|
else:
|
|
pos_auto = list(ax.get_position().bounds)
|
|
ax.set_aspect('auto', adjustable='box', anchor=(0.5, 0.5))
|
|
ax.set_position([pos_auto[0], pos_equal[1], pos_auto[2], pos_equal[3]])
|
|
ax.set_ylim(0.9, 30)
|
|
if i == 1:
|
|
hide_ticks(ax, 'left')
|
|
big_axes[i] = ax
|
|
ylabel(big_axes[0], ylabels['big'], transform=big_subfig.transSubfigure, **ylab_big_kwargs)
|
|
super_xlabel(xlabels['big'], big_subfig, big_axes[0], big_axes[-1], **xlab_big_kwargs)
|
|
letter_subplots(big_axes, 'cde', **letter_big_kwargs)
|
|
|
|
# Plot pure-song envelope snippets:
|
|
handle = plot_snippets(pure_axes[0, :], t_full, pure_data['snip_env'],
|
|
ymin=0, c=colors['env'], lw=lw['snip'])[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['snip'])
|
|
|
|
# Plot pure-song invariant snippets:
|
|
plot_snippets(pure_axes[2, :], t_full, pure_data['snip_inv'],
|
|
c=colors['inv'], lw=lw['snip'])
|
|
|
|
# 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, ymax, c=colors['env'], lw=lw['snip'])[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'],
|
|
ymin, ymax, c=colors['log'], lw=lw['snip'])
|
|
|
|
# Plot noise-song invariant snippets:
|
|
ymin, ymax = pure_axes[2, 0].get_ylim()
|
|
plot_snippets(noise_axes[2, :], t_full, noise_data['snip_inv'],
|
|
ymin, ymax, c=colors['inv'], lw=lw['snip'])
|
|
|
|
# Indicate time scale:
|
|
time_bar(noise_axes[-1, -1], **bar_kwargs)
|
|
|
|
# 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'])
|
|
|
|
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_plateaus:
|
|
# Indicate low and high plateaus of noise invariance curve:
|
|
low_ind, high_ind = get_saturation(noise_data['measure_inv'], **plateau_settings)
|
|
big_axes[1].axvspan(noise_scales[0], noise_scales[low_ind],
|
|
fc=noise_colors[0], **plateau_rect_kwargs)
|
|
big_axes[1].axvspan(noise_scales[low_ind], noise_scales[high_ind],
|
|
fc=noise_colors[1], **plateau_rect_kwargs)
|
|
|
|
# Plot species-specific noise-song invariance curves:
|
|
for i, (species, measure) in enumerate(species_measures.items()):
|
|
# Plot invariance curve:
|
|
color = species_colors[species]
|
|
big_axes[2].plot(noise_scales, measure, label=shorten_species(species),
|
|
c=color, lw=lw['spec'])
|
|
# Indicate saturation:
|
|
ind = thresh_inds[i]
|
|
scale = noise_scales[ind]
|
|
big_axes[2].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
|
transform=big_axes[2].get_xaxis_transform())
|
|
big_axes[2].plot(scale, 0, mfc=color, mec='k', alpha=0.75, zorder=6, **plateau_dot_kwargs,
|
|
transform=big_axes[2].get_xaxis_transform())
|
|
big_axes[2].vlines(scale, big_axes[2].get_ylim()[0], measure[ind],
|
|
color=color, **plateau_line_kwargs)
|
|
legend = big_axes[2].legend(**leg_kwargs)
|
|
[h.set_lw(lw['legend']) for h in legend.legend_handles]
|
|
|
|
if save_path is not None:
|
|
fig.savefig(save_path, bbox_inches='tight')
|
|
plt.show()
|
|
|
|
print('Done.')
|
|
embed()
|