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 pathdetectTemplate.m
469 lines (408 loc) · 18.4 KB
/
detectTemplate.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
function [MuseStruct, Tindx_unique, LFP_avg] = detectTemplate(cfg, MuseStruct, template, force)
% DETECTEMPLATE detect templates using normalized crosscorrelation.
% Compares templates, and writes markers of most fitting template to Muse
% event file.
%
% use as
% [MuseStruct, C_norm, Tindx_unique, LFP_avg] = detectTemplate(cfg, MuseStruct, template, force)
%
% Necessary fields (as defined in _setparams function):
%
% 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.visible = ft_getopt(cfg, 'visible', 'on');
cfg.template.reref = ft_getopt(cfg.template, 'reref', 'no');
cfg.template.refmethod = ft_getopt(cfg.template, 'refmethod', 'none');
cfg.template.latency = ft_getopt(cfg.template, 'latency', 'all');
cfg.template.writemuse = ft_getopt(cfg.template, 'writemuse', false);
cfg.template.threshold = ft_getopt(cfg.template, 'threshold', 4);
cfg.template.name = ft_getopt(cfg.template, 'name', 'TemplateDetect');
fname_out = fullfile(cfg.datasavedir, [cfg.prefix, 'MuseStruct_detectedTemplates.mat']);
if nargin == 1
if exist(fname_out, 'file')
fprintf('Loading results template detection: %s\n', fname_out);
% repeat to deal with load errors
count = 0;
err_count = 0;
while count == err_count
try
load(fname_out, 'MuseStruct', 'Tindx_unique', 'LFP_avg');
catch ME
err_count = err_count + 1;
disp('Something went wrong loading the file. Trying again...')
end
count = count + 1;
end
return;
else
warning('No precomputed data is found, not enough input arguments to compute data');
return
end
end
if exist(fname_out,'file') && force == false
fprintf('Loading results template detection: %s\n', fname_out);
% repeat to deal with load errors
count = 0;
err_count = 0;
while count == err_count
try
load(fname_out, 'MuseStruct', 'Tindx_unique', 'LFP_avg');
catch ME
err_count = err_count + 1;
end
count = count + 1;
end
return
end
fprintf('Detecting spikes\n');
% get file format
[isNeuralynx, isMicromed, isBrainvision] = get_data_format(cfg);
% process templates
for itemp = 1 : size(template, 2)
cfgtemp = [];
cfgtemp.latency = cfg.template.latency;
template{itemp} = ft_selectdata(cfgtemp, template{itemp});
if isfield(cfg.template,'resamplefs')
cfgtemp = [];
cfgtemp.resamplefs = cfg.template.resamplefs;
template{itemp} = ft_resampledata(cfgtemp, template{itemp});
end
end
% loop over parts
for ipart = 1 : size(cfg.directorylist,2)
offset = 0; % in samples
dirindx = []; %
datlength = [];
% loop over directories
for idir = 1 : size(cfg.directorylist{ipart}, 2)
if isNeuralynx
nfile = size(cfg.cluster.channel, 2); % one file per channel
elseif isMicromed
nfile = 1; % only one file with all electrodes
fname = fullfile(cfg.rawdir, [cfg.directorylist{ipart}{idir} '.TRC']);
elseif isBrainvision
nfile = 1; % only one file with all electrodes
fname = fullfile(cfg.rawdir, [cfg.directorylist{ipart}{idir} '.eeg']);
end
% concatinate channels
for ifile = 1 : nfile
cfgtemp = [];
if isNeuralynx
temp = dir(fullfile(cfg.rawdir, cfg.directorylist{ipart}{idir}, ['*', cfg.cluster.channel{ifile},'.ncs']));
cfgtemp.dataset = fullfile(cfg.rawdir, cfg.directorylist{ipart}{idir}, temp.name);
filedat{ifile} = ft_preprocessing(cfgtemp);
% labels have to be the same to append over directories
temp = strsplit(filedat{ifile}.label{1},'_');
filedat{ifile}.label{1} = strcat(temp{end-1},'_',temp{end});
else
cfgtemp.dataset = fname;
cfgtemp.channel = cfg.cluster.channel;
filedat{ifile} = ft_preprocessing(cfgtemp);
end
end
cfgtemp = [];
cfgtemp.keepsampleinfo = 'no';
dirdat{idir} = ft_appenddata(cfgtemp,filedat{:});
clear filedat
% bipolar rereferencing if requested
% TODO: create template from scratch using indices from clusterLFP
% then do same re-referencing as data
if strcmp(cfg.template.reref, 'yes')
if strcmp(cfg.template.refmethod, 'bipolar')
labels_nonum = regexprep(dirdat{idir}.label, '[0-9_]', '');
[~,~,indx] = unique(labels_nonum);
clear group
for i = 1 : max(indx)
cfgtemp = [];
cfgtemp.reref = 'yes';
cfgtemp.refmethod = 'bipolar';
cfgtemp.channel = dirdat{idir}.label(indx==i);
group{i} = ft_preprocessing(cfgtemp,dirdat{idir});
end
dirdat{idir} = ft_appenddata([],group{:});
clear group
end
end
if isfield(cfg.template, 'resamplefs')
cfgtemp = [];
cfgtemp.resamplefs = cfg.template.resamplefs;
dirdat{idir} = ft_resampledata(cfgtemp, dirdat{idir});
end
dirindx = [dirindx ones(1,length(dirdat{idir}.time{1})) * idir];
datlength(idir) = length(dirdat{idir}.time{1});
if idir > 1
offset = offset + length(dirdat{idir-1}.time{1});
cfgtemp = [];
cfgtemp.offset = offset;
dirdat{idir} = ft_redefinetrial(cfgtemp, dirdat{idir});
end
end
cumsumdatlength = cumsum(datlength);
cumsumdatlength = [0 cumsumdatlength(1:end-1)];
cfgtemp = [];
cfgtemp.keepsampleinfo = 'no';
temp = ft_appenddata(cfgtemp, dirdat{:});
clear dirdat
dat = rmfield(temp,'cfg');
dat.trial = {cat(2, temp.trial{:})};
dat.time = {cat(2, temp.time{:})};
clear temp
% sometimes data contains nan's probably at edges
dat.trial{1}(isnan(dat.trial{1})) = 0;
% find templates
for itemp = 1 : size(template, 2)
C = normxcorr2(template{itemp}.avg', dat.trial{1}');
C(1:size(template{itemp}.avg,2),:) = nan;
C(end-size(template{itemp}.avg,2):end,:) = nan;
noYshift = size(template{itemp}.avg,1);
C_norm{itemp} = normalize(C(:, noYshift)); clear C
threshold = nanstd(C_norm{itemp}) * cfg.template.threshold;
[~, Tindx{ipart}{itemp}, ~, ~] = findpeaks(C_norm{itemp}, 'MinPeakHeight', threshold, 'MinPeakDistance', dat.fsample*(-template{itemp}.time(1)+template{itemp}.time(end))/2);
end
% remove overlapping templates, selecting highest r
a = cell2mat(C_norm);
a = reshape(a, 1, numel(a));
a = normalize(a);
a = reshape(a, size(C_norm{1}, 1), size(C_norm, 2));
C_norm2 = [];
for i = 1 : size(C_norm, 2)
C_norm2{i} = a(:, i);
end
clear a
Tindx_unique{ipart} = Tindx{ipart};
for itemp = 1 : size(template, 2)
others = 1 : size(template, 2);
others(itemp) = [];
for iother = others
[indxA, indxB] = CommonElemTol(Tindx_unique{ipart}{itemp}, Tindx_unique{ipart}{iother}, 25);
comp = C_norm2{itemp}(indxA) >= C_norm2{iother}(indxB);
Tindx_unique{ipart}{itemp}(indxA(~comp)) = [];
Tindx_unique{ipart}{iother}(indxB(comp)) = [];
end
end
% plot correlation, threshold and average LFG
for itemp = 1 : size(template, 2)
% plot threshold per template
fig = figure('visible', cfg.visible); hold;
plot(C_norm{itemp});
axis tight
ax = axis;
plot([ax(1),ax(2)],[threshold, threshold],':k');
if ~isempty(Tindx_unique{ipart}{itemp})
scatter3(Tindx_unique{ipart}{itemp}, C_norm{itemp}(Tindx_unique{ipart}{itemp}), ones(size(Tindx_unique{ipart}{itemp}))*10, 'r.');
n = size(Tindx_unique{ipart}{itemp}, 1);
else
n = 0;
end
axis tight
box off
title(sprintf('n = %d', n));
% show separation in files and time
ax = axis;
axisnames = [];
for i = 1 : length(cumsumdatlength)
plot3([cumsumdatlength(i), cumsumdatlength(i)], [ax(3), ax(4)], [5, 5], 'color',[0, 0, 0]);
axisnames{i} = datestr(MuseStruct{ipart}{i}.starttime);
end
set(gca, 'TickDir', 'out');
xticks(cumsumdatlength);
xticklabels(axisnames);
xtickangle(90);
% print to file
set(fig,'PaperOrientation','landscape');
set(fig,'PaperUnits','normalized');
set(fig,'PaperPosition', [0 0 1 1]);
fname_fig = fullfile(cfg.imagesavedir, 'templates', [cfg.prefix, 'p', num2str(ipart), '_template', num2str(itemp),'_threshold.png']);
isdir_or_mkdir(fileparts(fname_fig));
exportgraphics(fig, fname_fig);
% skip further plotting if no templates were detected
if isempty(Tindx_unique{ipart}{itemp})
LFP_avg{ipart}{itemp} = [];
continue
end
% create LFP averages
startsample = Tindx_unique{ipart}{itemp} - size(template{itemp}.avg,2);
endsample = Tindx_unique{ipart}{itemp};
offset = zeros(size(Tindx_unique{ipart}{itemp})) + template{itemp}.time(1) * dat.fsample;
cfgtemp = [];
cfgtemp.trl = [startsample, endsample, offset];
cfgtemp.trl = round(cfgtemp.trl);
LFP_sel = ft_redefinetrial(cfgtemp, dat);
LFP_avg{ipart}{itemp} = ft_timelockanalysis([], LFP_sel);
% plot LFPs vs. template
fig = figure('visible', cfg.visible);
subplot(1, 3, 1); hold;
maxabs = -inf;
for itrial = 1 : size(LFP_sel.trial,2)
if max(max(abs(LFP_sel.trial{itrial}))) > maxabs
maxabs = max(max(abs(LFP_sel.trial{itrial})));
end
end
ytick = [];
for itrial = 1 : size(LFP_sel.trial,2)
ytick = [ytick, itrial * maxabs];
for ichan = 1 : size(LFP_sel.label,1)
plot(LFP_sel.time{itrial}, LFP_sel.trial{itrial}(ichan,:) + maxabs * ichan, 'color', [0.6, 0.6, 0.6]);
end
end
for ichan = 1 : size(LFP_sel.label,1)
plot(LFP_avg{ipart}{itemp}.time, LFP_avg{ipart}{itemp}.avg(ichan,:) + maxabs * ichan, 'color', [0, 0, 0]);
end
title(sprintf('n = %d', size(LFP_sel.trial,2)));
yticks(ytick);
set(gca,'TickLabelInterpreter', 'none')
set(gca,'fontsize', 6)
yticklabels(LFP_sel.label);
axis tight
box off
% plot overlapping trial LFPs
subplot(1, 3, 2); hold;
maxabs = -inf;
for itrial = 1 : size(LFP_avg{ipart}{itemp}.label, 1)
if max(max(abs(LFP_avg{ipart}{itemp}.avg))) > maxabs
maxabs = max(max(abs(LFP_avg{ipart}{itemp}.avg)));
end
end
for ichan = 1 : size(LFP_avg{ipart}{itemp}.label,1)
plot(template{itemp}.time, template{itemp}.avg(ichan,:) + maxabs * ichan, 'r');
plot(LFP_avg{ipart}{itemp}.time, LFP_avg{ipart}{itemp}.avg(ichan,:) + maxabs * ichan, 'k');
end
set(gca,'fontsize', 6)
set(gca,'Yticklabels',[])
yticklabels([]);
axis tight
box off
% plot average LPF template
subplot(2, 3, 3);
plot(template{itemp}.time, template{itemp}.avg', 'r');
title('Template');
set(gca,'fontsize', 6)
axis tight
box off
% plot average LPF matches
subplot(2, 3, 6);
plot(LFP_avg{ipart}{itemp}.time, LFP_avg{ipart}{itemp}.avg', 'k');
title('Average');
set(gca,'fontsize', 6)
axis tight
box off
% print to file
set(fig,'PaperOrientation','landscape');
set(fig,'PaperUnits','normalized');
set(fig,'PaperPosition', [0 0 1 1]);
fname_fig = fullfile(cfg.imagesavedir, 'templates', [cfg.prefix, 'p', num2str(ipart), '_template', num2str(itemp),'_LFP.png']);
isdir_or_mkdir(fileparts(fname_fig));
exportgraphics(fig, fname_fig);
end
% plot templates and thresholds for all templates
fig = figure('visible', cfg.visible);
for itemp = 1 : size(C_norm2, 2)
subplot(size(C_norm2, 2) + 1, 1, itemp); hold;
plot(C_norm2{itemp}, 'color', [0.5, 0.5, 0.5]);
axis tight
ax = axis;
plot([ax(1),ax(2)],[threshold, threshold],':k');
if ~isempty(Tindx_unique{ipart}{itemp})
scatter3(Tindx{ipart}{itemp}, C_norm2{itemp}(Tindx{ipart}{itemp}), ones(size(Tindx{ipart}{itemp}))*10, 'k.');
scatter3(Tindx_unique{ipart}{itemp}, C_norm2{itemp}(Tindx_unique{ipart}{itemp}), ones(size(Tindx_unique{ipart}{itemp}))*10, 'r.');
n = size(Tindx_unique{ipart}{itemp}, 1);
else
n = 0;
end
title(sprintf('template%d, n = %d', itemp, n));
set(gca, 'Layer', 'top');
xticks('');
set(gca,'fontsize', 6)
for i = 1 : length(cumsumdatlength)
plot3([cumsumdatlength(i), cumsumdatlength(i)], [ax(3), ax(4)], [5, 5], 'color', [0, 0, 0]);
axisnames{i} = datestr(MuseStruct{ipart}{i}.starttime);
end
axis tight
ax = axis;
box off
end
% add filenames to extra subplot
subplot(size(C_norm2, 2) + 1, 1, size(C_norm2, 2) + 1); hold;
set(gca,'fontsize', 6)
for i = 1 : length(cumsumdatlength)
plot3([cumsumdatlength(i), cumsumdatlength(i)], [ax(3), ax(4)], [5, 5], 'color', [0, 0, 0]);
end
axis tight
xlim([ax(1), ax(2)]);
set(gca, 'TickDir', 'out');
xticks(cumsumdatlength);
set(gca, 'xticklabels', axisnames);
xtickangle(90);
% print to file
set(fig,'PaperOrientation','landscape');
set(fig,'PaperUnits','normalized');
set(fig,'PaperPosition', [0 0 1 1]);
fname_fig = fullfile(cfg.imagesavedir, 'templates', [cfg.prefix, 'p', num2str(ipart), '_all_templates_threshold.png']);
isdir_or_mkdir(fileparts(fname_fig));
exportgraphics(fig, fname_fig);
% add to MuseStruct and add to markerfile if requested
for idir = unique(dirindx)
% remove previous template markers
names = fieldnames(MuseStruct{ipart}{idir}.markers);
for itemp = 1 : 100
for iname = 1 : size(names,1)
if strcmp(names(iname), sprintf('template%d', itemp))
MuseStruct{ipart}{idir}.markers = rmfield(MuseStruct{ipart}{idir}.markers, sprintf('template%d', itemp));
end
end
end
% add new template markers
for itemp = 1 : size(template, 2)
% remove offset of directory
indx = Tindx_unique{ipart}{itemp}(dirindx(Tindx_unique{ipart}{itemp}) == idir) - cumsumdatlength(idir) ;
% remove offset of xcorr
indx = indx - size(template{itemp}.time, 2);
% shift to t=0 in template
indx = indx - template{itemp}.time(1) * dat.fsample;
% add to musestruct
name = sprintf('template%d', itemp);
MuseStruct{ipart}{idir}.markers.(name).comment = 'Added by detectTemplate (Stephen)';
MuseStruct{ipart}{idir}.markers.(name).color = 'green';
MuseStruct{ipart}{idir}.markers.(name).offset = 'Added by detectTemplate (Stephen)';
MuseStruct{ipart}{idir}.markers.(name).classgroupid = '+3';
MuseStruct{ipart}{idir}.markers.(name).editable = 'Yes';
MuseStruct{ipart}{idir}.markers.(name).classid = '+666'; % will be replaced by writeMuseMarkers.m
MuseStruct{ipart}{idir}.markers.(name).synctime = (indx / dat.fsample)';
MuseStruct{ipart}{idir}.markers.(name).clock = (seconds(indx / dat.fsample) + MuseStruct{ipart}{idir}.starttime)';
end
if cfg.template.writemuse == true
fname_mrk = fullfile(cfg.rawdir, cfg.directorylist{ipart}{idir},'Events.mrk');
% backup markerfile
if ~exist(cfg.muse.backupdir,'DIR')
error('Backup directory does not exist');
end
[~, d] = fileparts(cfg.directorylist{ipart}{idir});
if ~exist(fullfile(cfg.muse.backupdir, d), 'DIR')
fprintf('Creating directory: %s\n', fullfile(cfg.muse.backupdir, d));
eval(sprintf('!mkdir %s', fullfile(cfg.muse.backupdir, d)));
end
fname_backup = sprintf('Events_%s.mrk', datestr(now, 'mm-dd-yyyy_HH-MM-SS'));
eval(sprintf('!cp %s %s', fname_mrk, fullfile(cfg.muse.backupdir, d, fname_backup)));
fprintf('Succesfully backed up markerfile to %s\n',fullfile(cfg.muse.backupdir, d, fname_backup));
% write to muse marker file
writeMuseMarkerfile(MuseStruct{ipart}{idir}, fname_mrk);
end
end
% regularly update inside loop, just in case
save(fname_out, 'MuseStruct', 'Tindx_unique', 'LFP_avg', '-v7.3');
close all
clear LFP_sel C_norm C_norm2
end