-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbf_write_gifti.m
129 lines (108 loc) · 3.58 KB
/
bf_write_gifti.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
function res = bf_write_gifti(BF, S)
% Writes out beamformer results as GIfTI meshes
% 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'};
visualise = cfg_menu;
visualise.tag = 'visualise';
visualise.name = 'Visualise the outputs';
visualise.help = {'Visualise the outputs in the graphics window.'};
visualise.labels = {'inflated', 'yes', 'no'};
visualise.values = {2, 1, 0};
visualise.val = {0};
res = cfg_branch;
res.tag = 'gifti';
res.name = 'GIfTI';
res.val = {normalise, space, visualise};
return
elseif nargin < 2
error('Two input arguments are required');
end
if ~isfield(BF.sources, 'mesh')
error('Only mesh source space is supported');
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'
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
save(gifti(source), [BF.data.D.fname '.surf.gii']);
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
fname = fullfile(pwd, [BF.output.image(i).label '.gii']);
G = gifti;
G.private.metadata(1).name = 'SurfaceID';
G.private.metadata(1).value = [BF.data.D.fname '.surf.gii'];
G.cdata = scale(i)*BF.output.image(i).val;
G.cdata = G.cdata(:);
save(G, fname, 'ExternalFileBinary');
res.files{i, 1} = fname;
if S.visualise
Fgraph = spm_figure('GetWin', fname); figure(Fgraph); clf
a = axes;
if S.visualise == 2
H = spm_mesh_render('Disp', spm_mesh_inflate(gifti([BF.data.D.fname '.surf.gii'])), 'Parent', a);
H = spm_mesh_render('Overlay', H, gifti(fname));
else
H = spm_mesh_render('Disp',gifti([BF.data.D.fname '.surf.gii']), 'Parent', a);
H = spm_mesh_render('Overlay', H, gifti(fname));
end
spm_mesh_render('Colormap', H, jet(256));
end
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
spm_progress_bar('Clear');