-
Notifications
You must be signed in to change notification settings - Fork 18
/
corticalThickness.R
214 lines (194 loc) · 7.96 KB
/
corticalThickness.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
#' Cortical thickness using deep learning
#'
#' Perform KellyKapowski cortical thickness using \code{deepAtropos} for
#' segmentation. Description concerning implementaiton and evaluation:
#'
#' \url{https://www.medrxiv.org/content/10.1101/2020.10.19.20215392v1}
#'
#' @param t1 input 3-D unprocessed T1-weighted brain image
#' @param antsxnetCacheDirectory destination directory for storing the downloaded
#' template and model weights. Since these can be resused, if
#' \code{is.null(antsxnetCacheDirectory)}, these data will be downloaded to the
#' subdirectory ~/.keras/ANTsXNet/.
#' @param verbose print progress.
#' @return Cortical thickness image and segmentation probability images.
#' @author Tustison NJ
#' @examples
#' \dontrun{
#' library( ANTsRNet )
#' library( keras )
#'
#' image <- antsImageRead( "t1w_image.nii.gz" )
#' kk <- corticalThickness( image )
#' }
#' @export
corticalThickness <- function( t1, antsxnetCacheDirectory = NULL, verbose = FALSE )
{
if( t1@dimension != 3 )
{
stop( "Input image must be 3-D" )
}
atropos <- deepAtropos( t1, doPreprocessing = TRUE,
antsxnetCacheDirectory = antsxnetCacheDirectory, verbose = verbose )
# Kelly Kapowski cortical thickness
kkSegmentation <- antsImageClone(atropos$segmentationImage)
kkSegmentation[kkSegmentation == 4] <- 3
grayMatter <- atropos$probabilityImages[[3]]
whiteMatter <- atropos$probabilityImages[[4]] + atropos$probabilityImages[[5]]
kk <- kellyKapowski( s = kkSegmentation, g = grayMatter, w = whiteMatter,
its = 45, r = 0.025, m = 1.5, x = 0, verbose = verbose )
return( list(
thicknessImage = kk,
segmentationImage = atropos$segmentationImage,
csfProbabilityImage = atropos$probabilityImages[[2]],
grayMatterProbabilityImage = atropos$probabilityImages[[3]],
whiteMatterProbabilityImage = atropos$probabilityImages[[4]],
deepGrayMatterProbabilityImage = atropos$probabilityImages[[5]],
brainStemProbabilityImage = atropos$probabilityImages[[6]],
cerebellumProbabilityImage = atropos$probabilityImages[[7]]
) )
}
#' Longitudinal cortical thickness using deep learning
#'
#' Perform KellyKapowski cortical thickness longitudinally using \code{deepAtropos}
#' for segmentation of the derived single-subject template. It takes inspiration from
#' the work described here:
#'
#' \url{https://pubmed.ncbi.nlm.nih.gov/31356207/}
#'
#' @param t1s input list of 3-D unprocessed T1-weighted brain images from a single subject
#' @param initialTemplate input image to define the orientation of the SST. Can be a string
#' (see \code{getANTsXNetData}) or a specified template. This allows the user to create a
#' SST outside of this routine.
#' @param numberOfIterations Defines the number of iterations for refining the SST.
#' @param refinementTransform Transform for defining the refinement registration transform.
#' See options in \code{antsRegistration}.
#' @param antsxnetCacheDirectory destination directory for storing the downloaded
#' template and model weights. Since these can be resused, if
#' \code{is.null(antsxnetCacheDirectory)}, these data will be downloaded to the
#' subdirectory ~/.keras/ANTsXNet/.
#' @param verbose print progress.
#' @return List consisting of the SST, and a (sub-)list for each subject consisting of
#' the preprocessed image, cortical thickness image, segmentation probability images,
#' and affine mapping to the SST.
#' @author Tustison NJ, Avants BB
#' @examples
#' \dontrun{
#' library( ANTsRNet )
#' library( keras )
#'
#' image <- antsImageRead( "t1w_image.nii.gz" )
#' kk <- corticalThickness( image )
#' }
#' @export
longitudinalCorticalThickness <- function( t1s, initialTemplate = "oasis", numberOfIterations = 1,
refinementTransform = "antsRegistrationSyNQuick[a]", antsxnetCacheDirectory = NULL,
verbose = FALSE )
{
###################
#
# Initial SST + optional affine refinement
#
##################
sst <- NULL
if( is.character( initialTemplate ) )
{
templateFileNamePath <- getANTsXNetData( initialTemplate, antsxnetCacheDirectory = antsxnetCacheDirectory )
sst <- antsImageRead( templateFileNamePath )
} else {
sst <- initialTemplate
}
for( s in seq.int( numberOfIterations ) )
{
if( verbose )
{
cat( "Refinement iteration", s, "( out of", numberOfIterations, ")\n" )
}
sstTmp <- antsImageClone( sst ) * 0
for( i in seq.int( length( t1s ) ) )
{
if( verbose )
{
cat( "\n\n***************************" )
cat( "\n\nSST processing image", i, "( out of", length( t1s ), ")\n\n" )
cat( "***************************\n\n" )
}
transformType <- "antsRegistrationSyNQuick[r]"
if( s > 1 )
{
transformType <- refinementTransform
}
t1Preprocessed <- preprocessBrainImage( t1s[[i]], truncateIntensity = c( 0.01, 0.99 ),
brainExtractionModality = NULL, templateTransformType = transformType,
template = sst, doBiasCorrection = FALSE, returnBiasField = FALSE,
doDenoising = FALSE, intensityNormalizationType = "01",
antsxnetCacheDirectory = antsxnetCacheDirectory, verbose = verbose )
sstTmp <- sstTmp + t1Preprocessed$preprocessedImage
}
sst <- sstTmp / length( t1s )
}
###################
#
# Preprocessing and affine transform to final SST
#
##################
t1sPreprocessed <- list()
for( i in seq.int( length( t1s ) ) )
{
if( verbose )
{
cat( "\n\n***************************" )
cat( "\n\nFinal processing image", i, "( out of", length( t1s ), ")\n\n" )
cat( "***************************\n\n" )
}
t1sPreprocessed[[i]] <- preprocessBrainImage( t1s[[i]], truncateIntensity = c( 0.01, 0.99 ),
brainExtractionModality = "t1", templateTransformType = "antsRegistrationSyNQuick[a]",
template = sst, doBiasCorrection = TRUE, returnBiasField = FALSE,
doDenoising = TRUE, intensityNormalizationType = "01",
antsxnetCacheDirectory = antsxnetCacheDirectory, verbose = verbose )
}
###################
#
# Deep Atropos of SST for priors
#
##################
sstAtropos <- deepAtropos( sst, doPreprocessing = TRUE,
antsxnetCacheDirectory = antsxnetCacheDirectory, verbose = verbose )
###################
#
# Traditional Atropos + KK for each image
#
##################
returnList <- list()
for( i in seq.int( length( t1sPreprocessed ) ) )
{
if( verbose )
{
cat( "Atropos for image", i, "( out of", length( t1s ), ")" )
}
atroposOutput <- atropos( t1sPreprocessed[[i]]$preprocessedImage,
x = t1sPreprocessed[[i]]$brainMask, i = sstAtropos$probabilityImages[2:7],
m = "[0.1,1x1x1]", c = "[5,0]", priorweight = 0.5, p = "Socrates[1]",
verbose = verbose )
kkSegmentation <- antsImageClone(atroposOutput$segmentation)
kkSegmentation[kkSegmentation == 4] <- 3
grayMatter <- atroposOutput$probabilityimages[[2]]
whiteMatter <- atroposOutput$probabilityimages[[3]] + atroposOutput$probabilityimages[[4]]
kk <- kellyKapowski( s = kkSegmentation, g = grayMatter, w = whiteMatter,
its = 45, r = 0.025, m = 1.5, x = 0, verbose = verbose )
returnList[[i]] <- list(
preprocessedImage = t1sPreprocessed[[i]]$preprocessedImage,
thicknessImage = kk,
segmentationImage = atroposOutput$segmentation,
csfProbabilityImage = atroposOutput$probabilityimages[[1]],
grayMatterProbabilityImage = atroposOutput$probabilityimages[[2]],
whiteMatterProbabilityImage = atroposOutput$probabilityimages[[3]],
deepGrayMatterProbabilityImage = atroposOutput$probabilityimages[[4]],
brainStemProbabilityImage = atroposOutput$probabilityimages[[5]],
cerebellumProbabilityImage = atroposOutput$probabilityimages[[6]],
templateTransforms = t1sPreprocessed[[i]]$templateTransforms
)
}
returnList[["singleSubjectTemplate"]] <- sst
return( returnList )
}