286 lines
7.2 KiB
Python
286 lines
7.2 KiB
Python
import plotstyle_plt
|
|
import glob
|
|
import numpy as np
|
|
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, title_subplot,\
|
|
plot_line, plot_barcode, strip_zeros, time_bar, super_xlabel
|
|
from IPython import embed
|
|
|
|
def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
|
|
ymin, ymax = ylimits(snippets, minval=ymin, maxval=ymax, pad=0.05)
|
|
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, 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/inv/full/{target}*.npz')
|
|
stages = ['raw', 'filt', 'env', 'log', 'inv', 'conv', 'bi', 'feat']
|
|
load_kwargs = dict(
|
|
files=stages,
|
|
keywords=['scales', 'snip', 'measure']
|
|
)
|
|
save_path = '../figures/fig_invariance_full.pdf'
|
|
|
|
# GRAPH SETTINGS:
|
|
fig_kwargs = dict(
|
|
figsize=(32/2.54, 16/2.54),
|
|
)
|
|
super_grid_kwargs = dict(
|
|
nrows=1,
|
|
ncols=3,
|
|
wspace=0,
|
|
hspace=0,
|
|
left=0,
|
|
right=1,
|
|
bottom=0,
|
|
top=1
|
|
)
|
|
subfig_specs = dict(
|
|
snip=(slice(None), slice(0, -1)),
|
|
big=(slice(None), -1),
|
|
)
|
|
snip_grid_kwargs = dict(
|
|
nrows=len(stages),
|
|
ncols=None,
|
|
wspace=0.1,
|
|
hspace=0.4,
|
|
left=0.15,
|
|
right=0.95,
|
|
bottom=0.08,
|
|
top=0.95
|
|
)
|
|
big_grid_kwargs = dict(
|
|
nrows=1,
|
|
ncols=1,
|
|
wspace=0,
|
|
hspace=0,
|
|
left=0.2,
|
|
right=0.96,
|
|
bottom=0.08,
|
|
top=0.95
|
|
)
|
|
|
|
# PLOT SETTINGS:
|
|
fs = dict(
|
|
lab_norm=16,
|
|
lab_tex=20,
|
|
letter=22,
|
|
tit_norm=16,
|
|
tit_tex=20,
|
|
bar=16,
|
|
)
|
|
colors = load_colors('../data/stage_colors.npz')
|
|
colors['raw'] = "#000000"
|
|
lw = dict(
|
|
raw=0.25,
|
|
filt=0.25,
|
|
env=0.25,
|
|
log=0.25,
|
|
inv=0.25,
|
|
conv=0.25,
|
|
bi=0,
|
|
feat=1,
|
|
big=3
|
|
)
|
|
xlabels = dict(
|
|
snip='time [s]',
|
|
big='scale $\\alpha$',
|
|
)
|
|
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=fs['lab_norm'],
|
|
ha='center',
|
|
va='bottom',
|
|
)
|
|
xlab_big_kwargs = dict(
|
|
y=0,
|
|
fontsize=fs['lab_norm'],
|
|
ha='center',
|
|
va='bottom',
|
|
)
|
|
ylab_snip_kwargs = dict(
|
|
x=0,
|
|
fontsize=fs['lab_tex'],
|
|
rotation=0,
|
|
ha='left',
|
|
va='center'
|
|
)
|
|
ylab_big_kwargs = dict(
|
|
x=0,
|
|
fontsize=fs['lab_norm'],
|
|
ha='center',
|
|
va='top',
|
|
)
|
|
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=fs['tit_norm'],
|
|
)
|
|
letter_snip_kwargs = dict(
|
|
x=0.02,
|
|
y=1,
|
|
ha='left',
|
|
va='top',
|
|
fontsize=fs['letter'],
|
|
fontweight='bold'
|
|
)
|
|
letter_big_kwargs = dict(
|
|
x=0,
|
|
y=1,
|
|
ha='left',
|
|
va='top',
|
|
fontsize=fs['letter'],
|
|
fontweight='bold'
|
|
)
|
|
bar_time = 5
|
|
bar_kwargs = dict(
|
|
dur=bar_time,
|
|
y0=-0.25,
|
|
y1=-0.1,
|
|
xshift=1,
|
|
color='k',
|
|
lw=0,
|
|
clip_on=False,
|
|
text_pos=(-0.1, 0.5),
|
|
text_str=f'${bar_time}\\,\\text{{s}}$',
|
|
text_kwargs=dict(
|
|
fontsize=fs['bar'],
|
|
ha='right',
|
|
va='center',
|
|
)
|
|
)
|
|
|
|
|
|
# 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_raw'].shape[0]) / config['rate']
|
|
|
|
# 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 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
|
|
time_bar(snip_axes[-1, -1], **bar_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 raw snippets:
|
|
plot_snippets(snip_axes[0, :], t_full, data['snip_raw'],
|
|
c=colors['raw'], lw=lw['raw'])
|
|
|
|
# Plot filtered snippets:
|
|
plot_snippets(snip_axes[1, :], t_full, data['snip_filt'],
|
|
c=colors['filt'], lw=lw['filt'])
|
|
|
|
# Plot envelope snippets:
|
|
plot_snippets(snip_axes[2, :], t_full, data['snip_env'],
|
|
ymin=0, c=colors['env'], lw=lw['env'])
|
|
|
|
# Plot logarithmic snippets:
|
|
plot_snippets(snip_axes[3, :], t_full, data['snip_log'],
|
|
ymax=0, c=colors['log'], lw=lw['log'])
|
|
|
|
# Plot invariant snippets:
|
|
plot_snippets(snip_axes[4, :], t_full, data['snip_inv'],
|
|
c=colors['inv'], lw=lw['inv'])
|
|
|
|
# Plot kernel response snippets:
|
|
plot_snippets(snip_axes[5, :], t_full, data['snip_conv'],
|
|
c=colors['conv'], lw=lw['conv'])
|
|
|
|
# Plot binary snippets:
|
|
plot_bi_snippets(snip_axes[6, :], t_full, data['snip_bi'],
|
|
color=colors['bi'], lw=lw['bi'])
|
|
|
|
# Plot feature snippets:
|
|
plot_snippets(snip_axes[7, :], t_full, data['snip_feat'],
|
|
ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
|
|
|
|
# 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)
|
|
plt.show()
|
|
|
|
print('Done.')
|
|
embed()
|