-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildLFPIndex.m
284 lines (270 loc) · 8.53 KB
/
BuildLFPIndex.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
function I = BuildLFPIndex(name,varargin)
%I = BuildLFPIndex(name,varargin)
%For Utah Folders, read in all the lfp files that are made and summarize contents
%For Spike2 Folders, compare Expts List with list of .lfp files
listonly = 0;
j = 1;
defaultarray = '';
while j <= length(varargin)
if strncmpi(varargin{j},'fixlist',4)
listonly = 2;
elseif strncmpi(varargin{j},'list',4)
listonly = 1;
end
j = j+1;
end
if iscellstr(name)
for j = 1:length(name)
I{j} = BuildLFPIndex(name{j},varargin(:));
end
return;
elseif iscell(name) %previous result
for j = 1:length(name)
if isfield(name{j},'D')
D = name{j}.D;
nerr = 0;
errs = {};
for k = 1:length(D)
x = PrintErrorSummary(D{k});
errs = {errs{:} x{:}};
nerr = nerr + length(x);
end
if nerr == 0
fprintf('No errors in %s\n',name{j}.name);
else
PrintErrorSummary(errs,'show');
end
end
end
return;
end
if strcmp(name,'jbe') %buidl index for jbe Utah data
d = mydir('/b/data/jbe/G*');
I = BuildLFPIndex({d.name},varargin{:});
return;
end
if isdir(name)
mnk = GetMonkeyName(name);
if isempty(defaultarray)
if strcmp(mnk,'jbe')
defaultarray = 'Utah';
else
defaultarray = 'uprobe';
end
end
goodexpts = 0;
outname = [name '/LFPlist.mat'];
Array = GetArrayConfig(name);
if isempty(Array) || ~isfield(Array,'type')
Array.type = defaultarray;
end
if listonly
if sum(strncmpi(Array.type,{'uprobe' 'vprobe'},4))
Expts = ReadExptDir(name);
for j = 1:length(Expts)
lfpname = [name '/' GetName(Expts{j},'lfp')];
if ~exist(name)
fprintf('Missing %s\n',name);
goodexpts(j) = 0;
else
goodexpts(j) = 1;
end
end
I.name = outname;
I.havefiles = goodexpts;
I.missing = sum(goodexpts ==0);
I.ngood = sum(goodexpts > 0);
elseif strncmpi(Array.type,'utah',4)
I.name = outname;
if ~exist(outname)
I.list = 0;
fprintf('No LFP List for %s\n',name);
else
I.list = 1;
I.exptno = [];
I.lowpasserror = 0;
OUT = load(outname);
if ~iscell(OUT.D)
D{1} = OUT.D;
OUT.D = D;
end
for j = 1:length(OUT.D)
exptno = 0;
ncut = 0;
if isfield(OUT.D{j},'exptno')
name = [outname 'Expt' num2str(OUT.D{j}.exptno)];
exptno = OUT.D{j}.exptno;
end
I.exptno = [I.exptno exptno];
if isfield(OUT.D{j},'lfpnames')
I.names{j} = OUT.D{j}.lfpnames{1};
name = OUT.D{j}.lfpnames{1};
elseif exptno > 0
name = [outname 'Expt' num2str(exptno)];
else
name = outname;
end
if isfield(OUT.D{j},'errs')
I.nerrs(j) = length(OUT.D{j}.errs);
for e = 1:length(OUT.D{j}.errs)
err = OUT.D{j}.errs{e};
if strfind(err,'points, array is')
fprintf('Bad Channel in %s\n',name);
elseif strfind(err,'filtered out')
ncut = ncut+1;
else
fprintf('Unknown Errors in Expt %d\n',j);
end
end
else
I.nerrs(j) = 0;
end
I.lowpasserror(j) = ncut;
end
if sum(I.lowpasserror)
fprintf('Filteriing wrong in %s\n',name);
end
end
else
I.err = sprintf('Unknown Array Type %s',Arrat,type);
cprintf('red','%s in %s\n',I.err,name);
I.name = outname;
end
if listonly == 1
return;
end
else
I.name = outname;
end
if exist(outname)
OUT = load(outname);
if isfield(OUT,'D')
if isstruct(OUT.D)
olddata = OUT.D;
OUT.D = {};
else
D = {};
for j = 1:length(OUT.D)
expts(j) = ExptIndex(OUT.D{j});
if expts(j) > 0
D(expts(j)) = OUT.D(j);
end
end
OUT.D = D;
end
end
end
if sum(strncmpi(Array.type,{'uprobe' 'vprobe'},4))
Expts = ReadExptDir(name);
I.LFPerrs = {};
I.LFPerrdata =[];
for j = 1:length(Expts)
[~, E] = LoadLFP(Expts{j});
if E.gotlfp == 0
goodexpts(j) = 0;
elseif isfield(E,'LFPerrs')
goodexpts(j) = 2; %have data, but with errors
I.LFPerrs = cat(1,I.LFPerrs(:),E.LFPerrs(:));
I.LFPerrdata = cat(2,I.LFPerrdata,E.LFPerrdata);
else
goodexpts(j) = 1;
end
end
I.goodexpts = goodexpts;
if sum(goodexpts)
save(outname,'-struct','I');
end
elseif strncmpi(Array.type,'Utah',4)
d = mydir([name '/Expt*.lfp.mat']);
if ~isempty(d)
for j = 1:length(d)
X = load(d(j).name);
e = GetExptNumber(d(j).name);
if isfield(X,'LFPHeader')
OUT.D{e}.Header = X.LFPHeader;
goodexpts(e) = 1;
OUT.D{e}.npts = size(X.LFP.rawlfp,1);
OUT.D{e}.nprobes = size(X.LFP.rawlfp,2);
elseif isfield(X,'LFP')
OUT.D{e}.npts = size(X.LFP.rawlfp,1);
OUT.D{e}.nprobes = size(X.LFP.rawlfp,2);
if isfield(X.LFP,'Header')
OUT.D{e}.Header = X.LFP.Header;
else
OUT.D{e} = CopyFields(OUT.D{e},X.LFP,'kernel','sd','decimate','samper');
end
goodexpts(e) = 1;
end
OUT.D{e}.exptno = e;
if OUT.D{e}.nprobes < length(Array.X)
fprintf('Only %d probes in %s\n',OUT.D{e}.nprobes,d(j).name);
goodexpts(e) = 2;
end
end
I.D = OUT.D;
if sum(goodexpts)
bid = find(~goodexpts); %no data here any more
for j =1:length(bid)
OUT.D{bid(j)} = [];
end
save(outname,'-struct','OUT');
end
end
else
cprintf('red','Unknown Array Type %s in %s\n',Array.type,name);
end
else
I.name = name;
end
function LFPerrs = PrintErrorSummary(LFP,varargin)
show = 0;
j = 1;
while j <= length(varargin)
if strncmpi(varargin{j},'show',4)
show = 1;
end
j = j+1;
end
LFPerrs = {};
if isfield(LFP,'errs')
for k = 1:length(LFP.errs)
probes(k) = GetProbeFromName(LFP.errs{k});
errs{k} = deblank(regexprep(LFP.errs{k},'.p[0-9]+FullV.mat',''));
end
[a,b] = unique(errs);
for k = 1:length(a)
id = find(strcmp(a{k},errs));
LFPerrs{end+1}.s = a{k};
LFPerrs{end}.probes = probes(id);
if length(id) > 10
pstr = sprintf('%d-%d',min(probes(id)),max(probes(id)));
else
pstr = sprintf(' %d',probes(id));
end
if show
fprintf('%s Probes %s\n',a{k},pstr);
end
end
elseif isfield(LFP,'Header') && isfield(LFP.Header,'errs')
LFPerrs = PrintErrorSummary(LFP.Header);
elseif iscell(LFP)
for k = 1:length(LFP)
expts(k) = GetExptNumber(LFP{k}.s);
errs{k} = deblank(regexprep(LFP{k}.s,'Expt[0-9]+',''));
end
[a,b] = unique(errs);
for k = 1:length(a)
id = find(strcmp(a{k},errs));
LFPerrs{end+1}.s = a{k};
LFPerrs{end}.expts = expts(id);
if show
fprintf('%s Expts %s\n',a{k},sprintf(' %d',expts(id)));
end
end
end
function eid = ExptIndex(X)
eid = 0;
if isfield(X,'exptno')
eid = X.exptno;
elseif isfield(X,'Header')
end