Lots of stuff. Syncing to home.
This commit is contained in:
@@ -5,46 +5,37 @@ import matplotlib.pyplot as plt
|
||||
from itertools import product
|
||||
from thunderhopper.modeltools import load_data
|
||||
from color_functions import load_colors
|
||||
from plot_functions import hide_axis, ylimits, xlabel, ylabel, plot_line, plot_barcode, strip_zeros
|
||||
from plot_functions import hide_axis, ylimits, xlabel, ylabel, title_subplot,\
|
||||
plot_line, plot_barcode, strip_zeros, time_bar, super_xlabel
|
||||
from IPython import embed
|
||||
|
||||
def add_snip_axes(fig, grid_kwargs):
|
||||
grid = fig.add_gridspec(**grid_kwargs)
|
||||
axes = np.zeros((grid.nrows, grid.ncols), dtype=object)
|
||||
for i, j in product(range(grid.nrows), range(grid.ncols)):
|
||||
axes[i, j] = fig.add_subplot(grid[i, j])
|
||||
[hide_axis(ax, 'left') for ax in axes.flatten()]
|
||||
[hide_axis(ax, 'bottom') for ax in axes.flatten()]
|
||||
return axes
|
||||
|
||||
def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
||||
ymin, ymax = ylimits(snippets, minval=ymin, maxval=ymax, pad=0.05)
|
||||
for ax, snippet in zip(axes, snippets.T):
|
||||
plot_line(ax, time, snippet, ymin=ymin, ymax=ymax, **kwargs)
|
||||
for i, ax in enumerate(axes):
|
||||
plot_line(ax, time, snippets[:, ..., i], ymin=ymin, ymax=ymax, **kwargs)
|
||||
return None
|
||||
|
||||
def plot_bi_snippets(axes, time, binary, **kwargs):
|
||||
for ax, binary in zip(axes, binary.T):
|
||||
plot_barcode(ax, time, binary[:, None], **kwargs)
|
||||
def plot_bi_snippets(axes, time, snippets, **kwargs):
|
||||
for i, ax in enumerate(axes):
|
||||
plot_barcode(ax, time, snippets[:, ..., i], **kwargs)
|
||||
return None
|
||||
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
target = 'Omocestus_rufipes'
|
||||
data_paths = glob.glob(f'../data/processed/{target}*.npz')
|
||||
stages = ['filt', 'env', 'log', 'inv', 'conv', 'bi', 'feat']
|
||||
data_paths = glob.glob(f'../data/inv/full/{target}*.npz')
|
||||
stages = ['raw', 'filt', 'env', 'log', 'inv', 'conv', 'bi', 'feat']
|
||||
load_kwargs = dict(
|
||||
files=stages,
|
||||
keywords=['scales', 'measure', 'spread']
|
||||
keywords=['scales', 'snip', 'measure']
|
||||
)
|
||||
save_path = None#'../figures/fig_invariance_full.pdf'
|
||||
save_path = '../figures/fig_invariance_full.pdf'
|
||||
|
||||
# GRAPH SETTINGS:
|
||||
fig_kwargs = dict(
|
||||
figsize=(32/2.54, 16/2.54),
|
||||
)
|
||||
super_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
nrows=1,
|
||||
ncols=3,
|
||||
wspace=0,
|
||||
hspace=0,
|
||||
@@ -54,104 +45,124 @@ super_grid_kwargs = dict(
|
||||
top=1
|
||||
)
|
||||
subfig_specs = dict(
|
||||
**{stage: (slice(0, -1), i) for i, stage in enumerate(stages)},
|
||||
big=(slice(None), -1)
|
||||
snip=(slice(None), slice(0, -1)),
|
||||
big=(slice(None), -1),
|
||||
)
|
||||
stage_grid_kwargs = dict(
|
||||
nrows=1,
|
||||
snip_grid_kwargs = dict(
|
||||
nrows=len(stages),
|
||||
ncols=None,
|
||||
wspace=0.05,
|
||||
hspace=0,
|
||||
left=0.07,
|
||||
wspace=0.1,
|
||||
hspace=0.4,
|
||||
left=0.15,
|
||||
right=0.95,
|
||||
bottom=0.15,
|
||||
top=0.9
|
||||
bottom=0.08,
|
||||
top=0.95
|
||||
)
|
||||
big_grid_kwargs = dict(
|
||||
nrows=1,
|
||||
ncols=1,
|
||||
wspace=0,
|
||||
hspace=0,
|
||||
left=0.15,
|
||||
left=0.2,
|
||||
right=0.96,
|
||||
bottom=0.1,
|
||||
bottom=0.08,
|
||||
top=0.95
|
||||
)
|
||||
|
||||
# PLOT SETTINGS:
|
||||
colors = load_colors('../data/stage_colors.npz')
|
||||
lw_snippets = dict(
|
||||
colors['raw'] = "#000000"
|
||||
lw = dict(
|
||||
raw=0.25,
|
||||
filt=0.25,
|
||||
env=0.5,
|
||||
log=0.5,
|
||||
inv=0.5,
|
||||
conv=0.5,
|
||||
bi=0.01,
|
||||
feat=2
|
||||
env=0.25,
|
||||
log=0.25,
|
||||
inv=0.25,
|
||||
conv=0.25,
|
||||
bi=0,
|
||||
feat=1,
|
||||
big=3
|
||||
)
|
||||
lw_big = 3
|
||||
xlabels = dict(
|
||||
analysis='scale $\\alpha$',
|
||||
snip='time [s]',
|
||||
big='scale $\\alpha$',
|
||||
)
|
||||
xlab_analysis_kwargs = dict(
|
||||
y=0.01,
|
||||
ylabels = dict(
|
||||
raw='$x$',
|
||||
filt='$x_{\\text{filt}}$',
|
||||
env='$x_{\\text{env}}$',
|
||||
log='$x_{\\text{log}}$',
|
||||
inv='$x_{\\text{inv}}$',
|
||||
conv='$c_i$',
|
||||
bi='$b_i$',
|
||||
feat='$f_i$',
|
||||
big='norm. intensity measure'
|
||||
)
|
||||
xlab_snip_kwargs = dict(
|
||||
y=0,
|
||||
fontsize=16,
|
||||
ha='center',
|
||||
va='bottom',
|
||||
)
|
||||
ylabels = dict(
|
||||
conv='$c_i$',
|
||||
bi='$b_i$',
|
||||
feat='$f_i$',
|
||||
analysis='ratio $\\text{SD}_{\\alpha}\\,/\\,\\text{SD}_{\\min[\\alpha]}$',
|
||||
# analysis='ratio $\\sigma_{\\alpha}\\,/\\,\\sigma_{\\min[\\alpha]}$',
|
||||
xlab_big_kwargs = dict(
|
||||
y=0,
|
||||
fontsize=16,
|
||||
ha='center',
|
||||
va='bottom',
|
||||
)
|
||||
ylab_snip_kwargs = dict(
|
||||
x=0.01,
|
||||
x=0,
|
||||
fontsize=20,
|
||||
rotation=0,
|
||||
ha='left',
|
||||
va='center',
|
||||
va='center'
|
||||
)
|
||||
ylab_analysis_kwargs = dict(
|
||||
x=0.02,
|
||||
ylab_big_kwargs = dict(
|
||||
x=0,
|
||||
fontsize=16,
|
||||
ha='center',
|
||||
va='top',
|
||||
)
|
||||
# xloc = dict(
|
||||
# analysis=10,
|
||||
# )
|
||||
# letter_snip_kwargs = dict(
|
||||
# x=0.02,
|
||||
# y=1,
|
||||
# ha='left',
|
||||
# va='top',
|
||||
# fontsize=22,
|
||||
# fontweight='bold'
|
||||
# )
|
||||
# letter_analysis_kwargs = dict(
|
||||
# x=0,
|
||||
# y=1,
|
||||
# ha='left',
|
||||
# va='top',
|
||||
# fontsize=22,
|
||||
# fontweight='bold'
|
||||
# )
|
||||
# bar_time = 5
|
||||
# bar_kwargs = dict(
|
||||
# y0=0.7,
|
||||
# y1=0.8,
|
||||
# color='k',
|
||||
# lw=0,
|
||||
# )
|
||||
# spread_kwargs = dict(
|
||||
# alpha=0.3,
|
||||
# lw=0,
|
||||
# zorder=0
|
||||
# )
|
||||
# kernel_ind = 0
|
||||
yloc = dict(
|
||||
raw=500,
|
||||
filt=500,
|
||||
env=250,
|
||||
log=25,
|
||||
inv=10,
|
||||
conv=1,
|
||||
feat=1,
|
||||
)
|
||||
title_kwargs = dict(
|
||||
x=0.5,
|
||||
yref=1,
|
||||
ha='center',
|
||||
va='top',
|
||||
fontsize=16,
|
||||
)
|
||||
letter_snip_kwargs = dict(
|
||||
x=0.02,
|
||||
y=1,
|
||||
ha='left',
|
||||
va='top',
|
||||
fontsize=22,
|
||||
fontweight='bold'
|
||||
)
|
||||
letter_big_kwargs = dict(
|
||||
x=0,
|
||||
y=1,
|
||||
ha='left',
|
||||
va='top',
|
||||
fontsize=22,
|
||||
fontweight='bold'
|
||||
)
|
||||
bar_time = 5
|
||||
bar_kwargs = dict(
|
||||
y0=0.8,
|
||||
y1=0.9,
|
||||
color='k',
|
||||
lw=0,
|
||||
)
|
||||
|
||||
|
||||
# EXECUTION:
|
||||
for data_path in data_paths:
|
||||
@@ -159,82 +170,95 @@ for data_path in data_paths:
|
||||
|
||||
# Load invariance data:
|
||||
data, config = load_data(data_path, **load_kwargs)
|
||||
t_full = np.arange(data['conv'].shape[0]) / config['env_rate']
|
||||
t_full = np.arange(data['snip_raw'].shape[0]) / config['rate']
|
||||
|
||||
# Reduce snippet data to kernel subset:
|
||||
data['conv'] = data['conv'][:, kernel_ind]
|
||||
data['bi'] = data['bi'][:, kernel_ind]
|
||||
data['feat'] = data['feat'][:, kernel_ind]
|
||||
# Adjust grid parameters:
|
||||
snip_grid_kwargs['ncols'] = data['example_scales'].size
|
||||
|
||||
# Prepare overall graph:
|
||||
fig = plt.figure(**fig_kwargs)
|
||||
super_grid = fig.add_gridspec(**super_grid_kwargs)
|
||||
|
||||
# Prepare pure-song snippet axes:
|
||||
pure_subfig = fig.add_subfigure(super_grid[subfig_specs['pure']])
|
||||
pure_grid_kwargs['nrows' if pure_grid_kwargs['nrows'] is None else 'ncols'] = data['example_scales'].size
|
||||
pure_axes = add_snip_axes(pure_subfig, pure_grid_kwargs)
|
||||
for ax, stage in zip(pure_axes[:, 0], stages):
|
||||
ylabel(ax, ylabels[stage], **ylab_snip_kwargs,
|
||||
transform=pure_subfig.transSubfigure)
|
||||
for ax, scale in zip(pure_axes[snip_specs['conv']], data['example_scales']):
|
||||
ax.set_title(f'$\\alpha={strip_zeros(scale)}$')
|
||||
pure_subfig.text(s='a', **letter_snip_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])
|
||||
hide_axis(ax, 'bottom')
|
||||
if i == 0:
|
||||
title = f'$\\alpha={strip_zeros(data["example_scales"][j])}$'
|
||||
title_subplot(ax, title, 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')
|
||||
if stages[i] != 'bi':
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc[stages[i]]))
|
||||
snip_axes[i, j] = ax
|
||||
super_xlabel(xlabels['snip'], snip_subfig, snip_axes[-1, 0], snip_axes[-1, -1], **xlab_snip_kwargs)
|
||||
time_bar(snip_axes[0, 0], bar_time, **bar_kwargs)
|
||||
|
||||
# Prepare analysis axis:
|
||||
analysis_subfig = fig.add_subfigure(super_grid[subfig_specs['analysis']])
|
||||
analysis_grid = analysis_subfig.add_gridspec(**analysis_grid_kwargs)
|
||||
analysis_ax = analysis_subfig.add_subplot(analysis_grid[0, 0])
|
||||
analysis_ax.set_xlim(data['scales'].min(), data['scales'].max())
|
||||
analysis_ax.xaxis.set_major_locator(plt.MultipleLocator(xloc['analysis']))
|
||||
xlabel(analysis_ax, xlabels['analysis'], **xlab_analysis_kwargs,
|
||||
transform=analysis_subfig.transSubfigure)
|
||||
# analysis_ax.set_yscale('log')
|
||||
ylabel(analysis_ax, ylabels['analysis'], **ylab_analysis_kwargs,
|
||||
transform=analysis_subfig.transSubfigure)
|
||||
analysis_subfig.text(s='c', **letter_analysis_kwargs)
|
||||
# Prepare single analysis axis:
|
||||
big_subfig = fig.add_subfigure(super_grid[subfig_specs['big']])
|
||||
big_grid = big_subfig.add_gridspec(**big_grid_kwargs)
|
||||
big_ax = big_subfig.add_subplot(big_grid[0, 0])
|
||||
big_ax.set_xlim(data['scales'].min(), data['scales'].max())
|
||||
big_ax.set_xscale('symlog', linthresh=data['scales'][1], linscale=0.5)
|
||||
big_ax.set_yscale('symlog', linthresh=0.01, linscale=0.1)
|
||||
xlabel(big_ax, xlabels['big'], **xlab_big_kwargs, transform=big_subfig.transSubfigure)
|
||||
ylabel(big_ax, ylabels['big'], **ylab_big_kwargs, transform=big_subfig.transSubfigure)
|
||||
|
||||
# Plot pure-song kernel response snippets:
|
||||
plot_snippets(pure_axes[snip_specs['conv']], t_full, data['conv'],
|
||||
c=colors['conv'], lw=lw_snippets['conv'])
|
||||
# Plot raw snippets:
|
||||
plot_snippets(snip_axes[0, :], t_full, data['snip_raw'],
|
||||
c=colors['raw'], lw=lw['raw'])
|
||||
|
||||
# Plot pure-song binary snippets:
|
||||
plot_bi_snippets(pure_axes[snip_specs['bi']], t_full, data['bi'],
|
||||
color=colors['bi'], lw=0)
|
||||
# Plot filtered snippets:
|
||||
plot_snippets(snip_axes[1, :], t_full, data['snip_filt'],
|
||||
c=colors['filt'], lw=lw['filt'])
|
||||
|
||||
# Plot pure-song feature snippets:
|
||||
plot_snippets(pure_axes[snip_specs['feat']], t_full, data['feat'],
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw_snippets['feat'])
|
||||
# Plot envelope snippets:
|
||||
plot_snippets(snip_axes[2, :], t_full, data['snip_env'],
|
||||
ymin=0, c=colors['env'], lw=lw['env'])
|
||||
|
||||
# Indicate time scale:
|
||||
time_bar(pure_axes[snip_specs['conv']][0], bar_time, **bar_kwargs)
|
||||
# Plot logarithmic snippets:
|
||||
plot_snippets(snip_axes[3, :], t_full, data['snip_log'],
|
||||
ymax=0, c=colors['log'], lw=lw['log'])
|
||||
|
||||
# # Plot noise-song kernel response snippets:
|
||||
# plot_snippets(noise_axes[snip_specs['conv']], t_full, noise_data['conv'],
|
||||
# c=colors['conv'], lw=lw_snippets['conv'])
|
||||
# Plot invariant snippets:
|
||||
plot_snippets(snip_axes[4, :], t_full, data['snip_inv'],
|
||||
c=colors['inv'], lw=lw['inv'])
|
||||
|
||||
# # Plot noise-song binary snippets:
|
||||
# plot_bi_snippets(noise_axes[snip_specs['bi']], t_full, noise_data['bi'],
|
||||
# color=colors['bi'], lw=0)
|
||||
# Plot kernel response snippets:
|
||||
plot_snippets(snip_axes[5, :], t_full, data['snip_conv'],
|
||||
c=colors['conv'], lw=lw['conv'])
|
||||
|
||||
# # Plot noise-song feature snippets:
|
||||
# plot_snippets(noise_axes[snip_specs['feat']], t_full, noise_data['feat'],
|
||||
# ymin=0, ymax=1, c=colors['feat'], lw=lw_snippets['feat'])
|
||||
# Plot binary snippets:
|
||||
plot_bi_snippets(snip_axes[6, :], t_full, data['snip_bi'],
|
||||
color=colors['bi'], lw=lw['bi'])
|
||||
|
||||
# # Indicate time scale:
|
||||
# time_bar(noise_axes[snip_specs['conv']][0], bar_time, **bar_kwargs)
|
||||
# Plot feature snippets:
|
||||
plot_snippets(snip_axes[7, :], t_full, data['snip_feat'],
|
||||
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
||||
|
||||
# Plot noise-song SD ratios (limited):
|
||||
analysis_ax.plot(data['scales'], data['measure_conv'],
|
||||
c=colors['conv'], lw=lw_analysis)
|
||||
lower, upper = data['spread_conv']
|
||||
analysis_ax.fill_between(data['scales'], lower, upper,
|
||||
color=colors['conv'], **spread_kwargs)
|
||||
analysis_ax.plot(data['scales'], data['measure_feat'],
|
||||
c=colors['feat'], lw=lw_analysis)
|
||||
lower, upper = data['spread_feat']
|
||||
analysis_ax.fill_between(data['scales'], lower, upper,
|
||||
color=colors['feat'], **spread_kwargs)
|
||||
# Analysis results:
|
||||
for stage in stages:
|
||||
key = f'measure_{stage}'
|
||||
if stage == 'bi':
|
||||
continue
|
||||
# Min-max normalization:
|
||||
base_ind = np.argmin(data['scales'])
|
||||
data[key] -= data[key][base_ind, ...]
|
||||
data[key] /= data[key].max(axis=0)
|
||||
|
||||
# Condense measure:
|
||||
if stage in ['conv', 'feat']:
|
||||
data[key] = np.nanmedian(data[key], axis=1)
|
||||
|
||||
# Plot measure over scales:
|
||||
big_ax.plot(data['scales'], data[key],
|
||||
c=colors[stage], lw=lw['big'])
|
||||
|
||||
if save_path is not None:
|
||||
fig.savefig(save_path)
|
||||
|
||||
Reference in New Issue
Block a user