ANTsX / ANTsRCore

Rcpp bindings for the C++ ANTs library used by the ANTsR package
9 stars 9 forks source link

ApplyTransforms with Unsigned Char #120

Open muschellij2 opened 4 years ago

muschellij2 commented 4 years ago

Describe the bug Segfault with unsigned char with applyTransforms.

To Reproduce

library(ANTsRCore)
#> 
#> Attaching package: 'ANTsRCore'
#> The following objects are masked from 'package:stats':
#> 
#>     sd, var
#> The following objects are masked from 'package:base':
#> 
#>     all, any, apply, max, min, prod, range, sum
destfile = file.path(tempdir(), "01.tar.xz")
dl = curl::curl_download(
  "https://archive.data.jhu.edu/api/access/datafile/1311?gbrecs=true",
  destfile = destfile)
res = untar(tarfile = destfile, exdir = tempdir())
image = file.path(tempdir(), "01", "BRAIN_1_Anonymized.nii.gz")
mask = file.path(tempdir(), "01", "BRAIN_1_Anonymized_Mask.nii.gz")

template.file = tempfile(fileext = ".nii.gz")
# template.file = system.file(
#   'scct_unsmooth_SS_0.01_128x128x128.nii.gz',
#   package = 'ichseg')
template_url = paste0("https://github.com/muschellij2/ichseg",
  "/raw/master/inst/",
  "scct_unsmooth_SS_0.01_128x128x128.nii.gz")
curl::curl_download(template_url, template.file)
image = check_ants(image)
mask = check_ants(mask)
ss = maskImage(image, mask)

template.file = check_ants(template.file)

reg = antsRegistration(fixed = template.file,
                       moving = ss,
                       typeofTransform = "Rigid",
                       affSampling = 64,
                       verbose = TRUE)
# works fine
native_template = antsApplyTransforms(
  fixed = ss,
  moving = template.file,
  interpolator = "nearestNeighbor",
  transformlist = reg$invtransforms,
  whichtoinvert = 1)

temp_space = reg$warpedmovout > 50
unique(temp_space)
#> [1] 0 1
temp_space
#> antsImage
#>   Pixel Type          : unsigned char 
#>   Components Per Pixel: 1 
#>   Dimensions          : 128x128x128 
#>   Voxel Spacing       : 1.5x1.5x1.5 
#>   Origin              : 90 125 -71 
#>   Direction           : -1 0 0 0 -1 0 0 0 1

# works fine
temp_space2 = antsImageClone(temp_space, "float")
native_template = antsApplyTransforms(
  fixed = ss,
  moving = temp_space2,
  interpolator = "nearestNeighbor",
  transformlist = reg$invtransforms,
  whichtoinvert = 1)

Failures

# segfaults
temp_space2 = antsImageClone(temp_space, "float")
native_template = antsApplyTransforms(
  fixed = ss,
  moving = temp_space,
  interpolator = "nearestNeighbor",
  transformlist = reg$invtransforms,
  whichtoinvert = 1)

 *** caught segfault ***
 *** caught segfault ***

address 0x1124e2000, cause 'memory not mapped'
address 0x112562000, cause 'memory not mapped'

Traceback:
 1: antsApplyTransforms(fixed = ss, moving = temp_space, interpolator = "nearestNeighbor",     transformlist = reg$invtransforms, whichtoinvert = 1)
Traceback:
 1: antsApplyTransforms(fixed = ss, moving = temp_space, interpolator = "nearestNeighbor", 
 *** caught segfault ***

 *** caught segfault ***    transformlist = reg$invtransforms, whichtoinvert = 1)

address 0x1125e2000, cause 'memory not mapped'
address 0x112462000, cause 'memory not mapped'

Created on 2020-07-09 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.0 (2020-04-24) #> os macOS Mojave 10.14.6 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2020-07-09 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> ANTsRCore * 0.7.4.6 2020-07-07 [1] Github (muschellij2/ANTsRCore@61c37a1) #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0) #> backports 1.1.8 2020-06-17 [1] CRAN (R 4.0.0) #> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.0) #> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0) #> curl 4.3 2019-12-02 [1] CRAN (R 4.0.0) #> desc 1.2.0 2020-06-01 [1] Github (muschellij2/desc@b0c374f) #> devtools 2.3.0.9000 2020-06-01 [1] Github (hadley/devtools@202ea81) #> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0) #> fs 1.4.2 2020-06-30 [1] CRAN (R 4.0.0) #> glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.0) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0) #> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.0) #> ITKR 0.5.3.2.0 2020-06-01 [1] Github (stnava/ITKR@9bdd5f8) #> knitr 1.29.1 2020-07-06 [1] Github (muschellij2/knitr@538cd65) #> lattice 0.20-41 2020-04-02 [1] CRAN (R 4.0.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0) #> Matrix 1.2-18 2019-11-27 [1] CRAN (R 4.0.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0) #> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 4.0.0) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0) #> processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.0) #> ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.0) #> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0) #> Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.0) #> RcppEigen 0.3.3.7.0 2019-11-16 [1] CRAN (R 4.0.0) #> remotes 2.1.1 2020-02-15 [1] CRAN (R 4.0.0) #> rlang 0.4.6.9000 2020-07-08 [1] Github (r-lib/rlang@f3cdf54) #> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0) #> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0) #> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0) #> usethis 1.6.1.9000 2020-07-06 [1] Github (r-lib/usethis@c1cb6d9) #> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0) #> xfun 0.15 2020-06-21 [1] CRAN (R 4.0.0) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library ```
muschellij2 commented 4 years ago

Also, this works fine as well:

# works fine
noinvert = antsApplyTransforms(
  fixed = template.file,
  moving = antsImageClone(ss > 0, "float"),
  interpolator = "nearestNeighbor",
  transformlist = reg$fwdtransforms)

but this segfaults (so it's not related to whichtoinvert):

# fails
noinvert = antsApplyTransforms(
  fixed = template.file,
  moving = ss > 0,
  interpolator = "nearestNeighbor",
  transformlist = reg$fwdtransforms)