-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathea_apply_normalization_tofile.m
180 lines (157 loc) · 6.13 KB
/
ea_apply_normalization_tofile.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 ea_apply_normalization_tofile(options,from,to,useinverse,interp,ref)
% this function applies lead-dbs normalizations to nifti files.
% currently just used to generate patient specific atlases,i.e., from MNI
% space to native space
if ischar(from)
from = {from};
end
if ischar(to)
to = {to};
end
if ~exist('interp', 'var')
interp=4;
end
if ~exist('ref', 'var')
ref='';
end
json = loadjson(options.subj.norm.log.method);
if ischar(interp)
if strcmp(interp,'auto') % only works if one image supplied
interp=detinterp(from,contains(json.method, {'ANTs','EasyReg'}));
end
end
if contains(json.method, {'ANTs','EasyReg'})
ea_ants_apply_transforms(options, from, to, useinverse, ref, '', interp);
elseif contains(json.method, 'FNIRT')
if useinverse
% In case inverse transformation using FSL, make sure the input has
% exactly the same affine matrix as the template.
for i=1:length(from)
ea_imcalc(from{i}, [ea_space, options.primarytemplate, '.nii'], '', interp);
end
end
ea_fsl_apply_normalization(options, from,to, useinverse, ref, '', interp);
elseif contains(json.method, 'SPM')
for i=1:length(from)
if strcmp(from{i}(end-2:end),'.gz')
wasgz = 1;
[~, fn] = fileparts(from{i});
copyfile(from{i}, [tempdir, fn, '.gz']);
gunzip([tempdir, fn, '.gz']);
from{i} = [tempdir, fn];
else
wasgz = 0;
end
refIsGz = 0;
% ATTENTION: when using PUSH method, transforming from native space
% to template space should use the INVERSE transformation,
% transforming from template space to native space should use
% FORWARD transformation.
usepush=1;
if useinverse
if isempty(ref)
ref = options.subj.preopAnat.(options.subj.AnchorModality).coreg;
elseif endsWith(ref, '.gz')
refIsGz = 1;
gunzip(ref);
ref = strrep(ref, '.gz', '');
end
if usepush
matlabbatch{1}.spm.util.defs.comp{1}.def = {[options.subj.norm.transform.forwardBaseName, 'spm.nii']};
matlabbatch{1}.spm.util.defs.out{1}.push.fnames = from(i);
matlabbatch{1}.spm.util.defs.out{1}.push.weight = {''};
matlabbatch{1}.spm.util.defs.out{1}.push.savedir.saveusr = {fileparts(to{i})};
matlabbatch{1}.spm.util.defs.out{1}.push.fov.file = {ref};
matlabbatch{1}.spm.util.defs.out{1}.push.preserve = 0;
matlabbatch{1}.spm.util.defs.out{1}.push.fwhm = [0.5 0.5 0.5];
matlabbatch{1}.spm.util.defs.out{1}.push.prefix = '';
else
matlabbatch{1}.spm.util.defs.comp{1}.def = {[options.subj.norm.transform.inverseBaseName, 'spm.nii']};
matlabbatch{1}.spm.util.defs.out{1}.pull.fnames = from(i);
matlabbatch{1}.spm.util.defs.out{1}.pull.savedir.saveusr = {fileparts(to{i})};
matlabbatch{1}.spm.util.defs.out{1}.pull.interp = interp;
matlabbatch{1}.spm.util.defs.out{1}.pull.mask = 1;
matlabbatch{1}.spm.util.defs.out{1}.pull.fwhm = [0.5 0.5 0.5];
matlabbatch{1}.spm.util.defs.out{1}.pull.prefix = '';
end
spm_jobman('run',{matlabbatch});
clear matlabbatch
else
if isempty(ref)
ref = [ea_space, options.primarytemplate, '.nii'];
elseif endsWith(ref, '.gz')
refIsGz = 1;
gunzip(ref);
ref = strrep(ref, '.gz', '');
end
if usepush
matlabbatch{1}.spm.util.defs.comp{1}.def = {[options.subj.norm.transform.inverseBaseName, 'spm.nii']};
matlabbatch{1}.spm.util.defs.out{1}.push.fnames = from(i);
matlabbatch{1}.spm.util.defs.out{1}.push.weight = {''};
matlabbatch{1}.spm.util.defs.out{1}.push.savedir.saveusr = {fileparts(to{i})};
matlabbatch{1}.spm.util.defs.out{1}.push.fov.file = {ref};
matlabbatch{1}.spm.util.defs.out{1}.push.preserve = 0;
matlabbatch{1}.spm.util.defs.out{1}.push.fwhm = [0.5 0.5 0.5];
matlabbatch{1}.spm.util.defs.out{1}.push.prefix = '';
else
matlabbatch{1}.spm.util.defs.comp{1}.def = {[options.subj.norm.transform.forwardBaseName, 'spm.nii']};
matlabbatch{1}.spm.util.defs.out{1}.pull.fnames = from(i);
matlabbatch{1}.spm.util.defs.out{1}.pull.savedir.saveusr = {fileparts(to{i})};
matlabbatch{1}.spm.util.defs.out{1}.pull.interp = interp;
matlabbatch{1}.spm.util.defs.out{1}.pull.mask = 1;
matlabbatch{1}.spm.util.defs.out{1}.pull.fwhm = [0.5 0.5 0.5];
matlabbatch{1}.spm.util.defs.out{1}.pull.prefix = '';
end
spm_jobman('run',{matlabbatch});
clear matlabbatch
end
pth = fileparts(to{i});
[~, fn, ext] = fileparts(from{i});
if strcmp(to{i}(end-2:end),'.gz')
to{i} = to{i}(1:end-3);
gzip_output = 1;
else
gzip_output = 0;
end
movefile(fullfile(pth, ['sw', fn, ext]), to{i});
if refIsGz
gzip(ref);
delete(ref);
end
if gzip_output
gzip(to{i});
delete(to{i});
end
if wasgz
ea_delete([tempdir, fn, '.gz']);
ea_delete([tempdir, fn]);
end
end
end
function interp=detinterp(from,ants)
if length(from)>1
ea_error('Auto detection only implemented for single images.')
end
nii=ea_load_nii(from{1});
outs=unique(nii.img(:));
if ants
interp='LanczosWindowedSinc'; % default
else
interp=1;
end
if length(outs)<3
if ants
interp='GenericLabel';
else
interp=0;
end
end
if length(outs)<100 % likely labeling file
if outs==round(outs) % integers only, pretty much certainly labeling file
if ants
interp='GenericLabel';
else
interp=0;
end
end
end