forked from netstim/leaddbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathea_apply_normalization_tofile.m
126 lines (112 loc) · 4.97 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
function ea_apply_normalization_tofile(options,from,to,directory,useinverse,interp,refim)
% 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 ~strcmp(directory(end),filesep)
directory=[directory,filesep];
end
if ~exist('interp','var')
interp=4;
end
if ~exist('refim','var')
refim='';
end
switch ea_whichnormmethod(directory)
case ea_getantsnormfuns % ANTs part here
ea_ants_applytransforms(options,from,to,useinverse,refim,'',interp);
case ea_getfslnormfuns % FSL part here
if useinverse
for fi=1:length(from) % assume from and to have same length (must have for this to work)
if strcmp(from{fi}(end-2:end),'.gz') % .gz support
wasgz = 1;
gunzip(from{fi});
delete(from{fi});
from{fi} = from{fi}(1:end-3);
else
wasgz = 0;
end
[dn,fn]=fileparts(from{fi});
matlabbatch{1}.spm.util.imcalc.input = {
[ea_space(options),options.primarytemplate,'.nii,1']
[from{fi},',1']
};
matlabbatch{1}.spm.util.imcalc.output = fn;
matlabbatch{1}.spm.util.imcalc.outdir = {dn};
matlabbatch{1}.spm.util.imcalc.expression = 'i2';
matlabbatch{1}.spm.util.imcalc.var = struct('name', {}, 'value', {});
matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
matlabbatch{1}.spm.util.imcalc.options.mask = 0;
matlabbatch{1}.spm.util.imcalc.options.interp=interp;
matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
spm_jobman('run',{matlabbatch});
clear matlabbatch
if wasgz
gzip(from{fi});
delete(from{fi});
from{fi} = [from{fi}, '.gz'];
end
end
end
ea_fsl_applytransforms(options,from,to,useinverse,refim,'',interp);
otherwise % SPM part here
for fi=1:length(from) % assume from and to have same length (must have for this to work)
if strcmp(from{fi}(end-2:end),'.gz') % .gz support
wasgz = 1;
[~,fn] = fileparts(from{fi});
copyfile(from{fi},[tempdir,fn,'.gz']);
gunzip([tempdir,fn,'.gz']);
from{fi} = [tempdir,fn];
else
wasgz = 0;
end
if useinverse
if isempty(refim)
refim = [directory,options.prefs.prenii_unnormalized];
end
matlabbatch{1}.spm.util.defs.comp{1}.def = {[directory,'y_ea_normparams.nii']};
matlabbatch{1}.spm.util.defs.out{1}.push.fnames = from(fi);
matlabbatch{1}.spm.util.defs.out{1}.push.weight = {''};
matlabbatch{1}.spm.util.defs.out{1}.push.savedir.saveusr = {fileparts(to{fi})};
matlabbatch{1}.spm.util.defs.out{1}.push.fov.file = {refim};
matlabbatch{1}.spm.util.defs.out{1}.push.preserve = 0;
matlabbatch{1}.spm.util.defs.out{1}.push.fwhm = [0 0 0];
matlabbatch{1}.spm.util.defs.out{1}.push.prefix = '';
spm_jobman('run',{matlabbatch});
clear matlabbatch
else
if isempty(refim)
spacedef=ea_getspacedef;
refim = [ea_space,spacedef.templates{1},'.nii'];
end
matlabbatch{1}.spm.util.defs.comp{1}.def = {[directory,'y_ea_inv_normparams.nii']};
matlabbatch{1}.spm.util.defs.out{1}.push.fnames = from(fi);
matlabbatch{1}.spm.util.defs.out{1}.push.weight = {''};
matlabbatch{1}.spm.util.defs.out{1}.push.savedir.saveusr = {fileparts(to{fi})};
matlabbatch{1}.spm.util.defs.out{1}.push.fov.file = {refim};
matlabbatch{1}.spm.util.defs.out{1}.push.preserve = 0;
matlabbatch{1}.spm.util.defs.out{1}.push.fwhm = [0 0 0];
matlabbatch{1}.spm.util.defs.out{1}.push.prefix = '';
spm_jobman('run',{matlabbatch});
clear matlabbatch
end
pth = fileparts(to{fi});
[~, fn, ext] = fileparts(from{fi});
if strcmp(to{fi}(end-2:end),'.gz')
to{fi} = to{fi}(1:end-3);
gzip_output = 1;
else
gzip_output = 0;
end
try % fails if to is a w prefixed file already
movefile(fullfile(pth,['w', fn, ext]),to{fi});
end
if gzip_output
gzip(to{fi});
delete(to{fi});
end
if wasgz
ea_delete([tempdir,fn,'.gz']);
ea_delete([tempdir,fn]);
end
end
end