This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspikeTrialStats.m
344 lines (285 loc) · 19.9 KB
/
spikeTrialStats.m
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
function [stats] = spikeTrialStats(cfg, SpikeTrials, force)
% SPIKERATESTATSEVENTS calculates and plots spike statistics
%
% use as
% [stats] = spikeTrialStats(cfg, SpikeTrials, force, postfix)
%
% This file is part of EpiCode, see
% http://www.github.com/stephenwhitmarsh/EpiCode for documentation and details.
%
% EpiCode is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% EpiCode is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with EpiCode. If not, see <http://www.gnu.org/licenses/>.
cfg.spike.RPV = ft_getopt(cfg.spike, 'RPV', 0.002);
cfg.spike.postfix = ft_getopt(cfg.spike, 'postfix', '');
fname = fullfile(cfg.datasavedir, [cfg.prefix, 'spikestats', cfg.spike.postfix, '.mat']);
if nargin == 1
if exist(fname, 'file')
fprintf('Reading %s\n', fname);
count = 0;
err_count = 0;
while count == err_count
try
load(fname, 'stats');
catch ME
err_count = err_count + 1;
end
count = count + 1;
end
return;
else
warning('No precomputed data is found, not enough input arguments to compute data');
stats = {};
return
end
end
if exist(fname, 'file') && force == false
load(fname, 'stats');
return
end
hyplabels = ["PHASE_1", "PHASE_2", "PHASE_3", "REM", "AWAKE"];
for ipart = 1 : size(SpikeTrials, 2)
for markername = string(cfg.spike.name)
if isempty(SpikeTrials{ipart}.(markername)); continue; end
if isfield(SpikeTrials{ipart}.(markername).trialinfo, 'hyplabel')
SpikeTrials{ipart}.(markername).trialinfo.hyplabel(SpikeTrials{ipart}.(markername).trialinfo.hyplabel == "NO_SCORE") = "AWAKE";
end
% ISIs independent of hyplabels
cfgtemp = [];
cfgtemp.outputunit = 'spikecount';
cfgtemp.bins = cfg.spike.ISIbins;%0 : 0.0005 : 0.200; % use bins of 0.5 milliseconds
cfgtemp.param = 'coeffvar'; % compute the coefficient of variation (sd/mn of isi)
isi_temp_all = ft_spike_isi(cfgtemp, SpikeTrials{ipart}.(markername));
% % Xcorr over conditions
% cfgtemp = [];
% cfgtemp.binsize = 0.001; % cfg.spike.ISIbins; % use bins of 0.5 milliseconds
% cfgtemp.maxlag = 0.200;
% cfgtemp.outputunit = 'proportion';
% cfgtemp.channelcmb = [SpikeTrials{ipart}.(markername).label', SpikeTrials{ipart}.(markername).label'];
% xcorr_temp = ft_spike_xcorr(cfgtemp, SpikeTrials{ipart}.(markername));
% ISIs dependent of hyplabels
if any(contains(SpikeTrials{ipart}.(markername).trialinfo.hyplabel, hyplabels))
for hyplabel = hyplabels
trials = SpikeTrials{ipart}.(markername).trialinfo.hyplabel == hyplabel;
if ~any(trials)
continue
end
% ISI per condition
cfgtemp = [];
cfgtemp.outputunit = 'proportion';
cfgtemp.bins = 0 : 0.0005 : 0.200; % cfg.spike.ISIbins; % use bins of 0.5 milliseconds
cfgtemp.param = 'coeffvar'; % compute the coefficient of variation (sd/mn of isi)
cfgtemp.trials = trials;
isi_temp_label.(hyplabel) = ft_spike_isi(cfgtemp, SpikeTrials{ipart}.(markername));
% Xcorr per condition
% cfgtemp = [];
% cfgtemp.binsize = 0.001; % cfg.spike.ISIbins; % use bins of 0.5 milliseconds
% cfgtemp.maxlag = 0.200;
% cfgtemp.trials = trials;
% cfgtemp.channelcmb = [SpikeTrials{ipart}.(markername).label', SpikeTrials{ipart}.(markername).label'];
% xcorr_hyp_temp.(hyplabel) = ft_spike_xcorr(cfgtemp, SpikeTrials{ipart}.(markername));
end
end
for itemp = 1 : size(SpikeTrials{ipart}.(markername).label, 2)
stats{ipart}.(markername){itemp}.isi = isi_temp_all.isi{itemp};
stats{ipart}.(markername){itemp}.isi_avg = isi_temp_all.avg(itemp, :);
stats{ipart}.(markername){itemp}.isi_avg_time = isi_temp_all.time;
stats{ipart}.(markername){itemp}.label = isi_temp_all.label{itemp};
% percentage refractory period violations
refr = sum( stats{ipart}.(markername){itemp}.isi < cfg.spike.RPV);
tot = length((stats{ipart}.(markername){itemp}.isi));
stats{ipart}.(markername){itemp}.RPV = refr/tot;
% spike autocorr
% cfgtemp = [];
% cfgtemp.spikechannel = SpikeTrials{ipart}.(markername).label{itemp};
% spike_temp = ft_spike_select(cfgtemp,SpikeTrials{ipart}.(markername));
% stats{ipart}.(markername){itemp}.autocorr = ft_spike_xcorr(cfgtemp, spike_temp);
% stats{ipart}.(markername){itemp}.autocorr = rmfield(stats{ipart}.(markername){itemp}.autocorr, 'cfg');
% stats{ipart}.(markername){itemp}.xcorr = xcorr_temp.xcorr;
% stats{ipart}.(markername){itemp}.xcorr_time = xcorr_temp.time;
if any(contains(SpikeTrials{ipart}.(markername).trialinfo.hyplabel, hyplabels))
for hyplabel = hyplabels
if ~isfield(isi_temp_label, hyplabel)
continue
end
stats{ipart}.(markername){itemp}.(hyplabel).isi = isi_temp_label.(hyplabel).isi{itemp};
stats{ipart}.(markername){itemp}.(hyplabel).isi_avg = isi_temp_label.(hyplabel).avg(itemp, :);
stats{ipart}.(markername){itemp}.(hyplabel).isi_avg_time = isi_temp_label.(hyplabel).time;
stats{ipart}.(markername){itemp}.(hyplabel).label = isi_temp_label.(hyplabel).label{itemp};
% stats{ipart}.(markername){itemp}.(hyplabel).xcorr = xcorr_hyp_temp.(hyplabel).xcorr;
% stats{ipart}.(markername){itemp}.(hyplabel).xcorr_time = xcorr_hyp_temp.(hyplabel).time;
end
end
ft_progress('init','text', sprintf('Starting on "%s", part %d of %d, unit %d of %d', markername, ipart, size(SpikeTrials, 2), itemp, size(SpikeTrials{ipart}.(markername).label, 2)));
% add trialinfo so that trials can later be selected
stats{ipart}.(markername){itemp}.trialinfo = SpikeTrials{ipart}.(markername).trialinfo;
for itrial = 1 : size(SpikeTrials{ipart}.(markername).trialinfo, 1)
ft_progress(itrial / size(SpikeTrials{ipart}.(markername).trialinfo, 1), 'Computing stats for trial %d of %d', itrial, size(SpikeTrials{ipart}.(markername).trialinfo, 1));
% get timings and ISIs per trial
indx = SpikeTrials{ipart}.(markername).trial{itemp} == itrial;
trial_length = SpikeTrials{ipart}.(markername).trialtime(itrial, 2) - SpikeTrials{ipart}.(markername).trialtime(itrial, 1);
t = SpikeTrials{ipart}.(markername).time{itemp}(indx);
isi_all = diff(t);
amps = SpikeTrials{ipart}.(markername).amplitude{itemp}(indx);
% counting bursts as in Colder et al. 1996, & Staba et al. 2002
indx = isi_all < 0.01;
burstindx = zeros(size(indx));
toremove = [];
isi_intraburst = [];
for burstlength = 1 : 10
pattern = [false, true(1, burstlength), false];
bindx = strfind(indx, pattern);
if ~isempty(bindx)
burstindx(bindx+1) = burstlength; % note +1 index because pattern starts with zero
% add to list to correct for bursts
for ii = 1 : size(bindx, 2)
% remove all but first spike (at +1)
toremove = [toremove, bindx(ii) + 2 : bindx(ii) + 2 + burstlength - 1]; % burstlength = 1; 0 1 0 -> 0 1 x 0
% add ISI within bursts
isi_intraburst = [isi_intraburst, isi_all(bindx(ii)+1:bindx(ii)+burstlength-1)];
end
end
stats{ipart}.(markername){itemp}.burstsum(itrial, burstlength) = length(bindx);
end
stats{ipart}.(markername){itemp}.burst_trialsum(itrial) = sum(stats{ipart}.(markername){itemp}.burstsum(itrial, :));
% concatinate ISIs, but only between bursts (not within)
stats{ipart}.(markername){itemp}.t_interburst{itrial} = t(burstindx ~= 0);
stats{ipart}.(markername){itemp}.isi_interburst{itrial} = diff(stats{ipart}.(markername){itemp}.t_interburst{itrial});
% remove subsequenct APs after first AP of a burst
stats{ipart}.(markername){itemp}.t_corrected{itrial} = t;
stats{ipart}.(markername){itemp}.t_corrected{itrial}(toremove) = [];
stats{ipart}.(markername){itemp}.isi_corrected{itrial} = diff(stats{ipart}.(markername){itemp}.t_corrected{itrial});
% basic descriptives
stats{ipart}.(markername){itemp}.trialavg_isi(itrial) = nanmean(isi_all);
stats{ipart}.(markername){itemp}.trialfreq(itrial) = length(t) / trial_length;%1/nanmean(isi_all);
stats{ipart}.(markername){itemp}.trialfreq_corrected(itrial) = 1/nanmean(stats{ipart}.(markername){itemp}.isi_corrected{itrial});
stats{ipart}.(markername){itemp}.spikecount(itrial) = size(t, 2);
stats{ipart}.(markername){itemp}.spikecount_corrected(itrial) = size(stats{ipart}.(markername){itemp}.t_corrected{itrial}, 2);
% regularity metrics according to Ponce-Alvarez, 2010
x = stats{ipart}.(markername){itemp}.isi_corrected{itrial}(1:end-1) ./ stats{ipart}.(markername){itemp}.isi_corrected{itrial}(2:end);
CV2_instant = 2 * abs(x - 1) ./ (x + 1);
stats{ipart}.(markername){itemp}.CV2_trial(itrial) = nanmean(CV2_instant);
x = isi_intraburst(1:end-1) ./ isi_intraburst(2:end);
CV2_intraburst_instant = 2 * abs(x - 1) ./ (x + 1);
stats{ipart}.(markername){itemp}.CV2_intraburst_trial(itrial) = nanmean(CV2_intraburst_instant);
LV_instant = 3 * (x - 1).^2 ./ (x + 1).^2;
stats{ipart}.(markername){itemp}.LV_trial(itrial) = nanmean(LV_instant);
IR_instant = abs(log(x));
stats{ipart}.(markername){itemp}.IR_trial(itrial) = nanmean(IR_instant);
SI_instant = 0.5 * log((x+1).^2/(4*x));
stats{ipart}.(markername){itemp}.SI_trial(itrial) = nanmean(SI_instant);
% calculate CV per trial for averged CV
stats{ipart}.(markername){itemp}.CV_trial(itrial) = nanstd(stats{ipart}.(markername){itemp}.isi_corrected{itrial}) / nanmean(stats{ipart}.(markername){itemp}.isi_corrected{itrial});
% short vs long ISIs for BI
stats{ipart}.(markername){itemp}.short(itrial) = sum(isi_all < 0.010);
stats{ipart}.(markername){itemp}.long(itrial) = sum(isi_all < 0.100);
% average spike amplitude
stats{ipart}.(markername){itemp}.amplitude(itrial) = nanmean(amps);
if isnan(nanmean(amps))
stats{ipart}.(markername){itemp}.burst_trialsum(itrial) = nan;
end
end % itrial
ft_progress('close');
end % itemp
%%%%%%%%%%%
%% SPIKY %%
%%%%%%%%%%%
if ~exist('SPIKY_check_spikes', 'file')
disp('SPIKY is not in your path. Skipping SPIKY calculations');
continue
end
% preallocate spike structure, with shuffled control
clear spikedata
spikedata{size(SpikeTrials{ipart}.(markername).trialinfo, 1)}{size(SpikeTrials{ipart}.(markername).label, 2)} = [];
spikedata_ctrl = spikedata;
for iunit = 1:size(SpikeTrials{ipart}.(markername).label, 2)
shuffled = SpikeTrials{ipart}.(markername).trial{iunit}(randperm(size(SpikeTrials{ipart}.(markername).trial{iunit}, 2)));
for itrial = 1:size(SpikeTrials{ipart}.(markername).trialinfo, 1)
spikedata{itrial}{iunit} = SpikeTrials{ipart}.(markername).time{iunit}(SpikeTrials{ipart}.(markername).trial{iunit} == itrial);
spikedata_ctrl{itrial}{iunit} = SpikeTrials{ipart}.(markername).time{iunit}(shuffled == itrial);
end
end
ft_progress('init','text', sprintf('Compute spike synchrony with Spiky \n%s p%d : %s', cfg.prefix(1:end-1), ipart, markername));
for itrial = 1 : length(spikedata)
ft_progress(0, 'processing trial %d from %d', itrial, length(spikedata));
try
% For trial data
if length(spikedata{itrial}) < 2
%need at least 2 spike trains to compute synchrony
stats{ipart}.(markername){itemp}.dist(:, itrial) = nan;
stats{ipart}.(markername){itemp}.dist_label = nan;
stats{ipart}.(markername){itemp}.dist_perm(:, itrial) = nan;
stats{ipart}.(markername){itemp}.dist_spikenr(:, itrial) = nan;
stats{ipart}.(markername){itemp}.dist_isi(:, itrial) = nan;
stats{ipart}.(markername){itemp}.dist_pooled(:, itrial) = nan;
stats{ipart}.(markername){itemp}.dist_psth(:, itrial) = nan;
continue
end
spikes = spikedata{itrial};
ori_spikes = spikes; %used to create control spikes data
para.tmin = SpikeTrials{ipart}.(markername).trialtime(itrial,1);
para.tmax = SpikeTrials{ipart}.(markername).trialtime(itrial,2);
para.dts = 1/SpikeTrials{ipart}.(markername).hdr.Fs;
para.select_measures = [0 1 0 0 0 0 0 0]; % {'ISI';'SPIKE';'RI_SPIKE';'SPIKE_realtime';'SPIKE_forward';'SPIKE_synchro';'SPIKE_order';'PSTH'};
para.num_trains = length(spikes);
d_para = para;
SPIKY_check_spikes
para = d_para;
spiky_ori = SPIKY_loop_f_distances(spikes, para);
% for control data with permuted trials
% use same parameters
spikes = spikedata_ctrl{itrial};
d_para = para;
SPIKY_check_spikes
para = d_para;
ctrl_trials = SPIKY_loop_f_distances(spikes, para);
% for control data with random distribution, same spike numbers
para.choice = 1;
spikes = SPIKY_f_spike_train_surrogates(ori_spikes, para);
ctrl_spikenr = SPIKY_loop_f_distances(spikes, para);
% for control data with same ISI distribution
para.choice = 2;
spikes = SPIKY_f_spike_train_surrogates(ori_spikes, para);
ctrl_isi = SPIKY_loop_f_distances(spikes, para);
% for control data with same pool of spikes
para.choice = 3;
spikes = SPIKY_f_spike_train_surrogates(ori_spikes, para);
ctrl_pooled = SPIKY_loop_f_distances(spikes, para);
% for control data with same psth
para.choice = 4;
spikes = SPIKY_f_spike_train_surrogates(ori_spikes, para);
ctrl_psth = SPIKY_loop_f_distances(spikes, para);
catch
warning('Something went wrong calculating Spiky values\n');
end
try
% reshape to unit-by-unit, and retain only spike distance
for itemp = 1 : size(SpikeTrials{ipart}.(markername).label, 2)
sel = 1 : size(SpikeTrials{ipart}.(markername).label, 2);
sel(itemp) = [];
stats{ipart}.(markername){itemp}.dist(:, itrial) = spiky_ori.SPIKE.matrix(itemp, sel);
stats{ipart}.(markername){itemp}.dist_label = SpikeTrials{ipart}.(markername).label(sel);
stats{ipart}.(markername){itemp}.dist_perm(:, itrial) = ctrl_trials.SPIKE.matrix(itemp, sel);
stats{ipart}.(markername){itemp}.dist_spikenr(:, itrial) = ctrl_spikenr.SPIKE.matrix(itemp, sel);
stats{ipart}.(markername){itemp}.dist_isi(:, itrial) = ctrl_isi.SPIKE.matrix(itemp, sel);
stats{ipart}.(markername){itemp}.dist_pooled(:, itrial) = ctrl_pooled.SPIKE.matrix(itemp, sel);
stats{ipart}.(markername){itemp}.dist_psth(:, itrial) = ctrl_psth.SPIKE.matrix(itemp, sel);
end
catch
warning('Something went wrong calculating Spiky values\n');
end
clear spikes ori_spikes
end
ft_progress('close');
end % markername
end % ipart
save(fname, 'stats', '-v7.3');