-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsetBatchSkullStripping.m
More file actions
210 lines (168 loc) · 6.97 KB
/
setBatchSkullStripping.m
File metadata and controls
210 lines (168 loc) · 6.97 KB
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
function matlabbatch = setBatchSkullStripping(matlabbatch, BIDS, opt, subLabel)
%
% Creates a batch to compute a brain mask based on the tissue probability maps
% from the segmentation.
%
% USAGE::
%
% matlabbatch = setBatchSkullStripping(matlabbatch, BIDS, opt, subLabel)
%
% :param matlabbatch: list of SPM batches
% :type matlabbatch: structure
%
% :type BIDS: structure
% :param BIDS: dataset layout.
% See also: bids.layout, getData.
%
% :param opt: Options chosen for the analysis.
% See checkOptions.
% :type opt: structure
%
% :param subLabel: subject label
% :type subLabel: char
%
% :returns: - :matlabbatch: (structure) The matlabbatch ready to run the spm job
%
% This function will get its inputs from the segmentation batch by reading
% the dependency from ``opt.orderBatches.segment``. If this field is not specified it will
% try to get the results from the segmentation by relying on the ``anat``
% image returned by ``getAnatFilename``.
%
% The threshold for inclusion in the mask can be set by::
%
% opt.skullstrip.threshold (default = 0.75)
%
% Any voxel with p(grayMatter) + p(whiteMatter) + p(CSF) > threshold
% will be included in the skull stripping mask.
%
% It is also possible to segment a functional image by setting
% ``opt.skullstrip.mean`` to ``true``
%
% Skullstripping can be skipped by setting
% ``opt.skullstrip.do`` to ``false``
%
% (C) Copyright 2020 bidspm developers
if ~opt.skullstrip.do
return
end
printBatchName('skull stripping', opt);
% if the input image is mean func image instead of anatomical
if opt.skullstrip.mean
[imageToSkullStrip, dataDir] = getMeanFuncFilename(BIDS, subLabel, opt);
else
[imageToSkullStrip, dataDir] = getAnatFilename(BIDS, opt, subLabel);
end
bf = bids.File(imageToSkullStrip, 'use_schema', false);
if isSkullstripped(bf)
id = 'imageAlreadySkullstripped';
msg = 'The image is already skullstripped. Skipping skullstripping batch.';
logger('WARNING', msg, 'id', id, 'filename', mfilename(), 'options', opt);
return
end
% if this is part of a pipeline
% we get the segmentation dependency to get the input from.
% Otherwise the files to process are stored in a cell
if isfield(opt, 'orderBatches') && ...
isfield(opt.orderBatches, 'segment') && ...
opt.orderBatches.segment > 0
input(1) = cfg_dep('Segment: Bias Corrected (1)', ...
returnDependency(opt, 'segment'), ...
substruct('.', 'channel', '()', {1}, ...
'.', 'biascorr', '()', {':'}));
input(2) = cfg_dep('Segment: c1 Images', ...
returnDependency(opt, 'segment'), ...
substruct('.', 'tiss', '()', {1}, ...
'.', 'c', '()', {':'}));
input(3) = cfg_dep('Segment: c2 Images', ...
returnDependency(opt, 'segment'), ...
substruct('.', 'tiss', '()', {2}, ...
'.', 'c', '()', {':'}));
input(4) = cfg_dep('Segment: c3 Images', ...
returnDependency(opt, 'segment'), ...
substruct('.', 'tiss', '()', {3}, ...
'.', 'c', '()', {':'}));
else
% bias corrected image
anatFile = bids.File(imageToSkullStrip);
filter = struct('suffix', anatFile.suffix, ...
'sub', anatFile.entities.sub, ...
'prefix', '', ...
'desc', 'biascor', ...
'space', 'individual');
biasCorrectedAnatImage = bids.query(BIDS, 'data', filter);
% tissue probability maps
filter = struct('suffix', 'probseg', ...
'res', '', ...
'sub', anatFile.entities.sub, ...
'prefix', '', ...
'space', 'individual');
filter.label = 'GM';
gmTpm = bids.query(BIDS, 'data', filter);
filter.label = 'WM';
wmTpm = bids.query(BIDS, 'data', filter);
filter.label = 'CSF';
csfTpm = bids.query(BIDS, 'data', filter);
input{1} = biasCorrectedAnatImage;
% grey matter
input{2} = gmTpm;
% white matter
input{3} = wmTpm;
% csf
input{4} = csfTpm;
if any(cellfun('isempty', input))
msg = sprintf('Missing data for skullstripping: run the segmentation.');
id = 'missingDataForSkullstripping';
logger('WARNING', msg, 'id', id, 'options', opt, 'filename', mfilename());
end
if any(cellfun(@(x) numel(x), input) > 1)
msg = sprintf(['Too much data for skullstripping: ', ...
'should have only bias corrected image + 1 TPM per tissue class.']);
id = 'tooMuchDataForSkullstripping';
logger('WARNING', msg, 'id', id, 'options', opt, 'filename', mfilename());
end
input = {input{1}, input{2}, input{3}, input{4}};
end
output = returnNameSkullstripOutput(imageToSkullStrip, 'image');
saveMetadataImage(dataDir, opt, output, imageToSkullStrip);
expression = sprintf('i1.*((i2+i3+i4)>%f)', opt.skullstrip.threshold);
matlabbatch = setBatchImageCalculation(matlabbatch, opt, input, output, dataDir, expression);
%% Add a batch to output the mask
maskOutput = returnNameSkullstripOutput(imageToSkullStrip, 'mask');
saveMetadataImage(dataDir, opt, maskOutput, imageToSkullStrip);
matlabbatch{end + 1} = matlabbatch{end};
matlabbatch{end}.spm.util.imcalc.expression = sprintf('(i2+i3+i4)>%f', ...
opt.skullstrip.threshold);
matlabbatch{end}.spm.util.imcalc.output = maskOutput;
%%
addSkullstrippedMetadataToRoot(BIDS);
end
function saveMetadataImage(dataDir, opt, output, imageToSkullStrip)
bf = bids.File(output);
json = bids.derivatives_json(output);
if strcmp(bf.suffix, 'mask')
json.content.Description = sprintf(['mask used for skullstripping values with', ...
'"p(GM) + p(WM) + p(CSF) > %f'], opt.skullstrip.threshold);
json.content.Type = 'brain';
else
json.content.Description = sprintf(['image skullstripped for values with', ...
'"p(GM) + p(WM) + p(CSF) > %f'], opt.skullstrip.threshold);
end
json.content.Sources{1} = relPath(imageToSkullStrip);
% TODO RawSources
% will depend on if it is bold or not,
% if we are skullstripping a normalise)
if isfield(bf.entities, 'space') && strcmp(bf.entities.space, 'individual')
json.content.SpatialReference{1} = 'scanner space';
end
bids.util.jsonencode(fullfile(dataDir, json.filename), ...
json.content);
end
function value = relPath(imageToSkullStrip)
bf = bids.File(imageToSkullStrip);
value = fullfile(bf.bids_path, bf.filename);
end
function addSkullstrippedMetadataToRoot(BIDS)
metadata = struct('SkullStripped', true);
filename = fullfile(BIDS.pth, 'desc-skullstripped.json');
bids.util.jsonencode(filename, metadata);
end