Syncing to home.

This commit is contained in:
j-hartling
2026-05-08 18:21:47 +02:00
parent 4b4a04ab2a
commit f14de13823
16 changed files with 578 additions and 395 deletions

View File

@@ -1,7 +1,9 @@
import numpy as np
import matplotlib.pyplot as plt
from thunderhopper.modeltools import load_data, save_data
from thunderhopper.filetools import search_files, crop_paths
from thunderhopper.filtertools import find_kern_specs
from thunderhopper.filters import sosfilter
from thunderhopper.model import process_signal
from IPython import embed
@@ -13,31 +15,24 @@ example_file = dict(
)[mode]
search_path = f'../data/field/processed/{mode}/'
data_paths = search_files('*', ext='npz', dir=search_path)
ref_path = '../data/inv/field/ref_measures.npz'
thresh_path = '../data/inv/field/thresholds.npz'
stages = ['raw', 'filt', 'env', 'log', 'inv', 'conv', 'feat']
pre_stages = stages[:-1]
save_path = f'../data/inv/field/{mode}/'
# ANALYSIS SETTINGS:
distances = np.load('../data/field/recording_distances.npy')[::-1]
thresh_rel = 0.5
thresh_rel = np.array([0, 0.5, 1, 1.5, 2, 2.5, 3])
init_scale = 10000
# SUBSET SETTINGS:
kernels = np.array([
[1, 0.002],
[-1, 0.002],
[2, 0.004],
[-2, 0.004],
[3, 0.032],
[-3, 0.032]
])
kernels = None
types = None#np.array([-1])
sigmas = None#np.array([0.001, 0.002, 0.004, 0.008, 0.016, 0.032])
types = None
sigmas = None
# PREPARATION:
if thresh_rel is not None:
# Get threshold values from pure-noise response SD:
thresh_abs = np.load(ref_path)['conv'] * thresh_rel
thresh_data = dict(np.load(thresh_path))
thresh_abs = thresh_rel[:, None] * thresh_data['sds'][None, :]
# EXECUTION:
for data_path, name in zip(data_paths, crop_paths(data_paths)):
@@ -48,9 +43,8 @@ for data_path, name in zip(data_paths, crop_paths(data_paths)):
data, config = load_data(data_path, files='raw')
song, rate = data['raw'], config['rate']
if thresh_rel is not None:
# Set kernel-specific thresholds:
config['feat_thresh'] = thresh_abs
# Sort max to min distance:
song = song[:, ::-1] * init_scale
# Reduce to kernel subset:
if any(var is not None for var in [kernels, types, sigmas]):
@@ -58,7 +52,7 @@ for data_path, name in zip(data_paths, crop_paths(data_paths)):
config['kernels'] = config['kernels'][:, kern_inds]
config['k_specs'] = config['k_specs'][kern_inds, :]
config['k_props'] = [config['k_props'][i] for i in kern_inds]
config['feat_thresh'] = config['feat_thresh'][kern_inds]
thresh_abs = thresh_abs[:, kern_inds]
# Get song segment to be analyzed:
time = np.arange(song.shape[0]) / rate
@@ -66,37 +60,81 @@ for data_path, name in zip(data_paths, crop_paths(data_paths)):
segment = (time >= start) & (time <= end)
# Prepare storage:
measures = {}
shape = (distances.size, config['k_specs'].shape[0], thresh_rel.size)
measures = dict(measure_feat=np.zeros(shape, dtype=float))
if save_detailed:
snippets = {}
shape = (song.shape[0], config['k_specs'].shape[0], distances.size, thresh_rel.size)
snippets = dict(snip_feat=np.zeros(shape, dtype=float))
# Process snippet:
signals, rates = process_signal(config, returns=stages, signal=song, rate=rate)
for stage in stages:
# Sort largest to smallest distance:
signals[stage] = signals[stage][..., ::-1]
# Process snippet (excluding features):
signals, rates = process_signal(config, returns=pre_stages, signal=song, rate=rate)
# Store results:
for stage in stages:
# Log intensity measures:
# Store non-feature results:
for stage in pre_stages:
mkey = f'measure_{stage}'
if stage == 'feat':
measures[mkey] = signals[stage][segment, ...].mean(axis=0)
else:
measures[mkey] = signals[stage][segment, ...].std(axis=0)
if measures[mkey].ndim == 2:
# Make shape (distances, kernels):
# Log intensity measures:
measures[mkey] = signals[stage][segment, ...].std(axis=0)
if stage == 'conv':
# Make shape (distances, kernels) for consistency:
measures[mkey] = np.moveaxis(measures[mkey], 1, 0)
# Log optional snippet data:
if save_detailed:
# Log optional snippet data:
snippets[f'snip_{stage}'] = signals[stage]
conv = signals['conv']
# Execute piecewise per threshold:
for i, thresholds in enumerate(thresh_abs):
# Execute piecewise per distance:
for j in range(conv.shape[-1]):
feat = sosfilter((conv[:, :, j] > thresholds).astype(float),
rate, config['feat_fcut'], 'lp',
padtype='fixed', padlen=config['padlen'])
# Log intensity measure:
measure = feat[segment, ...].mean(axis=0)
measures['measure_feat'][j, :, i] = measure
if save_detailed:
# Log optional snippet data:
snippets['snip_feat'][:, :, j, i] = feat
# # Log intensity measure, ensuring shape (distances, kernels, thresholds):
# measures['measure_feat'][:, :, i] = np.moveaxis(feat[segment, ...].mean(axis=0), 1, 0)
# if save_detailed:
# # Log optional snippet data:
# snippets['snip_feat'][:, :, :, i] = feat
# thresholds = thresholds[None, :, None]
# embed()
# # Finalize processing:
# feat = sosfilter((signals['conv'] > thresholds).astype(float),
# rate, config['feat_fcut'], 'lp',
# padtype='fixed', padlen=config['padlen'])
# if i == thresholds.shape[0] - 1:
# fig, axes = plt.subplots(1, 8, sharex=True, sharey=True, figsize=(16, 9))
# for j, ax in enumerate(axes):
# ax.plot(time, feat[..., j])
# plt.show()
# embed()
# # Log intensity measure, ensuring shape (distances, kernels, thresholds):
# measures['measure_feat'][:, :, i] = np.moveaxis(feat[segment, ...].mean(axis=0), 1, 0)
# if save_detailed:
# # Log optional snippet data:
# snippets['snip_feat'][:, :, :, i] = feat
# Save analysis results:
if save_path is not None:
data = dict(
distances=distances,
thresh_rel=thresh_rel,
thresh_abs=thresh_abs,
)
data.update(measures)
if save_detailed: