-
Notifications
You must be signed in to change notification settings - Fork 7
/
bf_write_nifti.m
177 lines (145 loc) · 4.76 KB
/
bf_write_nifti.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
function res = bf_write_nifti(BF, S)
% Writes out nifti images of beamformer results
% Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak
% $Id$
%--------------------------------------------------------------------------
if nargin == 0
normalise = cfg_menu;
normalise.tag = 'normalise';
normalise.name = 'Global normalisation';
normalise.help = {'Normalise image values by the mean'};
normalise.labels = {
'No'
'Each image separately'
'Across images'
}';
normalise.values = {
'no'
'separate'
'all'
}';
normalise.val = {'no'};
space = cfg_menu;
space.tag = 'space';
space.name = 'Image space';
space.help = {'Specify image space'};
space.labels = {
'MNI'
'Native'
'MNI-aligned'
}';
space.values = {
'mni'
'native'
'aligned'
}';
space.val = {'mni'};
nifti = cfg_branch;
nifti.tag = 'nifti';
nifti.name = 'NIfTI';
nifti.val = {normalise, space};
res = nifti;
return
elseif nargin < 2
error('Two input arguments are required');
end
scale = ones(1, numel(BF.output.image));
switch S.normalise
case 'separate'
for i = 1:numel(BF.output.image)
val = BF.output.image(i).val;
scale(i) = 1./mean(abs(val(~isnan(val))));
end
case 'all'
val = spm_vec({BF.output.image(:).val});
scale = scale./mean(abs(val(~isnan(val))));
end
switch S.space
case 'mni'
sMRI = fullfile(spm('dir'), 'canonical', 'single_subj_T1.nii');
case 'aligned'
sMRI = fullfile(spm('dir'), 'canonical', 'single_subj_T1.nii');
case 'native'
sMRI = BF.data.mesh.sMRI;
end
[pth,nam,ext,num] = spm_fileparts(sMRI);
sMRI = fullfile(pth, [nam ext]);
if isfield(BF.sources, 'grid') || isfield(BF.sources, 'voi')
if isfield(BF.sources, 'grid')
sourcespace = 'grid';
source = BF.sources.grid;
source.pos = BF.sources.grid.allpos;
else
sourcespace = 'voi';
source = [];
source.pos = BF.sources.pos;
source.inside = 1:size(BF.sources.pos, 1);
source.outside = [];
end
switch S.space
case 'mni'
source = ft_transform_geometry(BF.data.transforms.toMNI, source);
case 'aligned'
source = ft_transform_geometry(BF.data.transforms.toMNI_aligned, source);
case 'native'
source = ft_transform_geometry(BF.data.transforms.toNative, source);
end
cfg = [];
cfg.parameter = 'pow';
cfg.downsample = 1;
cfg.showcallinfo = 'no';
elseif isfield(BF.sources, 'mesh')
sourcespace = 'mesh';
switch S.space
case 'mni'
source = BF.sources.mesh.canonical;
case 'aligned'
source = BF.sources.mesh.individual;
source.vert = spm_eeg_inv_transform_points(BF.data.transforms.toMNI_aligned, source.vert);
case 'native'
source = BF.sources.mesh.individual;
source.vert = spm_eeg_inv_transform_points(BF.data.transforms.toNative, source.vert);
end
source = export(gifti(source), 'patch');
else
error('Unsupported source space type');
end
outvol = spm_vol(sMRI);
outvol.dt(1) = spm_type('float32');
nimages = numel(BF.output.image);
spm('Pointer', 'Watch');drawnow;
spm_progress_bar('Init', nimages , 'Writing out images'); drawnow;
if nimages > 100, Ibar = floor(linspace(1, nimages ,100));
else Ibar = 1:nimages; end
for i = 1:nimages
outvol.fname= fullfile(pwd, [BF.output.image(i).label '.nii']);
outvol = spm_create_vol(outvol);
source.pow = scale(i)*BF.output.image(i).val;
source.pow = source.pow(:);
switch sourcespace
case 'grid'
pow = source.pow;
source.pow = nan(size(source.pos, 1), 1);
source.pow(source.inside) = pow;
sourceint = ft_sourceinterpolate(cfg, source, ft_read_mri(sMRI, 'dataformat', 'nifti_spm'));
Y = sourceint.pow;
case 'mesh'
Y = spm_mesh_to_grid(source, outvol, source.pow);
spm_smooth(Y, Y, 1);
Y = Y.*(Y > max(source.pow)*exp(-8));
case 'voi'
cfg.interpmethod = 'sphere_avg';
cfg.sphereradius = 5;
sourceint = ft_sourceinterpolate(cfg, source, ft_read_mri(sMRI, 'dataformat', 'nifti_spm'));
Y = sourceint.pow;
Y = reshape(Y, sourceint.dim);
end
spm_write_vol(outvol, Y);
nifti.files{i, 1} = outvol.fname;
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
spm_progress_bar('Clear');
res = nifti;