/
fixdimord.m
212 lines (183 loc) · 7.16 KB
/
fixdimord.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
function [data] = fixdimord(data)
% FIXDIMORD ensures consistency between the dimord string and the axes
% that describe the data dimensions. The main purpose of this function
% is to ensure backward compatibility of all functions with data that has
% been processed by older FieldTrip versions.
%
% Use as
% [data] = fixdimord(data)
% This will modify the data.dimord field to ensure consistency.
% The name of the axis is the same as the name of the dimord, i.e. if
% dimord='freq_time', then data.freq and data.time should be present.
%
% The default dimensions in the data are described by
% 'time'
% 'freq'
% 'chan'
% 'chancmb'
% 'refchan'
% 'subj'
% 'rpt'
% 'rpttap'
% 'pos'
% 'ori'
% 'rgb'
% 'comp'
% 'voxel'
% Copyright (C) 2009-2014, Robert Oostenveld, Jan-Mathijs Schoffelen
%
% 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, keepsourcedimord = 0; end
%
% if any(ft_datatype(data, {'source', 'volume'})) && isfield(data, 'dimord') && ~keepsourcedimord
% % the old source data representation does not have a dimord, whereas the new source data representation does have a dimord
% ft_warning(sprintf('removing dimord "%s" from source representation data', data.dimord));
% data = rmfield(data, 'dimord');
% return
% else
% % it is ok
% return
% end
if ~isfield(data, 'dimord')
if ft_datatype(data, 'raw')
% it is raw data, which does not have a dimord -> this is ok
return
elseif ft_datatype(data, 'comp')
% it is component data, which resembles raw data -> this is ok
return
elseif ft_datatype(data, 'volume')
% it is volume data, which does not have a dimord -> this is ok
return
elseif ft_datatype(data, 'source') || ft_datatype(data, 'parcellation')
% it is old-style source data -> this is ok
return
else
% find the XXXdimord fields
fn = fieldnames(data);
sel = true(size(fn));
for i=1:length(fn)
sel(i) = ~isempty(strfind(fn{i}, 'dimord'));
end
df = fn(sel);
% use this function recursively on the XXXdimord fields
for i=1:length(df)
data.dimord = data.(df{i});
data = fixdimord(data);
data.(df{i}) = data.dimord;
data = rmfield(data, 'dimord');
end
% after the recursive call it should be ok
return
end
end % if no dimord
if strcmp(data.dimord, 'voxel')
% this means that it is position
data.dimord = 'pos';
end
dimtok = tokenize(data.dimord, '_');
if strncmp('{pos_pos}', data.dimord, 9)
% keep these together for bivariate source structures
dimtok = {'{pos_pos}', dimtok{3:end}};
end
for i=1:length(dimtok)
switch dimtok{i}
case {'tim' 'time' 'toi' 'latency'}
dimtok{i} = 'time';
case {'frq' 'freq' 'foi' 'frequency'}
dimtok{i} = 'freq';
case {'sgn' 'label' 'chan'}
dimtok{i} = 'chan';
case {'rpt' 'trial'}
dimtok{i} = 'rpt';
case {'subj' 'subject'}
dimtok{i} = 'subj';
case {'comp'}
% don't change, it is ok
case {'sgncmb' 'labelcmb' 'chancmb'}
dimtok{i} = 'chancmb';
case {'rpttap'}
% this is a 2D field, coding trials and tapers along the same dimension
% don't change, it is ok
case {'refchan'}
% don't change, it is ok
case {'ori'}
% don't change, it is ok
case {'rgb'}
% don't change, it is ok
case {'voxel' 'vox' 'repl' 'wcond'}
% these are used in some FieldTrip functions, but are not considered standard
ft_warning('unexpected dimord "%s"', data.dimord);
case {'pos'}
% this is for source data on a 3D grid, a cortical sheet, or unstructured positions
case {'{pos}'}
% this is for source data on a 3D grid, a cortical sheet, or unstructured positions
% the data itself is represented in a cell-array, e.g. source.mom or source.leadfield
case {'{pos_pos}'}
% this is for bivariate source data on a 3D grid, a cortical sheet, or unstructured positions
otherwise
ft_error('unexpected dimord "%s"', data.dimord);
end % switch dimtok
end % for length dimtok
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isfield(data, 'tim'), data.time = data.tim ; data = rmfield(data, 'tim') ; end
if isfield(data, 'toi'), data.time = data.toi ; data = rmfield(data, 'toi') ; end
if isfield(data, 'latency'), data.time = data.latency ; data = rmfield(data, 'latency') ; end
if isfield(data, 'frq'), data.freq = data.frq ; data = rmfield(data, 'frq') ; end
if isfield(data, 'foi'), data.freq = data.foi ; data = rmfield(data, 'foi') ; end
if isfield(data, 'frequency'), data.freq = data.frequency ; data = rmfield(data, 'frequency') ; end
if isfield(data, 'sgn'), data.label = data.sgn ; data = rmfield(data, 'sgn') ; end
if isfield(data, 'chan'), data.label = data.chan ; data = rmfield(data, 'chan') ; end
% if isfield(data, 'trial'), data.rpt = data.trial ; data = rmfield(data, 'trial') ; end % DO NOT CONVERT -> this is an exception
if isfield(data, 'subject'), data.subj = data.subject ; data = rmfield(data, 'subject') ; end
if isfield(data, 'sgncmb'), data.labelcmb = data.sgncmb ; data = rmfield(data, 'sgncmb') ; end
if isfield(data, 'chancmb'), data.labelcmb = data.chancmb ; data = rmfield(data, 'chancmb') ; end
% ensure that it is a column
if isfield(data, 'label')
data.label = data.label(:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% if isfield(data, 'trial')
% mat = data.trial;
% elseif isfield(data, 'individual')
% mat = data.individual;
% elseif isfield(data, 'avg')
% mat = data.avg;
% elseif isfield(data, 'crsspctrm')
% mat = data.crsspctrm;
% elseif isfield(data, 'powspctrm')
% mat = data.powspctrm;
% elseif isfield(data, 'fourierspctrm')
% mat = data.fourierspctrm;
% end
%
% add the descriptive axis for each dimension
% for i=1:length(dimtok)
% if isfield(data, dimtok{i})
% % the dimension is already described with its own axis
% % data = setfield(data, dimtok{i}, getfield(data, dimtok{i}));
% else
% % add an axis to the output data
% data = setfield(data, dimtok{i}, 1:size(mat,i));
% end
% end
% undo the tokenization
data.dimord = dimtok{1};
for i=2:length(dimtok)
data.dimord = [data.dimord '_' dimtok{i}];
end