-
Notifications
You must be signed in to change notification settings - Fork 0
/
linda_predict_custom.R
292 lines (286 loc) · 13.5 KB
/
linda_predict_custom.R
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
print_msg = function(msg, verbose = TRUE) {
if (verbose) {
msg = paste(format(Sys.time(), "\n%H:%M") , msg)
cat(msg)
}
}
linda_predict_custom =
function (file = NA, brain_mask = NULL, n_skull_iter = 2, verbose = TRUE,
outdir = NULL, voxel_resampling = c(2, 2, 2), sigma = 2,
reflaxis = 0, saveMNI = file.exists(system.file("extdata",
"pennTemplate", "templateToCh2_0GenericAffine.mat", package = "LINDA",
mustWork = FALSE)), cache = TRUE)
{
toc = Sys.time()
outputLog = capture.output({
print_msg(paste0("Starting LINDA v", packageVersion("LINDA")),
verbose = verbose)
if (is.na(file)) {
file = file.choose()
}
else {
if (!file.exists(file))
stop(paste("File inexistent:", file))
}
if (is.null(outdir)) {
outdir = file.path(dirname(file), "linda")
}
if (!dir.exists(outdir)) {
print_msg(paste("Creating folder:", outdir), verbose = verbose)
dir.create(outdir, showWarnings = FALSE, recursive = TRUE)
}
else {
print_msg(paste("Using existing folder:", outdir),
verbose = verbose)
}
segnative_file = file.path(outdir, "Prediction3_native.nii.gz")
if (file.exists(segnative_file) & cache) {
print_msg("\nLINDA segmentation already present in folder.\n Use \"cache=FALSE\" to reprocess and overwrite.",
verbose = verbose)
return(NULL)
}
template = system.file("extdata", "pennTemplate", "template.nii.gz",
package = "LINDA", mustWork = TRUE)
template_brain = system.file("extdata", "pennTemplate",
"templateBrain.nii.gz", package = "LINDA", mustWork = TRUE)
template_mask = system.file("extdata", "pennTemplate",
"templateBrainMask.nii.gz", package = "LINDA", mustWork = TRUE)
reader = function(x) {
if (!is.antsImage(x)) {
y = antsImageRead(x)
}
else {
y = antsImageClone(x)
}
return(y)
}
if (is.null(brain_mask)) {
ss_files = c(n4 = file.path(outdir, "N4corrected.nii.gz"),
brain_mask = file.path(outdir, "BrainMask.nii.gz"),
n4_brain = file.path(outdir, "N4corrected_Brain.nii.gz"))
L = as.list(ss_files)
if (all(file.exists(ss_files)) & cache) {
print_msg("Found existing skull stripped files, loading ...",
verbose = verbose)
ss = lapply(ss_files, antsImageRead)
n4 = ss$n4
submask = ss$brain_mask
simg = ss$n4_brain
}
else {
ss = n4_skull_strip(file = file, n_iter = n_skull_iter,
template = template, template_brain = template_brain,
template_mask = template_mask, verbose = verbose)
n4 = ss$n4
submask = ss$brain_mask
simg = ss$n4_brain
print_msg("Saving skull stripped files...", verbose = verbose)
antsImageWrite(n4, ss_files["n4"])
antsImageWrite(submask, ss_files["brain_mask"])
antsImageWrite(simg, ss_files["n4_brain"])
}
}
else {
print_msg("Brain mask provided by user, using to skull strip ...",
verbose = verbose)
brain_mask = reader(brain_mask)
brain_mask_file = file.path(outdir, "BrainMask.nii.gz")
antsImageWrite(brain_mask, brain_mask_file)
L = list(brain_mask = brain_mask_file)
simg = reader(file)
submask = brain_mask
simg = simg * submask
}
temp = antsImageRead(template)
tempbrain = antsImageRead(template_brain)
tempmask = antsImageRead(template_mask)
outfiles = c(flipped = file.path(outdir, "N4corrected_Brain_LRflipped.nii.gz"),
lesion_mask = file.path(outdir, "Mask.lesion1_asym.nii.gz"))
L = c(L, as.list(outfiles))
if (all(file.exists(outfiles)) & cache) {
print_msg("Found existing asymmetry mask, loading...",
verbose = verbose)
asymmetry = lapply(outfiles, antsImageRead)
mask.lesion1 = asymmetry$lesion_mask
}
else {
asymmetry = asymmetry_mask(img = simg, brain_mask = submask,
reflaxis = reflaxis, sigma = sigma, verbose = verbose)
mask.lesion1 = asymmetry$mask
asymmetry = asymmetry$reflection
antsImageWrite(asymmetry$warpedmovout, outfiles["flipped"])
print_msg("Saving asymmetry mask...", verbose = verbose)
antsImageWrite(mask.lesion1, outfiles["lesion_mask"])
}
outfiles = c(prediction = file.path(outdir, "Prediction1.nii.gz"),
lesion_mask = file.path(outdir, "Mask.lesion2.nii.gz"))
o = as.list(outfiles)
names(o) = paste0(names(o), "_1")
L = c(L, as.list(o))
if (all(file.exists(outfiles)) & cache) {
print_msg("Found existing mask from 1st prediction, loading ...",
verbose = verbose)
out1 = lapply(outfiles, antsImageRead)
mask.lesion2 = out1$lesion_mask
prediction = out1$prediction
}
else {
print_msg("1st round of prediction...", verbose = verbose)
out1 = run_prediction(img = simg, brain_mask = submask,
template_mask = tempmask, voxel_resampling = voxel_resampling,
template_brain = tempbrain, typeofTransform = "SyN",
lesion_mask = mask.lesion1, reflaxis = reflaxis,
sigma = sigma, verbose = verbose)
prediction = out1$prediction
print_msg("Saving prediction...", verbose = verbose)
antsImageWrite(prediction, outfiles["prediction"])
mask.lesion2 = out1$lesion_mask
antsImageWrite(mask.lesion2, outfiles["lesion_mask"])
}
outfiles = c(prediction = file.path(outdir, "Prediction2.nii.gz"),
lesion_mask = file.path(outdir, "Mask.lesion3.nii.gz"))
o = as.list(outfiles)
names(o) = paste0(names(o), "_2")
L = c(L, as.list(o))
if (all(file.exists(outfiles)) & cache) {
print_msg("Found existing mask from 2nd prediction, loading ...",
verbose = verbose)
out2 = lapply(outfiles, antsImageRead)
mask.lesion3 = out2$lesion_mask
prediction2 = out2$prediction
}
else {
print_msg("2nd round of prediction...", verbose = verbose)
out2 = run_prediction(img = simg, brain_mask = submask,
template_mask = tempmask, voxel_resampling = voxel_resampling,
template_brain = tempbrain, typeofTransform = "SyN",
lesion_mask = mask.lesion2, reflaxis = reflaxis,
sigma = sigma, verbose = verbose)
prediction2 = out2$prediction
antsImageWrite(prediction2, outfiles["prediction"])
mask.lesion3 = out2$lesion_mask
antsImageWrite(mask.lesion3, outfiles["lesion_mask"])
}
print_msg("3rd round of prediction...", verbose = verbose)
outfiles = c(prediction = file.path(outdir, "Prediction3.nii.gz"),
lesion_mask = file.path(outdir, "Mask.lesion4.nii.gz"))
o = as.list(outfiles)
names(o) = paste0(names(o), "_3")
L = c(L, as.list(o))
out3 = run_prediction(img = simg, brain_mask = submask,
template_mask = tempmask, voxel_resampling = voxel_resampling,
template_brain = tempbrain, typeofTransform = "SyNCC",
lesion_mask = mask.lesion3, reflaxis = reflaxis,
sigma = sigma, verbose = verbose)
prediction3 = out3$prediction
antsImageWrite(prediction3, outfiles["prediction"])
mask.lesion4 = out3$lesion_mask
antsImageWrite(mask.lesion4, outfiles["lesion_mask"])
reg3 = out3$registration
reg_to_template = file.path(outdir, "Reg3_registered_to_template.nii.gz")
antsImageWrite(reg3$warpedfixout, reg_to_template)
L$reg_to_template = reg_to_template
reg_to_sub_warp = file.path(outdir, "Reg3_template_to_sub_warp.nii.gz")
file.copy(reg3$fwdtransforms[1], reg_to_sub_warp)
L$reg_to_sub_warp = reg_to_sub_warp
reg_to_sub_aff = file.path(outdir, "Reg3_template_to_sub_affine.mat")
file.copy(reg3$fwdtransforms[2], reg_to_sub_aff)
L$reg_to_sub_aff = reg_to_sub_aff
reg_to_temp_aff = file.path(outdir, "Reg3_sub_to_template_affine.mat")
L$reg_to_temp_aff = reg_to_temp_aff
file.copy(reg3$invtransforms[1], reg_to_temp_aff)
reg_to_temp_warp = file.path(outdir, "Reg3_sub_to_template_warp.nii.gz")
L$reg_to_temp_warp = reg_to_temp_warp
file.copy(reg3$invtransforms[2], reg_to_temp_warp)
seg = out3$segmentation
seg_file = file.path(outdir, "Prediction3_template.nii.gz")
L$segmentation = seg_file
antsImageWrite(seg, seg_file)
print_msg("Saving 3rd final prediction in native space...",
verbose = verbose)
segnative_file = file.path(outdir, "Prediction3_native.nii.gz")
segnative = out3$segmentation_native
L$segmentation_native = segnative_file
antsImageWrite(segnative, segnative_file)
graded_map = out3$multi_res_seg$probs[[1]][[4]]
grad_file = file.path(outdir, "Prediction3_graded_map.nii.gz")
antsImageWrite(graded_map, grad_file)
probles = resampleImage(graded_map, dim(tempbrain), useVoxels = 1,
interpType = 0)
problesnative = antsApplyTransforms(fixed = simg, moving = probles,
transformlist = reg3$fwdtransforms, interpolator = "Linear",
verbose = verbose > 1)
probles_file = file.path(outdir, "Prediction3_probability_template.nii.gz")
L$lesion_probability_template = probles_file
print_msg("Saving probabilistic prediction in template space...",
verbose = verbose)
antsImageWrite(probles, probles_file)
problesnative_file = file.path(outdir, "Prediction3_probability_native.nii.gz")
L$lesion_probability_native = problesnative_file
print_msg("Saving probabilistic prediction in native space...",
verbose = verbose)
antsImageWrite(problesnative, problesnative_file)
warppenn = reg_to_temp_warp
affpenn = reg_to_temp_aff
mni = system.file("extdata", "pennTemplate", "ch2.nii.gz",
package = "LINDA", mustWork = TRUE)
mni = antsImageRead(mni)
warpmni = system.file("extdata", "pennTemplate", "templateToCh2_1Warp.nii.gz",
package = "LINDA", mustWork = FALSE)
affmni = system.file("extdata", "pennTemplate", "templateToCh2_0GenericAffine.mat",
package = "LINDA", mustWork = FALSE)
if (saveMNI) {
if (!all(file.exists(warpmni, affmni))) {
print_msg("No MNI transformations available, registering de novo\n get full release to eleminate this step",
verbose = verbose)
temp_to_ch2 = antsRegistration(fixed = mni, moving = temp,
typeofTransform = "SyNCC", verbose = verbose >
1)
matrices = c(temp_to_ch2$fwdtransforms, affpenn,
warppenn)
}
else {
matrices = c(warpmni, affmni, affpenn, warppenn)
}
print_msg("Transferring data in MNI (ch2) space...",
verbose = verbose)
submni = antsApplyTransforms(moving = simg, fixed = mni,
transformlist = matrices, interpolator = "Linear",
whichtoinvert = c(0, 0, 1, 0), verbose = verbose >
1)
lesmni = antsApplyTransforms(moving = segnative,
fixed = mni, transformlist = matrices, interpolator = "NearestNeighbor",
whichtoinvert = c(0, 0, 1, 0), verbose = verbose >
1)
print_msg("Saving subject in MNI (ch2) space...",
verbose = verbose)
t1_template = file.path(outdir, "Subject_in_MNI.nii.gz")
antsImageWrite(submni, t1_template)
L$t1_template = t1_template
print_msg("Saving lesion in MNI (ch2) space...",
verbose = verbose)
lesion_template = file.path(outdir, "Lesion_in_MNI.nii.gz")
L$lesion_template = lesion_template
antsImageWrite(lesmni, lesion_template)
}
else {
print_msg("Skipping data transformation in MNI (ch2) ...",
verbose = verbose)
}
tic = Sys.time()
runtime = paste(round(as.double(difftime(tic, toc)),
1), units(difftime(tic, toc)))
print_msg(paste("Done!", runtime, "\n"), verbose = verbose)
}, split = TRUE, type = "output")
logFile = file.path(outdir, "Console_Output.txt")
writeLines(outputLog, logFile)
if ("devtools" %in% installed.packages()) {
thisenvironment = devtools::session_info("LINDA")
}
else {
thisenvironment = sessionInfo()
}
writeLines(suppressMessages(capture.output(thisenvironment)),
file.path(outdir, "Session_Info.txt"))
#return(L) #Commented out, because it serves no purpose in my wrapper.
}