Skip to content

Commit 19627ae

Browse files
authored
Merge pull request #638 from Remi-Gau/fix_skip_segment
[FIX] properly skip segment and sullstrip
2 parents 741f67b + d9d48f5 commit 19627ae

20 files changed

+306
-124
lines changed
File renamed without changes.

demos/MoAE/moae_01_bids_app.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
cpp_spm();
5151

5252
%% Dopwnload the dataset
53-
download_data = true;
53+
download_data = false;
5454
clean = true;
5555
download_moae_ds(download_data, clean);
5656

miss_hit.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ regex_parameter_name: "[A-Z]+(_[A-Z]+)*|[a-z]+([A-Z]+[a-z]*)*"
1919

2020
exclude_dir: "lib"
2121
exclude_dir: "docs"
22+
exclude_dir: "WIP"
2223

2324
copyright_entity: "JH"
2425
copyright_entity: "DSS"
@@ -37,5 +38,5 @@ tab_width: 2
3738
# metrics limit for the code quality (https://florianschanda.github.io/miss_hit/metrics.html)
3839
metric "cnest": limit 5
3940
metric "file_length": limit 600
40-
metric "cyc": limit 18
41+
metric "cyc": limit 19
4142
metric "parameters": limit 7

src/batches/preproc/setBatchNormalizationSpatialPrepro.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
end
139139

140140
function biasCorrectedImage = getBiasCorrectedImage(matlabbatch, BIDS, opt)
141+
%
142+
% tries to grab a previously computed bias corrected image
143+
% if this fails we assume we are in a bidsSpatialPreproc workflow
144+
% and we rely on the batch dependency
145+
%
141146

142147
biasCorrectedImage = '';
143148

@@ -161,6 +166,11 @@
161166
end
162167

163168
function [gmTpm, wmTpm, csfTpm] = getTpms(matlabbatch, BIDS, opt)
169+
%
170+
% tries to grab a previously tissue probability maps
171+
% if this fails we assume we are in a bidsSpatialPreproc workflow
172+
% and we rely on the batch dependency
173+
%
164174

165175
gmTpm = '';
166176
wmTpm = '';
@@ -205,6 +215,11 @@
205215
end
206216

207217
function skullstrippedImage = getSkullstrippedImage(matlabbatch, BIDS, opt)
218+
%
219+
% tries to grab a skullstripped image
220+
% if this fails we assume we are in a bidsSpatialPreproc workflow
221+
% and we rely on the batch dependency
222+
%
208223

209224
skullstrippedImage = '';
210225

@@ -226,6 +241,11 @@
226241
end
227242

228243
function skullstripMask = getSkullstripMask(matlabbatch, BIDS, opt)
244+
%
245+
% tries to grab a skullstripped mask
246+
% if this fails we assume we are in a bidsSpatialPreproc workflow
247+
% and we rely on the batch dependency
248+
%
229249

230250
skullstripMask = '';
231251

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function matlabbatch = setBatchSegmentation(matlabbatch, opt, imageToSegment)
1+
function [matlabbatch, opt] = setBatchSegmentation(matlabbatch, opt, imageToSegment)
22
%
33
% Creates a batch to segment the anatomical image
44
%
@@ -16,24 +16,35 @@
1616
%
1717
% (C) Copyright 2020 CPP_SPM developers
1818

19+
if ~opt.segment.do
20+
opt.orderBatches.segment = 0;
21+
return
22+
else
23+
opt.orderBatches.segment = numel(matlabbatch) + 1;
24+
end
25+
1926
printBatchName('Segmentation anatomical image', opt);
2027

2128
% define SPM folder
2229
spmLocation = spm('dir');
2330

2431
% save bias correction field = false
2532
% save bias corrected image = true
26-
matlabbatch{end + 1}.spm.spatial.preproc.channel.write = [false true];
33+
preproc.channel.write = [false true];
2734

35+
% firts part assumes we are in the bidsSpatialPreproc workflow
2836
if isfield(opt, 'orderBatches') && isfield(opt.orderBatches, 'selectAnat')
2937

3038
% SAVE BIAS CORRECTED IMAGE
31-
matlabbatch{end}.spm.spatial.preproc.channel.vols(1) = ...
32-
cfg_dep('Named File Selector: Anatomical(1) - Files', ...
33-
returnDependency(opt, 'selectAnat'), ...
34-
substruct('.', 'files', '{}', {1}));
39+
preproc.channel.vols(1) = cfg_dep('Named File Selector: Anatomical(1) - Files', ...
40+
returnDependency(opt, 'selectAnat'), ...
41+
substruct('.', 'files', '{}', {1}));
3542
else
3643

