<jupyter_start><jupyter_text>Interpretablity of Speech Commands<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
# Data loc
save_dir = '../experiments/results/speech_commands/L2'
# Load data
shapelets = torch.load(save_dir + '/shapelets.pt')
shapelet_times_full = torch.load(save_dir + '/shapelet_times.pt')
minimizers = torch.load(save_dir + '/minimizers.pt')
minimizer_times_full = torch.load(save_dir + '/minimizer_times.pt')
lengths = torch.load(save_dir + '/lengths.pt')

# Single sample
idx = 0
shapelet = shapelets[idx][:, 0:14]
shapelet_times = shapelet_times_full[idx]
minimizer = minimizers[idx][:, 0:14]
minimizer_times = minimizer_times_full[idx]
length = lengths[0]
# Subset the minimizer to the shapelet times
subset_bool = (minimizer_times >= shapelet_times[0]) & (minimizer_times <= shapelet_times[-1])
subset_times = minimizer_times[subset_bool]
minimizer_subset = minimizer[subset_bool]

delta = minimizer_subset - shapelet
# Compute delta
delta = shapelet - minimizer_subset
vmin = min(shapelet.min(), minimizer_subset.min())
vmax = max(shapelet.max(), minimizer_subset.max())
time_ratio = (shapelet_times[-1] - shapelet_times[0]) / (minimizer_times[-1] - minimizer_times[0])
start_val = np.argwhere(minimizer_times == shapelet_times[0])[0]
end_val = np.argwhere(minimizer_times == shapelet_times[-1])[0]
vmax = max(vmax, -vmin)
vmin = min(vmin, -vmax)
fig, ax = plt.subplots(1, 3, figsize=(25, 3.5), gridspec_kw={'width_ratios': [1, time_ratio, time_ratio + 0.17 * time_ratio]})
cmap = sns.diverging_palette(12.2, 258.6, n=20, sep=14, s=90, l=40)
# sns.heatmap(minimizer_subset.T, ax=ax[1], cmap=cmap, vmin=vmin, vmax=vmax, cbar=False)
yticklabels = np.arange(1, 15)
sns.heatmap(minimizer.T, ax=ax[0], cmap=cmap, vmin=vmin, vmax=vmax, cbar=False, yticklabels=yticklabels)
sns.heatmap(shapelet.T, ax=ax[1], cmap=cmap, vmin=vmin, vmax=vmax, cbar=False)
sns.heatmap(delta.T, ax=ax[2], cmap=cmap, vmin=vmin, vmax=vmax, cbar=True, cbar_kws=dict(use_gridspec=True))

plt.subplots_adjust(wspace=0.1, hspace=0)

# Dashed lines around shapelet
ax[0].plot([start_val, start_val], [0.1, 13.95], color='black', ls='--')
ax[0].plot([start_val, end_val], [0.1, 0.1], color='black', ls='--')
ax[0].plot([start_val, end_val], [13.95, 13.95], color='black', ls='--')
ax[0].plot([end_val, end_val], [0.1, 13.95], color='black', ls='--')

ax[0].set_title('Training set minimizer', fontsize=18)
ax[1].set_title('Shapelet', fontsize=18)
ax[2].set_title('$\Delta$', fontsize=18)

ax[0].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
ax[1].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
ax[2].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)

ax[1].tick_params(axis='y', which='both', left=False, top=False, labelleft=False)
ax[2].tick_params(axis='y', which='both', left=False, top=False, labelleft=False)

ax[0].set_xlabel('$\overrightarrow{ Time }$', fontsize=18)
ax[1].set_xlabel('$\overrightarrow{ Time }$', fontsize=16)
ax[2].set_xlabel('$\overrightarrow{ Time }$', fontsize=16)

ax[0].set_ylabel('MFC coefficient', fontsize=16)
# ax[1].set_ylabel('MFC coefficient', fontsize=16)
# ax[2].set_ylabel('MFC coefficient', fontsize=16)

plt.savefig('./figures/new_speech_commands_heatmap.png', dpi=300, bbox_inches='tight')
fig, ax = plt.subplots(8, 5, figsize=(20, 25))
ax = ax.ravel()

shapelet = shapelets[0]
minimizer = minimizers[0]
shapelet_times = shapelet_times_full[0]
minimizer_times = minimizer_times_full[0]
minimizer_subset = minimizer[subset_bool]

for i in range(shapelet.shape[1]):
    ax[i].plot(shapelet_times, shapelet[:, i])
#     ax[i].plot(shapelet_times, minimizer_subset[:, i], ls='--')
    ax[i].plot(minimizer_times, minimizer[:, i], ls='--')
    ax[i].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
#     ax[i].tick_params(axis='y', which='both', bottom=False, top=False, labelleft=False)
    ax[i].set_title('Coefficient {}'.format(i))

plt.savefig('./figures/new_speech_commands_axisplots.png', dpi=300, bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1844401357246728, 'fraction_numerical': 0.031749878817256424, 'mean_word_length': 7.3373737373737375, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '652906', 'license': 'MIT', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1633, 'n_words': 275, 'path': 'notebooks/speech_interpretability.ipynb', 'repo_name': 'patrick-kidger/generalised_shapelets', 'size': '4126'}