-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfOnline.m
218 lines (177 loc) · 6.54 KB
/
rfOnline.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
function [bestCoords, stats] = rfOnline(ops, varargin)
% Compute a receptive field map online for sparse noise stimuli.
% Uses an estimate of the MUA signal (raw data, high pass filtered) for a
% subset of channels.
% Produces a plot of the RF location for these channels and a suggested
% coordinate to use for the center.
flag_computed = 0;
if ~isempty(varargin)
stats = varargin{1};
Protocol = stats.Protocol;
flag_computed = 1;
end
if ~flag_computed
disp('loading geometry');
load(ops.geomFilename);
% determine length of file to use
d = dir(ops.datFilename);
b = d.bytes;
sampsToRead = floor(b/ops.nChans/2);
% load Timeline
DateStr = ops.DateStr;
zserverDir = '\\zserver';
timelineDir = fullfile(zserverDir, 'Data', 'expInfo', ops.mouseName, DateStr, num2str(ops.expNum));
d = dir(fullfile(timelineDir, '*Timeline.mat'));
if ~isempty(d)
disp(['loading timeline file: ' d.name])
load(fullfile(timelineDir, d.name));
else
disp(['could not file timeline file in ' timelineDir]);
end
timelineSync = Timeline.rawDAQData(:,strcmp({Timeline.hw.inputs.name}, 'camSync'));
d = dir(fullfile(timelineDir, '*hardwareInfo.mat'));
if ~isempty(d)
disp(['loading hardwareInfo file: ' d.name])
load(fullfile(timelineDir, d.name)); % gives us "myScreenInfo"
else
disp(['could not file hardwareInfo file in ' timelineDir]);
end
% load Protocol
DateStr(DateStr =='-') = [];
protocolDir = fullfile(zserverDir, 'Data', 'trodes', ops.mouseName, DateStr, num2str(ops.expNum));
d = dir(fullfile(protocolDir, 'Protocol.mat'));
if ~isempty(d)
load(fullfile(protocolDir, 'Protocol.mat'));
else
disp(['could not file protocol file in ' protocolDir]);
end
%
% load data
disp(['starting to load data file: ' ops.datFilename]);
fid = fopen(ops.datFilename);
fsubsamp = 100;
Nsamps = ops.Fs * fsubsamp; % this number MUST be a multiple of fsubsamp
newFs = ops.Fs/fsubsamp;
ichn = chanMap(connected>0);
ichn = sort(ichn);
ichn = reshape(ichn , 10, []);
syncDat = zeros(1, sampsToRead, 'single');
ik = 0;
iks = 0;
while 1
dat = fread(fid, [ops.nChans Nsamps], '*int16');
dat = double(dat);
if ~isempty(dat)
syncDat(iks + (1:size(dat,2))) = dat(ops.syncChannelNum, :);
iks = iks + size(dat,2);
dat = double(permute(mean(reshape(dat(ichn, :), [size(ichn) size(dat,2)]),1), [3 2 1]));
dat(fsubsamp * ceil(size(dat,1)/fsubsamp),:) = 0;
switch ops.response
case 'lfp'
mua0 = computeLFP(dat, ops.Fs, fsubsamp);
case 'mua'
mua0 = datToMUA(dat, ops.Fs, fsubsamp);
case 'thresh'
mua0 = threshCross(dat, ops.Fs, fsubsamp, ops.thresh_sds);
end
if ik==0
mua = zeros(ceil(sampsToRead/fsubsamp), size(mua0,2));
end
mua(ik + (1:size(mua0,1)), :) = mua0;
ik = ik+size(mua0,1);
else
break;
end
clear dat
end
fclose(fid);
mua((1+ik):end, :) = [];
syncDat((1+iks):end) = [];
disp('finished loading data');
%
% synchronize TL and dat
disp('attempting to synchronize dat and TL')
datThresh = -2;
timelineThresh = 2;
datSyncSamps = find(syncDat(1:end-1)<datThresh & syncDat(2:end)>=datThresh);
% datSyncSamps = datSyncSamps(end-1:end); % choose the last two
%
datSyncSamps = datSyncSamps((2*ops.expNum-1) + [0 1]);
datSyncTimes = datSyncSamps/ops.Fs;
timelineSyncSamps = find(timelineSync(1:end-1)<timelineThresh & timelineSync(2:end)>=timelineThresh);
timelineSyncTimes = timelineSyncSamps/Timeline.hw.daqSampleRate;
datDur = diff(datSyncTimes);
tlDur = diff(timelineSyncTimes);
if abs(datDur-tlDur)>0.03
disp('dat and TL do not align');
keyboard
else
disp(' sync successful');
end
datAlignment = regress(datSyncTimes', [timelineSyncTimes ones(size(timelineSyncTimes))]);
% extract frame times from photodiode
disp('computing frame times from photodiode');
pd = Timeline.rawDAQData(:,strcmp({Timeline.hw.inputs.name}, 'photoDiode'));
threshUp = 0.2;
threshDown = 0.1;
pd(end+1) = pd(end);
flipTimes = detectPDiodeUpDown(pd, Timeline.hw.daqSampleRate, threshUp, threshDown);
% recreate stimulus traces
disp('computing stimulus that was shown');
myScreenInfo.windowPtr = NaN;
[allFrames, frameTimes] = computeSparseNoiseFrames(Protocol, flipTimes, myScreenInfo);
frameTimesDat = [frameTimes' ones(size(frameTimes'))]*datAlignment;
frameTimesDat = frameTimesDat';
% all we need to compute RFs is in stats
stats.frameTimesDat = frameTimesDat;
stats.newFs = newFs;
stats.allFrames = allFrames;
stats.mua = mua;
stats.Protocol = Protocol;
end
% compute spike-trig averages + extract magnitude of responses
disp('computing stimulus-triggered MUA signal')
resp = get_sta(stats.mua, stats.allFrames, stats.frameTimesDat, stats.newFs, ops);
% add the mean of all channels as last entry
resp(:,:,end+1) = mean(resp,3);
%
% find peak
disp('finding peaks and plotting');
pk = zeros(size(resp,3), 2);
nrow = ceil(sqrt(size(resp,3)/1.35));
ncol = ceil(size(resp,3)/nrow);
% plot
Mlim = 0;
for i = 1:size(resp,3)
resp0 = my_conv(my_conv(resp(:,:,i), ops.sig)', ops.sig)';
[Mmax, xmax] = max(abs(resp0), [], 2);
[maxALL, ymax] = max(abs(Mmax), [], 1);
Mlim = max(Mlim, maxALL);
end
for i = 1:size(resp,3)
resp0 = my_conv(my_conv(resp(:,:,i), ops.sig)', ops.sig)';
[Mmax, xmax] = max(resp0, [], 2);
[~, ymax] = max(Mmax, [], 1);
xmax = xmax(ymax);
pk(i,1) = ymax;
pk(i,2) = xmax;
subplot(nrow, ncol, i)
if ops.dynamicRFscale
imagesc(resp0)
else
imagesc(resp0, [-Mlim Mlim])
end
colorbar
xlabel(xmax)
ylabel(ymax)
if i==size(resp,3)
title('mean of all channels')
end
end
% compute best coordinates
xcenters = linspace(Protocol.pars(2), Protocol.pars(3), size(resp,2)+1);
xcenters = (xcenters(1:end-1) + xcenters(2:end))/2;
ycenters = linspace(Protocol.pars(5), Protocol.pars(4), size(resp,1)+1);
ycenters = (ycenters(1:end-1) + ycenters(2:end))/2;
bestCoords = [ycenters(pk(:,1))' xcenters(pk(:,2))'];
% end