Files
paper_2025/python/fig_invariance_rect-lp.py
j-hartling 16014c02a0 Wrote results rect-lp and log-hp :)
Finished some more figure captions.
2026-05-04 19:50:04 +02:00

417 lines
13 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
from color_functions import load_colors
from plot_functions import hide_axis, shift_subplot, shift_subplot, ylimits,\
super_xlabel, ylabel, hide_ticks,\
plot_line, strip_zeros, time_bar,\
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])
if j == 0:
shift_subplot(axes[i, j], dx=snip_col_shift)
[hide_axis(ax, 'left') for ax in axes[:, 2:].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/rect_lp/')[0]
save_path = '../figures/fig_invariance_rect_lp.pdf'
target_species = [
'Chorthippus_biguttulus',
'Chorthippus_mollis',
'Chrysochraon_dispar',
# 'Euchorthippus_declivus',
'Gomphocerippus_rufus',
'Omocestus_rufipes',
'Pseudochorthippus_parallelus',
]
stages = ['filt', 'env']
load_kwargs = dict(
files=stages,
keywords=['scales', 'cutoff', 'snip', 'measure']
)
# ANALYSIS SETTINGS:
relate_to_noise = True
exclude_zero = True
show_diag = True
snip_cutoff = np.array([np.nan, 2500, 250, 25])[2]
# GRAPH SETTINGS:
fig_kwargs = dict(
figsize=(32/2.54, 20/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, 2]
)
subfig_specs = dict(
pure=(0, slice(None)),
noise=(1, slice(None)),
big=(2, slice(None)),
)
block_height = 0.8
edge_padding = 0.08
snip_col_shift = -0.05
pure_grid_kwargs = dict(
nrows=len(stages),
ncols=None,
wspace=0.1,
hspace=0.15,
left=0.1 - snip_col_shift,
right=0.98,
bottom=1 - block_height - edge_padding,
top=1 - edge_padding,
height_ratios=[1, 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, 1]
)
big_col_shift = -0.05
big_grid_kwargs = dict(
nrows=1,
ncols=3,
wspace=0.25,
hspace=0,
left=pure_grid_kwargs['left'] + snip_col_shift - big_col_shift,
right=pure_grid_kwargs['right'],
bottom=0.04,
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')
colors['raw'] = (0., 0., 0.,)
species_colors = load_colors('../data/species_colors.npz')
lw = dict(
snip=0.5,
big=3,
spec=1.5,
legend=5,
)
dash_cycle = 6 # points
ls_env = [
(0, np.array((0.1,) * 10) * dash_cycle),
(0, np.array((0.6, 0.1, 0.2, 0.1)) * dash_cycle),
(0, np.array((0.3, 0.2, 0.3, 0.2)) * dash_cycle),
'solid',
] # [np.nan, 2500, 250, 25]
xlabels = dict(
big='scale $\\alpha$',
)
ylabels = dict(
filt='$x_{\\text{filt}}$\n$[\\text{a.u.}]$',
env='$x_{\\text{env}}$\n$[\\text{a.u.}]$',
big_pure='$\\sigma_x$',
big_noise='$\\sigma_x\\,/\\,\\sigma_{\\eta}$' if relate_to_noise else None,
)
xlab_big_kwargs = dict(
y=0,
fontsize=fs['lab_norm'],
ha='center',
va='bottom',
)
ylab_snip_kwargs = dict(
x=0.025,
fontsize=fs['lab_tex'],
rotation=0,
ha='center',
va='center',
)
ylab_big_kwargs = dict(
x=-0.2,
fontsize=fs['lab_tex'],
ha='center',
va='bottom',
)
ylim_zoom_factor = 0.03
yloc = dict(
filt=(3, 100),
env=(0.5, 30),
)
ypad = dict(
filt=0.05,
env=0.05,
)
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'],
)
bar_time = 5
bar_kwargs = dict(
dur=bar_time,
y0=-0.2,
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',
)
)
cutoff_leg_kwargs = dict(
ncols=1,
loc='upper left',
bbox_to_anchor=(0.05, 0.5, 0.5, 0.5),
frameon=False,
prop=dict(
size=14,
),
borderpad=0,
borderaxespad=0,
handletextpad=0.3
)
cutoff_leg_kwargs['handlelength'] = 2 * dash_cycle * lw['big'] / cutoff_leg_kwargs['prop']['size']
spec_leg_kwargs = dict(
ncols=2,
loc='lower center',
bbox_to_anchor=(0, 0, 1, 0.5),
frameon=False,
prop=dict(
size=13,
style='italic',
),
borderpad=0,
borderaxespad=0,
handlelength=0.75,
handletextpad=0.5,
columnspacing=1,
)
diag_kwargs = dict(
c=(0.3,) * 3,
lw=2,
ls='--',
zorder=1.9,
)
# PREPARATION:
species_measures = {}
for i, species in enumerate(target_species):
spec_path = search_files(species, incl=['noise', 'norm-base'], dir='../data/inv/rect_lp/condensed/')[0]
spec_data = dict(np.load(spec_path))
measure = spec_data['mean_env'].mean(axis=-1)
if exclude_zero:
measure = measure[spec_data['scales'] > 0, :]
species_measures[species] = measure
# 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']
cutoff_ind = np.nonzero(pure_data['cutoffs'] == snip_cutoff)[0][0]
if relate_to_noise:
# Relate noise-song measures to zero scale:
noise_data['measure_filt'] /= noise_data['measure_filt'][0]
noise_data['measure_env'] /= noise_data['measure_env'][0]
if exclude_zero:
# Exclude zero scales:
inds = pure_scales > 0
pure_scales = pure_scales[inds]
pure_data['measure_filt'] = pure_data['measure_filt'][inds]
pure_data['measure_env'] = pure_data['measure_env'][inds]
inds = noise_scales > 0
noise_scales = noise_scales[inds]
noise_data['measure_filt'] = noise_data['measure_filt'][inds]
noise_data['measure_env'] = noise_data['measure_env'][inds]
symlog_kwargs = dict(linthresh=pure_scales[pure_scales > 0][0], linscale=0.5)
# 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 (ax1, ax2), stage in zip(pure_axes[:, :2], stages):
ax1.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage][0]))
ax2.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage][1]))
ylabel(ax1, 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)
# 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 (ax1, ax2), stage in zip(noise_axes[:, :2], stages):
ax1.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage][0]))
ax2.yaxis.set_major_locator(plt.MultipleLocator(yloc[stage][1]))
ylabel(ax1, 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)
# 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_ylim(scales[0], scales[-1])
ax.set_xscale('symlog', **symlog_kwargs)
ax.set_yscale('symlog', **symlog_kwargs)
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.1, 100)
big_axes[i] = ax
shift_subplot(big_axes[0], dx=big_col_shift)
ylabel(big_axes[0], ylabels['big_pure'], transform=big_axes[0].transAxes, **ylab_big_kwargs)
ylabel(big_axes[1], ylabels['big_noise'], transform=big_axes[1].transAxes, **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 filtered snippets:
handle = plot_snippets(pure_axes[0, :], t_full, pure_data['snip_filt'],
c=colors['filt'], lw=lw['snip'], ypad=ypad['filt'])
# Plot pure-song envelope snippets:
plot_snippets(pure_axes[1, :], t_full, pure_data['snip_env'][..., cutoff_ind],
ymin=0, c=colors['env'], lw=lw['snip'], ypad=ypad['env'])
# Plot noise-song filtered snippets:
handle = plot_snippets(noise_axes[0, :], t_full, noise_data['snip_filt'], ypad=ypad['filt'],
*pure_axes[0, 0].get_ylim(), c=colors['filt'], lw=lw['snip'])
# Plot noise-song envelope snippets:
plot_snippets(noise_axes[1, :], t_full, noise_data['snip_env'][..., cutoff_ind],
*pure_axes[1, 0].get_ylim(), c=colors['env'], lw=lw['snip'])
# Zoom into first filtered snippet:
# ylim_zoom = np.array(noise_axes[0, -1].get_ylim()) * ylim_zoom_factor
# noise_axes[0, 0].set_ylim(*ylim_zoom)
ylim_zoom = ylimits(noise_data['snip_filt'][:, 0], noise_axes[0, 0], pad=ypad['filt'])
pure_axes[0, 0].set_ylim(*ylim_zoom)
# Zoom into first envelope snippet:
# ylim_zoom = np.array(noise_axes[1, -1].get_ylim()) * ylim_zoom_factor
# noise_axes[1, 0].set_ylim(*ylim_zoom)
ylim_zoom = ylimits(noise_data['snip_env'][:, 0, cutoff_ind], noise_axes[1, 0], minval=0, pad=ypad['env'])
pure_axes[1, 0].set_ylim(*ylim_zoom)
# 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_filt'], c=colors['filt'], lw=lw['big'])
handles = big_axes[0].plot(pure_scales, pure_data['measure_env'], c=colors['env'], lw=lw['big'])
[handle.set_ls(ls) for handle, ls in zip(handles, ls_env)]
# Plot noise-song measures (limited):
big_axes[1].plot(noise_scales, noise_data['measure_filt'], c=colors['filt'], lw=lw['big'])
handles = big_axes[1].plot(noise_scales, noise_data['measure_env'], c=colors['env'], lw=lw['big'])
[handle.set_ls(ls) for handle, ls in zip(handles, ls_env)]
# Add proxy legend:
proxy_handles = []
for i, cutoff in enumerate(pure_data['cutoffs']):
label = '$\\text{unfiltered}$' if np.isnan(cutoff) else f'${int(cutoff)}\\,\\text{{Hz}}$'
proxy_handles.append(big_axes[0].plot([], [], c=colors['env'], lw=lw['big'],
ls=ls_env[i], label=label)[0])
big_axes[0].legend(handles=proxy_handles, **cutoff_leg_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)
# Plot species-specific noise-song invariance curves:
leg_handles = []
for i, (species, measure) in enumerate(species_measures.items()):
handles = big_axes[2].plot(noise_scales, measure, label=shorten_species(species),
c=species_colors[species], lw=lw['spec'])
[handle.set_ls(ls) for handle, ls in zip(handles, ls_env)]
leg_handles.append(handles[-1])
legend = big_axes[2].legend(handles=leg_handles, **spec_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()