44+
% TODO
45+
% implement the opt.segment.do and opt.segment.force
46+
% with segmentationAlreadyDone ?
47+
3748
% in case a cell was given as input
3849
if iscell(imageToSegment)
3950
imageToSegment = char(imageToSegment);
@@ -42,60 +53,56 @@
4253
% add all the images to segment
4354
for iImg = 1:size(imageToSegment, 1)
4455
file = validationInputFile([], deblank(imageToSegment(iImg, :)));
45-
matlabbatch{end}.spm.spatial.preproc.channel.vols{iImg, 1} = file;
56+
preproc.channel.vols{iImg, 1} = file;
4657
end
4758

4859
end
4960

50-
matlabbatch{end}.spm.spatial.preproc.channel.biasreg = 0.001;
51-
matlabbatch{end}.spm.spatial.preproc.channel.biasfwhm = opt.segment.biasfwhm;
61+
preproc.channel.biasreg = 0.001;
62+
preproc.channel.biasfwhm = opt.segment.biasfwhm;
5263

5364
% CREATE SEGMENTS IN NATIVE SPACE OF GM, WM AND CSF
5465
nativeSpace = 1;
5566
dartelSpace = 0;
5667
native = [nativeSpace dartelSpace];
57-
matlabbatch{end}.spm.spatial.preproc.tissue(1).tpm = ...
58-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',1']};
59-
matlabbatch{end}.spm.spatial.preproc.tissue(1).ngaus = 1;
60-
matlabbatch{end}.spm.spatial.preproc.tissue(1).native = native;
61-
matlabbatch{end}.spm.spatial.preproc.tissue(1).warped = [0 0];
62-
63-
matlabbatch{end}.spm.spatial.preproc.tissue(2).tpm = ...
64-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',2']};
65-
matlabbatch{end}.spm.spatial.preproc.tissue(2).ngaus = 1;
66-
matlabbatch{end}.spm.spatial.preproc.tissue(2).native = native;
67-
matlabbatch{end}.spm.spatial.preproc.tissue(2).warped = [0 0];
68-
69-
matlabbatch{end}.spm.spatial.preproc.tissue(3).tpm = ...
70-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',3']};
71-
matlabbatch{end}.spm.spatial.preproc.tissue(3).ngaus = 2;
72-
matlabbatch{end}.spm.spatial.preproc.tissue(3).native = native;
73-
matlabbatch{end}.spm.spatial.preproc.tissue(3).warped = [0 0];
74-
75-
matlabbatch{end}.spm.spatial.preproc.tissue(4).tpm = ...
76-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',4']};
77-
matlabbatch{end}.spm.spatial.preproc.tissue(4).ngaus = 3;
78-
matlabbatch{end}.spm.spatial.preproc.tissue(4).native = [0 0];
79-
matlabbatch{end}.spm.spatial.preproc.tissue(4).warped = [0 0];
80-
81-
matlabbatch{end}.spm.spatial.preproc.tissue(5).tpm = ...
82-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',5']};
83-
matlabbatch{end}.spm.spatial.preproc.tissue(5).ngaus = 4;
84-
matlabbatch{end}.spm.spatial.preproc.tissue(5).native = [0 0];
85-
matlabbatch{end}.spm.spatial.preproc.tissue(5).warped = [0 0];
86-
87-
matlabbatch{end}.spm.spatial.preproc.tissue(6).tpm = ...
88-
{[fullfile(spmLocation, 'tpm', 'TPM.nii') ',6']};
89-
matlabbatch{end}.spm.spatial.preproc.tissue(6).ngaus = 2;
90-
matlabbatch{end}.spm.spatial.preproc.tissue(6).native = [0 0];
91-
matlabbatch{end}.spm.spatial.preproc.tissue(6).warped = [0 0];
68+
preproc.tissue(1).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',1']};
69+
preproc.tissue(1).ngaus = 1;
70+
preproc.tissue(1).native = native;
71+
preproc.tissue(1).warped = [0 0];
72+
73+
preproc.tissue(2).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',2']};
74+
preproc.tissue(2).ngaus = 1;
75+
preproc.tissue(2).native = native;
76+
preproc.tissue(2).warped = [0 0];
77+
78+
preproc.tissue(3).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',3']};
79+
preproc.tissue(3).ngaus = 2;
80+
preproc.tissue(3).native = native;
81+
preproc.tissue(3).warped = [0 0];
82+
83+
preproc.tissue(4).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',4']};
84+
preproc.tissue(4).ngaus = 3;
85+
preproc.tissue(4).native = [0 0];
86+
preproc.tissue(4).warped = [0 0];
87+
88+
preproc.tissue(5).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',5']};
89+
preproc.tissue(5).ngaus = 4;
90+
preproc.tissue(5).native = [0 0];
91+
preproc.tissue(5).warped = [0 0];
92+
93+
preproc.tissue(6).tpm = {[fullfile(spmLocation, 'tpm', 'TPM.nii') ',6']};
94+
preproc.tissue(6).ngaus = 2;
95+
preproc.tissue(6).native = [0 0];
96+
preproc.tissue(6).warped = [0 0];
9297

