ANTsX / ANTsR

R interface to the ANTs biomedical image processing library
https://antsx.github.io/ANTsR
Apache License 2.0
127 stars 35 forks source link

abpBrainExtraction fails #315

Closed vnckppl closed 4 years ago

vnckppl commented 4 years ago

Issue I am running abpBrainExtraction on a T1-weighted image that has previously been processed with abpN4. I use OASIS-30 data as template and mask.

I run the following code:

# Load template image and mask
tem <- antsImageRead(
  "/software/templates/OASIS-30/T_template0.nii.gz"
)
temmask <- antsImageRead(
  "/software/templates/OASIS-30/T_template0_BrainCerebellumMask.nii.gz"
)

# Load T1w data
myT1 <- antsImageRead("/data/in/T1w_n4.nii.gz")

# Build brain mask
mask <- abpBrainExtraction(
  img = myT1,
  tem = tem,
  temmask = temmask,
  tdir = "/data/in"
)

This results in:

source("example.R")
 bad det -1 v 1 u -1
 bad det -1 v 1 u -1 new 1
 bad det -1 v 1 u -1
 bad det -1 v 1 u -1 new 1

 *** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: antsImageHeaderInfo(filename)
 2: doTryCatch(return(expr), name, parentenv, handler)
 3: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 4: tryCatchList(expr, classes, parentenv, handlers)
 5: tryCatch(antsImageHeaderInfo(filename), error = function(e) return(NA))
 6: antsImageRead(x, ...)
 7: FUN(X[[i]], ...)
 8: FUN(X[[i]], ...)
 9: lapply(x, check_ants, ... = ...)
10: FUN(X[[i]], ...)
11: FUN(X[[i]], ...)
12: lapply(x, check_ants, ... = ...)
13: check_ants(image)
14: check_ants(image)
15: antsImageWrite(mask, "/data/in/mymask.nii.gz")
16: eval(ei, envir)
17: eval(ei, envir)
18: withVisible(eval(ei, envir))
19: source("example.R")

OS and Software versions:

dorianps commented 4 years ago

Looks to me like you don't have enough memory/ram. What are the specs of your system.

On Mon, May 11, 2020, 1:23 PM vnckppl notifications@github.com wrote:

Issue I am running abpBrainExtraction on a T1-weighted image that has previously been processed with abpN4. I use OASIS-30 data as template and mask.

I run the following code:

Load template image and mask

tem <- antsImageRead( "/software/templates/OASIS-30/T_template0.nii.gz" ) temmask <- antsImageRead( "/software/templates/OASIS-30/T_template0_BrainCerebellumMask.nii.gz" )

Load T1w data

myT1 <- antsImageRead("/data/in/T1w_n4.nii.gz")

Build brain mask

mask <- abpBrainExtraction( img = myT1, tem = tem, temmask = temmask, tdir = "/data/in" )

This results in:

source("example.R") bad det -1 v 1 u -1 bad det -1 v 1 u -1 new 1 bad det -1 v 1 u -1 bad det -1 v 1 u -1 new 1

caught segfault address (nil), cause 'memory not mapped'

Traceback: 1: antsImageHeaderInfo(filename) 2: doTryCatch(return(expr), name, parentenv, handler) 3: tryCatchOne(expr, names, parentenv, handlers[[1L]]) 4: tryCatchList(expr, classes, parentenv, handlers) 5: tryCatch(antsImageHeaderInfo(filename), error = function(e) return(NA)) 6: antsImageRead(x, ...) 7: FUN(X[[i]], ...) 8: FUN(X[[i]], ...) 9: lapply(x, check_ants, ... = ...) 10: FUN(X[[i]], ...) 11: FUN(X[[i]], ...) 12: lapply(x, check_ants, ... = ...) 13: check_ants(image) 14: check_ants(image) 15: antsImageWrite(mask, "/data/in/mymask.nii.gz") 16: eval(ei, envir) 17: eval(ei, envir) 18: withVisible(eval(ei, envir)) 19: source("example.R")

OS and Software versions:

  • OS: Debian GNU/Linux bullseye/sid"
  • R: R version 3.6.3
  • ITKR_0.5.3
  • ANTsR_0.5.4.2
  • ANTsRCore_0.7.4

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFJU7K5UKPBFTA6NWLPAL3RRAYAPANCNFSM4M6DCTPQ .

vnckppl commented 4 years ago

I run R in a Docker container on OSX. The system itself has 16gb of ram, with 10gb allocated to Docker and 1Gb Swap. I think this should be sufficient, but correct me if I am wrong. I even upped the swap to 2gb and re-ran the script, but I get the exact same errors.

dorianps commented 4 years ago

Then I don't know what can be. Have you properly mounted the folders in /software and /data/in/ ? Looks like it's failing when trying to read some image headers.

On Mon, May 11, 2020, 2:03 PM vnckppl notifications@github.com wrote:

I run R in a Docker container on OSX. The system itself has 16gb of ram, with 10gb allocated to Docker and 1Gb Swap. I think this should be sufficient, but correct me if I am wrong. I even upped the swap to 2gb and re-ran the script, but I get the exact same errors.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315#issuecomment-626861851, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFJU7JDFS4JDVVYMJIPLEDRRA4VTANCNFSM4M6DCTPQ .

vnckppl commented 4 years ago

I think I have. When I manually enter the container with docker run -it -v /some/local/folder:/data/in --entrypoint=/bin/bash someContainerID I can read and write to the local folder without any issues. Also, when I run R from within the container, I have no issues reading and writing images. Moreover, apbN4, antsRegistration, and ants_brain_extract (ANTsRNet) all run fine from within the container.

muschellij2 commented 4 years ago

