/
warp_scan.m
273 lines (228 loc) · 10.5 KB
/
warp_scan.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
function planC = warp_scan(deformS,movScanNum,movPlanC,planC,tmpDirPath,interpolation,inverseFlag)
% function planC = warp_scan(deformS,movScanNum,movPlanC,planC,tmpDirPath,interpolation)
%
% APA, 07/19/2012
if ~isstruct(deformS)
error('deformS must be in form of deformS --> baseScanUID, movScanUID, algorithm, algorithmParamsS')
end
if ~exist('interpolation','var')
interpolation = '';
end
if ~exist('inverseFlag','var')
inverseFlag = '';
end
if ~exist('tmpDirPath','var')
tmpDirPath = fullfile(getCERRPath,'ImageRegistration','tmpFiles');
end
%% Read in CERR options
optS = getCERROptions;
% Create b-spline coefficients file
algorithm = deformS.algorithm;
% else
% algorithm = 'PLASTIMATCH';
% end
%% Convert moving scan to .mha
indexMovS = movPlanC{end};
movScanOffset = movPlanC{indexMovS.scan}(movScanNum).scanInfo(1).CTOffset;
movScanName = [movPlanC{indexMovS.scan}(movScanNum).scanType '_deformed'];
[movScanUniqName,movScanUID] = genScanUniqName(movPlanC, movScanNum);
movScanFileName = fullfile(tmpDirPath,['movScan_',movScanUniqName,'.mha']);
success = createMhaScansFromCERR(movScanNum, movScanFileName, movPlanC);
%% Convert reference (target) scan to .mha (req for ANTs)
refScanNum = findScanByUID(planC,deformS.baseScanUID);
if ~isempty(refScanNum)
[refScanUniqName, baseScanUID] = genScanUniqName(planC, refScanNum);
refScanFileName = fullfile(tmpDirPath,['refScan_',refScanUniqName,'.mha']);
success = createMhaScansFromCERR(refScanNum, refScanFileName, planC);
else
baseScanUID = deformS.baseScanUID;
end
%% Output prefix
warpOutPrefix = fullfile(tmpDirPath,['warped_scan_', baseScanUID, '_', movScanUID]);
%% Run scan warp
switch upper(algorithm)
case 'ELASTIX'
% Generate name for the output directory
warpedDir = warpOutPrefix;
if ~exist(warpedDir,'dir')
mkdir(warpedDir)
end
% Read Elastix build path from CERROptions.json
elxTransformCmd = 'transformix';
if ~exist(optS.elastix_build_dir,'dir')
error(['ELASTIX executable not found on path ' optS.elastix_build_dir]);
end
%cd(optS.elastix_build_dir)
if isunix
elxTransformCmd = ['sh ', fullfile(optS.elastix_build_dir,elxTransformCmd)];
else
elxTransformCmd = fullfile(optS.elastix_build_dir,[elxTransformCmd,'.exe']);
end
transformC = fieldnames(deformS.algorithmParamsS);
for iTransform = 1:length(transformC)
fileC = deformS.algorithmParamsS.(transformC{iTransform});
transformFileName = fullfile(warpedDir,[transformC{iTransform},'.txt']);
fileC = fileC(:);
numRows = length(fileC);
ind = [];
indNoTf = [];
for row = 1:numRows
ind = strfind(fileC{row},'(InitialTransformParametersFileName');
indNoTf = strfind(fileC{row},...
'(InitialTransformParametersFileName "NoInitialTransform")');
if ~isempty(ind) || ~isempty(indNoTf)
break
end
end
if ~isempty(ind) && isempty(indNoTf)
indV = strfind(fileC{row},'"');
strTf = fileC{row};
tfFileName = strTf(indV(1)+1:indV(2)-1);
[~,fname,ext] = fileparts(tfFileName);
fname = [fname(1:end-2),'_',fname(end)];
fname = fullfile(warpedDir,[fname,ext]);
newStr = ['(InitialTransformParametersFileName "',fname,'")'];
fileC{row} = newStr;
end
cell2file(fileC,transformFileName);
end
indV = cellfun(@(x) str2double(x(end)),transformC); % will work only up to 9 transforms
lastTransform = max(indV);
lastTransformName = fullfile(warpedDir,...
['TransformParameters_',num2str(lastTransform),'.txt']);
%elxTransformCmd = [elxTransformCmd,' -def all -out ',...
% outputDirectory, ' -tp ',lastTransformName]; % DVF
% fullfile(outputDirectory,'deformationField.mhd'); % Name of mhd
% file containing DVF
elxTransformCmd = [elxTransformCmd,' -in ',movScanFileName,...
' -out ',warpedDir, ' -tp ',lastTransformName];
system(elxTransformCmd)
warpedNrrdFileName = fullfile(warpedDir,'result.nrrd');
% Read the warped output .nrrd file within CERR
[data3M, infoS] = nrrd_read(warpedNrrdFileName);
data3M = permute(data3M,[2,1,3]); % required since mha2cerr.m does this: permute(data3M,[2,1,3])
datamin = min(data3M(:));
movScanOffset = 0;
if datamin < 0
movScanOffset = -datamin;
end
save_flag = 0;
planC = mha2cerr(infoS,data3M,movScanOffset,movScanName, planC, save_flag);
% Cleanup
try
delete(movScanFileName)
rmdir(warpedDir,'s')
end
case {'LDDMM ANTS','QUICKSYN ANTS'}
warpedMhaFileName = [warpOutPrefix '.mha']; %fullfile(tmpDirPath,['warped_scan_', refScanUID, '_', movScanUID, '.mha']);
% ANTs path setup
if ~exist(optS.antspath_dir,'dir')
error(['ANTSPATH ' optS.antspath_dir ' not found on filesystem. Please review CERROptions.']);
end
antspath = fullfile(optS.antspath_dir,'bin');
setenv('ANTSPATH', antspath);
antsScriptPath = fullfile(optS.antspath_dir, 'Scripts');
antsCERRScriptPath = fullfile(getCERRPath,'CERR_core','ImageRegistration','antsScripts');
if isunix
setenv('PATH',[antspath ':' antsScriptPath ':' antsCERRScriptPath ':' getenv('PATH')]);
antsCommand = '';
else
setenv('PATH',[antspath ';' antsScriptPath ';' antsCERRScriptPath ';' getenv('PATH')]);
antsCommand = '';
end
% generate transform command
transformParams = buildAntsTransform(deformS.algorithmParamsS.antsWarpProducts);
% transformParams ='';
% if ~isempty(deformS.algorithmParamsS.antsWarpProducts.Warp)
% if exist(deformS.algorithmParamsS.antsWarpProducts.Warp,'file')
% transformParams = [' -t ' deformS.algorithmParamsS.antsWarpProducts.Warp ' '];
% else
% error(['Unable to complete transform, warp product missing: ' deformS.algorithmParamS.antsWarpProducts.Warp]);
% end
% end
% if ~isempty(deformS.algorithmParamsS.antsWarpProducts.Affine)
% if exist(deformS.algorithmParamsS.antsWarpProducts.Affine, 'file')
% transformParams = [transformParams ' -t ' deformS.algorithmParamsS.antsWarpProducts.Affine ' '];
% else
% error(['Unable to complete transform, affine product missing: ' deformS.algorithmParamsS.antsWarpProducts.Affine]);
% end
% end
% if isempty(transformParams)
% error('ANTs: No transformation products specified');
% end
if ~isempty(interpolation)
interpParams = [' -n ' interpolation];
else
interpParams = ' ';
end
antsCommand = [antsCommand ' antsApplyTransforms -d 3 -r ' refScanFileName ' -i ' movScanFileName ' -o ' warpedMhaFileName ' ' transformParams ' ' interpParams];
disp(antsCommand);
system(antsCommand);
disp('Warp complete, importing warped scan to planC...');
infoS = mha_read_header(warpedMhaFileName);
data3M = mha_read_volume(infoS);
save_flag = 0;
planC = mha2cerr(infoS,data3M,movScanOffset,movScanName, planC, save_flag);
disp('Warp scan import complete. Cleaning up...');
% clean up
try
delete(movScanFileName);
delete(refScanFileName);
delete(warpedMhaFileName);
catch err
disp(err);
disp('Unable to clean up ANTs output files');
end
otherwise % PLASTIMATCH
% Generate name for the output .mha file
warpedMhaFileName = [warpOutPrefix '.mha']; %fullfile(tmpDirPath, ['warped_scan_',baseScanUID,'_',movScanUID,'.mha']);
% if ~isstruct(deformS)
% bspFileName = deformS;
%indexS = planC{end};
%movScanUID = movPlanC{indexMovS.scan}(movScanNum).scanUID;
%baseScanUID = planC{indexS.scan}(movScanNum).scanUID;
% else
bspFileName = fullfile(getCERRPath,...
'ImageRegistration','tmpFiles',...
['bsp_coeffs_',baseScanUID,'_',movScanUID,'.txt']);
% end
success = write_bspline_coeff_file(bspFileName,deformS.algorithmParamsS);
% Switch to plastimatch directory if it exists
prevDir = pwd;
% Build plastimatch warp command
plmCommand = 'plastimatch warp ';
optName = fullfile(getCERRPath,'CERROptions.json');
optS = opts4Exe(optName);
if exist(optS.plastimatch_build_dir,'dir') && isunix
cd(optS.plastimatch_build_dir)
plmCommand = ['./',plmCommand];
end
% Issue plastimatch warp command
fail = system([plmCommand, '--input ', movScanFileName,...
' --output-img ', warpedMhaFileName, ' --xf ', bspFileName]);
if fail % try escaping slashes
system([plmCommand, '--input ', escapeSlashes(movScanFileName),...
' --output-img ', escapeSlashes(warpedMhaFileName),...
' --xf ', escapeSlashes(bspFileName)])
end
% Switch back to the previous directory
cd(prevDir)
% Read the warped output .mha file within CERR
infoS = mha_read_header(warpedMhaFileName);
data3M = mha_read_volume(infoS);
%[data3M,infoS] = readmha(warpedMhaFileName);
save_flag = 0;
planC = mha2cerr(infoS,data3M,movScanOffset,movScanName, planC, save_flag);
% Cleanup
try
if isstruct(deformS)
delete(bspFileName)
end
delete(movScanFileName)
delete(warpedMhaFileName)
end
%% Add associated base and moving scanUIDs
warpedScanNum = length(planC{indexS.scan});
planC{indexS.scan}(warpedScanNum).assocBaseScanUID = baseScanUID;
planC{indexS.scan}(warpedScanNum).assocMovingScanUID = movScanUID;
end