import plotstyle_plt 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 thunderhopper.filtertools import find_kern_specs from misc_functions import reduce_kernel_set, divide_by_zero, y_dist from color_functions import load_colors from plot_functions import hide_axis, reorder_by_sd, ylimits, super_xlabel, ylabel, title_subplot,\ plot_line, xlabel, time_bar, assign_colors,\ letter_subplot, letter_subplots, hide_ticks 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) handles = [] for i, ax in enumerate(axes): handles.append(plot_line(ax, time, snippets[:, ..., i], ymin=ymin, ymax=ymax, **kwargs)) return handles def plot_curves(ax, distances, measures, **kwargs): if measures.ndim == 1: handles = ax.plot(distances, measures, **kwargs) return handles, measures median_measure = np.nanmedian(measures, axis=1) line_handle = ax.plot(distances, median_measure, **kwargs)[0] return line_handle, median_measure def crop_noise_snippets(snippets, nin, nout, stages=['filt', 'env', 'log', 'inv', 'conv', 'feat']): half_offset = int((nin - nout) / 2) segment = np.arange(half_offset, half_offset + nout) for stage in stages: key = f'snip_{stage}' snippets[key] = snippets[key][segment, ...] return snippets # GENERAL SETTINGS: search_target = 'Pseudochorthippus_parallelus' stages = ['filt', 'env', 'log', 'inv', 'conv', 'feat'] song_example = 'Pseudochorthippus_parallelus_micarray-short_JJ_20240815T160355-20240815T160755-1m10s690ms-1m13s614ms' noise_example = 'merged_noise' song_path = search_files(song_example, dir='../data/inv/field/song/')[0] noise_path = search_files(noise_example, dir='../data/inv/field/noise/')[0] save_path = '../figures/fig_invariance_field.pdf' # ANALYSIS SETTINGS: offset_distance = 10 # centimeter thresh_rel = np.array([0, 0.5, 1, 1.5, 2, 2.5, 3])[4] kern_subset_kwargs = dict( combis=[['measure', 'snip'], ['conv', 'feat']], keys=['thresh_abs'], ) # SUBSET SETTINGS: types = np.array([1, -1, 2, -2, 3, -3, 4, -4]) # types = [1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10] sigmas = np.array([0.001, 0.002, 0.004, 0.008, 0.016]) # sigmas = [0.001, 0.002, 0.004, 0.008, 0.016, 0.032] kernels = None reduce_kernels = any(var is not None for var in [kernels, types, sigmas]) # GRAPH SETTINGS: fig_kwargs = dict( figsize=(32/2.54, 32/2.54), ) super_grid_kwargs = dict( nrows=2, ncols=2, wspace=0, hspace=0, left=0, right=1, bottom=0, top=1, height_ratios=[1, 1] ) subfig_specs = dict( snip=(0, slice(None)), raw=(1, 0), base=(1, 1), ) snip_grid_kwargs = dict( nrows=len(stages), ncols=None, wspace=0.1, hspace=0.4, left=0.13, right=0.98, bottom=0.05, top=0.95 ) raw_grid_kwargs = dict( nrows=2, ncols=1, wspace=0, hspace=0.15, left=0.14, right=0.9, bottom=0.25, top=0.95, height_ratios=[0.8, 0.2] ) base_grid_kwargs = dict( nrows=3, ncols=1, wspace=0, hspace=0.25, left=raw_grid_kwargs['left'], right=raw_grid_kwargs['right'], bottom=raw_grid_kwargs['bottom'], top=raw_grid_kwargs['top'], ) inset_dist_bounds = [1.01, 0, 0.95, 1] inset_ax_bounds = [raw_grid_kwargs['left'], 0.1, raw_grid_kwargs['right'] - raw_grid_kwargs['left'], 0.01] # PLOT SETTINGS: fs = dict( lab_norm=16, lab_tex=20, letter=22, tit_norm=16, tit_tex=20, bar=16, ) stage_colors = load_colors('../data/stage_colors.npz') kern_colors = dict( conv=load_colors('../data/conv_colors_subset.npz'), feat=load_colors('../data/feat_colors_subset.npz') ) lw = dict( filt=0.25, env=0.25, log=0.25, inv=0.25, conv=0.25, feat=1, single=3, swarm=1, legend=5, dist=1 ) xlabels = dict( high='$1\\,/\\,d\\,\\sim\\,\\alpha$ [cm$^{-1}$]', low='distance $d$ [cm]', ) ylabels = dict( filt='$x_{\\text{filt}}$\n$[\\text{a.u.}]$', env='$x_{\\text{env}}$\n$[\\text{a.u.}]$', log='$x_{\\text{log}}$\n$[\\text{dB}]$', inv='$x_{\\text{adapt}}$\n$[\\text{dB}]$', conv='$c_i$\n$[\\text{dB}]$', feat='$f_i$', raw=['$m$', '$\\mu_{f_i}$'], base=['$m\\,/\\,m_{\\eta}$', '$\\sigma_{c_i}\\,/\\,\\sigma_{\\eta_i}$', '$\\mu_{f_i}\\,/\\,\\mu_{\\eta_i}$'] ) xlab_high_kwargs = dict( y=0.15, fontsize=fs['lab_norm'], ha='center', va='bottom', ) xlab_low_kwargs = dict( y=0, fontsize=fs['lab_norm'], ha='center', va='bottom', ) ylab_snip_kwargs = dict( x=0.03, fontsize=fs['lab_tex'], rotation=0, ha='center', va='center', ma='center' ) ylab_big_kwargs = dict( x=0, fontsize=fs['lab_norm'], ha='center', va='top', ) yloc = dict( filt=300, env=100, log=50, inv=20, conv=0.5, feat=1, ) title_kwargs = dict( x=0.5, yref=1, ha='center', va='top', fontsize=fs['tit_norm'], ) letter_snip_kwargs = dict( x=0, yref=0.5, ha='left', va='center', fontsize=fs['letter'], ) letter_big_kwargs = dict( xref=0, y=1, ha='left', va='bottom', fontsize=fs['letter'], ) song_bar_time = 1 song_bar_kwargs = dict( dur=song_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'${song_bar_time}\\,\\text{{s}}$', text_kwargs=dict( fontsize=fs['bar'], ha='right', va='center', ) ) noise_bar_time = 0.5 noise_bar_kwargs = song_bar_kwargs.copy() noise_bar_kwargs['dur'] = noise_bar_time noise_bar_kwargs['text_str'] = f'${int(1000 * noise_bar_time)}\\,\\text{{ms}}$' leg_labels = dict( filt='$x_{\\text{filt}}$', env='$x_{\\text{env}}$', log='$x_{\\text{log}}$', inv='$x_{\\text{adapt}}$', conv='$c_i$', feat='$f_i$' ) leg_kwargs = dict( ncols=3, loc='upper left', bbox_to_anchor=(0.025, 0.9, 0.95, 0.1), frameon=False, prop=dict( size=20, ), borderpad=0, borderaxespad=0, handlelength=1, columnspacing=1, handletextpad=0.5, labelspacing=0.1 ) dist_line_kwargs = dict( lw=lw['dist'], ) dist_fill_kwargs = dict( lw=lw['dist'], ) # EXECUTION: # Load song invariance data: song_data, config = load_data(song_path, files='distances', keywords=['measure', 'snip', 'thresh']) t_song = np.arange(song_data['snip_filt'].shape[0]) / config['rate'] dists = song_data['distances'] + offset_distance scales = 1 / dists snip_dists = ['noise'] + [f'{int(d)}$\\,$cm' for d in dists] # Load noise invariance data: noise_data, _ = load_data(noise_path, keywords=['measure', 'snip', 'thresh']) noise_data = crop_noise_snippets(noise_data, noise_data['snip_filt'].shape[0], t_song.size) t_noise = np.arange(noise_data['snip_filt'].shape[0]) / config['rate'] # Reduce kernels: if reduce_kernels: kern_inds = find_kern_specs(config['k_specs'], kernels, types, sigmas) config['k_specs'] = config['k_specs'][kern_inds, :] config['kernels'] = config['kernels'][:, kern_inds] song_data = reduce_kernel_set(song_data, kern_inds, **kern_subset_kwargs) noise_data = reduce_kernel_set(noise_data, kern_inds, **kern_subset_kwargs) # Reduce thresholds: thresh_ind = np.nonzero(song_data['thresh_rel'] == thresh_rel)[0][0] song_data['measure_feat'] = song_data['measure_feat'][:, :, thresh_ind] song_data['snip_feat'] = song_data['snip_feat'][:, :, :, thresh_ind] noise_data['measure_feat'] = noise_data['measure_feat'][:, :, thresh_ind] noise_data['snip_feat'] = noise_data['snip_feat'][:, :, :, thresh_ind] # Adjust grid parameters: snip_grid_kwargs['ncols'] = len(snip_dists) # 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.yaxis.set_major_locator(plt.MultipleLocator(yloc[stages[i]])) hide_axis(ax, 'bottom') if i == 0: title = title_subplot(ax, snip_dists[j], ref=snip_subfig, **title_kwargs) if j == 0: ax.set_xlim(t_noise[0], t_noise[-1]) ylabel(ax, ylabels[stages[i]], **ylab_snip_kwargs, transform=snip_subfig.transSubfigure) else: ax.set_xlim(t_song[0], t_song[-1]) hide_axis(ax, 'left') snip_axes[i, j] = ax time_bar(snip_axes[-1, -1], **song_bar_kwargs) # time_bar(snip_axes[-1, 0], **noise_bar_kwargs) letter_subplot(snip_subfig, 'a', ref=title, **letter_snip_kwargs) # Prepare raw analysis axes: raw_subfig = fig.add_subfigure(super_grid[subfig_specs['raw']]) raw_grid = raw_subfig.add_gridspec(**raw_grid_kwargs) raw_axes = np.zeros((raw_grid.nrows,), dtype=object) for i in range(raw_grid.nrows): ax = raw_subfig.add_subplot(raw_grid[i, 0]) ax.set_xlim(scales[0], scales[-1]) ax.set_xscale('log') ylabel(ax, ylabels['raw'][i], transform=raw_subfig.transSubfigure, **ylab_big_kwargs) if i == 0: ax.set_yscale('symlog', linthresh=0.00001, linscale=0.1) hide_ticks(ax, 'bottom') else: transform = raw_subfig.transSubfigure + ax.transAxes.inverted() inset_x1 = transform.transform((inset_dist_bounds[2], 0))[0] inset_dist_bounds[2] = inset_x1 - inset_dist_bounds[0] raw_inset = ax.inset_axes(inset_dist_bounds) raw_inset.axis('off') raw_axes[i] = ax letter_subplots(raw_axes, 'bc', ref=raw_subfig, **letter_big_kwargs) xlabel(raw_axes[-1], xlabels['high'], transform=raw_subfig.transSubfigure, **xlab_high_kwargs) # Prepare base analysis axes: base_subfig = fig.add_subfigure(super_grid[subfig_specs['base']]) base_grid = base_subfig.add_gridspec(**base_grid_kwargs) base_axes = np.zeros((base_grid.nrows,), dtype=object) base_insets = np.zeros((base_grid.nrows - 1,), dtype=object) for i in range(base_grid.nrows): ax = base_subfig.add_subplot(base_grid[i, 0]) ax.set_xlim(scales[0], scales[-1]) ax.set_xscale('log') ax.set_yscale('log') ylabel(ax, ylabels['base'][i], transform=base_subfig.transSubfigure, **ylab_big_kwargs) if i < base_grid_kwargs['nrows'] - 1: hide_ticks(ax, 'bottom') if i > 0: inset = ax.inset_axes(inset_dist_bounds) inset.set_yscale('log') inset.axis('off') base_insets[i - 1] = inset base_axes[i] = ax letter_subplots(base_axes, 'def', ref=base_subfig, **letter_big_kwargs) xlabel(base_axes[-1], xlabels['high'], transform=base_subfig.transSubfigure, **xlab_high_kwargs) if True: # Plot filtered snippets: plot_snippets(snip_axes[0, 1:], t_song, song_data['snip_filt'], c=stage_colors['filt'], lw=lw['filt']) plot_line(snip_axes[0, 0], t_noise, noise_data['snip_filt'][:, 0], *snip_axes[0, 1].get_ylim(), c=stage_colors['filt'], lw=lw['filt']) # Plot envelope snippets: plot_snippets(snip_axes[1, 1:], t_song, song_data['snip_env'], ymin=0, c=stage_colors['env'], lw=lw['env']) plot_line(snip_axes[1, 0], t_noise, noise_data['snip_env'][:, 0], *snip_axes[1, 1].get_ylim(), c=stage_colors['env'], lw=lw['env']) # Plot logarithmic snippets: plot_snippets(snip_axes[2, 1:], t_song, song_data['snip_log'], c=stage_colors['log'], lw=lw['log']) plot_line(snip_axes[2, 0], t_noise, noise_data['snip_log'][:, 0], *snip_axes[2, 1].get_ylim(), c=stage_colors['log'], lw=lw['log']) # Plot invariant snippets: plot_snippets(snip_axes[3, 1:], t_song, song_data['snip_inv'], c=stage_colors['inv'], lw=lw['inv']) plot_line(snip_axes[3, 0], t_noise, noise_data['snip_inv'][:, 0], *snip_axes[3, 1].get_ylim(), c=stage_colors['inv'], lw=lw['inv']) # Plot kernel response snippets: all_handles = plot_snippets(snip_axes[4, 1:], t_song, song_data['snip_conv'], c=stage_colors['conv'], lw=lw['conv']) for i, handles in enumerate(all_handles): assign_colors(handles, config['k_specs'][:, 0], kern_colors['conv']) reorder_by_sd(handles, song_data['snip_conv'][..., i]) handles = plot_line(snip_axes[4, 0], t_noise, noise_data['snip_conv'][:, 0], *snip_axes[4, 1].get_ylim(), c=stage_colors['conv'], lw=lw['conv']) assign_colors(handles, config['k_specs'][:, 0], kern_colors['conv']) reorder_by_sd(handles, noise_data['snip_conv'][:, 0]) # Plot feature snippets: all_handles = plot_snippets(snip_axes[5, 1:], t_song, song_data['snip_feat'], ymin=0, ymax=1, c=stage_colors['feat'], lw=lw['feat']) for i, handles in enumerate(all_handles): assign_colors(handles, config['k_specs'][:, 0], kern_colors['feat']) reorder_by_sd(handles, song_data['snip_feat'][..., i]) handles = plot_line(snip_axes[5, 0], t_noise, noise_data['snip_feat'][:, 0], ymin=0, ymax=1, c=stage_colors['feat'], lw=lw['feat']) assign_colors(handles, config['k_specs'][:, 0], kern_colors['feat']) reorder_by_sd(handles, noise_data['snip_feat'][:, 0]) # Plot analysis results: leg_handles = [] for stage in stages: mkey = f'measure_{stage}' measure = song_data[mkey] color = stage_colors[stage] ## UNNORMALIZED MEASURE: # Plot single raw intensity curve (median where necessary): handles, curve = plot_curves(raw_axes[0], scales, measure, c=color, lw=lw['single']) # Add stage-specific proxy legend artist: leg_handles.append(raw_axes[0].plot([], [], c=color, label=leg_labels[stage])[0]) # Plot curve swarm: if stage == 'feat': # Sync y-limits: ylimits(measure, raw_axes[1], minval=0, pad=0.05) raw_inset.set_ylim(raw_axes[1].get_ylim()) # Plot swarm: handles = raw_axes[1].plot(scales, measure, lw=lw['swarm']) assign_colors(handles, config['k_specs'][:, 0], kern_colors[stage]) reorder_by_sd(handles, measure) # Plot distribution of saturation levels: line_kwargs = dist_line_kwargs | dict(c=color) fill_kwargs = dist_fill_kwargs | dict(color=color) y_dist(raw_inset, measure[-1], nbins=75, log=False, line_kwargs=line_kwargs, fill_kwargs=fill_kwargs) ## NORMALIZED MEASURE: # Relate to noise baseline: measure = divide_by_zero(song_data[mkey], noise_data[mkey].mean(axis=0)) # Plot single baseline-normalized intensity curve (median where necessary): handles, curve = plot_curves(base_axes[0], scales, measure, c=color, lw=lw['single']) # Plot curve swarm: if stage in ['conv', 'feat']: i0, i1 = (1, 0) if stage == 'conv' else (2, 1) # Sync y-limits: ylimits(measure, base_axes[i0], minval=0.9, pad=0.05) base_insets[i1].set_ylim(base_axes[i0].get_ylim()) # Plot swarm: handles = base_axes[i0].plot(scales, measure, lw=lw['swarm']) assign_colors(handles, config['k_specs'][:, 0], kern_colors[stage]) reorder_by_sd(handles, measure) # Plot distribution of saturation levels: line_kwargs = dist_line_kwargs | dict(c=color) fill_kwargs = dist_fill_kwargs | dict(color=color) y_dist(base_insets[i1], measure[-1], nbins=100, log=True, line_kwargs=line_kwargs, fill_kwargs=fill_kwargs) # Posthoc adjustments: raw_axes[0].set_ylim(top=100) base_axes[0].set_ylim(1, 100) base_axes[1].set_ylim(bottom=1) base_insets[0].set_ylim(bottom=1) base_axes[2].set_ylim(bottom=1) base_insets[1].set_ylim(bottom=1) # Add secondary x-axes: for subfig in [raw_subfig, base_subfig]: dual_ax = subfig.add_subplot(inset_ax_bounds) dual_ax.set_xlim(scales[0], scales[-1]) dual_ax.set_xscale('log') dual_ax.tick_params(axis='x', which='minor', bottom=False) dual_ax.tick_params(axis='x', which='major', labelrotation=45) dual_ax.set_xticks(scales, dists) hide_axis(dual_ax, 'left') xlabel(dual_ax, xlabels['low'], transform=subfig.transSubfigure, **xlab_low_kwargs) # Add legend to first analysis axis: legend = raw_axes[0].legend(handles=leg_handles, **leg_kwargs) [handle.set_lw(lw['legend']) for handle in legend.get_lines()] # Save graph: if save_path is not None: fig.savefig(save_path) plt.show() print('Done.') embed()