Holiday syncing :)
This commit is contained in:
@@ -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_kde, 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):
|
||||
@@ -26,39 +27,86 @@ def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
||||
handles.extend(plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs))
|
||||
return handles
|
||||
|
||||
def plot_dist_shifted(ax, data, axis, pdf=None, sigma=0.1, which='x',
|
||||
base=None, cap=None, add_pdf=False, shifted=False, **kwargs):
|
||||
if pdf is None:
|
||||
pdf, axis = get_kde(data, sigma, axis)
|
||||
if base is None:
|
||||
base = pdf.min()
|
||||
if cap is None:
|
||||
cap = pdf.max()
|
||||
pdf = (pdf - pdf.min()) / (pdf.max() - pdf.min()) * (cap - base) + base
|
||||
|
||||
if which == 'x':
|
||||
transform = ax.get_xaxis_transform()
|
||||
elif which == 'y':
|
||||
transform = ax.get_yaxis_transform()
|
||||
else:
|
||||
transform = ax.transData
|
||||
|
||||
rng = np.random.default_rng()
|
||||
handles = []
|
||||
for value in data:
|
||||
ind = np.nonzero(axis == value)[0][0]
|
||||
offset = base if not shifted else rng.uniform(base, pdf[ind])
|
||||
variables = (offset, value) if which=='y' else (value, offset)
|
||||
handles.extend(ax.plot(*variables, transform=transform, **kwargs))
|
||||
if add_pdf:
|
||||
variables = (pdf, axis) if which=='y' else (axis, pdf)
|
||||
pdf_handle = ax.plot(*variables, transform=transform, c='k', lw=1)
|
||||
return handles, pdf_handle
|
||||
return handles
|
||||
|
||||
def zalpha(handles, background='w', down=1):
|
||||
twins = []
|
||||
for handle in handles:
|
||||
twin = handle.copy()
|
||||
twin.set(color=background, alpha=1)
|
||||
twin.set_zorder(handle.get_zorder() - down)
|
||||
twins.append(twin)
|
||||
return twins
|
||||
|
||||
# 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/')
|
||||
ref_path = '../data/inv/log_hp/ref_measures.npz'
|
||||
save_path = '../figures/fig_invariance_log_hp.pdf'
|
||||
target_species = [
|
||||
'Omocestus_rufipes',
|
||||
'Chorthippus_biguttulus',
|
||||
'Chorthippus_mollis',
|
||||
'Chrysochraon_dispar',
|
||||
'Gomphocerippus_rufus',
|
||||
'Pseudochorthippus_parallelus',
|
||||
]
|
||||
stages = ['env', 'log', 'inv']
|
||||
load_kwargs = dict(
|
||||
files=stages,
|
||||
keywords=['scales', 'snip', 'measure']
|
||||
)
|
||||
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, 16/2.54),
|
||||
figsize=(32/2.54, 32/2.54),
|
||||
)
|
||||
super_grid_kwargs = dict(
|
||||
nrows=2,
|
||||
ncols=3,
|
||||
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=(0, slice(0, -1)),
|
||||
noise=(1, slice(0, -1)),
|
||||
big=(slice(None), -1),
|
||||
pure=(0, slice(None)),
|
||||
noise=(1, slice(None)),
|
||||
big=(2, slice(None)),
|
||||
)
|
||||
block_height = 0.8
|
||||
edge_padding = 0.08
|
||||
@@ -67,7 +115,7 @@ pure_grid_kwargs = dict(
|
||||
ncols=None,
|
||||
wspace=0.1,
|
||||
hspace=0.15,
|
||||
left=0.16,
|
||||
left=0.11,
|
||||
right=0.95,
|
||||
bottom=1 - block_height - edge_padding,
|
||||
top=1 - edge_padding,
|
||||
@@ -76,23 +124,23 @@ pure_grid_kwargs = dict(
|
||||
noise_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
ncols=None,
|
||||
wspace=0.1,
|
||||
hspace=0.15,
|
||||
left=0.16,
|
||||
right=0.95,
|
||||
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=2,
|
||||
ncols=1,
|
||||
wspace=0,
|
||||
hspace=0.3,
|
||||
left=0.19,
|
||||
right=0.96,
|
||||
bottom=0.09,
|
||||
top=0.98
|
||||
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',
|
||||
@@ -110,8 +158,14 @@ fs = dict(
|
||||
bar=16,
|
||||
)
|
||||
colors = load_colors('../data/stage_colors.npz')
|
||||
lw_snippets = 1
|
||||
lw_big = 3
|
||||
species_colors = load_colors('../data/species_colors.npz')
|
||||
noise_colors = [(0.5, 0.5, 0.5), (0.7, 0.7, 0.7)]
|
||||
lw = dict(
|
||||
snip=1,
|
||||
big=4,
|
||||
spec=2,
|
||||
plateau=1,
|
||||
)
|
||||
xlabels = dict(
|
||||
big='scale $\\alpha$',
|
||||
)
|
||||
@@ -135,7 +189,7 @@ ylab_snip_kwargs = dict(
|
||||
va='center',
|
||||
)
|
||||
ylab_big_kwargs = dict(
|
||||
x=0.05,
|
||||
x=0,
|
||||
fontsize=fs['lab_tex'],
|
||||
ha='center',
|
||||
va='top',
|
||||
@@ -160,10 +214,10 @@ letter_snip_kwargs = dict(
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
letter_big_kwargs = dict(
|
||||
x=0.05,
|
||||
yref=letter_snip_kwargs['yref'],
|
||||
x=0,
|
||||
y=1,
|
||||
ha='left',
|
||||
va='center',
|
||||
va='bottom',
|
||||
fontsize=fs['letter'],
|
||||
)
|
||||
zoom_inset_bounds = [0.1, 0.2, 0.8, 0.6]
|
||||
@@ -204,33 +258,77 @@ 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_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=10,
|
||||
markeredgecolor='k',
|
||||
markeredgewidth=1,
|
||||
# alpha=1,
|
||||
zorder=6,
|
||||
clip_on=False,
|
||||
# base=0,
|
||||
# cap=0.15,
|
||||
# add_pdf=True,
|
||||
)
|
||||
kde_kwargs = dict(
|
||||
sigma=0.1,
|
||||
)
|
||||
|
||||
# PREPARATION:
|
||||
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')}
|
||||
ref_measures = dict(np.load(ref_path))
|
||||
|
||||
species_measures = []
|
||||
for species_path in species_paths:
|
||||
species_data, _ = load_data(species_path, **load_kwargs)
|
||||
species_measure = species_data['measure_inv']
|
||||
species_measures = {}
|
||||
thresh_inds = np.zeros((len(target_species),), dtype=int)
|
||||
thresh_scales = np.zeros((len(target_species),), dtype=float)
|
||||
for i, species in enumerate(target_species):
|
||||
path = search_files(species, incl='noise', dir='../data/inv/log_hp/')[0]
|
||||
species_data = load_data(path, **load_kwargs)[0]
|
||||
measure = species_data['measure_inv']
|
||||
scales = species_data['scales']
|
||||
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
|
||||
thresh_inds[i] = get_saturation(measure, **plateau_settings)[1]
|
||||
thresh_scales[i] = scales[thresh_inds[i]]
|
||||
thresh_pdf, pdf_axis = get_kde(thresh_scales, axis=scales, **kde_kwargs)
|
||||
|
||||
# EXECUTION:
|
||||
for data_path in data_paths:
|
||||
@@ -273,7 +371,7 @@ for data_path in data_paths:
|
||||
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, 'c', ref=noise_title, **letter_snip_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)
|
||||
@@ -282,51 +380,49 @@ for data_path in data_paths:
|
||||
# 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.nrows,), dtype=object)
|
||||
for i, scales in enumerate([pure_scales, noise_scales]):
|
||||
ax = big_subfig.add_subplot(big_grid[i, 0])
|
||||
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', 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(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_title, **letter_big_kwargs)
|
||||
if i > 0:
|
||||
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_snippets)[0]
|
||||
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_snippets)
|
||||
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_snippets)
|
||||
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_snippets)[0]
|
||||
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_snippets)
|
||||
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_snippets)
|
||||
ymin, ymax, c=colors['inv'], lw=lw['snip'])
|
||||
|
||||
# Indicate time scale:
|
||||
time_bar(noise_axes[-1, -1], **bar_kwargs)
|
||||
@@ -342,34 +438,46 @@ for data_path in data_paths:
|
||||
noise_data['measure_inv'] /= ref_measures['inv']
|
||||
|
||||
# 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)
|
||||
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)
|
||||
|
||||
# Plot species measures:
|
||||
big_axes[1].plot(noise_scales, species_measures, 'k', lw=lw_big, zorder=2.1)
|
||||
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_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_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 measures:
|
||||
for i, (species, measure) in enumerate(species_measures.items()):
|
||||
color = species_colors[species]
|
||||
ind, scale = thresh_inds[i], thresh_scales[i]
|
||||
big_axes[2].plot(noise_scales, measure, label=shorten_species(species),
|
||||
c=color, lw=lw['spec'])
|
||||
big_axes[2].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
||||
transform=big_axes[2].get_xaxis_transform())
|
||||
handle = big_axes[2].plot(scale, 0, c=color, alpha=0.5, **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)
|
||||
big_axes[2].legend(**leg_kwargs)
|
||||
|
||||
# handles = plot_dist_shifted(big_axes[2], species_threshs, axis=pdf_axis,
|
||||
# pdf=thresh_pdf, **plateau_dot_kwargs)[0]
|
||||
# [h.set_color(species_colors[s]) for h, s in zip(handles, target_species)]
|
||||
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path, bbox_inches='tight')
|
||||
|
||||
Reference in New Issue
Block a user