Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This should work #16

Merged
merged 10 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: LINDA
Title: Lesion Identification with Neighborhood Data Analysis
Version: 0.3.2
Version: 0.4.0
Authors@R:
c(
person(given = "Dorian",
Expand All @@ -17,11 +17,14 @@ Authors@R:
Description: A neuroimaging toolkit for automatic segmentation
of chronic stroke lesions.
License: Apache License | file LICENSE
Depends: R (>= 2.10)
Imports:
ANTsR (>= 0.4.6),
ANTsRCore (>= 0.6.0.1),
randomForest,
magrittr
magrittr,
stats,
methods
Suggests:
covr,
knitr,
Expand Down
10 changes: 7 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Generated by roxygen2: do not edit by hand

export(LINDA.mrvnrfs.predict_chunks)
export(LINDA.mrvnrfs_chunks)
export(asymmetry_mask)
export(linda_mrvnrfs.predict_chunks)
export(linda_predict)
export(n4_skull_strip)
export(run_prediction)
import(randomForest)
importFrom(ANTsR,abpBrainExtraction)
importFrom(ANTsR,abpN4)
importFrom(ANTsR,composeTransformsToField)
importFrom(ANTsR,mrvnrfs)
importFrom(ANTsR,reflectImage)
importFrom(ANTsR,splitMask)
importFrom(ANTsRCore,antsApplyTransforms)
Expand All @@ -25,6 +25,10 @@ importFrom(ANTsRCore,lappend)
importFrom(ANTsRCore,makeImage)
importFrom(ANTsRCore,matrixToImages)
importFrom(ANTsRCore,resampleImage)
importFrom(ANTsRCore,resampleImageToTarget)
importFrom(ANTsRCore,smoothImage)
importFrom(ANTsRCore,thresholdImage)
importFrom(magrittr,"%>%")
importFrom(methods,formalArgs)
importFrom(stats,median)
importFrom(stats,predict)
22 changes: 17 additions & 5 deletions R/asymmetry_mask.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
asymmetry_mask = function(img,
brain_mask,
reflaxis = 0,
sigma = 2,
verbose = TRUE) {
#' Asymmetry Mask
#'
#' @param img an \code{antsImage} of T1 image
#' @param brain_mask an \code{antsImage} of brain mask
#' @param reflaxis Reflection axis
#' @param sigma Smoothing sigma
#' @param verbose print diagnostic messages
#'
#' @return A list of the reflection and the mask
#' @export
#'
asymmetry_mask = function(
img,
brain_mask,
reflaxis = 0,
sigma = 2,
verbose = TRUE) {
# compute asymmetry mask
print_msg("Computing asymmetry mask...", verbose = verbose)

Expand Down
26 changes: 0 additions & 26 deletions R/compose_warping.R

This file was deleted.

5 changes: 4 additions & 1 deletion R/getLesionFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ getLesionFeatures = function(
# kmask = bmask,
# verbose = verbose)$segmentation
feats[[1]] = (conavg - kmean) %>% iMath('Normalize')

# FEAT 2: gradient magnitude
feats[[2]] = img %>% iMath('Grad') %>% iMath('Normalize')

Expand All @@ -60,6 +59,7 @@ getLesionFeatures = function(
iMath(truncate, 0.01, 0.99) %>%
iMath('Normalize')


# FEAT 4: kmean
feats[[4]] = antsImageClone(kmean)

Expand All @@ -77,5 +77,8 @@ getLesionFeatures = function(
# FEAT 6: t1 itself
feats[[6]] = antsImageClone(img) # iMath(img,'Normalize')

names(feats) = c("d_controls", "d_controls_grad_mag",
"n4_d_controls", "kmean", "ref_diff",
"t1")
return(feats)
}
56 changes: 37 additions & 19 deletions R/linda_predict_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @importFrom ANTsRCore iMath antsImageRead antsImageWrite antsRegistration
#' @importFrom ANTsRCore resampleImage smoothImage thresholdImage antsImageClone
#' @importFrom ANTsRCore antsApplyTransforms is.antsImage
#' @importFrom ANTsRCore antsApplyTransforms is.antsImage resampleImageToTarget
#' @importFrom ANTsR abpN4 abpBrainExtraction reflectImage
#' @importFrom ANTsR composeTransformsToField splitMask
#' @importFrom magrittr %>%
Expand Down Expand Up @@ -290,52 +290,66 @@ linda_predict = function(

seg = out3$segmentation

L$segmentation = seg
seg_file = file.path(outdir, 'Prediction3_template.nii.gz')
L$segmentation = seg_file

antsImageWrite(seg, file.path(outdir, 'Prediction3_template.nii.gz'))
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

antsImageWrite(segnative,
file.path(outdir, 'Prediction3_native.nii.gz'))
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)

# save graded map
probles = resampleImage(graded_map,
dim(tempbrain),
useVoxels = 1,
interpType = 0)
# probles = resampleImageToTarget(
# image = graded_map,
# target = tempbrain,
# interpType = "nearestNeighbor")



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,
file.path(outdir,
'Prediction3_probability_template.nii.gz'))
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,
file.path(outdir,
'Prediction3_probability_native.nii.gz'))

antsImageWrite(problesnative, problesnative_file)


# save in MNI coordinates
print_msg("Transferring data in MNI (ch2) space...",
verbose = verbose)

warppenn = file.path(outdir, 'Reg3_sub_to_template_warp.nii.gz')
affpenn = file.path(outdir, 'Reg3_sub_to_template_affine.mat')

# warppenn = file.path(outdir, 'Reg3_sub_to_template_warp.nii.gz')
warppenn = reg_to_temp_warp
# affpenn = file.path(outdir, 'Reg3_sub_to_template_affine.mat')
affpenn = reg_to_temp_aff

mni = system.file("extdata", "pennTemplate", "ch2.nii.gz",
package = "LINDA",
Expand Down Expand Up @@ -391,11 +405,15 @@ linda_predict = function(

print_msg("Saving subject in MNI (ch2) space...",
verbose = verbose)
antsImageWrite(submni, file.path(outdir, 'Subject_in_MNI.nii.gz'))
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)
antsImageWrite(lesmni, file.path(outdir, 'Lesion_in_MNI.nii.gz'))
return(lesmni)

lesion_template = file.path(outdir, 'Lesion_in_MNI.nii.gz')
L$lesion_template = lesion_template
antsImageWrite(lesmni, lesion_template)
return(L)
}
Loading