forked from hMRI-group/hMRI-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbx_scfg_hmri_proc_Dartel.m
385 lines (344 loc) · 13.8 KB
/
tbx_scfg_hmri_proc_Dartel.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
function proc_dart = tbx_scfg_hmri_proc_Dartel
% Configuration file for the "histological MRI" (hMRI) toolbox
% Applying DARTEL segmentation on previously segmented images
% There are 3 sub-modules:
% - Run Dartel (create Templates)
% - Run Dartel (use existing Templates)
% - Normalise to MNI
% The organisation of the _cfg_ and _run_ are very close to the orginal
% files from SPM (see Dartel toolbox itself) but are also tailored for the
% hMRI toolbox, i.e. the processing of a set of parametric maps from a
% series of subject.
%_______________________________________________________________________
% Copyright (C) 2017 Cyclotron Research Centre
% Written by Christophe Phillips
% but largely inspired by the batch from the past VBQ toolbox.
% ---------------------------------------------------------------------
% Extract whole Dartel tbx configuration
% ---------------------------------------------------------------------
cfg_dartel = tbx_cfg_dartel;
% ---------------------------------------------------------------------
% indir Input directory as output directory
% ---------------------------------------------------------------------
indir = cfg_menu;
indir.tag = 'indir';
indir.name = 'Input directory';
indir.help = {['Output files will be written to the same folder as ',...
'each corresponding input file.']};
indir.labels = {'Yes'};
indir.values = {1};
indir.val = {1};
% ---------------------------------------------------------------------
% outdir Output directory for all data
% ---------------------------------------------------------------------
outdir = cfg_files;
outdir.tag = 'outdir';
outdir.name = 'Output directory, all together';
outdir.help = {['Select a directory where all output files from all '...
'subjects put together will be written to.']};
outdir.filter = 'dir';
outdir.ufilter = '.*';
outdir.num = [1 1];
% ---------------------------------------------------------------------
% outdir_ps Output directory for per-subject organisation
% ---------------------------------------------------------------------
outdir_ps = cfg_files;
outdir_ps.tag = 'outdir_ps';
outdir_ps.name = 'Output directory, with per-subject sub-directory';
outdir_ps.help = {['Select a directory where output files will be '...
'written to, in each subject''s sub-directory.']};
outdir_ps.filter = 'dir';
outdir_ps.ufilter = '.*';
outdir_ps.num = [1 1];
% ---------------------------------------------------------------------
% output Output choice
% ---------------------------------------------------------------------
output = cfg_choice;
output.tag = 'output';
output.name = 'Output choice';
output.help = {['Output directory can be the same as the input ',...
'directory for each input file or user selected (one for everything ',...
'or preserving a per-subject organisation).']};
output.values = {indir outdir outdir_ps };
output.val = {indir};
% ---------------------------------------------------------------------
%% warp_dartel_cr Run DARTEL (create Templates) -> extract from whole config
% ---------------------------------------------------------------------
eval(['warp_dartel_cr = cfg_dartel', ...
cfg_expr_values(cfg_dartel, 'warp'),';']);
% Add the output directory choice to the job structure, in 2nd position
% -> move the 'settings' to 3rd position
nFields = numel(warp_dartel_cr.val); %#ok<*NODEF>
if nFields>1
for ii = nFields:-1:2
warp_dartel_cr.val{ii+1} = warp_dartel_cr.val{ii};
end
end
output_dartel_cr = output;
% % Adjust 'output' for Dartel_Create -> set the help of each option
% help_txt = {...
% 'Template files will be written to the same folder as 1st subject input file.', ...
% 'Select a directory where the Templates files will be saved.', ...
% 'Select a directory in which a sub-directory ''Dartel_Templates'' will contain the Templates files.'};
% name_txt = {...
% 'Same folder as 1st subject input file', ...
% 'Output directory, all together', ...
% 'Output directory, with ''DartelTemplates'' sub-directory'};
% for ii=1:3
% output_dartel_cr.values{ii}.name = name_txt{ii};
% output_dartel_cr.values{ii}.help = help_txt(ii);
% end
warp_dartel_cr.val{2} = output_dartel_cr;
warp_dartel_cr.prog = @hmri_run_proc_dartel_template;
% ---------------------------------------------------------------------
%% warp_dartel_ex Run DARTEL (existing Templates) -> extract from whole config
% ---------------------------------------------------------------------
eval(['warp_dartel_ex = cfg_dartel', ...
cfg_expr_values(cfg_dartel, 'warp1'),';']);
% Add the output directory choice to the job structure, in 2nd position
% -> move the 'settings' to 3rd position
nFields = numel(warp_dartel_ex.val); %#ok<*NODEF>
if nFields>1
for ii = nFields:-1:2
warp_dartel_ex.val{ii+1} = warp_dartel_ex.val{ii};
end
end
output_dartel_cr = output;
warp_dartel_ex.val{2} = output_dartel_cr;
warp_dartel_ex.prog = @hmri_run_proc_dartel_warp;
% ---------------------------------------------------------------------
% vols_field Deformation fields
% ---------------------------------------------------------------------
vols_field = cfg_files;
vols_field.tag = 'vols_field';
vols_field.name = 'Flow fields';
vols_field.help = {'Flow fields.'};
vols_field.filter = 'image';
vols_field.ufilter = '^u_rc.*';
vols_field.num = [1 Inf];
% ---------------------------------------------------------------------
% vols_pm Parametric volumes
% ---------------------------------------------------------------------
vols_pm = cfg_files;
vols_pm.tag = 'vols_pm';
vols_pm.name = 'Maps';
vols_pm.help = {['Select whole brain parameter maps (e.g. MT, R2*, ',...
'FA etc), one type from all subjects (keep the order consistent!).']};
vols_pm.filter = 'image';
vols_pm.ufilter = '.*';
vols_pm.num = [1 Inf];
% ---------------------------------------------------------------------
% m_pams Parameter maps sets
% ---------------------------------------------------------------------
m_pams = cfg_repeat;
m_pams.tag = 'm_pams';
m_pams.name = 'Parameter maps';
m_pams.values = {vols_pm };
m_pams.val = {vols_pm };
m_pams.num = [1 Inf];
m_pams.help = {['Select whole brain parameter maps (e.g. MT, ',...
'R2*, FA etc).']};
% ---------------------------------------------------------------------
% vols_tc Tissue segments
% ---------------------------------------------------------------------
vols_tc = cfg_files;
vols_tc.tag = 'vols_tc';
vols_tc.name = 'c* images';
vols_tc.help = {'Select the tissue segment images (c*)'};
vols_tc.filter = 'image';
vols_tc.ufilter = '^c[\d].*'; % filenames starting with 'c' and a number
vols_tc.num = [1 Inf];
% ---------------------------------------------------------------------
% m_TCs Tissue segment image sets
% ---------------------------------------------------------------------
m_TCs = cfg_repeat;
m_TCs.tag = 'maps';
m_TCs.name = 'Segmented tissue class';
m_TCs.values = {vols_tc };
m_TCs.val = {vols_tc };
m_TCs.num = [0 Inf];
m_TCs.help = {['Select the tissue segement images',...
'of interest from all subjects. This should typically be the c1* ',...
'and c2* images, for GM and WM, in 2 separate sets of images.']};
% ---------------------------------------------------------------------
% multsdata Data
% ---------------------------------------------------------------------
multsdata = cfg_branch;
multsdata.tag = 'multsdata';
multsdata.name = 'Data';
multsdata.val = {m_TCs m_pams vols_field};
% multsdata.val = {multsdata_gm multsdata_wm multsdata_f multsdata_u};
% ---------------------------------------------------------------------
%% nrm Normalize to MNI -> extract from whole config
% ---------------------------------------------------------------------
eval(['nrm = cfg_dartel', ...
cfg_expr_values(cfg_dartel, 'mni_norm'),';']);
% ---------------------------------------------------------------------
% Reorganize SPM-batch entries for the hMRI-batch
% ---------------------------------------------------------------------
eval(['nrm' cfg_expr(nrm, 'data') '= multsdata;']);
% Drop out 2 fields:
% - the 'preserve' field, as this is fixed in the run function
% - the 'fwhm' field, as no smoothin applied here
eval(['nrm' regexprep(cfg_expr(nrm, 'preserve'), '{([0-9]+)}$', '($1)') '=[];']);
eval(['nrm' regexprep(cfg_expr(nrm, 'fwhm'), '{([0-9]+)}$', '($1)') '=[];']);
% Add the output directory chooice to the job structure, in 3rd position
nFields = numel(nrm.val);
if nFields>2
for ii = nFields:-1:3
nrm.val{ii+1} = nrm.val{ii};
end
end
nrm.val{3} = output;
% Function calls
nrm.prog = @hmri_run_proc_dartel_norm;
nrm.vout = @vout_norm_fun;
nrm.check = [];
% ---------------------------------------------------------------------
% dartel DARTEL Tools
% ---------------------------------------------------------------------
proc_dart = cfg_choice;
proc_dart.tag = 'proc_dart';
proc_dart.name = 'Proc. hMRI -> Dartel';
proc_dart.values = {warp_dartel_cr warp_dartel_ex nrm };
%dartel.num = [0 Inf];
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% =======================================================================
%% VOUT & CHECK FUNCTIONS
% =======================================================================
%_______________________________________________________________________
function dep = vout_norm_fun(job) %#ok<*INUSD>
cdep = cfg_dep;
% deal with mwc's
for ii=1:numel(job.multsdata.vols_tc)
cdep(end+1) = cfg_dep;
cdep(end).sname = sprintf('mwc#%d',ii);
cdep(end).src_output = substruct('.','vols_mwc','{}',{ii});
cdep(end).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
% deal with wc's
for ii=1:numel(job.multsdata.vols_tc)
cdep(end+1) = cfg_dep;
cdep(end).sname = sprintf('wc#%d',ii);
cdep(end).src_output = substruct('.','vols_wc','{}',{ii});
cdep(end).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
%deal with wPM's
for ii=1:numel(job.multsdata.vols_pm)
cdep(end+1) = cfg_dep;
cdep(end).sname = sprintf('warped Param Map #%d',ii);
cdep(end).src_output = substruct('.','vols_wpm','{}',{ii});
cdep(end).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
dep = cdep(2:end);
end
%_______________________________________________________________________
function dep = vout_dartel_warp(job) %#ok<*DEFNU>
for i=1:numel(job.images{1})
fdep(i) = cfg_dep;
fdep(i).sname = sprintf('Flow Field_subj%d',i);
fdep(i).src_output = substruct('.','files','()',{i});
fdep(i).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
dep = fdep;
end
%_______________________________________________________________________
function dep = vout_dartel_template(job)
if isa(job.settings.template,'cfg_dep') || ~ ...
isempty(deblank(job.settings.template))
for it=0:numel(job.settings.param),
tdep(it+1) = cfg_dep;
tdep(it+1).sname = sprintf('Template (Iteration %d)', it);
tdep(it+1).src_output = substruct('.','template','()',{it+1});
tdep(it+1).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
else
tdep = cfg_dep;
tdep = tdep(false);
end
for i=1:numel(job.images{1})
fdep(i) = cfg_dep;
fdep(i).sname = sprintf('Flow Field_subj%d',i);
fdep(i).src_output = substruct('.','files','()',{i});
fdep(i).tgt_spec = cfg_findspec({{'filter','nifti'}});
end
dep = [tdep fdep];
end
%_______________________________________________________________________
function chk = check_dartel_template(job)
n1 = numel(job.images);
n2 = numel(job.images{1});
chk = '';
for i=1:n1,
if numel(job.images{i}) ~= n2,
chk = 'Incompatible number of images';
break;
end;
end;
end
% ========================================================================
%% SUBFUNCTIONS to handle matlabbatch structure and fields
% ========================================================================
function expr = cfg_expr_values(c, varargin) %#ok<INUSL>
% Extracting the 'values' field with index matching the input argument and
% 'tag' of the matlabbatch structure.
expr = 'c';
for i=1:size(varargin,2)
% if strcmp(class(varargin{i}), 'double')
if isa(varargin{i}, 'double')
expr = [expr '.values{' num2str(varargin{i}) '}']; %#ok<*AGROW>
else
v = eval([expr ';']);
for j=1:size(v.values,2)
if strcmp(v.values{j}.tag, varargin{i})
break
end
end
expr = [expr '.values{' num2str(j) '}'];
end
end
expr = expr(2:end);
end
%_______________________________________________________________________
function expr = cfg_expr(c, varargin) %#ok<INUSL>
expr = 'c';
for i=1:size(varargin,2)
% if strcmp(class(varargin{i}), 'double')
if isa(varargin{i}, 'double')
expr = [expr '.val{' num2str(varargin{i}) '}'];
else
v = eval([expr ';']);
for j=1:size(v.val,2)
if strcmp(v.val{j}.tag, varargin{i})
break
end
end
expr = [expr '.val{' num2str(j) '}'];
end
end
expr = expr(2:end);
end
%_______________________________________________________________________
function c = unlimit(c)
try
if isa(c, 'cfg_files')
c.num = [0 Inf];
end
catch e %#ok<*NASGU>
end
try
for i=1:numel(c.val)
c.val{i} = unlimit(c.val{i});
end
catch e
end
end
%_______________________________________________________________________
function c = cfg_set_val(c, varargin)
expr = ['c' cfg_expr(c, varargin{1:end-1})];
eval([expr '.val={varargin{end}};']);
end
%_______________________________________________________________________