-
Notifications
You must be signed in to change notification settings - Fork 7
/
bf_sources_voi.m
149 lines (120 loc) · 3.99 KB
/
bf_sources_voi.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
function voi = bf_sources_voi(BF, S)
% Generate a set of VOIs specified in MNI coordinates
% Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging
% $Id$
%--------------------------------------------------------------------------
if nargin == 0
label = cfg_entry;
label.tag = 'label';
label.name = 'Label';
label.strtype = 's';
label.help = {'Label for the VOI'};
pos = cfg_entry;
pos.tag = 'pos';
pos.name = 'MNI coordinates';
pos.strtype = 'r';
pos.num = [1 3];
pos.help = {'Locations for the VOI in MNI coordinates'};
pos.val = {};
ori = cfg_entry;
ori.tag = 'ori';
ori.name = 'Orientation';
ori.strtype = 'r';
ori.num = [1 3];
ori.help = {'Source orientatons (only for single points, leave zeros for unoriented)'};
ori.val = {[0 0 0]};
voidef = cfg_branch;
voidef.tag = 'voidef';
voidef.name = 'VOI';
voidef.val = {label, pos, ori};
mask = cfg_files;
mask.tag = 'mask';
mask.name = 'MNI mask';
mask.filter = 'image';
mask.ufilter = '.*';
mask.num = [1 1];
mask.help = {'Select a mask image'};
maskdef = cfg_branch;
maskdef.tag = 'maskdef';
maskdef.name = 'Mask VOI';
maskdef.val = {label, mask};
vois = cfg_repeat;
vois.tag = 'vois';
vois.name = 'VOIs';
vois.num = [1 Inf];
vois.values = {voidef, maskdef};
vois.val = {voidef};
radius = cfg_entry;
radius.tag = 'radius';
radius.name = 'Radius';
radius.strtype = 'r';
radius.num = [1 1];
radius.val = {0};
radius.help = {'Radius (in mm) for the VOIs (leave 0 for single point)'};
resolution = cfg_entry;
resolution.tag = 'resolution';
resolution.name = 'Resolution';
resolution.strtype = 'r';
resolution.num = [1 1];
resolution.val = {5};
resolution.help = {'Resolution for placing grid points in each VOI (in mm)'};
voi = cfg_branch;
voi.tag = 'voi';
voi.name = 'VOIs in MNI space';
voi.val = {vois, radius, resolution};
return
elseif nargin < 2
error('Two input arguments are required');
end
iskull = export(gifti(BF.data.mesh.tess_iskull), 'ft');
M1 = BF.data.transforms.toNative;
M1 = BF.data.transforms.toMNI/M1;
iskull = ft_determine_units(ft_transform_geometry(M1, iskull));
% transform MNI coords in MNI space into space where we are doing the
% beamforming
M = inv(BF.data.transforms.toMNI);
if S.radius > 0
vec = -S.radius:S.resolution:S.radius;
[X, Y, Z] = ndgrid(vec, vec, vec);
sphere = [X(:) Y(:) Z(:)];
sphere(sqrt(X(:).^2 + Y(:).^2 + Z(:).^2) > S.radius, :) = [];
npnt = size(sphere, 1);
else
sphere = 0;
npnt = 1;
end
grid = bf_sources_grid(BF, struct('resolution', S.resolution, 'space', 'MNI template'));
mnigrid = ft_transform_geometry(BF.data.transforms.toMNI, grid);
nvoi = numel(S.vois);
voi = [];
voi.label = {};
voi.pos = [];
ori = [];
voi.pos2voi = [];
for i = 1:nvoi
switch char(fieldnames(S.vois{i}))
case 'voidef'
voi.label{i} = S.vois{i}.voidef.label;
voi.pos = [voi.pos; sphere+repmat(S.vois{i}.voidef.pos, npnt, 1)];
ori = [ori; S.vois{i}.voidef.ori];
voi.pos2voi = [voi.pos2voi i*ones(1, npnt)];
case 'maskdef'
voi.label{i} = S.vois{i}.maskdef.label;
V = spm_vol(char(S.vois{i}.maskdef.mask));
vox = spm_eeg_inv_transform_points(inv(V.mat), mnigrid.pos);
Y = spm_sample_vol(V, vox(:, 1), vox(:, 2), vox(:, 3), 0);
ind = find(~isnan(Y) & abs(Y)>0);
voi.pos = [voi.pos; mnigrid.pos(ind, :)];
ori = [ori;zeros(length(ind), 3)];
voi.pos2voi = [voi.pos2voi i*ones(1, length(ind))];
end
end
voi.label = voi.label(:);
% Remove points outside the brain
inside = ft_inside_headmodel(voi.pos, struct('bnd', iskull));
voi.pos(~inside, :) = [];
voi.pos2voi(~inside) = [];
if any(any(ori))
voi.ori = ori(inside, :);
end
voi = ft_transform_geometry(M, voi);