Cross-checked and polished remainders of fig_invariance_thresh_lp_species.pdf.

Added misc_functions.py for anything not plot-related.
This commit is contained in:
j-hartling
2026-03-31 09:36:55 +02:00
parent 411d50ffcf
commit 298969a067
12 changed files with 251 additions and 165 deletions

View File

@@ -4,10 +4,11 @@ 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, xlabel, ylabel, hide_ticks,\
from plot_functions import hide_axis, ylimits, super_xlabel, ylabel, hide_ticks,\
plot_line, strip_zeros, time_bar, zoom_inset,\
letter_subplot, title_subplot
letter_subplot, letter_subplots, title_subplot
from IPython import embed
def add_snip_axes(fig, grid_kwargs):
@@ -30,7 +31,14 @@ def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
# GENERAL SETTINGS:
target = 'Omocestus_rufipes'
data_paths = search_files(target, excl='noise', dir='../data/inv/log_hp/')
species_paths = search_files('*', incl='noise', dir='../data/inv/log_hp/')
target_species = [
'Omocestus_rufipes',
'Chorthippus_biguttulus',
'Chorthippus_mollis',
'Chrysochraon_dispar',
'Gomphocerippus_rufus',
'Pseudochorthippus_parallelus',
]
stages = ['env', 'log', 'inv']
load_kwargs = dict(
files=stages,
@@ -39,28 +47,29 @@ load_kwargs = dict(
save_path = '../figures/fig_invariance_log_hp.pdf'
compute_ratios = True
show_diag = True
show_noise = True
show_plateaus = True
# GRAPH SETTINGS:
fig_kwargs = dict(
figsize=(32/2.54, 32/2.54),
)
snip_rows = 1
big_rows = 1
# snip_rows = 1
# big_rows = 1
super_grid_kwargs = dict(
nrows=2 * snip_rows + big_rows,
nrows=3,
ncols=1,
wspace=0,
hspace=0,
left=0,
right=1,
bottom=0,
top=1
top=1,
height_ratios=[1, 1, 1]
)
subfig_specs = dict(
pure=(slice(0, snip_rows), slice(None)),
noise=(slice(snip_rows, 2 * snip_rows), slice(None)),
big=(slice(-big_rows, None), slice(None)),
pure=(0, slice(None)),
noise=(1, slice(None)),
big=(2, slice(None)),
)
block_height = 0.8
edge_padding = 0.08
@@ -112,6 +121,8 @@ fs = dict(
bar=16,
)
colors = load_colors('../data/stage_colors.npz')
species_colors = load_colors('../data/species_colors.npz')
noise_colors = [(0.5, 0.5, 0.5), (0.7, 0.7, 0.7)]
lw_snippets = 1
lw_big = 3
xlabels = dict(
@@ -206,15 +217,34 @@ bar_kwargs = dict(
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.75, 0.75, 0.75),
lw=2,
ls='--',
zorder=1.9,
)
noise_rel_thresh = 0.95
noise_kwargs = dict(
fc=(0.9, 0.9, 0.9),
plateau_settings = dict(
low=0.05,
high=0.95,
first=True,
last=True,
condense=None,
)
plateau_kwargs = dict(
ec='none',
lw=0,
zorder=1.5,
@@ -225,13 +255,13 @@ 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')}
species_measures = []
for species_path in species_paths:
species_measure = load_data(species_path, **load_kwargs)[0]['measure_inv']
species_measures = {}
for species in target_species:
path = search_files(species, incl='noise', dir='../data/inv/log_hp/')[0]
measure = load_data(path, **load_kwargs)[0]['measure_inv']
if compute_ratios:
species_measure /= ref_measures['inv']
species_measures.append(species_measure)
species_measures = np.array(species_measures).T
measure /= ref_measures['inv']
species_measures[species] = measure
# EXECUTION:
for data_path in data_paths:
@@ -291,14 +321,10 @@ for data_path in data_paths:
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(ax, 'c', **letter_big_kwargs)
else:
xlabel(ax, xlabels['big'], transform=big_subfig.transSubfigure, **xlab_big_kwargs)
letter_subplot(ax, 'd', **letter_big_kwargs)
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'],
@@ -352,25 +378,26 @@ 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)
# Plot species measures:
big_axes[2].plot(noise_scales, species_measures, 'k', 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_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 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_kwargs)
big_axes[1].axvspan(noise_scales[low_ind], noise_scales[high_ind],
fc=noise_colors[1], **plateau_kwargs)
# Plot species-specific noise-song measures:
for species, measure in species_measures.items():
label = shorten_species(species)
big_axes[2].plot(noise_scales, measure, label=label,
c=species_colors[species], lw=lw_big)
big_axes[2].legend(**leg_kwargs)
if save_path is not None:
fig.savefig(save_path, bbox_inches='tight')