Seriously, no idea. Wild amount of changes. Good luck.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import plotstyle_plt
|
||||
import glob
|
||||
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 get_saturation
|
||||
from color_functions import load_colors
|
||||
from plot_functions import hide_axis, ylimits, xlabel, ylabel, title_subplot,\
|
||||
plot_line, plot_barcode, strip_zeros, time_bar,\
|
||||
plot_line, strip_zeros, time_bar,\
|
||||
letter_subplot, letter_subplots
|
||||
from IPython import embed
|
||||
|
||||
@@ -17,11 +17,6 @@ def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
||||
plot_line(ax, time, snippets[:, ..., i], ymin=ymin, ymax=ymax, **kwargs)
|
||||
return None
|
||||
|
||||
def plot_bi_snippets(axes, time, snippets, **kwargs):
|
||||
for i, ax in enumerate(axes):
|
||||
plot_barcode(ax, time, snippets[:, ..., i], **kwargs)
|
||||
return None
|
||||
|
||||
def plot_curves(ax, scales, measures, fill_kwargs={}, **kwargs):
|
||||
if measures.ndim == 1:
|
||||
ax.plot(scales, measures, **kwargs)[0]
|
||||
@@ -39,8 +34,28 @@ def show_saturation(ax, scales, measures, high=0.95, **kwargs):
|
||||
marker='o', ms=10, zorder=6, clip_on=False, **kwargs)
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
target = 'Omocestus_rufipes'
|
||||
data_paths = glob.glob(f'../data/inv/full/{target}*.npz')
|
||||
target_species = [
|
||||
'Chorthippus_biguttulus',
|
||||
'Chorthippus_mollis',
|
||||
'Chrysochraon_dispar',
|
||||
'Euchorthippus_declivus',
|
||||
'Gomphocerippus_rufus',
|
||||
'Omocestus_rufipes',
|
||||
'Pseudochorthippus_parallelus',
|
||||
][5]
|
||||
example_file = {
|
||||
'Chorthippus_biguttulus': 'Chorthippus_biguttulus_GBC_94-17s73.1ms-19s977ms',
|
||||
'Chorthippus_mollis': 'Chorthippus_mollis_DJN_41_T28C-46s4.58ms-1m15s697ms',
|
||||
'Chrysochraon_dispar': 'Chrysochraon_dispar_DJN_26_T28C_DT-32s134ms-34s432ms',
|
||||
'Euchorthippus_declivus': 'Euchorthippus_declivus_FTN_79-2s167ms-2s563ms',
|
||||
'Gomphocerippus_rufus': 'Gomphocerippus_rufus_FTN_91-3-884ms-10s427ms',
|
||||
'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']
|
||||
@@ -105,9 +120,9 @@ lw = dict(
|
||||
log=0.25,
|
||||
inv=0.25,
|
||||
conv=0.25,
|
||||
bi=0,
|
||||
feat=1,
|
||||
big=3
|
||||
big=3,
|
||||
plateau=1.5,
|
||||
)
|
||||
xlabels = dict(
|
||||
big='scale $\\alpha$',
|
||||
@@ -118,7 +133,6 @@ ylabels = dict(
|
||||
log='$x_{\\text{db}}$',
|
||||
inv='$x_{\\text{inv}}$',
|
||||
conv='$c_i$',
|
||||
bi='$b_i$',
|
||||
feat='$f_i$',
|
||||
big=['intensity', 'rel. intensity', 'norm. intensity']
|
||||
)
|
||||
@@ -187,121 +201,160 @@ bar_kwargs = dict(
|
||||
va='center',
|
||||
)
|
||||
)
|
||||
|
||||
# PREPARATION:
|
||||
ref_data = dict(np.load(ref_path))
|
||||
plateau_settings = dict(
|
||||
low=0.05,
|
||||
high=0.95,
|
||||
first=True,
|
||||
last=True,
|
||||
condense=None,
|
||||
)
|
||||
plateau_line_kwargs = dict(
|
||||
lw=lw['plateau'],
|
||||
ls='--',
|
||||
zorder=1,
|
||||
)
|
||||
plateau_dot_kwargs = dict(
|
||||
marker='o',
|
||||
markersize=8,
|
||||
markeredgewidth=1,
|
||||
clip_on=False,
|
||||
)
|
||||
|
||||
# EXECUTION:
|
||||
for data_path in data_paths:
|
||||
print(f'Processing {data_path}')
|
||||
|
||||
# Load invariance data:
|
||||
data, config = load_data(data_path, **load_kwargs)
|
||||
t_full = np.arange(data['snip_filt'].shape[0]) / config['rate']
|
||||
# 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']
|
||||
|
||||
# Adjust grid parameters:
|
||||
snip_grid_kwargs['ncols'] = data['example_scales'].size
|
||||
# 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']
|
||||
|
||||
# Prepare overall graph:
|
||||
fig = plt.figure(**fig_kwargs)
|
||||
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
||||
# Adjust grid parameters:
|
||||
snip_grid_kwargs['ncols'] = snip_scales.size
|
||||
|
||||
# Prepare stage-specific snippet axes:
|
||||
snip_subfig = fig.add_subfigure(super_grid[subfig_specs['snip']])
|
||||
snip_grid = snip_subfig.add_gridspec(**snip_grid_kwargs)
|
||||
snip_axes = np.zeros((snip_grid.nrows, snip_grid.ncols), dtype=object)
|
||||
for i, j in product(range(snip_grid.nrows), range(snip_grid.ncols)):
|
||||
ax = snip_subfig.add_subplot(snip_grid[i, j])
|
||||
ax.set_xlim(t_full[0], t_full[-1])
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc[stages[i]]))
|
||||
hide_axis(ax, 'bottom')
|
||||
if i == 0:
|
||||
title = title_subplot(ax, f'$\\alpha={strip_zeros(data["example_scales"][j])}$',
|
||||
ref=snip_subfig, **title_kwargs)
|
||||
if j == 0:
|
||||
ylabel(ax, ylabels[stages[i]], **ylab_snip_kwargs, transform=snip_subfig.transSubfigure)
|
||||
else:
|
||||
hide_axis(ax, 'left')
|
||||
snip_axes[i, j] = ax
|
||||
time_bar(snip_axes[-1, -1], **bar_kwargs)
|
||||
letter_subplot(snip_subfig, 'a', ref=title, **letter_snip_kwargs)
|
||||
# Prepare overall graph:
|
||||
fig = plt.figure(**fig_kwargs)
|
||||
super_grid = fig.add_gridspec(**super_grid_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 in range(big_grid.ncols):
|
||||
ax = big_subfig.add_subplot(big_grid[0, i])
|
||||
ax.set_xlim(data['scales'][0], data['scales'][-1])
|
||||
ax.set_xscale('symlog', linthresh=data['scales'][1], linscale=0.5)
|
||||
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)
|
||||
big_axes[i] = ax
|
||||
letter_subplots(big_axes, 'bc', **letter_big_kwargs)
|
||||
# Prepare stage-specific snippet axes:
|
||||
snip_subfig = fig.add_subfigure(super_grid[subfig_specs['snip']])
|
||||
snip_grid = snip_subfig.add_gridspec(**snip_grid_kwargs)
|
||||
snip_axes = np.zeros((snip_grid.nrows, snip_grid.ncols), dtype=object)
|
||||
for i, j in product(range(snip_grid.nrows), range(snip_grid.ncols)):
|
||||
ax = snip_subfig.add_subplot(snip_grid[i, j])
|
||||
ax.set_xlim(t_full[0], t_full[-1])
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc[stages[i]]))
|
||||
hide_axis(ax, 'bottom')
|
||||
if i == 0:
|
||||
title = title_subplot(ax, f'$\\alpha={strip_zeros(snip_scales[j])}$',
|
||||
ref=snip_subfig, **title_kwargs)
|
||||
if j == 0:
|
||||
ylabel(ax, ylabels[stages[i]], **ylab_snip_kwargs, transform=snip_subfig.transSubfigure)
|
||||
else:
|
||||
hide_axis(ax, 'left')
|
||||
snip_axes[i, j] = ax
|
||||
time_bar(snip_axes[-1, -1], **bar_kwargs)
|
||||
letter_subplot(snip_subfig, 'a', ref=title, **letter_snip_kwargs)
|
||||
|
||||
# # Plot filtered snippets:
|
||||
# plot_snippets(snip_axes[0, :], t_full, data['snip_filt'],
|
||||
# c=colors['filt'], lw=lw['filt'])
|
||||
# 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 in range(big_grid.ncols):
|
||||
ax = big_subfig.add_subplot(big_grid[0, i])
|
||||
ax.set_xlim(scales[0], scales[-1])
|
||||
ax.set_xscale('symlog', linthresh=scales[1], linscale=0.5)
|
||||
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)
|
||||
big_axes[i] = ax
|
||||
letter_subplots(big_axes, 'bc', **letter_big_kwargs)
|
||||
|
||||
# # Plot envelope snippets:
|
||||
# plot_snippets(snip_axes[1, :], t_full, data['snip_env'],
|
||||
# ymin=0, c=colors['env'], lw=lw['env'])
|
||||
if False:
|
||||
# Plot filtered snippets:
|
||||
plot_snippets(snip_axes[0, :], t_full, snip['snip_filt'],
|
||||
c=colors['filt'], lw=lw['filt'])
|
||||
|
||||
# # Plot logarithmic snippets:
|
||||
# plot_snippets(snip_axes[2, :], t_full, data['snip_log'],
|
||||
# c=colors['log'], lw=lw['log'])
|
||||
# Plot envelope snippets:
|
||||
plot_snippets(snip_axes[1, :], t_full, snip['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw['env'])
|
||||
|
||||
# # Plot invariant snippets:
|
||||
# plot_snippets(snip_axes[3, :], t_full, data['snip_inv'],
|
||||
# c=colors['inv'], lw=lw['inv'])
|
||||
# Plot logarithmic snippets:
|
||||
plot_snippets(snip_axes[2, :], t_full, snip['snip_log'],
|
||||
c=colors['log'], lw=lw['log'])
|
||||
|
||||
# # Plot kernel response snippets:
|
||||
# plot_snippets(snip_axes[4, :], t_full, data['snip_conv'],
|
||||
# c=colors['conv'], lw=lw['conv'])
|
||||
# Plot invariant snippets:
|
||||
plot_snippets(snip_axes[3, :], t_full, snip['snip_inv'],
|
||||
c=colors['inv'], lw=lw['inv'])
|
||||
|
||||
# # Plot feature snippets:
|
||||
# plot_snippets(snip_axes[5, :], t_full, data['snip_feat'],
|
||||
# ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
# Plot kernel response snippets:
|
||||
plot_snippets(snip_axes[4, :], t_full, snip['snip_conv'],
|
||||
c=colors['conv'], lw=lw['conv'])
|
||||
|
||||
# Analysis results:
|
||||
scales_rel = data['scales'] - data['scales'][0]
|
||||
scales_rel /= scales_rel[-1]
|
||||
for stage in stages:
|
||||
measure = data[f'measure_{stage}']
|
||||
# Plot feature snippets:
|
||||
plot_snippets(snip_axes[5, :], t_full, snip['snip_feat'],
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
|
||||
# Plot unmodified intensity measures:
|
||||
curve = plot_curves(big_axes[0], data['scales'], measure, c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
show_saturation(big_axes[0], data['scales'], curve, c=colors[stage])
|
||||
# Plot analysis results:
|
||||
for stage in stages:
|
||||
# Get average unnormed measure across recordings:
|
||||
raw_measure = raw_data[f'mean_{stage}'].mean(axis=-1)
|
||||
|
||||
# # Relate to pure-noise reference:
|
||||
# norm_measure = measure / ref_data[stage]
|
||||
# Plot unmodified intensity measures:
|
||||
curve = plot_curves(big_axes[0], scales, raw_measure, 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]
|
||||
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)
|
||||
|
||||
# # Plot noise-related intensity measures:
|
||||
# big_axes[1].plot(data['scales'], norm_measure, c=colors[stage], lw=lw['big'])
|
||||
# Get average noise-related measure across recordings:
|
||||
norm_measure = norm_data[f'mean_{stage}'].mean(axis=-1)
|
||||
|
||||
# Normalize measure to [0, 1]:
|
||||
min_measure = measure.min(axis=0)
|
||||
max_measure = measure.max(axis=0)
|
||||
norm_measure = (measure - min_measure) / (max_measure - min_measure)
|
||||
# Plot noise-related intensity measure:
|
||||
curve = plot_curves(big_axes[1], scales, norm_measure, c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
|
||||
# Plot normalized intensity measures:
|
||||
curve = plot_curves(big_axes[1], data['scales'], norm_measure, c=colors[stage], lw=lw['big'],
|
||||
fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
show_saturation(big_axes[1], data['scales'], curve, c=colors[stage])
|
||||
# Indicate saturation point:
|
||||
if stage in ['log', 'inv', 'conv', 'feat']:
|
||||
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)
|
||||
|
||||
# # Plot over relative scales:
|
||||
# plot_curves(big_axes[2], scales_rel, norm_measure, c=colors[stage], lw=lw['big'],
|
||||
# fill_kwargs=dict(color=colors[stage], alpha=0.25))
|
||||
# scales_rel = curve - curve.min()
|
||||
# scales_rel /= scales_rel.max()
|
||||
# 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)
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
plt.show()
|
||||
# Plot range-normalized intensity measure:
|
||||
curve = plot_curves(big_axes[2], scales, norm_measure, 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']:
|
||||
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)
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
plt.show()
|
||||
|
||||
print('Done.')
|
||||
embed()
|
||||
|
||||
Reference in New Issue
Block a user