-
Notifications
You must be signed in to change notification settings - Fork 28
/
limo_create_single_trials.m
241 lines (213 loc) · 8.85 KB
/
limo_create_single_trials.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
function limo_create_single_trials(EEG,varargin)
% FORMAT limo_create_single_trials(EEG,options)
%
% example:
% options = {'format','matrix', 'datatype', 'channels', ...
% 'erp','off','spec','on','ersp','off','itc','on','rmicacomps','on', ...
% 'erpparams',[],'specparams',[],'erspparams',[], 'interp','off', ...
% 'scalp','off','recompute','on','savetrials','on'};
% limo_create_single_trials('my_subject.set',options{:})
%
% INPUT EEG is a EEG.set of a single subject from EEGLAB
% option is a structure with various fiedls depending on what data
% type needs to be computed -- the idea here is that the data in
% EEG.data are the 'raw' data and we here apply IC removals and then
% create output -- see std_precomp
%
% option.format: 'matrix' (default) or 'cell'
% option.datatype: 'channels' or 'components'
%
% Optional fields
% 'erp' - ['on'|'off'] pre-compute ERPs for each dataset (default 'on')
% 'spec' - ['on'|'off'] pre-compute spectrum for each dataset (default 'on')
% Use 'specparams' to set spectrum parameters.
% 'ersp' - ['on'|'off'] pre-compute ERSP for each dataset (default 'on')
% Use 'erspparams' to set time/frequency parameters.
% 'itc' - ['on'|'off'] pre-compute ITC for each dataset (default 'on')
% Use 'erspparams' to set time/frequency parameters.
% 'erpparams' - [cell array] Parameters for the std_erp function. See
% std_erp for more information.
% 'specparams' - [cell array] Parameters for the std_spec function. See
% std_spec for more information.
% 'erspparams' - [cell array] Optional arguments for the std_ersp function.
% 'rmicacomps' - ['on'|'off'|'processica'] remove ICA components pre-selected in
% each dataset (EEGLAB menu item, "Tools > Reject data using ICA
% > Reject components by map). This option is ignored when
% precomputing measures for ICA clusters. Default is 'off'.
% 'processica' forces to process ICA components instead of
% removing them (default 'on')
%
% OUTPUT file_names the names of the files creates
% + single trials data files stored on the drive
%
% see also std_precomp
%
% Author: Cyril Pernet (LIMO Team), The university of Edinburgh, 2014
% Arnaud Delorme (EEGLAB), SCCN, 2014
%
% ------------------------------
% Copyright (C) LIMO Team 2019
%% deal with options
% set defaults
try
options = varargin;
if ~isempty( varargin ),
for i = 1:2:numel(options)
opt.(options{i}) = options{i+1};
end
else opt= []; end;
catch
error('limo_create_single_trials() error: calling convention {''key'', value, ... } error'); return;
end;
try, opt.format; catch, opt.format = 'matrix'; end;
try, opt.datatype; catch, opt.datatype = 'channels'; end;
try, opt.erp; catch, opt.erp = 'on'; end;
try, opt.spec; catch, opt.spec = 'on'; end;
try, opt.ersp; catch, opt.ersp = 'on'; end;
try, opt.itc; catch, opt.itc = 'on'; end;
try, opt.rmicacomps; catch, opt.rmicacomps = 'on'; end;
try, opt.interp; catch, opt.interp = 'off'; end;
try, opt.erpparams; catch, opt.erpparams = []; end;
try, opt.specparams; catch, opt.specparams = []; end;
try, opt.erspparams; catch, opt.erspparams = []; end;
if isfield(options,'itc') && isfield(options,'ersp')
opt.itc = options.ersp; % for consitancy use ersp
end
if isfield(options,'ersp') && ~isfield(options,'itc')
opt.itc = 'off';
end
opt.savetrials = 'on';
opt.scalp = 'off';
opt.recompute = 'on';
fields = fieldnames(opt);
c = 1;
for i = 1: length(structfun(@numel,opt))
if ~any ([isempty(eval(['opt.' fields{i}])),strcmp(fields(i),'format'), strcmp(fields(i),'datatype')])
in_options{c} = fields{i};
in_options{c+1} = eval(['opt.' fields{i}]);
c = c+2;
end
end
%% create a STUDY just for that subject
if ~isstruct(EEG)
ALLEEG = pop_loadset(EEG);
else
ALLEEG = EEG; clear EEG;
end
STUDY = struct;
[trash,name]=fileparts(ALLEEG.filepath); clear trash
[STUDY ALLEEG] = std_editset(STUDY, ALLEEG, 'commands',{{'index' 1 'subject' name}},'updatedat','off' );
STUDY.name = ALLEEG.filename; [STUDY ALLEEG] = std_checkset(STUDY, ALLEEG);
name = [ALLEEG.filepath filesep ALLEEG.filename '_single_trials'];
STUDY.design.cell.filebase = name;
%% compute
[STUDY ALLEEG] = std_precomp(STUDY,ALLEEG,opt.datatype,in_options{:});
clear STUDY
%% save-delete data / update EEG.set file
if strcmp(opt.datatype,'channels')
if strcmp(opt.erp,'on')
data = load('-mat',[name '.daterp']);
ALLEEG.etc.timeerp = data.times;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_daterp.mat'],'data'); clear data
ALLEEG.etc.datafiles.daterp = [name '_daterp.mat'];
delete([name '.daterp']);
else
ALLEEG.etc.datafiles.daterp = [name '.daterp'];
end
end
if strcmp(opt.spec,'on')
if strcmp(opt.format,'matrix')
[data,trash,ALLEEG.etc.freqspec] = limo_struct2mat([name '.datspec']);
save([name '_datspec.mat'],'data'); clear data trash
ALLEEG.etc.datafiles.datspec = [name '_datspec.mat'];
delete([name '.datspec']);
else
data = load('-mat',[name '.datspec']);
ALLEEG.etc.freqspec = data.freqs;
ALLEEG.etc.datafiles.datspec = [name '.datspec'];
end
end
if strcmp(opt.ersp,'on')
disp('reading single trials ersp, be patient ..')
if strcmp(opt.format,'matrix')
[data,ALLEEG.etc.timeersp,ALLEEG.etc.freqersp] = limo_struct2mat([name '.dattimef']);
save([name '_datersp.mat'],'data'); clear data
ALLEEG.etc.datafiles.datersp = [name '_datersp.mat'];
delete([name '.dattimef']);
else
data = load('-mat',[name '.dattimef']);
ALLEEG.etc.timeersp = data.times;
ALLEEG.etc.freqersp = data.freqs;
ALLEEG.etc.datafiles.datersp = [name '.dattimef'];
end
end
if strcmp(opt.itc,'on')
data = load('-mat',[name '.datitc']);
ALLEEG.etc.timeitc = data.times;
ALLEEG.etc.freqitc = data.freqs;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_datitc.mat'],'data'); clear data
ALLEEG.etc.datafiles.datitc = [name '_datitc.mat'];
delete([name '.datitc']);
else
ALLEEG.etc.datafiles.datitc = [name '.datitc'];
end
end
end
if strcmp(opt.datatype,'ica')
if strcmp(opt.erp,'on')
data = load('-mat',[name '.icaerp']);
ALLEEG.etc.timeerp = data.times;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_icaerp.mat'],'data'); clear data
ALLEEG.etc.datafiles.daterp = [name '_icaerp.mat'];
delete([name '.icaerp']);
else
ALLEEG.etc.datafiles.icaerp = [name '.icaerp'];
end
end
if strcmp(opt.spec,'on')
data = load('-mat',[name '.icaspec']);
ALLEEG.etc.freqspec = data.freqs;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_icaspec.mat'],'data'); clear data
ALLEEG.etc.datafiles.icaspec = [name '_icaspec.mat'];
delete([name '.icaspec']);
else
ALLEEG.etc.datafiles.icaspec = [name '.icaspec'];
end
end
if strcmp(opt.ersp,'on')
data = load('-mat',[name '.icatimef']);
ALLEEG.etc.timeersp = data.times;
ALLEEG.etc.freqersp = data.freqs;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_icaersp.mat'],'data'); clear data
ALLEEG.etc.datafiles.icaersp = [name '_icaersp.mat'];
delete([name '.icatimef']);
else
ALLEEG.etc.datafiles.icaersp = [name '.icatimef'];
end
end
if strcmp(opt.itc,'on')
data = load('-mat',[name '.icaitc']);
ALLEEG.etc.timeitc = data.times;
ALLEEG.etc.freqitc = data.freqs;
if strcmp(opt.format,'matrix')
data = limo_struct2mat(data);
save([name '_icaitc.mat'],'data'); clear data
ALLEEG.etc.datafiles.datitc = [name '_icaitc.mat'];
delete([name '.icaitc']);
else
ALLEEG.etc.datafiles.icaitc = [name '.icaitc'];
end
end
end
EEG = ALLEEG; clear ALLEEG;
pop_saveset(EEG, 'filename', EEG.filename, 'filepath',EEG.filepath,'savemode' ,'twofiles');