Holiday syncing :)

This commit is contained in:
j-hartling
2026-04-02 16:00:56 +02:00
parent 298969a067
commit 0b9264b1e1
14 changed files with 627 additions and 667 deletions

View File

@@ -4,9 +4,11 @@ import numpy as np
import matplotlib.pyplot as plt
from itertools import product
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, super_xlabel
plot_line, plot_barcode, strip_zeros, time_bar,\
letter_subplot, letter_subplots
from IPython import embed
def plot_snippets(axes, time, snippets, ymin=None, ymax=None, **kwargs):
@@ -20,52 +22,70 @@ def plot_bi_snippets(axes, time, snippets, **kwargs):
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]
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 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)
# GENERAL SETTINGS:
target = 'Omocestus_rufipes'
data_paths = glob.glob(f'../data/inv/full/{target}*.npz')
stages = ['raw', 'filt', 'env', 'log', 'inv', 'conv', 'bi', 'feat']
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']
)
save_path = '../figures/fig_invariance_full.pdf'
# GRAPH SETTINGS:
fig_kwargs = dict(
figsize=(32/2.54, 16/2.54),
figsize=(32/2.54, 20/2.54),
)
super_grid_kwargs = dict(
nrows=1,
ncols=3,
nrows=2,
ncols=1,
wspace=0,
hspace=0,
left=0,
right=1,
bottom=0,
top=1
top=1,
height_ratios=[3, 2]
)
subfig_specs = dict(
snip=(slice(None), slice(0, -1)),
big=(slice(None), -1),
snip=(0, 0),
big=(1, 0),
)
snip_grid_kwargs = dict(
nrows=len(stages),
ncols=None,
wspace=0.1,
hspace=0.4,
left=0.15,
left=0.08,
right=0.95,
bottom=0.08,
top=0.95
)
big_grid_kwargs = dict(
nrows=1,
ncols=1,
wspace=0,
ncols=3,
wspace=0.2,
hspace=0,
left=0.2,
left=snip_grid_kwargs['left'],
right=0.96,
bottom=0.08,
bottom=0.2,
top=0.95
)
@@ -79,9 +99,7 @@ fs = dict(
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,
@@ -92,25 +110,17 @@ lw = dict(
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}}$',
log='$x_{\\text{db}}$',
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',
big=['intensity', 'rel. intensity', 'norm. intensity']
)
xlab_big_kwargs = dict(
y=0,
@@ -126,18 +136,17 @@ ylab_snip_kwargs = dict(
va='center'
)
ylab_big_kwargs = dict(
x=0,
x=-0.12,
fontsize=fs['lab_norm'],
ha='center',
va='top',
va='bottom',
)
yloc = dict(
raw=500,
filt=500,
env=250,
log=25,
inv=10,
conv=1,
filt=3000,
env=1000,
log=50,
inv=20,
conv=2,
feat=1,
)
title_kwargs = dict(
@@ -148,20 +157,18 @@ title_kwargs = dict(
fontsize=fs['tit_norm'],
)
letter_snip_kwargs = dict(
x=0.02,
y=1,
x=0,
yref=0.5,
ha='left',
va='top',
va='center',
fontsize=fs['letter'],
fontweight='bold'
)
letter_big_kwargs = dict(
x=0,
y=1,
ha='left',
va='top',
va='bottom',
fontsize=fs['letter'],
fontweight='bold'
)
bar_time = 5
bar_kwargs = dict(
@@ -181,6 +188,8 @@ bar_kwargs = dict(
)
)
# PREPARATION:
ref_data = dict(np.load(ref_path))
# EXECUTION:
for data_path in data_paths:
@@ -188,7 +197,7 @@ for data_path in data_paths:
# Load invariance data:
data, config = load_data(data_path, **load_kwargs)
t_full = np.arange(data['snip_raw'].shape[0]) / config['rate']
t_full = np.arange(data['snip_filt'].shape[0]) / config['rate']
# Adjust grid parameters:
snip_grid_kwargs['ncols'] = data['example_scales'].size
@@ -204,78 +213,91 @@ for data_path in data_paths:
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 = f'$\\alpha={strip_zeros(data["example_scales"][j])}$'
title_subplot(ax, title, ref=snip_subfig, **title_kwargs)
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')
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)
letter_subplot(snip_subfig, 'a', ref=title, **letter_snip_kwargs)
# Prepare single analysis axis:
# Prepare analysis axes:
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)
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)
# 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[0, :], t_full, data['snip_filt'],
# c=colors['filt'], lw=lw['filt'])
# 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[1, :], t_full, data['snip_env'],
# ymin=0, c=colors['env'], lw=lw['env'])
# 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[2, :], t_full, data['snip_log'],
# c=colors['log'], lw=lw['log'])
# 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[3, :], t_full, data['snip_inv'],
# c=colors['inv'], lw=lw['inv'])
# 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[4, :], t_full, data['snip_conv'],
# c=colors['conv'], lw=lw['conv'])
# 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'])
# # Plot feature snippets:
# plot_snippets(snip_axes[5, :], t_full, data['snip_feat'],
# ymin=0, ymax=1, c=colors['feat'], lw=lw['feat'])
# Analysis results:
scales_rel = data['scales'] - data['scales'][0]
scales_rel /= scales_rel[-1]
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)
measure = data[f'measure_{stage}']
# Condense measure:
if stage in ['conv', 'feat']:
data[key] = np.nanmedian(data[key], axis=1)
# 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 measure over scales:
big_ax.plot(data['scales'], data[key],
c=colors[stage], lw=lw['big'])
# # Relate to pure-noise reference:
# norm_measure = measure / ref_data[stage]
# # Plot noise-related intensity measures:
# big_axes[1].plot(data['scales'], norm_measure, c=colors[stage], lw=lw['big'])
# 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 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])
# # 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()
if save_path is not None:
fig.savefig(save_path)