-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_brick_dartel_jacobian.m
145 lines (129 loc) · 5.17 KB
/
spm_brick_dartel_jacobian.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
function [in, out, opt] = spm_brick_dartel_jacobian(in,out,opt)
% This function generates Jacobian determinant fields
%
% IN (structure) with the following fields:
% <SUBJECT> (string) flowfield image
%
% OUT (structure, optional) with the following fields:
% <SUBJECT> (string) filename of Jacbobian determinant field output
%
% OPT.FOLDER_OUT (string, default same as IN.<SUBJECT>) the folder where
% to generate outputs. If left empty, the outputs for a given image
% in IN.<SUBJECT> will be written to the same originating path (i.e.
% path of IN.<SUBJECT> == path of outputs from IN.<SUBJECT>).
% OPT.FLAG_TEST (boolean, default false) flag to run a test without
% generating outputs.
%
% If OPT.FLAG_TEST is true, the brick does nothing but still updates the
% default file names in OUT, as well as default options in OPT.
% By default (OUT = struct) the brick generates all output files.
% If file names are specified in OUT, those will be used over defaults.
% If a file name 'skip' is specified, the output is not generated.
%
% This is an interface to the DARTEL tools from SPM12.
% It performs non-linear registration. This involves iteratively matching
% all the selected images to a template generated from their own mean. A
% series of Template*.nii files are generated, which become increasingly
% crisp as the registration proceeds. The non-linear registration can also
% match individual images to pre-existing template data. Start out with
% smooth templates, and select crisp templates for the later iterations.
%
% _________________________________________________________________________
% Copyright (c) Angela Tam, Yasser Iturria-Medina, Pierre Bellec
% Montreal Neurological Institute, 2017
% Centre de recherche de l'institut de geriatrie de Montreal,
% Department of Computer Science and Operations Research
% University of Montreal, Quebec, Canada, 2017
% Maintainer : [email protected]
% See licensing information in the code.
% Keywords : SPM, DARTEL, nonlinear registration, interface
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
global gb_psom_name_job
if ~exist('in','var')||~exist('out','var')
error('spm:brick','syntax: [IN,OUT,OPT] = SPM_BRICK_DARTEL_JACOBIAN(IN,OUT,OPT).\n Type ''help spm_brick_dartel_jacobian'' for more info.')
end
%% Input
if ~isstruct(in)
error('IN should be a structure')
end
if ~isstruct(out)
error('OUT should be a structure')
end
%% Temporary folder
if ~isempty(gb_psom_name_job)
path_tmp = [tempdir filesep gb_psom_name_job filesep];
else
path_tmp = tempdir;
end
%% Options
if nargin < 3
opt = struct;
end
opt = psom_struct_defaults(opt , ...
{ 'flag_test', 'folder_out' },...
{ false , '' });
%% settings for DARTEL brick
opt_dartel.K = 6;
opt_dartel.folder_out = '';
opt_dartel.flag_test = opt.flag_test;
opt = psom_defaults(opt_dartel, opt );
%% Building default output names
list_sub = fieldnames(in);
if isempty(out) || isempty(fieldnames(out))
for ss = 1:length(list_sub)
[path_f,name_f,ext_f] = fileparts(in.(list_sub{ss}));
if isempty(path_f)
path_f = '.';
end
out.(list_sub{ss}) = [path_f filesep 'jac_' name_f(3:end) ext_f];
end
end
if opt.flag_test == 1
return
end
folder_out = opt.folder_out;
opt = rmfield ( opt , { 'folder_out' , 'flag_test'}); % Get rid of the options not supported by spm dartel functions
%% Brick really starts here
spm_in = struct2cell(in);
% saving the determinants of the jacobians
opt.flowfields = spm_in;
spm_dartel_jacobian(opt);
%% rename the jacobian outputs to user-specified filenames
for ss = 1:length(list_sub)
f_out = out.(list_sub{ss});
[path_r,name_r,ext_r] = fileparts(spm_in{ss});
if isempty(path_r)
path_r = '.';
end
jac = [path_r filesep 'jac_' name_r(3:end) ext_r];
if ~strcmp(f_out, jac)
movefile(jac, f_out);
end
end
%% move the outputs to opt.folder_out if specified
if ~isempty(folder_out)
for ss = 1:length(list_sub)
f_out = out.(list_sub{ss});
[path_r,name_r,ext_r] = fileparts(spm_in{ss});
if isempty(path_r)
path_r = '.';
end
movefile([path_r filesep f_out], folder_out);
end
end