ImaGIN_BadChannelQualityControl.m
2.81 KB
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
function D = ImaGIN_BadChannelQualityControl(S)
% -=============================================================================
% This function is part of the ImaGIN software:
% https://f-tract.eu/
%
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE AUTHORS
% DO NOT ASSUME ANY LIABILITY OR RESPONSIBILITY FOR ITS USE IN ANY CONTEXT.
%
% Copyright (c) 2000-2018 Inserm U1216
% =============================================================================-
%
% Authors: Viateur Tuyisenge & Olivier David
% FileIn: path linking to meeg object
FileIn = S.dataset;
try
D = spm_eeg_load(FileIn); % Load the cropped meeg object
catch
FileIn = spm_select(1, '\.mat$', 'Select data file');
D=spm_eeg_load(FileIn);
end
[badDir,cutName,~] = fileparts(FileIn);
bPrefix = strcat(badDir,'/',cutName);
bIdxAuto = load(strcat(bPrefix,'_bIdx.txt')); % txt file of indices computed by machine learning
bIdxChecked = load(strcat(bPrefix,'_bIdxChecked.txt')); % txt file of indices manually written during quality control
if ~isempty(bIdxChecked)
bIdx = bIdxChecked(:);
else
bIdx = bIdxAuto(:);
end
elec= sensors(D,'eeg'); % add channels without positions into bad channels
pos = elec.elecpos;
Sens = elec.label;
chanLbs = D.chanlabels;
idxNaN = find(isnan(pos(:,1)));
if ~isempty(idxNaN)
bIdx = [bIdx(:);idxNaN(:)];
bIdx = sort(unique(bIdx));
end
T = readtable(strcat(bPrefix,'.csv'));
tNote = cell(size(T,1),1);
tNote(:) = {'Good'};
tNote(ismember(T.noIdx,bIdx)) = {'Bad'};
T(:,9) = tNote;
csvfilename = strcat(bPrefix,'.csv');
writetable(T,csvfilename,'Delimiter',',');
badchaFile = fopen(strcat(bPrefix,'_bChans.txt'), 'w');
if ~isempty(bIdx)
for i = 1:length(bIdx)
fprintf(badchaFile, '%d %s\n', bIdx(i), char(chanLbs{bIdx(i)}));
end
end
fclose(badchaFile);
try
monoRecordings = fopen(fullfile(badDir, ['recordings_monopolar_', cutName, '.txt']), 'w'); % export monopolar recording channels
for i = 1:length(Sens)
if isempty(find(strcmp(Sens{i}, chanLbs(bIdx)),1))
if isempty(regexpi(char(Sens{i}),'ecg'))
fprintf(monoRecordings, '%s\n', char(Sens{i}));
end
end
end
fclose(monoRecordings);
catch exception
throw(exception)
end
tmpIdx = D.badchannels; % old badchannels indices
D = badchannels(D,tmpIdx,0); % reset meeg object bad indices
if ~isempty(bIdx)
D = badchannels(D, bIdx,1); % add new badchannel indeces in meeg object
end
Dbad = clone(D, bPrefix, [D.nchannels D.nsamples D.ntrials]); % save meeg with badchannel indices in Badchannel directory
Dbad(:,:,:) = D(:,:,:);
save(Dbad);