|
a |
|
b/R/ich_segment.R |
|
|
1 |
#' @title Predict ICH Segmentation |
|
|
2 |
#' @description Will preprocess and predict the ICH voxels |
|
|
3 |
#' |
|
|
4 |
#' @param img CT image, object of class \code{nifti} or |
|
|
5 |
#' character filename |
|
|
6 |
#' @param mask binary brain mask, object of class \code{nifti} or |
|
|
7 |
#' character filename |
|
|
8 |
#' @param model model to use for prediction, |
|
|
9 |
#' either the random forest (rf) or logistic |
|
|
10 |
#' @param save_imgs Logical to save all images that are created as |
|
|
11 |
#' predictors |
|
|
12 |
#' @param outdir Output directory of saved images, needs to be set |
|
|
13 |
#' if \code{save_imgs = TRUE} |
|
|
14 |
#' @param stub Basename to write image names if \code{save_imgs = TRUE} |
|
|
15 |
#' @param verbose Print diagnostic output |
|
|
16 |
#' @param shiny Should shiny progress be called? |
|
|
17 |
#' @param roi Filename of ROI, which will be transformed |
|
|
18 |
#' @param erode_mask Should the brain mask be eroded? |
|
|
19 |
#' @param outfile filename for native-space, smoothed, |
|
|
20 |
#' thresholded image. |
|
|
21 |
#' @param ... Additional options passsed to \code{\link{ich_preprocess}} |
|
|
22 |
#' |
|
|
23 |
#' @return List of output prediction/probability images |
|
|
24 |
#' @export |
|
|
25 |
#' @importFrom fslr have.fsl |
|
|
26 |
ich_segment = function(img, |
|
|
27 |
..., |
|
|
28 |
verbose = TRUE, |
|
|
29 |
shiny = FALSE, |
|
|
30 |
model = c("rf", "logistic", "big_rf"), |
|
|
31 |
outfile = NULL) { |
|
|
32 |
|
|
|
33 |
model = match.arg(model) |
|
|
34 |
|
|
|
35 |
L = ich_process_predictors( |
|
|
36 |
img = img, |
|
|
37 |
..., |
|
|
38 |
verbose = verbose, |
|
|
39 |
roi = NULL) |
|
|
40 |
df = L$img.pred$df |
|
|
41 |
nim = L$img.pred$nim |
|
|
42 |
L$img.pred = NULL |
|
|
43 |
preprocess = L$preprocess |
|
|
44 |
rm(list = "L"); gc() |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
# data(MOD) |
|
|
48 |
############################################################## |
|
|
49 |
# Making prediction images |
|
|
50 |
############################################################## |
|
|
51 |
# grabbing the environment to extract exported stuff |
|
|
52 |
if (verbose) { |
|
|
53 |
msg = "# Running ich_predict" |
|
|
54 |
message(msg) |
|
|
55 |
} |
|
|
56 |
if (shiny) { |
|
|
57 |
shiny::setProgress(message = msg, value = 3/3 - 0.3) |
|
|
58 |
} |
|
|
59 |
L = ich_predict(df = df, |
|
|
60 |
nim = nim, |
|
|
61 |
model = model, |
|
|
62 |
native_img = img, |
|
|
63 |
native = TRUE, |
|
|
64 |
verbose = verbose, |
|
|
65 |
transformlist = preprocess$invtransforms, |
|
|
66 |
interpolator = preprocess$interpolator, |
|
|
67 |
shiny = shiny, |
|
|
68 |
outfile = outfile) |
|
|
69 |
L$preprocess = preprocess |
|
|
70 |
if (shiny) { |
|
|
71 |
shiny::setProgress(value = 3/3) |
|
|
72 |
} |
|
|
73 |
return(L) |
|
|
74 |
|
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
#' @rdname ich_segment |
|
|
79 |
#' @export |
|
|
80 |
ich_process_predictors = function( |
|
|
81 |
img, |
|
|
82 |
mask = NULL, |
|
|
83 |
save_imgs = FALSE, |
|
|
84 |
outdir = NULL, |
|
|
85 |
stub = NULL, |
|
|
86 |
verbose = TRUE, |
|
|
87 |
shiny = FALSE, |
|
|
88 |
roi = NULL, |
|
|
89 |
erode_mask = TRUE, |
|
|
90 |
...) { |
|
|
91 |
|
|
|
92 |
if (!have.fsl()) { |
|
|
93 |
stop("FSL Path Not Found!") |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
if (verbose) { |
|
|
97 |
msg = "# Processing The Data" |
|
|
98 |
message(msg) |
|
|
99 |
} |
|
|
100 |
if (shiny) { |
|
|
101 |
shiny::setProgress(message = msg, value = 0) |
|
|
102 |
} |
|
|
103 |
if (save_imgs) { |
|
|
104 |
if (is.character(img)) { |
|
|
105 |
if (is.null(stub)) { |
|
|
106 |
stub = paste0(nii.stub(img, bn = TRUE), "_reg_") |
|
|
107 |
} |
|
|
108 |
} |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
if (save_imgs) { |
|
|
112 |
stopifnot(!is.null(outdir)) |
|
|
113 |
stopifnot(!is.null(stub)) |
|
|
114 |
} |
|
|
115 |
if (is.null(outdir)) { |
|
|
116 |
outdir = tempdir() |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
trans_fname = trans_mask_fname = trans_roi_fname = "" |
|
|
120 |
if (save_imgs) { |
|
|
121 |
trans_fname = file.path(outdir, paste0(stub, "_", "image.nii.gz")) |
|
|
122 |
if (!is.null(roi)) { |
|
|
123 |
trans_roi_fname = file.path(outdir, paste0(stub, "_", "roi.nii.gz")) |
|
|
124 |
} else { |
|
|
125 |
trans_roi_fname = NULL |
|
|
126 |
} |
|
|
127 |
trans_mask_fname = file.path(outdir, paste0(stub, "_", "mask.nii.gz")) |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
fnames = c(trans_fname, trans_roi_fname, trans_mask_fname) |
|
|
131 |
|
|
|
132 |
if (!all(file.exists(fnames))) { |
|
|
133 |
# orig.img = img |
|
|
134 |
preprocess = ich_preprocess( |
|
|
135 |
img = img, |
|
|
136 |
mask = mask, |
|
|
137 |
verbose = verbose, |
|
|
138 |
shiny = shiny, |
|
|
139 |
roi = roi, |
|
|
140 |
...) |
|
|
141 |
|
|
|
142 |
timg = preprocess$transformed_image |
|
|
143 |
troi = preprocess$transformed_roi |
|
|
144 |
tmask = preprocess$transformed_mask > 0.5 |
|
|
145 |
|
|
|
146 |
if (save_imgs) { |
|
|
147 |
writenii(timg, trans_fname) |
|
|
148 |
if (!is.null(troi)) { |
|
|
149 |
writenii(troi, trans_roi_fname) |
|
|
150 |
} |
|
|
151 |
writenii(tmask, trans_mask_fname) |
|
|
152 |
} |
|
|
153 |
} else { |
|
|
154 |
timg = readnii(trans_fname) |
|
|
155 |
if (file.exists(trans_roi_fname)) { |
|
|
156 |
troi = readnii(trans_roi_fname) |
|
|
157 |
} else { |
|
|
158 |
troi = NULL |
|
|
159 |
} |
|
|
160 |
tmask = readnii(trans_mask_fname) |
|
|
161 |
preprocess = list( |
|
|
162 |
transformed_roi = troi, |
|
|
163 |
transformed_image = timg, |
|
|
164 |
transformed_mask = tmask |
|
|
165 |
) |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
|
|
|
169 |
L = list( |
|
|
170 |
preprocess = preprocess |
|
|
171 |
) |
|
|
172 |
rm(list = "preprocess"); gc() |
|
|
173 |
|
|
|
174 |
if (verbose) { |
|
|
175 |
msg = "# Making Predictors" |
|
|
176 |
message(msg) |
|
|
177 |
} |
|
|
178 |
if (shiny) { |
|
|
179 |
shiny::setProgress(message = msg, value = 1/3) |
|
|
180 |
} |
|
|
181 |
|
|
|
182 |
img.pred = make_predictors( |
|
|
183 |
timg, mask = tmask, |
|
|
184 |
roi = troi, |
|
|
185 |
save_imgs = save_imgs, |
|
|
186 |
stub = stub, |
|
|
187 |
outdir = outdir, |
|
|
188 |
verbose = verbose, |
|
|
189 |
shiny = shiny, |
|
|
190 |
erode_mask = erode_mask) |
|
|
191 |
L$img.pred = img.pred |
|
|
192 |
rm(list = "img.pred") |
|
|
193 |
gc() |
|
|
194 |
|
|
|
195 |
if (shiny) { |
|
|
196 |
shiny::setProgress(value = 2/3) |
|
|
197 |
} |
|
|
198 |
return(L) |
|
|
199 |
} |