9398
% SAVE FORWARD DEFORMATION FIELD FOR NORMALISATION
94-
matlabbatch{end}.spm.spatial.preproc.warp.mrf = 1;
95-
matlabbatch{end}.spm.spatial.preproc.warp.cleanup = 1;
96-
matlabbatch{end}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
97-
matlabbatch{end}.spm.spatial.preproc.warp.affreg = 'mni';
98-
matlabbatch{end}.spm.spatial.preproc.warp.fwhm = 0;
99-
matlabbatch{end}.spm.spatial.preproc.warp.samp = opt.segment.samplingDistance;
100-
matlabbatch{end}.spm.spatial.preproc.warp.write = [1 1];
99+
preproc.warp.mrf = 1;
100+
preproc.warp.cleanup = 1;
101+
preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
102+
preproc.warp.affreg = 'mni';
103+
preproc.warp.fwhm = 0;
104+
preproc.warp.samp = opt.segment.samplingDistance;
105+
preproc.warp.write = [1 1];
106+
107+
matlabbatch{end + 1}.spm.spatial.preproc = preproc;
101108
end

src/batches/preproc/setBatchSkullStripping.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848

4949
printBatchName('skull stripping', opt);
5050

51-
[imageToSkullStrip, dataDir] = getAnatFilename(BIDS, opt, subLabel);
52-
5351
% if the input image is mean func image instead of anatomical
5452
if opt.skullstrip.mean
5553
[imageToSkullStrip, dataDir] = getMeanFuncFilename(BIDS, subLabel, opt);
54+
else
55+
[imageToSkullStrip, dataDir] = getAnatFilename(BIDS, opt, subLabel);
5656
end
5757

5858
bf = bids.File(imageToSkullStrip, 'use_schema', false);
@@ -68,7 +68,9 @@
6868
% if this is part of a pipeline we get the segmentation dependency to get
6969
% the input from.
7070
% Otherwise the files to process are stored in a cell
71-
if isfield(opt, 'orderBatches') && isfield(opt.orderBatches, 'segment')
71+
if isfield(opt, 'orderBatches') && ...
72+
isfield(opt.orderBatches, 'segment') && ...
73+
opt.orderBatches.segment > 0
7274

7375
input(1) = cfg_dep('Segment: Bias Corrected (1)', ...
7476
returnDependency(opt, 'segment'), ...
@@ -92,10 +94,8 @@
9294

9395
else
9496

95-
anatImage = getAnatFilename(BIDS, opt, subLabel);
96-
9797
% bias corrected image
98-
anatFile = bids.File(anatImage);
98+
anatFile = bids.File(imageToSkullStrip);
9999
filter = struct('suffix', anatFile.suffix, ...
100100
'sub', anatFile.entities.sub, ...
101101
'prefix', '', ...

src/batches/setBatchSelectAnat.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
%
99
% :param matlabbatch: list of SPM batches
1010
% :type matlabbatch: structure
11+
%
1112
% :param BIDS: BIDS layout returned by ``getData``.
1213
% :type BIDS: structure
14+
%
1315
% :param opt: structure or json filename containing the options. See
1416
% ``checkOptions()`` and ``loadAndCheckOptions()``.
1517
% :type opt: structure
18+
%
1619
% :param subLabel: subject label
1720
% :type subLabel: string
1821
%

src/bids/getAnatFilename.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
% [anatImage, anatDataDir] = getAnatFilename(BIDS, subLabel, opt)
1111
%
1212
% :param BIDS:
13-
% :type BIDS: structure
13+
% :type BIDS: structure
14+
%
1415
% :param subLabel:
15-
% :param subLabel: string
16+
% :param subLabel: char
17+
%
1618
% :type opt:
17-
% :param opt: structure
19+
% :param opt: structure
1820
%
1921
% :returns: - :anatImage: (string)
2022
% - :anatDataDir: (string)
@@ -55,7 +57,7 @@
5557
anat = anat{1};
5658
anatImage = unzipAndReturnsFullpathName(anat);
5759

58-
msg = sprintf('selecting anat file: %s\n', pathToPrint(anat));
60+
msg = sprintf(' selecting anat file: %s\n', pathToPrint(anat));
5961
printToScreen(msg, opt);
6062

6163
[anatDataDir, anatImage, ext] = spm_fileparts(anatImage);

src/defaults/checkOptions.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,14 @@
308308
fieldsToSet.useFieldmaps = true;
309309

310310
%% Options for segmentation
311+
fieldsToSet.segment.do = true;
311312
fieldsToSet.segment.force = false;
312313
fieldsToSet.segment.biasfwhm = 60;
313314
fieldsToSet.segment.samplingDistance = 3;
314315

315316
%% Options for skullstripping
316317
fieldsToSet.skullstrip.do = true;
318+
fieldsToSet.skullstrip.force = false;
317319
fieldsToSet.skullstrip.threshold = 0.75;
318320
fieldsToSet.skullstrip.mean = false;
319321

0 commit comments

Comments
 (0)