-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcosmo_meeg_find_layout.m
253 lines (227 loc) · 9.59 KB
/
cosmo_meeg_find_layout.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
function layout=cosmo_meeg_find_layout(ds, varargin)
% finds an MEEG channel layout associated with a dataset (edited to fix using wrong layout)
%
% layout=cosmo_meeg_find_layout(ds, varargin)
%
% Inputs:
% 'chantype', ct string indicating the channel type (if the dataset
% has channel labels that allow for different types of
% channels. Depending on the dataset, possible options
% are:
% - 'meg_planar' pairs of planar MEG
% - 'meg_axial' axial MEG
% - 'meg_planar_combined' combined planar MEG
% - 'meg_combined_from_planar' pairs of planar MEG [*]
% - 'eeg' eeg channels
%
% Output:
% layout MEG channel layout with fields
% .pos Nx2 x and y coordinates (for N channels)
% .width Nx1 channel widths
% .height Nx1 channel heights
% .label Nx1 cell with channel labels
% .name string with name of layout
% [*] when chantype is set to
% 'meg_combined_from_planar', layout also contains a
% field .parent which is a layout in itself for the
% 'meg_planar_combined channels'. In that case,
% layout.parent has the .pos, .width, .height, .label
% and .name fields (all of size Mx1 or Mx2 for M
% planar-combined channels, and in addition a cell
% .child_label (of size Mx1) which contains the
% channel labels in layout.name.
%
% Examples:
% % (This example requires FieldTrip)
% cosmo_skip_test_if_no_external('fieldtrip');
% %
% % generate neuromag306 dataset
% ds=cosmo_synthetic_dataset('type','meeg','sens','neuromag306_all');
% % get layout for the planar channels
% pl_layout=cosmo_meeg_find_layout(ds,'chantype','meg_planar');
% cosmo_disp(pl_layout.label)
% %|| { 'MEG0113'
% %|| 'MEG0112'
% %|| 'MEG0122'
% %|| :
% %|| 'MEG2643'
% %|| 'COMNT'
% %|| 'SCALE' }@206x1
% cosmo_disp([pl_layout.pos pl_layout.width pl_layout.height])
% %|| [ -0.408 0.253 0.0358 0.0369
% %|| -0.408 0.284 0.0358 0.0369
% %|| -0.328 0.285 0.0358 0.0369
% %|| : : : :
% %|| 0.373 -0.0821 0.0358 0.0369
% %|| -0.547 -0.5 0.0358 0.0369
% %|| 0.547 -0.5 0.0358 0.0369 ]@206x4
% pl_layout.name
% %|| 'neuromag306planar.lay'
% %
% % get layout for axial (magnetometer) channels
% mag_layout=cosmo_meeg_find_layout(ds,'chantype','meg_axial');
% cosmo_disp(mag_layout.label);
% %|| { 'MEG0111'
% %|| 'MEG0121'
% %|| 'MEG0131'
% %|| :
% %|| 'MEG2641'
% %|| 'COMNT'
% %|| 'SCALE' }@104x1
% %
% % get layout for planar channels, but add a 'parent' layout which has the
% % combined_planar channels
% combined_from_planar_layout=cosmo_meeg_find_layout(ds,'chantype',...
% 'meg_combined_from_planar');
% cosmo_disp(combined_from_planar_layout.label);
% %|| { 'MEG0113'
% %|| 'MEG0112'
% %|| 'MEG0122'
% %|| :
% %|| 'MEG2643'
% %|| 'COMNT'
% %|| 'SCALE' }@206x1
% cosmo_disp(combined_from_planar_layout.parent.label);
% %|| { 'MEG0112+0113'
% %|| 'MEG0122+0123'
% %|| 'MEG0132+0133'
% %|| :
% %|| 'MEG2642+2643'
% %|| 'COMNT'
% %|| 'SCALE' }@104x1
% cosmo_disp(combined_from_planar_layout.parent.child_label);
% %|| { { 'MEG0112'
% %|| 'MEG0113' }
% %|| { 'MEG0122'
% %|| 'MEG0123' }
% %|| { 'MEG0132'
% %|| 'MEG0133' }
% %|| :
% %|| { 'MEG2642'
% %|| 'MEG2643' }
% %|| { }
% %|| { } }@104x1
%
% See also: ft_prepare_neighbors, cosmo_meeg_chan_neighbors,
% cosmo_meeg_chan_neighborhood
%
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #
defaults.chantype=[];
defaults.min_coverage=.2;
opt=cosmo_structjoin(defaults, varargin);
% get the sensor labels and channel types applicable to this dataset
chantype2senstype=get_dataset_senstypes(ds,opt);
% get the single channel type based on the input and available channels
ds_chantype=get_base_chantype(chantype2senstype,opt);
% get the sensor type corresponding with the channel type
senstype=chantype2senstype.(ds_chantype);
% use the mapping from senstype to layout to get the layout
senstype2layout=cosmo_meeg_senstype2layout_mapping();
layout=senstype2layout.(senstype);
% ensure that enough labels are covered in the dataset
ds_label=get_dataset_channel_label(ds);
coverage=label_coverage(ds_label(:),layout.label(:));
% no coverage, throw an error
if coverage<opt.min_coverage
error(['channel coverage %.1f%% < 100%% for %s. This means '...
'that the channel layout was not recognized based '...
'on the channel labels. Your options are:\n '...
'1) if this is a custom layout, define a neighbor '...
'struct with fields .label and neighblabel (similar '....
'to CoSMoMVPA''s ft_meeg_chan_neighbors and '...
'FieldTrip''s ft_prepare_neighbors), and provide this '...
'as the only input to cosmo_meeg_chan_neighborhood\n'...
'2) if this is a common MEG or EEG layout, please get '...
'in touch with the CoSMoMVPA developers to see if '...
'support for this layout can be added'], ...
100*coverage, ds_chantype);
end
% in case of planar channels with senstype set to
% meg_combined_from_planar, add a parent layout that maps to the
% original layout
is_planar_layout=isempty(cosmo_strsplit(ds_chantype,'_planar',-1));
if is_planar_layout && strcmp(opt.chantype,'meg_combined_from_planar')
parent_senstype=chantype2senstype.meg_combined_from_planar;
parent_layout=senstype2layout.(parent_senstype);
sens_label=get_senstype_label(senstype);
parent_sens_label=get_senstype_label(parent_senstype);
nlabels=numel(parent_layout.label);
parent_layout.child_label=cell(nlabels,1);
all_child_labels=cell(nlabels,1);
for k=1:nlabels
i=find(cosmo_match(parent_sens_label,parent_layout.label(k)));
if numel(i)~=1
parent_layout.child_label{k}=cell(0,1);
end
child_label=sort(sens_label(i,:));
all_child_labels{k}=child_label(:);
parent_layout.child_label{k}=child_label(:);
end
% sanity check
assert(cosmo_overlap({sens_label(:)},...
{cat(1,all_child_labels{:})})==1);
layout.parent=parent_layout;
end
function label=get_senstype_label(senstype)
sc=cosmo_meeg_senstype_collection();
label=sc.(senstype).label;
function senstype_mapping=get_dataset_senstypes(ds,opt)
% helper function to get supported senstypes
[unused,senstype_mapping]=cosmo_meeg_chantype(ds,opt);
keys=fieldnames(senstype_mapping);
if cosmo_match({'meg_planar'},keys)
planar_senstype=senstype_mapping.meg_planar;
planar_combined_senstype=[planar_senstype '_combined'];
sc=cosmo_meeg_senstype_collection();
if isfield(sc,planar_combined_senstype)
senstype_mapping.meg_combined_from_planar=...
planar_combined_senstype;
end
end
function c=label_coverage(x,y)
% helper function to compute coverage of labels by a layout
c=mean(cosmo_match(x(:),y(:)));
function chan_labels=get_dataset_channel_label(ds)
% helper function to get labels from dataset
if iscellstr(ds)
chan_labels=ds;
else
[dim, index, attr_name, dim_name]=cosmo_dim_find(ds,'chan',true);
chan_labels=ds.a.(dim_name).values{index};
end
function chantype=get_base_chantype(chantype2senstype, opt)
% helper function to get the base (i.e. the one
% returned in layout) channel type.
% (this is not the parent type)
has_chantype=ischar(opt.chantype) && ~isempty(opt.chantype);
chantypes=fieldnames(chantype2senstype);
has_key=@(key)cosmo_match({key},chantypes);
error_msg='';
if has_chantype
if strcmp(opt.chantype,'meg_combined_from_planar')
if has_key('meg_planar')
chantype='meg_planar';
else
error_msg=sprintf(['Cannot use senstype ''%s'' because '...
'meg_planar not available'],...
opt.chantype);
end
elseif ~has_key(opt.chantype);
error_msg=sprintf('chantype ''%s'' is not supported',...
opt.chantype);
else
chantype=opt.chantype;
end
else
if numel(chantypes)>1
error_msg=['''chantype'' argument is not provided, but '...
'multiple types are available'];
else
chantype=chantypes{1};
end
end
if ~isempty(error_msg)
error('%s. Set the ''chantype'' argument to one of: ''%s''.',...
error_msg,cosmo_strjoin(chantypes, ''', '''));
end