-
Notifications
You must be signed in to change notification settings - Fork 4
/
uhd_pipeline.py
704 lines (618 loc) · 28.4 KB
/
uhd_pipeline.py
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
# %%
import os
from pathlib import Path
import numpy as np
import h5py
import scipy.io
import time
import torch
import shutil
from sklearn.decomposition import PCA
from spike_psvae import filter_standardize
from spike_psvae.cluster_uhd import run_full_clustering
from spike_psvae.drifty_deconv_uhd import full_deconv_with_update, get_registered_pos
from spike_psvae import subtract, ibme
from spike_psvae.ibme import register_nonrigid
from spike_psvae.ibme_corr import calc_corr_decent
from spike_psvae.ibme import fast_raster
from spike_psvae.ibme_corr import psolvecorr
from spike_psvae.waveform_utils import get_pitch
from spike_psvae.uhd_split_merge import run_full_merge_split
from spike_psvae.post_processing_uhd import full_post_processing
import spike_psvae.newton_motion_est as newt
from spike_psvae.waveform_utils import get_pitch
from spike_psvae.post_processing_uhd import final_split_merge
# %%
from spikeinterface.preprocessing import highpass_filter, common_reference, zscore, phase_shift
import spikeinterface.core as sc
if __name__ == "__main__":
# %%
""""
Set parameters / directories name here
I recommend to keep default parameters if possible
"""
# %%
raw_data_name = "binfile.bin" #raw rec location
dtype_raw = 'int16' #dtype of raw rec
output_all = "data_set_name" #everything will be saved here
Path(output_all).mkdir(exist_ok=True)
geom_path = 'geom.npy'
geom = np.load(geom_path)
rec_len_sec = 4000 #length of rec in seconds
n_channels = 385 #number of channels (before preprocessing)
sampling_rate = 30000
savefigs = True # To save summary figs at each step
# %%
if savefigs:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import colorcet as ccet
# %%
nogpu = False # default is to use gpu when possible - set this to True to keep everything on cpu. Gpu speeds things up a lot.
trough_offset=42 #Keep these two params for good denoising
spike_length_samples=121
# %%
#preprocessing parameters
preprocessing=True
apply_filter=True
n_channels_before_preprocessing=n_channels
channels_to_remove=[] #Typically the reference channel - IMPORTANT: make sure it is not included in the geometry array
low_frequency=300
high_factor=0.1
order=3
median_subtraction=True #Set to False on Downsampled datasets
adcshift_correction=True #Set to False on Downsampled datasets
t_start_preproc=0
t_end_preproc=None
#Multi processing params
n_job_preprocessing=4
n_sec_chunk_preprocessing=1
# Initial Detection - Localization parameters
detect_localize = True
subh5_name = Path(output_all) / "initial_detect_localize/subtraction.h5" #This is in case detection has already been ran and we input the subtraction h5 file name here
overwrite_detect=True
t_start_detect = 0 #This is to run detection on full data, and then sort only a "good" portion (no artefacts)
t_end_detect = None #These params correspond to recording AFTER preprocessing
nn_denoise=True
# I recommend not saving wfs / residuals for memory issue
save_residual = False
save_subtracted_waveforms = False
save_cleaned_waveforms = False
save_denoised_waveforms = False
save_subtracted_tpca_projs = False
save_cleaned_tpca_projs = True
save_denoised_ptp_vectors = False
thresholds_subtract = [12, 9, 6] #thresholds for subtraction
peak_sign = "neg" #Important
nn_detect=False
denoise_detect=False
save_denoised_tpca_projs = False
neighborhood_kind = "box"
enforce_decrease_kind = "radial"
extract_box_radius = 100
tpca_rank = 8
n_sec_pca = 20
n_sec_chunk_detect = 1
nsync = 0
n_jobs_detect = 4 # set to 0 if multiprocessing doesn't work
n_loc_workers = 4 #recommend setting these to n_cpus/4
localization_kind = "logbarrier"
localize_radius = 100
loc_feature="peak" # change to "peak" to try new loc idea
# Registration parameters
registration=True
sigma_reg=0.1
max_disp=100 # This is not the actual max displacement, we don't use paris of bins with relative disp>max_disp when computing full displacement
max_dt=250
mincorr=0.6
prior_lambda=1
# Clustering parameters
clustering=True
t_start_clustering=0
t_end_clustering=3000 # AVOID areas with artefacts in initial clustering (i.e. strokes etc...)
len_chunks_cluster=300 # 5 min
threshold_ptp_cluster=3
triage_quantile_cluster=100
frame_dedup_cluster=20
log_c=5 #to make clusters isotropic
scales=(1, 1, 50)
#Deconv parameters
deconvolve=True
t_start_deconv=0
t_end_deconv=None
n_sec_temp_update=rec_len_sec #Keep that to the full time - does nto work for now :)
bin_size_um=3
adaptive_bin_size_selection=False
n_jobs_deconv=4
n_jobs_extract_deconv=2
max_upsample=8
refractory_period_frames=10
min_spikes_bin=None
max_spikes_per_unit=250
deconv_threshold=250 #1000
su_chan_vis=1.5
su_temp_on=None
deconv_th_for_temp_computation=1000 #
adaptive_th_for_temp_computation=False
poly_params=[500, 200, 20, 1]
n_sec_train_feats=20
n_sec_chunk_deconv=1
overwrite_deconv=True
augment_low_snr_temps=True
min_spikes_to_augment=50
save_cleaned_tpca_projs=True
save_denoised_tpca_projs=True
second_deconvolve=True
third_deconvolve=True
# %%
preprocessing_dir = Path(output_all) / "preprocessing"
# Don't trust spikeinterface preprocessing :( ...
if preprocessing:
print("Preprocessing...")
preprocessing_dir = Path(output_all) / "preprocessing"
Path(preprocessing_dir).mkdir(exist_ok=True)
if t_end_preproc is None:
t_end_preproc=rec_len_sec
filter_standardize.filter_standardize_rec_mp(preprocessing_dir,raw_data_name, dtype_raw,
rec_len_sec, n_channels = n_channels_before_preprocessing, channels_to_remove=channels_to_remove,
t_start=t_start_preproc, t_end=t_end_preproc,
apply_filter=apply_filter, low_frequency=low_frequency,
high_factor=high_factor, order=order, sampling_frequency=sampling_rate,
median_subtraction=median_subtraction, adcshift_correction=adcshift_correction, n_jobs=-1)
# Update data name and type if preprocesssed
raw_data_name = Path(preprocessing_dir) / "standardized.bin"
dtype_raw = "float32"
else:
raw_data_name = Path(preprocessing_dir) / "standardized.bin"
dtype_raw = "float32"
"""
spike interface preprocessing does not work...
recording = sc.read_binary(
raw_data_name,
sampling_rate,
n_channels_before_preprocessing,
dtype_raw,
time_axis=0,
is_filtered=False,
)
recording = recording._remove_channels(channels_to_remove)
# set geometry
recording.set_dummy_probe_from_locations(
geom, shape_params=dict(radius=10)
)
if t_end_preproc is not None:
recording = recording.frame_slice(start_frame=int(sampling_rate * t_start_preproc), end_frame=int(sampling_rate * t_end_preproc))
if apply_filter:
recording = highpass_filter(recording, freq_min=low_frequency, filter_order=order)
if standardize:
recording = zscore(recording)
if adcshift_correction:
sampShifts = npSampShifts()
recording = phase_shift(recording, inter_sample_shift=sampShifts)
if median_subtraction:
recording = common_reference(recording)
recording.save(folder=preprocessing_dir, n_jobs=n_job_preprocessing, chunk_size=sampling_rate*n_sec_chunk_preprocessing, progressbar=True)
"""
# %%
# Subtraction
t_start_detect-=t_start_preproc
if t_end_detect is None:
t_end_detect=rec_len_sec
t_end_detect-=t_start_preproc
# %%
detect_dir = Path(output_all) / "initial_detect_localize"
Path(detect_dir).mkdir(exist_ok=True)
if detect_localize:
print("Detection...")
sub_h5 = subtract.subtraction_binary(
raw_data_name,
Path(detect_dir),
geom=geom,
overwrite=overwrite_detect,
sampling_rate=sampling_rate,
do_nn_denoise=nn_denoise,
save_residual=save_residual,
save_subtracted_waveforms=save_subtracted_waveforms,
save_cleaned_waveforms=save_cleaned_waveforms,
save_denoised_waveforms=save_denoised_waveforms,
save_subtracted_tpca_projs=save_subtracted_tpca_projs,
save_cleaned_tpca_projs=save_cleaned_tpca_projs,
save_denoised_ptp_vectors=save_denoised_ptp_vectors,
thresholds=thresholds_subtract,
peak_sign=peak_sign,
nn_detect=nn_detect,
denoise_detect=denoise_detect,
save_denoised_tpca_projs=save_denoised_tpca_projs,
neighborhood_kind=neighborhood_kind,
enforce_decrease_kind=enforce_decrease_kind,
extract_box_radius=extract_box_radius,
t_start=t_start_detect,
t_end=t_end_detect,
tpca_rank=tpca_rank,
n_sec_pca=n_sec_pca,
n_sec_chunk=n_sec_chunk_detect,
nsync=nsync,
n_jobs=n_jobs_detect,
loc_workers=n_loc_workers,
device="cpu" if nogpu else None,
localization_kind=localization_kind,
localize_radius=localize_radius,
loc_feature=loc_feature,
)
# %%
else:
sub_h5=subh5_name
# %%
with h5py.File(sub_h5, "r+") as h5:
cleaned_tpca_group = h5["cleaned_tpca"]
tpca_mean = cleaned_tpca_group["tpca_mean"][:]
tpca_components = cleaned_tpca_group["tpca_components"][:]
localization_results = np.array(h5["localizations{}".format(loc_feature)][:])
maxptps = np.array(h5["maxptps"][:])
spike_index = np.array(h5["spike_index"][:])
# %%
# Load tpca
tpca = PCA(tpca_components.shape[0])
tpca.mean_ = tpca_mean
tpca.components_ = tpca_components
# %%
z = localization_results[:, 2]
x = localization_results[:, 0]
# %%
# remove spikes localized at boundaries
x_bound_low = geom[:, 0].min()
x_bound_high = geom[:, 0].max()
z_bound_low = geom[:, 1].min()
z_bound_high = geom[:, 1].max()
idx_remove_too_far = np.flatnonzero(np.logical_and(
np.logical_and(z>z_bound_low-100, z<z_bound_high+100),
np.logical_and(x>x_bound_low-100, x<x_bound_high+100)))
# %%
maxptps = maxptps[idx_remove_too_far]
z = z[idx_remove_too_far]
spike_index = spike_index[idx_remove_too_far]
x = x[idx_remove_too_far]
localization_results = localization_results[idx_remove_too_far]
if registration:
print("Registration...")
device_reg = "cpu"
motion_est, extra = newt.register(
maxptps,
z,
spike_index[:, 0]/sampling_rate,
rigid=True,
win_step_um=50,
win_scale_um=150,
win_margin_um=-75,
raster_kw=dict(gaussian_smoothing_sigma_um=1,post_transform=None,amp_scale_fn=None,avg_in_bin=True),
weights_kw=dict(weights_threshold_low=0.2, weights_threshold_high=0.2, mincorr=0.1, max_dt_s=100),
thomas_kw=dict(lambda_s=0, lambda_t=1),
max_disp_um=100,
device=device_reg,
pbar=False,
)
displacement_rigid = motion_est.displacement
fname_disp = Path(detect_dir) / "displacement_rigid.npy"
np.save(fname_disp, displacement_rigid)
else:
fname_disp = Path(detect_dir) / "displacement_rigid.npy"
displacement_rigid = np.load(fname_disp)
# %%
if savefigs:
if t_end_detect is None:
t_end_detect = t_start_detect + len(displacement_rigid)
vir = cm.get_cmap('jet')
ptp_arr = maxptps.copy()
ptp_arr = np.log(ptp_arr)
ptp_arr -= ptp_arr.min()
ptp_arr /= ptp_arr.max()
color_array = vir(ptp_arr)
fname_detect_fig = Path(detect_dir) / "detection_displacement_raster_plot.png"
plt.figure(figsize = (10, 5))
plt.scatter(spike_index[:, 0]/sampling_rate, z, color = color_array, s = 1, alpha=0.05)
plt.plot(np.arange(t_start_detect, t_end_detect), displacement_rigid[t_start_detect:t_end_detect]-displacement_rigid[t_start_detect], color = 'red')
plt.plot(np.arange(t_start_detect, t_end_detect), displacement_rigid[t_start_detect:t_end_detect]-displacement_rigid[t_start_detect]+100, color = 'red')
plt.plot(np.arange(t_start_detect, t_end_detect), displacement_rigid[t_start_detect:t_end_detect]-displacement_rigid[t_start_detect]+200, color = 'red')
plt.savefig(fname_detect_fig)
plt.close()
# %%
# Clustering
cluster_dir = Path(output_all) / "initial_clustering"
Path(cluster_dir).mkdir(exist_ok=True)
if clustering:
print("Clustering...")
if t_end_clustering is None:
t_end_clustering=rec_len_sec
time_temp_computation = t_end_clustering//2 # TODO: remove this later
t_start_clustering-=t_start_preproc
t_end_clustering-=t_start_preproc
spt, maxptps, x, z, spike_index = run_full_clustering(t_start_clustering, t_end_clustering, cluster_dir, raw_data_name, geom, spike_index,
localization_results, maxptps, displacement_rigid, len_chunks=len_chunks_cluster, threshold_ptp=threshold_ptp_cluster,
fs=sampling_rate, triage_quantile_cluster=triage_quantile_cluster, frame_dedup_cluster=frame_dedup_cluster,
time_temp_comp_merge=time_temp_computation, log_c=log_c, scales=scales, savefigs=savefigs, deconv_resid_th=0.5)
else:
fname_spt_cluster = Path(cluster_dir) / "spt_full_cluster.npy"
fname_x = Path(cluster_dir) / "x_full_cluster.npy"
fname_z = Path(cluster_dir) / "z_full_cluster.npy"
fname_maxptps = Path(cluster_dir) / "maxptps_full_cluster.npy"
fname_spike_index = Path(cluster_dir) / "spike_index_full_cluster.npy"
spt = np.load(fname_spt_cluster)
x = np.load(fname_x)
z = np.load(fname_z)
maxptps = np.load(fname_maxptps)
spike_index = np.load(fname_spike_index)
idx_kept = np.flatnonzero(spt[:, 1]>-1)
x = x[idx_kept]
z = z[idx_kept]
maxptps = maxptps[idx_kept]
spike_index = spike_index[idx_kept]
spt = spt[idx_kept]
if deconvolve:
print("First Deconvolution...")
deconv_dir_all = Path(output_all) / "deconvolution"
Path(deconv_dir_all).mkdir(exist_ok=True)
deconv_dir = Path(deconv_dir_all) / "deconv_results"
Path(deconv_dir).mkdir(exist_ok=True)
extract_deconv_dir = Path(deconv_dir_all) / "deconv_extracted"
Path(extract_deconv_dir).mkdir(exist_ok=True)
if t_end_deconv is None:
t_end_deconv=rec_len_sec
if adaptive_bin_size_selection:
n_units = spt[:, 1].max()+1
spread_x = np.zeros(n_units)
for k in range(n_units):
idx_k = np.flatnonzero(spt[:, 1]==k)
# Next thing: USE STD INSTEAD OF MAD
spread_x[k] = 1.65*np.median(np.abs(x[idx_k]-np.median(x[idx_k])))/0.6745
pitch = get_pitch(np.load(geom_path))
divisors = np.arange(1, pitch+1)
divisors = divisors[6 % divisors==0]
bins_sizes_um=divisors[np.abs(spread_x[:, None] - divisors).argmin(1)].astype('int')
print(bins_sizes_um)
else:
bins_sizes_um=None
# Uncomment To start where we were :)
# fname_spt_cluster = extract_deconv_dir / "spike_train_final_deconv.npy"
# fname_x = extract_deconv_dir / "x_final_deconv.npy"
# fname_z = extract_deconv_dir / "z_final_deconv.npy"
# fname_maxptps = extract_deconv_dir / "maxptps_final_deconv.npy"
# fname_dist_metric = extract_deconv_dir / "dist_metric_final_deconv.npy"
# spt = np.load(fname_spt_cluster)
# x = np.load(fname_x)
# z = np.load(fname_z)
# maxptps = np.load(fname_maxptps)
# dist_metric = np.load(fname_dist_metric)
dist_metric=None
deconv_h5 = full_deconv_with_update(deconv_dir, extract_deconv_dir,
raw_data_name, geom, displacement_rigid,
spt, spike_index, maxptps, x, z, t_start_deconv, t_end_deconv, sub_h5,
n_sec_temp_update=n_sec_temp_update,
bin_size_um=bin_size_um,
bins_sizes_um=bins_sizes_um,
pfs=sampling_rate,
n_jobs=n_jobs_deconv,
n_jobs_extract_deconv=n_jobs_extract_deconv,
trough_offset=trough_offset,
spike_length_samples=spike_length_samples,
max_upsample=max_upsample,
refractory_period_frames=refractory_period_frames,
min_spikes_bin=min_spikes_bin,
max_spikes_per_unit=max_spikes_per_unit,
tpca=tpca,
deconv_threshold=deconv_threshold,
su_chan_vis=su_chan_vis,
deconv_th_for_temp_computation=deconv_th_for_temp_computation,
adaptive_th_for_temp_computation=adaptive_th_for_temp_computation,
poly_params=poly_params,
extract_radius_um=extract_box_radius,
loc_radius=localize_radius,
loc_feature=loc_feature,
n_sec_train_feats=n_sec_train_feats,
n_sec_chunk=n_sec_chunk_deconv,
overwrite=overwrite_deconv,
p_bar=True,
save_chunk_results=False,
dist_metric=dist_metric,
save_cleaned_tpca_projs=save_cleaned_tpca_projs,
save_temps=False)
# if savefigs:
# spt = np.load(Path(extract_deconv_dir) / "spike_train_final_deconv.npy")
# z = np.load(Path(extract_deconv_dir) / "z_final_deconv.npy")
# fname_deconv_fig=Path(extract_deconv_dir) / "full_deconv_raster_plot.png"
# ccolors = ccet.glasbey[:spt[:, 1].max()+1]
# plt.figure(figsize = (10, 5))
# for k in range(spt[:, 1].max()+1):
# idx = spt[:, 1]==k
# plt.scatter(spt[idx, 0]//sampling_rate, z[idx], c = ccolors[k], s = 1, alpha = 0.1)
# plt.savefig(fname_deconv_fig)
# plt.close()
else:
deconv_dir_all = Path(output_all) / "deconvolution"
Path(deconv_dir_all).mkdir(exist_ok=True)
deconv_dir = Path(deconv_dir_all) / "deconv_results"
Path(deconv_dir).mkdir(exist_ok=True)
extract_deconv_dir = Path(deconv_dir_all) / "deconv_extracted"
Path(extract_deconv_dir).mkdir(exist_ok=True)
if second_deconvolve:
deconv_h5 = Path(extract_deconv_dir) / 'deconv_results.h5'
print("Second Deconvolution...")
deconv_dir_all = Path(output_all) / "second_deconv"
Path(deconv_dir_all).mkdir(exist_ok=True)
deconv_dir = Path(deconv_dir_all) / "deconv_results"
Path(deconv_dir).mkdir(exist_ok=True)
extract_deconv_dir = Path(deconv_dir_all) / "deconv_extracted"
Path(extract_deconv_dir).mkdir(exist_ok=True)
split_merge_dir = Path(deconv_dir_all) / "split_merge_input"
Path(split_merge_dir).mkdir(exist_ok=True)
if t_end_deconv is None:
t_end_deconv=rec_len_sec
if adaptive_bin_size_selection:
n_units = spt[:, 1].max()+1
spread_x = np.zeros(n_units)
for k in range(n_units):
idx_k = np.flatnonzero(spt[:, 1]==k)
# Next thing: USE STD INSTEAD OF MAD
spread_x[k] = 1.65*np.median(np.abs(x[idx_k]-np.median(x[idx_k])))/0.6745
pitch = get_pitch(np.load(geom_path))
divisors = np.arange(1, pitch+1)
divisors = divisors[6 % divisors==0]
bins_sizes_um=divisors[np.abs(spread_x[:, None] - divisors).argmin(1)].astype('int')
print(bins_sizes_um)
else:
bins_sizes_um=None
# Uncomment To start where we were :)
# fname_spt_cluster = extract_deconv_dir / "spike_train_final_deconv.npy"
# fname_x = extract_deconv_dir / "x_final_deconv.npy"
# fname_z = extract_deconv_dir / "z_final_deconv.npy"
# fname_maxptps = extract_deconv_dir / "maxptps_final_deconv.npy"
# fname_dist_metric = extract_deconv_dir / "dist_metric_final_deconv.npy"
# spt = np.load(fname_spt_cluster)
# x = np.load(fname_x)
# z = np.load(fname_z)
# maxptps = np.load(fname_maxptps)
# dist_metric = np.load(fname_dist_metric)
dist_metric=None
with h5py.File(deconv_h5, "r+") as h5:
spike_index = np.array(h5["spike_index"][:])
channel_index = np.array(h5["channel_index"][:])
spt = np.array(h5["deconv_spike_train"][:])
maxptps = np.array(h5["maxptps"][:])
channel_index = np.array(h5["channel_index"][:])
localizations = np.array(h5["localizations{}".format(loc_feature)][:])
x = localizations[:, 0]
z = localizations[:, 2]
z_reg = z - displacement_rigid[spt[:, 0]//sampling_rate]
print("Split/Merge...")
labels_split_merge = run_full_merge_split(deconv_h5, spt, spike_index,
channel_index, geom, raw_data_name,
z, z_reg, x)
np.save(split_merge_dir / "labels_input_split_merged.npy", labels_split_merge)
# labels_split_merge = np.load(split_merge_dir / "labels_input_split_merged.npy")
spt[:, 1] = labels_split_merge
print("Second Deconv...")
deconv_h5 = full_deconv_with_update(deconv_dir, extract_deconv_dir,
raw_data_name, geom, displacement_rigid,
spt, spike_index, maxptps, x, z, t_start_deconv, t_end_deconv, deconv_h5,
n_sec_temp_update=n_sec_temp_update,
bin_size_um=bin_size_um,
bins_sizes_um=bins_sizes_um,
pfs=sampling_rate,
n_jobs=n_jobs_deconv,
n_jobs_extract_deconv=n_jobs_extract_deconv,
trough_offset=trough_offset,
spike_length_samples=spike_length_samples,
max_upsample=max_upsample,
refractory_period_frames=refractory_period_frames,
min_spikes_bin=min_spikes_bin,
max_spikes_per_unit=max_spikes_per_unit,
tpca=tpca,
deconv_threshold=deconv_threshold,
su_chan_vis=su_chan_vis,
deconv_th_for_temp_computation=deconv_th_for_temp_computation,
adaptive_th_for_temp_computation=adaptive_th_for_temp_computation,
poly_params=poly_params,
extract_radius_um=extract_box_radius,
loc_radius=localize_radius,
loc_feature=loc_feature,
n_sec_train_feats=n_sec_train_feats,
n_sec_chunk=n_sec_chunk_deconv,
overwrite=overwrite_deconv,
p_bar=True,
save_chunk_results=False,
dist_metric=dist_metric,
save_cleaned_tpca_projs=save_cleaned_tpca_projs,
save_temps=False)
if third_deconvolve:
print("Third Deconvolution...")
deconv_dir_all = Path(output_all) / "third_deconv"
Path(deconv_dir_all).mkdir(exist_ok=True)
deconv_dir = Path(deconv_dir_all) / "deconv_results"
Path(deconv_dir).mkdir(exist_ok=True)
extract_deconv_dir = Path(deconv_dir_all) / "deconv_extracted"
Path(extract_deconv_dir).mkdir(exist_ok=True)
split_merge_dir = Path(deconv_dir_all) / "split_merge_input"
Path(split_merge_dir).mkdir(exist_ok=True)
if t_end_deconv is None:
t_end_deconv=rec_len_sec
if adaptive_bin_size_selection:
n_units = spt[:, 1].max()+1
spread_x = np.zeros(n_units)
for k in range(n_units):
idx_k = np.flatnonzero(spt[:, 1]==k)
# Next thing: USE STD INSTEAD OF MAD
spread_x[k] = 1.65*np.median(np.abs(x[idx_k]-np.median(x[idx_k])))/0.6745
pitch = get_pitch(np.load(geom_path))
divisors = np.arange(1, pitch+1)
divisors = divisors[6 % divisors==0]
bins_sizes_um=divisors[np.abs(spread_x[:, None] - divisors).argmin(1)].astype('int')
print(bins_sizes_um)
else:
bins_sizes_um=None
dist_metric=None
with h5py.File(deconv_h5, "r+") as h5:
spike_index = np.array(h5["spike_index"][:])
channel_index = np.array(h5["channel_index"][:])
spt = np.array(h5["deconv_spike_train"][:])
maxptps = np.array(h5["maxptps"][:])
channel_index = np.array(h5["channel_index"][:])
localizations = np.array(h5["localizations{}".format(loc_feature)][:])
x = localizations[:, 0]
z = localizations[:, 2]
z_reg = z - displacement_rigid[spt[:, 0]//sampling_rate]
print("Split/Merge...")
labels_split_merge = run_full_merge_split(deconv_h5, spt, spike_index,
channel_index, geom, raw_data_name,
z, z_reg, x)
np.save(split_merge_dir / "labels_input_split_merged.npy", labels_split_merge)
spt[:, 1] = labels_split_merge
print("Third Deconv...")
deconv_h5 = full_deconv_with_update(deconv_dir, extract_deconv_dir,
raw_data_name, geom, displacement_rigid,
spt, spike_index, maxptps, x, z, t_start_deconv, t_end_deconv, deconv_h5,
n_sec_temp_update=n_sec_temp_update,
bin_size_um=bin_size_um,
bins_sizes_um=bins_sizes_um,
pfs=sampling_rate,
n_jobs=n_jobs_deconv,
n_jobs_extract_deconv=n_jobs_extract_deconv,
trough_offset=trough_offset,
spike_length_samples=spike_length_samples,
max_upsample=max_upsample,
refractory_period_frames=refractory_period_frames,
min_spikes_bin=min_spikes_bin,
max_spikes_per_unit=max_spikes_per_unit,
tpca=tpca,
deconv_threshold=deconv_threshold,
su_chan_vis=su_chan_vis,
deconv_th_for_temp_computation=deconv_th_for_temp_computation,
adaptive_th_for_temp_computation=adaptive_th_for_temp_computation,
poly_params=poly_params,
extract_radius_um=extract_box_radius,
loc_radius=localize_radius,
loc_feature=loc_feature,
n_sec_train_feats=n_sec_train_feats,
n_sec_chunk=n_sec_chunk_deconv,
overwrite=overwrite_deconv,
p_bar=True,
save_chunk_results=False,
dist_metric=dist_metric,
save_cleaned_tpca_projs=save_cleaned_tpca_projs,
save_temps=False)
with h5py.File(deconv_h5, "r+") as h5:
# print(subject)
# print("-" * len(subject))
for k in h5:
print(" - ", k) #, h5[k].shape
geom = h5["geom"][:]
geom_array = np.array(h5["geom"][:])
spike_index = np.array(h5["spike_index"][:])
channel_index = np.array(h5["channel_index"][:])
spt = np.array(h5["deconv_spike_train"][:])
maxptps = np.array(h5["maxptps"][:])
spike_index = np.array(h5["spike_index"][:])
localizations = np.array(h5["localizationspeak"][:])
dist_metric = np.array(h5["deconv_dist_metrics"][:])
x = localizations[:, 0]
z_abs = localizations[:, 2]
labels_final = final_split_merge(spt, z_abs, x, displacement_rigid, geom, raw_data_name)
np.save(Path(deconv_dir_all) / "labels_final.npy", labels_final)