-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_NEAR.m
152 lines (124 loc) · 4.98 KB
/
run_NEAR.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
function [outEEG] = run_NEAR(dname, dloc, params)
% run_NEAR() - runs NEAR pipeline for each subject data with the given
% params
%
% Syntax: [outEEG] = run_NEAR(dname, dloc, sname, sloc, params)
%
% Inputs:
% dname - Name of the dataset
% dloc - Corresponding file location
% params - A struct file defined by the user to run NEAR with custom configurations
%
% Outputs:
% outEEG - NEAR Pre-Processed EEG struct ready to be saved and for further time-frequency analysis
%
% Examples:
% [outEEG] = run_NEAR('s12.set', 'D:\\Data\\, params);
%
% Other m-files required: NEAR_plotBadChannels.m, EEGLAB related files
%
% See also: pop_NEAR
% Author: Velu Prabhakar Kumaravel
% PhD Student (FBK & CIMEC-UNITN, Trento, Italy)
% email: [email protected]
% May 2021; Last revision: 19-May-2021
% parameter extraction
isLPF = params.isLPF;
isHPF = params.isHPF;
isSegt = params.isSegt;
isBadCh = params.isBadCh;
isVisIns = params.isVisIns;
isBadSeg = params.isBadSeg;
isInterp = params.isInterp;
isAvg = params.isAvg;
lpc = params.lpc;
nOrder = params.nOrder;
hptf = params.hptf;
look_thr = params.look_thr;
isplot = params.isplot;
isFlat = params.isFlat;
flatWin = params.flatWin;
isLOF = params.isLOF;
dist_metric = params.dist_metric;
thresh_lof = params.thresh_lof;
isPeriodogram = params.isPeriodogram;
frange = params.frange;
winsize = params.winsize;
winov = params.winov;
pthresh = params.pthresh;
rej_cutoff = params.rej_cutoff;
rej_mode = params.rej_mode;
addn_reject = params.addn_reject;
% import data
EEG = pop_loadset('filename',dname,'filepath',dloc);
origEEG = EEG; % making a copy of raw data
% filter data
if(isLPF)
EEG = pop_eegfiltnew(EEG, [], lpc, nOrder, 0, [], 0); % low-pass filter
fprintf('\nData is low-pass filtered\n');
end
if(isHPF)
EEG=clean_drifts(EEG,hptf, []); %high-pass filter
fprintf('\nData is high-pass filtered\n');
end
% segment data using fixation intervals
if(isSegt)
if(~isempty(params.sname) && ~isempty(params.sloc))
try
lookFile=importdata([params.sloc '\\' params.sname]); % make sure it is generic for MAC and Windows (TO BE FIXED)
catch
error('An error occurred in importing the segmentation file. If you think this is a bug, please report on the github repo issues section');
end
end
if(~isempty(lookFile))
try
tmp = strsplit(dname, '.');
sheetName = tmp{1};
lookTimes=NEAR_getLookTimes(lookFile,sheetName,look_thr);
catch
error('An error occurred in segmentation. Does the file contain the segmentation intervals for the given subject?\n');
end
else
error('We cannot find the file. Please check the file path and run again.\n');
end
% segment EEG data
EEG = pop_select( EEG,'time',lookTimes);
fprintf('\nSegmentation is done\n');
end
if (isBadCh)
[EEG, flat_ch, lof_ch, periodo_ch, ~] = NEAR_getBadChannels(EEG, isFlat, flatWin, isLOF, thresh_lof, dist_metric, ...
isPeriodogram, frange, winsize, winov, pthresh, isplot);
disp('Bad Channel Detection is performed successfully\n');
if(isVisIns)
% visual inspection and reject using 'Reject' button on the GUI
NEAR_plotBadChannels (EEG, flat_ch, lof_ch, periodo_ch);
else
% direct removal of bad channels without any GUI
badChans = sort(unique(union(union(flat_ch, lof_ch),periodo_ch)));
if(~isempty(badChans))
if(size(badChans,1) ~= 1)
badChans = badChans';
end
end
EEG = pop_select(EEG, 'nochannel', badChans);
end
else
disp('NEAR Bad Channel Detection is not employed. Set the variable ''isBadCh'' to 1 to enable bad channel detection\n');
end
if(isBadSeg)
EEG = pop_clean_rawdata(EEG, 'FlatlineCriterion','off','ChannelCriterion','off','LineNoiseCriterion','off', ...
'Highpass','off','BurstCriterion',rej_cutoff,'WindowCriterion',addn_reject,'BurstRejection',rej_mode,'Distance','Euclidian');
fprintf('\nArtifacted epochs are corrected by ASR algorithm\n');
end
if(isInterp)
EEG = pop_interp(EEG, origEEG.chanlocs, 'spherical');
fprintf('\nMissed channels are spherically interpolated\n');
end
% Note: The following code is valid only for Cz reference systems
% Please change the code accordingly.
if(isAvg)
EEG = pop_reref(EEG, [],'refloc',struct('labels',{'Cz'},'Y',{0},'X',{5.4492e-16},'Z',{8.8992},'sph_theta',{0},'sph_phi',{90},'sph_radius',{8.8992},'theta',{0},'radius',{0},'type',{''},'ref',{''},'urchan',{132},'datachan',{0}));
EEG.comments = pop_comments(EEG.comments,'','Average reference, reference channel Cz added',1);
end
outEEG = EEG;
end