-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_eeg_reduce.m
176 lines (147 loc) · 5.17 KB
/
spm_eeg_reduce.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
function D = spm_eeg_reduce(S)
% Apply data reduction to M/EEG dataset
% FORMAT D = spm_eeg_reduce(S)
% S - input structure
%
% fields of S:
% S.D - MEEG object or filename of M/EEG mat-file with
%
% S.channels - cell array of channel names. Can include generic
% wildcards: 'All', 'EEG', 'MEG' etc
% S.conditions - cell array of condition trial names.
% S.method - name for the spectral estimation to use. This
% corresponds to the name of a plug-in function that comes
% after 'spm_eeg_reduce_' prefix.
% S.keeporig - keep the original unreduced channels (1) or remove
% (0, default).
% S.keepothers - keep the other (not involved) channels
% S.settings - plug-in specific settings
% S.timewin - time windows or interest
% S.prefix - prefix for the output file (default - 'R')
%
% Output:
% D - M/EEG object
%__________________________________________________________________________
% Copyright (C) 2012-2016 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak
% $Id: spm_eeg_reduce.m 6843 2016-07-28 10:55:47Z vladimir $
SVNrev = '$Rev: 6843 $';
%-Startup
%--------------------------------------------------------------------------
spm('FnBanner', mfilename, SVNrev);
spm('FigName','M/EEG reduce'); spm('Pointer','Watch');
%-Configure the analysis
%--------------------------------------------------------------------------
if ~isfield(S, 'channels'), S.channels = 'all'; end
if ~isfield(S, 'conditions'), S.conditions.all = 1; end
if ~isfield(S, 'timewin'), S.timewin = [-Inf Inf]; end
if ~isfield(S, 'keeporig'), S.keeporig = false; end
if ~isfield(S, 'keepothers'), S.keepothers = true; end
if ~isfield(S, 'prefix'), S.prefix = 'R'; end
if iscell(S.D)
badind = [];
for i = 1:numel(S.D)
DD{i} = spm_eeg_load(S.D{i});
badind = [badind DD{i}.badchannels];
end
D = DD{1};
badind = unique(badind);
else
D = spm_eeg_load(S.D);
DD{1} = D;
badind = D.badchannels;
end
chanind = setdiff(D.selectchannels(S.channels), badind);
if isempty(chanind)
error('No channels selected.');
end
%%%%%%%%%
% MWW
samples = {};
for i = 1:size(S.timewin, 1)
samples{i} = D.indsample(S.timewin(i, 1)):D.indsample(S.timewin(i, 2));
end
if isfield(S.conditions, 'all')
trials = 1:D.ntrials;
else
trials = D.indtrial(S.conditions, 'GOOD');
if isempty(trials)
error('No trials matched the selection, check the specified condition labels');
end
end
%%%%%%%%%%
if ~isfield(S, 'method')
S.method = 'pca';
S.settings.ncomp = min(length(floor(chanind)/2), 100);
end
if isfield(S, 'settings')
S1 = S.settings;
else
S1 = [];
end
if numel(DD)>1
S1.D = DD;
else
S1.D = D;
end
S1.chanind = chanind;
S1.trials = trials;
S1.samples = samples;
montage = feval(['spm_eeg_reduce_' S.method], S1);
if ~isempty(badind)
montage.labelorg = [montage.labelorg(:); D.chanlabels(badind)'];
if S.keeporig
montage.tra((end+1):(end+length(badind)), (end+1):(end+length(badind))) = eye(length(badind));
else
montage.tra(end, end+length(badind)) = 0;
end
if isfield(montage, 'chantypeorg')
montage.chantypeorg = [montage.chantypeorg(:); lower(D.chantype(badind))'];
end
if isfield(montage, 'chanunitorg')
montage.chanunitorg = [montage.chanunitorg(:); D.units(badind)'];
end
end
% This is to discard bad channels but keep other channels (like non MEEG).
if S.keeporig
montage.labelnew = [montage.labelnew(:); D.chanlabels(chanind)'];
montage.tra((end+1):(end+length(chanind)), 1:length(chanind)) = eye(length(chanind));
if isfield(montage, 'chantypenew')
montage.chantypenew = [montage.chantypenew(:); lower(D.chantype([badind chanind]))'];
end
if isfield(montage, 'chanunitnew')
montage.chanunitnew = [montage.chanunitnew(:); D.units([badind chanind])'];
end
end
% Reorder as in the original file. This might be handy when reducing and
% then grand-averaging or merging across subjects
[sel1, sel2] = spm_match_str(D.chanlabels, montage.labelnew);
sortind = [spm_vec(sel2); spm_vec(setdiff(1:length(montage.labelnew), sel2))];
montage.labelnew = montage.labelnew(sortind);
montage.tra = montage.tra(sortind, :);
if isfield(montage, 'chantypenew')
montage.chantypenew = montage.chantypenew(sortind);
end
if isfield(montage, 'chanunitnew')
montage.chanunitnew = montage.chanunitnew(sortind);
end
S1 = [];
S1.montage = montage;
S1.keepothers = S.keepothers;
S1.prefix = S.prefix;
S1.updatehistory = 0;
for i = 1:numel(DD)
S1.D = DD{i};
D = spm_eeg_montage(S1);
%-Save new M/EEG dataset(s)
%--------------------------------------------------------------------------
D = D.history('spm_eeg_reduce', S);
save(D);
DD{i} = D;
end
if numel(DD)>1
D = DD;
end
%-Cleanup
%--------------------------------------------------------------------------
spm('FigName','M/EEG reduce: done'); spm('Pointer','Arrow');