-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpikeResp.m
428 lines (367 loc) · 15.3 KB
/
SpikeResp.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
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
classdef SpikeResp < handle
properties
cats, trials, binSize
spikeTimes, trains, PSTH
stim
isi, isiHist, isiBins
spec, specFreq
rate, rateMean, rateStd
BARSrate, BARSconfint
PSTHrate, tauOpt, ISE
xc, lags
Crr, Csr, Tsr, F, Pss, Psr, Prr, Pr1r2;
yLab=''
catNames=[]
end
methods (Access='public')
function self = SpikeResp(spikeTimes, binSize)
% SpikeResp(spikeTimes, binSize)
if nargin==1
binSize = 0.2;%ms
end
self.binSize = binSize;
self.spikeTimes = spikeTimes;
self.cats = size(self.spikeTimes,1);
self.trials = size(self.spikeTimes,2);
self.catNames = num2cellstr(1:self.cats);
self.getRawTrains();
end
function getRawTrains(self)
[self.PSTH, trainStdDummy, self.trains] = times2trains(self.spikeTimes, self.binSize);
end
function setStim(self, stim, stimBinSize)
% add stim for each cat, performs resampling and error checking
% setStim(stim, stimBinSize)
if size(stim,1)~=self.cats
fprintf('\n STIM has the wrong dimensions');
else
if stimBinSize~=self.binSize
if length(stim)<10000
stim = resample(stim',100*self.binSize,100*stimBinSize)';
else
stim = resample(stim',self.binSize/self.binSize,stimBinSize/self.binSize)';
end
end
self.stim = stim;
end
end
function srSub = getSubSetResp(self, subPart)
%returns a SpikeResp with subPart spikeTimes deleted (set to 0)
spikeTimesSub = self.spikeTimes;
for cat = 1:self.cats
for trial = 1:self.trials
spikeTimesSub(cat, trial, rand(size(spikeTimesSub,3),1)>subPart)=0;
end
end
srSub = SpikeResp(spikeTimesSub, self.binSize);
end
function srShift = getShiftResp(self, shift)
%returns a SpikeResp with spikeTimes shifted by shift
shiftSpikeTimes = self.spikeTimes;
shiftSpikeTimes(shiftSpikeTimes>0) = shiftSpikeTimes(shiftSpikeTimes>0) + shift;
srShift = SpikeResp(shiftSpikeTimes,self.binSize);
end
function srRand = getRandResp(self)
self.getRawTrains();
resp = self.trains;
for cat = 1:self.cats
for trial = 1:self.trials
tmp = find(resp(cat,trial,randperm(length(resp)))>0);
randSpikeTimes(cat,trial,1:length(tmp)) = tmp;
end
end
srRand = SpikeResp(randSpikeTimes,self.binSize);
end
function srTrim = getTrimResp(self, start, stop, period )
%returns a SpikeResp with spikeTimes trimmed
trimSpikeTimes = self.spikeTimes;
trimSpikeTimes = trimTimes(trimSpikeTimes,start,stop, period);
srTrim = SpikeResp(trimSpikeTimes,self.binSize);
end
function getSpec(self)
% PREPARE
Fs = 1000/self.binSize;
fftLen = ceil(size(self.trains,2)/1000)*1000;%ensure that spec is evaluated at integer frequency values
% SPECTRAL ESTIMATION
%self.spec = zeros(self.trials,self.cats,fftLen);
for cat = 1:self.cats
% estimate response amp by welch's method
% [psd freq] = getSpecWin(squeeze(rate(cat,:,:)),400,.5,Fs);
% freqIdxAmp = find(ismember(freq, harmFreq))
% harmAmp((cat-1)*trials + (1:trials),:) = sqrt(psd(:,freqIdxAmp));
% estimate full response (amp+phase) using the periodogramm method
[spec freq] = getSpec(squeeze(self.trains(cat,:,:)),Fs,fftLen);
self.spec(cat,:,:) = spec;
end
self.specFreq = freq;
end
function getCoherence(self)
self.getRawTrains();
idx = 1:min(size(self.stim,2), size(self.trains,3));
y = self.trains(:,:,idx);
x = self.stim(:,idx);
self.Pss = zeros(self.cats,1, 513);
self.Prr = zeros(self.cats, self.trials, 513);
self.Pr1r2 = zeros(self.cats, .5*(self.trials^2 - self.trials), 513);
cnt = 0;
for cat = 1:self.cats
[self.Pss(cat,1,:), self.F] = pwelch(x(cat,:),[],512,1024,1000./self.binSize);
for trial = 1:self.trials
self.Psr(cat,trial,:) = cpsd(x(cat,:),y(cat,trial,:),[],512,1024,1000./self.binSize);
self.Prr(cat,trial,:) = pwelch(y(cat,trial,:),[],512,1024,1000./self.binSize);
for trial2 = trial+1:self.trials
cnt = cnt + 1;
[self.Pr1r2(cat,cnt,:), self.F] = cpsd(y(cat,trial2,:),y(cat,trial,:),[],512,1024,1000./self.binSize);
end
end
end
self.Csr = mean(self.Psr,2).*conj(mean(self.Psr,2))./(self.Pss.*mean(self.Prr,2));
self.Crr = mean(self.Pr1r2,2).*conj(mean(self.Pr1r2,2))./(mean(self.Prr,2).*mean(self.Prr,2));
self.Tsr = conj(mean(self.Psr,2))./self.Pss;
end
function getISI(self)
for cat = 1:self.cats
isiTmp = [];
for trial = 1:self.trials
st = self.spikeTimes(cat,trial,self.spikeTimes(cat,trial,:)>0);
isiTmp = [isiTmp(:); diff(st(:))];
end
self.isi(cat,1:length(isiTmp)) = isiTmp;
[isiHistTmp, bins] = hist(isiTmp, 0:1:150);
self.isiHist(cat,:) = isiHistTmp;
self.isiBins = bins;
end
end
function getISIcorr(self)
% determine serial correlations between ISI's following Farkhooi
% et al. 2009
self.getISI();
thisISI = [];
for cat = 1:self.cats%concatenate all ISI
thisISI = [thisISI self.isi(cat,self.isi(cat,:)>0)];
end
matISI = [thisISI(3:end-1);thisISI(2:end-2);thisISI(1:end-3)]';%copy for correlation
[rho,p] = corr(matISI,'type','Spearman');
ISIrho = rho([2,3]) %take only rho for 1vs2 and 1vs3
ISIrhop = p([2,3]) %and their p-values
end
function [xc,lags] = getCORR(self,maxLag)
%[xc,lags] = getCORR(self,maxLag)
xc = zeros(self.cats,self.trials,maxLag);
for cat = 1:self.cats
for trial = 1:self.trials
a=xcov(squeeze(self.trains(cat,trial,:)),maxLag,'coeff');
xc(cat,trial,:) = a(maxLag+2:end);
end
end
lags = (1:maxLag)*self.binSize;
self.lags = lags;
self.xc = xc;
end
function getRate(self,varargin)
stimLen = max(self.spikeTimes(:));
stimStart = 0;
if nargin>1
stimLen = varargin{1};
end
if nargin>2
stimStart = varargin{2};
end
self.rate = zeros(self.cats,self.trials);
for cat = 1:self.cats
for trial = 1:self.trials
self.rate(cat,trial) = sum(self.spikeTimes(cat,trial,:)>stimStart & self.spikeTimes(cat,trial,:)<stimStart+stimLen);
end
end
self.rate = self.rate./stimLen*1000;
self.rateMean = mean(self.rate,2);
self.rateStd = std(self.rate,[],2);
end
function getPSTHKernel(self, varargin)
%PARAMS
% tauAll - [OPTIONAL] list of std of the Gaussian filter in ms
if isempty(varargin)
tauAll = round(2.^(1:.5:5)*self.binSize);
else
tauAll = varargin{1};
end
for tau = 1:length(tauAll)
win = gausswin(tauAll(tau)*6,tauAll(tau));
win = win./sum(win);
for cat = 1:self.cats
for trial = 1:self.trials
convTrains(cat,trial,:) = conv(squeeze(self.trains(cat,trial,:)),win,'same');
end
PSTH(cat,:) = squeeze(mean(squeeze(convTrains(cat,:,:))));
ISE(tau,cat,:) = sum(bsxfun(@minus, squeeze(convTrains(cat,:,:)),PSTH(cat,:)).^2,2);
end
end
MISE = mean(ISE,3);
[val,tau] = min(MISE);
self.tauOpt = tauAll(tau);
self.ISE = ISE;
win = gausswin(tauAll(tau)*6,tauAll(tau));
win = win./sum(win);
for cat = 1:self.cats
for trial = 1:self.trials
convTrains(cat,trial,:) = conv(squeeze(self.trains(cat,trial,:)),win,'same');
end
self.PSTHrate(cat,:) = squeeze(mean(squeeze(convTrains(cat,:,:))));
end
end
function filterTrains(self,win,sizeTag)
if nargin==2, sizeTag='same';end
self.getRawTrains();
if length(win)==1
tau = win;
x =-3*tau:self.binSize:3*tau;
win = exp(-(x./tau).^2);%gauss;
win = win./sum(win);
end
self.PSTH = [];
for cat = 1:self.cats
for trial = 1:self.trials
newTrains(cat,trial,:) = conv(squeeze(self.trains(cat,trial,:)),win, sizeTag);
end
self.PSTH(cat,:) = squeeze(mean(newTrains(cat,:,:),2));
end
self.trains = newTrains;
end
function getPSTHBARS(self)
for cat = 1:self.cats
data(:,2) = sum(squeeze(self.trains(cat,:,:)),1);
data(:,1) = (1:length(self.trains))*self.binSize;
for i = 1:20
barsP_mat(data,self.trials);
end
load -ascii samp_mu;
%compute the confidence intervals
for ndx = 1:size(samp_mu,2)
self.BARSconfint(cat,1,ndx) = prctile(samp_mu(:,ndx),1);
self.BARSconfint(cat,2,ndx) = prctile(samp_mu(:,ndx),99);
end
%get the mean estimate
self.BARSrate(cat,:) = mean(samp_mu,1);
end
end
function plotISI(self)
for cat = 1:self.cats
subplot(1,self.cats,cat)
area(self.isiBins,self.isiHist(cat,:));
ylabel(self.catNames);
end
drawnow
end
function h = plotDot(self, varargin)
%plotDot([startTime endTime])
%startTime and endTime are in ms and OPTIONAL
if ~isempty(varargin)
idx = varargin{1}/self.binSize;
else
idx = [min(self.spikeTimes(:)) length(self.PSTH)]/self.binSize;
end
h = [];
hold on;
for cat = 1:self.cats
st = reshape(self.spikeTimes(cat,:,:),self.trials,size(self.spikeTimes,3));
spikeIdx = find(st~=0.0 & st>idx(1) & st<idx(2));
spikes = st(spikeIdx);
[trialIdx j]= ind2sub(size(st),spikeIdx);
y = trialIdx/self.trials+(1.2*cat-1);
htmp = line([spikes';spikes'],[y';y'+1/(self.trials+2)],'Color','k');
h = [h; htmp];
end
hold off;
axis('tight');
set(gca,'YLim',[-.5 1.2*self.cats+.5])
ylabel(self.yLab)
xlabel('time [ms]');
set(gca,'YTick',(1:self.cats)*1.2-.5, 'YTickLabel',self.catNames);
drawnow;
end
function plotPSTH(self,varargin)
%plotPSTH([startTime endTime])
%startTime and endTime are in ms and OPTIONAL
if ~isempty(varargin)
idx = (varargin{1}(1):varargin{1}(2))/self.binSize;
else
idx = (1:length(self.PSTH))/self.binSize;
end
col = [0 0 0];
if length(varargin)>1,col = varargin{2};end
PSTH = self.PSTH(:,idx);
if ~iscell(self.catNames), self.catNames = num2cellstr(self.catNames);end
h = myPlotMult(idx*self.binSize,flipud(PSTH),fliplr(flipud(self.catNames)));
set(h,'Color',col)
end
function plotDotPSTH(self)
self.plotDot()
hold on
plotPSTH = self.PSTH./max(self.PSTH(:))/1.2;
for cat = 1:self.cats
hp(cat) = plot(plotPSTH(cat,:) + cat - 1/(self.trials+2));
end
set(hp,'Color','r','LineWidth',1.5);
end
function [hLine,hError] = plotRate(self,varargin)
X = 1:length(self.rateMean);
if nargin>1, X = varargin{1};end
[hLine,hError] = myErrorBar(X,self.rateMean,self.rateStd);
end
function srFlat = flatten(self)
% concatenates all cats and trials of spikeTimes and stim (if
% nonempty)
len = length(self.stim)*self.binSize;
count = 0;
newTimes = [];
for cat = 1:self.cats
thisTimes = squeeze(self.spikeTimes(cat,:,:));
thisTimes = thisTimes(:);
newTimes = [newTimes(:)' thisTimes(thisTimes>0)' + len*(cat-1)];
end
srFlat = SpikeResp(reshape(newTimes,1,1,[]),self.binSize);
if length(size(self.stim))>2
for dim = 1:size(self.stim,3)
newStim(:,dim) = reshape(self.stim(:,:,dim)',1,[]);
end
srFlat.setStim(newStim,self.binSize);
else
srFlat.setStim(reshape(self.stim',1,[]),self.binSize);
end
end
function [stReshape,catReshape, trialReshape] = reshapeSpikeTimes(self)
%makes spikeTimes 2D, returning cat and trial indices
stReshape = SpikeResp(...
reshape(permute(self.spikeTimes,[2,1,3]), self.cats*self.trials,[]),...
self.binSize);
catReshape = ceil((1:cats*trials)/trials);
trialReshape = repmat(1:trials,1,cats);
end
function export2HDF5(self,fileName)
%make stim 2D for saving
tmpTimes = reshape(permute(self.spikeTimes,[2,1,3]),self.cats*self.trials,[]);
hdf5write(fileName,'/spikeTimes',tmpTimes)
hdf5write(fileName,'/trials',self.trials,'WriteMode', 'append')
hdf5write(fileName,'/cats',self.cats,'WriteMode', 'append')
hdf5write(fileName,'/stim',self.stim,'WriteMode', 'append')
end
function exportTxt(self, fileName)
if strcmp(self.catNames{1},'')
self.catNames = num2cellstr(1:self.cats);
end
if self.cats==1 || self.trials==1
catSpikes = squeeze(self.spikeTimes);
catStim = squeeze(self.stim);
save([fileName '_spikes.txt'], 'catSpikes','-ASCII')
save([fileName '_stim.txt'], 'catStim' ,'-ASCII')
else
for cat = 1:self.cats
catSpikes = squeeze(self.spikeTimes(cat,:,:));
catStim = self.stim(cat,:);
save([fileName '_spikes_' self.catNames{cat} '.txt'], 'catSpikes','-ASCII')
save([fileName '_stim_' self.catNames{cat} '.txt'], 'catStim' ,'-ASCII')
end
end
end
end
end