-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseProcessor5.py
More file actions
1497 lines (1167 loc) · 40.6 KB
/
Copy pathMouseProcessor5.py
File metadata and controls
1497 lines (1167 loc) · 40.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import gc
import json
import tempfile
import shutil
import numpy as np
if not hasattr(np, "in1d"):
np.in1d = np.isin
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from neo.io import NeuralynxIO
from scipy.ndimage import uniform_filter1d
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
import tridesclous as tdc
from tridesclous.signalpreprocessor import (
offline_signal_preprocessor,
estimate_medians_mads_after_preprocesing,
)
TDC_TEMP_ROOT = r"E:\FDA Raw Data\ephys\tdc_temp"
os.makedirs(
TDC_TEMP_ROOT,
exist_ok=True
)
# ==========================================
# TRIDESCLOUS HELPERS
# ==========================================
def get_preprocessor_params(fs, n_channels=1):
"""
Fetch Tridesclous auto preprocessor params for the given sampling rate.
"""
temp_folder = tempfile.mkdtemp(
prefix="tdc_params_",
dir=TDC_TEMP_ROOT
)
try:
dummy = np.zeros((int(fs), n_channels), dtype='float32')
dummy_file = os.path.join(temp_folder, 'dummy.raw')
dummy.tofile(dummy_file)
dataio = tdc.DataIO(dirname=temp_folder)
dataio.set_data_source(
type='RawData',
filenames=[dummy_file],
dtype='float32',
sample_rate=fs,
total_channel=n_channels
)
dataio.add_one_channel_group(channels=list(range(n_channels)), chan_grp=0)
params = tdc.get_auto_params_for_catalogue(dataio, chan_grp=0)
return params['preprocessor']
except Exception as e:
print(f"Warning: could not fetch TDC params, using defaults. ({e})")
return {'highpass_freq': 300., 'lowpass_freq': 5000.,
'smooth_size': 0, 'common_ref_removal': False}
finally:
dataio = None
gc.collect()
try:
shutil.rmtree(
temp_folder
)
except Exception as cleanup_error:
print(
f"Warning: could not remove "
f"temporary parameter folder: "
f"{cleanup_error}"
)
def calculate_noise_metrics_excluding_spikes(
channel_data,
spike_indices,
fs,
preprocessor_params,
snippet_duration_s=60.0,
exclude_start_s=60.0,
spike_mask_before_ms=1.0,
spike_mask_after_ms=2.0,
):
"""
Calculate MAD and RMS noise from a fixed snippet while excluding
samples surrounding detected spikes.
The snippet begins after exclude_start_s. Detected spike windows
are masked before calculating the RMS noise floor.
"""
results = {
'mad_uV': 0.0,
'rms_noise_uV': 1.0,
'mean_peak_signal_uV': 0.0,
'channel_snr': 0.0,
}
try:
total_samples = channel_data.shape[0]
snippet_samples = int(snippet_duration_s * fs)
start = int(exclude_start_s * fs)
stop = min(start + snippet_samples, total_samples)
if stop <= start:
print(" Recording is too short for requested noise snippet.")
return results
snippet = channel_data[start:stop]
data_2d = (
snippet[:, None]
if snippet.ndim == 1
else snippet
)
filtered = offline_signal_preprocessor(
data_2d,
fs,
normalize=False,
**preprocessor_params
)
flat = filtered[:, 0]
_, mad = estimate_medians_mads_after_preprocesing(
data_2d,
fs,
**preprocessor_params
)
mad_uv = float(mad[0])
results['mad_uV'] = mad_uv
noise_mask = np.ones(flat.shape[0], dtype=bool)
before_samples = int(
spike_mask_before_ms / 1000.0 * fs
)
after_samples = int(
spike_mask_after_ms / 1000.0 * fs
)
spike_indices = np.asarray(
spike_indices,
dtype=np.int64
)
spikes_in_snippet = spike_indices[
(spike_indices >= start)
& (spike_indices < stop)
]
relative_spikes = spikes_in_snippet - start
for spike_index in relative_spikes:
mask_start = max(
0,
spike_index - before_samples
)
mask_stop = min(
flat.shape[0],
spike_index + after_samples + 1
)
noise_mask[mask_start:mask_stop] = False
noise_samples = flat[noise_mask]
# Additional robust amplitude mask
noise_samples = noise_samples[
(noise_samples >= -3.0 * mad_uv)
& (noise_samples <= 3.0 * mad_uv)
]
if noise_samples.shape[0] > 0:
results['rms_noise_uV'] = float(
np.sqrt(
np.mean(noise_samples ** 2)
)
)
print(
f" Noise snippet: "
f"{start / fs:.2f}s -> {stop / fs:.2f}s"
)
print(
f" Spikes masked in snippet: "
f"{len(relative_spikes)}"
)
print(
f" Noise samples retained: "
f"{noise_samples.shape[0]:,}"
)
except Exception as e:
print(f" [noise metric error] {e}")
import traceback
traceback.print_exc()
return results
def compute_snr_from_waveforms(waveforms, channel_metrics):
"""
Fill mean_peak_signal_uV and channel_snr into channel_metrics (in-place).
signal = |mean( min(waveform) for each spike )|
SNR = signal / rms_noise_uV
"""
if waveforms is None or len(waveforms) == 0:
channel_metrics['mean_peak_signal_uV'] = 0.0
channel_metrics['channel_snr'] = 0.0
return channel_metrics
peaks = np.array([np.min(wf) for wf in waveforms])
mean_peak = float(abs(np.mean(peaks)))
rms_noise = channel_metrics.get('rms_noise_uV', 1.0)
channel_metrics['mean_peak_signal_uV'] = mean_peak
channel_metrics['channel_snr'] = mean_peak / rms_noise if rms_noise > 0 else 0.0
return channel_metrics
print(f"Using tridesclous version: {tdc.__version__}")
# =========================================================
# HELPER FUNCTIONS (Adapted from noiseFloor.py)
# =========================================================
def calculate_cluster_quality_metrics(waveforms, spike_times, sampling_rate, cluster_label, channel_metrics):
"""Calculate quality metrics for a specific cluster"""
# Extract channel-wide metrics
mad_uV = channel_metrics.get('mad_uV', 0)
rms_noise_uV = channel_metrics.get('rms_noise_uV', 1)
mean_peak_signal_uV = channel_metrics.get('mean_peak_signal_uV', 0)
channel_snr = channel_metrics.get('channel_snr', 0)
base_metrics = {
"cluster_label": int(cluster_label),
"n_spikes": len(spike_times),
"median_peak_to_trough_amplitude": 0,
# Channel Stats (Repeated for every cluster on this channel)
"channel_mad_uV": float(mad_uV),
"channel_noise_floor_rms_uV": float(rms_noise_uV),
"channel_mean_peak_signal_uV": float(mean_peak_signal_uV),
"channel_snr": float(channel_snr),
"snr": float(channel_snr) # Mapped for plotting compatibility
}
if len(waveforms) == 0:
return base_metrics
waveforms_array = np.array(waveforms)
# Median peak-to-trough amplitude
peak_to_trough_amplitudes = []
for wf in waveforms_array:
peak_idx = np.argmin(wf)
peak_amplitude = wf[peak_idx]
if peak_idx < len(wf) - 1:
trough_amplitude = np.max(wf[peak_idx:])
peak_to_trough_amp = trough_amplitude - peak_amplitude
peak_to_trough_amplitudes.append(peak_to_trough_amp)
median_peak_to_trough_amplitude = np.median(peak_to_trough_amplitudes) if peak_to_trough_amplitudes else 0
base_metrics.update({
"median_peak_to_trough_amplitude": float(median_peak_to_trough_amplitude)
})
return base_metrics
def save_cluster_metrics_plot(channel_id, cluster_metrics_list, output_dir):
"""Create and save a 2x2 visualization of cluster quality metrics."""
if not cluster_metrics_list:
return
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
fig.suptitle(f"Channel {channel_id} - Cluster Quality Metrics", fontsize=16, fontweight='bold')
cluster_labels = [m['cluster_label'] for m in cluster_metrics_list]
tick_labels = [str(cl) for cl in cluster_labels]
x_pos = np.arange(len(cluster_labels))
colors = plt.cm.tab10(np.linspace(0, 1, max(len(cluster_labels), 1)))
# Panel 1: Spike counts
axes[0, 0].bar(x_pos, [m['n_spikes'] for m in cluster_metrics_list], color=colors)
axes[0, 0].set_xticks(x_pos)
axes[0, 0].set_xticklabels(tick_labels)
axes[0, 0].set_xlabel("Cluster Label")
axes[0, 0].set_ylabel("Number of Spikes")
axes[0, 0].set_title("Spike Count per Cluster")
axes[0, 0].grid(True, alpha=0.3)
# Panel 2: Channel SNR (global — same value for every cluster bar)
axes[0, 1].bar(x_pos, [m['channel_snr'] for m in cluster_metrics_list], color=colors)
axes[0, 1].set_xticks(x_pos)
axes[0, 1].set_xticklabels(tick_labels)
axes[0, 1].set_xlabel("Cluster Label")
axes[0, 1].set_ylabel("SNR (|Mean Peak| / RMS Noise)")
axes[0, 1].set_title("Channel SNR (Global)")
axes[0, 1].grid(True, alpha=0.3)
# Panel 3: Median peak-to-trough amplitude
axes[1, 0].bar(x_pos, [m['median_peak_to_trough_amplitude'] for m in cluster_metrics_list], color=colors)
axes[1, 0].set_xticks(x_pos)
axes[1, 0].set_xticklabels(tick_labels)
axes[1, 0].set_xlabel("Cluster Label")
axes[1, 0].set_ylabel("Amplitude (µV)")
axes[1, 0].set_title("Median Peak-to-Trough Amplitude")
axes[1, 0].grid(True, alpha=0.3)
# Panel 4: Full text summary
axes[1, 1].axis('off')
m0 = cluster_metrics_list[0]
summary_text = f"Channel {channel_id} Summary\n"
summary_text += "=" * 34 + "\n"
summary_text += f"MAD: {m0['channel_mad_uV']:.3f} µV\n"
summary_text += f"Noise Floor (RMS): {m0['channel_noise_floor_rms_uV']:.3f} µV\n"
summary_text += f"Signal (Mean Peak): {m0['channel_mean_peak_signal_uV']:.3f} µV\n"
summary_text += f"Channel SNR: {m0['channel_snr']:.3f}\n"
summary_text += "=" * 34 + "\n\n"
for m in cluster_metrics_list:
summary_text += f"Cluster {m['cluster_label']}:\n"
summary_text += f" Spikes: {m['n_spikes']}\n"
summary_text += f" Median Pk-to-Tr: {m['median_peak_to_trough_amplitude']:.3f} µV\n\n"
axes[1, 1].text(0.05, 0.97, summary_text, transform=axes[1, 1].transAxes,
verticalalignment='top', fontsize=9, fontfamily='monospace',
bbox=dict(boxstyle="round,pad=0.5", facecolor="wheat", alpha=0.9))
plt.tight_layout()
filename = os.path.join(output_dir, f'channel_{channel_id}_cluster_metrics.png')
plt.savefig(filename, dpi=300, bbox_inches='tight')
print(f"Cluster metrics plot saved: {filename}")
plt.close()
def cluster_channel_with_tridesclous(channel_id, raw_data, sampling_rate, output_dir):
"""
Run Tridesclous clustering on a single channel and save results.
"""
print(f"\n{'='*60}")
print(f"Processing Channel {channel_id}")
print(f"{'='*60}")
temp_folder = tempfile.mkdtemp(
prefix=f"tdc_channel_{channel_id}_",
dir=TDC_TEMP_ROOT
)
try:
# Extract single channel data
channel_data = raw_data[
:,
channel_id:channel_id+1
]
if channel_data.dtype != np.float32:
channel_data = channel_data.astype(
np.float32,
copy=False
)
total_samples = channel_data.shape[0]
# -----------------------------------------------------------
# STEP 1: Run spike detection FIRST so we know where the
# spikes are before picking a snippet for the noise floor.
# -----------------------------------------------------------
raw_filename = os.path.join(temp_folder, 'raw_data.raw')
channel_data.tofile(raw_filename)
# Setup Tridesclous
dataio = tdc.DataIO(dirname=temp_folder)
dataio.set_data_source(type='RawData', filenames=[raw_filename],
dtype='float32', sample_rate=sampling_rate, total_channel=1)
dataio.add_one_channel_group(channels=[0])
cc = tdc.CatalogueConstructor(
dataio=dataio,
chan_grp=0
)
params = tdc.get_auto_params_for_catalogue(
dataio,
chan_grp=0
)
try:
cc.apply_all_steps(
params,
verbose=True
)
except ValueError as e:
if "need at least one array to concatenate" in str(e):
print(
f"⚠️ Tridesclous found no usable waveform "
f"selection for channel {channel_id}."
)
print(
f"Skipping channel {channel_id}: "
"no waveforms available for clustering."
)
return {
"channel_id": channel_id,
"status": "NO_USABLE_WAVEFORMS",
"n_clusters": 0,
"cluster_metrics": [],
"channel_metrics": None
}
raise
cc.make_catalogue_for_peeler()
catalogue = dataio.load_catalogue(chan_grp=0)
peeler = tdc.Peeler(dataio)
peeler.change_params(catalogue=catalogue)
peeler.run(progressbar=True)
spikes = dataio.get_spikes(seg_num=0, chan_grp=0).copy()
# -----------------------------------------------------------
# STEP 2: Calculate Channel Noise Metrics (RMS SNR Method)
# using a fixed 60s snippet after the first 60s.
# Detected spike windows are masked before estimating noise.
# -----------------------------------------------------------
print("Calculating channel noise metrics (RMS method)...")
all_spike_indices = spikes['index']
preprocessor_params = get_preprocessor_params(
sampling_rate,
n_channels=1
)
noise_metrics = calculate_noise_metrics_excluding_spikes(
channel_data,
all_spike_indices,
sampling_rate,
preprocessor_params,
snippet_duration_s=60.0,
exclude_start_s=60.0,
spike_mask_before_ms=1.0,
spike_mask_after_ms=2.0,
)
channel_metrics = noise_metrics
print(
f" Channel MAD: "
f"{channel_metrics['mad_uV']:.2f} µV"
)
print(
f" Noise Floor (RMS): "
f"{channel_metrics['rms_noise_uV']:.2f} µV"
)
print(
" Channel SNR: "
"(pending spike extraction)"
)
# -----------------------------------------------------------
# Get unique clusters (excluding noise cluster -1)
unique_labels = np.unique(spikes['cluster_label'][spikes['cluster_label'] >= 0])
n_clusters = len(unique_labels)
n_template_samples = catalogue['centers0'].shape[1]
pre_samples = n_template_samples // 2
post_samples = n_template_samples - pre_samples
if n_clusters == 0:
print(f"⚠️ No clusters found for channel {channel_id}, but checking for detected spikes...")
# Check for detected spikes
all_spike_indices = spikes['index']
return {
"channel_id": channel_id,
"status": "NO_SPIKES_DETECTED",
"n_clusters": 0,
"cluster_metrics": [],
"channel_metrics": channel_metrics,
}
print(f"Found {n_clusters} clusters on channel {channel_id}")
# Organize data
cluster_data = {}
all_waveforms = []
all_labels = []
for spike in spikes:
peak_idx, label = spike['index'], spike['cluster_label']
start, end = peak_idx - pre_samples, peak_idx + post_samples
if start >= 0 and end <= len(channel_data) and label >= 0:
waveform = channel_data[start:end, 0]
spike_time = peak_idx / sampling_rate
all_waveforms.append(waveform)
all_labels.append(label)
if label not in cluster_data:
cluster_data[label] = {'waveforms': [], 'times': []}
cluster_data[label]['waveforms'].append(waveform)
cluster_data[label]['times'].append(spike_time)
# Compute SNR from all detected waveform peaks (all clusters combined)
compute_snr_from_waveforms(np.array(all_waveforms), channel_metrics)
# Templates and Metrics
cluster_templates = {}
cluster_metrics = []
for label in unique_labels:
waveforms = cluster_data[label]['waveforms']
times = cluster_data[label]['times']
mean_template = np.mean(waveforms, axis=0)
cluster_templates[label] = mean_template
metrics = calculate_cluster_quality_metrics(waveforms, times, sampling_rate, label, channel_metrics)
cluster_metrics.append(metrics)
print(f" Cluster {label}: {len(waveforms)} spikes")
# Save results
np.save(os.path.join(output_dir, f'channel_{channel_id}_cluster_templates.npy'), cluster_templates)
with open(os.path.join(output_dir, f'channel_{channel_id}_cluster_metrics.json'), 'w') as f:
json.dump(cluster_metrics, f, indent=2)
save_cluster_metrics_plot(channel_id, cluster_metrics, output_dir)
# Visualizations (PCA etc)
all_waveforms = np.array(all_waveforms)
all_labels = np.array(all_labels)
print("Performing PCA on waveforms...")
max_pca_waveforms = 5000
if len(all_waveforms) > max_pca_waveforms:
rng = np.random.default_rng(42)
pca_indices = rng.choice(
len(all_waveforms),
size=max_pca_waveforms,
replace=False
)
pca_waveforms = all_waveforms[
pca_indices
]
pca_labels = all_labels[
pca_indices
]
print(
f"Using {max_pca_waveforms:,} of "
f"{len(all_waveforms):,} waveforms "
f"for PCA visualization."
)
else:
pca_waveforms = all_waveforms
pca_labels = all_labels
pca = PCA(
n_components=3
)
principal_components = pca.fit_transform(
pca_waveforms
)
colors = plt.cm.tab10(np.linspace(0, 1, len(unique_labels)))
color_map = {label: colors[i] for i, label in enumerate(unique_labels)}
# PLOT 1: Mean Cluster Waveforms
plt.figure(figsize=(16, 9))
t_axis = np.linspace(-pre_samples / sampling_rate * 1000,
post_samples / sampling_rate * 1000,
n_template_samples)
for label in unique_labels:
mask = (all_labels == label)
wfs_to_plot = all_waveforms[mask]
n_to_plot = min(200, len(wfs_to_plot))
indices_to_plot = np.random.choice(len(wfs_to_plot), n_to_plot, replace=False)
for i in indices_to_plot:
plt.plot(t_axis, wfs_to_plot[i], color=color_map[label], linewidth=0.5, alpha=0.15)
for label in unique_labels:
mean_template = cluster_templates[label]
plt.plot(t_axis, mean_template, color=color_map[label], linewidth=3, zorder=10,
label=f"Cluster {label}")
plt.title(f"Channel {channel_id} — Mean Cluster Waveforms (Ch SNR: {channel_metrics['channel_snr']:.2f})")
plt.xlabel("Time (ms)")
plt.ylabel("Amplitude (µV)")
plt.legend(loc='best')
plt.grid(True, alpha=0.4, linestyle='--')
plt.savefig(os.path.join(output_dir, f"channel_{channel_id}_clusters_mean.png"), dpi=300)
plt.close()
# PLOT 2: PCA 2D
fig, ax = plt.subplots(
figsize=(10, 8)
)
for label in unique_labels:
mask = (
pca_labels == label
)
ax.scatter(
principal_components[mask, 0],
principal_components[mask, 1],
color=color_map[label],
s=15,
alpha=0.7,
label=f"Cluster {label}"
)
ax.set_xlabel("PC1")
ax.set_ylabel("PC2")
ax.set_title(
f"Channel {channel_id} — PCA 2D"
)
ax.legend(loc='best')
ax.grid(
True,
alpha=0.3,
linestyle='--'
)
plt.savefig(
os.path.join(
output_dir,
f"channel_{channel_id}_pca_2d.png"
),
dpi=300
)
plt.close()
# PLOT 3: 3D PCA
fig = plt.figure(
figsize=(12, 10)
)
ax = fig.add_subplot(
111,
projection='3d'
)
for label in unique_labels:
mask = (
pca_labels == label
)
ax.scatter(
principal_components[mask, 0],
principal_components[mask, 1],
principal_components[mask, 2],
color=color_map[label],
s=15,
alpha=0.6,
label=f"Cluster {label}"
)
ax.set_xlabel("PC1")
ax.set_ylabel("PC2")
ax.set_zlabel("PC3")
ax.set_title(
f"Channel {channel_id} — PCA 3D"
)
ax.legend(loc='best')
plt.savefig(
os.path.join(
output_dir,
f"channel_{channel_id}_pca_3d.png"
),
dpi=300
)
plt.close()
# Save spike times
for label in unique_labels:
df = pd.DataFrame({
'channel_id': channel_id,
'cluster_label': label,
'spike_time': cluster_data[label]['times']
})
df.to_csv(os.path.join(output_dir, f'channel_{channel_id}_cluster_{label}_spike_times.csv'), index=False)
return {
'channel_id': channel_id,
'status': "SUCCESS",
'n_clusters': n_clusters,
'cluster_templates': cluster_templates,
'cluster_metrics': cluster_metrics,
'channel_metrics': channel_metrics
}
except Exception as e:
print(f"Error processing channel {channel_id}: {e}")
import traceback
traceback.print_exc()
return {
"channel_id": channel_id,
"status": "ERROR",
"n_clusters": 0,
"cluster_metrics": [],
"channel_metrics": None
}
finally:
try:
peeler = None
except:
pass
try:
catalogue = None
except:
pass
try:
cc = None
except:
pass
try:
dataio = None
except:
pass
try:
spikes = None
except:
pass
try:
channel_data = None
except:
pass
try:
all_waveforms = None
except:
pass
try:
all_labels = None
except:
pass
try:
pca_waveforms = None
except:
pass
try:
pca_labels = None
except:
pass
try:
principal_components = None
except:
pass
try:
cluster_data = None
except:
pass
try:
cluster_templates = None
except:
pass
gc.collect()
try:
shutil.rmtree(
temp_folder
)
except Exception as cleanup_error:
print(
f"Warning: could not remove "
f"temporary channel folder: "
f"{cleanup_error}"
)
def get_excluded_channels(n_channels):
print(f"\n{'='*60}\nCHANNEL EXCLUSION\n{'='*60}")
print(f"Total channels available: 0 to {n_channels-1}")
response = input("\nExclude channels? (y/n): ").lower().strip()
if response not in ['y', 'yes']: return []
print("Enter channel IDs to exclude (comma-separated):")
try:
excluded = [int(ch.strip()) for ch in input("Excluded channels: ").strip().split(',')]
return [ch for ch in excluded if 0 <= ch < n_channels]
except Exception:
print("Invalid input. No channels excluded.")
return []
def generate_car_groups(
raw_data,
sampling_rate,
excluded_channels,
sliding_window=100000,
analysis_downsample=1000,
correlation_threshold=0.9
):
"""
Automatically generate CAR groups from the loaded raw recording.
Parameters
----------
raw_data : np.ndarray
Shape: samples x channels
excluded_channels : list
Channels excluded from CAR and clustering.
sliding_window : int
Moving-average window used on rectified signal.
analysis_downsample : int
Downsampling factor used for CAR grouping.
correlation_threshold : float
Minimum correlation for channels to be grouped.
Returns
-------
car_groups : list[list[int]]
Automatically generated CAR groups.
correlation_matrix : np.ndarray
Correlation matrix for included channels.
"""
print("\n" + "=" * 60)
print("AUTOMATIC PRE-CAR GROUPING")
print("=" * 60)
included_channels = [
ch
for ch in range(raw_data.shape[1])
if ch not in excluded_channels
]
if not included_channels:
raise ValueError("No channels available for CAR grouping.")
print(f"Included channels: {included_channels}")
downsampled_chunks = []
chunk_size = int(30 * sampling_rate)
for start in range(0, raw_data.shape[0], chunk_size):
end = min(start + chunk_size, raw_data.shape[0])
print(
f"Pre-CAR analysis samples "
f"{start:,} -> {end:,}"
)
rectified = np.abs(
raw_data[
start:end,
included_channels
]
).astype(
np.float32,
copy=False
)
moving = np.empty_like(
rectified,
dtype=np.float32
)
for ch in range(rectified.shape[1]):
moving[:, ch] = uniform_filter1d(
rectified[:, ch],
size=sliding_window,
mode="nearest"
)
downsampled_chunks.append(
moving[::analysis_downsample].copy()
)
del rectified
del moving
signal_downsample = np.vstack(
downsampled_chunks
)
del downsampled_chunks
print(
"Pre-CAR analysis array shape:",
signal_downsample.shape
)
signal_sorted = np.sort(
signal_downsample,
axis=0
)[::-1]
n = max(
1,
round(signal_sorted.shape[0] / 100)
)
signal_peaks = signal_sorted[:n]
signal_peaks_mean = np.mean(
signal_peaks,
axis=0
)
signal_peaks_mean_sorted_index = np.argsort(
signal_peaks_mean
)[::-1]
correlation_matrix = np.corrcoef(
signal_downsample,
rowvar=False
)
print("Correlation matrix done.")
high_correlation = (
correlation_matrix > correlation_threshold
)
remaining = list(
signal_peaks_mean_sorted_index
)
local_groups = []
while remaining:
anchor = remaining[0]
current_group = [anchor]