/
ft_datatype.m
301 lines (277 loc) · 10.4 KB
/
ft_datatype.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
function [type, dimord] = ft_datatype(data, desired)
% FT_DATATYPE determines the type of data represented in a FieldTrip data structure
% and returns a string with raw, freq, timelock source, comp, spike, source, volume,
% dip, montage, event.
%
% Use as
% [type, dimord] = ft_datatype(data)
% [bool] = ft_datatype(data, desired)
%
% See also FT_DATATYPE_COMP, FT_DATATYPE_FREQ, FT_DATATYPE_MVAR,
% FT_DATATYPE_SEGMENTATION, FT_DATATYPE_PARCELLATION, FT_DATATYPE_SOURCE,
% FT_DATATYPE_TIMELOCK, FT_DATATYPE_DIP, FT_DATATYPE_HEADMODEL,
% FT_DATATYPE_RAW, FT_DATATYPE_SENS, FT_DATATYPE_SPIKE, FT_DATATYPE_VOLUME
% Copyright (C) 2008-2022, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% 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$
if nargin<2
desired = [];
end
% determine the type of input data
israw = isfield(data, 'label') && isfield(data, 'time') && isa(data.time, 'cell') && isfield(data, 'trial') && isa(data.trial, 'cell') && ~isfield(data,'trialtime');
isfreq = ((isfield(data, 'label') && ~isfield(data, 'pos')) || isfield(data, 'labelcmb')) && isfield(data, 'freq') && ~isfield(data,'trialtime') && ~isfield(data,'origtrial'); %&& (isfield(data, 'powspctrm') || isfield(data, 'crsspctrm') || isfield(data, 'cohspctrm') || isfield(data, 'fourierspctrm') || isfield(data, 'powcovspctrm'));
istimelock = isfield(data, 'label') && isfield(data, 'time') && ~isfield(data, 'freq') && ~isfield(data,'timestamp') && ~isfield(data,'trialtime') && ~(isfield(data, 'trial') && iscell(data.trial)) && ~isfield(data, 'pos'); %&& ((isfield(data, 'avg') && isnumeric(data.avg)) || (isfield(data, 'trial') && isnumeric(data.trial) || (isfield(data, 'cov') && isnumeric(data.cov))));
iscomp = isfield(data, 'label') && isfield(data, 'topo') || isfield(data, 'topolabel');
isvolume = isfield(data, 'transform') && isfield(data, 'dim') && ~isfield(data, 'pos');
issource = (isfield(data, 'pos') || isfield(data, 'pnt')) && isstruct(data) && numel(data)==1; % pnt is deprecated, this does not apply to a mesh array
ismesh = (isfield(data, 'pos') || isfield(data, 'pnt')) && (isfield(data, 'tri') || isfield(data, 'tet') || isfield(data, 'hex')); % pnt is deprecated
isdip = isfield(data, 'dip');
ismvar = isfield(data, 'dimord') && ~isempty(strfind(data.dimord, 'lag'));
isfreqmvar = isfield(data, 'freq') && isfield(data, 'transfer');
ischan = check_chan(data);
issegmentation = check_segmentation(data);
isparcellation = check_parcellation(data);
ismontage = isfield(data, 'labelold') && isfield(data, 'labelnew') && isfield(data, 'tra');
isevent = isfield(data, 'type') && isfield(data, 'value') && isfield(data, 'sample') && isfield(data, 'offset') && isfield(data, 'duration');
isheadmodel = isfield(data, 'type') && ischar(data.type) && ismember(data.type, {'bemcp', 'concentricspheres', 'dipoli', 'duneuro', 'fns', 'halfspace', 'halfspace_monopole', 'infinite', 'infinite_currentdipole', 'infinite_magneticdipole', 'infinite_monopole', 'interpolate', 'localspheres', 'openmeeg', 'simbio', 'singleshell', 'singlesphere', 'slab_monopole'});
islayout = all(isfield(data, {'label', 'pos', 'width', 'height'})); % mask and outline are optional
if issource && isstruct(data) && numel(data)>1
% this applies to struct arrays with meshes, i.e. with a pnt+tri
issource = false;
end
if ~isfreq
% this applies to a freq structure from 2003 up to early 2006
isfreq = all(isfield(data, {'foi', 'label', 'dimord'})) && ~isempty(strfind(data.dimord, 'frq'));
end
% check if it is a spike structure
spk_hastimestamp = isfield(data,'label') && isfield(data, 'timestamp') && isa(data.timestamp, 'cell');
spk_hastrials = isfield(data,'label') && isfield(data, 'time') && isa(data.time, 'cell') && isfield(data, 'trial') && isa(data.trial, 'cell') && isfield(data, 'trialtime') && isa(data.trialtime, 'numeric');
spk_hasorig = isfield(data,'origtrial') && isfield(data,'origtime'); % for compatibility
isspike = isfield(data, 'label') && (spk_hastimestamp || spk_hastrials || spk_hasorig);
% check if it is a sensor array
isgrad = isfield(data, 'label') && isfield(data, 'coilpos') && isfield(data, 'coilori');
iselec = isfield(data, 'label') && isfield(data, 'elecpos');
isopto = isfield(data, 'label') && isfield(data, 'optopos');
if isspike
type = 'spike';
elseif israw && iscomp
type = 'raw+comp';
elseif istimelock && iscomp
type = 'timelock+comp';
elseif isfreq && iscomp
type = 'freq+comp';
elseif israw
type = 'raw';
elseif iscomp
type = 'comp';
elseif isfreqmvar
% freqmvar should go before freq
type = 'freqmvar';
elseif isfreq
type = 'freq';
elseif ismvar
type = 'mvar';
elseif isdip
% this should go before timelock
type = 'dip';
elseif istimelock
type = 'timelock';
elseif isvolume && issegmentation
type = 'volume+label';
elseif isvolume
type = 'volume';
elseif ismesh && isparcellation
type = 'mesh+label';
elseif islayout
% this should go before source
type = 'layout';
elseif issource && isparcellation
type = 'source+label';
elseif issource && ismesh
type = 'source+mesh';
elseif ismesh
type = 'mesh';
elseif issource
type = 'source';
elseif ischan
% this results from avgovertime/avgoverfreq after timelockstatistics or freqstatistics
type = 'chan';
elseif isgrad
type = 'grad';
elseif iselec
type = 'elec';
elseif isopto
type = 'opto';
elseif ismontage
type = 'montage';
elseif isevent
type = 'event';
elseif isheadmodel
type = 'headmodel';
else
type = 'unknown';
end
if nargin>1
% return a boolean value
switch desired
case 'raw'
type = any(strcmp(type, {'raw', 'raw+comp'}));
case 'timelock'
type = any(strcmp(type, {'timelock', 'timelock+comp'}));
case 'freq'
type = any(strcmp(type, {'freq', 'freq+comp'}));
case 'comp'
type = any(strcmp(type, {'comp', 'raw+comp', 'timelock+comp', 'freq+comp'}));
case 'volume'
type = any(strcmp(type, {'volume', 'volume+label'}));
case 'source'
type = any(strcmp(type, {'source', 'source+label', 'mesh', 'mesh+label', 'source+mesh'})); % a single mesh does qualify as source structure
type = type && isstruct(data) && numel(data)==1; % an array of meshes does not qualify
case 'mesh'
type = any(strcmp(type, {'mesh', 'mesh+label', 'source+mesh'}));
case 'segmentation'
type = any(strcmp(type, {'segmentation', 'volume+label'}));
case 'parcellation'
type = any(strcmp(type, {'parcellation', 'source+label' 'mesh+label'}));
case 'sens'
type = any(strcmp(type, {'grad', 'elec', 'opto'}));
otherwise
type = strcmp(type, desired);
end % switch
end
if nargout>1
% FIXME this should be replaced with getdimord in the calling code
% also return the dimord of the input data
if isfield(data, 'dimord')
dimord = data.dimord;
else
dimord = 'unknown';
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [res] = check_chan(data)
if ~isstruct(data) || any(isfield(data, {'time', 'freq', 'pos', 'dim', 'transform'}))
res = false;
elseif isfield(data, 'dimord') && any(strcmp(data.dimord, {'chan', 'chan_chan'}))
res = true;
else
res = false;
fn = fieldnames(data);
for i=1:numel(fn)
if isfield(data, [fn{i} 'dimord']) && any(strcmp(data.([fn{i} 'dimord']), {'chan', 'chan_chan'}))
res = true;
break;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [res] = check_segmentation(volume)
res = false;
if ~isfield(volume, 'dim') && ~isfield(volume, 'transform')
return
end
if isfield(volume, 'pos')
return
end
if any(isfield(volume, {'seg', 'csf', 'white', 'gray', 'skull', 'scalp', 'brain'}))
res = true;
return
end
fn = fieldnames(volume);
isboolean = [];
cnt = 0;
for i=1:length(fn)
if isfield(volume, [fn{i} 'label'])
res = true;
return
else
if (islogical(volume.(fn{i})) || isnumeric(volume.(fn{i}))) && isequal(size(volume.(fn{i})),volume.dim)
cnt = cnt+1;
if islogical(volume.(fn{i}))
isboolean(cnt) = true;
else
isboolean(cnt) = false;
end
end
end
end
if ~isempty(isboolean)
res = all(isboolean);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [res] = check_parcellation(source)
res = false;
if numel(source)>1
% this applies to struct arrays with meshes, i.e. with a pnt+tri
return
end
if ~isfield(source, 'pos')
return
end
fn = fieldnames(source);
fb = false(size(fn));
npos = size(source.pos,1);
for i=1:numel(fn)
% for each of the fields check whether it might be a logical array with the size of the number of sources
tmp = source.(fn{i});
fb(i) = numel(tmp)==npos && islogical(tmp);
end
if sum(fb)>1
% the presence of multiple logical arrays suggests it is a parcellation
res = true;
end
if res == false % check if source has more D elements
check = 0;
for i = 1: length(fn)
fname = fn{i};
switch fname
case 'tri'
npos = size(source.tri,1);
check = 1;
case 'hex'
npos = size(source.hex,1);
check = 1;
case 'tet'
npos = size(source.tet,1);
check = 1;
end
end
if check == 1 % check if elements are labelled
for i=1:numel(fn)
tmp = source.(fn{i});
fb(i) = numel(tmp)==npos && islogical(tmp);
end
if sum(fb)>1
res = true;
end
end
end
fn = fieldnames(source);
for i=1:length(fn)
if isfield(source, [fn{i} 'label']) && isnumeric(source.(fn{i}))
res = true;
return
end
end