Added loads of units in nearly all graphs.
Overhauled fig_invariance_full.pdf. Added some legends, somewhere.
This commit is contained in:
@@ -5,7 +5,8 @@ from itertools import product
|
||||
from thunderhopper.filetools import search_files
|
||||
from thunderhopper.modeltools import load_data
|
||||
from thunderhopper.filtertools import find_kern_specs
|
||||
from misc_functions import get_saturation
|
||||
from misc_functions import get_saturation, reduce_kernel_set, exclude_zero_scale,\
|
||||
divide_by_zero
|
||||
from color_functions import load_colors
|
||||
from plot_functions import hide_axis, reorder_by_sd, ylimits, super_xlabel, ylabel, title_subplot,\
|
||||
plot_line, strip_zeros, time_bar, assign_colors,\
|
||||
@@ -20,30 +21,15 @@ def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
||||
ymin=ymin, ymax=ymax, **kwargs))
|
||||
return handles
|
||||
|
||||
def plot_curves(ax, scales, measures, fill_kwargs={}, **kwargs):
|
||||
if measures.ndim == 1:
|
||||
ax.plot(scales, measures, **kwargs)[0]
|
||||
return measures
|
||||
median_measure = np.median(measures, axis=1)
|
||||
spread_measure = [np.percentile(measures, 25, axis=1),
|
||||
np.percentile(measures, 75, axis=1)]
|
||||
ax.plot(scales, median_measure, **kwargs)[0]
|
||||
ax.fill_between(scales, *spread_measure, **fill_kwargs)
|
||||
return median_measure
|
||||
|
||||
def exclude_zero_scale(data, stages):
|
||||
inds = data['scales'] > 0
|
||||
data['scales'] = data['scales'][inds]
|
||||
for stage in stages:
|
||||
data[f'mean_{stage}'] = data[f'mean_{stage}'][inds, ...]
|
||||
return data
|
||||
|
||||
def reduce_kernel_set(data, inds, keyword, stages=['conv', 'feat']):
|
||||
for stage in stages:
|
||||
key = f'{keyword}_{stage}'
|
||||
data[key] = data[key][:, inds, ...]
|
||||
return data
|
||||
|
||||
def plot_curves(ax, scales, measures, fill_kwargs={}, compress=False, **kwargs):
|
||||
if not compress or measures.ndim == 1:
|
||||
handles = ax.plot(scales, measures, **kwargs)
|
||||
return handles, measures
|
||||
median_measure = np.nanmedian(measures, axis=1)
|
||||
spread_measure = np.nanpercentile(measures, [25, 75], axis=1)
|
||||
line_handle = ax.plot(scales, median_measure, **kwargs)[0]
|
||||
fill_handle = ax.fill_between(scales, *spread_measure, **fill_kwargs)
|
||||
return [line_handle, fill_handle], median_measure
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
target_species = [
|
||||
@@ -65,29 +51,28 @@ example_file = {
|
||||
'Pseudochorthippus_parallelus': 'Pseudochorthippus_parallelus_GBC_88-6s678ms-9s32.3ms'
|
||||
}[target_species]
|
||||
stages = ['filt', 'env', 'log', 'inv', 'conv', 'feat']
|
||||
raw_path = search_files(target_species, incl='unnormed', dir='../data/inv/full/condensed/')[0]
|
||||
base_path = search_files(target_species, incl='base', dir='../data/inv/full/condensed/')[0]
|
||||
range_path = search_files(target_species, incl='range', dir='../data/inv/full/condensed/')[0]
|
||||
snip_path = search_files(example_file, dir='../data/inv/full/')[0]
|
||||
data_path = search_files(example_file, dir='../data/inv/full/')[0]
|
||||
save_path = '../figures/fig_invariance_full.pdf'
|
||||
|
||||
# ANALYSIS SETTINGS:
|
||||
exclude_zero = True
|
||||
compress_kernels = True
|
||||
thresh_rel = np.array([0, 0.5, 1, 1.5, 2, 2.5, 3])[4]
|
||||
scale_subset_kwargs = dict(
|
||||
combis=[['measure'], stages],
|
||||
)
|
||||
kern_subset_kwargs = dict(
|
||||
combis=[['measure', 'snip'], ['conv', 'feat']],
|
||||
keys=['thresh_abs'],
|
||||
)
|
||||
|
||||
# SUBSET SETTINGS:
|
||||
types = np.array([1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10])
|
||||
sigmas = np.array([0.001, 0.002, 0.004, 0.008, 0.016, 0.032])
|
||||
# types = [1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10]
|
||||
sigmas = np.array([0.001, 0.002, 0.004, 0.008, 0.016, 0.032])
|
||||
# sigmas = [0.001, 0.002, 0.004, 0.008, 0.016, 0.032]
|
||||
kernels = np.array([
|
||||
[1, 0.002],
|
||||
[-1, 0.002],
|
||||
[2, 0.004],
|
||||
[-2, 0.004],
|
||||
[3, 0.032],
|
||||
[-3, 0.032]
|
||||
])
|
||||
kernels = None
|
||||
reduce_kernels = any(var is not None for var in [kernels, types, sigmas])
|
||||
|
||||
# GRAPH SETTINGS:
|
||||
fig_kwargs = dict(
|
||||
@@ -113,7 +98,7 @@ snip_grid_kwargs = dict(
|
||||
ncols=None,
|
||||
wspace=0.1,
|
||||
hspace=0.4,
|
||||
left=0.11,
|
||||
left=0.13,
|
||||
right=0.98,
|
||||
bottom=0.08,
|
||||
top=0.95
|
||||
@@ -138,9 +123,11 @@ fs = dict(
|
||||
tit_tex=20,
|
||||
bar=16,
|
||||
)
|
||||
colors = load_colors('../data/stage_colors.npz')
|
||||
conv_colors = load_colors('../data/conv_colors_all.npz')
|
||||
feat_colors = load_colors('../data/feat_colors_all.npz')
|
||||
stage_colors = load_colors('../data/stage_colors.npz')
|
||||
kern_colors = dict(
|
||||
conv=load_colors('../data/conv_colors_all.npz'),
|
||||
feat=load_colors('../data/feat_colors_all.npz')
|
||||
)
|
||||
lw = dict(
|
||||
filt=0.25,
|
||||
env=0.25,
|
||||
@@ -150,16 +137,17 @@ lw = dict(
|
||||
feat=1,
|
||||
big=3,
|
||||
plateau=1.5,
|
||||
legend=5,
|
||||
)
|
||||
xlabels = dict(
|
||||
big='scale $\\alpha$',
|
||||
)
|
||||
ylabels = dict(
|
||||
filt='$x_{\\text{filt}}$',
|
||||
env='$x_{\\text{env}}$',
|
||||
log='$x_{\\text{db}}$',
|
||||
inv='$x_{\\text{adapt}}$',
|
||||
conv='$c_i$',
|
||||
filt='$x_{\\text{filt}}$\n$[\\text{a.u.}]$',
|
||||
env='$x_{\\text{env}}$\n$[\\text{a.u.}]$',
|
||||
log='$x_{\\text{log}}$\n$[\\text{dB}]$',
|
||||
inv='$x_{\\text{adapt}}$\n$[\\text{dB}]$',
|
||||
conv='$c_i$\n$[\\text{dB}]$',
|
||||
feat='$f_i$',
|
||||
big=['measure', 'rel. measure', 'norm. measure']
|
||||
)
|
||||
@@ -170,11 +158,12 @@ xlab_big_kwargs = dict(
|
||||
va='bottom',
|
||||
)
|
||||
ylab_snip_kwargs = dict(
|
||||
x=0,
|
||||
x=0.03,
|
||||
fontsize=fs['lab_tex'],
|
||||
rotation=0,
|
||||
ha='left',
|
||||
va='center'
|
||||
ha='center',
|
||||
va='center',
|
||||
ma='center'
|
||||
)
|
||||
ylab_big_kwargs = dict(
|
||||
x=-0.2,
|
||||
@@ -228,6 +217,29 @@ bar_kwargs = dict(
|
||||
va='center',
|
||||
)
|
||||
)
|
||||
leg_kwargs = dict(
|
||||
ncols=1,
|
||||
loc='upper left',
|
||||
bbox_to_anchor=(0.05, 0.5, 0.5, 0.5),
|
||||
frameon=False,
|
||||
prop=dict(
|
||||
size=20,
|
||||
),
|
||||
borderpad=0,
|
||||
borderaxespad=0,
|
||||
handlelength=1,
|
||||
columnspacing=1,
|
||||
handletextpad=0.5,
|
||||
labelspacing=0.1
|
||||
)
|
||||
leg_labels = dict(
|
||||
filt='$x_{\\text{filt}}$',
|
||||
env='$x_{\\text{env}}$',
|
||||
log='$x_{\\text{log}}$',
|
||||
inv='$x_{\\text{adapt}}$',
|
||||
conv='$c_i$',
|
||||
feat='$f_i$'
|
||||
)
|
||||
plateau_settings = dict(
|
||||
low=0.05,
|
||||
high=0.95,
|
||||
@@ -249,26 +261,30 @@ plateau_dot_kwargs = dict(
|
||||
|
||||
# EXECUTION:
|
||||
|
||||
# Load raw (unnormed) invariance data:
|
||||
data, config = load_data(raw_path, files='scales', keywords='mean')
|
||||
if exclude_zero:
|
||||
data = exclude_zero_scale(data, stages)
|
||||
scales = data['scales']
|
||||
# Load invariance data:
|
||||
data, config = load_data(data_path, keywords=['snip', 'scales', 'measure', 'thresh'])
|
||||
t_full = np.arange(data['snip_filt'].shape[0]) / config['rate']
|
||||
|
||||
# Load snippet data:
|
||||
snip, _ = load_data(snip_path, files='example_scales', keywords='snip')
|
||||
t_full = np.arange(snip['snip_filt'].shape[0]) / config['rate']
|
||||
snip_scales = snip['example_scales']
|
||||
|
||||
# Optional kernel subset:
|
||||
reduce_kernels = False
|
||||
if any(var is not None for var in [kernels, types, sigmas]):
|
||||
# Reduce kernels:
|
||||
if reduce_kernels:
|
||||
kern_inds = find_kern_specs(config['k_specs'], kernels, types, sigmas)
|
||||
data = reduce_kernel_set(data, kern_inds, keyword='mean')
|
||||
snip = reduce_kernel_set(snip, kern_inds, keyword='snip')
|
||||
data = reduce_kernel_set(data, kern_inds, **kern_subset_kwargs)
|
||||
config['k_specs'] = config['k_specs'][kern_inds, :]
|
||||
config['kernels'] = config['kernels'][:, kern_inds]
|
||||
reduce_kernels = True
|
||||
|
||||
# Reduce thresholds:
|
||||
thresh_ind = np.nonzero(data['thresh_rel'] == thresh_rel)[0][0]
|
||||
data['measure_feat'] = data['measure_feat'][:, :, thresh_ind]
|
||||
data['snip_feat'] = data['snip_feat'][:, :, :, thresh_ind]
|
||||
|
||||
# Remember pure-noise reference measures:
|
||||
ref_data = {stage: data[f'measure_{stage}'][0, ...] for stage in stages}
|
||||
|
||||
# Reduce scales:
|
||||
if exclude_zero:
|
||||
data = exclude_zero_scale(data, **scale_subset_kwargs)
|
||||
scales = data['scales']
|
||||
snip_scales = data['example_scales']
|
||||
|
||||
# Adjust grid parameters:
|
||||
snip_grid_kwargs['ncols'] = snip_scales.size
|
||||
@@ -307,114 +323,125 @@ for i in range(big_grid.ncols):
|
||||
ax.set_xscale('symlog', linthresh=scales[1], linscale=0.5)
|
||||
ax.set_yscale('symlog', linthresh=0.01, linscale=0.1)
|
||||
ylabel(ax, ylabels['big'][i], **ylab_big_kwargs)
|
||||
if i < (big_grid.ncols - 1):
|
||||
ax.set_ylim(scales[0], scales[-1])
|
||||
else:
|
||||
ax.set_ylim(0, 1)
|
||||
# if i < (big_grid.ncols - 1):
|
||||
# ax.set_ylim(scales[0], scales[-1])
|
||||
# else:
|
||||
# ax.set_ylim(0, 1)
|
||||
big_axes[i] = ax
|
||||
super_xlabel(xlabels['big'], big_subfig, big_axes[0], big_axes[-1], **xlab_big_kwargs)
|
||||
letter_subplots(big_axes, 'bcd', **letter_big_kwargs)
|
||||
|
||||
if True:
|
||||
# Plot filtered snippets:
|
||||
plot_snippets(snip_axes[0, :], t_full, snip['snip_filt'],
|
||||
c=colors['filt'], lw=lw['filt'])
|
||||
plot_snippets(snip_axes[0, :], t_full, data['snip_filt'],
|
||||
c=stage_colors['filt'], lw=lw['filt'])
|
||||
|
||||
# Plot envelope snippets:
|
||||
plot_snippets(snip_axes[1, :], t_full, snip['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw['env'])
|
||||
plot_snippets(snip_axes[1, :], t_full, data['snip_env'],
|
||||
ymin=0, c=stage_colors['env'], lw=lw['env'])
|
||||
|
||||
# Plot logarithmic snippets:
|
||||
plot_snippets(snip_axes[2, :], t_full, snip['snip_log'],
|
||||
c=colors['log'], lw=lw['log'])
|
||||
plot_snippets(snip_axes[2, :], t_full, data['snip_log'],
|
||||
c=stage_colors['log'], lw=lw['log'])
|
||||
|
||||
# Plot invariant snippets:
|
||||
plot_snippets(snip_axes[3, :], t_full, snip['snip_inv'],
|
||||
c=colors['inv'], lw=lw['inv'])
|
||||
plot_snippets(snip_axes[3, :], t_full, data['snip_inv'],
|
||||
c=stage_colors['inv'], lw=lw['inv'])
|
||||
|
||||
# Plot kernel response snippets:
|
||||
all_handles = plot_snippets(snip_axes[4, :], t_full, snip['snip_conv'],
|
||||
c=colors['conv'], lw=lw['conv'])
|
||||
all_handles = plot_snippets(snip_axes[4, :], t_full, data['snip_conv'],
|
||||
c=stage_colors['conv'], lw=lw['conv'])
|
||||
for i, handles in enumerate(all_handles):
|
||||
assign_colors(handles, config['k_specs'][:, 0], conv_colors)
|
||||
reorder_by_sd(handles, snip['snip_conv'][..., i])
|
||||
assign_colors(handles, config['k_specs'][:, 0], kern_colors['conv'])
|
||||
reorder_by_sd(handles, data['snip_conv'][..., i])
|
||||
|
||||
# Plot feature snippets:
|
||||
all_handles = plot_snippets(snip_axes[5, :], t_full, snip['snip_feat'],
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
all_handles = plot_snippets(snip_axes[5, :], t_full, data['snip_feat'],
|
||||
ymin=0, ymax=1, c=stage_colors['feat'], lw=lw['feat'])
|
||||
for i, handles in enumerate(all_handles):
|
||||
assign_colors(handles, config['k_specs'][:, 0], feat_colors)
|
||||
reorder_by_sd(handles, snip['snip_feat'][..., i])
|
||||
del snip
|
||||
assign_colors(handles, config['k_specs'][:, 0], kern_colors['feat'])
|
||||
reorder_by_sd(handles, data['snip_feat'][..., i])
|
||||
|
||||
# Remember saturation points:
|
||||
# Plot analysis results:
|
||||
crit_inds, crit_scales = {}, {}
|
||||
|
||||
# Unnormed measures:
|
||||
leg_handles = []
|
||||
for stage in stages:
|
||||
# Plot average intensity measure across recordings:
|
||||
curve = plot_curves(big_axes[0], scales, data[f'mean_{stage}'].mean(axis=-1),
|
||||
c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
# Indicate saturation point:
|
||||
mkey = f'measure_{stage}'
|
||||
measure = data[mkey]
|
||||
color = stage_colors[stage]
|
||||
fill_kwargs = dict(color=color, alpha=0.25)
|
||||
|
||||
# Plot raw intensity measure curve(s):
|
||||
handles, curve = plot_curves(big_axes[0], scales, measure, fill_kwargs,
|
||||
compress_kernels, c=color, lw=lw['big'])
|
||||
if not compress_kernels and stage in ['conv', 'feat']:
|
||||
assign_colors(handles, config['k_specs'][:, 0], kern_colors[stage])
|
||||
|
||||
# Add stage-specific proxy legend artist:
|
||||
leg_handles.append(big_axes[0].plot([], [], c=color, lw=lw['big'],
|
||||
label=leg_labels[stage])[0])
|
||||
|
||||
# Indicate saturation point(s):
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
ind = get_saturation(curve, **plateau_settings)[1]
|
||||
scale = scales[ind]
|
||||
big_axes[0].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
||||
transform=big_axes[0].get_xaxis_transform())
|
||||
big_axes[0].plot(scale, 0, mfc=colors[stage], mec='k', alpha=0.75, zorder=6, **plateau_dot_kwargs,
|
||||
transform=big_axes[0].get_xaxis_transform())
|
||||
big_axes[0].vlines(scale, big_axes[0].get_ylim()[0], curve[ind],
|
||||
color=colors[stage], **plateau_line_kwargs)
|
||||
# Log saturation point:
|
||||
crit_inds[stage] = ind
|
||||
crit_scales[stage] = scale
|
||||
del data
|
||||
if compress_kernels or stage in ['log', 'inv']:
|
||||
scale = scales[ind]
|
||||
crit_scales[stage] = scale
|
||||
big_axes[0].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
||||
transform=big_axes[0].get_xaxis_transform())
|
||||
big_axes[0].plot(scale, 0, mfc=color, mec='k', alpha=0.75, zorder=6, **plateau_dot_kwargs,
|
||||
transform=big_axes[0].get_xaxis_transform())
|
||||
big_axes[0].vlines(scale, big_axes[0].get_ylim()[0], curve[ind],
|
||||
color=color, **plateau_line_kwargs)
|
||||
|
||||
# Noise baseline-related measures:
|
||||
data, _ = load_data(base_path, files='scales', keywords='mean')
|
||||
if exclude_zero:
|
||||
data = exclude_zero_scale(data, stages)
|
||||
if reduce_kernels:
|
||||
data = reduce_kernel_set(data, kern_inds, keyword='mean')
|
||||
for stage in stages:
|
||||
# Plot average intensity measure across recordings:
|
||||
curve = plot_curves(big_axes[1], scales, data[f'mean_{stage}'].mean(axis=-1),
|
||||
c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
# Indicate saturation point:
|
||||
# Relate to noise baseline:
|
||||
measure = divide_by_zero(data[mkey], ref_data[stage])
|
||||
|
||||
# Plot baseline-normalized ntensity measure curve(s):
|
||||
handles, curve = plot_curves(big_axes[1], scales, measure, fill_kwargs,
|
||||
compress_kernels, c=color, lw=lw['big'])
|
||||
if not compress_kernels and stage in ['conv', 'feat']:
|
||||
assign_colors(handles, config['k_specs'][:, 0], kern_colors[stage])
|
||||
|
||||
# Indicate saturation point(s):
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
ind, scale = crit_inds[stage], crit_scales[stage]
|
||||
big_axes[1].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
||||
transform=big_axes[1].get_xaxis_transform())
|
||||
big_axes[1].plot(scale, 0, mfc=colors[stage], mec='k', alpha=0.75, zorder=6, **plateau_dot_kwargs,
|
||||
transform=big_axes[1].get_xaxis_transform())
|
||||
big_axes[1].vlines(scale, big_axes[1].get_ylim()[0], curve[ind],
|
||||
color=colors[stage], **plateau_line_kwargs)
|
||||
del data
|
||||
ind = crit_inds[stage]
|
||||
scale = crit_scales[stage]
|
||||
if compress_kernels or stage in ['log', 'inv']:
|
||||
big_axes[1].plot(scale, 0, c='w', alpha=1, zorder=5.5, **plateau_dot_kwargs,
|
||||
transform=big_axes[1].get_xaxis_transform())
|
||||
big_axes[1].plot(scale, 0, mfc=color, mec='k', alpha=0.75, zorder=6, **plateau_dot_kwargs,
|
||||
transform=big_axes[1].get_xaxis_transform())
|
||||
big_axes[1].vlines(scale, big_axes[1].get_ylim()[0], curve[ind],
|
||||
color=color, **plateau_line_kwargs)
|
||||
if stage in ['filt', 'env']:
|
||||
continue
|
||||
|
||||
# Min-max normalized measures:
|
||||
data, _ = load_data(range_path, files='scales', keywords='mean')
|
||||
if exclude_zero:
|
||||
data = exclude_zero_scale(data, stages)
|
||||
if reduce_kernels:
|
||||
data = reduce_kernel_set(data, kern_inds, keyword='mean')
|
||||
for stage in ['log', 'inv', 'conv', 'feat']:
|
||||
# Plot average intensity measure across recordings:
|
||||
curve = plot_curves(big_axes[2], scales, data[f'mean_{stage}'].mean(axis=-1),
|
||||
c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
# Relate to curve maximum:
|
||||
measure = data[mkey] / np.nanmax(data[mkey], axis=0)
|
||||
|
||||
# Indicate saturation point:
|
||||
# Plot max-normalized ntensity measure curve(s):
|
||||
handles, curve = plot_curves(big_axes[2], scales, measure, fill_kwargs,
|
||||
compress_kernels, c=color, lw=lw['big'])
|
||||
if not compress_kernels and stage in ['conv', 'feat']:
|
||||
assign_colors(handles, config['k_specs'][:, 0], kern_colors[stage])
|
||||
|
||||
# Indicate saturation point(s):
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
ind, scale = crit_inds[stage], crit_scales[stage]
|
||||
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=colors[stage], 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], curve[ind],
|
||||
color=colors[stage], **plateau_line_kwargs)
|
||||
del data
|
||||
ind = crit_inds[stage]
|
||||
scale = crit_scales[stage]
|
||||
if compress_kernels or stage in ['log', 'inv']:
|
||||
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], curve[ind],
|
||||
color=color, **plateau_line_kwargs)
|
||||
|
||||
# Add legend to first analysis axis:
|
||||
legend = big_axes[0].legend(handles=leg_handles, **leg_kwargs)
|
||||
[handle.set_lw(lw['legend']) for handle in legend.get_lines()]
|
||||
|
||||
# Save graph:
|
||||
if save_path is not None:
|
||||
|
||||
Reference in New Issue
Block a user