-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapRetinotopyCallaway.m
410 lines (339 loc) · 13.4 KB
/
MapRetinotopyCallaway.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
function [] = MapRetinotopyCallaway(AnimalName,Date)
% MapRetinotopyCallaway.m
%
% Will take data from a retinotopic mapping experiment and extract the
% retinotopy of the LFP recording electrode. The stimulus used here is
% the periodic drifting bar with flashing counter-phase checkerboard from
% the file RetinotopyCallaway.m
% Must have the Matlab Offline Files SDK on the current path
%
%INPUT: AnimalName - unique identifier for the animal as a number, e.g.
% 12345
% Date - date of the experiment input as a number yearMonthDay,
% e.g. 20160525
%OUTPUT: plots
%
% Created: 2016/05/31, 24 Cummington, Boston
% Byron Price
% Updated: 2018/03/21
% By: Byron Price
% read in the .plx file
EphysFileName = strcat('RetinoCallData',num2str(Date),'_',num2str(AnimalName));
if exist(strcat(EphysFileName,'.mat'),'file') ~= 2
readall(strcat(EphysFileName,'.plx'));
end
StimulusFileName = strcat('RetinoCallStim',num2str(Date),'_',num2str(AnimalName),'.mat');
EphysFileName = strcat(EphysFileName,'.mat');
load(EphysFileName,'allad','adfreq','tsevs','svStrobed','SlowPeakV',...
'SlowADResBits','adgains','adfreqs','allts')
load(StimulusFileName)
driftSpeed = stimParams.driftSpeed; % units of degrees per second
% stimFreq = stimParams.stimFreq;
% Width = stimParams.Width;
w_pixels = stimParams.w_pixels;
h_pixels = stimParams.h_pixels;
reps = stimParams.reps;
checkRefresh = stimParams.checkRefresh;
holdTime = stimParams.holdTime;
driftTime = stimParams.driftTime;
centerPos = stimParams.centerPos;
Flashes = stimParams.Flashes;
numDirs = stimParams.numDirs;
DirNames = stimParams.DirNames;
ifi = stimParams.ifi;
mmPerPixel = stimParams.mmPerPixel;
DistToScreen = stimParams.DistToScreen;
theta = stimParams.theta;
phi = stimParams.phi;
stimulationFrequency = 1/checkRefresh;
sampleFreq = adfreq;
Chans = find(~cellfun(@isempty,allad));numChans = length(Chans);
strobeStart = 33;
% notch filter the data
dataLength = length(allad{1,Chans(1)});
ChanData = zeros(dataLength,numChans);
preAmpGain = 1;
for ii=1:numChans
voltage = 1000.*((allad{1,Chans(ii)}).*SlowPeakV)./(0.5*(2^SlowADResBits)*adgains(Chans(ii))*preAmpGain);
% ChanData(:,ii) = voltage;
n = 2;
% lowpass = 100/(sampleFreq/2); % fraction of Nyquist frequency
% blo = fir1(n,lowpass,'low',hamming(n+1));
% temp = filter(blo,1,voltage);
%
notch = 60/(sampleFreq/2);
bw = notch/n;
[b,a] = iirnotch(notch,bw);
ChanData(:,ii) = filtfilt(b,a,voltage);
end
timeStamps = 0:1/sampleFreq:dataLength/sampleFreq-1/sampleFreq;
if length(timeStamps) ~= dataLength
display('Error: Review allad cell array and timing')
return;
end
strobeTimes = tsevs{1,strobeStart};
stimLen = round(driftTime*sampleFreq);
% temp = stimLen;
% stimLen(1) = temp(2);stimLen(2) = temp(1);
ifi = floor(ifi*sampleFreq);
% COLLECT DATA IN THE PRESENCE OF VISUAL STIMULI
Response = cell(numChans,3);
for ii=1:numChans
Response{ii,1} = zeros(2*reps,stimLen(1));
Response{ii,2} = zeros(2*reps,stimLen(2));
greyStrobes = strobeTimes(svStrobed==0);
numGrey = length(greyStrobes);
Response{ii,3} = zeros(2*numGrey,stimLen(1));
count = 1;
for jj=1:numGrey
onsetTime = round(greyStrobes(jj)*sampleFreq)+ifi;
offsetTime = onsetTime+stimLen(1)-1;
Response{ii,3}(count,:) = ChanData(onsetTime:offsetTime,ii);
count = count+1;
onsetTime = offsetTime+1;
offsetTime = onsetTime+stimLen(1)-1;
Response{ii,3}(count,:) = ChanData(onsetTime:offsetTime,ii);
count = count+1;
end
horzCount = 1;
vertCount = 1;
for jj=1:numDirs
strobeNum = jj;
currentStrobeTimes = strobeTimes(svStrobed==strobeNum);
for kk=1:reps
onsetTime = round(currentStrobeTimes(kk)*sampleFreq)+ifi;
if strcmp(DirNames{jj},'Left') == 1 || strcmp(DirNames{jj},'Right') == 1
offsetTime = onsetTime+stimLen(1)-1;
elseif strcmp(DirNames{jj},'Down') == 1 || strcmp(DirNames{jj},'Up') == 1
offsetTime = onsetTime+stimLen(2)-1;
end
if strcmp(DirNames{jj},'Left') == 1 || strcmp(DirNames{jj},'Down') == 1
tempLFP = flipud(ChanData(onsetTime:offsetTime,ii));
else
tempLFP = ChanData(onsetTime:offsetTime,ii);
end
if strcmp(DirNames{jj},'Left') == 1 || strcmp(DirNames{jj},'Right') == 1
Response{ii,1}(horzCount,:) = tempLFP;
horzCount = horzCount+1;
elseif strcmp(DirNames{jj},'Down') == 1 || strcmp(DirNames{jj},'Up') == 1
Response{ii,2}(vertCount,:) = tempLFP;
vertCount = vertCount+1;
end
end
end
end
%time = linspace(0,driftTime,stimLen);
horzPosition = linspace(min(phi),max(phi),stimLen(1)); % azimuth
vertPosition = linspace(min(theta),max(theta),stimLen(2)); % altitude
waveletSize = 100; %
kernelLen = round(waveletSize*checkRefresh*sampleFreq);
if mod(kernelLen,2) == 0
kernelLen = kernelLen+1;
end
x = linspace(-waveletSize/2*checkRefresh,waveletSize/2*checkRefresh,kernelLen);
stdGauss = (waveletSize/2)*checkRefresh/4;
gaussKernel = exp(-(x.*x)./(2*stdGauss*stdGauss));
kernel = exp(-2*pi*x*1i*stimulationFrequency).*gaussKernel;
noiseFreqs = [stimulationFrequency-5,stimulationFrequency-2.5,stimulationFrequency-2,stimulationFrequency-1.5,...
stimulationFrequency+1.5,stimulationFrequency+2,stimulationFrequency+2.5,stimulationFrequency+5];
noiseKernels = zeros(length(noiseFreqs),length(kernel));
for ii=1:length(noiseFreqs)
noiseKernels(ii,:) = exp(-2*pi*x*1i*noiseFreqs(ii)).*gaussKernel;
end
transformBaseline = zeros(numChans,1);
for ii=1:numChans
% numGrey = size(Response{ii,3},1);
% temp = zeros(numGrey,stimLen(1));
% for jj=1:numGrey
% data = Response{ii,3}(jj,:);
% convData = conv(data,kernel,'same');
% convData = sqrt(convData.*conj(convData));
%
% noiseConvData = zeros(size(convData));
% for kk=1:length(noiseFreqs)
% temp = conv(data,noiseKernels(kk,:),'same');
% temp = sqrt(temp.*conj(temp));
% noiseConvData = noiseConvData+temp./length(noiseFreqs);
% end
%
% temp(jj,:) = convData./noiseConvData;
% end
transformBaseline(ii) = 1;
end
DirNames = cell(2,1);DirNames{1} = 'Horizontal Sweep';DirNames{2} = 'Vertical Sweep';
transformResponse = cell(numChans,2);
Results = struct('b',{cell(numChans,2)},'se',{cell(numChans,2)},...
'F',{cell(numChans,2)},'ScreenPos',{cell(numChans,2)},'Center',{cell(numChans,2)},...
'FWHM',{cell(numChans,2)});
downsampleFactor = 5;
dsStimLen = ceil(stimLen/downsampleFactor);
for ii=1:numChans
for jj=1:2
transformResponse{ii,jj} = zeros(2*reps,dsStimLen(jj));
position = zeros(2*reps,dsStimLen(jj));
for kk=1:2*reps
data = Response{ii,jj}(kk,:);
convData = conv(data,kernel,'same');
% convData = convData(1:length(data));
convData = sqrt(convData.*conj(convData));
noiseConvData = zeros(size(convData));
for ll=1:length(noiseFreqs)
temp = conv(data,noiseKernels(ll,:),'same');
temp = sqrt(temp.*conj(temp));
noiseConvData = noiseConvData+temp./length(noiseFreqs);
end
transformResponse{ii,jj}(kk,:) = convData(1:downsampleFactor:end)./...
noiseConvData(1:downsampleFactor:end)-transformBaseline(ii);
if jj==1
position(kk,:) = horzPosition(1:downsampleFactor:end);
elseif jj==2
position(kk,:) = vertPosition(1:downsampleFactor:end);
end
end
y = transformResponse{ii,jj};y=y(:);
effectiveN = (length(data)/(kernelLen/2))*2*reps;
design = [ones(length(y),1),position(:),position(:).*position(:)];
[b,~,stats] = glmfit(design,y,'normal','link','log','constant','off');
%[b,FI] = GetMLest(design,y,b);
%asymptotVar = pinv((1/effectiveN).*FI);
%standardError = sqrt(diag(asymptotVar));
b = real(b);
standardError = real(stats.se);
Results.b{ii,jj} = b;
Results.se{ii,jj} = standardError;
mainDev = GetDeviance(design,y,b);
% F-test
restrictDesign = ones(length(y),1);
[~,restrictDev,~] = glmfit(restrictDesign,y,'normal','constant','off');
F = ((restrictDev-mainDev)/(length(b)-1))/(mainDev/(effectiveN-length(b)-1));
Ftest_p = fcdf(F,length(b)-1,effectiveN-length(b),'upper');
Results.F{ii,jj} = [F,Ftest_p,length(b)-2,effectiveN-length(b)];
Results.ScreenPos{ii,jj} = position(1,:)';
Results.Center{ii,jj} = (-b(2)/(2*b(3)));
Results.FWHM{ii,jj} = 2*sqrt(-log(2)/b(3));
forDisplayDesign = [ones(size(position,2),1),position(1,:)',position(1,:)'.*position(1,:)'];
yhat = exp(forDisplayDesign*b);
temp = position';y = reshape(y,[2*reps,dsStimLen(jj)])';y = y(:);
temp = temp(:);
midPoint = round(length(y)/2);
figure();plot(temp(1:midPoint),y(1:midPoint),'.b');
hold on;
plot(temp(midPoint+1:end),y(midPoint+1:end),'.b');hold on;
plot(position(1,:)',yhat,'c','LineWidth',5)
title(sprintf('Chan: %d - %s',ii,DirNames{jj}));
end
end
figure();
for ii=1:numChans
xPos = Results.ScreenPos{ii,1};
yPos = Results.ScreenPos{ii,2};
bHorz = Results.b{ii,1};
bVert = Results.b{ii,2};
horzDesign = [ones(length(xPos),1),xPos,xPos.^2];
vertDesign = [ones(length(yPos),1),yPos,yPos.^2];
muHorz = exp(horzDesign*bHorz);
muVert = exp(vertDesign*bVert);
muHorz = repmat(muHorz',[dsStimLen(2),1]);
muVert = repmat(muVert,[1,dsStimLen(1)]);
finalIm = muHorz.*muVert;
subplot(numChans,1,ii);
imagesc(linspace(xPos(1),xPos(end),dsStimLen(1)).*180/pi,linspace(yPos(1),yPos(end),dsStimLen(2)).*180/pi,finalIm);
set(gca,'YDir','normal');colormap('jet');
title(sprintf('LFP Retinotopy: Chan %d, Animal %d',ii,AnimalName));
xlabel('Azimuth (degrees)');
ylabel('Altitude (degrees)');
end
fileName = sprintf('RetinoCallResults%d_%d.mat',Date,AnimalName);
save(fileName,'transformResponse','Results','DirNames','w_pixels','h_pixels',...
'stimulationFrequency','waveletSize','kernel','Response','stimLen',...
'dsStimLen','downsampleFactor','centerPos','mmPerPixel','ifi','DistToScreen');
% timebandwidth = 60; % approximate standard deviation in time is
% % 0.5*sqrt(timebandwidth/2)
% for ii=1:numChans
% for jj=1:2
% figure();
% for kk=1:2*reps
% [wt,f] = cwt(Response{ii,jj}(kk,:),sampleFreq,'TimeBandwidth',timebandwidth);
% wt = sqrt(wt.*conj(wt));
% [~,ind] = min(abs(f-stimulationFrequency));
% wt = wt(ind:ind,:);
% wt = mean(wt,1);
% baseline = quantile(wt,0.05);
% subplot(2*reps,1,kk);plot(time,wt-baseline);
% end
% end
% end
% discrete or continuous wavelet transform, hilbert transform
% hilbert(x), dwt, or cwt
% windowLen = floor(checkRefresh*sampleFreq);
% x = 0:windowLen;y = sin(2*pi*x/windowLen);
% z = Response{1,1}(1,:);
% w = conv(z,y);
% plot(w.*w);
end
function [b,FI] = GetMLest(design,y,b)
b = real(b);
maxIter = 3e3;
tolerance = 1e-6;
stepSize = 1e-2;
numParams = length(b);
currentParams = b;
currentLikelihood = GetNormalLikelihood(design,y,currentParams);
iter = 1;
difference = 1;
lineSteps = [stepSize,1e-6,1e-5,1e-4,1e-3,1e-1,0.5e-1,1,10];lineN = length(lineSteps);
lineLikelies = zeros(lineN,1);
while iter < maxIter && difference > tolerance
difference = 0;
for jj=1:numParams
newB = currentParams;newB(jj) = currentParams(jj)+lineSteps(1);
newLikelihood = GetNormalLikelihood(design,y,newB);
gradient = (newLikelihood-currentLikelihood)./lineSteps(1);
lineLikelies(1) = newLikelihood;
for kk=2:lineN
newB = currentParams;newB(jj) = currentParams(jj)+sign(gradient)*lineSteps(kk);
lineLikelies(kk) = GetNormalLikelihood(design,y,newB);
end
[maxLikely,ind] = max(lineLikelies);
if maxLikely > currentLikelihood
difference = difference+maxLikely-currentLikelihood;
currentLikelihood = maxLikely;
currentParams(jj) = currentParams(jj)+sign(gradient)*lineSteps(ind);
else
difference = difference+1;
end
iter = iter+1;
end
end
b = currentParams;
FI = GetFisherInfo(design,y,b);
end
function [loglikelihood] = GetNormalLikelihood(design,y,b)
n = length(y);
% log link in this case
sumsquares = sum((exp(design*b)-y).^2);
residualVar = (1/n)*sumsquares;
loglikelihood = -(n/2)*log(2*pi)-(n/2)*log(residualVar)-(1/(2*residualVar))*sumsquares;
end
function [deviance] = GetDeviance(design,y,b)
deviance = sum((exp(design*b)-y).^2);
end
function [FI] = GetFisherInfo(design,y,b)
stepSize = 1e-3;
numParams = length(b);
set = [1,1;1,-1;-1,1;-1,-1];
signs = [1,-1,-1,1];
FI = zeros(numParams,numParams);
for ii=1:numParams
for jj=1:numParams
temp = 0;
for kk=1:4
shifts = set(kk,:);
newB = b;newB(ii) = b(ii)+shifts(1)*stepSize;
newB(jj) = b(jj)+shifts(2)*stepSize;
temp = temp+signs(kk)*GetNormalLikelihood(design,y,newB);
end
FI(ii,jj) = (1/(4*stepSize*stepSize))*(temp);
end
end
end