forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldtrip2fiff.m
327 lines (277 loc) · 9.89 KB
/
fieldtrip2fiff.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
function fieldtrip2fiff(filename, data)
% FIELDTRIP2FIFF saves a FieldTrip raw data structure as a fiff-file, allowing it
% to be further analyzed by the Neuromag software, or in the MNE suite software.
%
% Use as
% fieldtrip2fiff(filename, data)
% where filename is the name of the output file, and data is a raw data structure
% as obtained from FT_PREPROCESSING, or a timelock structure obtained from
% FT_TIMELOCKANALYSIS.
%
% If the data comes from preprocessing and has only one trial, then it writes the
% data into raw continuous format. If present in the data, the original header
% from neuromag is reused (also removing the non-used channels). Otherwise, the
% function tries to create a correct header, which might or might not contain the
% correct scaling and channel location. If the data contains events in the cfg
% structure, it writes the events in the MNE format (three columns) into a file
% based on "filename", ending with "-eve.fif"
%
% See also FT_DATATYPE_RAW, FT_DATATYPE_TIMELOCK
% Copyright (C) 2012-2013, Jan-Mathijs Schoffelen, Gio Piantoni
%
% This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
% for the documentation and details.
%
% FieldTrip 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.
%
% FieldTrip 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 FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% this ensures that the path is correct and that the ft_defaults global variable is available
ft_defaults
% ensure that the filename has the correct extension
[pathstr, name, ext] = fileparts(filename);
if ~strcmp(ext, '.fif')
error('if the filename is specified with extension, this should read .fif');
end
fifffile = [pathstr filesep name '.fif'];
eventfile = [pathstr filesep name '-eve.fif'];
% ensure the mne-toolbox to be on the path
ft_hastoolbox('mne', 1);
% check if the input data is valid for this function
data = ft_checkdata(data, 'datatype', {'raw', 'timelock'}, 'feedback', 'yes');
istlck = ft_datatype(data, 'timelock');
isepch = ft_datatype(data, 'raw');
israw = false;
if isepch && numel(data.trial) == 1
isepch = false;
israw = true;
end
% Create a fiff-header, or take it from the original header if possible
if ft_senstype(data, 'neuromag') && isfield(data, 'hdr')
fprintf('Using the original FIFF header, but channel locations are read \nfrom .grad and .elec in the data, if they exist\n')
info = data.hdr.orig;
else
info.meas_id.version = NaN;
info.meas_id.machid = [NaN;NaN];
info.meas_id.secs = NaN;
info.meas_id.usecs = NaN;
info.meas_date = [NaN;NaN];
info.acq_pars = []; % needed by raw
info.acq_stim = []; % needed by raw
info.highpass = NaN;
info.lowpass = NaN;
% no strictly necessary, but the inverse functions in MNE works better if
% this matrix is present
info.dev_head_t.from = 1;
info.dev_head_t.to = 4;
info.dev_head_t.trans = eye(4);
info.ctf_head_t = [];
info.dig = [];
info.projs = struct('kind', {}, 'active', {}, 'desc', {}, 'data', {});
info.comps = struct('ctfkind', {}, 'kind', {}, 'save_calibrated', {}, ...
'rowcals', {}, 'colcals', {}, 'data', {});
info.bads = [];
if isepch
info.sfreq = 1./mean(diff(data.time{1}));
info.isaverage = 0;
info.isepoched = 1;
info.iscontinuous = 0;
elseif istlck
info.sfreq = 1./mean(diff(data.time));
info.isaverage = 1;
info.isepoched = 0;
info.iscontinuous = 0;
end
end
if israw
info.sfreq = data.fsample;
elseif isepch
info.sfreq = 1 ./ mean(diff(data.time{1}));
elseif istlck
info.sfreq = 1 ./ mean(diff(data.time));
end
info.ch_names = data.label(:)';
info.chs = sens2fiff(data);
info.nchan = numel(data.label);
if israw
[outfid, cals] = fiff_start_writing_raw(fifffile, info);
fiff_write_raw_buffer(outfid, data.trial{1}, cals);
fiff_finish_writing_raw(outfid);
% write events, if they exists
if isfield(data, 'cfg')
event = ft_findcfg(data.cfg, 'event');
else
event = [];
end
if ~isempty(event)
eve = convertevent(event);
mne_write_events(eventfile, eve);
fprintf('Writing events to %s\n', eventfile)
end
elseif isepch
error('fieldtrip2fiff:NotImplementedError', 'Function to write epochs to MNE not implemented yet')
for j = 1:length(data.trial)
evoked(j).aspect_kind = 100;
evoked(j).is_smsh = 0; % FIXME: How could we tell?
evoked(j).nave = 1; % FIXME: Use the real value
evoked(j).first = round(data.time{j}(1)*info.sfreq);
evoked(j).last = round(data.time{j}(end)*info.sfreq);
evoked(j).times = data.time{j};
evoked(j).comment = sprintf('FieldTrip data, category/trial %d', j);
evoked(j).epochs = data.trial{j};
end
% fiffdata.info = info;
% fiffdata.evoked = evoked;
% fiff_write_XXX(fifffile, fiffdata);
elseif istlck
evoked.aspect_kind = 100;
evoked.is_smsh = 0;
evoked.nave = max(data.dof(:));
evoked.first = round(data.time(1)*info.sfreq);
evoked.last = round(data.time(end)*info.sfreq);
evoked.times = data.time;
evoked.comment = sprintf('FieldTrip data averaged');
evoked.epochs = data.avg;
fiffdata.info = info;
fiffdata.evoked = evoked;
fiff_write_evoked(fifffile, fiffdata);
end
%-------------------
% subfunction
function [chs] = sens2fiff(data)
% use orig information if available
if isfield(data, 'hdr') && isfield(data.hdr, 'orig') && ...
isfield(data.hdr.orig, 'chs')
[dummy, i_label, i_chs] = intersect(data.label, {data.hdr.orig.chs.ch_name});
chs(i_label) = data.hdr.orig.chs(i_chs);
return
end
% otherwise reconstruct it
fprintf('Reconstructing channel locations, it might be inaccurate\n')
FIFF = fiff_define_constants; % some constants are not defined in the MATLAB function
if isfield(data, 'grad')
hasgrad = true;
else
hasgrad = false;
end
if isfield(data, 'elec')
haselec = true;
elec = ft_convert_units(data.elec, 'cm'); % that MNE uses cm
else
haselec = false;
end
cnt_grad = 0;
cnt_elec = 0;
cnt_else = 0;
for k = 1:numel(data.label)
% create a struct for each channel
chs(1,k).scanno = k;
chs(1,k).ch_name = data.label{k};
chs(1,k).range = 1;
chs(1,k).cal = 1;
i_grad = false;
i_elec = false;
if hasgrad
i_grad = strcmp(data.grad.label, data.label{k});
elseif haselec
i_elec = strcmp(elec.label, data.label{k});
end
if any(i_grad)
chs(1,k).kind = FIFF.FIFFV_MEG_CH;
cnt_grad = cnt_grad + 1;
chs(1,k).logno = cnt_grad;
switch data.grad.chantype{i_grad}
case 'megmag'
chs(1,k).coil_type = 3024;
chs(1,k).unit = FIFF.FIFF_UNIT_T;
case 'megplanar'
chs(1,k).coil_type = 3012;
chs(1,k).unit = FIFF.FIFF_UNIT_T_M;
case 'meggrad'
chs(1,k).coil_type = 3022;
chs(1,k).unit = FIFF.FIFF_UNIT_T;
otherwise
fprintf('Unknown channel type %s, assigned to meggrad', data.grad.chantype{i_grad})
chs(1,k).coil_type = 3022;
chs(1,k).unit = FIFF.FIFF_UNIT_T;
end
chs(1,k).coil_trans = eye(4);
chs(1,k).unit_mul = 0;
chs(1,k).coord_frame = FIFF.FIFFV_COORD_HEAD;
chs(1,k).eeg_loc = [];
chs(1,k).loc = [data.grad.chanpos(i_grad,:)'; reshape(eye(3),[9 1])];
elseif any(i_elec)
chs(1,k).kind = FIFF.FIFFV_EEG_CH;
cnt_elec = cnt_elec + 1;
chs(1,k).logno = cnt_elec;
chs(1,k).coil_type = NaN;
chs(1,k).coil_trans = [];
chs(1,k).unit = 107; % volts FIFF.FIFF_UNIT_V
chs(1,k).unit_mul = -6; % micro FIFF.FIFF_UNITM_MU
chs(1,k).coord_frame = FIFF.FIFFV_COORD_DEVICE;
chs(1,k).eeg_loc = [elec.chanpos(i_elec,:)' zeros(3,1)] / 100;
chs(1,k).loc = [chs(1,k).eeg_loc(:); 0; 1; 0; 0; 0; 1];
else
chs(1,k).kind = NaN;
cnt_else = cnt_else + 1;
chs(1,k).logno = cnt_else;
chs(1,k).coil_type = NaN;
chs(1,k).coil_trans = [];
chs(1,k).unit = NaN;
chs(1,k).unit_mul = 0;
chs(1,k).coord_frame = NaN;
chs(1,k).eeg_loc = [];
chs(1,k).loc = zeros(12,1);
end
end
function eve = convertevent(event)
% tentative code, with lots of assumption
%CTF should use backpanel trigger
backpanel = strcmp({event.type}, 'backpanel trigger');
if any(backpanel)
fprintf('Writing the value of the backpanel trigger into the event file\n')
trigger = [event(backpanel).value];
eve = zeros(numel(trigger), 3);
eve(:,1) = [event(backpanel).sample];
eve(:,3) = [event(backpanel).value];
return
end
% use ev_type and ev_value
ev_type = unique({event.type});
% convert to cell of strings
if any(cellfun(@isnumeric, {event.value}))
event_value = cellfun(@num2str, {event.value}, 'uni', false);
else
event_value = {event.value};
end
ev_value = unique(event_value);
eve = zeros(numel(event), 3);
for i1 = 1:numel(ev_type)
for i2 = 1:numel(ev_value)
i_type = strcmp({event.type}, ev_type{i1});
i_value = strcmp(event_value, ev_value{i2});
marker = i1 * 10 + i2;
if any(i_type & i_value)
eve(i_type & i_value, 1) = [event(i_type & i_value).sample];
eve(i_type & i_value, 2) = marker;
end
end
end
% report event coding
newev = unique(eve(:,2));
fprintf('EVENTS have been coded as:\n')
for i = 1:numel(newev)
i_type = floor(newev(i)/10);
i_value = mod(newev(i), 10);
fprintf('type: %s, value %s -> % 3d\n', ev_type{i_type}, ev_value{i_value}, newev(i))
end