-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdz_Curate.m
More file actions
700 lines (583 loc) · 25.7 KB
/
dz_Curate.m
File metadata and controls
700 lines (583 loc) · 25.7 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
%%%By Diksha Zutshi--modified for chenaging tetsing order
function dz_Curate(basename,datfil, clufile,thresholds,pipeline)
%%Algorithm flow:
%1 Check for firing rate for non-biological clusters-noise
%2 Check for noise
%3 Check for cluster to be a MUA cluster
%4 Assign as Single Unit
disp('Inside assessNoise');
disp(['Data file: ', datfil]);
%% reading
[acgEvaluationMode, maxHW, minAmp,maxAmp, minSlope, firingThreshold,acgallthreshold, acgalllabel,correlationthreshold]=thresholds{:};
% Check if clufile is empty
if isempty(clufile)
error('clufile cannot be empty');
end
%% Extract the directory path from clufile
[a, ~] = fileparts(clufile);
%%add folders to path
%addpath([b filesep 'HelperFunctions']);%%helper function folder
%% ACTIVE CHANENLS: to only use the active channels
% === Load rez to access channel map ===
param=fullfile(a,'parameters.mat');
load(param, 'parameters');
nChannels = parameters.nChannels;
fs=parameters.samplingRate;
%parameters={totalChannels,fs};
%% Load spike times
tsfil = [a filesep 'spike_times.npy'];
ts = readNPY(tsfil);
ts = double(ts) / fs;
%%reading clu file
clu = readNPY(clufile);
if length(clu) ~= length(ts)
error('clu IDs do not match length of times');
end
posfil= [a filesep 'channel_positions.npy'];
pos = readNPY(posfil); % Nx2 matrix
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialize variables
uclu = unique(clu);
nclu=numel(uclu);
good = true(nclu, 1);
noiseReasons = cell(nclu, 1);
Reasons = cell(nclu, 1); %%%this will hold reasons why a cluster isn't Good.
passReason=[];
%% LOW FIRING RATE:
%%%RECORDING SIZE
% Calculate the recording duration
fileInfo = dir(datfil); % Get file information
fileSize = fileInfo.bytes; % Get file size in bytes
totalSamples1 = fileSize / (2 * nChannels); % Calculate the total number of samples
recordingDuration = totalSamples1 / fs; % Duration in seconds
recordingDurationHours = recordingDuration / 3600; % Duration in hours
%% extracting and fiktering wfs from dat
good1=find(good);%%check for all the rest of the clusters
clustersToCheck=good1;
%%EXTRACTION OF Waveforms
outputsDir = fullfile(a, 'Outputs'); % path to Outputs folder
% Create Outputs folder if it doesn't exist
if ~exist(outputsDir, 'dir')
mkdir(outputsDir);
end
outputFileName = fullfile(outputsDir, basename + ".mat");
outputfile = fullfile(outputsDir, basename + "_filtered.mat");
if ~exist(outputfile,'file')
[wf] = dz_getWaveform(datfil, clu, ts, clustersToCheck,parameters);
save(outputFileName, 'wf');
dz_filterWaveform(outputFileName, outputfile,fs);
wfdata=load(outputfile);
else
wfdata=load(outputfile);
end
%extracting the waveforms::: in uV
clippedWaveforms = wfdata.clippedWaveforms;
wfs=length(clippedWaveforms);
channelCorrelations = wfdata.channelCorrelations;
corrCell=cell(nclu, 1);
%% Extracting 3 point waveform and waveform features
[amplitudes,halfwidths,slopes,spikeType,wfs,good,noiseReasons,Reasons,chRangeAmp]=dz_extractWfVariables(wfdata,uclu,fs,good,noiseReasons,Reasons,pos);
%% Extracting ACG proportions
acgfile = fullfile(outputsDir, basename + "acg.mat");
[Proportions,isEmpty]=dz_extractAcgVariables(uclu,acgfile,clu,ts);
%% FUNCTION DEFINITIONS:::::::::::::::::::::::>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%% firing rate check
function dz_evaluateLowFiringRates(recordingDuration,firingThreshold)
for ix = 1:nclu
% actualclusterid=20;
% tst = ts(clu == actualclusterid);
if ~good(ix)
continue;
end
i = uclu(ix);
tst = ts(clu == i);
totalSpikes=length(tst);
Averagefiringrate=totalSpikes/recordingDuration;
if(Averagefiringrate<firingThreshold)
%disp("low");
%disp(ix);
noiseReasons{ix} = 'lowRate';
Reasons{ix} = sprintf('Low firing rate: %.3f Hz < %.3f Hz (Threshold)', Averagefiringrate, firingThreshold);
good(ix)=false;
else
good(ix)=true;
end
end
end
%% correlation
function dz_analyzeCorrelation(wfs, channelCorrelations,wfdata,pos)
bestWaveformsChannel=wfdata.bestWaveformsChannel;
for ix=1:wfs
% actualindex=99;
% ix = find(uclu == actualindex);
corrValues = channelCorrelations{ix};
thisBestChannel = bestWaveformsChannel{ix};
if ~good(ix)
continue;
end
if isempty(corrValues)
continue;
end
bestPos = pos(thisBestChannel,:);
d = sqrt(sum((pos - bestPos).^2,2));
[~, idx] = sort(d);
chRange = idx(1:11); % best channel + 10 neighbors
localCorrValues = corrValues(chRange);
localCorrValues=localCorrValues(2:end);
totalChannels = length(localCorrValues);
numHighCorr = sum(localCorrValues > correlationthreshold);
percentHighCorr = (numHighCorr / totalChannels) * 100;
if (percentHighCorr>80)
noiseReasons{ix} = 'Noise';
Reasons{ix} = sprintf('High Correlation (%.2f%%) on all Channels', percentHighCorr);
good(ix)=false;
else
noiseReasons{ix} = 'good';
Reasons{ix} = sprintf('Passed Correlation Check:(%.2f%%) ', percentHighCorr);
good(ix)=true;
end
corrCell{ix}=percentHighCorr;
end
end
%% amplitude
function dz_analyzeAmplitude(wfs,minAmp,maxAmp,amplitudes,chRangeAmp)%max and min
for ix=1:wfs
% actualindex=157;
% ix = find(uclu == actualindex);
thisAmplitude=amplitudes{ix};
if ~good(ix)
continue;
end
if isempty(thisAmplitude) || ~isscalar(thisAmplitude) || isnan(thisAmplitude)
continue;
end
thisChRangeAmp=chRangeAmp{ix};
Diff=thisAmplitude-thisChRangeAmp;
Diff(Diff == 0) = [];
totalChannels = length(thisChRangeAmp);
numHighAmpDiff = sum(Diff > maxAmp);
percentHighAmpDiff = (numHighAmpDiff / totalChannels) * 100;
if (percentHighAmpDiff>70) || thisAmplitude <= minAmp
noiseReasons{ix} = 'Noise';
Reasons{ix} = sprintf('Amplitude is too low or all local channel differences exceed %.3f uV', maxAmp);
good(ix) = false;
else
noiseReasons{ix} = 'good';
Reasons{ix} = sprintf('Passed amplitude check: amplitude = %.3f uV, not all local differences exceed %.3f uV', ...
thisAmplitude, maxAmp);
good(ix) = true;
end
end
end
%% halfwidth
function dz_analyzeHalfWidth(wfs, halfwidths,maxHW)
for ix=1:wfs
thisHalfWidth=halfwidths{ix};
if ~good(ix)
continue;
end
if isempty(thisHalfWidth) || ~isscalar(thisHalfWidth) || isnan(thisHalfWidth)
continue;
end
if thisHalfWidth<=maxHW
noiseReasons{ix} = 'good';
Reasons{ix} = sprintf('Passed the halfwidth check: %.3f Hz < %.3f Hz (Threshold)', thisHalfWidth, maxHW);
good(ix)=true;
else
noiseReasons{ix} = 'Noise';
Reasons{ix} = sprintf('Halfwidth too wide: %.3f Hz > %.3f Hz (Threshold)', thisHalfWidth, maxHW);
good(ix)=false;
end
end
end
%% slope
function dz_analyzeSlope(wfs,minSlope,slopes)
for ix=1:wfs
if ~good(ix)
continue;
end
% actualindex=203;
% ix = find(uclu == actualindex);
thisSlope=slopes{ix};%uV/ms
if isempty(thisSlope) || ~isscalar(thisSlope) || isnan(thisSlope)
continue;
end
if thisSlope>=minSlope
noiseReasons{ix} = 'good';
Reasons{ix} = sprintf('Passed the slope check: %.3f Hz > %.3f Hz (Threshold)', thisSlope, minSlope);
good(ix)=true;
else
noiseReasons{ix} = 'Noise';
Reasons{ix} = sprintf('Slope too low,potential slow waveform: %.3f Hz < %.3f Hz (Threshold)', thisSlope, minSlope);
good(ix)=false;
end
end
end
%% empty acg check
function dz_acgEmpty(nclu,isEmpty,Proportions)
for ix = 1:nclu
if ~good(ix)
continue;
else
% actualindex=9;
% ix = find(uclu == actualindex);
thisEmpty=isEmpty(ix);
thisProportion=Proportions{ix};
if thisEmpty==true
good(ix)=false;
noiseReasons{ix} = 'Noise';
Reasons{ix} = sprintf('Empty ACG: (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
end
end
end
end
%% acgall
function dz_acgAllCheck(nclu,acgallthreshold, acgalllabel,Proportions)
for ix = 1:nclu
% actualindex=53;
% ix = find(uclu == actualindex);
if ~good(ix)
continue;
else
thisProportion=Proportions{ix};
if all(thisProportion>acgallthreshold)
good(ix)=false;
noiseReasons{ix} = acgalllabel;
Reasons{ix} = sprintf('Centerbins proportion greater than 80 percent of the shoulder (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
else
good(ix)=true;
noiseReasons{ix} = 'good';
Reasons{ix} = sprintf('Passed ACGall check: (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
end
end
end
end
%% mua check
function dz_muaCheck(nclu,acgEvaluationMode,Proportions)
for ix = 1:nclu
if ~good(ix)
continue;
else
thisProportion=Proportions{ix};
thisProportion=round((thisProportion*100))/100;
thisProportion=round((thisProportion*100),4)/100;
flag=false;
switch acgEvaluationMode
case 'lenient'
%±2 ms bins must be < 30
if thisProportion(1) > 0.3 || thisProportion(6) > 0.3
flag = true;
disp('MUA: violation at ±2 ms bins (>30%)');
passReason = sprintf('High activity in 2ms bins compared to the shoulders — %.2f%%, %.2f%%', thisProportion(1) * 100, thisProportion(6) * 100);
%Allow 0th and ±1ms bins to be < 30% if outer bins are as low as 30
elseif (thisProportion(1) < 0.3 && thisProportion(6) < 0.3) && all(thisProportion(2:5) < 0.2)
flag = false;
disp('Single Unit');
passReason = sprintf('Passed all the checks — (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
else
flag = true;
%disp('MUA');
passReason = sprintf('High activity in 0th & 1ms bins compared to the shoulders — (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
end
case 'strict'
if any((thisProportion*100)>5)
flag = true;
disp('MUA: violation at ±2 ms bins');
passReason = sprintf('MUA: violation at ±2 ms bins — (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
else
flag=false;
disp('Single Unit');
passReason = sprintf('Passed all the checks — (%.2f%%, %.2f%%, %.2f%%, %.2f%%, %.2f%%)', ...
thisProportion(1) * 100, thisProportion(2) * 100, thisProportion(4) * 100, thisProportion(5) * 100, thisProportion(6) * 100);
end
end
%
if flag==true
good(ix)=false;
noiseReasons{ix} = 'MUA ';
Reasons{ix} = passReason;
continue;
end
%%labelling good if it passed all the noise checks
good(ix)=true;
noiseReasons{ix}='good';
Reasons{ix} = passReason;
end
end
end
%% checking for pipeline order
survivalCounts = zeros(1, length(pipeline) + 1);
survivalLabels = cell(1, length(pipeline) + 1);
goodCounts = zeros(1, length(pipeline) + 1);
% before any filtering
survivalCounts(1) = nclu;
survivalLabels{1} = 'start';
goodCounts(1) = nclu; % before any final labeling, none explicitly marked good yet
radarLabels = pipeline;
for k = 1:length(pipeline)
step = pipeline{k};
switch step
case 'lowFiring'
dz_evaluateLowFiringRates(recordingDuration, firingThreshold);
radarLabels{k} = 'Low Firing';
case 'correlation'
dz_analyzeCorrelation(wfs, channelCorrelations,wfdata,pos);
radarLabels{k} = 'Correlation';
case 'amplitude'
dz_analyzeAmplitude(wfs, minAmp, maxAmp, amplitudes,chRangeAmp);
radarLabels{k} = 'Amplitude';
case 'halfWidth'
dz_analyzeHalfWidth(wfs, halfwidths, maxHW);
radarLabels{k} = 'Half Width';
case 'slope'
dz_analyzeSlope(wfs, minSlope, slopes);
radarLabels{k} = 'Slope';
case 'acgEmpty'
dz_acgEmpty(nclu,isEmpty,Proportions);
radarLabels{k} = 'ACG Empty';
case 'acgAll'
dz_acgAllCheck(nclu, acgallthreshold, acgalllabel, Proportions);
radarLabels{k} = 'ACG All';
case 'mua'
dz_muaCheck(nclu, acgEvaluationMode, Proportions);
radarLabels{k} = 'MUA';
otherwise
error('Unknown pipeline step: %s', step);
end
labels=strings(size(noiseReasons));
for i=1:numel(noiseReasons)
x=noiseReasons{i};
if isempty(x)
labels(i)="";
else
labels(i)=string(x);
end
end
labels=strtrim(labels);
isNeuronal = labels == "" | labels == "good" | labels == "MUA";
isGoodOnly = labels == "" |labels == "good";
survivalCounts(k+1) = sum(isNeuronal); % k+1 for next, k is before pipeline starts
goodCounts(k+1) = sum(isGoodOnly);
survivalLabels{k+1} = step;
end
%% PLOTS>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% %% survival curve
survivalPercent = 100 * goodCounts / nclu;
fig = figure;
plot(0:length(pipeline), survivalPercent, '-o', 'LineWidth', 1.5);
xticks(0:length(pipeline));
xticklabels(survivalLabels);
xtickangle(45);
xlabel('Pipeline step');
ylabel('Clusters remaining (%)');
title('SpikeCleaner survival across pipeline');
grid on;
savePath = fullfile(a, 'SpikeCleaner_survival_curve.png');
saveas(fig, savePath);
% saving as csv
survivalTable = table( ...
(0:length(pipeline))', ...
goodCounts(:), ...
survivalPercent(:), ...
'VariableNames', {'StepIndex','ClustersRemaining','PercentRemaining'});
%
% writetable(survivalTable, fullfile(a, 'SpikeCleaner_survival_curve.csv'));
print(fig, fullfile(a, 'SpikeCleaner_survival_curve'), '-dsvg');
%% radar plot for unit survival
removedPerStep = zeros(1, length(pipeline));
for k = 1:length(pipeline)
removedPerStep(k) = survivalCounts(k) - survivalCounts(k+1);
end
removedPercent = 100 * removedPerStep / nclu;
theta = linspace(0, 2*pi, length(removedPercent)+1);
rho = [removedPercent removedPercent(1)];
fig = figure;
polarplot(theta, rho, '-o', 'LineWidth', 1.5);
hold on;
% red marker on first step
text(theta(1), rho(1)+10, 'counterclockwise', ...
'Color', 'r', ...
'FontWeight', 'bold', ...
'HorizontalAlignment', 'center');
thetaticks(rad2deg(theta(1:end-1)));
thetaticklabels(radarLabels);
title('Percentage of Noisy Clusters Removed by each SpikeCleaner step');
%
% savePath = fullfile(a, 'SpikeCleaner_clusterremovalcurve.png');
% saveas(fig, savePath);
print(fig, fullfile(a, 'SpikeCleaner_clusterremovalcurve'), '-dsvg');
%% radar plot for unit survival
removedPerStep = zeros(1, length(pipeline));
for k = 1:length(pipeline)
removedPerStep(k) = goodCounts(k) - goodCounts(k+1);
end
removedPercent = 100 * removedPerStep / nclu;
theta = linspace(0, 2*pi, length(removedPercent)+1);
rho = [removedPercent removedPercent(1)];
fig1 = figure;
polarplot(theta, rho, '-o', 'LineWidth', 1.5);
hold on;
% red marker on first step
text(theta(1), rho(1)+10, 'counterclockwise', ...
'Color', 'r', ...
'FontWeight', 'bold', ...
'HorizontalAlignment', 'center');
thetaticks(rad2deg(theta(1:end-1)));
thetaticklabels(radarLabels);
title('Percentage of Non-Single Unit Clusters Removed by each SpikeCleaner step');
% savePath = fullfile(a, 'SpikeCleaner_clusterremovalcurve.png');
% saveas(fig1, savePath);
print(fig1, fullfile(a, 'SpikeCleaner_clusterremovalcurve_SUs'), '-dsvg');
%% statistics saved
goodPercent = 100 * goodCounts / nclu;
neuronalPercent = 100 * survivalCounts(:) / nclu;
survivalLabels = survivalLabels(:);
survivalCounts = survivalCounts(:);
goodCounts = goodCounts(:);
neuronalPercent = neuronalPercent(:);
goodPercent = goodPercent(:);
StepwiseStats = table( ...
survivalLabels(:), ...
survivalCounts(:), ...
neuronalPercent, ...
goodCounts(:), ...
goodPercent, ...
'VariableNames', {'Step','Good_MUACount','Good_MUAPercent','GoodCount','GoodPercent'});
%% save stepwise stats table as an image
figTable = figure('Position',[100 100 1600 650]);
% main title
annotation(figTable,'textbox', ...
[0 0.82 1 0.08], ...
'String','SpikeCleaner Stepwise Survival Statistics', ...
'EdgeColor','none', ...
'HorizontalAlignment','center', ...
'FontSize',14, ...
'FontWeight','bold');
% explanatory subtitle
annotation(figTable,'textbox', ...
[0 0.8 1 0.07], ...
'String','All clusters begin as candidate single-units (SU). Pipeline progressively removes non-neuronal clusters. Neuronal = Good + MUA, SU = Good', ...
'EdgeColor','none', ...
'HorizontalAlignment','center', ...
'FontSize',10);
t = uitable(figTable, ...
'Data', table2cell(StepwiseStats), ...
'ColumnName', StepwiseStats.Properties.VariableNames, ...
'RowName', [], ...
'Units','Normalized', ...
'Position',[0.03 0.08 0.94 0.72], ...
'FontSize',12);
t.ColumnWidth = {160 160 160 160 160};
%
% saveas(figTable, fullfile(a, 'SpikeCleaner_stepwise_stats_table.png'));
% writetable(StepwiseStats, ...
% fullfile(a, 'SpikeCleaner_stepwise_stats_table.csv'));
% saveas(figTable, fullfile(a, 'SpikeCleaner_stepwise_stats_table.fig'));
print(figTable, fullfile(a, 'SpikeCleaner_stepwise_stats_table'), '-dsvg');
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% WRITING THE OUTPUT IN THE SAME FOLDER AS OF DATA TO BE READ BY PHY
outputFile = fullfile(a, 'cluster_SpikeCleaner.tsv'); %%inside the animal file
fid = fopen(fullfile(a,'cluster_SpikeCleaner.tsv'),'wt');
fprintf(fid,'cluster_id\tSpikeCleaner\r\n'); % header
for ix = 1:numel(uclu)
cluster_index = uclu(ix);
s = noiseReasons{ix}; % a single string/char
s = regexprep(s,'\r?\n',' '); % remove any embedded newlines
fprintf(fid,'%d\t%s\r\n',cluster_index,s); % CRLF so Excel/Windows behave
end
fclose(fid);
%% WRITING THE OUTPUT IN THE SAME FOLDER AS OF DATA TO BE READ BY PHY
outputFile = fullfile(a, 'cluster_Spikereasons.tsv'); %%inside the animal file
fid = fopen(fullfile(a,'cluster_Spikereasons.tsv'),'wt');
fprintf(fid,'cluster_id\tReasons\r\n');
for ix = 1:numel(uclu)
cluster_index = uclu(ix);
s = Reasons{ix};
if isempty(s), s = ' '; end
s = regexprep(s,'\r?\n',' ');
fprintf(fid,'%d\t%s\r\n',cluster_index,s); % two columns, new row each time
end
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%WRITING HALFWIDTHS
halfwidthfile=fullfile(a,'halfwidths.tsv' );
% % Open the file for writing halfwidth
fid1 = fopen(halfwidthfile, 'wt');
if fid1 == -1
error('Cannot open file: %s', halfwidthfile);
end
fprintf(fid1, 'cluster_id\tHalf Widths\n');
for ix = 1:length(uclu) % Loop over the actual cluster IDs (since uclu contains the cluster IDs)
cluster_index=uclu(ix);
halfWidth=halfwidths{ix};
if isempty(halfWidth)
halfWidth = ''; % Default to " " if no specific reason provided
end
fprintf(fid1, '%d\t%d\n', cluster_index,halfWidth);
end
fclose(fid1);
disp('Finished processing halfwidth');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%WRITING SLOPES
slopess=fullfile(a,'slopes.tsv' );
%
% % Open the file for writing halfwidth
fid1 = fopen(slopess, 'wt');
if fid1 == -1
error('Cannot open file: %s', slopess);
end
fprintf(fid1, 'cluster_id\t slopes \n');
for ix = 1:length(uclu) % Loop over the actual cluster IDs (since uclu contains the cluster IDs)
cluster_index=uclu(ix);
Slope=slopes{ix};
if isempty(Slope)
Slope = ''; % Default to " " if no specific reason provided
end
fprintf(fid1, '%d\t%d\n', cluster_index,Slope);
end
fclose(fid1);
disp('Finished processing slopes');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%WRITING corr
correlation=fullfile(a,'correlation.tsv' );
%
% % Open the file for writing halfwidth
fid1 = fopen(correlation, 'wt');
if fid1 == -1
error('Cannot open file: %s', correlation);
end
fprintf(fid1, 'cluster_id\t correlation \n');
for ix = 1:length(uclu) % Loop over the actual cluster IDs (since uclu contains the cluster IDs)
cluster_index=uclu(ix);
percentHighCorr=corrCell{ix};
if isempty(percentHighCorr)
percentHighCorr = ''; % Default to " " if no specific reason provided
end
fprintf(fid1, '%d\t%d\n', cluster_index,percentHighCorr);
end
fclose(fid1);
disp('Finished processing corr');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%WRITING amplitude
amplitudess=fullfile(a,'amplitudess.tsv' );
%
% % Open the file for writing halfwidth
fid1 = fopen(amplitudess, 'wt');
if fid1 == -1
error('Cannot open file: %s', amplitudess);
end
fprintf(fid1, 'cluster_id\t amplitude \n');
for ix = 1:length(uclu) % Loop over the actual cluster IDs (since uclu contains the cluster IDs)
cluster_index=uclu(ix);
amplitude=amplitudes{ix};
if isempty(amplitude)
amplitude = ''; % Default to " " if no specific reason provided
end
fprintf(fid1, '%d\t%d\n', cluster_index,amplitude);
end
fclose(fid1);
disp('Finished processing amplitude');
end