[242173]: / R / ich_preprocess.R

Download this file

182 lines (170 with data), 5.4 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
#' @title Preprocess data for ICH Segmentation
#' @description Will do skull stripping and registration to the CT template
#'
#' @param img CT image, object of class \code{nifti} or
#' character filename
#' @param skull_strip Should the image be skull stripped? If not,
#' a mask must be specified. If mask specified, will override this
#' option
#' @param mask binary brain mask, object of class \code{nifti} or
#' character filename
#' @param robust If skull stripping, should
#' \code{\link{CT_Skull_Strip_robust}}
#' be used vs. \code{\link{CT_Skull_Strip}}
#' @param template.file Template to register to (Skull stripped template)
#' @param outprefix Passed to \code{\link[extrantsr]{registration}} for
#' the output transformations
#' @param typeofTransform Transformation to the template
#' @param interpolator Interpolation to be done after transformation
#' @param outfile Output filename for transformed registered image
#' @param verbose Print diagnostic output
#' @param shiny Should shiny progress be called?
#' @param ... Additional options passsed to \code{\link{CT_Skull_Strip_robust}}
#' or \code{\link{CT_Skull_Strip}}
#' @param roi Filename of ROI, which will be transformed
#' @param n4_correct Should N4 bias-field correction be done after
#' skull-stripping
#' @param ss.template.file Template file to pass to
#' \code{\link{CT_Skull_Strip_robust}} robust registration/neck removal
#' @param ss.template.mask Template mask to pass to
#' \code{\link{CT_Skull_Strip_robust}} robust registration/neck removal
#'
#'
#' @return List of output images and transformations
#' @importFrom neurobase check_nifti mask_img window_img
#' @importFrom extrantsr registration ants_apply_transforms bias_correct
#' @export
ich_preprocess = function(
img,
skull_strip = TRUE,
robust = TRUE,
mask = NULL,
n4_correct = FALSE,
template.file = system.file(
"scct_unsmooth_SS_0.01.nii.gz",
package = "ichseg"),
outprefix = NULL,
typeofTransform = c("Rigid", "Affine"),
interpolator = "Linear",
outfile = NULL,
verbose = TRUE,
shiny = FALSE,
roi = NULL,
ss.template.file =
system.file("scct_unsmooth_SS_0.01.nii.gz",
package = "ichseg"),
ss.template.mask =
system.file("scct_unsmooth_SS_0.01_Mask.nii.gz",
package = "ichseg"),
...) {
if (skull_strip & is.null(mask)) {
if (shiny) {
shiny::incProgress(message = "Skull Stripping Image")
}
if (robust) {
ss = CT_Skull_Strip_robust(img, retimg = TRUE, ...,
template.file = ss.template.file,
template.mask = ss.template.mask)
} else {
ss = CT_Skull_Strip(img, retimg = TRUE, ...)
}
mask = ss > 0
} else {
stopifnot(!is.null(mask))
}
mask = check_nifti(mask)
mask = mask > 0
img = check_nifti(img)
ss = mask_img(img, mask)
ss = window_img(ss, window = c(0, 100), replace = "zero")
if (n4_correct) {
if (verbose) {
msg = "# Running N4 Correction"
message(msg)
}
n4_img = extrantsr::bias_correct(
ss, correction = "N4",
mask = mask,
verbose = verbose > 1)
ss = n4_img
}
if (is.null(outprefix)) {
outprefix = tempfile()
}
msg = "# Rigid-Body Registration"
if (verbose) {
message(msg)
}
if (shiny) {
shiny::incProgress(message = msg)
}
typeofTransform = match.arg(typeofTransform)
res = registration(
filename = ss,
skull_strip = FALSE,
correct = FALSE,
outfile = outfile,
retimg = TRUE,
typeofTransform = typeofTransform,
template.file = template.file,
interpolator = interpolator,
remove.warp = FALSE,
outprefix = outprefix,
verbose = verbose > 1)
omask = ants_apply_transforms(fixed = template.file,
moving = mask,
# typeofTransform = typeofTransform,
interpolator = interpolator,
transformlist = res$fwdtransforms)
if (!is.null(roi)) {
roi_interpolator = "genericLabel"
oroi = ants_apply_transforms(fixed = template.file,
moving = roi,
# typeofTransform = typeofTransform,
interpolator = roi_interpolator,
transformlist = res$fwdtransforms)
res$roi_interpolator = roi_interpolator
} else {
oroi = NULL
}
res$mask = mask
res$transformed_roi = oroi
res$ss_image = ss
res$transformed_image = res$outfile
res$transformed_mask = omask
res$outfile = NULL
return(res)
}
#' @export
#' @rdname ich_preprocess
ich_cnn_preprocess = function(
...,
template.file = system.file(
"scct_unsmooth_SS_0.01_128x128x128.nii.gz",
package = "ichseg")
) {
args = list(...)
nargs = names(args)
if ("skull_strip" %in% nargs) {
skull_strip = args$robust
if (!skull_strip) {
warning("ich_cnn_preprocess needs skull_strip = TRUE")
}
args$skull_strip = TRUE
}
if ("robust" %in% nargs) {
robust = args$robust
if (!robust) {
warning("ich_cnn_preprocess needs robust = TRUE")
}
args$robust = TRUE
}
args$template.file = template.file
args$smooth_before_threshold = TRUE
args$smooth.factor = 1
args$remove.neck = TRUE
args$recog = FALSE
args$nvoxels = 0
res = do.call(ich_preprocess, args = args)
return(res)
}