Again, numerous changes.
Mostly figure polishing and fixing. Crucial fix to "short" invariance analysis.
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 thunderhopper.filtertools import find_kern_specs
|
||||
from misc_functions import get_saturation
|
||||
from color_functions import load_colors
|
||||
from plot_functions import hide_axis, ylimits, xlabel, ylabel, title_subplot,\
|
||||
plot_line, strip_zeros, time_bar,\
|
||||
plot_line, strip_zeros, time_bar, set_clip_box,\
|
||||
letter_subplot, letter_subplots
|
||||
from IPython import embed
|
||||
|
||||
@@ -28,10 +29,19 @@ def plot_curves(ax, scales, measures, fill_kwargs={}, **kwargs):
|
||||
ax.fill_between(scales, *spread_measure, **fill_kwargs)
|
||||
return median_measure
|
||||
|
||||
def show_saturation(ax, scales, measures, high=0.95, **kwargs):
|
||||
high_ind = get_saturation(measures, high=high)[1]
|
||||
return ax.plot(scales[high_ind], 0, transform=ax.get_xaxis_transform(),
|
||||
marker='o', ms=10, zorder=6, clip_on=False, **kwargs)
|
||||
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
|
||||
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
target_species = [
|
||||
@@ -52,21 +62,34 @@ example_file = {
|
||||
'Omocestus_rufipes': 'Omocestus_rufipes_DJN_32-40s724ms-48s779ms',
|
||||
'Pseudochorthippus_parallelus': 'Pseudochorthippus_parallelus_GBC_88-6s678ms-9s32.3ms'
|
||||
}[target_species]
|
||||
raw_path = search_files(target_species, incl='raw', dir='../data/inv/full/condensed/')[0]
|
||||
norm_path = search_files(target_species, incl='norm', dir='../data/inv/full/condensed/')[0]
|
||||
snip_path = search_files(example_file, dir='../data/inv/full/')[0]
|
||||
trace_path = search_files(target_species, dir='../data/inv/full/collected/')[0]
|
||||
ref_path = '../data/inv/full/ref_measures.npz'
|
||||
save_path = '../figures/fig_invariance_full.pdf'
|
||||
stages = ['filt', 'env', 'log', 'inv', 'conv', 'feat']
|
||||
load_kwargs = dict(
|
||||
files=stages,
|
||||
keywords=['scales', 'snip', 'measure']
|
||||
)
|
||||
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]
|
||||
save_path = '../figures/fig_invariance_full.pdf'
|
||||
|
||||
# ANALYSIS SETTINGS:
|
||||
exclude_zero = True
|
||||
|
||||
# SUBSET SETTINGS:
|
||||
types = np.array([1, -1, 2, -2, 3, -3, 4, -4])
|
||||
sigmas = np.array([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 = [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
|
||||
|
||||
# GRAPH SETTINGS:
|
||||
fig_kwargs = dict(
|
||||
figsize=(32/2.54, 20/2.54),
|
||||
figsize=(32/2.54, 32/2.54),
|
||||
)
|
||||
super_grid_kwargs = dict(
|
||||
nrows=2,
|
||||
@@ -222,16 +245,25 @@ plateau_dot_kwargs = dict(
|
||||
|
||||
# EXECUTION:
|
||||
|
||||
# Load invariance data:
|
||||
raw_data, config = load_data(raw_path, files='scales', keywords='mean')
|
||||
norm_data, _ = load_data(norm_path, files='scales', keywords='mean')
|
||||
scales = raw_data['scales']
|
||||
# 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 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]):
|
||||
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')
|
||||
reduce_kernels = True
|
||||
|
||||
# Adjust grid parameters:
|
||||
snip_grid_kwargs['ncols'] = snip_scales.size
|
||||
|
||||
@@ -270,43 +302,48 @@ for i in range(big_grid.ncols):
|
||||
ax.set_yscale('symlog', linthresh=0.01, linscale=0.1)
|
||||
xlabel(ax, xlabels['big'], transform=big_subfig, **xlab_big_kwargs)
|
||||
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)
|
||||
big_axes[i] = ax
|
||||
letter_subplots(big_axes, 'bc', **letter_big_kwargs)
|
||||
letter_subplots(big_axes, 'bcd', **letter_big_kwargs)
|
||||
|
||||
if False:
|
||||
if True:
|
||||
# Plot filtered snippets:
|
||||
plot_snippets(snip_axes[0, :], t_full, snip['snip_filt'],
|
||||
c=colors['filt'], lw=lw['filt'])
|
||||
c=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'])
|
||||
ymin=0, c=colors['env'], lw=lw['env'])
|
||||
|
||||
# Plot logarithmic snippets:
|
||||
plot_snippets(snip_axes[2, :], t_full, snip['snip_log'],
|
||||
c=colors['log'], lw=lw['log'])
|
||||
c=colors['log'], lw=lw['log'])
|
||||
|
||||
# Plot invariant snippets:
|
||||
plot_snippets(snip_axes[3, :], t_full, snip['snip_inv'],
|
||||
c=colors['inv'], lw=lw['inv'])
|
||||
c=colors['inv'], lw=lw['inv'])
|
||||
|
||||
# Plot kernel response snippets:
|
||||
plot_snippets(snip_axes[4, :], t_full, snip['snip_conv'],
|
||||
c=colors['conv'], lw=lw['conv'])
|
||||
c=colors['conv'], lw=lw['conv'])
|
||||
|
||||
# Plot feature snippets:
|
||||
plot_snippets(snip_axes[5, :], t_full, snip['snip_feat'],
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
del snip
|
||||
|
||||
# Plot analysis results:
|
||||
# Remember saturation points:
|
||||
crit_inds, crit_scales = {}, {}
|
||||
|
||||
# Unnormed measures:
|
||||
for stage in stages:
|
||||
# Get average unnormed measure across recordings:
|
||||
raw_measure = raw_data[f'mean_{stage}'].mean(axis=-1)
|
||||
|
||||
# Plot unmodified intensity measures:
|
||||
curve = plot_curves(big_axes[0], scales, raw_measure, c=colors[stage], lw=lw['big'],
|
||||
# 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:
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
ind = get_saturation(curve, **plateau_settings)[1]
|
||||
@@ -317,43 +354,60 @@ for stage in stages:
|
||||
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
|
||||
|
||||
# Get average noise-related measure across recordings:
|
||||
norm_measure = norm_data[f'mean_{stage}'].mean(axis=-1)
|
||||
|
||||
# Plot noise-related intensity measure:
|
||||
curve = plot_curves(big_axes[1], scales, norm_measure, c=colors[stage], lw=lw['big'],
|
||||
# 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:
|
||||
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
|
||||
|
||||
# Normalize measure to [0, 1]:
|
||||
min_measure = raw_measure.min(axis=0)
|
||||
max_measure = raw_measure.max(axis=0)
|
||||
norm_measure = (raw_measure - min_measure) / (max_measure - min_measure)
|
||||
|
||||
# Plot range-normalized intensity measure:
|
||||
curve = plot_curves(big_axes[2], scales, norm_measure, c=colors[stage], lw=lw['big'],
|
||||
# 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 stages:
|
||||
# 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))
|
||||
|
||||
# Indicate saturation point:
|
||||
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
|
||||
|
||||
# Save graph:
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
file_name = save_path.replace('.pdf', f'_{target_species}.pdf')
|
||||
fig.savefig(file_name)
|
||||
plt.show()
|
||||
|
||||
print('Done.')
|
||||
|
||||
Reference in New Issue
Block a user