Overhauled Thresh-LP analysis and figures (WIP).
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import glob
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from thunderhopper.modeltools import load_data, save_data
|
||||
from thunderhopper.filetools import crop_paths
|
||||
from thunderhopper.filters import sosfilter
|
||||
@@ -12,30 +13,40 @@ save_path = '../data/inv/thresh_lp/'
|
||||
|
||||
# ANALYSIS SETTINGS:
|
||||
add_noise = False
|
||||
threshold = 0.5
|
||||
example_scales = np.array([threshold, 0.6, 1, 10, 50, 100])
|
||||
scales = np.linspace(threshold + 0.1, 100, 100)
|
||||
if not add_noise:
|
||||
example_scales = example_scales[example_scales > threshold]
|
||||
thresh_percent = 90
|
||||
example_scales = np.array([0, 0.5, 1, 10, 50])
|
||||
scales = np.geomspace(0.01, 50, 100)
|
||||
scales = np.unique(np.concatenate((scales, example_scales)))
|
||||
plot_results = True
|
||||
|
||||
# EXECUTION:
|
||||
for data_path, name in zip(data_paths, crop_paths(data_paths)):
|
||||
print(f'Processing {name}')
|
||||
save_name = save_path + name
|
||||
|
||||
# Get normalized pure-song kernel responses:
|
||||
# Get pure-song kernel responses:
|
||||
data, config = load_data(data_path, files='conv')
|
||||
song, rate = data['conv'], data['conv_rate']
|
||||
song /= song.std(axis=0)
|
||||
|
||||
# Prepare kernel-specific thresholds:
|
||||
threshold *= song.max(axis=0, keepdims=True)
|
||||
# Get song segment to be analyzed:
|
||||
time = np.arange(song.shape[0]) / rate
|
||||
start, end = data['songs_0'].ravel()
|
||||
segment = (time >= start) & (time <= end)
|
||||
|
||||
# Normalize song component:
|
||||
song /= song[segment, :].std(axis=0)
|
||||
|
||||
if add_noise:
|
||||
# Get normalized noise:
|
||||
rng = np.random.default_rng()
|
||||
noise = rng.normal(size=(song.shape[0], 1))
|
||||
noise /= noise.std()
|
||||
noise /= noise[segment].std()
|
||||
|
||||
# Prepare noise-bound threshold:
|
||||
threshold = np.percentile(noise, thresh_percent, axis=0)
|
||||
else:
|
||||
# Reuse threshold from previous noise run:
|
||||
threshold = np.load(save_name + '_noise.npz')['thresh']
|
||||
|
||||
# Prepare snippet storage:
|
||||
shape = song.shape + (example_scales.size,)
|
||||
@@ -71,23 +82,32 @@ for data_path, name in zip(data_paths, crop_paths(data_paths)):
|
||||
feat[:, :, scale_ind] = scaled_feat
|
||||
|
||||
# Get "intensity measure" per stage:
|
||||
measure_conv[i] = scaled_conv.std(axis=0)
|
||||
measure_feat[i] = scaled_feat.mean(axis=0)
|
||||
measure_conv[i] = scaled_conv[segment, :].std(axis=0)
|
||||
measure_feat[i] = scaled_feat[segment, :].mean(axis=0)
|
||||
|
||||
# Relate to smallest scale:
|
||||
base_ind = np.argmin(scales)
|
||||
measure_conv /= measure_conv[base_ind, :]
|
||||
measure_feat /= measure_feat[base_ind, :]
|
||||
# # Relate to smallest scale:
|
||||
# base_ind = np.argmin(scales)
|
||||
# measure_conv /= measure_conv[base_ind, :]
|
||||
|
||||
if plot_results:
|
||||
fig, (ax1, ax2) = plt.subplots(2, 1)
|
||||
ax1.plot(scales, measure_conv)
|
||||
ax1.plot(scales, measure_conv.mean(axis=1), c='k')
|
||||
ax1.plot(scales, np.median(measure_conv, axis=1), c='k', ls='--')
|
||||
ax2.plot(scales, measure_feat)
|
||||
ax2.plot(scales, np.nanmean(measure_feat, axis=1), c='k')
|
||||
ax2.plot(scales, np.nanmedian(measure_feat, axis=1), c='k', ls='--')
|
||||
plt.show()
|
||||
|
||||
# Condense measures across kernels:
|
||||
spread_conv = np.zeros((2, scales.size))
|
||||
spread_conv[0] = np.percentile(measure_conv, 25, axis=1)
|
||||
spread_conv[1] = np.percentile(measure_conv, 75, axis=1)
|
||||
measure_conv = np.median(measure_conv, axis=1)
|
||||
spread_conv[0] = np.nanpercentile(measure_conv, 25, axis=1)
|
||||
spread_conv[1] = np.nanpercentile(measure_conv, 75, axis=1)
|
||||
measure_conv = np.nanmedian(measure_conv, axis=1)
|
||||
spread_feat = np.zeros((2, scales.size))
|
||||
spread_feat[0] = np.percentile(measure_feat, 25, axis=1)
|
||||
spread_feat[1] = np.percentile(measure_feat, 75, axis=1)
|
||||
measure_feat = np.median(measure_feat, axis=1)
|
||||
spread_feat[0] = np.nanpercentile(measure_feat, 25, axis=1)
|
||||
spread_feat[1] = np.nanpercentile(measure_feat, 75, axis=1)
|
||||
measure_feat = np.nanmedian(measure_feat, axis=1)
|
||||
|
||||
# Save analysis results:
|
||||
if save_path is not None:
|
||||
@@ -101,10 +121,11 @@ for data_path, name in zip(data_paths, crop_paths(data_paths)):
|
||||
spread_conv=spread_conv,
|
||||
measure_feat=measure_feat,
|
||||
spread_feat=spread_feat,
|
||||
thresh=threshold,
|
||||
thresh_perc=thresh_percent,
|
||||
)
|
||||
file_name = save_path + name
|
||||
if add_noise:
|
||||
file_name += '_noise'
|
||||
save_data(file_name, data, config, overwrite=True)
|
||||
save_name += '_noise'
|
||||
save_data(save_name, data, config, overwrite=True)
|
||||
print('Done.')
|
||||
embed()
|
||||
|
||||
Reference in New Issue
Block a user