Can you try n4BiasCorrection, with and without a mask, and see if that works? Also - please confirm that "/data/in/mymask.nii.gz" does not currently exist. Best, John

On Mon, May 11, 2020 at 2:22 PM vnckppl notifications@github.com wrote:

I think I have. When I manually enter the container with docker run -it -v /some/local/folder:/data/in --entrypoint=/bin/bash I can read and write to the local folder without any issues. Also, when I run R from within the container, I have no issues reading and writing images. Moreover, apbN4, antsRegistration, and ants_brain_extract (ANTsRNet) all run fine from within the container.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315#issuecomment-626872319, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLX55VZ4FZBQK2HSZT3RRA65HANCNFSM4M6DCTPQ .

vnckppl commented 4 years ago

Starting the container with:

docker run -it \
       -v /some/local/input/folder:/data/in:ro \
       -v /some/local/output/folder:/data/out \
       --entrypoint=/bin/bash \
       someContainerID

and then running R, sourcing the following code:

## * Load Library
library("ANTsR")

## * Load data
base <- "/data/in"

myt1w <- antsImageRead(
  file.path(base, "T1w_n4.nii.gz")
)
myt1w_mask <- antsImageRead(
  file.path(base, "T1w_n4_mask.nii.gz")
)

## * Run N4 correction on entire image
# Ouptut folder
out_dir <- "/data/out/N4_noMask"
dir.create(out_dir)
# Run N4
output_image <- n4BiasFieldCorrection(myt1w, verbose = TRUE)
# Save output file
outfile <- file.path(out_dir, "N4_noMask.nii.gz")
antsImageWrite(output_image, outfile)

## * Run N4 correction within brain mask
# Ouptut folder
out_dir <- "/data/out/N4_withinMask"
dir.create(out_dir)
# Run N4
output_image <- n4BiasFieldCorrection(myt1w, myt1w_mask, verbose = TRUE)
# Save output file
outfile <- file.path(out_dir, "N4_withinMask.nii.gz")
antsImageWrite(output_image, outfile)

runs fine and produces the two expected N4 corrected images.

muschellij2 commented 4 years ago

Sorry - I mean N4 passing in the mask, such as n4BiasFieldCorrection(img, mask = mask)

Best, John

On Mon, May 11, 2020 at 3:44 PM vnckppl notifications@github.com wrote:

Starting the container with:

docker run -it \ -v /some/local/input/folder:/data/in:ro \ -v /some/local/output/folder:/data/out \ --entrypoint=/bin/bash \ someContainerID

and then running R, sourcing the following code:

* Load Library

library("ANTsR")

* Load mask data

base <- "/data/in"

myt1w <- antsImageRead( file.path(base, "T1w_n4.nii.gz") ) myt1w_mask <- antsImageRead( file.path(base, "T1w_n4_mask.nii.gz") )

* Run N4 correction on entire image

Ouptut folder

out_dir <- "/data/out/N4_noMask" dir.create(out_dir)

Run N4

output_image <- n4BiasFieldCorrection(myt1w, verbose = TRUE)

Save output file

outfile <- file.path(out_dir, "N4_noMask.nii.gz") antsImageWrite(output_image, outfile)

* Run N4 correction on entire image

Ouptut folder

out_dir <- "/data/out/N4_withinMask" dir.create(out_dir)

Run N4

output_image <- n4BiasFieldCorrection(myt1w, verbose = TRUE)

Save output file

outfile <- file.path(out_dir, "N4_withinMask.nii.gz") antsImageWrite(output_image, outfile)

runs fine and produces the two expected N4 corrected images.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315#issuecomment-626917997, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLXKMD44BZOMAAGVBDTRRBISNANCNFSM4M6DCTPQ .

vnckppl commented 4 years ago

Hi John, I did (and have updated my previous post). Same results, i.e., this runs smoothly.

muschellij2 commented 4 years ago

Only way likely is to share the image. Best, John

On Mon, May 11, 2020 at 4:12 PM vnckppl notifications@github.com wrote:

Hi John, I did (and have updated my previous post). Same results, i.e., this runs smoothly.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315#issuecomment-626932961, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLVN5QJ7BZWNVKR74HLRRBL2BANCNFSM4M6DCTPQ .

vnckppl commented 4 years ago

I am getting the same error with a default template that comes with FSL, so I guess the image itself is not the problem. I am further investigating the issue. I am building a virtual environment with ANTsR using Packrat so that I can try and see if a different setup produces the same error. However, I am running into some other issues with install_github("stnava/ANTsR") for which I have opened another thread here.

vnckppl commented 4 years ago

I figured out what went wrong. I was expecting abpBrainExtraction to write out an antsImage, but it is writing out a list. When I tried to write out the list as an antsImage, R throws the memory error:

 *** caught segfault ***
address (nil), cause 'memory not mapped'

Everything works now, and the masks look good. For some image, I do see this warning/error in the terminal though:

 bad det -1 v -1 u 1
 bad det -1 v -1 u 1 new 1
 bad det -1 v -1 u 1
 bad det -1 v -1 u 1 new 1

Should I be worried about that?

dorianps commented 4 years ago

That warning happens often but I haven't seen bad results when it appears.

On Tue, May 12, 2020, 1:04 PM vnckppl notifications@github.com wrote:

Closed #315 https://github.com/ANTsX/ANTsR/issues/315.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/315#event-3328462800, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFJU7IF4M7PL7XS5DFCUXTRRF6QVANCNFSM4M6DCTPQ .

cookpa commented 4 years ago

Those errors come from antsAI. It is a multi-start optimizer that tries a bunch of rotations and translations to initialize registrations. Some of them will be so far off from the right solution that they fail. If you see hundreds of them, it might be a problem, but a few are expected.