Added some cmap functions.

Selected species-specific  colors.
Quite some progress on fig_invariance_thresh_lp_species.pdf.
This commit is contained in:
j-hartling
2026-03-26 17:26:30 +01:00
parent 1a29b95782
commit 92ee4eda6f
11 changed files with 737 additions and 132 deletions

View File

@@ -6,8 +6,8 @@ from itertools import product
from thunderhopper.filetools import search_files
from thunderhopper.modeltools import load_data
from thunderhopper.filtertools import find_kern_specs
from color_functions import load_colors, shade_colors
from plot_functions import hide_axis, ylimits, xlabel, ylabel, super_ylabel,\
from color_functions import load_colors, shade_colors, create_listed_cmap
from plot_functions import hide_axis, title_subplot, ylimits, xlabel, ylabel, super_ylabel,\
plot_line, plot_barcode, strip_zeros, time_bar,\
letter_subplot, letter_subplots, hide_ticks,\
super_xlabel, super_ylabel, assign_colors
@@ -125,73 +125,90 @@ def split_subplot(ax, side='right', size=10, pad=10):
inputs = zip(*force_sequence(side, size, pad, equal_size=True))
return [div.append_axes(s, f'{n}%', f'{p}%') for s, n, p in inputs]
def shorten_species(name):
genus, species = name.split('_')
return genus[0] + '. ' + species
# GENERAL SETTINGS:
targets = [
target_species = [
'Omocestus_rufipes',
'Chorthippus_biguttulus',
# 'Chorthippus_mollis',
# 'Chrysochraon_dispar',
'Chorthippus_mollis',
'Chrysochraon_dispar',
'Gomphocerippus_rufus',
# 'Pseudochorthippus_parallelus',
'Pseudochorthippus_parallelus',
]
pure_paths = search_files(targets, incl='subset', excl='noise', dir='../data/inv/thresh_lp/')
n_species = len(target_species)
load_kwargs = dict(
keywords=['scales', 'measure', 'thresh']
)
save_path = '../figures/fig_invariance_thresh_lp_species.pdf'
exclude_zero = True
show_noise = True
# SUBSET SETTINGS:
thresh_percent = np.array([0.6, 0.75, 0.999])[0]
kernels = np.array([
thresh_rel = np.array([0.5, 1, 3])[0]
kern_specs = np.array([
[1, 0.008],
[2, 0.004],
[3, 0.002],
])[np.array([0, 1])]
n_kernels = kern_specs.shape[0]
# GRAPH SETTINGS:
fig_kwargs = dict(
figsize=(32/2.54, 16/2.54),
figsize=(32/2.54, 20/2.54),
)
n_species = len(targets)
super_grid_kwargs = dict(
nrows=2,
ncols=n_species + 2,
nrows=3,
ncols=1,
wspace=0,
hspace=0,
left=0,
right=1,
bottom=0,
top=1
top=1,
height_ratios=[1, 4, 3]
)
subfig_specs = dict(
spec=(slice(None), slice(0, n_species)),
big=(slice(None), slice(n_species, None))
song=(0, 0),
feat=(1, 0),
space=(2, 0)
)
spec_grid_kwargs = dict(
feat_grid_kwargs = dict(
nrows=2,
ncols=n_species,
wspace=0.25,
hspace=0.1,
left=0.1,
right=0.97,
hspace=0.15,
left=0.06,
right=0.985,
bottom=0.1,
top=0.94
)
big_grid_kwargs = dict(
nrows=2,
ncols=1,
wspace=0,
hspace=0.2,
left=0,
right=1,
bottom=spec_grid_kwargs['bottom'],
top=spec_grid_kwargs['top']
song_grid_kwargs = dict(
nrows=1,
ncols=n_species,
wspace=feat_grid_kwargs['wspace'],
hspace=0,
left=feat_grid_kwargs['left'],
right=feat_grid_kwargs['right'],
bottom=0.1,
top=0.8
)
space_grid_kwargs = dict(
nrows=1,
ncols=2,
wspace=0.2,
hspace=0,
left=feat_grid_kwargs['left'],
right=feat_grid_kwargs['right'],
bottom=0.05,
top=0.95
)
anchor_kwargs = dict(
aspect='equal',
adjustable='box',
anchor=(0.3, 0.5)
anchor=(0, 0.5)
)
inset_kwargs = dict(
y0=0.7,
@@ -208,50 +225,56 @@ fs = dict(
tit_tex=20,
bar=16,
)
base_color = load_colors('../data/stage_colors.npz')['feat']
spec_cmaps = [
'Reds',
'Greens',
'Blues',
]
species_colors = load_colors('../data/species_colors.npz')
kernel_shades = [0, 0.5]
# scale_shades = [1, 0]
lw = dict(
spec=2,
song=0.5,
feat=3,
kern=3
)
zorder = dict(
Omocestus_rufipes=2,
Chorthippus_biguttulus=2.5,
Chorthippus_mollis=2.4,
Chrysochraon_dispar=2,
Gomphocerippus_rufus=2,
Pseudochorthippus_parallelus=2,
)
space_kwargs = dict(
s=30,
)
xlabels = dict(
spec='scale $\\alpha$',
big='$\\mu_{f_1}$'
feat='scale $\\alpha$',
space='$\\mu_{f_1}$'
)
ylabels = dict(
spec='$\\mu_f$',
big='$\\mu_{f_2}$',
feat='$\\mu_f$',
space='$\\mu_{f_2}$',
bar='scale $\\alpha$',
)
xlab_spec_kwargs = dict(
xlab_feat_kwargs = dict(
y=0,
fontsize=fs['lab_norm'],
ha='center',
va='bottom',
)
xlab_big_kwargs = dict(
xlab_space_kwargs = dict(
y=0,
fontsize=fs['lab_tex'],
ha='center',
va='bottom',
)
ylab_spec_kwargs = dict(
ylab_feat_kwargs = dict(
x=0,
fontsize=fs['lab_tex'],
ha='left',
va='center',
)
ylab_big_kwargs = dict(
x=0.03,
ylab_space_kwargs = dict(
x=0,
fontsize=fs['lab_tex'],
ha='center',
ha='left',
va='center',
)
ylab_cbar_kwargs = dict(
@@ -261,28 +284,57 @@ ylab_cbar_kwargs = dict(
va='bottom',
)
xloc = dict(
big=0.5,
space=0.5,
)
yloc = dict(
spec=0.5,
big=0.5
feat=0.5,
space=0.5
)
letter_spec_kwargs = dict(
symlog_kwargs = dict(
linscale=0.5,
)
title_kwargs = dict(
x=0.5,
yref=1,
ha='center',
va='top',
fontsize=fs['tit_norm'],
fontstyle='italic'
)
letter_feat_kwargs = dict(
x=0,
yref=1,
ha='center',
va='top',
fontsize=fs['letter'],
)
letter_big_kwargs = dict(
letter_space_kwargs = dict(
x=0,
yref=1,
ha='center',
va='top',
fontsize=fs['letter'],
)
time_bar_kwargs = dict(
dur=0.05,
song_bar_time = 1.0
song_bar_kwargs = dict(
dur=song_bar_time,
y0=-0.1,
y1=0,
xshift=0,
color='k',
lw=0,
clip_on=False,
# text_pos=(-0.1, 0.5),
text_str=f'${int(1000 * song_bar_time)}\\,\\text{{ms}}$',
text_kwargs=dict(
fontsize=fs['bar'],
ha='right',
va='center',
)
)
kern_bar_time = 0.05
kern_bar_kwargs = dict(
dur=kern_bar_time,
y0=inset_kwargs['y0'],
y1=inset_kwargs['y0'] + 0.03,
color='k',
@@ -290,11 +342,16 @@ time_bar_kwargs = dict(
)
cbar_bounds = [
0.05,
big_grid_kwargs['bottom'],
space_grid_kwargs['bottom'],
0.15,
big_grid_kwargs['top'] - big_grid_kwargs['bottom']
space_grid_kwargs['top'] - space_grid_kwargs['bottom']
]
shade_factors = [0.9, -0.9]
noise_kwargs = dict(
fc=(0.9, 0.9, 0.9),
ec='none',
lw=0,
zorder=0.5,
)
# EXECUTION:
@@ -302,105 +359,165 @@ shade_factors = [0.9, -0.9]
fig = plt.figure(**fig_kwargs)
super_grid = fig.add_gridspec(**super_grid_kwargs)
# Prepare species-specific axes:
spec_subfig = fig.add_subfigure(super_grid[subfig_specs['spec']])
spec_grid = spec_subfig.add_gridspec(**spec_grid_kwargs)
spec_axes = np.zeros((spec_grid_kwargs['nrows'], n_species), dtype=object)
for i, j in product(range(spec_grid_kwargs['nrows']), range(n_species)):
ax = spec_subfig.add_subplot(spec_grid[i, j])
ax.set_xscale('symlog', linthresh=0.1, linscale=0.5)
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc['spec']))
# Prepare song axes:
song_subfig = fig.add_subfigure(super_grid[subfig_specs['song']])
song_grid = song_subfig.add_gridspec(**song_grid_kwargs)
song_axes = np.zeros((n_species,), dtype=object)
for i in range(n_species):
ax = song_subfig.add_subplot(song_grid[i])
hide_axis(ax, 'bottom')
hide_axis(ax, 'left')
song_axes[i] = ax
# Prepare feature invariance axes:
feat_subfig = fig.add_subfigure(super_grid[subfig_specs['feat']])
feat_grid = feat_subfig.add_gridspec(**feat_grid_kwargs)
feat_axes = np.zeros((feat_grid_kwargs['nrows'], n_species), dtype=object)
for i, j in product(range(feat_grid_kwargs['nrows']), range(n_species)):
ax = feat_subfig.add_subplot(feat_grid[i, j])
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc['feat']))
ax.set_ylim(0, 1)
spec_axes[i, j] = ax
super_xlabel(xlabels['spec'], spec_subfig, spec_axes[-1, 0], spec_axes[-1, -1], **xlab_spec_kwargs)
super_ylabel(ylabels['spec'], spec_subfig, spec_axes[-1, 0], spec_axes[0, 0], **ylab_spec_kwargs)
[hide_ticks(ax, side='bottom') for ax in spec_axes[0, :]]
[hide_ticks(ax, side='left') for ax in spec_axes[:, 1:].ravel()]
letter_subplots(spec_axes[0, :], labels='abc', ref=spec_subfig, **letter_spec_kwargs)
feat_axes[i, j] = ax
super_xlabel(xlabels['feat'], feat_subfig, feat_axes[-1, 0], feat_axes[-1, -1], **xlab_feat_kwargs)
super_ylabel(ylabels['feat'], feat_subfig, feat_axes[-1, 0], feat_axes[0, 0], **ylab_feat_kwargs)
[hide_ticks(ax, side='bottom') for ax in feat_axes[0, :]]
[hide_ticks(ax, side='left') for ax in feat_axes[:, 1:].ravel()]
letter_subplots(feat_axes[0, :], labels='abc', ref=feat_subfig, **letter_feat_kwargs)
# Prepare kernel insets:
x0 = np.linspace(0, 1, kernels.shape[0] + 1)[:-1] + 1 / kernels.shape[0] / 2
x0 = np.linspace(0, 1, n_kernels + 1)[:-1] + 1 / n_kernels / 2
x0 -= inset_kwargs['w'] / 2
insets = []
for i in range(kernels.shape[0]):
for i in range(n_kernels):
bounds = [x0[i], inset_kwargs['y0'], inset_kwargs['w'], inset_kwargs['h']]
inset = spec_axes[0, 0].inset_axes(bounds)
inset = feat_axes[0, 0].inset_axes(bounds)
inset.set_title(rf'$k_{{{i+1}}}$', fontsize=20)
inset.axis('off')
insets.append(inset)
# Prepare feature space axes:
big_subfig = fig.add_subfigure(super_grid[subfig_specs['big']])
big_grid = big_subfig.add_gridspec(**big_grid_kwargs)
big_axes = np.zeros(super_grid_kwargs['nrows'], dtype=object)
for i in range(big_axes.size):
ax = big_subfig.add_subplot(big_grid[i, 0])
space_subfig = fig.add_subfigure(super_grid[subfig_specs['space']])
space_grid = space_subfig.add_gridspec(**space_grid_kwargs)
space_axes = np.zeros(space_grid_kwargs['ncols'], dtype=object)
for i in range(space_axes.size):
ax = space_subfig.add_subplot(space_grid[i])
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.xaxis.set_major_locator(plt.MultipleLocator(xloc['big']))
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc['big']))
ax.xaxis.set_major_locator(plt.MultipleLocator(xloc['space']))
ax.yaxis.set_major_locator(plt.MultipleLocator(yloc['space']))
ax.set_aspect(**anchor_kwargs)
# ax.set_ylabel(ylabels['big'], **ylab_big_kwargs)
ylabel(ax, ylabels['big'], transform=big_subfig.transSubfigure, **ylab_big_kwargs)
big_axes[i] = ax
super_xlabel(xlabels['big'], big_subfig, big_axes[1], big_axes[1], **xlab_big_kwargs)
hide_ticks(big_axes[0], side='bottom')
letter_subplot(big_axes[0], 'd', ref=big_subfig, **letter_big_kwargs)
# ax.set_ylabel(ylabels['space'], **ylab_space_kwargs)
ylabel(ax, ylabels['space'], transform=space_subfig.transSubfigure, **ylab_space_kwargs)
space_axes[i] = ax
super_xlabel(xlabels['space'], space_subfig, space_axes[1], space_axes[1], **xlab_space_kwargs)
hide_ticks(space_axes[0], side='bottom')
letter_subplot(space_axes[0], 'd', ref=space_subfig, **letter_space_kwargs)
# Prepare colorbars:
cbar_bounds[0] += big_axes[-1].get_position().x1
bar_axes = [big_subfig.add_axes(cbar_bounds)]
bar_axes.extend(split_subplot(bar_axes[0], side=['right', 'right'], size=100, pad=0))
cbar_bounds[0] += space_axes[-1].get_position().x1
bar_axes = [space_subfig.add_axes(cbar_bounds)]
bar_axes.extend(split_subplot(bar_axes[0], side=['right'] * (n_species - 1),
size=100, pad=0))
# Prepare kernel-specific color shading:
kern_factors = np.linspace(*kernel_shades, n_kernels)
kern_colors_bw = shade_colors((0., 0., 0.), kern_factors)
# Plot results per species:
for i, pure_path in enumerate(pure_paths):
print(f'Processing {pure_path}')
noise_path = pure_path.replace('.npz', '_noise.npz')
min_feat = np.zeros((n_species, n_kernels), dtype=float)
for i, species in enumerate(target_species):
print(f'Processing {species}')
# Fetch species-specific recording file:
song_path = search_files(species, dir='../data/processed/')[0]
# Load song data:
song_data, _ = load_data(song_path, files='filt')
song, rate = song_data['filt'], song_data['filt_rate']
# Plot species snippet:
song_ax = song_axes[i]
time = np.arange(song.shape[0]) / rate
plot_line(song_ax, time, song, ypad=0.05, c='k', lw=lw['song'])
title_subplot(song_ax, shorten_species(species), ref=song_subfig, **title_kwargs)
time_bar(song_ax, **song_bar_kwargs)
# Fetch species-specific invariance files:
pure_path = search_files(species, incl='pure', dir='../data/inv/thresh_lp/')[0]
noise_path = search_files(species, incl='noise', dir='../data/inv/thresh_lp/')[0]
# Load invariance data:
pure_data, config = load_data(pure_path, **load_kwargs)
noise_data, _ = load_data(noise_path, **load_kwargs)
scales = pure_data['scales']
# Reduce to kernel subset and single threshold:
thresh_ind = np.nonzero(pure_data['thresh_perc'] == thresh_percent)[0][0]
kern_inds = find_kern_specs(config['k_specs'], kerns=kernels)
# Reduce to kernel subset and a single threshold:
thresh_ind = np.nonzero(pure_data['thresh_rel'] == thresh_rel)[0][0]
kern_inds = find_kern_specs(config['k_specs'], kerns=kern_specs)
config['k_specs'] = config['k_specs'][kern_inds]
config['kernels'] = config['kernels'][:, kern_inds]
pure_measure = pure_data['measure_feat'][:, kern_inds, thresh_ind]
noise_measure = noise_data['measure_feat'][:, kern_inds, thresh_ind]
if exclude_zero:
# Reduce to nonzero scales:
nonzero_inds = scales > 0
scales = scales[nonzero_inds]
pure_measure = pure_measure[nonzero_inds, :]
noise_measure = noise_measure[nonzero_inds, :]
min_feat[i, :] = noise_measure.min(axis=0)
# Plot invariance curves:
pure_ax, noise_ax = spec_axes[:, i]
pure_ax.plot(scales, pure_measure, c=base_color, lw=lw['spec'])
noise_ax.plot(scales, noise_measure, c=base_color, lw=lw['spec'])
# Prepare species-specific colors:
base_color = species_colors[species]
kern_colors = shade_colors(base_color, kern_factors)
scale_factors = np.linspace(1, 0, scales.size)
scale_cmap = create_listed_cmap(shade_colors(base_color, scale_factors))
scale_cmap_bw = create_listed_cmap(shade_colors((0., 0., 0.), scale_factors))
# Plot feature invariance curves:
pure_ax, noise_ax = feat_axes[:, i]
symlog_kwargs['linthresh'] = scales[scales > 0][0]
[ax.set_xscale('symlog', **symlog_kwargs) for ax in feat_axes[:, i]]
pure_ax.set_xscale('symlog', **symlog_kwargs)
noise_ax.set_xscale('symlog', **symlog_kwargs)
handles = pure_ax.plot(scales, pure_measure, lw=lw['feat'])
[h.set_color(c) for h, c in zip(handles, kern_colors)]
handles = noise_ax.plot(scales, noise_measure, lw=lw['feat'])
[h.set_color(c) for h, c in zip(handles, kern_colors)]
if i == 0:
# Indicate kernel waveforms:
ylims = ylimits(config['kernels'], pad=0.05)
xlims = (config['k_times'][0], config['k_times'][-1])
for j, inset in enumerate(insets):
inset.plot(config['k_times'], config['kernels'][:, j],
c='k', lw=lw['kern'])
for kern, inset, c in zip(config['kernels'].T, insets, kern_colors_bw):
inset.plot(config['k_times'], kern, c=c, lw=lw['kern'])
inset.set_xlim(xlims)
inset.set_ylim(ylims)
time_bar(insets[0], parent=spec_axes[0, 0], **time_bar_kwargs)
time_bar(insets[0], parent=feat_axes[0, 0], **kern_bar_kwargs)
# Plot pure feature space:
handle = big_axes[0].scatter(pure_measure[:, 0], pure_measure[:, 1],
c=scales, cmap=spec_cmaps[i], **space_kwargs)
from matplotlib.colors import LogNorm
norm = LogNorm(vmin=scales[scales > 0][0], vmax=scales[-1])
handle = space_axes[0].scatter(pure_measure[:, 0], pure_measure[:, 1],
c=scales, cmap=scale_cmap, norm=norm,
zorder=zorder[species], **space_kwargs)
# Plot noise feature space:
big_axes[1].scatter(noise_measure[:, 0], noise_measure[:, 1],
c=scales, cmap=spec_cmaps[i], **space_kwargs)
space_axes[1].scatter(noise_measure[:, 0], noise_measure[:, 1],
c=scales, cmap=scale_cmap, norm=norm,
zorder=zorder[species], **space_kwargs)
# Indicate scale color code:
big_subfig.colorbar(handle, cax=bar_axes[i])
bar_axes[i].set_yscale('symlog', linthresh=scales[1], linscale=0.2)
if i < len(pure_paths) - 1:
space_subfig.colorbar(handle, cax=bar_axes[i])
bar_axes[i].set_yscale('symlog', **symlog_kwargs)
if i < n_species - 1:
hide_ticks(bar_axes[i], 'right', ticks=False)
else:
ylabel(bar_axes[i], ylabels['bar'], transform=big_subfig.transSubfigure, **ylab_cbar_kwargs)
ylabel(bar_axes[i], ylabels['bar'], transform=space_subfig.transSubfigure, **ylab_cbar_kwargs)
if show_noise:
# Indicate feature noise floor:
min_feat = min_feat.mean(axis=0)
space_axes[-1].add_patch(plt.Rectangle((0, 0), min_feat[0], min_feat[1], **noise_kwargs))
if save_path is not None:
fig.savefig(save_path)